|
1
|
|
|
import sys |
|
|
|
|
|
|
2
|
|
|
import logging |
|
3
|
|
|
import os |
|
4
|
|
|
from tqdm import tqdm |
|
5
|
|
|
|
|
6
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
7
|
|
|
|
|
8
|
|
|
class DICOM_validate: |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
@staticmethod |
|
11
|
|
|
def MRN(input_string): |
|
|
|
|
|
|
12
|
|
|
string=str(input_string) |
|
|
|
|
|
|
13
|
|
|
if not string.isdigit(): |
|
14
|
|
|
return False |
|
15
|
|
|
try: |
|
16
|
|
|
MRN = int(string) |
|
|
|
|
|
|
17
|
|
|
if 0 < MRN < 9999999: |
|
|
|
|
|
|
18
|
|
|
return True |
|
19
|
|
|
else: |
|
20
|
|
|
return False |
|
21
|
|
|
except ValueError: |
|
22
|
|
|
return False |
|
23
|
|
|
|
|
24
|
|
|
@staticmethod |
|
25
|
|
|
def file(file_path): |
|
26
|
|
|
""" |
|
27
|
|
|
validate to check if the DICOM file is an actual DICOM file. |
|
28
|
|
|
:param file_path: |
|
29
|
|
|
:return: |
|
30
|
|
|
""" |
|
31
|
|
|
logger = logging.getLogger(__name__) |
|
32
|
|
|
|
|
33
|
|
|
global dicom |
|
|
|
|
|
|
34
|
|
|
dicom = None |
|
35
|
|
|
|
|
36
|
|
|
from pydicom.filereader import InvalidDicomError |
|
37
|
|
|
from pydicom.filereader import read_file |
|
38
|
|
|
try: |
|
39
|
|
|
dicom = read_file(file_path) |
|
40
|
|
|
except InvalidDicomError: |
|
41
|
|
|
logger.info(file_path + " is not a DICOM file. Skipping") |
|
42
|
|
|
return False, None |
|
43
|
|
|
return True, dicom |
|
44
|
|
|
|
|
45
|
|
|
@staticmethod |
|
46
|
|
|
def path(dir_path): |
|
47
|
|
|
""" |
|
48
|
|
|
Some basic information of the participants must be consistent across the files, such as the SCAN DATE (assuming they are not scanning across MIDNIGHT POINT) |
|
|
|
|
|
|
49
|
|
|
Birthday date, subject name, etc MUST BE CONSISTENT across a SINGLE subject's folder, RIGHT! |
|
50
|
|
|
|
|
51
|
|
|
:param dir_path: |
|
52
|
|
|
:return: |
|
53
|
|
|
""" |
|
54
|
|
|
logger = logging.getLogger(__name__) |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
# Input check |
|
58
|
|
|
if not os.path.exists(dir_path) or not os.path.isdir(dir_path): |
|
59
|
|
|
logger.info("Bad data folder path") |
|
60
|
|
|
return False |
|
61
|
|
|
|
|
62
|
|
|
files = os.listdir(dir_path) |
|
63
|
|
|
|
|
64
|
|
|
# Used to record the first encountered patientID and name, and will check against subsequent folder for same matching information. |
|
|
|
|
|
|
65
|
|
|
global PatientID, PatientName |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
validated_DICOM_files = [] |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
from DICOM.elements import DICOM_elements |
|
|
|
|
|
|
70
|
|
|
logger.info("Checking individual dicom files for patient info consistencies") |
|
71
|
|
|
|
|
72
|
|
|
# Check individual DICOM file for consistencies. |
|
73
|
|
|
for file in tqdm(files): |
|
74
|
|
|
|
|
75
|
|
|
# Skip current file if they are not DICOM files. |
|
76
|
|
|
isDICOM, _ = DICOM_validate.file(file) |
|
|
|
|
|
|
77
|
|
|
if not isDICOM: |
|
78
|
|
|
continue |
|
79
|
|
|
|
|
80
|
|
|
# Record first instance of patient ID and patient name. |
|
81
|
|
|
if PatientID is None and PatientName is None: |
|
|
|
|
|
|
82
|
|
|
Success, PatientID = DICOM_elements.retrieve(files[0], "PatientID") |
|
|
|
|
|
|
83
|
|
|
Success, PatientName = DICOM_elements.retrieve(files[0], "PatientName") |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
# raise issue if not successful |
|
86
|
|
|
if not Success: |
|
87
|
|
|
logger.info("DICOME meta data retrieval failure.") |
|
88
|
|
|
return False |
|
89
|
|
|
continue |
|
90
|
|
|
|
|
91
|
|
|
# Check consistencies across folders in terms of patient ID, NAME. |
|
92
|
|
|
CurrentPatientID = DICOM_elements.retrieve(file, "PatientID") |
|
|
|
|
|
|
93
|
|
|
CurrentPatientName = DICOM_elements.retrieve(file, "PatientName") |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
if not (PatientID == CurrentPatientID) or not (PatientName == CurrentPatientName): |
|
|
|
|
|
|
96
|
|
|
logger.info("PatientID or Name mismatch from the dicom archive. .") |
|
97
|
|
|
return False |
|
98
|
|
|
|
|
99
|
|
|
validated_DICOM_files.append(file) |
|
100
|
|
|
|
|
101
|
|
|
return True, validated_DICOM_files |
|
102
|
|
|
|
|
103
|
|
|
#if __name__ == '__main__': |
|
104
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.