| Conditions | 10 |
| Total Lines | 57 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Python.DICOM.validate.DICOM_validate.path() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import sys |
||
| 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) |
||
| 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. |
||
| 65 | global PatientID, PatientName |
||
| 66 | |||
| 67 | validated_DICOM_files = [] |
||
| 68 | |||
| 69 | from DICOM.elements import DICOM_elements |
||
| 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) |
||
| 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: |
||
| 82 | Success, PatientID = DICOM_elements.retrieve(files[0], "PatientID") |
||
| 83 | Success, PatientName = DICOM_elements.retrieve(files[0], "PatientName") |
||
| 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") |
||
| 93 | CurrentPatientName = DICOM_elements.retrieve(file, "PatientName") |
||
| 94 | |||
| 95 | if not (PatientID == CurrentPatientID) or not (PatientName == CurrentPatientName): |
||
| 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 | |||
| 104 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.