Conditions | 16 |
Total Lines | 65 |
Code Lines | 39 |
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 deepreg.util.save_array() 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 os |
||
75 | def save_array( |
||
76 | save_dir: str, |
||
77 | arr: Union[np.ndarray, tf.Tensor], |
||
78 | name: str, |
||
79 | normalize: bool, |
||
80 | save_nifti: bool = True, |
||
81 | save_png: bool = True, |
||
82 | overwrite: bool = True, |
||
83 | ): |
||
84 | """ |
||
85 | :param save_dir: path of the directory to save |
||
86 | :param arr: 3D or 4D array to be saved |
||
87 | :param name: name of the array, e.g. image, label, etc. |
||
88 | :param normalize: true if the array's value has to be normalized when saving pngs, |
||
89 | false means the value is between [0, 1]. |
||
90 | :param save_nifti: if true, array will be saved in nifti |
||
91 | :param save_png: if true, array will be saved in png |
||
92 | :param overwrite: if false, will not save the file in case the file exists |
||
93 | """ |
||
94 | if isinstance(arr, tf.Tensor): |
||
95 | arr = arr.numpy() |
||
96 | if len(arr.shape) not in [3, 4]: |
||
97 | raise ValueError(f"arr must be 3d or 4d numpy array or tf tensor, got {arr}") |
||
98 | is_4d = len(arr.shape) == 4 |
||
99 | if is_4d: |
||
100 | # if 4D array, it must be 3 channels |
||
101 | if arr.shape[3] != 3: |
||
102 | raise ValueError( |
||
103 | f"4d arr must have 3 channels as last dimension, " |
||
104 | f"got arr.shape = {arr.shape}" |
||
105 | ) |
||
106 | |||
107 | # save in nifti format |
||
108 | if save_nifti: |
||
109 | nifti_file_path = os.path.join(save_dir, name + ".nii.gz") |
||
110 | if overwrite or (not os.path.exists(nifti_file_path)): |
||
111 | # save only if need to overwrite or doesn't exist |
||
112 | os.makedirs(save_dir, exist_ok=True) |
||
113 | # output with Nifti1Image can be loaded by |
||
114 | # - https://www.slicer.org/ |
||
115 | # - http://www.itksnap.org/ |
||
116 | # - http://ric.uthscsa.edu/mango/ |
||
117 | # However, outputs with Nifti2Image couldn't be loaded |
||
118 | nib.save( |
||
119 | img=nib.Nifti1Image(arr, affine=np.eye(4)), filename=nifti_file_path |
||
120 | ) |
||
121 | |||
122 | # save in png |
||
123 | if save_png: |
||
124 | png_dir = os.path.join(save_dir, name) |
||
125 | dir_existed = os.path.exists(png_dir) |
||
126 | if normalize: |
||
127 | # normalize arr such that it has only values between 0, 1 |
||
128 | arr = normalize_array(arr=arr) |
||
129 | for depth_index in range(arr.shape[2]): |
||
130 | png_file_path = os.path.join(png_dir, f"depth{depth_index}_{name}.png") |
||
131 | if overwrite or (not os.path.exists(png_file_path)): |
||
132 | if not dir_existed: |
||
133 | os.makedirs(png_dir, exist_ok=True) |
||
134 | plt.imsave( |
||
135 | fname=png_file_path, |
||
136 | arr=arr[:, :, depth_index, :] if is_4d else arr[:, :, depth_index], |
||
137 | vmin=0, |
||
138 | vmax=1, |
||
139 | cmap="PiYG" if is_4d else "gray", |
||
140 | ) |
||
224 |