WIP Code for ISMRM23 (Sourcery refactored)#3
WIP Code for ISMRM23 (Sourcery refactored)#3sourcery-ai[bot] wants to merge 1 commit intoWIP_ISMRM23from
Conversation
| @@ -2,6 +2,7 @@ | |||
| """ | |||
| """ | |||
|
|
|||
There was a problem hiding this comment.
Lines 184-210 refactored with the following changes:
- Use f-string instead of string concatenation [×18] (
use-fstring-for-concatenation)
| self.model_type = cmd_args.model | ||
| self.lr_1 = cmd_args.learning_rate | ||
| self.logger.info("learning rate " + str(self.lr_1)) | ||
| self.logger.info(f"learning rate {str(self.lr_1)}") |
There was a problem hiding this comment.
Function Pipeline.__init__ refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| trainDS = SRDataset(logger=self.logger, patch_size=self.patch_size, | ||
| dir_path=vol_path, | ||
| label_dir_path=label_path, | ||
| # TODO: implement non-iso patch-size, now only using the first element | ||
| stride_depth=self.stride_depth, stride_length=self.stride_length, | ||
| stride_width=self.stride_width, Size=None, fly_under_percent=None, | ||
| # TODO: implement fly_under_percent, if needed | ||
| patch_size_us=self.patch_size, pre_interpolate=None, norm_data=False, | ||
| pre_load=True, | ||
| return_coords=True, | ||
| files_us=crossvalidation_set) # TODO implement patch_size_us if required - patch_size//scaling_factor | ||
| if get_subjects_only: | ||
| return trainDS | ||
| # sampler = tio.data.UniformSampler(self.patch_size) | ||
| # patches_queue = tio.Queue( | ||
| # trainDS, | ||
| # max_length=(self.samples_per_epoch // len(trainDS.pre_loaded_data['pre_loaded_img'])) * 2, | ||
| # samples_per_volume=1, | ||
| # sampler=sampler, | ||
| # num_workers=0, | ||
| # start_background=True | ||
| # ) | ||
| # return patches_queue | ||
| return trainDS | ||
| return SRDataset( | ||
| logger=self.logger, | ||
| patch_size=self.patch_size, | ||
| dir_path=vol_path, | ||
| label_dir_path=label_path, | ||
| # TODO: implement non-iso patch-size, now only using the first element | ||
| stride_depth=self.stride_depth, | ||
| stride_length=self.stride_length, | ||
| stride_width=self.stride_width, | ||
| Size=None, | ||
| fly_under_percent=None, | ||
| # TODO: implement fly_under_percent, if needed | ||
| patch_size_us=self.patch_size, | ||
| pre_interpolate=None, | ||
| norm_data=False, | ||
| pre_load=True, | ||
| return_coords=True, | ||
| files_us=crossvalidation_set, | ||
| ) |
There was a problem hiding this comment.
Function Pipeline.create_TIOSubDS refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Lift code into else after jump in control flow (
reintroduce-else) - Hoist repeated code outside conditional statement (
hoist-statement-from-if) - Replace index in for loop with direct reference (
for-index-replacement)
This removes the following comments ( why? ):
# num_workers=0,
# max_length=(self.samples_per_epoch // len(trainDS.pre_loaded_data['pre_loaded_img'])) * 2,
# return patches_queue
# TODO implement patch_size_us if required - patch_size//scaling_factor
# )
# start_background=True
# sampler = tio.data.UniformSampler(self.patch_size)
# samples_per_volume=1,
# patches_queue = tio.Queue(
# sampler=sampler,
# trainDS,
| union = torch.sum(y_true_f + y_pred_f) - intersection | ||
| score = (intersection + self.smooth) / (union + self.smooth) | ||
| return score | ||
| return (intersection + self.smooth) / (union + self.smooth) |
There was a problem hiding this comment.
Function IOU.forward refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| torch.save(param, os.path.join(self.output_dir, 'nan_floss_{}_param.pt'.format(name))) | ||
| raise RuntimeError(" classname " + self.__class__.__name__ + "i " + str( | ||
| i) + f" module: {module} classname {self.__class__.__name__} Found NAN in output {i} at indices: ", | ||
| nan_mask.nonzero(), "where:", out[nan_mask.nonzero()[:, 0].unique(sorted=True)]) | ||
| torch.save(param, os.path.join(self.output_dir, f'nan_floss_{name}_param.pt')) | ||
| raise RuntimeError( | ||
| ( | ||
| f" classname {self.__class__.__name__}i {str(i)}" | ||
| + f" module: {module} classname {self.__class__.__name__} Found NAN in output {i} at indices: " | ||
| ), | ||
| nan_mask.nonzero(), | ||
| "where:", | ||
| out[nan_mask.nonzero()[:, 0].unique(sorted=True)], | ||
| ) |
There was a problem hiding this comment.
Function FocalTverskyLoss.nan_hook refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting) - Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation)
| underKSPVol = performUndersamplingKSP(fullKSPVol, mask, maskmatpath, zeropad) | ||
| underImgVol = ifft2c(underKSPVol) | ||
| return underImgVol | ||
| return ifft2c(underKSPVol) |
There was a problem hiding this comment.
Function performUndersampling refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| underKSPVol = np.multiply(fullKSPVol.transpose((2, 0, 1)), mask).transpose((1, 2, 0)) | ||
| else: | ||
| temp = [] | ||
| for i in range(mask.shape[0]): | ||
| maskline = mask[i, :] | ||
| if maskline.any(): | ||
| temp.append(fullKSPVol[i, ...]) | ||
| temp = np.array(temp) | ||
| underKSPVol = [] | ||
| for i in range(mask.shape[1]): | ||
| maskline = mask[:, i] | ||
| if maskline.any(): | ||
| underKSPVol.append(temp[:, i, ...]) | ||
| underKSPVol = np.array(underKSPVol).swapaxes(0, 1) | ||
| return underKSPVol | ||
| return np.multiply(fullKSPVol.transpose((2, 0, 1)), mask).transpose((1, 2, 0)) | ||
| temp = [] | ||
| for i in range(mask.shape[0]): | ||
| maskline = mask[i, :] | ||
| if maskline.any(): | ||
| temp.append(fullKSPVol[i, ...]) | ||
| temp = np.array(temp) | ||
| underKSPVol = [] | ||
| for i in range(mask.shape[1]): | ||
| maskline = mask[:, i] | ||
| if maskline.any(): | ||
| underKSPVol.append(temp[:, i, ...]) | ||
| return np.array(underKSPVol).swapaxes(0, 1) |
There was a problem hiding this comment.
Function performUndersamplingKSP refactored with the following changes:
- Lift return into if (
lift-return-into-if) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| if closure is not None: | ||
| loss = closure() | ||
|
|
||
| loss = closure() if closure is not None else None |
There was a problem hiding this comment.
Function Madam.step refactored with the following changes:
- Move setting of default value for variable into
elsebranch (introduce-default-else) - Replace if statement with if expression (
assign-if-exp)
| print('convert_and_save_tif:size of image:'+ str(len(image_list))) | ||
| print(f'convert_and_save_tif:size of image:{len(image_list)}') | ||
| with TiffImagePlugin.AppendingTiffWriter(output_path + filename, True) as tifWriter: | ||
| for im in image_list: | ||
| # with open(DATASET_FOLDER+tiff_in) as tiff_in: | ||
| im.save(tifWriter) | ||
| tifWriter.newFrame() | ||
| print("Conversion to tiff completed, image saved as {}".format(filename)) | ||
| print(f"Conversion to tiff completed, image saved as {filename}") |
There was a problem hiding this comment.
Function convert_and_save_tif refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation) - Replace call to format with f-string (
use-fstring-for-formatting) - Remove unnecessary calls to
str()from formatted values in f-strings (remove-str-from-fstring)
| print('convert_and_save_tif:size of image:' + str(len(image_list))) | ||
| print(f'convert_and_save_tif:size of image:{len(image_list)}') | ||
| with TiffImagePlugin.AppendingTiffWriter(output_path + filename, True) as tifWriter: | ||
| for im in image_list: | ||
| # with open(DATASET_FOLDER+tiff_in) as tiff_in: | ||
| im.save(tifWriter) | ||
| tifWriter.newFrame() | ||
| print("Conversion to tiff completed, image saved as {}".format(filename)) | ||
| print(f"Conversion to tiff completed, image saved as {filename}") |
There was a problem hiding this comment.
Function convert_and_save_tif_greyscale refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation) - Replace call to format with f-string (
use-fstring-for-formatting) - Remove unnecessary calls to
str()from formatted values in f-strings (remove-str-from-fstring)
Pull Request #2 refactored by Sourcery.
Since the original Pull Request was opened as a fork in a contributor's
repository, we are unable to create a Pull Request branching from it.
To incorporate these changes, you can either:
Merge this Pull Request instead of the original, or
Ask your contributor to locally incorporate these commits and push them to
the original Pull Request
Incorporate changes via command line
NOTE: As code is pushed to the original Pull Request, Sourcery will
re-run and update (force-push) this Pull Request with new refactorings as
necessary. If Sourcery finds no refactorings at any point, this Pull Request
will be closed automatically.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Help us improve this pull request!