-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored main branch #1
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
base: main
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.
default=list(), | ||
default=[], | ||
) | ||
|
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 get_parser
refactored with the following changes:
- Replace list() with [] (
list-literal
)
self.dataset_configs = dict() | ||
self.dataset_configs = {} |
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 DataModuleFromConfig.__init__
refactored with the following changes:
- Replace dict() with {} (
dict-literal
)
self.datasets = dict( | ||
(k, instantiate_from_config(self.dataset_configs[k])) | ||
self.datasets = { | ||
k: instantiate_from_config(self.dataset_configs[k]) | ||
for k in self.dataset_configs | ||
) | ||
} | ||
|
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 DataModuleFromConfig.setup
refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
)
shuffle=False if is_iterable_dataset else True, | ||
shuffle=not is_iterable_dataset, |
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 DataModuleFromConfig._train_dataloader
refactored with the following changes:
- Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
if 'callbacks' in self.lightning_config: | ||
if ( | ||
'metrics_over_trainsteps_checkpoint' | ||
in self.lightning_config['callbacks'] | ||
): | ||
os.makedirs( | ||
os.path.join(self.ckptdir, 'trainstep_checkpoints'), | ||
exist_ok=True, | ||
) | ||
if 'callbacks' in self.lightning_config and ( | ||
'metrics_over_trainsteps_checkpoint' | ||
in self.lightning_config['callbacks'] | ||
): | ||
os.makedirs( | ||
os.path.join(self.ckptdir, 'trainstep_checkpoints'), | ||
exist_ok=True, | ||
) | ||
print('Project config') | ||
print(OmegaConf.to_yaml(self.config)) | ||
OmegaConf.save( | ||
self.config, | ||
os.path.join(self.cfgdir, '{}-project.yaml'.format(self.now)), | ||
self.config, os.path.join(self.cfgdir, f'{self.now}-project.yaml') | ||
) | ||
|
||
|
||
print('Lightning config') | ||
print(OmegaConf.to_yaml(self.lightning_config)) | ||
OmegaConf.save( | ||
OmegaConf.create({'lightning': self.lightning_config}), | ||
os.path.join( | ||
self.cfgdir, '{}-lightning.yaml'.format(self.now) | ||
), | ||
os.path.join(self.cfgdir, f'{self.now}-lightning.yaml'), | ||
) | ||
|
||
else: | ||
# ModelCheckpoint callback created log directory --- remove it | ||
if not self.resume and os.path.exists(self.logdir): | ||
dst, name = os.path.split(self.logdir) | ||
dst = os.path.join(dst, 'child_runs', name) | ||
os.makedirs(os.path.split(dst)[0], exist_ok=True) | ||
try: | ||
os.rename(self.logdir, dst) | ||
except FileNotFoundError: | ||
pass | ||
|
||
elif not self.resume and os.path.exists(self.logdir): | ||
dst, name = os.path.split(self.logdir) | ||
dst = os.path.join(dst, 'child_runs', name) | ||
os.makedirs(os.path.split(dst)[0], exist_ok=True) | ||
try: | ||
os.rename(self.logdir, dst) | ||
except FileNotFoundError: | ||
pass |
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 SetupCallback.on_pretrain_routine_start
refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Merge nested if conditions (
merge-nested-ifs
) - Replace call to format with f-string [×2] (
use-fstring-for-formatting
)
This removes the following comments ( why? ):
# ModelCheckpoint callback created log directory --- remove it
additional_allowed_origins = ( | ||
opt.cors if opt.cors else [] | ||
) # additional CORS allowed origins | ||
additional_allowed_origins = opt.cors or [] |
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.
Lines 55-57
refactored with the following changes:
- Simplify if expression by using or (
or-if-exp-identity
)
This removes the following comments ( why? ):
# additional CORS allowed origins
logger = True if verbose else False | ||
engineio_logger = True if verbose else False | ||
logger = bool(verbose) | ||
engineio_logger = bool(verbose) |
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.
Lines 93-136
refactored with the following changes:
- Simplify boolean if expression [×2] (
boolean-if-exp-identity
) - Remove redundant exceptions from an except clause (
remove-redundant-exception
) - Replace length-one exception tuple with exception (
simplify-single-exception-tuple
)
This removes the following comments ( why? ):
# coreformer.process(self, image, strength, device, seed=None, fidelity=0.75)
print(f">> System config requested") | ||
print(">> System config requested") |
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 handle_request_capabilities
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
print(f">> Latest images requested") | ||
print(">> Latest images requested") |
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 handle_request_images
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
) - Simplify boolean if expression (
boolean-if-exp-identity
)
print(f">> Cancel processing requested") | ||
print(">> Cancel processing requested") |
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 handle_cancel
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
rfc_dict["postprocessing"] = postprocessing if len(postprocessing) > 0 else None | ||
rfc_dict["postprocessing"] = postprocessing or None |
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 parameters_to_generated_image_metadata
refactored with the following changes:
- Simplify if expression by using or (
or-if-exp-identity
) - Simplify sequence length comparison (
simplify-len-comparison
)
if "image" in metadata: | ||
if "seed" in metadata["image"]: | ||
seed = metadata["image"]["seed"] | ||
if "image" in metadata and "seed" in metadata["image"]: | ||
seed = metadata["image"]["seed"] | ||
|
||
filename = f"{prefix}.{seed}" | ||
|
||
if step_index: | ||
filename += f".{step_index}" | ||
if postprocessing: | ||
filename += f".postprocessed" | ||
filename += ".postprocessed" | ||
|
||
filename += ".png" | ||
|
||
path = pngwriter.save_image_and_prompt_to_png( | ||
return pngwriter.save_image_and_prompt_to_png( | ||
image=image, dream_prompt=command, metadata=metadata, name=filename | ||
) | ||
|
||
return path |
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 save_image
refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs
) - Replace f-string with no interpolated values with string (
remove-redundant-fstring
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
""" | ||
|
||
switches = list() | ||
switches = [] |
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 parameters_to_command
refactored with the following changes:
- Replace list() with [] (
list-literal
) - Replace f-string with no interpolated values with string [×2] (
remove-redundant-fstring
)
if not seed >= 0: | ||
if seed < 0: | ||
return False | ||
|
||
# Weight must be between 0 and 1 | ||
if not (weight >= 0 and weight <= 1): | ||
if weight < 0 or weight > 1: |
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 parse_seed_weights
refactored with the following changes:
- Simplify logical expression using De Morgan identities [×2] (
de-morgan
)
hires_fix = False, | ||
**args, | ||
): # eat up additional cruft | ||
): # eat up additional cruft |
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 Generate.prompt2image
refactored with the following changes:
- Use x is None rather than x == None [×2] (
none-compare
) - Replace list() with [] (
list-literal
) - Replace f-string with no interpolated values with string [×2] (
remove-redundant-fstring
)
interval = 0 | ||
for cl in self.cum_cycles[1:]: | ||
for interval, cl in enumerate(self.cum_cycles[1:]): | ||
if n <= cl: | ||
return interval | ||
interval += 1 |
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 LambdaWarmUpCosineScheduler2.find_in_interval
refactored with the following changes:
- Replace manual loop counter with call to enumerate (
convert-to-enumerate
)
if self.verbosity_interval > 0: | ||
if n % self.verbosity_interval == 0: | ||
print( | ||
f'current step: {n}, recent lr-multiplier: {self.last_f}, ' | ||
f'current cycle {cycle}' | ||
) | ||
if self.verbosity_interval > 0 and n % self.verbosity_interval == 0: | ||
print( | ||
f'current step: {n}, recent lr-multiplier: {self.last_f}, ' | ||
f'current cycle {cycle}' | ||
) | ||
if n < self.lr_warm_up_steps[cycle]: | ||
f = ( | ||
self.f_max[cycle] - self.f_start[cycle] | ||
) / self.lr_warm_up_steps[cycle] * n + self.f_start[cycle] | ||
self.last_f = f | ||
return f |
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 LambdaWarmUpCosineScheduler2.schedule
refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs
) - Hoist repeated code outside conditional statement [×2] (
hoist-statement-from-if
)
if self.verbosity_interval > 0: | ||
if n % self.verbosity_interval == 0: | ||
print( | ||
f'current step: {n}, recent lr-multiplier: {self.last_f}, ' | ||
f'current cycle {cycle}' | ||
) | ||
if self.verbosity_interval > 0 and n % self.verbosity_interval == 0: | ||
print( | ||
f'current step: {n}, recent lr-multiplier: {self.last_f}, ' | ||
f'current cycle {cycle}' | ||
) | ||
|
||
if n < self.lr_warm_up_steps[cycle]: | ||
f = ( | ||
self.f_max[cycle] - self.f_start[cycle] | ||
) / self.lr_warm_up_steps[cycle] * n + self.f_start[cycle] | ||
self.last_f = f | ||
return f | ||
else: | ||
f = self.f_min[cycle] + (self.f_max[cycle] - self.f_min[cycle]) * ( | ||
self.cycle_lengths[cycle] - n | ||
) / (self.cycle_lengths[cycle]) | ||
self.last_f = f | ||
return f | ||
|
||
self.last_f = f | ||
return f |
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 LambdaLinearScheduler.schedule
refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs
) - Hoist repeated code outside conditional statement [×2] (
hoist-statement-from-if
)
print(f'>> The ldm.simplet2i module is deprecated. Use ldm.generate instead. It is a drop-in replacement.') | ||
print( | ||
'>> The ldm.simplet2i module is deprecated. Use ldm.generate instead. It is a drop-in replacement.' | ||
) | ||
|
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 T2I.__init__
refactored with the following changes:
- Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
# xc a list of captions to plot | ||
b = len(xc) | ||
txts = list() | ||
txts = [] |
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 log_txt_as_img
refactored with the following changes:
- Replace list() with [] (
list-literal
)
if not isinstance(x, torch.Tensor): | ||
return False | ||
return (len(x.shape) == 4) and (x.shape[1] > 3) | ||
return ( | ||
(len(x.shape) == 4) and (x.shape[1] > 3) | ||
if isinstance(x, torch.Tensor) | ||
else False | ||
) |
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 ismap
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
) - Swap if/else branches of if expression to remove negation (
swap-if-expression
)
if not isinstance(x, torch.Tensor): | ||
return False | ||
return (len(x.shape) == 4) and (x.shape[1] == 3 or x.shape[1] == 1) | ||
return ( | ||
len(x.shape) == 4 and x.shape[1] in [3, 1] | ||
if isinstance(x, torch.Tensor) | ||
else False | ||
) |
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 isimage
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
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
) - Swap if/else branches of if expression to remove negation (
swap-if-expression
)
if idx_to_fn: | ||
res = func(data, worker_id=idx) | ||
else: | ||
res = func(data) | ||
res = func(data, worker_id=idx) if idx_to_fn else func(data) |
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 _do_parallel_data_prefetch
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
f'WARNING:"data" argument passed to parallel_data_prefetch is a dict: Using only its values and disregarding keys.' | ||
'WARNING:"data" argument passed to parallel_data_prefetch is a dict: Using only its values and disregarding keys.' | ||
) | ||
|
||
data = list(data.values()) | ||
if target_data_type == 'ndarray': | ||
data = np.asarray(data) | ||
else: | ||
data = list(data) | ||
data = np.asarray(data) if target_data_type == 'ndarray' else list(data) |
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 parallel_data_prefetch
refactored with the following changes:
- Replace f-string with no interpolated values with string [×2] (
remove-redundant-fstring
) - Replace if statement with if expression [×2] (
assign-if-exp
) - Replace unneeded comprehension with generator (
comprehension-to-generator
) - Lift code into else after jump in control flow (
reintroduce-else
) - Swap if/else branches of if expression to remove negation (
swap-if-expression
)
This removes the following comments ( why? ):
# order outputs
with open(path_to_yaml) as f: | ||
di2s = yaml.load(f) | ||
return dict((v, k) for k, v in di2s.items()) | ||
return {v: k for k, v in di2s.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 synset2idx
refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
)
example = dict((k, self.labels[k][i]) for k in self.labels) | ||
example = {k: self.labels[k][i] for k in self.labels} | ||
image = Image.open(example['file_path_']) | ||
if not image.mode == 'RGB': | ||
if image.mode != 'RGB': |
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 LSUNBase.__getitem__
refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
) - Simplify logical expression using De Morgan identities (
de-morgan
)
example = {} | ||
image = Image.open(self.image_paths[i % self.num_images]) | ||
|
||
if not image.mode == 'RGB': | ||
if image.mode != 'RGB': |
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 PersonalizedBase.__getitem__
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Simplify logical expression using De Morgan identities (
de-morgan
) - Merge dictionary assignment with declaration (
merge-dict-assign
)
example = {} | ||
image = Image.open(self.image_paths[i % self.num_images]) | ||
|
||
if not image.mode == 'RGB': | ||
if image.mode != 'RGB': |
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 PersonalizedBase.__getitem__
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Simplify logical expression using De Morgan identities (
de-morgan
) - Merge dictionary assignment with declaration (
merge-dict-assign
)
# if no optionals or positionals are available, usage is just prog | ||
elif usage is None and not actions: | ||
elif not actions: | ||
usage = 'invoke>' | ||
elif usage is None: | ||
else: | ||
prog='invoke>' | ||
# build full usage string | ||
action_usage = self._format_actions_usage(actions, groups) # NEW | ||
usage = ' '.join([s for s in [prog, action_usage] if s]) | ||
# omit the long line wrapping code |
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 ArgFormatter._format_usage
refactored with the following changes:
- Remove redundant conditional [×2] (
remove-redundant-if
)
This removes the following comments ( why? ):
# if no optionals or positionals are available, usage is just prog
# omit the long line wrapping code
switches[0] = switches[0][: len(switches[0]) - 1] | ||
switches[0] = switches[0][:-1] |
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 Args.parse_cmd
refactored with the following changes:
- Simplify accessing last index of list (
simplify-negative-index
)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.33%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
Branch
main
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
main
branch, then run:Help us improve this pull request!