-
Notifications
You must be signed in to change notification settings - Fork 0
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 refactored xin branch #1
base: xin
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.
Sourcery timed out performing refactorings.
Due to GitHub API limits, only the first 60 comments can be shown.
args = super().parse_args() | ||
return args | ||
return super().parse_args() |
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 ArgParser.parse_args
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
raise Exception('No existing model_path: ' + args.model_path) | ||
raise Exception(f'No existing model_path: {args.model_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 get_logger
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
)
metrics = {} | ||
logs = [] | ||
for i in range(args.num_proc): | ||
for _ in range(args.num_proc): | ||
log = queue.get() | ||
logs = logs + log | ||
|
||
for metric in logs[0].keys(): | ||
metrics[metric] = sum([log[metric] for log in logs]) / len(logs) | ||
metrics = { | ||
metric: sum(log[metric] for log in logs) / len(logs) | ||
for metric in logs[0].keys() | ||
} | ||
|
||
for k, v in metrics.items(): | ||
print('Test average {} at [{}/{}]: {}'.format(k, args.step, args.max_step, v)) | ||
print(f'Test average {k} at [{args.step}/{args.max_step}]: {v}') |
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 main
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Replace unused for index with underscore (
for-index-underscore
) - Convert for loop into dictionary comprehension (
dict-comprehension
) - Replace unneeded comprehension with generator (
comprehension-to-generator
) - Replace call to format with f-string (
use-fstring-for-formatting
)
original_name = name[0:-6] | ||
state_sum = target[original_name+'_state-data-'] | ||
original_name = name[:-6] | ||
state_sum = target[f'{original_name}_state-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 KGEServer._push_handler
refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
"""Get data from data_path/dataset/part_machine_id | ||
"""Get data from data_path/dataset/part_machine_id |
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_server_data
refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
print('batch size ({}) is incompatible to the negative sample size ({}). Change the batch size to {}'.format( | ||
old_batch_size, neg_sample_size, batch_size)) | ||
print( | ||
f'batch size ({old_batch_size}) is incompatible to the negative sample size ({neg_sample_size}). Change the batch size to {batch_size}' | ||
) | ||
|
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_compatible_batch_size
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
assert f_remote.status_code == 200, 'fail to open {}'.format(url) | ||
assert f_remote.status_code == 200, f'fail to open {url}' |
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 _download_and_extract
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
for i, l in enumerate(f): | ||
pass | ||
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 _file_line
refactored with the following changes:
- Remove nested block which has no effect (
remove-empty-nested-block
)
print('Reading {} triples....'.format(mode)) | ||
print(f'Reading {mode} triples....') |
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 KGDataset.read_triple
refactored with the following changes:
- Replace call to format with f-string [×2] (
use-fstring-for-formatting
)
print('Reading {} triples....'.format(mode)) | ||
print(f'Reading {mode} triples....') |
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 PartitionKGDataset.read_triple
refactored with the following changes:
- Replace call to format with f-string [×2] (
use-fstring-for-formatting
)
url = 'https://data.dgl.ai/dataset/{}.zip'.format(name) | ||
url = f'https://data.dgl.ai/dataset/{name}.zip' | ||
|
||
if not os.path.exists(os.path.join(path, name)): | ||
print('File not found. Downloading from', url) | ||
_download_and_extract(url, path, name + '.zip') | ||
_download_and_extract(url, path, f'{name}.zip') |
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 KGDatasetFB15k.__init__
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
url = 'https://data.dgl.ai/dataset/{}.zip'.format(name) | ||
url = f'https://data.dgl.ai/dataset/{name}.zip' | ||
|
||
if not os.path.exists(os.path.join(path, name)): | ||
print('File not found. Downloading from', url) | ||
_download_and_extract(url, path, name + '.zip') | ||
_download_and_extract(url, path, f'{name}.zip') |
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 KGDatasetFB15k237.__init__
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
url = 'https://data.dgl.ai/dataset/{}.zip'.format(name) | ||
url = f'https://data.dgl.ai/dataset/{name}.zip' | ||
|
||
if not os.path.exists(os.path.join(path, name)): | ||
print('File not found. Downloading from', url) | ||
_download_and_extract(url, path, name + '.zip') | ||
_download_and_extract(url, path, f'{name}.zip') |
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 KGDatasetWN18.__init__
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
url = 'https://data.dgl.ai/dataset/{}.zip'.format(name) | ||
url = f'https://data.dgl.ai/dataset/{name}.zip' | ||
|
||
if not os.path.exists(os.path.join(path, name)): | ||
print('File not found. Downloading from', url) | ||
_download_and_extract(url, path, name + '.zip') | ||
_download_and_extract(url, path, f'{name}.zip') |
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 KGDatasetWN18rr.__init__
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
url = 'https://data.dgl.ai/dataset/{}.zip'.format(name) | ||
url = f'https://data.dgl.ai/dataset/{name}.zip' | ||
|
||
if not os.path.exists(os.path.join(path, name)): | ||
print('File not found. Downloading from', url) | ||
_download_and_extract(url, path, '{}.zip'.format(name)) | ||
_download_and_extract(url, path, f'{name}.zip') |
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 KGDatasetFreebase.__init__
refactored with the following changes:
- Replace call to format with f-string [×2] (
use-fstring-for-formatting
)
print('relation partition {} edges into {} parts'.format(len(heads), n)) | ||
print(f'relation partition {len(heads)} edges into {n} parts') |
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 BalancedRelationPartition
refactored with the following changes:
- Replace call to format with f-string [×3] (
use-fstring-for-formatting
) - Convert for loop into list comprehension (
list-comprehension
)
print('random partition {} edges into {} parts'.format(len(heads), n)) | ||
print(f'random partition {len(heads)} edges into {n} parts') |
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 RandomPartition
refactored with the following changes:
- Replace call to format with f-string [×2] (
use-fstring-for-formatting
)
raise Exception('get invalid type: ' + eval_type) | ||
raise Exception(f'get invalid type: {eval_type}') |
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 EvalDataset.get_edges
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
if model_name == 'RESCAL': | ||
rel_dim = relation_dim * entity_dim | ||
else: | ||
rel_dim = relation_dim | ||
|
||
rel_dim = relation_dim * entity_dim if model_name == 'RESCAL' else relation_dim |
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 KEModel.__init__
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
self.entity_emb.save(path, dataset+'_'+self.model_name+'_entity') | ||
self.entity_emb.save(path, f'{dataset}_{self.model_name}_entity') | ||
if self.strict_rel_part or self.soft_rel_part: | ||
self.global_relation_emb.save(path, dataset+'_'+self.model_name+'_relation') | ||
self.global_relation_emb.save(path, f'{dataset}_{self.model_name}_relation') | ||
else: | ||
self.relation_emb.save(path, dataset+'_'+self.model_name+'_relation') | ||
self.relation_emb.save(path, f'{dataset}_{self.model_name}_relation') | ||
|
||
self.score_func.save(path, dataset+'_'+self.model_name) | ||
self.score_func.save(path, f'{dataset}_{self.model_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 KEModel.save_emb
refactored with the following changes:
- Use f-string instead of string concatenation [×11] (
use-fstring-for-concatenation
)
self.entity_emb.load(path, dataset+'_'+self.model_name+'_entity') | ||
self.relation_emb.load(path, dataset+'_'+self.model_name+'_relation') | ||
self.score_func.load(path, dataset+'_'+self.model_name) | ||
self.entity_emb.load(path, f'{dataset}_{self.model_name}_entity') | ||
self.relation_emb.load(path, f'{dataset}_{self.model_name}_relation') | ||
self.score_func.load(path, f'{dataset}_{self.model_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 KEModel.load_emb
refactored with the following changes:
- Use f-string instead of string concatenation [×8] (
use-fstring-for-concatenation
)
if neg_deg_sample: | ||
neg_g.neg_sample_size = neg_sample_size | ||
mask = mask.reshape(num_chunks, chunk_size, neg_sample_size) | ||
return neg_score * mask | ||
else: | ||
if not neg_deg_sample: | ||
return neg_score | ||
neg_g.neg_sample_size = neg_sample_size | ||
mask = mask.reshape(num_chunks, chunk_size, neg_sample_size) | ||
return neg_score * mask |
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 KEModel.predict_neg_score
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
res = nd.sqrt(nd.clip(squared_res, 1e-30, np.finfo(np.float32).max)) | ||
return res | ||
return nd.sqrt(nd.clip(squared_res, 1e-30, np.finfo(np.float32).max)) |
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 batched_l2_dist
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
res = nd.norm(a - b, ord=1, axis=-1) | ||
return res | ||
return nd.norm(a - b, ord=1, axis=-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 batched_l1_dist
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
self.projection_emb.save(path, name+'projection') | ||
self.projection_emb.save(path, f'{name}projection') |
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 TransRScore.save
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
file_name = os.path.join(path, name+'.npy') | ||
file_name = os.path.join(path, f'{name}.npy') |
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 ExternalEmbedding.load
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
np.random.seed(42) | ||
|
||
from models.mxnet.score_fun import * | ||
from models.mxnet.tensor_models import ExternalEmbedding | ||
else: | ||
import torch as th | ||
th.manual_seed(42) | ||
np.random.seed(42) | ||
|
||
from models.pytorch.score_fun import * | ||
from models.pytorch.tensor_models import ExternalEmbedding | ||
np.random.seed(42) | ||
|
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 31-39
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
)
neg_score = self.head_neg_score(neg_head, rel, tail, | ||
num_chunks, chunk_size, neg_sample_size) | ||
return self.head_neg_score( | ||
neg_head, rel, tail, num_chunks, chunk_size, neg_sample_size | ||
) | ||
|
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 BaseKEModel.predict_neg_score
refactored with the following changes:
- Lift return into if (
lift-return-into-if
)
assert f_remote.status_code == 200, 'fail to open {}'.format(url) | ||
assert f_remote.status_code == 200, f'fail to open {url}' |
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 _download
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
bin_path = "/tmp/dataset/livejournal/livejournal_{}.bin".format(format) | ||
bin_path = f"/tmp/dataset/livejournal/livejournal_{format}.bin" |
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_graph
refactored with the following changes:
- Replace call to format with f-string [×3] (
use-fstring-for-formatting
)
Sourcery Code Quality Report❌ Merging this PR will decrease code quality in the affected files by 0.11%.
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
xin
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
xin
branch, then run:Help us improve this pull request!