| Conditions | 6 |
| Total Lines | 63 |
| Code Lines | 30 |
| 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:
| 1 | import sys |
||
| 13 | @staticmethod |
||
| 14 | def into_folder(input_folder, output_folder): |
||
| 15 | """ |
||
| 16 | This function sort a input folder with or without sub layers and automaticlly flatten everything into a folder before then MOVING them into protocol based folder sorted by acquisition series. |
||
| 17 | :param input_folder: Input_folder can be a root folder or flat. |
||
| 18 | :return: |
||
| 19 | """ |
||
| 20 | logger = logging.getLogger("DICOM sorting operation") |
||
| 21 | |||
| 22 | #Element to check: Series number. |
||
| 23 | |||
| 24 | from PythonUtils.file import flatcopy |
||
| 25 | from PythonUtils.folder import recursive_list |
||
| 26 | |||
| 27 | # Get files |
||
| 28 | file_list = recursive_list(input_folder) |
||
| 29 | |||
| 30 | if not os.path.isdir(output_folder): |
||
| 31 | os.mkdir(output_folder) |
||
| 32 | |||
| 33 | |||
| 34 | # copy them to a flat structure to the output folder. |
||
| 35 | flatcopy(file_list, output_folder, DICOM_validate.file) |
||
| 36 | |||
| 37 | # decompress them if necessary. |
||
| 38 | # oshelper_files.decompress_folder(output_folder) |
||
| 39 | |||
| 40 | # Get files list again. |
||
| 41 | file_list = recursive_list(output_folder) |
||
| 42 | |||
| 43 | exception_encountered = 0 |
||
| 44 | |||
| 45 | logger.info("Sorting files into folders:") |
||
| 46 | |||
| 47 | # File here should be the FULL path. |
||
| 48 | for file in tqdm(file_list): |
||
| 49 | |||
| 50 | success1, SeriesNumber = DICOM_elements.retrieve(file, "SeriesNumber") |
||
| 51 | |||
| 52 | success2, SeriesDescription = DICOM_elements.retrieve(file, "SeriesDescription") |
||
| 53 | |||
| 54 | if not success1 or not success2: |
||
| 55 | logger.info("Skipped file with no acquisition series information: " + file) |
||
| 56 | exception_encountered = exception_encountered + 1 |
||
| 57 | continue |
||
| 58 | |||
| 59 | # Check MRI Series folder exists |
||
| 60 | DestinationFolder = str(SeriesNumber) + '_' + SeriesDescription |
||
| 61 | DestinationFolder = DestinationFolder.replace(' ', '_') |
||
| 62 | DestinationFolder = DestinationFolder.replace(':', '_') |
||
| 63 | DestinationFolder = DestinationFolder.replace(r'/', '_') |
||
| 64 | DestinationFolder = DestinationFolder.replace(r'\\', '_') |
||
| 65 | |||
| 66 | # Make destination folder if not exist. |
||
| 67 | os.chdir(output_folder) |
||
| 68 | if not os.path.exists(DestinationFolder): |
||
| 69 | os.mkdir(DestinationFolder) |
||
| 70 | |||
| 71 | # Get file name. |
||
| 72 | _, filename = os.path.split(file) |
||
| 73 | |||
| 74 | shutil.move(file, os.path.join(DestinationFolder, filename)) |
||
| 75 | logger.info("Total error encoutnered: " + str(exception_encountered)) |
||
| 76 | |||
| 80 |
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.