Browse Source

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

tags/nilmtools-2.0.0
Jim Paris 3 years ago
parent
commit
fe87c3fab4
2 changed files with 22 additions and 4 deletions
  1. +15
    -3
      nilmtools/math.py
  2. +7
    -1
      tests/test.py

+ 15
- 3
nilmtools/math.py 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)



+ 7
- 1
tests/test.py 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)

Loading…
Cancel
Save