more accurate timestep

master
michael 2021-08-10 13:16:48 -07:00
parent 3239ac2a2c
commit 22e438963b
7 changed files with 59 additions and 8 deletions

1
.gitignore vendored
View File

@ -140,3 +140,4 @@ cython_debug/
# Local stuff to exclude
.vscode/
mainwin.py
*.sqlite

49
app.py
View File

@ -6,6 +6,10 @@ from db import mem
from mainwin import Ui_MainWindow
from widgets import BaseGraph
import pathlib
from re import search as re_search
from varname.core import nameof
from hashlib import md5
from sqlitedict import SqliteDict
class AppWindow(QtWidgets.QMainWindow, Ui_MainWindow):
@ -13,6 +17,16 @@ class AppWindow(QtWidgets.QMainWindow, Ui_MainWindow):
super(AppWindow, self).__init__()
self.setupUi(self)
# Define syncables
synced_value_widgets = []
synced_check_widgets = []
for wname in vars(self):
w = vars(self).get(wname)
if re_search(r"^spin\_(.*)$", wname) != None:
synced_value_widgets.append(w)
elif re_search(r"^check\_(.*)$", wname) != None:
synced_check_widgets.append(w)
# Helpers
def display_warning(message: str):
@ -40,9 +54,11 @@ class AppWindow(QtWidgets.QMainWindow, Ui_MainWindow):
return
mem['x_data'] = data.transpose()[0]
mem['y_data'] = data.transpose()[1]
timestep = mem['x_data'][1] - mem['x_data'][0]
# timestep = mem['x_data'][1] - mem['x_data'][0]
timestep = (mem['x_data'][-1] - mem['x_data'][0]) / len(mem['x_data'])
mem['timestep'] = timestep
self.spin_timestep.setValue(timestep)
print(timestep)
self.raw_data_graph.plot() # Graph new stuff
# self.groups_graph.clear() # Clear old stuff
@ -61,6 +77,37 @@ class AppWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.voltage.setVisible(False)
self.graph_tabs.setCurrentIndex(0)
# Load from persistent storage & bind write actions
# path_hash = md5(filename.encode('utf-8')).hexdigest()
# def set_value(name, val):
# with SqliteDict(f"./db/{path_hash}.sqlite", autocommit=True) as storage:
# print(f"Change {name} to {val}.")
# storage[name] = val
# print(f"Check: {storage[name]}")
# with SqliteDict(f"./db/{path_hash}.sqlite", autocommit=True) as storage:
# for w in synced_value_widgets:
# name = w.objectName()
# try:
# w.setValue(bool(storage[name]))
# print(f"Loaded {name}.")
# except KeyError:
# print(f"Failed to load object {name}.")
# pass
# w.valueChanged.connect(lambda x: set_value(name, x))
# for w in synced_check_widgets:
# name = w.objectName()
# try:
# w.setChecked(storage[name])
# print(f"Loaded {name}.")
# except KeyError:
# print(f"Failed to load object {name}.")
# pass
# w.stateChanged.connect(lambda: set_value(name, w.isChecked()))
# Universal Actions stuff

View File

@ -1,7 +1,7 @@
import numpy as np
from scipy.signal import find_peaks, correlate
from scipy.optimize import curve_fit
from memdb import mem
from db import mem
def minmax(data):
return np.min(data), np.max(data)
@ -281,6 +281,7 @@ def fit_peaks(
overlayed_peak_row = []
for peak_data in peaks_cut:
x_data = np.arange(len(peak_data)) # just placeholder indices
# x_data = np.arange(0, len(peak_data)*mem['timestep'], mem['timestep'])
if not use_advanced:
peak_index = np.argmax(peak_data, axis=0)
else:
@ -289,7 +290,6 @@ def fit_peaks(
params_guess = (peak_index+shift_over, a, y0, tau)
x_data_target = x_data[peak_index+shift_over:]
peak_data_target = peak_data[peak_index+shift_over:]
# popt, pcov = curve_fit(exp_func, x_data_target, peak_data_target, bounds=([-np.inf, 0.0, -np.inf, 0.0], np.inf))
popt, pcov = curve_fit(exp_func, x_data_target, peak_data_target, bounds=([-np.inf, 0.0, -np.inf, 0.0], np.inf), p0=params_guess, maxfev=10000000)
equation_row.append({'popt': popt, 'pcov': pcov})
overlayed_peak_row.append(peak_index)
@ -300,9 +300,9 @@ def fit_peaks(
return equations # list linked with isolated_peaks
def get_tau_data(equation_data):
def get_time_constants(equation_data):
"""
Extracts time constant from all equations (2d array)
Extracts time constant from all fit-output equations (2d array)
Returns
-------
@ -316,6 +316,8 @@ def get_tau_data(equation_data):
for e in r:
tau = e['popt'][3]
row.append(tau)
equation_data.append(row)
tau_data.append(row)
print(tau_data)
return tau_data

0
db/.gitkeep Normal file
View File

View File

@ -1,5 +1,6 @@
PyQt5
pandas
matplotlib
numpy
pyinstaller==4.4
sqlitedict

BIN
screenshots/fit_demo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

View File

@ -78,7 +78,7 @@
<number>6</number>
</property>
<property name="minimum">
<double>-99.989999999999995</double>
<double>0.000000000000000</double>
</property>
<property name="singleStep">
<double>0.010000000000000</double>