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

Python.DICOM.convert   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A DICOM_convert.to_minc() 0 3 1
B DICOM_convert.to_nii() 0 35 5
A DICOM_convert.to_BIDS() 0 3 1
A DICOM_convert.fix_series() 0 8 4
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 subprocess
5
import re
6
7
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
8
9
class DICOM_convert:
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_convert 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...
10
11
    @staticmethod
12
    def to_nii(input_folder, output_folder):
13
        """
14
15
        :param input_folder: Input_folder can be a root folder or flat.
16
        :return:
17
        """
18
        logger = logging.getLogger("DICOM to NII conversion")
19
        if not os.path.exists(input_folder) or not os.path.exists(output_folder):
20
            return False, "Argument input or output folder does not exist"
21
22
        try:
23
24
            # SUPER IMPORTANT! MAKE SURE dcm2niix by Chris Roden is in the system path!
25
            subprocess.check_output(['dcm2niix',
26
                                     '-b', 'y',
27
                                     '-z', 'y',
28
                                     '-v', 'y',
29
                                     '-f', "%s_%p",
30
                                     '-o', output_folder,
31
                                    input_folder])
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 1 space).
Loading history...
32
33
        # When dcmdjpeg has errors
34
        except subprocess.CalledProcessError as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e 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...
35
            logger.info(e)
36
            ErrorMessage = "File type not compatible"
0 ignored issues
show
Coding Style Naming introduced by
The name ErrorMessage 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...
37
            logger.info(ErrorMessage)
38
            return False, ErrorMessage
39
        except Exception as e:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
Coding Style Naming introduced by
The name e 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...
40
            logger.info(e)
41
            ErrorMessage = "dcm2niix decompression call failed! Make sure dcm2niix is in your SYSTEM OS PATH and then check your input file:"
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (141/100).

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

Loading history...
Coding Style Naming introduced by
The name ErrorMessage 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...
42
            logger.info(ErrorMessage)
43
            return False, ErrorMessage
44
45
        return True, "All conversion successfully finished."
46
47
    @staticmethod
48
    def fix_series(input_folder):
0 ignored issues
show
Coding Style introduced by
This method 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...
49
        os.chdir(input_folder)
50
        for file in os.listdir(input_folder):
51
            if re.match(r'^[0-9]_', file):
52
                os.rename(file, '00'+ file)
53
            elif re.match(r'^[0-9][0-9]_', file):
54
                os.rename(file, '0'+ file)
55
56
    @staticmethod
57
    def to_minc(input_folder, output_folder):
0 ignored issues
show
Coding Style introduced by
This method 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...
58
        raise NotImplementedError
59
60
    @staticmethod
61
    def to_BIDS(input_folder, output_folder):
0 ignored issues
show
Coding Style Naming introduced by
The name to_BIDS does not conform to the method 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...
Coding Style introduced by
This method 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...
62
        raise NotImplementedError
63
64
65
if __name__ == "__main__":
66
    DICOM_convert.fix_series(r"C:\FullyAnonymizedSubjects\Wed3ConvertResult\raw_sorted")
67
    #DICOM_convert.to_nii(r"C:\FullyAnonymizedSubjects\2018-08-15_TestSubject2\Wed2-Decompressed", r"C:\FullyAnonymizedSubjects\2018-08-15_TestSubject2\Test")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (158/100).

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

Loading history...
68