-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery Starbot ⭐ refactored JJBT/RevO #1
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to GitHub API limits, only the first 60 comments can be shown.
backbone = object_from_dict(cfg) | ||
return backbone | ||
return object_from_dict(cfg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function create_backbone
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
train_dataloaders = dict() | ||
train_dataloaders = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function create_train_dataloader
refactored with the following changes:
- Replace dict() with {} (
dict-literal
)
val_dataloaders = dict() | ||
val_dataloaders = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function create_val_dataloader
refactored with the following changes:
- Replace dict() with {} (
dict-literal
)
params = dict() | ||
params['type'] = cfg.type | ||
params['q_root'] = cfg.query.root | ||
params['s_root'] = cfg.support.root | ||
params['q_ann_filename'] = cfg.query.annotations | ||
params['s_ann_filename'] = cfg.support.annotations | ||
params['k_shot'] = cfg.k_shot | ||
params['q_img_size'] = cfg.input_size | ||
params['backbone_stride'] = cfg.backbone_stride | ||
params = { | ||
'type': cfg.type, | ||
'q_root': cfg.query.root, | ||
's_root': cfg.support.root, | ||
'q_ann_filename': cfg.query.annotations, | ||
's_ann_filename': cfg.support.annotations, | ||
'k_shot': cfg.k_shot, | ||
'q_img_size': cfg.input_size, | ||
'backbone_stride': cfg.backbone_stride, | ||
} | ||
|
||
q_transform = create_augmentations(cfg.transforms) | ||
s_transform = create_augmentations(cfg.transforms) | ||
params['q_transform'] = q_transform | ||
params['s_transform'] = s_transform | ||
|
||
dataset = object_from_dict(params) | ||
return dataset | ||
return object_from_dict(params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function create_dataset
refactored with the following changes:
- Merge dictionary assignment with declaration [×8] (
merge-dict-assign
) - Replace dict() with {} (
dict-literal
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
dataset_length = cfg.len | ||
shuffle = cfg.shuffle | ||
|
||
if dataset_length: | ||
if dataset_length := cfg.len: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function create_dataloader
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
) - Use named expression to simplify assignment and conditional (
use-named-expression
)
if denominator == 0: | ||
result = 0. | ||
else: | ||
result = numerator / denominator | ||
|
||
return result | ||
return 0. if denominator == 0 else numerator / denominator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Precision.compute
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if self._total == 0: | ||
result = dict.fromkeys(self._loss_sum_dict.keys(), 0) | ||
else: | ||
result = dict() | ||
for loss_name, loss_value in self._loss_sum_dict.items(): | ||
result[loss_name] = loss_value / self._total | ||
|
||
return result | ||
return ( | ||
dict.fromkeys(self._loss_sum_dict.keys(), 0) | ||
if self._total == 0 | ||
else { | ||
loss_name: loss_value / self._total | ||
for loss_name, loss_value in self._loss_sum_dict.items() | ||
} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function TorchLoss.compute
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Convert for loop into dictionary comprehension (
dict-comprehension
) - Replace dict() with {} (
dict-literal
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
output_img = draw(pr_input['q_img'][0], outputs[0]) | ||
return output_img | ||
return draw(pr_input['q_img'][0], outputs[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function fn
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if self.last_epoch == 0: | ||
if self.last_epoch == 0 or self.last_epoch > self.W_steps: | ||
return self.base_lrs | ||
elif self.last_epoch <= self.W_steps: | ||
return [base_lr * (self.last_epoch / self.W_steps) for base_lr in self.base_lrs] | ||
else: | ||
return self.base_lrs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function WarmUp.get_lr
refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks
) - Remove redundant conditional (
remove-redundant-if
)
self.validation_metrics = dict() | ||
self.validation_metrics = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function State.__init__
refactored with the following changes:
- Replace dict() with {} (
dict-literal
)
msg = f'Validation ' | ||
msg = 'Validation ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function State.log_validation
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
self.accelerator = Accelerator(cpu=bool(cfg.device == 'cpu'), fp16=cfg.amp) | ||
self.accelerator = Accelerator(cpu=cfg.device == 'cpu', fp16=cfg.amp) | ||
self.model, self.optimizer = self.accelerator.prepare(self.model, self.optimizer) | ||
self.train_dataloader_dict, self.val_dataloader_dict = \ | ||
self.prepare_dataloader_dict(self.train_dataloader_dict, self.val_dataloader_dict) | ||
self.prepare_dataloader_dict(self.train_dataloader_dict, self.val_dataloader_dict) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Trainer.__init__
refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
result = list() | ||
result = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Trainer.prepare_dataloader_dict
refactored with the following changes:
- Replace list() with [] (
list-literal
)
if attributes is None: | ||
self._attributes = dict() | ||
else: | ||
self._attributes = attributes | ||
|
||
self._attributes = {} if attributes is None else attributes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Callback.__init__
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Replace dict() with {} (
dict-literal
)
if filename is not None: | ||
self.filename = filename | ||
else: | ||
self.filename = self._search_checkpoint() | ||
|
||
self.filename = filename if filename is not None else self._search_checkpoint() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function LoadCheckpointCallback.__init__
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
filename = phase + f'{i:05d}.png' | ||
filename = f'{phase}{i:05d}.png' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function transform_to_coco_format
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function main
:
elif isinstance(h_resize_limit, tuple) or isinstance(h_resize_limit, list): | ||
elif isinstance(h_resize_limit, (tuple, list)): | ||
assert all(list(map(lambda x: isinstance(x, float), h_resize_limit))) | ||
assert all(list(map(lambda x: 0. <= x, h_resize_limit))) | ||
assert all(list(map(lambda x: x >= 0., h_resize_limit))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function RandomResize.__init__
refactored with the following changes:
- Merge isinstance calls [×2] (
merge-isinstance
) - Ensure constant in comparison is on the right [×2] (
flip-comparison
)
if y < 0 or y + h > self.dataset.H: | ||
return False | ||
return True | ||
return y >= 0 and y + h <= self.dataset.H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function MegapixelMNIST.Sample._locate_pathces
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
colour * np.stack((patch,) * self.n_channels, axis=2) | ||
colour * np.stack((patch,) * self.n_channels, axis=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function MegapixelMNIST.Sample.get_sample
:
sample = self.Sample(self, idxs).get_sample() | ||
|
||
return sample | ||
return self.Sample(self, idxs).get_sample() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function MegapixelMNIST._get_sample
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
filename = phase + f'{i:05d}.png' | ||
filename = f'{phase}{i:05d}.png' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function transform_to_coco_format
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the following improvement in Function main
:
if x < 0 or x + 28 > self._W: | ||
return False | ||
if y < 0 or y + 28 > self._H: | ||
return False | ||
return True | ||
return False if x < 0 or x + 28 > self._W else y >= 0 and y + 28 <= self._H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function MegapixelMNISTGrided._get_positions
refactored with the following changes:
- Lift code into else after jump in control flow [×2] (
reintroduce-else
) - Replace if statement with if expression [×2] (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
filename = phase + f'{i:05d}.png' | ||
filename = f'{phase}{i:05d}.png' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function transform_to_coco_format
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
filtered_coco = { | ||
return { | ||
'images': imgs_to_keep, | ||
'annotations': anns_to_keep, | ||
'categories': cats_to_keep | ||
'categories': cats_to_keep, | ||
} | ||
return filtered_coco |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function filter_coco_by_cats
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
support_image_meta = {} | ||
support_image_meta['file_name'] = str(i).rjust(5, '0') + '.jpg' | ||
support_image_meta = {'file_name': str(i).rjust(5, '0') + '.jpg'} | ||
support_image_meta['height'] = support_image.shape[0] | ||
support_image_meta['weight'] = support_image.shape[1] | ||
support_image_meta['id'] = i | ||
support_images.append(support_image_meta) | ||
|
||
support_ann = {} | ||
support_ann['num_keypoints'] = ann['num_keypoints'] | ||
support_ann['keypoints'] = support_kps | ||
support_ann['image_id'] = support_image_meta['id'] | ||
support_ann['bbox'] = support_bbox | ||
support_ann['category_id'] = ann['category_id'] | ||
support_ann['id'] = i | ||
support_ann = { | ||
'num_keypoints': ann['num_keypoints'], | ||
'keypoints': support_kps, | ||
'image_id': support_image_meta['id'], | ||
'bbox': support_bbox, | ||
'category_id': ann['category_id'], | ||
'id': i, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function gen_support_pool
refactored with the following changes:
- Merge dictionary assignment with declaration [×7] (
merge-dict-assign
)
res = urllib.request.urlopen(req, timeout=10) | ||
return res | ||
return urllib.request.urlopen(req, timeout=10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function url_request
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
context = { | ||
'image_mode': 'RGB', | ||
'shape': self.shape, | ||
'name': self.name | ||
} | ||
return context | ||
return {'image_mode': 'RGB', 'shape': self.shape, 'name': self.name} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ImageInput.get_context
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
return "data:image/png;base64," + base64_str | ||
return f"data:image/png;base64,{base64_str}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function array_to_base64
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
master
branch, then run: