This repository was archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathexample.py
116 lines (99 loc) · 3.73 KB
/
example.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
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
"""
cortx_dicom
Example.
"""
# Modules from the standard library which are used in this example only
from random import shuffle
from time import sleep
# Modules from the standard library which are used by the module too
from os import listdir
from os.path import isfile, join
# Import additional library which is used by the module too
import pydicom
# Importing the module
from cortx_dicom import Config, CortxDicom
DEMO_COUNT = 5
DICOM_FOLDER = 'dicoms'
TEST_BUCKET = 'dicom-test'
print('*** Preparation ***')
# Loading configuration
# PLEASE: don't forget to change settings according to your circumstances.
Config.load('config.json', Config.FILE_JSON)
# Instantiate CortxDicom
cortxdicom = CortxDicom()
# Create a bucket to test
buckets = cortxdicom.s3_buckets__()
if TEST_BUCKET not in buckets:
cortxdicom.s3_engine.create_bucket(Bucket=TEST_BUCKET)
# Making filelist to create really random test
filelist = [f for f in listdir(DICOM_FOLDER) if isfile(join(DICOM_FOLDER, f))]
len_filelist = len(filelist)
if len_filelist < DEMO_COUNT:
REAL_COUNT = len_filelist
else:
REAL_COUNT = DEMO_COUNT
print('There are {} files in the dicoms folder, {} will be randomly selected.'
.format(len_filelist, REAL_COUNT))
# Storing and basic searching
print('*** Storage demonstration ***')
shuffle(filelist)
es_ids, s3_ids = [], []
for i in range(REAL_COUNT):
dicomfile = pydicom.dcmread(join(DICOM_FOLDER, filelist[i]))
len_before = len(dicomfile)
CortxDicom.apply_filter(dicomfile, CortxDicom.FILTER_HIPAA)
len_after = len(dicomfile)
print('File {}/{}: {} fields removed to fit HIPAA regulation ({}/{})'
.format(i, REAL_COUNT, len_before - len_after, len_after, len_before))
es_id = cortxdicom.store(dicomfile, bucket=TEST_BUCKET)
if es_id is None:
print('Something went wrong at storing.')
continue
_result = cortxdicom.search(es_id, just_find=False)
if _result is None:
print('Something went wrong at searching.')
continue
s3_id = _result[1]
es_ids.append(es_id)
s3_ids.append(s3_id)
print('File stored as "{}" in S3, information as "{}" in elasticsearch.'
.format(s3_id, es_id))
print('*** Pause - 15 seconds to let VM doing its jobs. ***')
sleep(15)
print('*** Search demonstration - Elasticsearch side ***')
# This query always returns data
result = cortxdicom.search({"query": {"match_all": {}}})
print('If you want to know about potential matches, the result is {}.'
.format(result))
result = cortxdicom.search({"query": {"match_all": {}}}, just_find=False)
if result is None:
print('No match found.')
else:
print('Result of the search')
for row in result:
print(row)
print('Difference between CortxDicom.search() and CortxDicom.get().')
result = cortxdicom.get({"query": {"match_all": {}}})
if result is None:
print('No match found.')
else:
print('Result of the search')
for row in result:
print(type(row))
print('*** Search demonstration - S3 side ***')
result = cortxdicom.get(object_key=s3_ids[0], bucket=TEST_BUCKET)
if result is None:
print('No match found.')
else:
print('DICOM file:')
print(result)
print('*** Cleaning up everything ***')
cortxdicom.es_engine.indices.delete(index=CortxDicom.ES_DEFAULT_INDEX,
ignore=[400, 404])
buckets = cortxdicom.s3_buckets__()
if TEST_BUCKET in buckets:
dicomfiles = cortxdicom.s3_objects__(TEST_BUCKET)
for dicomfile in dicomfiles:
cortxdicom.s3_engine.delete_object(Bucket=TEST_BUCKET, Key=dicomfile)
cortxdicom.s3_engine.delete_bucket(Bucket=TEST_BUCKET)
print('*** The End - Thanks for running me. ***')