2
0
forked from awendt/pyrelacs

14 Commits

Author SHA1 Message Date
wendtalexander
565d6e5318 [plotting] move calibrtion plot outside of main window 2024-09-30 16:22:24 +02:00
wendtalexander
b35a9212ac [plotting] move calibration plot outside of mainwindow.py 2024-09-30 16:22:00 +02:00
wendtalexander
56c8b59ccd [project] changing to absolut imports 2024-09-30 12:10:54 +02:00
wendtalexander
cbc86598b0 [ui] adding information to text field 2024-09-30 12:10:28 +02:00
wendtalexander
031b5098d5 [plotting calibration] fixing power spectrum 2024-09-30 12:09:39 +02:00
wendtalexander
c33e4cc32f Merge branch 'main' into calibration 2024-09-30 10:13:48 +02:00
wendtalexander
4868b0e196 Merge branch 'jgrewe-icons' 2024-09-30 10:13:04 +02:00
wendtalexander
00f6b11740 [project] removing data.nix file 2024-09-30 10:10:32 +02:00
wendtalexander
e0491c5917 [repos/calibration] fixing plots 2024-09-30 10:10:12 +02:00
wendtalexander
9c80091d16 [repos/calibration] removing plots 2024-09-30 10:09:44 +02:00
wendtalexander
2e264fb582 [project] changing to absolut imports 2024-09-30 09:49:11 +02:00
wendtalexander
ab51fa7475 [ui] removing duplicate toolbar, adding textfield 2024-09-30 09:48:47 +02:00
wendtalexander
6a3a610cd3 [project] changing to absolut imports 2024-09-30 09:46:27 +02:00
wendtalexander
8b02b9083f [repos] adding check for spec.loader 2024-09-30 09:45:14 +02:00
6 changed files with 205 additions and 209 deletions

View File

@@ -3,12 +3,16 @@ import sys
from PyQt6.QtCore import QSettings from PyQt6.QtCore import QSettings
from PyQt6.QtWidgets import QApplication from PyQt6.QtWidgets import QApplication
from . import info from pyrelacs import info
from .ui.mainwindow import PyRelacs from pyrelacs.ui.mainwindow import PyRelacs
from .util.logging import config_logging from pyrelacs.util.logging import config_logging
log = config_logging() log = config_logging()
from . import resources # best created with pyside6-rcc resources.qrc -o resources.py (rcc produces an error...) from pyrelacs import (
resources,
) # best created with pyside6-rcc resources.qrc -o resources.py (rcc produces an error...)
def main(): def main():
app = QApplication(sys.argv) app = QApplication(sys.argv)

Binary file not shown.

View File

@@ -159,87 +159,7 @@ class Calibration(MccDac):
unit="s", unit="s",
) )
beat = channel1 + channel2
beat_square = beat**2
f, powerspec = welch(beat, fs=self.SAMPLERATE)
powerspec = decibel(powerspec)
f_sq, powerspec_sq = welch(beat_square, fs=self.SAMPLERATE)
powerspec_sq = decibel(powerspec_sq)
peaks = find_peaks(powerspec_sq, prominence=20)[0]
f_stim, powerspec_stim = welch(channel1, fs=self.SAMPLERATE)
powerspec_stim = decibel(powerspec_stim)
f_in, powerspec_in = welch(channel2, fs=self.SAMPLERATE)
powerspec_in = decibel(powerspec_in)
# axes[0, 0].plot(
# t,
# channel1,
# label=f"{db_value} Readout Channel0",
# color=colors[i],
# )
# axes[0, 0].plot(
# t,
# channel2,
# label=f"{db_value} Readout Channel1",
# color=colors_in[i],
# )
#
# axes[0, 1].plot(
# f_stim,
# powerspec_stim,
# label=f"{db_value} powerspec Channel0",
# color=colors[i],
# )
# axes[0, 1].plot(
# f_in,
# powerspec_in,
# label=f"{db_value} powerspec Channel2",
# color=colors_in[i],
# )
# axes[0, 1].set_xlabel("Freq [HZ]")
# axes[0, 1].set_ylabel("dB")
#
# axes[1, 0].plot(
# t,
# beat,
# label="Beat",
# color=colors[i],
# )
# axes[1, 0].plot(
# t,
# beat**2,
# label="Beat squared",
# color=colors_in[i],
# )
# axes[1, 0].legend()
#
# axes[1, 1].plot(
# f,
# powerspec,
# color=colors[i],
# )
# axes[1, 1].plot(
# f_sq,
# powerspec_sq,
# color=colors_in[i],
# label=f"dB {db_value}, first peak {np.min(f_sq[peaks])}",
# )
# axes[1, 1].scatter(
# f_sq[peaks],
# powerspec_sq[peaks],
# color="maroon",
# )
# axes[1, 1].set_xlabel("Freq [HZ]")
# axes[1, 1].set_ylabel("dB")
# axes[0, 0].legend()
# axes[1, 1].legend()
# plt.show()
self.set_analog_to_zero() self.set_analog_to_zero()
self.disconnect_dac()
def decibel(power, ref_power=1.0, min_power=1e-20): def decibel(power, ref_power=1.0, min_power=1e-20):

