Passed
Pull Request — master (#3)
by Yang
04:58
created

Python.DICOM.sort.DICOM_sort.into_folder()   B

Complexity

Conditions 6

Size

Total Lines 63
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 30
nop 2
dl 0
loc 63
rs 8.2266
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import sys
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import logging
3
import os
4
import shutil
5
from DICOM.elements import DICOM_elements
0 ignored issues
show
introduced by
Unable to import 'DICOM.elements'
Loading history...
6
from DICOM.validate import DICOM_validate
0 ignored issues
show
introduced by
Unable to import 'DICOM.validate'
Loading history...
7
from tqdm import tqdm
8
9
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
10
11
class DICOM_sort:
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_sort does not conform to the class naming conventions ([A-Z_][a-zA-Z0-9]+$).

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.

Loading history...
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
12
13
    @staticmethod
14
    def into_folder(input_folder, output_folder):
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (16/15).
Loading history...
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.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (199/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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
0 ignored issues
show
introduced by
Unable to import 'PythonUtils.file'
Loading history...
25
        from PythonUtils.folder import recursive_list
0 ignored issues
show
introduced by
Unable to import 'PythonUtils.folder'
Loading history...
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")
0 ignored issues
show
Coding Style Naming introduced by
The name SeriesNumber does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

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.

Loading history...
51
52
            success2, SeriesDescription = DICOM_elements.retrieve(file, "SeriesDescription")
0 ignored issues
show
Coding Style Naming introduced by
The name SeriesDescription does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

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.

Loading history...
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
0 ignored issues
show
Coding Style Naming introduced by
The name DestinationFolder does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

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.

Loading history...
61
            DestinationFolder = DestinationFolder.replace(' ', '_')
0 ignored issues
show
Coding Style Naming introduced by
The name DestinationFolder does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

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.

Loading history...
62
            DestinationFolder = DestinationFolder.replace(':', '_')
0 ignored issues
show
Coding Style Naming introduced by
The name DestinationFolder does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

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.

Loading history...
63
            DestinationFolder = DestinationFolder.replace(r'/', '_')
0 ignored issues
show
Coding Style Naming introduced by
The name DestinationFolder does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

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.

Loading history...
64
            DestinationFolder = DestinationFolder.replace(r'\\', '_')
0 ignored issues
show
Coding Style Naming introduced by
The name DestinationFolder does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

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.

Loading history...
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")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (121/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Bug introduced by
A suspicious escape sequence \F was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
Bug introduced by
A suspicious escape sequence \S was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
Bug introduced by
A suspicious escape sequence \T was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
80