-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_swift_notify_cube.py
executable file
·82 lines (71 loc) · 2.79 KB
/
upload_swift_notify_cube.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
import argparse
import time
from os import listdir, system
from os.path import isfile, join
import swiftclient
from chrisclient import client
from chrisclient.exceptions import ChrisRequestException
from pydicom import dcmread
parser = argparse.ArgumentParser(description='COVID-Net Training Script')
parser.add_argument('--imageDir',
default='images',
type=str,
help='Directory containing images to upload')
args = parser.parse_args()
chris_client = client.Client("http://localhost:8000/api/v1/", "chris",
"chris1234")
# Swift service settings
DEFAULT_FILE_STORAGE = 'swift.storage.SwiftStorage'
SWIFT_AUTH_URL = 'http://localhost:8080/auth/v1.0'
SWIFT_USERNAME = 'chris:chris1234'
SWIFT_KEY = 'testing'
SWIFT_CONTAINER_NAME = 'users'
output_path = 'SERVICES/PACS/covidnet'
folder = args.imageDir
# Get all dicom images
dcmFiles = [[folder, f] for f in listdir(folder) if isfile(join(folder, f))]
for f in dcmFiles:
system(f'swift -A {SWIFT_AUTH_URL} -U {SWIFT_USERNAME} ' +
'-K testing upload users {}/{} '.format(f[0], f[1]) +
'--object-name "{}/{}"'.format(output_path, f[1]))
# Initiate a Swift service connection
conn = swiftclient.Connection(user=SWIFT_USERNAME,
key=SWIFT_KEY,
authurl=SWIFT_AUTH_URL)
object_list = []
poll_loop = 0
max_polls = 20
while len(object_list) < len(dcmFiles) and poll_loop < max_polls:
object_list = conn.get_container(SWIFT_CONTAINER_NAME,
prefix=output_path,
full_listing=True)[1]
if len(object_list) < len(dcmFiles):
time.sleep(0.2)
poll_loop += 1
for index in range(len(dcmFiles)):
f = dcmFiles[index]
with open(join(f[0], f[1]), 'rb') as infile:
ds = dcmread(infile)
pacs_data = {
'path': f'SERVICES/PACS/covidnet/{f[1]}',
'PatientID': str(ds.PatientID),
'PatientName': str(ds.PatientName),
'PatientBirthDate': str(ds.PatientBirthDate),
'PatientAge': str(ds.PatientAge),
'PatientSex': str(ds.PatientSex),
'ProtocolName': str(ds.ProtocolName),
'StudyDate': str(ds.StudyDate),
'StudyInstanceUID': str(ds.StudyInstanceUID),
'StudyDescription': str(ds.StudyDescription),
'SeriesInstanceUID': str(ds.SeriesInstanceUID),
'SeriesDescription': str(ds.SeriesDescription),
'Modality': ds.Modality,
'pacs_name': 'covidnet'
}
try:
chris_client.register_pacs_file(pacs_data)
except ChrisRequestException as e:
print(f"{f[1]}: {e}")
continue
print('SUCCESS')