View File

@@ -8,9 +8,9 @@ import nixio as nix
import importlib.util import importlib.util
from pyrelacs.util.logging import config_logging from pyrelacs.util.logging import config_logging
log = config_logging() log = config_logging()
from IPython import embed
class Repro: class Repro:
""" """
@@ -25,7 +25,6 @@ class Repro:
def run_repro( def run_repro(
self, nix_file: nix.File, name: str, file: pathlib.Path, *args, **kwargs self, nix_file: nix.File, name: str, file: pathlib.Path, *args, **kwargs
) -> None: ) -> None:
spec = importlib.util.spec_from_file_location("rep", file) spec = importlib.util.spec_from_file_location("rep", file)
if not spec: if not spec:
log.error("Could not load the file") log.error("Could not load the file")
@@ -35,7 +34,10 @@ class Repro:
log.error("Could not load the module of the repro") log.error("Could not load the module of the repro")
else: else:
sys.modules[name] = module sys.modules[name] = module
spec.loader.exec_module(module) if spec.loader is not None:
spec.loader.exec_module(module)
else:
log.error(f"{spec.loader} is None")
if hasattr(module, name): if hasattr(module, name):
rep_class = getattr(module, name) rep_class = getattr(module, name)
rep_class.run(nix_file) rep_class.run(nix_file)

View File

