def fhmm_disag(): # TRAIN disag = fhmm_exact.FHMM() disag.train(meters) # TEST appliance_powers = disag.disaggregate_chunk(mains) for meter, df in appliance_powers.iteritems(): appliance = meter.dominant_appliance() appliance_type = appliance.identifier.type if appliance_type in MAINS_APPLIANCES: y_pred = df.values np.save(join(BASE_DIRECTORY, 'FHMM_' + appliance_type), y_pred) appliance_powers = disag.disaggregate_chunk(fridge_mains) for meter, df in appliance_powers.iteritems(): appliance = meter.dominant_appliance() appliance_type = appliance.identifier.type if appliance_type in ['fridge freezer']: y_pred = df.values np.save(join(BASE_DIRECTORY, 'FHMM_' + appliance_type), y_pred) appliance_powers = disag.disaggregate_chunk(kettle_mains) for meter, df in appliance_powers.iteritems(): appliance = meter.dominant_appliance() appliance_type = appliance.identifier.type if appliance_type in ['kettle']: y_pred = df.values np.save(join(BASE_DIRECTORY, 'FHMM_' + appliance_type), y_pred)
def import_nilm_model(self, filepath, m_type): if m_type is "FHMM": self.model = fhmm_exact.FHMM() self.model.import_model(filepath) elif m_type is "CombOpt": self.model = combinatorial_optimisation.CombinatorialOptimisation() self.model.import_model(filepath)
def fhmm(start_train, end_train, start_test, end_test, train_elec): #Start training data.set_window(start_train, end_train) elec = data.buildings[1].elec fhmm = fhmm_exact.FHMM() fhmm.train(train_elec, sample_period=1) #Start disaggregating data.set_window(start_test, end_test) disag_filename = './build/disagg_sum_fhmm_{}_k.h5'.format( len(train_elec.meters)) output = HDFDataStore(disag_filename, 'w') fhmm.disaggregate(elec.mains(), output) output.close() dates_dict = { "start_train": start_train, "end_train": end_train, "start_test": start_test, "end_test": end_test } # write test and train timeframe into json file with open(disag_filename + ".json", 'w') as dates_file: json.dump(dates_dict, dates_file) disag = DataSet(disag_filename) disag_elec = disag.buildings[1].elec disag_elec.plot() plt.title("FHMM") plt.show() #Calculate F1-Score f1 = f1_score(disag_elec, train_elec) f1.index = disag_elec.get_labels(f1.index) f1.plot(kind='barh') plt.ylabel('appliance') plt.xlabel('f-score') plt.title("FHMM") plt.show()
import matplotlib.pyplot as plt """ This file replicates issue #376 (which should now be fixed) https://github.com/nilmtk/nilmtk/issues/376 """ data_dir = '/data' building_number = 3 disag_filename = join(data_dir, 'disag-fhmm' + str(building_number) + '.h5') data = DataSet(join(data_dir, 'redd.h5')) print("Loading building " + str(building_number)) elec = data.buildings[building_number].elec top_train_elec = elec.submeters().select_top_k(k=5) fhmm = fhmm_exact.FHMM() fhmm.train(top_train_elec) output = HDFDataStore(disag_filename, 'w') fhmm.disaggregate(elec.mains(), output) output.close() ### f1score fhmm disag = DataSet(disag_filename) disag_elec = disag.buildings[building_number].elec f1 = f1_score(disag_elec, elec) f1.index = disag_elec.get_labels(f1.index) f1.plot(kind='barh') plt.ylabel('appliance') plt.xlabel('f-score')
df_all_noNan = df_all.dropna() a = df_all_noNan.keys() middleTime = a[int(math.floor(a.size/2))] middleTimeStr = "%d-%02d-%02d %02d:%02d:%02d" % (middleTime.year, middleTime.month, middleTime.day, middleTime.hour, middleTime.minute, middleTime.second) print(middleTimeStr) train.set_window(end=middleTimeStr) test.set_window(start=middleTimeStr) train_elec = train.buildings[building_number].elec test_elec = test.buildings[building_number].elec top_train_elec = train_elec.submeters().select_top_k(k=5) fhmm = fhmm_exact.FHMM() #mk change this later to default fhmm.train(top_train_elec, sample_period=60, resample=True) outputAddress = "/nilmtk/data/iawe_449_3.h5" output = HDFDataStore(outputAddress, 'w') fhmm.disaggregate(test_elec.mains(), output, sample_period=60, resample=True) output.close() disag = DataSet(outputAddress) #load FHMM prediction disag_elec = disag.buildings[building_number].elec #disag_elec.plot() # plot all disaggregated data f1 = f1_score(disag_elec, test_elec) f1.index = disag_elec.get_labels(f1.index) f1.plot(kind='barh') disag.store.window = TimeFrame(start='2013-07-10 18:00:00-05:00', end='2013-07-17 04:00:00-05:00')
test = DataSet(testPath) train_elec = train.buildings[trainBuilding].elec test_elec = test.buildings[testBuilding].elec # select the larger sampling period between the train and the test set samplePeriod = next(iter( train.metadata['meter_devices'].values()))['sample_period'] testSamples = next(iter( test.metadata['meter_devices'].values()))['sample_period'] if samplePeriod < testSamples: samplePeriod = testSamples # train the appropriate algorithm clf = '' if algorithm == 'fhmm': clf = fhmm_exact.FHMM() elif algorithm == 'combOpt': print('here') clf = CombinatorialOptimisation() start = time.time() clf.train(train_elec, sample_period=samplePeriod) end = time.time() print('Training runtime =', end - start, 'seconds.') # make predicitons pred = {} testChunks = test_elec.mains().load(sample_period=samplePeriod) for i, chunk in enumerate(testChunks): chunk_drop_na = chunk.dropna() pred[i] = clf.disaggregate_chunk(chunk_drop_na) print('---------------------------------')
def create_nilm_model(self, m_type): if m_type is "FHMM": self.model = fhmm_exact.FHMM() elif m_type is "CombOpt": self.model = combinatorial_optimisation.CombinatorialOptimisation()