Fix error detection in nilm-sinefit, and improve test coverage for math.py

This commit is contained in:
Jim Paris 2020-08-06 14:53:42 -04:00
parent 8fd511b5df
commit fe87c3fab4
2 changed files with 22 additions and 4 deletions
nilmtools
tests

View File

@ -3,8 +3,18 @@
# Miscellaenous useful mathematical functions
from nilmdb.utils.printf import *
from numpy import *
from scipy import *
import scipy
def numpy_raise_errors(func):
def wrap(*args, **kwargs):
old = seterr('raise')
try:
return func(*args, **kwargs)
finally:
seterr(**old)
return wrap
@numpy_raise_errors
def sfit4(data, fs):
"""(A, f0, phi, C) = sfit4(data, fs)
@ -25,10 +35,12 @@ def sfit4(data, fs):
(Verified to match sfit4.m)
"""
N = len(data)
if N < 2:
raise ValueError("bad data")
t = linspace(0, (N-1) / float(fs), N)
## Estimate frequency using FFT (step b)
Fc = fft(data)
Fc = scipy.fft.fft(data)
F = abs(Fc)
F[0] = 0 # eliminate DC
@ -76,7 +88,7 @@ def sfit4(data, fs):
phi = arctan2(s[0], s[1]) # eqn B.22 (flipped for sin instead of cos)
C = s[2]
return (A, f0, phi, C)
except Exception as e:
except Exception as e: # pragma: no cover (not sure if we can hit this?)
# something broke down; just return zeros
return (0, 0, 0, 0)

View File

@ -763,7 +763,7 @@ class TestAllCommands(CommandTester):
client.stream_create("/train/matches2", "uint8_1")
self.ok(get_json("tests/data/trainola2.js"))
def test010_pipewatch(self):
def test_10_pipewatch(self):
self.main = nilmtools.pipewatch.main
self.fail(f"")
@ -832,3 +832,9 @@ class TestAllCommands(CommandTester):
self.ok(f"--yes tests/data/cleanup-nodecim.cfg")
self.ok(f"--estimate tests/data/cleanup-nodecim.cfg")
def test_12_misc(self):
# Fill in test cases that were missed by earlier code
with assert_raises(ValueError):
nilmtools.math.sfit4([1], 5)
nilmtools.math.sfit4([1,2], 5)