-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcm4chee-connector.py
More file actions
134 lines (102 loc) · 4.36 KB
/
Copy pathdcm4chee-connector.py
File metadata and controls
134 lines (102 loc) · 4.36 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
125
126
127
128
129
130
131
132
133
134
from helpers.connector import dcm4cheeConnector
from argparse import ArgumentParser
import os
import csv
# from dotenv import load_dotenv
from helpers import config
from datetime import datetime
def read_csv(file):
parameters = {}
try:
reader = csv.reader(open(file, 'r'))
except FileNotFoundError:
print('Query CSV file not found.')
return
for row in reader:
if len(row) == 2:
parameters[row[0]] = row[1]
return parameters
def main():
parser = ArgumentParser(description='DCM4Chee Connector toolKIT')
parser.add_argument('-q', '--query', action="store_true", help='Query to send to DCM4CHEE')
parser.add_argument('-d', '--download', action='store_true', help='Download data from DCM4CHEE')
parser.add_argument('-o', '--out', action='store_true', help='Output CSV file for query results')
parser.add_argument('--aet', type=str, help='AET of the DCM4CHEE server')
parser.add_argument('--clientId', type=str, help='Client ID of the DCM4CHEE server')
parser.add_argument('--query-csv', type=str, help='CSV file containing query parameters')
parser.add_argument('--dicom-destination', type=str, help='Destination folder for downloaded data')
args = parser.parse_args()
aet = args.aet if args.aet else os.getenv('AET')
clientId = args.clientId if args.clientId else os.getenv('CLIENT_ID')
username = os.getenv('USERNAME') if os.getenv('USERNAME') else None
password = os.getenv('PASSWORD') if os.getenv('PASSWORD') else None
if username is None:
username = str(input('Enter username: '))
if username is None:
print('Username is required.')
return
if password is None:
password = str(input('Enter password: '))
if password is None:
print('Password is required.')
return
query_csv = args.query_csv if args.query_csv else None
connector = dcm4cheeConnector(clientId, aet, username, password)
authentication = connector.authenticate()
if authentication['status'] != 200:
print('Authentication failed. Please check your credentials.')
return
else:
print(f'Authentication successful. {authentication["msg"]}')
if args.query:
if query_csv is None and os.getenv('QUERY_CSV') is not None:
query_csv = os.getenv('QUERY_CSV')
else:
print('CSV file with query parameters is required.')
return
if not os.path.exists(query_csv):
print('Query CSV file not found.')
return
query_params = read_csv(query_csv)
if args.out:
output_csvfile = f"{datetime.now().strftime('%Y%m%d')}_output.csv"
else:
output_csvfile = None
clause = connector.getQueryParams(query_params)
result = connector.query(clause)
print(result['msg'])
export_result = ""
if result['status'] == True and output_csvfile is not None:
export_result = connector.exportQueryAsCSV(output_csvfile)
elif result['status'] == True:
export_result = connector.exportQueryAsTable()
print(export_result)
if args.download:
if query_csv is None and os.getenv('QUERY_CSV') is not None:
query_csv = os.getenv('QUERY_CSV')
else:
print('CSV file with query parameters is required.')
return
if not os.path.exists(query_csv):
print('Query CSV file not found.')
return
if args.dicom_destination:
dicom_destination = args.dicom_destination
elif os.getenv('DESTINATION') is not None:
dicom_destination = os.getenv('DESTINATION')
else:
print('Destination folder is required to download data.')
return
if not os.path.exists(dicom_destination):
os.makedirs(dicom_destination)
# Query Data
query_params = read_csv(query_csv)
clause = connector.getQueryParams(query_params)
result = connector.query(clause)
print(result['msg'])
export_result = ""
if result['status'] == True :
export_result = connector.exportQueryAsDicom(destination=dicom_destination)
print(export_result)
if __name__ == "__main__":
main()