Fix error detection in nilm-sinefit, and improve test coverage for math.py
This commit is contained in:
parent
8fd511b5df
commit
fe87c3fab4
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user