| Conditions | 7 | 
| Total Lines | 59 | 
| Code Lines | 49 | 
| 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 ast  | 
            ||
| 51 | def create_dummy_dataset(  | 
            ||
| 52 | num_images: int,  | 
            ||
| 53 | size_range: Tuple[int, int],  | 
            ||
| 54 | directory: Optional[TypePath] = None,  | 
            ||
| 55 | suffix: str = '.nii.gz',  | 
            ||
| 56 | force: bool = False,  | 
            ||
| 57 | verbose: bool = False,  | 
            ||
| 58 | ):  | 
            ||
| 59 | from .data import ScalarImage, LabelMap, Subject  | 
            ||
| 60 | output_dir = tempfile.gettempdir() if directory is None else directory  | 
            ||
| 61 | output_dir = Path(output_dir)  | 
            ||
| 62 | images_dir = output_dir / 'dummy_images'  | 
            ||
| 63 | labels_dir = output_dir / 'dummy_labels'  | 
            ||
| 64 | |||
| 65 | if force:  | 
            ||
| 66 | shutil.rmtree(images_dir)  | 
            ||
| 67 | shutil.rmtree(labels_dir)  | 
            ||
| 68 | |||
| 69 | subjects: List[Subject] = []  | 
            ||
| 70 | if images_dir.is_dir():  | 
            ||
| 71 | for i in trange(num_images):  | 
            ||
| 72 |             image_path = images_dir / f'image_{i}{suffix}' | 
            ||
| 73 |             label_path = labels_dir / f'label_{i}{suffix}' | 
            ||
| 74 | subject = Subject(  | 
            ||
| 75 | one_modality=ScalarImage(image_path),  | 
            ||
| 76 | segmentation=LabelMap(label_path),  | 
            ||
| 77 | )  | 
            ||
| 78 | subjects.append(subject)  | 
            ||
| 79 | else:  | 
            ||
| 80 | images_dir.mkdir(exist_ok=True, parents=True)  | 
            ||
| 81 | labels_dir.mkdir(exist_ok=True, parents=True)  | 
            ||
| 82 | if verbose:  | 
            ||
| 83 |             print('Creating dummy dataset...')  # noqa: T001 | 
            ||
| 84 | iterable = trange(num_images)  | 
            ||
| 85 | else:  | 
            ||
| 86 | iterable = range(num_images)  | 
            ||
| 87 | for i in iterable:  | 
            ||
| 88 | shape = np.random.randint(*size_range, size=3)  | 
            ||
| 89 | affine = np.eye(4)  | 
            ||
| 90 | image = np.random.rand(*shape)  | 
            ||
| 91 | label = np.ones_like(image)  | 
            ||
| 92 | label[image < 0.33] = 0  | 
            ||
| 93 | label[image > 0.66] = 2  | 
            ||
| 94 | image *= 255  | 
            ||
| 95 | |||
| 96 |             image_path = images_dir / f'image_{i}{suffix}' | 
            ||
| 97 | nii = nib.Nifti1Image(image.astype(np.uint8), affine)  | 
            ||
| 98 | nii.to_filename(str(image_path))  | 
            ||
| 99 | |||
| 100 |             label_path = labels_dir / f'label_{i}{suffix}' | 
            ||
| 101 | nii = nib.Nifti1Image(label.astype(np.uint8), affine)  | 
            ||
| 102 | nii.to_filename(str(label_path))  | 
            ||
| 103 | |||
| 104 | subject = Subject(  | 
            ||
| 105 | one_modality=ScalarImage(image_path),  | 
            ||
| 106 | segmentation=LabelMap(label_path),  | 
            ||
| 107 | )  | 
            ||
| 108 | subjects.append(subject)  | 
            ||
| 109 | return subjects  | 
            ||
| 110 | |||
| 201 |