|
1
|
|
|
import os |
|
|
|
|
|
|
2
|
|
|
import logging |
|
3
|
|
|
from DICOM.validate import DICOM_validator |
|
|
|
|
|
|
4
|
|
|
from DICOM.decompress import DICOM_RequireDecompression, DICOM_TransferSyntax |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
def recursive_list_files(root_dicom_path): |
|
8
|
|
|
""" |
|
9
|
|
|
load all the files, validate and then pass to decompress or anonimize. |
|
10
|
|
|
:param path: |
|
11
|
|
|
:return: |
|
12
|
|
|
""" |
|
13
|
|
|
global file_list |
|
|
|
|
|
|
14
|
|
|
file_list = [] |
|
15
|
|
|
|
|
16
|
|
|
for root, directories, filenames in os.walk(root_dicom_path): |
|
|
|
|
|
|
17
|
|
|
#for directory in directories: |
|
18
|
|
|
#file_list.append(os.path.join(root, directory)) |
|
19
|
|
|
for filename in filenames: |
|
20
|
|
|
file_list.append(os.path.join(root,filename)) |
|
|
|
|
|
|
21
|
|
|
return file_list |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def decompress_folder(file_list): |
|
|
|
|
|
|
25
|
|
|
""" |
|
26
|
|
|
Decompress all compressed file within the lsit. |
|
27
|
|
|
:param file_list: |
|
28
|
|
|
:return: |
|
29
|
|
|
""" |
|
30
|
|
|
|
|
31
|
|
|
for file in file_list: |
|
32
|
|
|
logger = logging.getLogger("Compression checking file: " + file) |
|
33
|
|
|
|
|
34
|
|
|
# find if the file is DICOM, if not, skip this file. |
|
35
|
|
|
is_DICOM_file, _ = DICOM_validator(file) |
|
|
|
|
|
|
36
|
|
|
if not is_DICOM_file: |
|
37
|
|
|
continue |
|
38
|
|
|
|
|
39
|
|
|
# check if the file is compressed. |
|
40
|
|
|
TransferSyntax = DICOM_TransferSyntax(file) |
|
|
|
|
|
|
41
|
|
|
try: |
|
42
|
|
|
RequireDecompression = DICOM_RequireDecompression(TransferSyntax) |
|
|
|
|
|
|
43
|
|
|
# if RequireDecompression: |
|
44
|
|
|
# DecompressFile |
|
45
|
|
|
|
|
46
|
|
|
except ValueError: |
|
47
|
|
|
logger.info("Unknwonw DICOM syntax. You sure it is DICOM?") |
|
48
|
|
|
continue |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def copy_files_to_flat_folder(file_list, destination_path): |
|
|
|
|
|
|
53
|
|
|
""" |
|
54
|
|
|
Takes in a list of files and flatten them to the desintation path while ensure they are guarnteed to be unique files. |
|
|
|
|
|
|
55
|
|
|
:param file_list: the list of files from different path. |
|
56
|
|
|
:param destination_path: |
|
57
|
|
|
""" |
|
58
|
|
|
logger = logging.getLogger(__name__) |
|
59
|
|
|
logger.info("Copying checking and checking files to destination: " + destination_path) |
|
60
|
|
|
|
|
61
|
|
|
from shutil import copyfile |
|
62
|
|
|
|
|
63
|
|
|
for file in file_list: |
|
64
|
|
|
|
|
65
|
|
|
# find if the file is DICOM, if not, skip this file. |
|
66
|
|
|
is_DICOM_file, _ = DICOM_validator(file) |
|
|
|
|
|
|
67
|
|
|
if not is_DICOM_file: |
|
68
|
|
|
continue |
|
69
|
|
|
|
|
70
|
|
|
# get the final path name. |
|
71
|
|
|
file_name = os.path.basename(file) |
|
72
|
|
|
destination_path_name = os.path.join(destination_path, file_name) |
|
73
|
|
|
|
|
74
|
|
|
# check if the final path is unique. |
|
75
|
|
|
unique, new_name = is_file_name_unique(destination_path_name) |
|
76
|
|
|
|
|
77
|
|
|
# append date time microsecond string if the file is not unique. |
|
78
|
|
|
if not unique: |
|
79
|
|
|
destination_path_name = new_name |
|
80
|
|
|
|
|
81
|
|
|
copyfile(file, destination_path_name) |
|
82
|
|
|
|
|
83
|
|
|
def is_file_name_unique(path): |
|
84
|
|
|
""" |
|
85
|
|
|
Determine if the proposed file exist and suggest alternative name. |
|
86
|
|
|
:param path: |
|
87
|
|
|
:return: |
|
88
|
|
|
""" |
|
89
|
|
|
import datetime |
|
90
|
|
|
timestamp = datetime.datetime.now().isoformat() |
|
91
|
|
|
timestamp = timestamp.replace(':','') # string : which are incompatible with path |
|
|
|
|
|
|
92
|
|
|
if os.path.exists(path): |
|
|
|
|
|
|
93
|
|
|
return False, path + "_" + timestamp |
|
94
|
|
|
else: |
|
95
|
|
|
return True, path |
|
96
|
|
|
|
|
97
|
|
|
def zip_with_name(path, name): |
|
|
|
|
|
|
98
|
|
|
return |
|
|
|
|
|
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.