In [207]:
import sys
sys.path.append('../../')
import disaggregator
import disaggregator.OakParkDatasetAdapter as opdsa
import disaggregator.appliance as app
import disaggregator.utils as utils
from datetime import datetime
import calendar

In [232]:
reload(disaggregator.appliance)
reload(disaggregator.utils)
reload(disaggregator.OakParkDatasetAdapter)
reload(app)


Out[232]:
<module 'disaggregator.appliance' from '../../disaggregator/appliance.pyc'>

In [233]:
reload(opdsa)


Out[233]:
<module 'disaggregator.OakParkDatasetAdapter' from '../../disaggregator/OakParkDatasetAdapter.pyc'>

In [106]:
app_set = opdsa.generate_set_by_year_month(2013,4)

In [109]:
app_set.metadata


Out[109]:
{'dataids': [], 'source': 'OakPark'}

In [121]:
db = opdsa.get_db_connection()

homes = opdsa.get_homes(db)

dataids = homes.keys()

In [223]:
homes_traces = opdsa.homes_to_traces(homes)

In [226]:
trace_list = homes_traces.values()

In [231]:
(trace_list[0].series.index.summary())==(trace_list[1].series.index.summary())


Out[231]:
False

In [234]:
utils.traces_aligned(trace_list)


<class 'pandas.tseries.index.DatetimeIndex'>
[2014-06-16 00:00:00, ..., 2014-07-15 23:30:00]
Length: 1440, Freq: None, Timezone: None
Out[234]:
False

In [128]:
complete_homes = opdsa.get_list_of_homes_with_certain_year_month(homes_traces,2013,4)

In [158]:
homes_traces.keys['']


Out[158]:
['5065',
 '9455',
 '3494',
 '9308',
 '3534',
 '8343',
 '7833',
 '7879',
 '8132',
 '3698',
 '1472',
 '8354',
 '1253',
 '8508',
 '8736',
 '1396',
 '4309',
 '3082',
 '2288',
 '2872',
 '2913',
 '2853',
 '9526',
 '7622',
 '4796',
 '4790',
 '1881',
 '1550',
 '4528',
 '4391',
 '8534',
 '2066']

In [219]:
year = 2013
month = 4
new_dict = {}
for h in complete_homes:
    homes_traces[h].series=opdsa.get_home_series_by_year_month(homes_traces[h],year,month)
    #print homes_traces[h].series.index.summary()
    #print homes_traces[h].series.index[0]
    new_dict[h]=homes_traces[h]
    
instances = [app.ApplianceInstance([t],t.metadata) for t in new_dict.values()]
metadata_set= {'source':'OakPark','dataids':complete_homes}
for i in instances:
    print i.traces[0].series.index[0]
app_set = app.ApplianceSet(instances,metadata_set)


2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
2013-04-01 00:00:00
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-219-d2cd509f3b28> in <module>()
     11 for i in instances:
     12     print i.traces[0].series.index[0]
---> 13 app_set = app.ApplianceSet(instances,metadata_set)

/Users/sabina/wikienergy/disaggregator/appliance.pyc in __init__(self, instances, metadata)
    183         Initializes an appliance set given a list of instances.
    184         '''
--> 185         if not utils.instances_aligned(instances):
    186             self.instances = utils.align_instances(instances)
    187         else:

/Users/sabina/wikienergy/disaggregator/utils.py in instances_aligned(instances)
    373     traces = map(list,zip(*[instance.traces for instance in instances]))
    374     for traces_ in traces:
--> 375         if not traces_aligned(traces_):
    376             print "two"
    377             return False

/Users/sabina/wikienergy/disaggregator/utils.py in traces_aligned(traces)
    356     indices = [trace.series.index.summary() for trace in traces]
    357     for index in indices[1:]:
--> 358         if not indices[0].equals(index):
    359             print index
    360             return False

AttributeError: 'unicode' object has no attribute 'equals'

In [236]:
t_list = new_dict.values()

In [239]:
utils.traces_aligned(t_list)


Out[239]:
True

In [3]:
home= homes[dataids[0]]

In [6]:
trace = opdsa.generate_trace_by_dataid(homes,dataids[0])

In [10]:
dt_index =  trace.series.index

In [29]:
trace.series.index.searchsorted(datetime(2013, 1, 2))


Out[29]:
8112

In [44]:
year = 2013
month = 1
day = 1
dt_start = datetime(year,month,day)
dt_end = datetime(year,month,calendar.monthrange(year,month)[1])

In [60]:
index_start = trace.series.index.searchsorted(dt_start)
index_end = trace.series.index.searchsorted(dt_end)
print index_end-index_start


1440

In [52]:
test = trace.series[index_start:index_end]

In [56]:
def get_home_series_by_year_month(year,month,trace):
    '''
    Returns the home's series sliced by year and month. 
    '''
    dt_start = datetime(year,month,1)
    dt_end = datetime(year,month,calendar.monthrange(year,month)[1])
    index_start = trace.series.index.searchsorted(dt_start)
    index_end = trace.series.index.searchsorted(dt_end)
    
    if index_start == 0 or index_end==0:
        return -1
    else:
        return  trace.series[index_start:index_end]

In [70]:
def get_list_of_homes_with_certain_month_year(homes,year,month):
    '''
    Returns a list of homes which have complete trace info for given year and month. 
    Assumes that homes is dict where keys are dataids and values are traces. 
    '''
    complete_homes = []
    for h in homes.keys():
        dt_start = datetime(year,month,1)
        dt_end = datetime(year,month,calendar.monthrange(year,month)[1])
        index_start = homes[h].series.index.searchsorted(dt_start)
        index_end = homes[h].series.index.searchsorted(dt_end)
        if index_end-index_start==((dt_end-dt_start)*48).days:
            complete_homes.append(h)
    return complete_homes

In [71]:
get_list_of_homes_with_certain_month_year(hom,2013,6)


1392
1392 days, 0:00:00
Out[71]:
['3923']

In [62]:
dataids[0]


Out[62]:
'3923'

In [63]:
hom = {'3923':trace}

In [ ]:


In [ ]: