In [24]:
from os.path import isdir, join, dirname, abspath
from os import listdir, getcwd
homedir='/Users/GJWood/nilm_gjw_data/building1'
dir_list = [i for i in listdir(homedir) if isdir(join(homedir,i))]
dir_list.sort()
while dir_list is not 
print 'Current dir list:',dir_list
for folder in dir_list:


Current dir list: ['elec']

In [15]:
from os import listdir, getcwd
dir_list = listdir('/Users/GJWood/nilm_gjw_data/building1/elec'/Users)
dir_list.sort()
print dir_list


['4-POWER_REAL_FINE 2013-11-20 Dump.csv', '4-POWER_REAL_FINE 2013-11-28 Dump.csv', '4-POWER_REAL_FINE 2013-12-01 Dump.csv', '4-POWER_REAL_FINE 2013-12-04 Dump.csv', '4-POWER_REAL_FINE 2013-12-13 Dump.csv', '4-POWER_REAL_FINE 2013-12-27 Dump.csv', '4-POWER_REAL_FINE 2014-01-09 Dump.csv', '4-POWER_REAL_FINE 2014-02-09 Dump.csv', '4-POWER_REAL_FINE 2014-03-27 Dump.csv', '4-POWER_REAL_FINE 2014-05-26 Dump.csv', '4-POWER_REAL_FINE 2015-05-12 Dump.csv', '4-POWER_REAL_FINE 2015-05-27 dump.csv', '5-POWER_REACTIVE_STANDARD 2013-11-20 Dump.csv', '5-POWER_REACTIVE_STANDARD 2013-11-28 Dump.csv', '5-POWER_REACTIVE_STANDARD 2013-12-01 Dump.csv', '5-POWER_REACTIVE_STANDARD 2013-12-04 Dump.csv', '5-POWER_REACTIVE_STANDARD 2013-12-13 Dump.csv', '5-POWER_REACTIVE_STANDARD 2013-12-27 Dump.csv', '5-POWER_REACTIVE_STANDARD 2014-01-09 Dump.csv', '5-POWER_REACTIVE_STANDARD 2014-02-09 Dump.csv', '5-POWER_REACTIVE_STANDARD 2014-03-27 Dump.csv', '5-POWER_REACTIVE_STANDARD 2014-05-26 Dump.csv', '5-POWER_REACTIVE_STANDARD 2015-05-12 Dump.csv', '5-POWER_REACTIVE_STANDARD 2015-05-27 dump.csv']

In [47]:
import pandas as pd
import numpy as np
from copy import deepcopy
from os.path import join, isdir, isfile
from os import listdir
import fnmatch
import re
import datetime
from sys import stdout
from nilmtk.utils import get_datastore
from nilmtk.datastore import Key
from nilmtk.timeframe import TimeFrame
from nilmtk.measurement import LEVEL_NAMES
from nilmtk.utils import get_module_directory, check_directory_exists
from nilm_metadata import convert_yaml_to_hdf5, save_yaml_to_datastore
filename_prefix_mapping = {
	'apparent' : ('4-POWER_REAL_FINE '),
	'reactive' : ('5-POWER_REACTIVE_STANDARD ')
}
filename_suffix_mapping = {
	'apparent' : (' Dump'),
	'reactive' : (' Dump')
}
TIMEZONE = "Europe/London"
TIMESTAMP_COLUMN_NAME = "timestamp"
ACTIVE_COLUMN_NAME = "active"
REACTIVE_COLUMN_NAME = "reactive"
bld_re = re.compile('building\d+')
bld_nbr_re = re.compile ('\d+')
iso_date_re = re.compile ('\d{4}-\d{2}-\d{2}') # used to pull the date from the file name
os.chdir('/Users/GJWood/nilm_gjw_data')
df = pd.DataFrame(columns=[TIMESTAMP_COLUMN_NAME,ACTIVE_COLUMN_NAME,REACTIVE_COLUMN_NAME])
for current_dir, dirs_in_current_dir, files in os.walk(os.getcwd()):
    print 'current_dir',current_dir,'cwd',  os.getcwd()
    print dirs_in_current_dir
    m = bld_re.search(current_dir)
    if m:
        building_name = m.group()
        building_number = int(bld_nbr_re.search(building_name).group())
    for items in fnmatch.filter(files, "4*.csv"):
        d= iso_date_re.search(items).group()
        fn1 = filename_prefix_mapping['apparent']+d+filename_suffix_mapping['apparent']+'.csv'
        fn2 = filename_prefix_mapping['reactive']+d+filename_suffix_mapping['reactive']+'.csv'
        print fn1 +' <-> '+ fn2
        ffn1 = join(current_dir,fn1)
        ffn2 = join(current_dir,fn2)
        df1 = pd.read_csv(ffn1,names=[TIMESTAMP_COLUMN_NAME,ACTIVE_COLUMN_NAME])
        df2 = pd.read_csv(ffn2,names=[TIMESTAMP_COLUMN_NAME,REACTIVE_COLUMN_NAME])
        df3 = pd.merge(df1,df2,on=TIMESTAMP_COLUMN_NAME)
        df = pd.concat([df,df3])
df.drop_duplicates(subset=[TIMESTAMP_COLUMN_NAME], inplace=True)
df.index = pd.to_datetime(df.timestamp.values, unit='s', utc=True)
df = df.tz_convert(TIMEZONE)
df = df.drop(TIMESTAMP_COLUMN_NAME, 1)
print df


