-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate_data.py
60 lines (43 loc) · 1.91 KB
/
generate_data.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
51
52
53
54
55
56
57
58
59
60
import json
from pathlib import Path
from pydicom import dcmread
import click
from progress.bar import Bar
from database import MyPACSdatabase
config_root = r'./config.json'
columns = ['sop_instance_uid', 'patient_name', 'patient_id', 'study_instance_uid', 'modality', 'body_part_examined',
'series_description', 'series_instance_uid', 'local_file_path', 'adapter', 'annotation']
def create_db(db):
db.query_return_none('DROP TABLE IF EXISTS mypacs_test')
db.query_file_return_none('./sql/create.sql')
def insert_data_to_db(db, roots):
for root in roots:
dicom_path_list = list(Path(root).rglob('*.dcm'))
record_list = []
with Bar(f'Processing DICOM Files at root[{root}]:', max=len(dicom_path_list)) as bar:
for path in dicom_path_list:
record_list.append(generate_record_dict(str(path)))
bar.next()
db.bulk_query_file('./sql/insert.sql', record_list)
print('Insert all DICOM files to DB!')
def generate_record_dict(path: str):
dataset = dcmread(path)
values = [dataset.SOPInstanceUID, dataset.PatientName, dataset.PatientID, dataset.StudyInstanceUID,
dataset.Modality, dataset.BodyPartExamined, dataset.get("SeriesDescription", "==NONE=="),
dataset.SeriesInstanceUID, path, None, None]
if 'LIDC' in path:
values[-2] = 'LIDC'
xml_file = list(Path(path).parent.glob('*.xml'))
values[-1] = str(xml_file[0]) if len(xml_file) > 0 else None
return dict(zip(columns, values))
@click.command()
@click.option('--root', '-r', help='DICOM file root', multiple=True)
@click.option('--config', '-c', default=config_root, help='config file path')
def main(root, config):
with open(config, 'r') as file:
db_config = json.load(file)['database']
db = MyPACSdatabase(db_config)
create_db(db)
insert_data_to_db(db, root)
if __name__ == '__main__':
main()