Skip to content

Commit

Permalink
LocateGoodSectionsNode actually appears to work... needs more testing...
Browse files Browse the repository at this point in the history
  • Loading branch information
JackKelly committed May 1, 2014
1 parent 1250f47 commit 1bdd079
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
Binary file added data/energy_complex.h5
Binary file not shown.
2 changes: 1 addition & 1 deletion nilmtk/pipeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from nilmtk.pipeline.pipeline import Pipeline
from nilmtk.pipeline.energyresults import EnergyResults
from nilmtk.pipeline.energynode import EnergyNode
from nilmtk.pipeline.locategapsnode import LocateGapsNode
from nilmtk.pipeline.locategoodsectionsnode import LocateGoodSectionsNode
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def reframe_index(index, window_start=None, window_end=None):
window_start, window_end : pd.Timestamp
The start and end of the window of interest. If this window
is larger than the duration of `data` then a single zero will be
is larger than the duration of `data` then a single timestamp will be
inserted at `window_start` or `window_end` as necessary. If this window
is shorter than the duration of `data` data will be cropped.
is shorter than the duration of `data` then `data` will be cropped.
Returns
-------
Expand Down Expand Up @@ -48,14 +48,13 @@ def reframe_index(index, window_start=None, window_end=None):

return index

class LocateGapsNode(Node):
class LocateGoodSectionsNode(Node):

requirements = {'device': {
'max_sample_period': 'ANY VALUE'}}
postconditions = {'preprocessing': {'gaps_located':True}}
requirements = {'device': {'max_sample_period': 'ANY VALUE'}}
postconditions = {'preprocessing': {'good_sections_located':True}}

def __init__(self, name='locate_gaps'):
super(LocateGapsNode, self).__init__(name)
def __init__(self, name='locate_good_sections'):
super(LocateGoodSectionsNode, self).__init__(name)

def process(self, df, metadata=None):
assert metadata is not None
Expand All @@ -66,8 +65,19 @@ def process(self, df, metadata=None):
index = reframe_index(index, df.timeframe.start, df.timeframe.end)
timedeltas_sec = timedelta64_to_secs(np.diff(index.values))
overlong_timedeltas = timedeltas_sec > max_sample_period
gap_starts = index[:-1][overlong_timedeltas]
gap_ends = index[1:][overlong_timedeltas]
good_sect_starts = index[1:][overlong_timedeltas]
good_sect_starts = good_sect_starts.insert(0, index[0])
good_sect_ends = index[:-1][overlong_timedeltas]
good_sect_ends = good_sect_ends.insert(len(good_sect_ends), index[-1])

assert len(good_sect_starts) == len(good_sect_ends)

mask = []
for start, end in zip(good_sect_starts, good_sect_ends):
try:
mask.append(TimeFrame(start, end))
except ValueError:
pass # silently ignore good sections of zero length

results = getattr(df, 'results', {})
# locate_gaps_results = LocateGapsResults()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python
from __future__ import print_function, division
import unittest
from nilmtk.pipeline import Pipeline, EnergyNode, LocateGapsNode
from nilmtk.pipeline import Pipeline, EnergyNode, LocateGoodSectionsNode
from nilmtk.pipeline.energynode import _energy_per_power_series
from nilmtk import TimeFrame, EMeter, HDFDataStore, Loader
from nilmtk.consts import JOULES_PER_KWH
Expand All @@ -15,14 +15,14 @@ class TestLocateGaps(unittest.TestCase):

@classmethod
def setUpClass(cls):
filename = join(data_dir(), 'energy.h5')
filename = join(data_dir(), 'energy_complex.h5')
cls.datastore = HDFDataStore(filename)
cls.loader = Loader(store=cls.datastore, key='/building1/electric/meter1')

def test_pipeline(self):
meter = EMeter()
meter.load(self.loader)
nodes = [LocateGapsNode()]
nodes = [LocateGoodSectionsNode()]
pipeline = Pipeline(nodes)
pipeline.run(meter)

Expand Down
3 changes: 2 additions & 1 deletion nilmtk/tests/generate_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def create_random_hdf5():


def create_energy_hdf5(simple=True):
FILENAME = join(data_dir(), 'energy.h5')
fname = 'energy.h5' if simple else 'energy_complex.h5'
FILENAME = join(data_dir(), fname)

df = power_data(simple=simple)

Expand Down

0 comments on commit 1bdd079

Please sign in to comment.