From 6049041c63f363de0228164204a33a7142127958 Mon Sep 17 00:00:00 2001 From: DerMoehre Date: Mon, 17 Oct 2022 07:08:24 +0200 Subject: [PATCH] changed mean_stdv() to numpy --- fave/extractFormants.py | 24 +++++----------------- tests/fave/extract/test_extractFormants.py | 1 + 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/fave/extractFormants.py b/fave/extractFormants.py index 48a4119..8ea6496 100755 --- a/fave/extractFormants.py +++ b/fave/extractFormants.py @@ -1068,28 +1068,14 @@ def maximumIntensity(intensities, times): def mean_stdv(valuelist): """returns the arithmetic mean and sample standard deviation (N-1 in the denominator) of a list of values""" - n = len(valuelist) - empty = 0 - if n > 0: - if n == 1: + np_valuelist = np.array(valuelist,dtype=np.float64) + if len(np_valuelist) > 0: + if len(np_valuelist) == 1: mean = valuelist[0] stdv = 0 else: - sum_i = 0 - for i in range(n): - if valuelist[i] == None: - empty += 1 - continue - else: - sum_i += valuelist[i] - mean = sum_i / (n - empty) - diffsum_i = 0 - for i in range(n): - if valuelist[i] == None: - continue - else: - diffsum_i += (valuelist[i] - mean) ** 2 - stdv = math.sqrt(diffsum_i / ((n - empty) - 1)) + mean = np.nanmean(np_valuelist) + stdv = np.nanstd(np_valuelist, ddof=1) else: # empty list mean = None diff --git a/tests/fave/extract/test_extractFormants.py b/tests/fave/extract/test_extractFormants.py index 9f8838a..362dbd1 100644 --- a/tests/fave/extract/test_extractFormants.py +++ b/tests/fave/extract/test_extractFormants.py @@ -1,4 +1,5 @@ +from cmath import nan import logging import pytest import numpy as np