Passed
Push — master ( 9e6838...cf264a )
by Yang
27s
created

Python.DICOM.elements.DICOM_computeScanAge()   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
cc 2
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
3
from DICOM.validate import DICOM_validator
0 ignored issues
show
introduced by
Unable to import 'DICOM.validate'
Loading history...
4
5
6
def DICOM_retrieveElements(file_path, data_element):
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_retrieveElements 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...
7
    """
8
    A low level function used to retrieve elements from DICOM and return a LIST of matching element. ACCEPT PARTIAL MATCH
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...
9
    :param file_path:
10
    :param data_element:
11
    :return: LIST of all data elements that match the pattern provided in the data_element and their value.  NO Regular EXPRESSION.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (131/100).

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

Loading history...
12
    """
13
    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...
14
    if not success:
15
        return False, None, None
16
17
    # Get a list of all data elements that can have element label.
18
    element_values = DICOM.data_element(data_element).value
19
20
    return True, element_values
21
22
23
def DICOM_updateElement(file_path, data_element, element_value, out_path):
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_updateElement 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...
24
    """
25
    Update a particular data_element to the desired value, then write back to the SOURCE FILE!
26
    :param file_path:
27
    :param data_element:
28
    :param element_value:
29
    :return: bool on operation success, and string on reason.
30
    """
31
32
    """BE AWARE that if the key does not exist, it will not be created currently!"""
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
33
    logger = logging.getLogger(__name__)
34
    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...
35
    if not success:
36
        return False, "DICOM not valid."
37
38
    try:
39
        DICOM.data_element(data_element).value = element_value
40
    except KeyError:
41
        logger.info("Key " + data_element + " does not exist, creating the key.")
42
        return False, "DICOM key field does not exist. Not sure how to create one yet. "
43
    DICOM.save_as(out_path)
44
    return True, "Data element update completed."
45
46
47
def DICOM_retrieveMRN(file_path):
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_retrieveMRN 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...
48
    """
49
    Read the PatientID field which normally used as MRN number.
50
    :param file_path:
51
    :return: MRN number, as a STRING
52
    """
53
    success, value = DICOM_retrieveElements(file_path, "PatientID")
54
55
    if not success:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
56
        return False, None
57
    else:
58
        return True, value
59
60
61
def DICOM_computeScanAge(file_path):
0 ignored issues
show
Coding Style Naming introduced by
The name DICOM_computeScanAge 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...
62
    """
63
    Read the PatientID field which normally used as MRN number.
64
    :param file_path:
65
    :return: Age as a relative delta time object.
66
    """
67
68
    from dateutil.relativedelta import relativedelta
69
70
    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...
71
    if not success:
72
        return False, None
73
    from datetime import datetime
74
    scan_date = datetime.strptime(DICOM.SeriesDate, "%Y%m%d")
75
    birthday = datetime.strptime(DICOM.PatientBirthDate, "%Y%m%d")
76
    age = relativedelta(scan_date, birthday)
77
    return True, age
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...