| Conditions | 5 |
| Total Lines | 51 |
| Code Lines | 41 |
| 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 urllib.parse |
||
| 33 | def __init__(self, load_4d_tissues: bool = True): |
||
| 34 | self.name = f'mni_icbm152_nlin_sym_09c_nifti' |
||
| 35 | self.url_base = 'http://www.bic.mni.mcgill.ca/~vfonov/icbm/2009/' |
||
| 36 | dir_name = 'icbm_2009c_nonlinear_symmetric/' |
||
| 37 | self.filename = f'{self.name}.zip' |
||
| 38 | self.url = urllib.parse.urljoin(self.url_base, self.filename) |
||
| 39 | download_root = get_torchio_cache_dir() / self.name |
||
| 40 | if download_root.is_dir(): |
||
| 41 | print(f'Using cache found in {download_root}') |
||
| 42 | else: |
||
| 43 | download_and_extract_archive( |
||
| 44 | self.url, |
||
| 45 | download_root=download_root, |
||
| 46 | filename=self.filename, |
||
| 47 | remove_finished=True, |
||
| 48 | ) |
||
| 49 | |||
| 50 | files_dir = download_root / 'mni_icbm152_nlin_sym_09c' |
||
| 51 | |||
| 52 | p = files_dir / 'mni_icbm152' |
||
| 53 | m = 'tal_nlin_sym_09c' |
||
| 54 | s = '.nii.gz' |
||
| 55 | |||
| 56 | tissues_path = files_dir / f'{p}_tissues_{m}.nii.gz' |
||
| 57 | if not tissues_path.is_file(): |
||
| 58 | gm = LabelMap(f'{p}_gm_{m}.nii') |
||
| 59 | wm = LabelMap(f'{p}_wm_{m}.nii') |
||
| 60 | csf = LabelMap(f'{p}_csf_{m}.nii') |
||
| 61 | gm[DATA] = torch.cat((gm[DATA], wm[DATA], csf[DATA])) |
||
|
|
|||
| 62 | gm.save(tissues_path) |
||
| 63 | |||
| 64 | for fp in files_dir.glob('*.nii'): |
||
| 65 | compress(fp, fp.with_suffix('.nii.gz')) |
||
| 66 | fp.unlink() |
||
| 67 | |||
| 68 | subject_dict = dict( |
||
| 69 | t1=ScalarImage(f'{p}_t1_{m}{s}'), |
||
| 70 | eyes=LabelMap(f'{p}_t1_{m}_eye_mask{s}'), |
||
| 71 | face=LabelMap(f'{p}_t1_{m}_face_mask{s}'), |
||
| 72 | brain=LabelMap(f'{p}_t1_{m}_mask{s}'), |
||
| 73 | t2=ScalarImage(f'{p}_t2_{m}{s}'), |
||
| 74 | pd=ScalarImage(f'{p}_csf_{m}{s}'), |
||
| 75 | ) |
||
| 76 | if load_4d_tissues: |
||
| 77 | subject_dict['tissues'] = LabelMap(tissues_path) |
||
| 78 | else: |
||
| 79 | subject_dict['gm'] = LabelMap(f'{p}_gm_{m}{s}') |
||
| 80 | subject_dict['wm'] = LabelMap(f'{p}_wm_{m}{s}') |
||
| 81 | subject_dict['csf'] = LabelMap(f'{p}_csf_{m}{s}') |
||
| 82 | |||
| 83 | super().__init__(subject_dict) |
||
| 84 |