-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.py
69 lines (56 loc) · 2.06 KB
/
images.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
import csv
import datetime
import re
import sys
import time
from pathdbapi import *
def usage():
print('Usage: ' + sys.argv[0] + ' [username] [password] [collection name | "all"]')
exit(1)
if len(sys.argv) == 4:
username = sys.argv[1]
password = sys.argv[2]
collection = sys.argv[3]
else:
usage()
host = "http://quip-pathdb"
api = MyApi(host, username, password)
hasNext = True
collection_id = 0
collection_name = ''
out_dir = '/data/reports/'
everything = False
stamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d_%H%M%S')
if 'all' in collection.lower():
everything = True
images_url = '/listofimages?_format=json'
lookup_table = api.get_collection_lookup_table()
name = 'all'
else:
everything = False
collection_id, collection_name = api.get_collection_info(collection)
if len(collection_name) == 0:
print("Could not find collection:", collection)
exit(1)
images_url = '/listofimages/' + str(collection_id) + '?_format=json'
name = re.sub('\W+', '', collection_name).lower()
with open(out_dir + 'images_' + name + '_' + stamp + '.csv', mode='w') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(['Collection', 'Study ID', 'Subject ID', 'Image ID', 'Date'])
count = 0
while hasNext:
url = images_url + '&page=' + str(count)
count += 1
response = api.get_data(url)
if len(response) > 0:
for r in response:
datetime_str = r['created'][0]['value'].replace("T", " ")
datetime_str = datetime_str.replace("+00:00", "")
if everything:
collection_id = r['field_collection'][0]['target_id']
collection_name = lookup_table[collection_id]
csv_writer.writerow([collection_name, r['studyid'][0]['value'], r['clinicaltrialsubjectid'][0]['value'],
r['imageid'][0]['value'], datetime_str])
else:
hasNext = False
exit(0)