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

Python.DICOM.validate   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A DICOM_validate.file() 0 20 2
A DICOM_validate.MRN() 0 13 4
C DICOM_validate.path() 0 57 10
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
from tqdm import tqdm
5
6
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
7
8
class DICOM_validate:
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_validate 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...
9
10
    @staticmethod
11
    def MRN(input_string):
0 ignored issues
show
Coding Style Naming introduced by
The name MRN 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...
12
        string=str(input_string)
0 ignored issues
show
Coding Style introduced by
Exactly one space required around assignment
Loading history...
13
        if not string.isdigit():
14
            return False
15
        try:
16
            MRN = int(string)
0 ignored issues
show
Coding Style Naming introduced by
The name MRN 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...
17
            if 0 < MRN < 9999999:
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...
18
                return True
19
            else:
20
                return False
21
        except ValueError:
22
            return False
23
24
    @staticmethod
25
    def file(file_path):
26
        """
27
        validate to check if the DICOM file is an actual DICOM file.
28
        :param file_path:
29
        :return:
30
        """
31
        logger = logging.getLogger(__name__)
32
33
        global dicom
0 ignored issues
show
Coding Style Naming introduced by
The name dicom does not conform to the constant naming conventions ((([A-Z_][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...
Bug introduced by
Global variable 'dicom' undefined at the module level
Loading history...
34
        dicom = None
35
36
        from pydicom.filereader import InvalidDicomError
37
        from pydicom.filereader import read_file
38
        try:
39
            dicom = read_file(file_path)
40
        except InvalidDicomError:
41
            logger.info(file_path + " is not a DICOM file. Skipping")
42
            return False, None
43
        return True, dicom
44
45
    @staticmethod
46
    def path(dir_path):
47
        """
48
        Some basic information of the participants must be consistent across the files, such as the SCAN DATE (assuming they are not scanning across MIDNIGHT POINT)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (164/100).

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

Loading history...
49
        Birthday date, subject name, etc MUST BE CONSISTENT across a SINGLE subject's folder, RIGHT!
50
51
        :param dir_path:
52
        :return:
53
        """
54
        logger = logging.getLogger(__name__)
55
56
57
        # Input check
58
        if not os.path.exists(dir_path) or not os.path.isdir(dir_path):
59
            logger.info("Bad data folder path")
60
            return False
61
62
        files = os.listdir(dir_path)
63
64
        # Used to record the first encountered patientID and name, and will check against subsequent folder for same matching information.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (138/100).

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

Loading history...
65
        global PatientID, PatientName
0 ignored issues
show
Coding Style Naming introduced by
The name PatientID does not conform to the constant naming conventions ((([A-Z_][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 Naming introduced by
The name PatientName does not conform to the constant naming conventions ((([A-Z_][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...
Bug introduced by
Global variable 'PatientID' undefined at the module level
Loading history...
Bug introduced by
Global variable 'PatientName' undefined at the module level
Loading history...
66
67
        validated_DICOM_files = []
0 ignored issues
show
Coding Style Naming introduced by
The name validated_DICOM_files 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...
68
69
        from DICOM.elements import DICOM_elements
0 ignored issues
show
introduced by
Unable to import 'DICOM.elements'
Loading history...
70
        logger.info("Checking individual dicom files for patient info consistencies")
71
72
        # Check individual DICOM file for consistencies.
73
        for file in tqdm(files):
74
75
            # Skip current file if they are not DICOM files.
76
            isDICOM, _ = DICOM_validate.file(file)
0 ignored issues
show
Coding Style Naming introduced by
The name isDICOM 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...
77
            if not isDICOM:
78
                continue
79
80
            # Record first instance of patient ID and patient name.
81
            if PatientID is None and PatientName is None:
0 ignored issues
show
Bug introduced by
The variable PatientID was used before it was assigned.
Loading history...
Bug introduced by
The variable PatientName was used before it was assigned.
Loading history...
82
                Success, PatientID = DICOM_elements.retrieve(files[0], "PatientID")
0 ignored issues
show
Coding Style Naming introduced by
The name Success 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...
83
                Success, PatientName = DICOM_elements.retrieve(files[0], "PatientName")
0 ignored issues
show
Coding Style Naming introduced by
The name Success 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...
84
85
                # raise issue if not successful
86
                if not Success:
87
                    logger.info("DICOME meta data retrieval failure.")
88
                    return False
89
                continue
90
91
            # Check consistencies across folders in terms of patient ID, NAME.
92
            CurrentPatientID = DICOM_elements.retrieve(file, "PatientID")
0 ignored issues
show
Coding Style Naming introduced by
The name CurrentPatientID 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...
93
            CurrentPatientName = DICOM_elements.retrieve(file, "PatientName")
0 ignored issues
show
Coding Style Naming introduced by
The name CurrentPatientName 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...
94
95
            if not (PatientID == CurrentPatientID) or not (PatientName == CurrentPatientName):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after not.
Loading history...
96
                logger.info("PatientID or Name mismatch from the dicom archive. .")
97
                return False
98
99
            validated_DICOM_files.append(file)
100
101
        return True, validated_DICOM_files
102
103
    #if __name__ == '__main__':
104