From 3315eed1dad821bd990936b0adcb68422b41f02c Mon Sep 17 00:00:00 2001 From: Ryan Xu Date: Sun, 4 Dec 2022 14:40:46 -0800 Subject: [PATCH] convert the resistance value to scientific notation --- read_arduino.py | 10 ++++++---- serial_plotter.py | 9 +++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/read_arduino.py b/read_arduino.py index fbb03a8..207e280 100644 --- a/read_arduino.py +++ b/read_arduino.py @@ -31,9 +31,10 @@ def read(): dat_list = np.random.randint(0, v_in * 1000, SENSORS_MAX) # create a randomized voltage data # take only the nonzero indices, and truncated to two decimal places to "filter" out some hardware errors dat_sel = np.trunc((np.take(dat_list, sensor_ports) / 1000) * 10**2) / 10**2 - r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) + r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration # write + export values as .csv format - dat = f", ".join(np.insert(r_arr.astype(str), 0, datetime.now().strftime('%H:%M:%S'))) + # converted resistance values in array to scientific notation + dat = f", ".join(np.insert(np.format_float_scientific(r_arr.astype(str)), 0, datetime.now().strftime('%H:%M:%S'))) print(dat) f = open(file_name, "a", newline="", encoding="utf-8") f.write(dat + '\n') @@ -54,9 +55,10 @@ def read(): # take only the nonzero indices, and truncated to two decimal places to "filter" out some hardware errors dat_sel = np.trunc((np.take(dat_list, sensor_ports) / 1000) * 10**2) / 10**2 - r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) + r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration # write + export values as .csv format - dat = f", ".join(np.insert(r_arr.astype(str), 0, datetime.now().strftime('%H:%M:%S'))) + # converted resistance values in array to scientific notation + dat = f", ".join(np.insert(np.format_float_scientific(r_arr.astype(str)), 0, datetime.now().strftime('%H:%M:%S'))) print(dat) f = open(file_name, "a", newline="", encoding="utf-8") f.write(dat + '\n') diff --git a/serial_plotter.py b/serial_plotter.py index 2ceef0b..33f4ae8 100644 --- a/serial_plotter.py +++ b/serial_plotter.py @@ -4,7 +4,7 @@ import os, json, traceback, wx import numpy as np import matplotlib import matplotlib.pyplot as plt - +import decimal matplotlib.use("WXAgg") # for JetBrains IDE to force use wxPython as backend UI for plotting @@ -74,9 +74,10 @@ class SerialPlotter: self.sensorsData[i].append(row[i]) # plot a line - # TODO: round the number to 1-2- decimal places - self.axs.plot(self.timeStamps[i], self.sensorsData[i], color=self.colors[i], - label=f'sensor {i + 1}, latest: {np.floor(self.sensorsData[i][-1])} $\Omega$') + # round the number to scientific notation + self.axs.plot(self.timeStamps[i],self.sensorsData[i], color=self.colors[i], + label=f'sensor {i + 1}, latest: {np.format_float_scientific(self.sensorsData[i][-1], precision = 2)} $\Omega$') + self.axs.set_xlabel('Time (seconds)') self.axs.set_ylabel(u'Resistance ($\Omega$)') i += 1