def setUp_dynamic(self): # select total power data in a period # datastore = Data_store(redd_hdf5_path='D:\SJTU\湖北项目\数据\h5s/30xiayurong.h5') # datastore = Data_store(redd_hdf5_path='D:\SJTU\湖北项目\数据\h5s/30xusuqian.h5') # datastore = Data_store(redd_hdf5_path='/home/uftp/hubei/30xusuqian.h5') # datastore = Data_store(redd_hdf5_path='/home/uftp/hubei/30fake.h5') pss = [] # datarange = [pd.Timestamp('2017-12-15 10:00:00'), pd.Timestamp('2017-12-15 12:00:00')] appliance_truth = {} appliance_consumtion = {} for app in self.datastore.appliance_names: if (app in ['meter', 'TVbox', 'TV']) and self.home == 'xusuqian': if app == 'meter': totalpower = self.datastore.get_instance_ps( appliance_name=app, instance='1').loc[self.datarange[0]:self.datarange[-1]] continue theps = self.datastore.get_instance_ps( appliance_name=app, instance='1').loc[self.datarange[0]:self.datarange[-1]] appliance_truth[app] = theps pss.append(theps) appliance_consumtion[app] = Tools.ps_consumption(theps=theps) # if(app=='lamp'): # print() knownps = aggregate_with_resample(pss) appliance_truth['unknown'] = totalpower - knownps appliance_consumtion['unknown'] = Tools.ps_consumption( theps=appliance_truth['unknown']) ps = totalpower cluster = Clustering() print('miaomiaomiao?') del pss # ps.plot() # ps=median_filter(ps=ps) # ps.plot() # plt.show() # 获得states_list from readData.getdistributions import getDistribitions self.appliance_truth = appliance_truth # centers_list, states_list = getDistribitions(ps=ps, redd_hdf5_path='D:\SJTU\湖北项目\数据\h5s/30xusuqian.h5', # center_path='D:\SJTU\湖北项目\数据\ori\\xusuqian') # centers_list, states_list = getDistribitions(ps=ps) centers_list, states_list = getDistribitions( ps=ps, redd_hdf5_path='/home/uftp/hubei/30%s.h5' % self.home, center_path='/home/uftp/hubei/ori/%s' % self.home, load=False) self.appliance_consumtion = appliance_consumtion self.inference = Inference(total_ps=ps, states_list=states_list)
class TestMedian_filter(TestCase): # datastore = Data_store(redd_hdf5_path='/home/uftp/hubei/30xusuqian.h5') datastore = Data_store(redd_hdf5_path='D:\SJTU\湖北项目\数据\h5s/30xusuqian.h5') pss = [] datarange = [ pd.Timestamp('2017-12-15 10:00:00'), pd.Timestamp('2017-12-15 12:00:00') ] for app in datastore.appliance_names: pss.append( datastore.get_instance_ps( appliance_name=app, instance='1').loc[datarange[0]:datarange[-1]]) ps = aggregate_with_resample(pss) def test_median_filter(self): ps = self.ps ps.plot() ps = median_filter(ps=ps) ps.plot() plt.show()
# datarange = [pd.Timestamp('2017-12-15 10:00:00'), pd.Timestamp('2017-12-15 12:00:00')] appliance_truth = {} appliance_consumtion = {} for app in datastore.appliance_names: if (app in ['meter']) and home == 'xiayurong': if app == 'meter': totalpower = datastore.get_instance_ps( appliance_name=app, instance='1').loc[datarange[0]:datarange[-1]] continue theps = datastore.get_instance_ps( appliance_name=app, instance='1').loc[datarange[0]:datarange[-1]] appliance_truth[app] = theps pss.append(theps) appliance_consumtion[app] = Tools.ps_consumption(theps=theps) # if(app=='lamp'): # print() knownps = aggregate_with_resample(pss) appliance_truth['unknown'] = totalpower - knownps appliance_consumtion['unknown'] = Tools.ps_consumption( theps=appliance_truth['unknown']) labels = [] fracs = [] for key, var in appliance_consumtion.items(): print(key + ":" + str(var)) labels.append(key) fracs.append(var) # plt.subplot() plt.pie(fracs, labels=labels, shadow=True) plt.show()
def staticGo(datastore,datarange, home='xusuqian', unknown=True): def r3_get_r2(r3list, label, app_statedict: dict, psdict: dict): print('Using history data to extract r2 belonging to r3') df = pd.DataFrame(columns=list(psdict.keys())) r3combi = {} # generating dataframe for key, ps in psdict.items(): app_state = app_statedict[key] state_num = len(app_state) clf = NearestCentroid() clf.fit(np.append([0], np.array([i.center_value for i in app_state])).reshape(-1, 1), np.array(range(state_num + 1))) df[key] = clf.predict(ps.values.reshape(-1, 1)) for idn, r3 in enumerate(r3list): idx = label == idn tempt = df.iloc[idx] combination = set([tuple(i) for i in list(tempt.values)]) r3combi[idn] = combination r2list = [] state_count = [] for r2row in combination: app_state_tuple = [] for kk, key in enumerate(df.columns): if r2row[kk] > 0: app_state_tuple.append(app_statedict[key][r2row[kk] - 1]) if app_state_tuple != []: r2list.append(State_r2(tuple(app_state_tuple))) state_count.append(np.count_nonzero((tempt == np.array(r2row)).all(1))) else: r2list.append(State_r2(None)) state_count.append(np.count_nonzero((tempt == np.array(r2row)).all(1))) r3.set_state_r2_list(r2list, False) r3.statecount = state_count if (len(r2list) != len(state_count)): print() print('r2 extracting finished') appliance_truth = {} appliance_consumtion = {} if unknown: ps = None for app in datastore.appliance_names: if (app in ['TVbox', 'TV']) and home == 'xusuqian': continue if (app in ['lamp', 'TV']) and home == 'xiayurong': continue if (app in ['sterilizer', 'iron', 'kitchen', 'TV']) and home == 'zhouqi': continue if (app == 'meter'): for key in datastore.keys_dict[app]: meterdata = datastore.get_instance_ps(appliance_name=app, instance=key).loc[datarange[0]: datarange[-1]] if ps == None: ps = meterdata else: ps += meterdata continue for key in datastore.keys_dict[app]: theps = datastore.get_instance_ps(appliance_name=app, instance=key).loc[datarange[0]: datarange[-1]] appliance_truth[app + '_' + key] = theps appliance_consumtion[app + '_' + key] = Tools.ps_consumption(theps=theps) else: pss = [] for app in datastore.appliance_names: if (app in ['TVbox', 'TV']) and home == 'xusuqian': continue if (app in ['lamp', 'TV']) and home == 'xiayurong': continue if (app in ['sterilizer', 'iron', 'kitchen', 'TV']) and home == 'zhouqi': continue if (app in ['meter', 'unknown']): continue for key in datastore.keys_dict[app]: theps = datastore.get_instance_ps(appliance_name=app, instance=key).loc[datarange[0]:datarange[-1]] appliance_truth[app + '_' + key] = theps appliance_consumtion[app + '_' + key] = Tools.ps_consumption(theps=theps) pss.append(theps) ps = aggregate_with_resample(pss) del pss ps_dict = appliance_truth print('搞定了ps') # ps = median_filter(ps=ps) _, states_list = getDistribitions(ps=ps, redd_hdf5_path='/home/uftp/hubei/4test/30%s.h5' % home, center_path='/home/uftp/hubei/ori/%s' % home, load=False) clustering = Clustering() centers = clustering.deal_with_ps_b(ps=ps) centers = step_clustering(ps) print('类中心是:') print(centers) # centers=Tools.deserialize_object('centers') Tools.serialize_object(centers, 'centers') state_r3_list = [State_r3(value=center) for center in centers] centers_array = np.array([[center] for center in centers]).reshape(-1, 1) kmeans = KMeans(n_clusters=len(centers), random_state=0) kmeans.cluster_centers_ = centers_array toLabel = ps.values.reshape(-1, 1) label_idxs = kmeans.predict(toLabel) states_dict = {} for state in states_list: appliance_name = state.appliance_type instance = state.instance try: states_dict[appliance_name + '_' + instance].append(state) except KeyError: states_dict[appliance_name + '_' + instance] = [state] # states_dict, ps_dict, label_idxs, state_r3_list r3_get_r2(state_r3_list, label_idxs, states_dict, ps_dict) print('rer3') return appliance_truth, ps, states_list, centers, state_r3_list,appliance_consumtion # staticGo()
def setUp_dynamic(self): # select total power data in a period # datastore = Data_store(redd_hdf5_path='D:\SJTU\湖北项目\数据\h5s/30xiayurong.h5') # datastore = Data_store(redd_hdf5_path='D:\SJTU\湖北项目\数据\h5s/30xusuqian.h5') # datastore = Data_store(redd_hdf5_path='/home/uftp/hubei/30xusuqian.h5') # datastore = Data_store(redd_hdf5_path='/home/uftp/hubei/30fake.h5') pss = [] # datarange = [pd.Timestamp('2017-12-15 10:00:00'), pd.Timestamp('2017-12-15 12:00:00')] appliance_truth = {} appliance_consumtion = {} if self.unknown: for app in self.datastore.appliance_names: if (app in ['TVbox', 'TV']) and self.home == 'xusuqian': continue if (app in ['lamp', 'TV']) and self.home == 'xiayurong': continue if (app in ['sterilizer', 'iron', 'kitchen', 'TV' ]) and self.home == 'zhouqi': continue if (app == 'meter'): for key in self.datastore.keys_dict[app]: meterdata = self.datastore.get_instance_ps( appliance_name=app, instance=key ).loc[self.datarange[0]:self.datarange[-1]] try: ps += meterdata except: ps = meterdata continue for key in self.datastore.keys_dict[app]: theps = self.datastore.get_instance_ps( appliance_name=app, instance=key).loc[self.datarange[0]:self.datarange[-1]] appliance_truth[app + '_' + key] = theps appliance_consumtion[app + '_' + key] = Tools.ps_consumption( theps=theps) else: for app in self.datastore.appliance_names: if (app in ['TVbox', 'TV']) and self.home == 'xusuqian': continue if (app in ['lamp', 'TV']) and self.home == 'xiayurong': continue if (app in ['sterilizer', 'iron', 'kitchen', 'TV' ]) and self.home == 'zhouqi': continue if (app in ['meter', 'unknown']): continue for key in self.datastore.keys_dict[app]: theps = self.datastore.get_instance_ps( appliance_name=app, instance=key).loc[self.datarange[0]:self.datarange[-1]] appliance_truth[app + '_' + key] = theps appliance_consumtion[app + '_' + key] = Tools.ps_consumption( theps=theps) pss.append(theps) ps = aggregate_with_resample(pss) del pss # ps.plot() # ps=median_filter(ps=ps) # ps.plot() # plt.show() # 获得states_list from readData.getdistributions import getDistribitions self.appliance_truth = appliance_truth # centers_list, states_list = getDistribitions(ps=ps, redd_hdf5_path='D:\SJTU\湖北项目\数据\h5s/30xusuqian.h5', # center_path='D:\SJTU\湖北项目\数据\ori\\xusuqian') # centers_list, states_list = getDistribitions(ps=ps) centers_list, states_list = getDistribitions( ps=ps, redd_hdf5_path='/home/uftp/hubei/30%s.h5' % self.home, center_path='/home/uftp/hubei/ori/%s' % self.home, load=False) self.appliance_consumtion = appliance_consumtion self.inference = Inference(total_ps=ps, states_list=states_list)