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 refactored main branch #1

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

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Apr 30, 2021

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:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Comment on lines -22 to +33

## Load the models one by one.
print("Preparing the encoder, the synthesizer and the vocoder...")
encoder.load_model(enc_model_fpath)
synthesizer = Synthesizer(syn_model_fpath)
vocoder.load_model(voc_model_fpath)


texts_array = []
with open('documentation/voice_cloning/sample_text.txt', 'r') as voice_cloning_para:
texts_array = voice_cloning_para.readlines()

Copy link
Author

Choose a reason for hiding this comment

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

Function voice_creation_build refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)

Comment on lines -35 to +49
if isinstance(fpath_or_wav, str) or isinstance(fpath_or_wav, Path):
if isinstance(fpath_or_wav, (str, Path)):
wav, source_sr = librosa.load(str(fpath_or_wav), sr=None)
else:
wav = fpath_or_wav

# Resample the wav if needed
if source_sr is not None and source_sr != sampling_rate:
wav = librosa.resample(wav, source_sr, sampling_rate)

# Apply the preprocessing: normalize volume and shorten long silences
if normalize:
wav = normalize_volume(wav, audio_norm_target_dBFS, increase_only=True)
if webrtcvad and trim_silence:
wav = trim_long_silences(wav)

Copy link
Author

Choose a reason for hiding this comment

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

Function preprocess_wav refactored with the following changes:

Comment on lines -51 to +53

frames = torch.from_numpy(frames_batch).to(_device)
embed = _model.forward(frames).detach().cpu().numpy()
return embed
return _model.forward(frames).detach().cpu().numpy()
Copy link
Author

Choose a reason for hiding this comment

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

Function embed_frames_batch refactored with the following changes:

Comment on lines -17 to +18
self.sample_data = dict()
self.sample_data = {}

Copy link
Author

Choose a reason for hiding this comment

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

Function DatasetLog.__init__ refactored with the following changes:

if not param_name in self.sample_data:
if param_name not in self.sample_data:
Copy link
Author

Choose a reason for hiding this comment

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

Function DatasetLog.add_sample refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

y = g * F.relu(x1) + (1. - g) * x
return y
return g * F.relu(x1) + (1. - g) * x
Copy link
Author

Choose a reason for hiding this comment

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

Function HighwayNetwork.forward refactored with the following changes:

Comment on lines -57 to +56
if speaker_embedding.dim() == 1:
idx = 0
else:
idx = 1

idx = 0 if speaker_embedding.dim() == 1 else 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 Encoder.add_speaker_embedding refactored with the following changes:

Comment on lines -115 to +110
for i in range(num_highways):
for _ in range(num_highways):
Copy link
Author

Choose a reason for hiding this comment

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

Function CBHG.__init__ refactored with the following changes:

Comment on lines -516 to +511
parameters = sum([np.prod(p.size()) for p in parameters]) / 1_000_000
parameters = sum(np.prod(p.size()) for p in parameters) / 1_000_000
Copy link
Author

Choose a reason for hiding this comment

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

Function Tacotron.num_params refactored with the following changes:

Comment on lines -13 to +14
return "\n".join([" ".join(seq[i:i + max_words]) for i in range(0, len(seq), max_words)])
return "\n".join(
" ".join(seq[i:i + max_words]) for i in range(0, len(seq), max_words))
Copy link
Author

Choose a reason for hiding this comment

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

Function split_title_line refactored with the following changes:

