|
1
|
|
|
from orthanc.query import orthanc_query |
|
|
|
|
|
|
2
|
|
|
from LORIS.helper import LORIS_helper |
|
|
|
|
|
|
3
|
|
|
import os |
|
|
|
|
|
|
4
|
|
|
import logging |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
def step1_orthanc2DICOM(data_folder_path): |
|
|
|
|
|
|
8
|
|
|
""" |
|
9
|
|
|
The purpose of this function is to connect to orthanc, retrieve a list of subjects, download each one in turn and unzip them somewhere. |
|
|
|
|
|
|
10
|
|
|
:return: whether it succeeded, and if succeeded, a list of all folders. |
|
11
|
|
|
""" |
|
12
|
|
|
logger = logging.getLogger("Step1: Orthanc to DICOM files") |
|
13
|
|
|
|
|
14
|
|
|
# Input check |
|
15
|
|
|
if not os.path.exists(data_folder_path) or not os.path.isdir(data_folder_path): |
|
16
|
|
|
logger.info("Bad data folder path") |
|
17
|
|
|
return False, None |
|
18
|
|
|
|
|
19
|
|
|
# Query Orthanc for all subjects |
|
20
|
|
|
reseponse_code, list_subjects = orthanc_query.getOrthanc("patients/") |
|
21
|
|
|
if not LORIS_helper.is_response_success(reseponse_code, 200): |
|
22
|
|
|
logger.info("Could not connect to Orthanc.") |
|
23
|
|
|
return False, None |
|
24
|
|
|
|
|
25
|
|
|
all_subject_folder_paths = [] |
|
26
|
|
|
|
|
27
|
|
|
# For all subjects, unzip them somewhere. |
|
28
|
|
|
for subject in list_subjects: |
|
29
|
|
|
|
|
30
|
|
|
status, zip_file = orthanc_query.getPatientZipOrthanc(subject) |
|
31
|
|
|
|
|
32
|
|
|
# Checks for output from getPatientZipOrthanc |
|
33
|
|
|
if not LORIS_helper.is_response_success(status, 200): |
|
34
|
|
|
logger.info("Could not get patient zip files from Orthanc") |
|
35
|
|
|
return False, None |
|
36
|
|
|
if not os.path.exists(zip_file): |
|
37
|
|
|
logger.info("Could not locate saved zip files after dowwnloading from Orthanc") |
|
38
|
|
|
return False, None |
|
39
|
|
|
|
|
40
|
|
|
# Create a directory based on the zip file, which has UUID as name and guarnteed to be unique. |
|
|
|
|
|
|
41
|
|
|
file, ext = os.path.splitext(zip_file) |
|
|
|
|
|
|
42
|
|
|
final_folder_name = data_folder_path + file |
|
43
|
|
|
os.mkdir(final_folder_name) |
|
44
|
|
|
orthanc_query.flatUnZip(zip_file, final_folder_name) #files are guarnteed to be unique and no overwrite of each other. . |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
# Keep track of path extracted to for subsequent processing. |
|
47
|
|
|
all_subject_folder_paths.append(final_folder_name) |
|
48
|
|
|
return True, all_subject_folder_paths |
|
49
|
|
|
|
|
50
|
|
|
def step2_dicom2LORIS(folder_paths): |
|
|
|
|
|
|
51
|
|
|
""" |
|
52
|
|
|
Within each folder, verify they all have the same PatientID. |
|
53
|
|
|
:param folder_paths: |
|
54
|
|
|
:return: |
|
55
|
|
|
""" |
|
56
|
|
|
|
|
|
|
|
|
|
57
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.