current_dir c:\Users\GJWood\nilm_gjw_data cwd c:\Users\GJWood\nilm_gjw_data
['.ipynb_checkpoints', 'building1', 'HDF5', 'metadata']
current_dir c:\Users\GJWood\nilm_gjw_data\.ipynb_checkpoints cwd c:\Users\GJWood\nilm_gjw_data
[]
current_dir c:\Users\GJWood\nilm_gjw_data\building1 cwd c:\Users\GJWood\nilm_gjw_data
['elec']
current_dir c:\Users\GJWood\nilm_gjw_data\building1\elec cwd c:\Users\GJWood\nilm_gjw_data
[]
4-POWER_REAL_FINE 2013-11-20 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2013-11-20 Dump.csv
4-POWER_REAL_FINE 2013-11-28 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2013-11-28 Dump.csv
4-POWER_REAL_FINE 2013-12-01 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2013-12-01 Dump.csv
4-POWER_REAL_FINE 2013-12-04 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2013-12-04 Dump.csv
4-POWER_REAL_FINE 2013-12-13 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2013-12-13 Dump.csv
4-POWER_REAL_FINE 2013-12-27 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2013-12-27 Dump.csv
4-POWER_REAL_FINE 2014-01-09 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2014-01-09 Dump.csv
4-POWER_REAL_FINE 2014-02-09 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2014-02-09 Dump.csv
4-POWER_REAL_FINE 2014-03-27 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2014-03-27 Dump.csv
4-POWER_REAL_FINE 2014-05-26 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2014-05-26 Dump.csv
4-POWER_REAL_FINE 2015-05-12 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2015-05-12 Dump.csv
4-POWER_REAL_FINE 2015-05-27 Dump.csv <-> 5-POWER_REACTIVE_STANDARD 2015-05-27 Dump.csv
current_dir c:\Users\GJWood\nilm_gjw_data\HDF5 cwd c:\Users\GJWood\nilm_gjw_data
[]
current_dir c:\Users\GJWood\nilm_gjw_data\metadata cwd c:\Users\GJWood\nilm_gjw_data
[]
                           active  reactive
2013-11-12 09:28:27+00:00    2553      -319
2013-11-12 09:28:30+00:00    2553        22
2013-11-12 09:28:31+00:00    2553       157
2013-11-12 09:28:32+00:00    2637       157
2013-11-12 09:28:59+00:00    2552      -230
2013-11-12 09:29:00+00:00    2487       302
2013-11-12 09:29:01+00:00    2487        36
2013-11-12 09:29:04+00:00    2638      -730
2013-11-12 09:29:15+00:00    2539       134
2013-11-12 09:29:19+00:00    2688      -556
2013-11-12 09:29:31+00:00    2496       128
2013-11-12 09:29:47+00:00    2449        68
2013-11-12 09:29:51+00:00    2532      -491
2013-11-12 09:30:03+00:00    2471       165
2013-11-12 09:30:04+00:00    2530       165
2013-11-12 09:30:23+00:00    2603      -319
2013-11-12 09:30:27+00:00    2568      -450
2013-11-12 09:30:38+00:00     588      -475
2013-11-12 09:30:39+00:00     588      -787
2013-11-12 09:30:40+00:00     545      -787
2013-11-12 09:30:44+00:00     494      -644
2013-11-12 09:30:47+00:00     368        30
2013-11-12 09:30:48+00:00     390        30
2013-11-12 09:30:51+00:00     586      -818
2013-11-12 09:30:52+00:00     488      -818
2013-11-12 09:30:56+00:00     549      -442
2013-11-12 09:31:00+00:00     506      -653
2013-11-12 09:31:03+00:00     348        72
2013-11-12 09:31:04+00:00     388        72
2013-11-12 09:31:07+00:00     588      -788
...                           ...       ...
2015-05-27 11:41:33+01:00     334       112
2015-05-27 11:41:34+01:00     362        80
2015-05-27 11:43:55+01:00     361        78
2015-05-27 11:43:56+01:00     329       116
2015-05-27 11:53:06+01:00     389       127
2015-05-27 11:53:07+01:00     433        93
2015-05-27 11:55:01+01:00     373        85
2015-05-27 11:55:02+01:00     337       121
2015-05-27 12:24:58+01:00     280       118
2015-05-27 12:24:59+01:00     234        76
2015-05-27 12:32:17+01:00     238        52
2015-05-27 12:59:19+01:00     331       112
2015-05-27 12:59:20+01:00     413        88
2015-05-27 13:06:46+01:00     203        82
2015-05-27 13:07:35+01:00     278       133
2015-05-27 13:16:13+01:00     287       121
2015-05-27 13:16:14+01:00     236        74
2015-05-27 13:32:23+01:00     423       105
2015-05-27 13:32:24+01:00     369       127
2015-05-27 13:37:17+01:00     333       137
2015-05-27 13:37:18+01:00     397       161
2015-05-27 13:52:28+01:00     389        98
2015-05-27 13:54:36+01:00    2498       309
2015-05-27 14:02:15+01:00    2481       316
2015-05-27 14:02:16+01:00    2505       344
2015-05-27 14:04:53+01:00     442       122
2015-05-27 14:08:54+01:00     472       145
2015-05-27 14:08:55+01:00     416       175
2015-05-27 14:15:53+01:00     377       179
2015-05-27 14:41:08+01:00    1048       183

[80297 rows x 2 columns]

In [57]:
print join(homedir,"HDF5","nilm_gjw_data.hdf5")


/Users/GJWood/nilm_gjw_data/building1\HDF5\nilm_gjw_data.hdf5

In [ ]: