Passed
Pull Request — master (#2)
by Yang
02:09
created

Python.DICOM.decompress   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 58
dl 0
loc 100
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
B DICOM_decompress() 0 43 6
A DICOM_QuickTransferSyntaxCheck() 0 12 3
A DICOM_TransferSyntax() 0 17 2
B DICOM_RequireDecompression() 0 15 8
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
5
from DICOM.validate import DICOM_validator
0 ignored issues
show
introduced by
Unable to import 'DICOM.validate'
Loading history...
6
from pydicom.filereader import read_file_meta_info
0 ignored issues
show
introduced by
Imports from package pydicom are not grouped
Loading history...
7
8
9
def DICOM_decompress(input_file, out_put):
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_decompress does not conform to the function 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...
10
    """
11
    A wrapper for DCMDJPEG for decompression
12
    :param input_file:
13
    :param out_put:
14
    :return:
15
    """
16
    logger = logging.getLogger(__name__)
17
18
    if os.path.exists(out_put):
19
        return False, "Output_exist already."
20
21
    try:
22
        subprocess.check_output(['dcmdjpeg', input_file, out_put])
23
24
    # When dcmdjpeg has errors
25
    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...
26
        logger.info(e.output)
27
        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...
28
        logger.info(ErrorMessage)
29
        return False, ErrorMessage
30
    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...
31
        logger.info(e.output)
0 ignored issues
show
Bug introduced by
The Instance of Exception does not seem to have a member named output.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
32
        ErrorMessage = "Unknown exception detected. Contact the author about " + 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...
33
        logger.info(ErrorMessage)
34
        return False, ErrorMessage
35
36
    # Ensure that data is actually written out.
37
    if not os.path.exists(out_put):
38
        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...
39
        logger.info(ErrorMessage)
40
        return False, ErrorMessage
41
42
    # Test read the data after writing.
43
    try:
44
        pydicom.read_file(out_put)
45
    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...
46
        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 (156/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...
47
        logger.info(ErrorMessage)
48
        return False, ErrorMessage
49
50
    logger.info("Success written " + input_file + " to " + out_put)
51
    return True, "All good"
52
53
def DICOM_RequireDecompression(transfer_syntax):
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_RequireDecompression does not conform to the function 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
    """
55
    Determine if the transfer syntax symbolize LEE or JPEG compressed!
56
    :param transfer_syntax:
57
    :return: whether the DICOM files are compressed.
58
    """
59
60
    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...
61
        raise ValueError
62
    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 (108/100).

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

Loading history...
63
        return False
64
    elif transfer_syntax[18] == '4' or transfer_syntax[18] == '5' or transfer_syntax[18] == '6':
65
        return True
66
    else:
67
        raise ValueError
68
69
70
def DICOM_TransferSyntax(file_path):
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_TransferSyntax does not conform to the function 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...
71
    """
72
    Used to find if a file is compressed
73
    :param file_path:
74
    :return:
75
    """
76
77
    # Validity check:
78
    success, DICOM = DICOM_validator(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...
79
    if not success:
80
        raise IOError
81
82
    # Now read the meta information.
83
    dicom_file = read_file_meta_info(file_path)
84
    transfer_syntax = dicom_file.TransferSyntaxUID
85
86
    return transfer_syntax
87
88
def DICOM_QuickTransferSyntaxCheck(file_path):
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_QuickTransferSyntaxCheck does not conform to the function 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 function 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...
89
    # Validity check:
90
    success, DICOM = DICOM_validator(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...
91
    if not success:
92
        raise IOError
93
    import dicom
94
95
    # Now read the meta information.
96
    if DICOM.file_meta.TransferSyntaxUID in dicom.dataset.NotCompressedPixelTransferSyntaxes:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
Bug introduced by
The Module dicom does not seem to have a member named dataset.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
97
        return False
98
    else:
99
        return True
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...