@@ -8,7 +8,7 @@ from PyQt6.QtWidgets import (
QMainWindow, QMainWindow,
QPlainTextEdit, QPlainTextEdit,
QMenuBar, QMenuBar,
QStatusBar QStatusBar,
) )
import uldaq import uldaq
import numpy as np import numpy as np
@@ -18,22 +18,35 @@ import pyqtgraph as pg
from pathlib import Path as path from pathlib import Path as path
from scipy.signal import welch, find_peaks from scipy.signal import welch, find_peaks
from ..worker import Worker from pyrelacs.worker import Worker
from ..repros.repros import Repro from pyrelacs.repros.repros import Repro
from ..util.logging import config_logging from pyrelacs.util.logging import config_logging
from .about import AboutDialog from pyrelacs.ui.about import AboutDialog
from pyrelacs.ui.plots.calibration import CalibrationPlot
log = config_logging() log = config_logging()
_root = path(__file__).parent.parent _root = path(__file__).parent.parent
from IPython import embed from IPython import embed
class PyRelacs(QMainWindow): class PyRelacs(QMainWindow):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
# self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon) # Ensure icons are displayed with text self.setToolButtonStyle(
Qt.ToolButtonStyle.ToolButtonTextBesideIcon
) # Ensure icons are displayed with text
self.setWindowTitle("PyRelacs") self.setWindowTitle("PyRelacs")
self.beat_plot = pg.PlotWidget()
self.power_plot = pg.PlotWidget() self.figure = pg.GraphicsLayoutWidget()
filename = path.joinpath(path.cwd(), "data.nix")
if filename.exists():
self.nix_file = nix.File.open(str(filename), nix.FileMode.ReadOnly)
else:
self.nix_file = nix.File.open(str(filename), nix.FileMode.Overwrite)
self.calibration_plot = CalibrationPlot(self.figure, self.nix_file)
self.threadpool = QThreadPool() self.threadpool = QThreadPool()
self.repros = Repro() self.repros = Repro()
@@ -48,67 +61,70 @@ class PyRelacs(QMainWindow):
self.create_toolbars() self.create_toolbars()
layout = QGridLayout() layout = QGridLayout()
layout.addWidget(self.plot_calibration_button, 0, 0) layout.addWidget(self.figure, 0, 0, 2, 2)
layout.addWidget(self.daq_disconnect_button, 0, 1) layout.addWidget(self.text, 2, 0, 1, 2)
layout.addWidget(self.beat_plot, 2, 0, 1, 2)
layout.addWidget(self.power_plot, 3, 0, 1, 2)
self.toolbar = QToolBar("Repros")
self.addToolBar(self.toolbar)
self.repros_to_toolbar()
# self.setFixedSize(QSize(400, 300))
widget = QWidget() widget = QWidget()
widget.setLayout(layout) widget.setLayout(layout)
self.setCentralWidget(widget) self.setCentralWidget(widget)
filename = path.joinpath(path.cwd(), "data.nix")
self.nix_file = nix.File.open(
str(filename), nix.FileMode.Overwrite
)
def create_actions(self): def create_actions(self):
self._rlx_exitaction = QAction(QIcon(":/icons/exit.png"), "Exit", self) self._rlx_exitaction = QAction(QIcon(":/icons/exit.png"), "Exit", self)
self._rlx_exitaction.setStatusTip("Close relacs") self._rlx_exitaction.setStatusTip("Close relacs")
self._rlx_exitaction.setShortcut(QKeySequence("Alt+q")) self._rlx_exitaction.setShortcut(QKeySequence("Alt+q"))
self._rlx_exitaction.triggered.connect(self.on_exit) self._rlx_exitaction.triggered.connect(self.on_exit)
self._rlx_aboutaction = QAction("about") self._rlx_aboutaction = QAction("about")
self._rlx_aboutaction.setStatusTip("Show about dialog") self._rlx_aboutaction.setStatusTip("Show about dialog")
self._rlx_aboutaction.setEnabled(True) self._rlx_aboutaction.setEnabled(True)
self._rlx_aboutaction.triggered.connect(self.on_about) self._rlx_aboutaction.triggered.connect(self.on_about)
self._daq_connectaction = QAction(QIcon(":icons/connect.png"), "Connect DAQ", self) self._daq_connectaction = QAction(
QIcon(":icons/connect.png"), "Connect DAQ", self
)
self._daq_connectaction.setStatusTip("Connect to daq device") self._daq_connectaction.setStatusTip("Connect to daq device")
# self._daq_connectaction.setShortcut(QKeySequence("Alt+d")) # self._daq_connectaction.setShortcut(QKeySequence("Alt+d"))
self._daq_connectaction.triggered.connect(self.connect_dac) self._daq_connectaction.triggered.connect(self.connect_dac)
self._daq_disconnectaction = QAction(QIcon(":/icons/disconnect.png"), "Disconnect DAQ", self) self._daq_disconnectaction = QAction(
QIcon(":/icons/disconnect.png"), "Disconnect DAQ", self
)
self._daq_disconnectaction.setStatusTip("Disconnect the DAQ device") self._daq_disconnectaction.setStatusTip("Disconnect the DAQ device")
# self._daq_connectaction.setShortcut(QKeySequence("Alt+d")) # self._daq_connectaction.setShortcut(QKeySequence("Alt+d"))
self._daq_disconnectaction.triggered.connect(self.disconnect_dac) self._daq_disconnectaction.triggered.connect(self.disconnect_dac)
self._daq_calibaction = QAction(QIcon(":/icons/calibration.png"), "Plot calibration", self) self._daq_calibaction = QAction(
QIcon(":/icons/calibration.png"), "Plot calibration", self
)
self._daq_calibaction.setStatusTip("Calibrate the attenuator device") self._daq_calibaction.setStatusTip("Calibrate the attenuator device")
# self._daq_calibaction.setShortcut(QKeySequence("Alt+d")) # self._daq_calibaction.setShortcut(QKeySequence("Alt+d"))
self._daq_calibaction.triggered.connect(self.plot_calibration) self._daq_calibaction.triggered.connect(self.calibration_plot.plot)
self.create_menu() self.create_menu()
def create_menu(self): def create_menu(self):
menu = self.menuBar() menu = self.menuBar()
file_menu = menu.addMenu("&File") if menu is not None:
file_menu.addAction(self._rlx_exitaction) file_menu = menu.addMenu("&File")
file_menu.addAction(self._rlx_aboutaction) device_menu = menu.addMenu("&DAQ")
help_menu = menu.addMenu("&Help")
device_menu = menu.addMenu("&DAQ") if file_menu is not None:
device_menu.addAction(self._daq_connectaction) file_menu.addAction(self._rlx_exitaction)
device_menu.addAction(self._daq_disconnectaction) file_menu.addAction(self._rlx_aboutaction)
device_menu.addSeparator()
device_menu.addAction(self._daq_calibaction) if device_menu is not None:
device_menu.addAction(self._daq_connectaction)
device_menu.addAction(self._daq_disconnectaction)
device_menu.addSeparator()
device_menu.addAction(self._daq_calibaction)
if help_menu is not None:
help_menu.addSeparator()
# help_menu.addAction(self._help_action)
else:
log.error("could not create file menu and device menu")
self.on_exit()
help_menu = menu.addMenu("&Help")
help_menu.addSeparator()
# help_menu.addAction(self._help_action)
self.setMenuBar(menu) self.setMenuBar(menu)
def create_toolbars(self): def create_toolbars(self):
@@ -145,82 +161,27 @@ class PyRelacs(QMainWindow):
self.plot_calibration_button = QPushButton("Plot Calibration") self.plot_calibration_button = QPushButton("Plot Calibration")
self.plot_calibration_button.setCheckable(True) self.plot_calibration_button.setCheckable(True)
self.plot_calibration_button.clicked.connect(self.plot_calibration) self.plot_calibration_button.clicked.connect(self.calibration_plot.plot)
def plot_calibration(self):
def decibel(power, ref_power=1.0, min_power=1e-20):
"""Transform power to decibel relative to ref_power.
\\[ decibel = 10 \\cdot \\log_{10}(power/ref\\_power) \\]
Power values smaller than `min_power` are set to `-np.inf`.
Parameters
----------
power: float or array
Power values, for example from a power spectrum or spectrogram.
ref_power: float or None or 'peak'
Reference power for computing decibel.
If set to `None` or 'peak', the maximum power is used.
min_power: float
Power values smaller than `min_power` are set to `-np.inf`.
Returns
-------
decibel_psd: array
Power values in decibel relative to `ref_power`.
"""
if np.isscalar(power):
tmp_power = np.array([power])
decibel_psd = np.array([power])
else:
tmp_power = power
decibel_psd = power.copy()
if ref_power is None or ref_power == "peak":
ref_power = np.max(decibel_psd)
decibel_psd[tmp_power <= min_power] = float("-inf")
decibel_psd[tmp_power > min_power] = 10.0 * np.log10(
decibel_psd[tmp_power > min_power] / ref_power
)
if np.isscalar(power):
return decibel_psd[0]
else:
return decibel_psd
block = self.nix_file.blocks[0]
colors = ["red", "green", "blue", "black", "yellow"]
for i, (stim, fish) in enumerate(
zip(list(block.data_arrays)[::2], list(block.data_arrays)[1::2])
):
beat = stim[:] + fish[:]
beat_squared = beat**2
f, powerspec = welch(beat, fs=40_000.0)
powerspec = decibel(powerspec)
f_sq, powerspec_sq = welch(beat_squared, fs=40_000.0)
powerspec_sq = decibel(powerspec_sq)
peaks = find_peaks(powerspec_sq, prominence=20)[0]
pen = pg.mkPen(colors[i])
self.beat_plot.plot(
np.arange(0, len(beat)) / 40_000.0,
beat_squared,
pen=pen,
# name=stim.name,
)
self.power_plot.plot(f_sq, powerspec_sq, pen=pen)
self.power_plot.plot(f[peaks], powerspec_sq[peaks], pen=None, symbol="x")
def connect_dac(self): def connect_dac(self):
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB) devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
try: try:
self.daq_device = uldaq.DaqDevice(devices[0]) self.daq_device = uldaq.DaqDevice(devices[0])
log.debug(f"Found daq devices {len(devices)}, connecting to the first one") log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
self.daq_device.connect()
log.debug("Connected")
except IndexError: except IndexError:
log.debug("DAQ is not connected, closing") log.error("DAQ is not connected")
self.on_exit() log.error("Please connect a DAQ device to the system")
self.daq_connect_button.setDisabled(True)
if hasattr(PyRelacs, "daq_device"):
try:
self.daq_device.connect()
log.debug("Connected")
except uldaq.ul_exception.ULException as e:
log.error(f"Could not Connect to DAQ: {e}")
self.daq_connect_button.setDisabled(True)
else:
log.debug("Already handeld the error")
pass
def disconnect_dac(self): def disconnect_dac(self):
try: try:
@@ -233,16 +194,6 @@ class PyRelacs(QMainWindow):
except AttributeError: except AttributeError:
log.debug("DAQ was not connected") log.debug("DAQ was not connected")
def repros_to_toolbar(self):
repro_names, file_names = self.repros.names_of_repros()
for rep, fn in zip(repro_names, file_names):
individual_repro_button = QAction(rep, self)
individual_repro_button.setStatusTip("Button")
individual_repro_button.triggered.connect(
lambda checked, n=rep, f=fn: self.run_repro(n, f)
)
self.toolbar.addAction(individual_repro_button)
def run_repro(self, n, fn): def run_repro(self, n, fn):
self.text.appendPlainText(f"started Repro {n}, {fn}") self.text.appendPlainText(f"started Repro {n}, {fn}")
worker = Worker(self.repros.run_repro, self.nix_file, n, fn) worker = Worker(self.repros.run_repro, self.nix_file, n, fn)
@@ -252,19 +203,25 @@ class PyRelacs(QMainWindow):
self.threadpool.start(worker) self.threadpool.start(worker)
def add_to_textfield(self, s: str):
self.text.appendPlainText(s)
def on_exit(self): def on_exit(self):
print("exit button!") log.info("exit button!")
self.add_to_textfield("exiting")
self.close() self.close()
def on_about(self, e): def on_about(self, e):
about = AboutDialog(self) about = AboutDialog(self)
about.show() about.show()
def print_output(self, s): def print_output(self, s):
print(s) log.info(s)
self.add_to_textfield(s)
def thread_complete(self): def thread_complete(self):
print("THREAD COMPLETE!") log.info("Thread complete!")
self.add_to_textfield("Thread complete!")
def progress_fn(self, n): def progress_fn(self, n):
print("%d%% done" % n) print("%d%% done" % n)

