Skip to content

Commit

Permalink
reorganising stats and preprocessing nodes. About to modify nodes so
Browse files Browse the repository at this point in the history
they chain one to another rather than using pipeline.
  • Loading branch information
JackKelly committed Jul 3, 2014
1 parent 3407be6 commit bb69147
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 244 deletions.
6 changes: 3 additions & 3 deletions nilmtk/elecmeter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import print_function, division
from .pipeline import Pipeline, ClipNode, EnergyNode, LocateGoodSectionsNode
from .pipeline import Pipeline, Clip, TotalEnergy, GoodSections
from .hashable import Hashable
from .appliance import Appliance
from .datastore import Key
Expand Down Expand Up @@ -229,7 +229,7 @@ def total_energy(self, **load_kwargs):
-------
nilmtk.pipeline.EnergyResults object
"""
nodes = [ClipNode(), EnergyNode()]
nodes = [Clip(), TotalEnergy()]
results = self._run_pipeline(nodes, **load_kwargs)
return results['energy']

Expand All @@ -243,7 +243,7 @@ def good_sections(self):
-------
sections: list of nilmtk.TimeFrame objects
"""
nodes = [LocateGoodSectionsNode()]
nodes = [GoodSections()]
results = self._run_pipeline(nodes)
return results['good_sections']

Expand Down
File renamed without changes.
6 changes: 0 additions & 6 deletions nilmtk/pipeline/__init__.py

This file was deleted.

124 changes: 0 additions & 124 deletions nilmtk/pipeline/pipeline.py

This file was deleted.

35 changes: 0 additions & 35 deletions nilmtk/pipeline/tests/test_pipeline.py

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from warnings import warn
from nilmtk.utils import index_of_column_name

class ClipNode(Node):
class Clip(Node):

# Not very well specified. Really want to specify that
# we need 'lower_limit' and 'upper_limit' to be specified in
Expand Down
Empty file.
File renamed without changes.
Empty file added nilmtk/stats/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import print_function, division
from node import Node, UnsatisfiedRequirementsError
from node import Node
import numpy as np
from numpy import diff, concatenate
from nilmtk import TimeFrame
from nilmtk.utils import timedelta64_to_secs
from locategoodsectionsresults import LocateGoodSectionsResults
from goodsectionsresults import GoodSectionsResults


class LocateGoodSectionsNode(Node):
class GoodSections(Node):
"""Locate sections of data where the sample period is <= max_sample_period.
Attributes
Expand Down Expand Up @@ -105,54 +105,9 @@ def process(self, df, metadata):
self.previous_chunk_ended_with_open_ended_good_section = (
ends_with_open_ended_good_section)

good_section_results = LocateGoodSectionsResults(max_sample_period)
good_section_results = GoodSectionsResults(max_sample_period)
good_section_results.append(df.timeframe, {'sections': [sections]})
results = getattr(df, 'results', {})
results[self.name] = good_section_results
df.results = results
return df


# reframe_index is perhaps not needed any more. Might be
# safe to remove it. Leaving it here for now as it might
# come in handy, and is well tested at the moment.
def reframe_index(index, window_start=None, window_end=None):
"""
Parameters
----------
index : pd.DatetimeIndex
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 timestamp will be
inserted at `window_start` or `window_end` as necessary. If this window
is shorter than the duration of `data` then `data` will be cropped.
Returns
-------
index : pd.DatetimeIndex
"""

tz = index.tzinfo
reset_tz = False

# Handle window...
if window_start is not None:
if window_start >= index[0]:
index = index[index >= window_start]
else:
index = index.insert(0, window_start)
reset_tz = True

if window_end is not None:
if window_end <= index[-1]:
index = index[index <= window_end]
else:
index = index.insert(len(index), window_end)
reset_tz = True

if reset_tz:
# index.insert breaks timezone.
index = index.tz_localize('UTC').tz_convert(tz)

return index
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pandas as pd
from datetime import timedelta

class LocateGoodSectionsResults(Results):
class GoodSectionsResults(Results):
"""
Attributes
----------
Expand All @@ -15,7 +15,7 @@ class LocateGoodSectionsResults(Results):

def __init__(self, max_sample_period):
self.max_sample_period_td = timedelta(seconds=max_sample_period)
super(LocateGoodSectionsResults, self).__init__()
super(GoodSectionsResults, self).__init__()