Comment on lines -102 to +106
def decode_mu_law(y, mu, from_labels=True) :
def decode_mu_law(y, mu, from_labels=True):
if from_labels:
y = label_2_float(y, math.log2(mu))
mu = mu - 1
x = np.sign(y) / mu * ((1 + mu) ** np.abs(y) - 1)
return x
return np.sign(y) / mu * ((1 + mu) ** np.abs(y) - 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 decode_mu_law refactored with the following changes:

bar = ''
for i in range(size):
bar += '█' if i <= done else '░'
return bar
return ''.join('█' if i <= done else '░' for i in range(size))
Copy link
Author

Choose a reason for hiding this comment

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

Function progbar refactored with the following changes:

Comment on lines -24 to +32
def simple_table(item_tuples) :
def simple_table(item_tuples):

border_pattern = '+---------------------------------------'
whitespace = ' '

headings, cells, = [], []

for item in item_tuples :
for item in item_tuples:

heading, cell = str(item[0]), str(item[1])

pad_head = True if len(heading) < len(cell) else False
pad_head = len(heading) < len(cell)
Copy link
Author

Choose a reason for hiding this comment

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

Function simple_table refactored with the following changes:

Comment on lines -75 to +81
def time_since(started) :
def time_since(started):
elapsed = time.time() - started
m = int(elapsed // 60)
s = int(elapsed % 60)
if m >= 60 :
h = int(m // 60)
m = m % 60
return f'{h}h {m}m {s}s'
else :
if m < 60:
return f'{m}m {s}s'

h = int(m // 60)
m %= 60
return f'{h}h {m}m {s}s'
Copy link
Author

Choose a reason for hiding this comment

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

Function time_since refactored with the following changes:

Comment on lines -59 to +65

if normalize:
mel = mel / hp.mel_max_abs_value
mel = torch.from_numpy(mel[None, ...])
wav = _model.generate(mel, batched, target, overlap, hp.mu_law, progress_callback)
return wav
return _model.generate(
mel, batched, target, overlap, hp.mu_law, progress_callback
)
Copy link
Author

Choose a reason for hiding this comment

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

Function infer_waveform refactored with the following changes:

if torch.cuda.is_available():
mels = mels.cuda()
else:
mels = mels.cpu()
mels = mels.cuda() if torch.cuda.is_available() else mels.cpu()
Copy link
Author

Choose a reason for hiding this comment

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

Function WaveRNN.generate refactored with the following changes:

Comment on lines -282 to +279
if side == 'before' or side == 'both':
if side in ['before', 'both']:
Copy link
Author

Choose a reason for hiding this comment

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

Function WaveRNN.pad_tensor refactored with the following changes:

Comment on lines -432 to +429
parameters = sum([np.prod(p.size()) for p in parameters]) / 1_000_000
parameters = sum(np.prod(p.size()) for p in parameters) / 1_000_000
Copy link
Author

Choose a reason for hiding this comment

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

Function WaveRNN.num_params refactored with the following changes:

Comment on lines -62 to +71
image_list = list()
image_list = []

if not 'user_id' in user_dict:
if 'user_id' not in user_dict:
raise create_error_message('Bad Request')

if user_dict['image_id'] == '':
image_list = user_table.find_one({'_id': ObjectId(user_dict['user_id'])})['images']
image_table.delete(image_list)
return user_table.delete_one({'_id': ObjectId(user_dict['user_id'])})

Copy link
Author

Choose a reason for hiding this comment

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

Function delete refactored with the following changes:

  • Replace list() with [] (list-literal)
  • Simplify logical expression using De Morgan identities (de-morgan)

return True if user_table.find_one({'name': user.get_name()}) else False
return bool(user_table.find_one({'name': user.get_name()}))
Copy link
Author

Choose a reason for hiding this comment

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

Function user_already_exists refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Apr 30, 2021

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.14%.

Quality metrics Before After Change
Complexity 4.84 ⭐ 4.69 ⭐ -0.15 👍
Method Length 64.57 🙂 64.30 🙂 -0.27 👍
Working memory 11.74 😞 11.73 😞 -0.01 👍
Quality 61.72% 🙂 61.86% 🙂 0.14% 👍
Other metrics Before After Change
Lines 2835 2812 -23
Changed files Quality Before Quality After Quality Change
documentation/voice_cloning/voice_creation_script.py 54.40% 🙂 54.52% 🙂 0.12% 👍
src/models/voice_cloning/encoder/audio.py 65.43% 🙂 65.74% 🙂 0.31% 👍
src/models/voice_cloning/encoder/inference.py 65.68% 🙂 65.63% 🙂 -0.05% 👎
src/models/voice_cloning/encoder/preprocess.py 59.31% 🙂 59.33% 🙂 0.02% 👍
src/models/voice_cloning/encoder/visualizations.py 65.30% 🙂 65.61% 🙂 0.31% 👍
src/models/voice_cloning/encoder/data_objects/speaker.py 86.46% ⭐ 86.61% ⭐ 0.15% 👍
src/models/voice_cloning/encoder/data_objects/speaker_verification_dataset.py 87.98% ⭐ 88.13% ⭐ 0.15% 👍
src/models/voice_cloning/synthesizer/audio.py 77.98% ⭐ 78.54% ⭐ 0.56% 👍
src/models/voice_cloning/synthesizer/inference.py 60.65% 🙂 60.70% 🙂 0.05% 👍
src/models/voice_cloning/synthesizer/preprocess.py 36.38% 😞 36.43% 😞 0.05% 👍
src/models/voice_cloning/synthesizer/synthesizer_dataset.py 67.60% 🙂 67.53% 🙂 -0.07% 👎
src/models/voice_cloning/synthesizer/models/tacotron.py 63.56% 🙂 63.65% 🙂 0.09% 👍
src/models/voice_cloning/synthesizer/utils/plot.py 55.51% 🙂 55.51% 🙂 0.00%
src/models/voice_cloning/vocoder/audio.py 89.49% ⭐ 89.85% ⭐ 0.36% 👍
src/models/voice_cloning/vocoder/display.py 68.52% 🙂 68.91% 🙂 0.39% 👍
src/models/voice_cloning/vocoder/inference.py 66.20% 🙂 66.03% 🙂 -0.17% 👎
src/models/voice_cloning/vocoder/models/deepmind_version.py 46.49% 😞 46.49% 😞 0.00%
src/models/voice_cloning/vocoder/models/fatchord_version.py 52.84% 🙂 53.35% 🙂 0.51% 👍
src/services/user_image_repository.py 79.05% ⭐ 79.22% ⭐ 0.17% 👍

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

File Function Complexity Length Working Memory Quality Recommendation
src/models/voice_cloning/vocoder/models/fatchord_version.py WaveRNN.generate 21 😞 564 ⛔ 25 ⛔ 16.91% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
src/models/voice_cloning/synthesizer/preprocess.py preprocess_speaker 31 😞 253 ⛔ 19 ⛔ 19.16% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
src/models/voice_cloning/synthesizer/preprocess.py split_on_silences 20 😞 430 ⛔ 19 ⛔ 21.20% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
src/models/voice_cloning/vocoder/models/deepmind_version.py WaveRNN.generate 1 ⭐ 441 ⛔ 32 ⛔ 33.86% 😞 Try splitting into smaller methods. Extract out complex expressions
src/models/voice_cloning/encoder/preprocess.py _preprocess_speaker_dirs 20 😞 219 ⛔ 10 😞 39.17% 😞 Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

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!

Let us know what you think of it by mentioning @sourcery-ai in a comment.

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.

None yet

0 participants