Skip to content
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 knasti/PopNet #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SourceryAI
Copy link

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:

git fetch https://github.com/sourcery-ai-bot/PopNet master
git merge --ff-only FETCH_HEAD
git reset HEAD^

for cur_epoch in range(self.model.cur_epoch_tensor.eval(self.sess), 1, 1):
for _ in range(self.model.cur_epoch_tensor.eval(self.sess), 1, 1):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseTrain.test refactored with the following changes:

  • Replace unused for index with underscore (for-index-underscore)

if self.i_test == self.list_num_test_batches[self.pair_no]:
if self.i_test == self.list_num_test_batches[pair_no]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DataGenerator.next_test_batch refactored with the following changes:

  • Use previously assigned local variable (use-assigned-variable)

Comment on lines -250 to +265
for i in range(len(self.x_data)):
for x_datum in self.x_data:
cur_row = 0
cur_col = 0
x_datum
for j in range(self.no_chunks):
if self.chunk_cols == cur_col: # Change to new row and reset column if it reaches the end
cur_row += 1
cur_col = 0
self.x_data[i]
raster[cur_row * self.chunk_height: (cur_row + 1) * self.chunk_height, cur_col * self.chunk_width: (cur_col + 1) * self.chunk_width, :] = self.x_data[i][j]
raster[
cur_row
* self.chunk_height : (cur_row + 1)
* self.chunk_height,
cur_col * self.chunk_width : (cur_col + 1) * self.chunk_width,
:,
] = x_datum[j]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PrepData.input_to_tif refactored with the following changes:

  • Hoist statements out of for/while loops (hoist-statement-from-loop)
  • Replace index in for loop with direct reference (for-index-replacement)

Comment on lines -21 to +25
output_nr = 2020
for file in os.listdir(self.config.output_pred_dir):
if file.endswith(".tif"):
output_nr += 10
output_nr = 2020 + sum(
10
for file in os.listdir(self.config.output_pred_dir)
if file.endswith(".tif")
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DataWriter.write_outputs refactored with the following changes:

  • Convert for loop into call to sum() (sum-comprehension)

Comment on lines -43 to +45
for i in range(len(data_loader.data_label_pairs)):
x_data = data_loader.data_label_pairs[i][0][:, :, :]
y_true = data_loader.data_label_pairs[i][1][:, :, 0]
for data_label_pair in data_loader.data_label_pairs:
x_data = data_label_pair[0][data_loader]
y_true = data_label_pair[1][data_loader.data_label_pairs[i][1][:, :, 0]]
Copy link
Author

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:

  • Replace index in for loop with direct reference (for-index-replacement)

summaries_dict = {}
summaries_dict['loss'] = loss
summaries_dict['acc'] = acc
summaries_dict = {'loss': loss, 'acc': acc}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ExampleTrainer.train_epoch refactored with the following changes:

  • Merge dictionary assignment with declaration (merge-dict-assign)

summaries_dict = {}
summaries_dict['pop_loss'] = pop_loss
summaries_dict['abs_loss'] = abs_loss
summaries_dict['loss'] = loss

summaries_dict = {'pop_loss': pop_loss, 'abs_loss': abs_loss, 'loss': loss}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PopTrainer.train_epoch refactored with the following changes:

  • Merge dictionary assignment with declaration (merge-dict-assign)

Comment on lines -68 to +64
summaries_dict = {}
summaries_dict['pop_loss'] = pop_loss
summaries_dict['abs_loss'] = abs_loss
summaries_dict['loss'] = loss

summaries_dict = {'pop_loss': pop_loss, 'abs_loss': abs_loss, 'loss': loss}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PopTrainer.test_epoch refactored with the following changes:

  • Merge dictionary assignment with declaration (merge-dict-assign)

utils/utils.py Outdated
Comment on lines 10 to 11
args = argparser.parse_args()
return args No newline at end of file
return argparser.parse_args()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_args refactored with the following changes:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)

Comment on lines -58 to +110
pred_2020 = pred_2020 + pop_array
pred_2020 += pop_array
else:
pred_2020 = pop_array
print(np.max(pop_array))

elif file == 'pred_2030.tif':
if type(pred_2030) is np.ndarray:
pred_2030 = pred_2030 + pop_array
pred_2030 += pop_array
else:
pred_2030 = pop_array

elif file == 'pred_2040.tif':
if type(pred_2040) is np.ndarray:
pred_2040 = pred_2040 + pop_array
pred_2040 += pop_array
else:
pred_2040 = pop_array

elif file == 'pred_2050.tif':
if type(pred_2050) is np.ndarray:
pred_2050 = pred_2050 + pop_array
pred_2050 += pop_array
else:
pred_2050 = pop_array

elif file == 'pred_2060.tif':
if type(pred_2060) is np.ndarray:
pred_2060 = pred_2060 + pop_array
pred_2060 += pop_array
else:
pred_2060 = pop_array

elif file == 'pred_2070.tif':
if type(pred_2070) is np.ndarray:
pred_2070 = pred_2070 + pop_array
pred_2070 += pop_array
else:
pred_2070 = pop_array

elif file == 'pred_2080.tif':
if type(pred_2080) is np.ndarray:
pred_2080 = pred_2080 + pop_array
pred_2080 += pop_array
else:
pred_2080 = pop_array

elif file == 'pred_2090.tif':
if type(pred_2090) is np.ndarray:
pred_2090 = pred_2090 + pop_array
pred_2090 += pop_array
else:
pred_2090 = pop_array

else:
if type(pred_2100) is np.ndarray:
pred_2100 = pred_2100 + pop_array
pred_2100 += pop_array
else:
pred_2100 = pop_array

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 58-110 refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)

@sourcery-ai
Copy link

sourcery-ai bot commented Aug 3, 2020

Sourcery Code Quality Report (beta)

✅  Merging this PR will increase code quality in the affected files by 0.03 out of 10.

Quality metrics Before After Change
Complexity 3.37 3.32 -0.05 🔵
Method Length 96.93 95.76 -1.17 🔵
Quality 7.96 7.99 0.03 🔵
Other metrics Before After Change
Lines 1058 1054 -4
Changed files Quality Before Quality After Quality Change
base/base_train.py 9.43 9.43 0.00
data_loader/data_generator.py 7.20 7.21 0.01 🔵
data_loader/data_writer.py 8.25 8.29 0.04 🔵
mains/main.py 8.11 8.14 0.03 🔵
mains/use_model.py 6.95 6.95 0.00
trainers/example_trainer.py 8.93 8.97 0.04 🔵
trainers/pop_trainer.py 8.39 8.47 0.08 🔵
utils/utils.py 9.61 9.64 0.03 🔵
visualizations/monte_carlo.py 6.99 7.00 0.01 🔵

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Overall Recommendation
data_loader/data_generator.py PrepData.create_chunks 34 544.61 2.09 Split out functionality
data_loader/data_generator.py PrepTrainTest.create_chunks 28 542.66 2.50 Split out functionality
mains/use_model.py main 16 447.74 3.56 Split out functionality
data_loader/data_generator.py PrepTrainTest.train_batches 6 235.94 5.27 Split out functionality
data_loader/data_generator.py PrepTrainTest.test_batches 6 235.94 5.27 Split out functionality

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!

Let us know what you think of it via email or our Gitter!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant