def detect_anomaly( self, filename = '' ): sys.stdout.write( '[INFO]: start detecting file:%s\n' % (filename) ) #load file all_text = load_file( self.input_path+filename ) if all_text == False: sys.stderr.write( "[ERROR]: load input file failed!" ) return input_list = [] for line in all_text: try: inbound_data = json.loads( line.strip() ) events_num = self.get_event_num( inbound_data ) input_list.append(events_num) except Exception: sys.stderr.write( '[ERROR]: load json failed! %s' %s (line) ) #sort input_list.sort(lambda x,y:cmp(x[1],y[1]), reverse=True) #write result to file f_write = open( self.output_path + 'res.'+ filename, 'a' ) for item in input_list: f_write.write('%s,%s\n' % (item[0], item[1]) ) f_write.close() sys.stdout.write( '[INFO]: done. write result into path:%s\n' % (self.output_path))
def trigger(self): """ 添加触发事件 """ self.file1.clicked.connect(lambda: tools.choose_file(self.file_path1)) self.file2.clicked.connect(lambda: tools.choose_file(self.file_path2)) self.start.clicked.connect(lambda: tools.load_file(self.file_path1, self.file_path2, self.content1, self.content2, self.repetition_rate))
def set_localT(self, value): self._localT = value ## do some other fancy stuff Box._locaT = value self.temperature_history.append(value) ## move to chain for chain in self.chain_list: chain.temperature_history.append(value) ## load new value of exp if value in settings.EXP_TABLES: settings.EXP_TABLE = settings.EXP_TABLES[value] logging.info('change exp_table %f ' % value) else: settings.EXP_TABLE = tools.load_file(settings.ROOT_DIR + \ settings.FILES_TEMPLATE['exp'] % value, {}) settings.EXP_TABLES[value] = settings.EXP_TABLE logging.info('load new exp_table %f' % value) logging.info("Set temperature to: %f" % value)
def load_profile(self): all_text = load_file( self.path ) if all_text == False: sys.stderr.write( "[ERROR]: load profile failed!\n" ) return False #load all the contents into tempory dictionary profile_dict = {} for line in all_text: try: profile = json.loads( line.strip() ) profile_dict[profile['type']] = {'thresholds': profile['thresholds'], 'window':profile['window']} except Exception: sys.stderr.write( "[ERROR] load json failed! %s\n" % (line)) if len(profile_dict) > 0: sys.stdout.write( '[INFO]: load the device profile successfully!\n') return profile_dict else: sys.stderr.write( '[ERROR]: load device profile failed!\n') return False
import tools import GSR import numpy as np # import matplotlib.pyplot as plt import windowing as win filename = "./data/GSR_F01_F.txt" T1 = 0.75 T2 = 2 MX = 1 DELTA = 0.02 nFS = 16 gsr_data = tools.load_file(filename, header=8, sep=",") # 8 "," #TODO GAUSSIANA gsr_data = tools.downsampling(gsr_data, nFS) # plt.figure(1) # plt.plot(gsr_data[:,0], gsr_data[:,1]) # plt.xlabel("Time (s)") # plt.ylabel("GSR (uS)") # plt.title("Raw GSR") # t_gsr, gsr = GSR.remove_spikes(gsr_data[:,1], nFS) t_gsr = gsr_data[:, 0] gsr = gsr_data[:, 1] print gsr.shape # print t_gsr.shape, gsr.shape, gsr_data.shape t_driver, driver, phasic_d, tonic_d = GSR.estimate_drivers( t_gsr, gsr, T1, T2, MX, DELTA)
def test_load_file_normal(self): self.assertEqual( tools.load_file('test_load_file.txt'), ['abcd\n','ef\n'], '#2 test load_file failed' )
def test_load_file_empty(self): self.assertEqual( tools.load_file('abc'), False, '#1 test load_file failed' )
def load_chat_texts(): patterns = tools.load_file('dialog-ger.md') patterns.extend(tools.load_file('german-aixml.md')) patterns.extend(tools.load_file('german-aixml-2.md')) return patterns
"TIME", "ACCX", "ACCY", "ACCZ", "GYRX", "GYRY", "GYRZ", "MAGX", "MAGY", "MAGZ", "LAB" ] col_acc = ["ACCX", "ACCY", "ACCZ"] col_gyr = ["GYRX", "GYRY", "GYRZ"] col_mag = ["MAGX", "MAGY", "MAGZ"] empaticaAccCoeff = 2 * 9.81 / 128 empaticafsamp = 32 sensAccCoeff = 8 * 9.81 / 32768 sensGyrCoeff = 2000 / 32768 sensMagCoeff = 0.007629 sensfsamp = 100 data = tools.load_file(filename, sep=',', header=1) data = tools.downsampling(data, 50) t = tools.selectCol(data, columns_in, "TIME") acc = tools.selectCol(data, columns_in, col_acc) gyr = tools.selectCol(data, columns_in, col_gyr) mag = tools.selectCol(data, columns_in, col_mag) lab = tools.selectCol(data, columns_in, "LAB") acc = inertial.convert_units(acc, coeff=sensAccCoeff) gyr = inertial.convert_units(gyr, coeff=sensGyrCoeff) mag = inertial.convert_units(mag, coeff=sensMagCoeff) # tools.array_labels_to_csv(np.column_stack([t, acc]), np.array(columns_in), "./output/preproc_"+filename[7:-4]+".csv")
import tools import GSR import matplotlib.pyplot as plt import numpy as np import windowing as win filename="./data/GSR.csv" T1=0.75 T2=2 MX=1 DELTA=0.02 FS=4 nFS=4 gsr_data = tools.load_file(filename, header=1, sep=";") # 8 "," #TODO GAUSSIANA # gsr_data=tools.downsampling(gsr_data, FS, nFS) plt.figure(1) plt.plot(gsr_data[:,0], gsr_data[:,1]) plt.xlabel("Time (s)") plt.ylabel("GSR (uS)") plt.title("Raw GSR") # t_gsr, gsr = GSR.remove_spikes(gsr_data[:,1], nFS) t_gsr = gsr_data[:,0] gsr = gsr_data[:,1] # print t_gsr.shape, gsr.shape, gsr_data.shape t_driver, driver, phasic_d, tonic_d= GSR.estimate_drivers(t_gsr, gsr, T1, T2, MX, DELTA, FS=FS) windows=win.generate_dummy_windows(len(phasic_d), 80, 10) features = GSR.extract_features(phasic_d, t_driver, DELTA, windows)
columns_in=["TIME", "ACCX","ACCY","ACCZ", "GYRX","GYRY","GYRZ", "MAGX","MAGY","MAGZ", "LAB"] col_acc=["ACCX", "ACCY", "ACCZ"] col_gyr=["GYRX", "GYRY", "GYRZ"] col_mag=["MAGX", "MAGY", "MAGZ"] empaticaAccCoeff=2*9.81/128 empaticafsamp=32 sensAccCoeff=8*9.81/32768 sensGyrCoeff=2000/32768 sensMagCoeff=0.007629 sensfsamp=100 data = tools.load_file(filename, sep=',', header=1) data=tools.downsampling(data, 50) t=tools.selectCol(data, columns_in, "TIME") acc=tools.selectCol(data, columns_in, col_acc) gyr=tools.selectCol(data, columns_in, col_gyr) mag=tools.selectCol(data, columns_in, col_mag) lab=tools.selectCol(data, columns_in, "LAB") acc= inertial.convert_units(acc, coeff=sensAccCoeff) gyr= inertial.convert_units(gyr, coeff=sensGyrCoeff) mag= inertial.convert_units(mag, coeff=sensMagCoeff) # tools.array_labels_to_csv(np.column_stack([t, acc]), np.array(columns_in), "./output/preproc_"+filename[7:-4]+".csv")
def data_loader(flag): data = load_file(flag) mg, lg = (4, 4) if len(data) < 4 else (3, 2) return data, mg, lg