import os
import zipfile
import subprocess

from tqdm import *
import pandas as pd


def create_dir(dir_path):
    if not os.path.isdir(dir_path):
        os.makedirs(dir_path)
    return


parent_dir = os.path.dirname(os.getcwd())
data_dir = os.path.join(parent_dir, "data", "Asgeir UCSF", "Zipped DICOM")
unzipped_dir = os.path.join(parent_dir, "data", "Unzipped")
decompressed_dir = os.path.join(parent_dir, "data", "Dicompressed")

create_dir(unzipped_dir)
create_dir(decompressed_dir)

'''
Unzip file into folder
'''

# zips_list = os.listdir(data_dir)
# for z in zips_list:
#     z_name = z.split(".")[0]
#     z_path = os.path.join(data_dir, z)
#     z2path = os.path.join(unzipped_dir, z_name)
#     print("Preprocessing on " + z_path)
#     try:
#         with zipfile.ZipFile(z_path, "r") as zip_ref:
#             zip_ref.extractall(z2path)
#     except:
#         print("  Cannot unzip: " + z_path)


'''
Decompress DICOM files
'''

subjects = []
scans = []
undecomps = []

for d in tqdm(os.listdir(unzipped_dir)):
    sub_d = os.path.join(unzipped_dir, d)
    subsub_d = os.path.join(sub_d, os.listdir(sub_d)[0])
    for scan in os.listdir(subsub_d):
        input_scan_dir = os.path.join(subsub_d, scan)
        output_scan_dir = os.path.join(decompressed_dir, d, scan)
        create_dir(output_scan_dir)
        undecomp = 0
        for dcm in os.listdir(input_scan_dir):
            input_dcm = os.path.join(input_scan_dir, dcm)
            output_dcm = os.path.join(output_scan_dir, dcm)
            try:
                command = ["gdcmconv", "-w", input_dcm, output_dcm]
                subprocess.call(command)
            except:
                undecomp += 1
                continue
        subjects.append(d)
        scans.append(scan)
        undecomps.append(undecomp)

df = pd.DataFrame(data={"subject": subjects, "scans": scan, "log": undecomps})
df.to_csv("decompress_logs.csv", index=False)

@@ -40,47 +40,51 @@
 ess%0A
-%0A
 from 
-tqdm import *%0Aimport pandas as pd
+multiprocessing import Pool, cpu_count
 %0A%0A%0Ad
@@ -946,48 +946,289 @@
 ''%0A%0A
-subjects = %5B%5D%0Ascans
+%0Adef unwarp_decompress(arg, **kwarg):%0A    return decompress(*arg, **kwarg)%0A%0A%0Adef decompress(input_path, output_path):%0A    try:%0A        command
  = %5B
-%5D%0Aundecomp
+%22gdcmconv%22, %22-w%22, input_path, output_path%5D%0A        subprocess.call(command)%0A    except:%0A        pass%0A%0A%0Ainput_dcms, output_dcm
 s = %5B%5D
-%0A
+, %5B%5D
 %0Afor
@@ -1237,13 +1237,8 @@
  in 
-tqdm(
 os.l
@@ -1257,17 +1257,16 @@
 ped_dir)
-)
 :%0A    su
@@ -1556,29 +1556,8 @@
 ir)%0A
-        undecomp = 0%0A
     
@@ -1616,27 +1616,33 @@
    input_dcm
- = 
+s.append(
 os.path.join
@@ -1654,32 +1654,33 @@
 t_scan_dir, dcm)
+)
 %0A            out
@@ -1686,19 +1686,25 @@
 tput_dcm
- = 
+s.append(
 os.path.
@@ -1733,71 +1733,23 @@
 dcm)
-%0A            try:%0A                command = %5B%22gdcmconv%22, %22-w%22, 
+)%0A%0Aparas = zip(
 inpu
@@ -1753,16 +1753,17 @@
 nput_dcm
+s
 , output
@@ -1770,337 +1770,77 @@
 _dcm
-%5D%0A                subprocess.call(command)%0A            except:%0A                undecomp += 1%0A                continue%0A        subjects.append(d)%0A        scans.append(scan)%0A        undecomps.append(undecomp)%0A%0Adf = pd.DataFrame(data=%7B%22subject%22: subjects, %22scans%22: scan, %22log%22: undecomps%7D)%0Adf.to_csv(%22decompress_logs.csv%22, index=False
+s)%0Apool = Pool(processes=cpu_count())%0Apool.map(unwarp_decompress, paras
 )%0A

Add multiprocessing