View File

@@ -0,0 +1,113 @@
from IPython import embed
import pyqtgraph as pg
import numpy as np
from scipy.signal import welch, find_peaks
from scipy.integrate import romb
class CalibrationPlot:
def __init__(self, figure: pg.GraphicsLayoutWidget, nix_file):
self.figure = figure
self.nix_file = nix_file
def plot(self):
self.figure.setBackground("w")
self.beat_plot = self.figure.addPlot(row=0, col=0)
self.power_plot = self.figure.addPlot(row=1, col=0)
self.beat_plot.addLegend()
self.power_plot.addLegend()
# self.power_plot.setLogMode(x=False, y=True)
block = self.nix_file.blocks[0]
colors = ["red", "green", "blue", "black", "yellow"]
for i, (stim, fish) in enumerate(
zip(list(block.data_arrays)[::2], list(block.data_arrays)[1::2])
):
f_stim, stim_power = welch(
stim[:],
fs=40_000.0,
window="flattop",
nperseg=100_000,
)
stim_power = self.decibel(stim_power)
stim_max_power_index = np.argmax(stim_power)
freq_stim = f_stim[stim_max_power_index]
f_fish, fish_power = welch(
fish[:],
fs=40_000.0,
window="flattop",
nperseg=100_000,
)
fish_power = self.decibel(fish_power)
fish_max_power_index = np.argmax(fish_power)
freq_fish = f_fish[fish_max_power_index]
beat_frequency = np.abs(freq_fish - freq_stim)
beat = stim[:] + fish[:]
beat_squared = beat**2
f, powerspec = welch(
beat_squared,
window="flattop",
fs=40_000.0,
nperseg=100_000,
)
powerspec = self.decibel(powerspec)
padding = 20
integration_window = powerspec[
(f > beat_frequency - padding) & (f < beat_frequency + padding)
]
peaks = find_peaks(powerspec, prominence=40)[0]
pen = pg.mkPen(colors[i])
self.beat_plot.plot(
np.arange(0, len(beat)) / 40_000.0,
beat,
pen=pen,
name=stim.name,
)
self.power_plot.plot(f, powerspec, pen=pen, name=stim.name)
self.power_plot.plot(f[peaks], powerspec[peaks], pen=None, symbol="x")
def decibel(self, power, ref_power=1.0, min_power=1e-20):
"""Transform power to decibel relative to ref_power.
\\[ decibel = 10 \\cdot \\log_{10}(power/ref\\_power) \\]
Power values smaller than `min_power` are set to `-np.inf`.
Parameters
----------
power: float or array
Power values, for example from a power spectrum or spectrogram.
ref_power: float or None or 'peak'
Reference power for computing decibel.
If set to `None` or 'peak', the maximum power is used.
min_power: float
Power values smaller than `min_power` are set to `-np.inf`.
Returns
-------
decibel_psd: array
Power values in decibel relative to `ref_power`.
"""
if np.isscalar(power):
tmp_power = np.array([power])
decibel_psd = np.array([power])
else:
tmp_power = power
decibel_psd = power.copy()
if ref_power is None or ref_power == "peak":
ref_power = np.max(decibel_psd)
decibel_psd[tmp_power <= min_power] = float("-inf")
decibel_psd[tmp_power > min_power] = 10.0 * np.log10(
decibel_psd[tmp_power > min_power] / ref_power
)
if np.isscalar(power):
return decibel_psd[0]
else:
return decibel_psd