-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathanonymize.py
50 lines (40 loc) · 1.79 KB
/
anonymize.py
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
import argparse
import os
import pydicom
import numpy as np
from tqdm import tqdm
import pandas as pd
import hashlib
if __name__ == "__main__":
os.makedirs('dataset_masked', exist_ok = True)
tag_df = pd.read_csv('tags.csv')
SOPInstanceUIDs = []
StudyInstanceUIDs = []
encoded_ids = []
for mset in ['test', 'train']:
os.makedirs('dataset_masked/' + mset, exist_ok = True)
for rdir,_,files in os.walk('dataset/' + mset):
for file in files:
dicom_path = os.path.join(rdir, file)
print(dicom_path)
dcm = pydicom.read_file(dicom_path)
dcm.file_meta.ImplementationClassUID = "1.2.3.4"
MediaStorageSOPInstanceUID = dcm.file_meta.MediaStorageSOPInstanceUID
hash_object = hashlib.md5(MediaStorageSOPInstanceUID.encode())
encoded = str(hash_object.hexdigest())
dcm.file_meta.MediaStorageSOPInstanceUID = encoded
SOPInstanceUIDs.append(dcm.SOPInstanceUID)
StudyInstanceUIDs.append(dcm.StudyInstanceUID)
encoded_ids.append(encoded)
dcm_keys = list(dcm.keys())
for k in dcm_keys:
if k not in tag_df.keyword.values:
dcm.pop(k, None)
print(dcm)
new_dicom_path = dicom_path.replace('dataset/', 'dataset_masked/').replace(file, encoded+'.dicom')
dcm.save_as(new_dicom_path)
image_uid_encode_df = pd.DataFrame()
image_uid_encode_df['encode_id'] = np.array(encoded_ids)
image_uid_encode_df['SOPInstanceUID'] = np.array(SOPInstanceUIDs)
image_uid_encode_df['StudyInstanceUID'] = np.array(StudyInstanceUIDs)
image_uid_encode_df.to_csv('encoded_id.csv', index=False)