|
1
|
|
|
import sys |
|
|
|
|
|
|
2
|
|
|
import logging |
|
3
|
|
|
import os |
|
4
|
|
|
import shutil |
|
5
|
|
|
from DICOM.elements import DICOM_elements |
|
|
|
|
|
|
6
|
|
|
from DICOM.validate import DICOM_validate |
|
|
|
|
|
|
7
|
|
|
from tqdm import tqdm |
|
8
|
|
|
|
|
9
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
10
|
|
|
|
|
11
|
|
|
class DICOM_sort: |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
@staticmethod |
|
14
|
|
|
def into_folder(input_folder, output_folder): |
|
|
|
|
|
|
15
|
|
|
""" |
|
16
|
|
|
This function sort a input folder with or without sub layers and automaticlly flatten everything into a folder before then MOVING them into protocol based folder sorted by acquisition series. |
|
|
|
|
|
|
17
|
|
|
:param input_folder: Input_folder can be a root folder or flat. |
|
18
|
|
|
:return: |
|
19
|
|
|
""" |
|
20
|
|
|
logger = logging.getLogger("DICOM sorting operation") |
|
21
|
|
|
|
|
22
|
|
|
#Element to check: Series number. |
|
23
|
|
|
|
|
24
|
|
|
from PythonUtils.file import flatcopy |
|
|
|
|
|
|
25
|
|
|
from PythonUtils.folder import recursive_list |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
# Get files |
|
28
|
|
|
file_list = recursive_list(input_folder) |
|
29
|
|
|
|
|
30
|
|
|
if not os.path.isdir(output_folder): |
|
31
|
|
|
os.mkdir(output_folder) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
# copy them to a flat structure to the output folder. |
|
35
|
|
|
flatcopy(file_list, output_folder, DICOM_validate.file) |
|
36
|
|
|
|
|
37
|
|
|
# decompress them if necessary. |
|
38
|
|
|
# oshelper_files.decompress_folder(output_folder) |
|
39
|
|
|
|
|
40
|
|
|
# Get files list again. |
|
41
|
|
|
file_list = recursive_list(output_folder) |
|
42
|
|
|
|
|
43
|
|
|
exception_encountered = 0 |
|
44
|
|
|
|
|
45
|
|
|
logger.info("Sorting files into folders:") |
|
46
|
|
|
|
|
47
|
|
|
# File here should be the FULL path. |
|
48
|
|
|
for file in tqdm(file_list): |
|
49
|
|
|
|
|
50
|
|
|
success1, SeriesNumber = DICOM_elements.retrieve(file, "SeriesNumber") |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
success2, SeriesDescription = DICOM_elements.retrieve(file, "SeriesDescription") |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
if not success1 or not success2: |
|
55
|
|
|
logger.info("Skipped file with no acquisition series information: " + file) |
|
56
|
|
|
exception_encountered = exception_encountered + 1 |
|
57
|
|
|
continue |
|
58
|
|
|
|
|
59
|
|
|
# Check MRI Series folder exists |
|
60
|
|
|
DestinationFolder = str(SeriesNumber) + '_' + SeriesDescription |
|
|
|
|
|
|
61
|
|
|
DestinationFolder = DestinationFolder.replace(' ', '_') |
|
|
|
|
|
|
62
|
|
|
DestinationFolder = DestinationFolder.replace(':', '_') |
|
|
|
|
|
|
63
|
|
|
DestinationFolder = DestinationFolder.replace(r'/', '_') |
|
|
|
|
|
|
64
|
|
|
DestinationFolder = DestinationFolder.replace(r'\\', '_') |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
# Make destination folder if not exist. |
|
67
|
|
|
os.chdir(output_folder) |
|
68
|
|
|
if not os.path.exists(DestinationFolder): |
|
69
|
|
|
os.mkdir(DestinationFolder) |
|
70
|
|
|
|
|
71
|
|
|
# Get file name. |
|
72
|
|
|
_, filename = os.path.split(file) |
|
73
|
|
|
|
|
74
|
|
|
shutil.move(file, os.path.join(DestinationFolder, filename)) |
|
75
|
|
|
logger.info("Total error encoutnered: " + str(exception_encountered)) |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
if __name__ == "__main__": |
|
79
|
|
|
DICOM_sort.into_folder("C:\FullyAnonymizedSubjects\StreamLineTest", "C:\FullyAnonymizedSubjects\StreamLineTest\Test") |
|
|
|
|
|
|
80
|
|
|
|
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.