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

DICOM_RequireDecompression()   B

Complexity

Conditions 8

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 7.3333
c 0
b 0
f 0
cc 8
nop 1
1
import logging
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 os
3
import pydicom, subprocess
0 ignored issues
show
introduced by
Multiple imports on one line (pydicom, subprocess)
Loading history...
introduced by
standard import "import pydicom, subprocess" should be placed before "import pydicom, subprocess"
Loading history...
4
from PythonUtils.folder import recursive_list
0 ignored issues
show
introduced by
Unable to import 'PythonUtils.folder'
Loading history...
5
from tqdm import tqdm
6
7
8
from pydicom.filereader import read_file_meta_info
0 ignored issues
show
introduced by
Imports from package pydicom are not grouped
Loading history...
9
10
from DICOM.validate import DICOM_validate
0 ignored issues
show
introduced by
Unable to import 'DICOM.validate'
Loading history...
11
12
class DICOM_decompress:
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_decompress 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...
13
14
    @staticmethod
15
    def save_as(input_file, out_put):
16
        """
17
        A wrapper for DCMDJPEG for decompression
18
        :param input_file:
19
        :param out_put:
20
        :return:
21
        """
22
        logger = logging.getLogger(__name__)
23
24
        if os.path.exists(out_put):
25
            return False, "Output_exist already."
26
27
        try:
28
            # SUPER IMPORTANT! MAKE SURE DCMDJPEG is in the system path!
29
            subprocess.check_output(['dcmdjpeg', input_file, out_put])
30
31
        # When dcmdjpeg has errors
32
        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...
33
            logger.info(e)
34
            ErrorMessage = "File type not compatible for " + input_file
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...
35
            logger.info(ErrorMessage)
36
            return False, ErrorMessage
37
        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...
38
            logger.info(e)
39
            ErrorMessage = "DCMDJPEG decompression call failed! Make sure DCMDJPEG is in your SYSTEMOS PATH and then check your input file:" + input_file
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (153/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...
40
            logger.info(ErrorMessage)
41
            return False, ErrorMessage
42
43
        # Ensure that data is actually written out.
44
        if not os.path.exists(out_put):
45
            ErrorMessage = "Cannot write out final file for some reason " + input_file
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...
46
            logger.info(ErrorMessage)
47
            return False, ErrorMessage
48
49
        # Test read the data after writing.
50
        try:
51
            pydicom.read_file(out_put)
52
        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...
53
            ErrorMessage = "Exception encountered while verifying the proper writing out of the DICOM data. Contact author to investigate, attach " + input_file
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (160/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...
54
            logger.info(e)
55
            logger.info(ErrorMessage)
56
            return False, ErrorMessage
57
58
        logger.info("Success written " + input_file + " to " + out_put)
59
        return True, "All good"
60
61
    @staticmethod
62
    def check_decompression(transfer_syntax):
63
        """
64
        Determine if the transfer syntax symbolize LEE or JPEG compressed!
65
        :param transfer_syntax:
66
        :return: whether the DICOM files are compressed.
67
        """
68
69
        if not ("1.2.840.10008.1.2" in transfer_syntax):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after not.
Loading history...
70
            raise ValueError
71
        elif transfer_syntax == "1.2.840.10008.1.2" or transfer_syntax[18] == '1' or transfer_syntax[18] == '2':
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (112/100).

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

Loading history...
72
            return False
73
        elif transfer_syntax[18] == '4' or transfer_syntax[18] == '5' or transfer_syntax[18] == '6':
74
            return True
75
        else:
76
            raise ValueError
77
78
    @staticmethod
79
    def get_transferSyntax(file_path):
0 ignored issues
show
Coding Style Naming introduced by
The name get_transferSyntax 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...
80
        """
81
        Used to find if a file is compressed
82
        :param file_path:
83
        :return:
84
        """
85
86
        # Validity check:
87
        success, DICOM = DICOM_validate.file(file_path)
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM 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...
Unused Code introduced by
The variable DICOM seems to be unused.
Loading history...
88
        if not success:
89
            raise IOError
90
91
        # Now read the meta information.
92
        dicom_file = read_file_meta_info(file_path)
93
        transfer_syntax = dicom_file.TransferSyntaxUID
94
95
        return transfer_syntax
96
97
    @staticmethod
98
    def check_decompression_quick(file_path):
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...
99
        # Validity check:
100
        success, DICOM = DICOM_validate.file(file_path)
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM 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...
101
        if not success:
102
            raise IOError
103
        import pydicom.uid
0 ignored issues
show
Comprehensibility Bug introduced by
pydicom is re-defining a name which is already available in the outer-scope (previously defined on line 3).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
104
105
        # Now read the meta information.
106
        if DICOM.file_meta.TransferSyntaxUID in pydicom.uid.UncompressedPixelTransferSyntaxes:
0 ignored issues
show
Unused Code introduced by
The if statement can be replaced with 'return bool(test)'
Loading history...
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
107
            return True
108
        else:
109
            return False
110
111
    @staticmethod
112
    def filelist(file_list):
113
        """
114
        Decompress all compressed files in the list OVERWRITE the files.
115
        :param file_list:
116
        :return:
117
        """
118
        logger = logging.getLogger("Decompressing files")
119
120
        for file in tqdm(file_list):
121
122
            logger.info("Decompressing: " + file)
123
124
            # find if the file is DICOM, if not, skip this file.
125
            is_DICOM_file, _ = DICOM_validate.file(file)
0 ignored issues
show
Coding Style Naming introduced by
The name is_DICOM_file 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...
126
            if not is_DICOM_file:
127
                continue
128
129
            # check if the file is compressed.
130
            TransferSyntax = DICOM_decompress.get_transferSyntax(file)
0 ignored issues
show
Coding Style Naming introduced by
The name TransferSyntax 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...
131
            try:
132
                RequireDecompression = DICOM_decompress.check_decompression(TransferSyntax)
0 ignored issues
show
Coding Style Naming introduced by
The name RequireDecompression 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...
133
                if RequireDecompression:
134
                    DICOM_decompress.save_as(file, file)
135
136
            except ValueError:
137
                logger.info("Unknwonw DICOM syntax. You sure it is DICOM?")
138
                continue
139
140
    @staticmethod
141
    def decompress_folder(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...
142
        files_list = recursive_list(input_folder)
143
        DICOM_decompress.filelist(files_list)
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...