-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreating_manifests_transfer_to_bucket.py
More file actions
124 lines (110 loc) · 4.37 KB
/
creating_manifests_transfer_to_bucket.py
File metadata and controls
124 lines (110 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Creating both imaging and clinical manifests and transferring files to bucket
Run the script as follows: python creating_manifests_transfer_to_bucket.py PATH_TO_IMAGES or PATH_TO_SUBMISSION_TSVS
Author: Maryam Vazirabad
Updated: 3/24/2023
"""
import sys
import pandas as pd
from pathlib import Path
import os
import glob
import numpy as np
import tqdm
from tqdm import tqdm
import hashlib
import pydicom
def ask_user(directory_path):
user_date = int(input("Enter the intended date for the manifest (ex: 20220101): "))
user_select = int(input("Does the inputted directory have images or submission TSVs? Enter 0 for images, "
"1 for submission TSVs: "))
try:
if user_select == 0:
s_url = 's3://storage.ir.rsna.ai/replicated-data-rsna/RSNA_' + str(user_date) + '/data/'
file_type = 'dcm'
create_data_manifest(s_url, directory_path, file_type, 'imaging', str(user_date), user_select)
elif user_select == 1:
s_url = 's3://storage.ir.rsna.ai/replicated-data-rsna/RSNA_' + str(user_date) + '/'
# file_type = 'tsv'
create_clinical_manifest(s_url, directory_path, 'clinical', str(user_date))
else:
return ask_user(directory_path)
except Exception as error:
print("Unexpected error")
print(error)
return ask_user(directory_path)
def create_clinical_manifest(s_url, directory_path, title, date):
text_files = glob.glob(str(directory_path) + '/*')
images_path = os.listdir(str(directory_path))
numpy_array = np.array(images_path)
df = pd.DataFrame(numpy_array, columns=['file_name'])
hashes = []
file_size = []
acl = []
url = []
for i in tqdm(range(len(images_path))):
md5_hash = hashlib.md5()
a_file = open(text_files[i], "rb")
content = a_file.read()
md5_hash.update(content)
digest = md5_hash.hexdigest()
hashes.append(digest)
acl.append('Open-R1')
file_size.append(os.path.getsize(text_files[i]))
url.append(s_url + images_path[i])
df['md5sum'] = np.array(hashes)
df['acl'] = np.array(acl)
df['storage_urls'] = np.array(url)
df['file_size'] = np.array(file_size)
new_manifest = 'clinical_manifest_RSNA_' + date + '.tsv'
df.to_csv(new_manifest, sep='\t', index=False)
def create_data_manifest(s_url, directory_path, file_type, title, date, user_select):
images_path = glob.glob(str(directory_path) + "/**/*.dcm", recursive=True)
df = pd.DataFrame()
hashes = []
file_size = []
acl = []
url = []
case_ids = []
study_uids = []
series_uids = []
instance_uids = []
modality = []
file_names = []
for i in tqdm(range(len(images_path))):
md5_hash = hashlib.md5()
a_file = open(images_path[i], "rb")
content = a_file.read()
md5_hash.update(content)
digest = md5_hash.hexdigest()
hashes.append(digest)
acl.append('Open-R1')
file_size.append(os.path.getsize(images_path[i]))
instance_uids.append(str(os.path.basename(images_path[i])).replace('.dcm', ''))
# Get relevant DICOM tags:
tag = pydicom.read_file(images_path[i])
case_ids.append(str(tag[0x0010, 0x0020].value))
study_uids.append(str(tag[0x0020, 0x000D].value))
series_uids.append(str(tag[0x0020, 0x000E].value))
modality.append(str(tag[0x0008, 0x0060].value))
file_name = str(tag[0x0010, 0x0020].value) + '/' + str(tag[0x0020, 0x000D].value) + '/' + str(
tag[0x0020, 0x000E].value) + '/' + str(os.path.basename(images_path[i]))
file_names.append(file_name)
url.append(s_url + file_name)
df['md5sum'] = np.array(hashes)
df['acl'] = np.array(acl)
df['storage_urls'] = np.array(url)
df['file_size'] = np.array(file_size)
df['case_ids'] = np.array(case_ids)
df['study_uid'] = np.array(study_uids)
df['series_uid'] = np.array(series_uids)
df['modality'] = np.array(modality)
df['file_name'] = np.array(file_names)
new_manifest = 'image_manifest_RSNA_' + date + '.tsv'
df.to_csv(new_manifest, sep='\t', index=False)
def chunks(lst: list, n: int) -> list:
for i in range(0, len(lst), n):
yield lst[i:i + n]
if __name__ == "__main__":
data_root_path = Path(sys.argv[1]) # path to folder holding the batch/submission TSVs
ask_user(data_root_path)