def fit(self, write_lrdata=1): lr = LinearRegression() if write_lrdata == 1: # Write Header for LrData with open(self.lrdata_file[self.stype], "w") as csvlf: writer = csv.writer(csvlf) writer.writerow(self.lr_header[self.stype]) self.x.clear() self.y.clear() with open(sda.stdata_file[self.stype]) as csvf: reader = csv.reader(csvf) header = next(reader) # print(header) for row in reader: sdata = sda.StringData(row) tmp = [] # Tension self.y.append(float(sdata.get_tension())) tmp.append(float(sdata.get_tension())) xlist = self.get_lrdata_xlist(sdata) self.x.append(xlist) tmp.extend(xlist) # print(tmp) if write_lrdata == 1: # Write Data for LrData with open(self.lrdata_file[self.stype], "a") as csvlf: writer = csv.writer(csvlf) writer.writerow(tmp) npa_x = np.array(self.x) npa_y = np.array(self.y) lr.fit(npa_x, npa_y) lr_coef = [] for val in lr.coef_: lr_coef.append(val) lr_coef.append(lr.intercept_) with open(self.lrcoef_file[self.stype], "w") as csvlf: writer = csv.writer(csvlf) writer.writerow(lr_coef) # Caluculate MAE, MSE, R2 sum_error = 0.0 sum_error2 = 0.0 sum_error2_avg = 0.0 self.lrcal_y.clear() self.error.clear() y_avg = sum(self.y) / len(self.y) for i, xlist in enumerate(self.x): self.lrcal_y.append(self.get_lrcal_tension(xlist)) error = self.y[i] - self.lrcal_y[i] error_avg = self.y[i] - y_avg self.error.append(error) sum_error += abs(error) sum_error2 += error * error sum_error2_avg += error_avg * error_avg self.maerror = sum_error / len(self.x) self.mserror = sum_error2 / len(self.x) self.r2_score = 1 - sum_error2 / sum_error2_avg
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0]))) import string_data as sda import string_rec as sre import string_lr as slr for key, name in sda.stdata_file.items(): x = [] y = [] with open(name) as csvf: reader = csv.reader(csvf) header = next(reader) # print(header) i = 0 for row in reader: sdata = sda.StringData(row) i += 1 # Frequency x.append(sdata.get_f0()) # Tension y.append(sdata.get_tension()) sample_n = int(i / sre.REC_N) # Show graph plt.title(sda.sname[key] + " Stringing Data " + str(sample_n)) plt.xlabel("Frequency (Hz)") plt.ylabel("Tension (lbs)") plt.scatter(x, y) pf1 = np.polyfit(x, y, 1) pf1_a = round(pf1[0], 4) pf1_b = round(pf1[1], 3) if pf1_b < 0:
if os.path.exists(dirpath) == False: print(dirpath + " is not found.") sys.exit() dir = Path(dirpath) files = sorted(dir.glob("*.wav")) x = [] y = [] yy = [] cnt = 0 x_max = 0 for wavfile in files: wavfile = str(wavfile) sdata = sda.StringData() sdata.make_data(wavfile) stype = sdata.get_stype() stglr = slr.StringLr01(stype) xlist = stglr.get_lrdata_xlist(sdata) if cnt == 0: dt0 = sdata.get_datetime() td = sdata.get_datetime() - dt0 hours = td.total_seconds() / 3600.0 x.append(hours) y.append(stglr.get_lrcal_tension(xlist)) yy.append(sdata.get_tension()) cnt += 1 x_max = td.seconds # Show graph
def run_click(): # run_btn.grid_forget() # data_exp_label_text.set("Recording...") messagebox.showinfo( "Rcording", "After ckicking [OK] button,\nplease hit your racket face near PC.") sdef = sde.StringDef(stype) main_s = "" cross_s = "" if main_s_comb.get() != "": main_s = sdef.get_key_from_name(main_s_comb.get()) if cross_s_comb.get() != "": cross_s = sdef.get_key_from_name(cross_s_comb.get()) args = "" if stype == "B": args = sda.get_argstr(stype, main_t_comb.get(), cross_t_comb.get(), main_s, cross_s, size_comb.get(), "", "") if stype == "T": args = sda.get_argstr(stype, main_t_comb.get(), cross_t_comb.get(), main_s, cross_s, size_comb.get(), main_n_comb.get(), cross_n_comb.get()) csv_append = 0 if data_comb.get() == "Yes": csv_append = 1 if csv_append == 0: srec = sre.StringRec(args, "tmp/") if csv_append == 1: srec = sre.StringRec(args) # data_exp_label_text.set("Calculating...") tension = [] for filename in srec.get_file_list(): sdata = sda.StringData() sdata.make_data(filename) stglr = slr.StringLr01(stype) xlist = stglr.get_lrdata_xlist(sdata) tension.append(stglr.get_lrcal_tension(xlist)) tension_error = 0.0 for i, val in enumerate(tension): tension_error += abs(tension[0] - val) tension_error = tension_error / (len(tension) - 1) print("MAError = " + str(tension_error)) rstr = "" if tension_error > sda.tension_error_val[stype]: for filename in srec.get_file_list(): os.remove(filename) print("Removed: " + filename) rstr = "Measurement error.\nPlease try again." else: if csv_append == 1: for filename in srec.get_file_list(): sdata = sda.StringData() sdata.make_data(filename, csv_append) total = 0.0 avg = 0.0 for i, val in enumerate(tension): rstr += str(i + 1) + ": " + str(round(val, 2)) + " lbs\n" total += float(val) avg = total / len(tension) rstr += "Average: " + str(round(avg, 2)) + " lbs" srec.init_file_list() messagebox.showinfo("Results", rstr) print(rstr) main_win.focus_force()