def append(self, timeframe, new_results):
"""Append a single result.
Expand All @@ -25,7 +25,7 @@ def append(self, timeframe, new_results):
timeframe : nilmtk.TimeFrame
new_results : {'sections': list of TimeFrame objects}
"""
super(LocateGoodSectionsResults, self).append(timeframe, new_results)
super(GoodSectionsResults, self).append(timeframe, new_results)

@property
def last_results(self):
Expand Down Expand Up @@ -64,4 +64,4 @@ def combined(self):

def unify(self, other):
# TODO!
super(LocateGoodSectionsResults, self).unify(other)
super(GoodSectionsResults, self).unify(other)
Empty file added nilmtk/stats/tests/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/python
from __future__ import print_function, division
import unittest
from nilmtk.pipeline import Pipeline, EnergyNode, LocateGoodSectionsNode
from nilmtk.pipeline import Pipeline, TotalEnergy, GoodSections
from nilmtk.pipeline.locategoodsectionsnode import reframe_index
from nilmtk.pipeline.locategoodsectionsresults import LocateGoodSectionsResults
from nilmtk.pipeline.locategoodsectionsresults import GoodSectionsResults
from nilmtk.pipeline.energynode import _energy_for_power_series
from nilmtk import TimeFrame, ElecMeter, HDFDataStore
from nilmtk.elecmeter import ElecMeterID
Expand All @@ -28,7 +28,7 @@ def setUpClass(cls):
def test_pipeline(self):
meter = ElecMeter(store=self.datastore, metadata=self.meter_meta,
meter_id=METER_ID)
nodes = [LocateGoodSectionsNode()]
nodes = [GoodSections()]
pipeline = Pipeline(nodes)
pipeline.run(meter)

Expand Down Expand Up @@ -97,7 +97,7 @@ def test_process(self):
df.timeframe = TimeFrame(index[0], index[-1])
df.look_ahead = pd.DataFrame()

locate = LocateGoodSectionsNode()
locate = GoodSections()
locate.process(df, metadata)
results = df.results['good_sections'].combined
self.assertEqual(len(results), 4)
Expand All @@ -114,10 +114,10 @@ def test_process(self):
pd.Timestamp("2011-01-01 00:06:20")
]
for split_point in [[4,6,9,17], [4,10,12,17]]:
locate = LocateGoodSectionsNode()
locate = GoodSections()
df.results = {}
prev_i = 0
aggregate_results = LocateGoodSectionsResults(MAX_SAMPLE_PERIOD)
aggregate_results = GoodSectionsResults(MAX_SAMPLE_PERIOD)
for j,i in enumerate(split_point):
cropped_df = df.iloc[prev_i:i]
cropped_df.timeframe = TimeFrame(timestamps[j], timestamps[j+1])
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, ClipNode
from nilmtk.pipeline import Pipeline, TotalEnergy, Clip
from nilmtk.pipeline.energynode import _energy_for_power_series
from nilmtk import TimeFrame, ElecMeter, HDFDataStore
from nilmtk.elecmeter import ElecMeterID
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_pipeline(self):
meter = ElecMeter(store=self.datastore,
metadata=self.meter_meta,
meter_id=METER_ID)
nodes = [ClipNode(), EnergyNode()]
nodes = [Clip(), TotalEnergy()]
pipeline = Pipeline(nodes)
pipeline.run(meter)
energy = deepcopy(pipeline.results['energy'])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/usr/bin/python
from __future__ import print_function, division
import unittest
from nilmtk.pipeline.energyresults import EnergyResults
from nilmtk.pipeline.energyresults import TotalEnergyResults
from nilmtk import TimeFrame

class TestEnergyResults(unittest.TestCase):

def test_append(self):
er = EnergyResults()
er = TotalEnergyResults()
tf = TimeFrame('2012-01-01','2012-01-02')
er.append(tf, {'apparent':40, 'reactive':30, 'active':20})
self.assertEqual(er._data.index.size, 1)
self.assertEqual(er._data.index[0], tf.start)
self.assertEqual(er._data['end'][tf.start], tf.end)

def test_combined(self):
er = EnergyResults()
er = TotalEnergyResults()
tf = TimeFrame('2012-01-01','2012-01-02')
er.append(tf, {'apparent':40})
tf2 = TimeFrame('2012-01-02','2012-01-03')
Expand Down

0 comments on commit bb69147

Please sign in to comment.