Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/tensorflow_tts/utils/outliers.py
1558 views
1
# -*- coding: utf-8 -*-
2
# Copyright 2020 Minh Nguyen (@dathudeptrai)
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
"""Outliers detection and remove."""
16
import numpy as np
17
18
19
def is_outlier(x, p25, p75):
20
"""Check if value is an outlier."""
21
lower = p25 - 1.5 * (p75 - p25)
22
upper = p75 + 1.5 * (p75 - p25)
23
return x <= lower or x >= upper
24
25
26
def remove_outlier(x, p_bottom: int = 25, p_top: int = 75):
27
"""Remove outlier from x."""
28
p_bottom = np.percentile(x, p_bottom)
29
p_top = np.percentile(x, p_top)
30
31
indices_of_outliers = []
32
for ind, value in enumerate(x):
33
if is_outlier(value, p_bottom, p_top):
34
indices_of_outliers.append(ind)
35
36
x[indices_of_outliers] = 0.0
37
38
# replace by mean f0.
39
x[indices_of_outliers] = np.max(x)
40
return x
41
42