@@ -0,0 +1,1922 @@
+import os%0Aimport zipfile%0Aimport subprocess%0A%0Afrom tqdm import *%0Aimport pandas as pd%0A%0A%0Adef create_dir(dir_path):%0A    if not os.path.isdir(dir_path):%0A        os.makedirs(dir_path)%0A    return%0A%0A%0Aparent_dir = os.path.dirname(os.getcwd())%0Adata_dir = os.path.join(parent_dir, %22data%22, %22Asgeir UCSF%22, %22Zipped DICOM%22)%0Aunzipped_dir = os.path.join(parent_dir, %22data%22, %22Unzipped%22)%0Adecompressed_dir = os.path.join(parent_dir, %22data%22, %22Dicompressed%22)%0A%0Acreate_dir(unzipped_dir)%0Acreate_dir(decompressed_dir)%0A%0A'''%0AUnzip file into folder%0A'''%0A%0A# zips_list = os.listdir(data_dir)%0A# for z in zips_list:%0A#     z_name = z.split(%22.%22)%5B0%5D%0A#     z_path = os.path.join(data_dir, z)%0A#     z2path = os.path.join(unzipped_dir, z_name)%0A#     print(%22Preprocessing on %22 + z_path)%0A#     try:%0A#         with zipfile.ZipFile(z_path, %22r%22) as zip_ref:%0A#             zip_ref.extractall(z2path)%0A#     except:%0A#         print(%22  Cannot unzip: %22 + z_path)%0A%0A%0A'''%0ADecompress DICOM files%0A'''%0A%0Asubjects = %5B%5D%0Ascans = %5B%5D%0Aundecomps = %5B%5D%0A%0Afor d in tqdm(os.listdir(unzipped_dir)):%0A    sub_d = os.path.join(unzipped_dir, d)%0A    subsub_d = os.path.join(sub_d, os.listdir(sub_d)%5B0%5D)%0A    for scan in os.listdir(subsub_d):%0A        input_scan_dir = os.path.join(subsub_d, scan)%0A        output_scan_dir = os.path.join(decompressed_dir, d, scan)%0A        create_dir(output_scan_dir)%0A        undecomp = 0%0A        for dcm in os.listdir(input_scan_dir):%0A            input_dcm = os.path.join(input_scan_dir, dcm)%0A            output_dcm = os.path.join(output_scan_dir, dcm)%0A            try:%0A                command = %5B%22gdcmconv%22, %22-w%22, input_dcm, output_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)%0A

Decompress DICOM file