def analyze_data2(save_var, sample_size, weight_values, file_path, data_filename): if not weight_values: return _datalist, _fencelist, f, ss = [0.0], [], Fence(), sample_size+10 _fencelist.append(f) total_weight = 0.0 for i in range(len(weight_values)): try: data = float(weight_values[i]) except: continue _datalist.append(data) ########## Algorithms Part ########## if (data - total_weight) >= save_var: # record weight _fencelist[-1].piglet_list[-1].weight_list.append(data) elif (total_weight - data) >= save_var: # pick up pig if data < save_var: total_weight = 0.0 f = Fence() _fencelist.append(f) # claculate the average if(len(_fencelist[-1].piglet_list[-1].weight_list)) >= ss: temp_list = _fencelist[-1].piglet_list[-1].weight_list[(ss-sample_size):] for i in range(len(temp_list)): temp_list[i] -= total_weight temp_list[i] = round(temp_list[i],2) _fencelist[-1].piglet_list[-1].real_weight_list = [""] * (ss-sample_size) _fencelist[-1].piglet_list[-1].real_weight_list.extend(temp_list) _fencelist[-1].piglet_list[-1].weight = round(np.mean(temp_list),2) total_weight += round(np.mean(temp_list), 2) _fencelist[-1].weight = total_weight p = Pig() # create a new pig _fencelist[-1].piglet_list.append(p) _fencelist[-1].piglet_num = len(_fencelist[-1].piglet_list) -1 x = PrettyTable() x.field_names = ["pig"] for k in range(1, ss + 1): x.field_names.append(str(k)) x.field_names.append("weight") for i in range(len(_fencelist[0].piglet_list)): temp = [str(i+1)+"_measure"] temp.extend(list(_fencelist[0].piglet_list[i].weight_list)) temp.append(_fencelist[0].piglet_list[i].weight) while(len(temp) < ss + 2): temp.append("none") x.add_row(temp) temp = [str(i+1)+"_calculate"] temp.extend(list(_fencelist[0].piglet_list[i].real_weight_list)) temp.append(_fencelist[0].piglet_list[i].weight) while(len(temp) < ss + 2): temp.append("none") x.add_row(temp) datafile = open(today()+'.html','a', newline='') datafile.write(data_filename + ' method2, sample: '+str(sample_size)+ x.get_html_string()+'\n===========\n') datafile.write('\n') datafile.close() analyzed_data_output("Method2", file_path, data_filename, _fencelist)
def __init__(self, parent, controller, system): tk.Frame.__init__(self, parent) self.controller = controller self.system = system # initial frame self.data_frame() self.weight_frame() self.table_frame() # start weighting self.system.datafile = open(today()+'_'+time()+'.log',"w") f = Fence() self.system.fence_list.append(f) self.totalWeight = 0.0 self.zeroing() self.system.serialthread.start() self.read_data()
def output_csv(self): file_path = getcwd() #取路徑的資料 with open(file_path + "/" + today() + "weaned weight" +'.csv','a+',encoding="utf-8",newline='') as csv_file: write = csv.writer(csv_file) header = ["sow id", "piglet id", "weight","fence weight","number born alive"] write.writerow(header) print("===OUTPUT CSV DEBUG PART===") for j in range(len(self.system.fence_list)-1): print("fence_weight: "+str(self.system.fence_list[j].weight)) # 印在終端機的 if self.system.fence_list[j].weight is not None: for i in range(len(self.system.fence_list[j].piglet_list)-1): print("pig_weight: "+str(self.system.fence_list[j].piglet_list[i].weight)) temp = self.system.fence_list[j].pig_id[i] # [母豬,小豬] temp.append(str(self.system.fence_list[j].piglet_list[i].weight)) # [母豬,小豬,小豬重量] print(list_to_str(temp)) # 印在終端機的 write.writerow(temp) temp1 = ["", "", "", self.system.fence_list[j].weight, len(self.system.fence_list[j].piglet_list)-1] write.writerow(temp1) print("===OUTPUT CSV DEBUG PART===")
def analyzed_data_output(statistic_method, file_path, data_filename, _fencelist): #儲存不同統計方法所得之秤重值,並輸出至csv檔 with open(file_path + "/" + today() + "analyzed data" +'.csv','a',encoding="utf-8",newline='') as csv_file: write = csv.writer(csv_file) inner_list = [data_filename, str(statistic_method)] for j in range(1): # for j in range(len(_fencelist)): for i in range(len(_fencelist[j].piglet_list)): if _fencelist[j].piglet_list[i].weight is not None: inner_list.append(_fencelist[j].piglet_list[i].weight) analyzed_datalist = [inner_list] print(analyzed_datalist) output_list = zip(*analyzed_datalist) write.writerows(output_list) return analyzed_datalist
def analyze_data4(save_var, sample_size, weight_values, file_path, data_filename): if not weight_values: return _datalist, _fencelist, f, ss = [0.0], [], Fence(), sample_size+10 _fencelist.append(f) total_weight = 0.0 for i in range(len(weight_values)): try: data = float(weight_values[i]) except: continue _datalist.append(data) ########## Algorithms Part ########## if (data - total_weight) >= save_var: # record weight _fencelist[-1].piglet_list[-1].weight_list.append(data) elif (total_weight - data) >= save_var: # pick up pig if data < save_var: total_weight = 0.0 f = Fence() _fencelist.append(f) # claculate the average if(len(_fencelist[-1].piglet_list[-1].weight_list)) >= ss: temp_list = _fencelist[-1].piglet_list[-1].weight_list[(ss-sample_size):] for i in range(len(temp_list)): temp_list[i] -= total_weight temp_list[i] = round(temp_list[i], 2) _fencelist[-1].piglet_list[-1].real_weight_list = [""] * (ss-sample_size) _fencelist[-1].piglet_list[-1].real_weight_list.extend(temp_list) # ave = round(mean(temp_list), 2) # 平均 # std_err = round(std(temp_list), 2) # 標準差 # _fencelist[-1].piglet_list[-1].std_err = std_err ave = np.mean(temp_list) # 平均 std_err = np.std(temp_list) # 標準差 _fencelist[-1].piglet_list[-1].std_err = round(std_err,2) temp_list_std, _fencelist[-1].piglet_list[-1].mark_std = [], [0] * ss for i in range(len(temp_list)): if abs(temp_list[i] - ave) <= 0.8*std_err: temp_list_std.append(temp_list[i]) _fencelist[-1].piglet_list[-1].mark_std[i+(ss-sample_size)] = 1 _fencelist[-1].piglet_list[-1].std_weight_list = temp_list_std last_ave = round(np.mean(temp_list_std), 2) _fencelist[-1].piglet_list[-1].weight = last_ave total_weight += last_ave _fencelist[-1].weight = total_weight p = Pig() # create a new pig _fencelist[-1].piglet_list.append(p) _fencelist[-1].piglet_num = len(_fencelist[-1].piglet_list) -1 x = PrettyTable() x.field_names = ["pig"] for k in range(1, ss+1): x.field_names.append(str(k)) x.field_names.extend(["std", "weight"]) for i in range(len(_fencelist[0].piglet_list)): if _fencelist[0].piglet_list[0] == 0.0: continue temp = [str(i+1)+"_measure"] temp.extend(list(_fencelist[0].piglet_list[i].weight_list)) while(len(temp) < ss + 3): temp.append("none") x.add_row(temp) temp = [str(i+1)+"_calculate"] temp.extend(list(_fencelist[0].piglet_list[i].real_weight_list)) temp.append(_fencelist[0].piglet_list[i].std_err) while(len(temp) < ss + 3): temp.append("none") x.add_row(temp) temp = [str(i+1)+"_std"] for j in range(len(_fencelist[0].piglet_list[i].real_weight_list)): if j < ss-sample_size: continue if _fencelist[0].piglet_list[i].mark_std[j] == 0: _fencelist[0].piglet_list[i].real_weight_list[j] = '-' temp.extend(_fencelist[0].piglet_list[i].real_weight_list) while(len(temp) < ss + 1): temp.append("none") temp.append(_fencelist[0].piglet_list[i].std_err) temp.append(_fencelist[0].piglet_list[i].weight) x.add_row(temp) datafile = open(today()+'.html', 'a', newline='\n') datafile.write(data_filename+' method4, sample: '+str(sample_size)+x.get_html_string()+'\n===========\n') datafile.write('\n') datafile.close() analyzed_data_output("Method4", file_path, data_filename, _fencelist)
def read_data(self): #讀取資料 value = True while self.system.dataQueue.qsize(): try: # get data from queue data = self.system.dataQueue.get() currentTime = self.system.timeQueue.get() # print("original data: "+ str(data)) data = data.decode().strip("ST,GS,").strip("US,GS,").strip("ST,NT,").strip("ST,TR,").strip("OL,GS") data = data.strip().strip("kg").strip().strip("+").replace(" ", "") if value: try: # transfer to float data = float(data) value = False error = 0 except: error = 1 print("read_data except_error") if not error: self.weight_var.set(data) # local_dt = datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Asia/Taipei')) # currentTime = pytz.timezone('Asia/Taipei').normalize(local_dt).strftime('%H:%M:%S\'%f')[:-3] print("serial: " + currentTime + " " + str(data)) self.system.datafile.write(currentTime + " " + str(data) + "\n") ########## Algorithms Part ########## if (data - self.totalWeight) >= self.system.threshold: # record weight self.system.fence_list[-1].piglet_list[-1].weight_list.append(data) self.system.fence_list[-1].piglet_list[-1].time_list.append(currentTime) self.pig_weight_show.configure(bg="gray77",fg="gray77") self.litter_weighing_show.configure(bg="gray77",fg="gray77") elif (self.totalWeight - data) >= self.system.threshold: # pick up pig if data < self.system.threshold and data >=0: self.litter_weight.set(str(round(self.system.fence_list[-1].weight,2))+"kg") self.litter_weighing_show.configure(bg="white",fg="black") self.en_sow.configure(font=("Calibri",26),width=10, fg="red") self.input_sow_id.set("請輸入耳號") self.totalWeight = 0.0 self.clear_table() self.system.datafile.close() self.system.datafile = open(today()+'_'+time()+'.log',"w") f = Fence() self.system.fence_list.append(f) self.zeroing() # claculate the average statement = False #自動/手動決定重量有不同的判斷式 if (len(self.system.fence_list[-1].piglet_list[-1].weight_list)) >= self.system.sampleSize: if self.system.autoMode: statement = True if str(self.focus_get()) == ".!frame.!frame.!button2": if not self.system.autoMode: statement = True local_dt = datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Asia/Taipei')) tmp = pytz.timezone('Asia/Taipei').normalize(local_dt).strftime('%H:%M:%S\'%f')[:-3] + " press_decide_weight_button\n" self.system.datafile.write(tmp) #記錄按按鈕的時間 if statement: # check if stationary or not index = self.system.fence_list[-1].piglet_list[-1].index temp_list = self.system.fence_list[-1].piglet_list[-1].weight_list[index:index+self.system.sampleSize] time_list = self.system.fence_list[-1].piglet_list[-1].time_list[index:index+self.system.sampleSize] self.system.fence_list[-1].piglet_list[-1].index += 1 # pass the temp_list and time_list into kpss_test function to check if it is stationary or not ser_data = pd.Series(temp_list, index=time_list) self.system.fence_list[-1].piglet_list[-1].kptest.append(1 if kpss_test(ser_data) else 0) # record the kpss return value if len(self.system.fence_list[-1].piglet_list[-1].kptest) < 5: continue # less than five times if self.system.fence_list[-1].piglet_list[-1].kptest[-5:] != [1]*5: continue # the latest five value is not stationary # is stationary! -> calculate the average temp_list = self.system.fence_list[-1].piglet_list[-1].weight_list[index-4:index+self.system.sampleSize] temp_list = [round(i-self.totalWeight, 2) for i in temp_list] # minus the former totalWeight to get actual weight last_ave = round(np.mean(temp_list), 2) # get the average self.system.fence_list[-1].piglet_list[-1].weight = last_ave # record the pig weight actual value self.totalWeight += last_ave # renew totalWeight self.system.fence_list[-1].weight = self.totalWeight # record the fence weight # 儲存豬耳號 temp_ID=[self.input_sow_id.get() ,self.input_piglet_id.get()] self.system.fence_list[-1].pig_id.append(temp_ID) # record ID self.tree.insert("","end",values=[self.input_piglet_id.get(), str(last_ave)]) # add pigID and weight in the table self.update_minmax(last_ave) # update the min and max value # claculate pig number if self.system.fence_list[-1].piglet_list is not []: self.piglet_save_num.set("已存豬數:"+str(len(self.system.fence_list[-1].piglet_list))) self.system.fence_list[-1].piglet_num = len(self.system.fence_list[-1].piglet_list) # show information on GUI self.piglet_weight.set(str(round(self.system.fence_list[-1].piglet_list[-1].weight,2))+"kg") self.pig_weight_show.configure(bg="white",fg="black") self.en_piglet.configure(font=("Calibri",26),width=10, fg="red") self.input_piglet_id.set("請輸入耳號") # create a new pig, be ready to weight the next pig p = Pig() self.system.fence_list[-1].piglet_list.append(p) except queue.Empty: pass self.weight_value_show = self.label_weighing_nowshow.after(100, self.read_data)