Compare commits

...

3 Commits

3 changed files with 109 additions and 29 deletions

View File

@ -1,5 +1,6 @@
import time import time
from IPython import embed from IPython import embed
from PyQt6.QtCore import QMutex
import nixio import nixio
from pyrelacs.dataio.circbuffer import CircBuffer from pyrelacs.dataio.circbuffer import CircBuffer
@ -12,29 +13,53 @@ class NixWriter:
def __init__(self, buffer: CircBuffer) -> None: def __init__(self, buffer: CircBuffer) -> None:
self.buffer = buffer self.buffer = buffer
def write_nix(self, *args, **kwargs): def write_nix(
self._write_header() self,
items = 0 data_array: nixio.DataArray,
chunk = 1000 mutex: QMutex,
channel: int = 0,
chunk_size=1000,
*args,
**kwargs,
):
index = 0
log.debug("Starting the writing") log.debug("Starting the writing")
self.write = True self.write = True
while self.write: while self.write:
total_count = self.buffer.totalcount() total_count = self.buffer.totalcount(channel=channel)
if total_count - items >= chunk: if total_count - index >= chunk_size:
# log.debug(items) mutex.lock()
log.debug(index)
try: try:
data, _ = self.buffer.read(items, extend=chunk) _, data = self.buffer.read(
self.data_array.append(data) index, extend=chunk_size, channel=channel
)
if index == 0:
data_array.write_direct(data)
else:
data_array.append(data)
index += chunk_size
except IndexError as e: except IndexError as e:
time.sleep(0.001) time.sleep(0.001)
log.debug(f"{e}") log.debug(f"{e}")
items += chunk mutex.unlock()
else: else:
time.sleep(0.001) time.sleep(0.001)
continue continue
total_count = self.buffer.totalcount(channel=channel)
try:
mutex.lock()
_, data = self.buffer.read(
index, extend=total_count - index, channel=channel
)
data_array.append(data)
mutex.unlock()
index += total_count - index
except IndexError as e:
log.error(f"Could not read the last samples, {e}")
log.debug("Stoppint the writing") log.debug("Stoppint the writing")
log.debug(f"Samples written {items}") log.debug(f"Samples written {index}")
self.nix_file.close()
def _write_header(self): def _write_header(self):
self.nix_file = nixio.File.open(path="data.nix", mode=nixio.FileMode.Overwrite) self.nix_file = nixio.File.open(path="data.nix", mode=nixio.FileMode.Overwrite)

View File

@ -1,5 +1,7 @@
import time import time
from pathlib import Path as path from pathlib import Path as path
from datetime import datetime
from dataclasses import asdict
from PyQt6.QtGui import QAction, QIcon, QKeySequence from PyQt6.QtGui import QAction, QIcon, QKeySequence
from PyQt6.QtCore import Qt, QSize, QThreadPool, QMutex from PyQt6.QtCore import Qt, QSize, QThreadPool, QMutex
@ -13,9 +15,8 @@ from PyQt6.QtWidgets import (
QStatusBar, QStatusBar,
) )
from pyqtgraph.Qt.QtCore import QThread
import uldaq import nixio
import nixio as nix
import pyqtgraph as pg import pyqtgraph as pg
import quantities as pq import quantities as pq
@ -110,6 +111,7 @@ class PyRelacs(QMainWindow):
self.sinus_producer = SinProducer(self.buffer) self.sinus_producer = SinProducer(self.buffer)
self.nix_writer = NixWriter(self.buffer) self.nix_writer = NixWriter(self.buffer)
self.create_nix_file(f"{_root}/test.nix", self.config.metadata)
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)
@ -222,6 +224,22 @@ class PyRelacs(QMainWindow):
repro_toolbar.addAction(repro_action) repro_toolbar.addAction(repro_action)
self.addToolBar(Qt.ToolBarArea.BottomToolBarArea, repro_toolbar) self.addToolBar(Qt.ToolBarArea.BottomToolBarArea, repro_toolbar)
def create_nix_file(self, file_path, metadata):
self.nix_file = nixio.File.open(
path=f"{file_path}", mode=nixio.FileMode.Overwrite
)
self.block = self.nix_file.create_block("recording", "testfile")
self.section = self.nix_file.create_section("metadata", "config.yaml")
for key, value in asdict(metadata).items():
self.section[key] = value
self.data_array_analog1 = self.block.create_data_array(
"Analog1", "ndarray", shape=(1000,), dtype=nixio.DataType.Double
)
self.data_array_analog2 = self.block.create_data_array(
"Analog2", "ndarray", shape=(1000,), dtype=nixio.DataType.Double
)
def recenter_continously_plot(self): def recenter_continously_plot(self):
self.continously_plot.refresh() self.continously_plot.refresh()
@ -251,12 +269,33 @@ class PyRelacs(QMainWindow):
self.continously_plot.plot() self.continously_plot.plot()
def record(self): def record(self):
nix_writer = Worker(self.nix_writer.write_nix) self.create_nix_file("test.nix", self.config.metadata)
log.debug("Created nix file")
nix_writer = Worker(
self.nix_writer.write_nix,
data_array=self.data_array_analog1,
mutex=self.mutex,
channel=0,
chunk_size=1000,
)
nix_writer.signals.result.connect(self.print_output) nix_writer.signals.result.connect(self.print_output)
nix_writer.signals.finished.connect(self.thread_complete) nix_writer.signals.finished.connect(self.thread_complete)
nix_writer.signals.progress.connect(self.progress_fn) nix_writer.signals.progress.connect(self.progress_fn)
self.threadpool.start(nix_writer) self.threadpool.start(nix_writer)
nix_writer2 = Worker(
self.nix_writer.write_nix,
data_array=self.data_array_analog2,
mutex=self.mutex,
channel=0,
chunk_size=1000,
)
nix_writer2.signals.result.connect(self.print_output)
nix_writer2.signals.finished.connect(self.thread_complete)
nix_writer2.signals.progress.connect(self.progress_fn)
self.threadpool.start(nix_writer2)
def stop_recording(self): def stop_recording(self):
self.add_to_textfield("Stopping the recording") self.add_to_textfield("Stopping the recording")
self.continously_plot.stop_plotting() self.continously_plot.stop_plotting()
@ -272,6 +311,8 @@ class PyRelacs(QMainWindow):
if hasattr(PyRelacs, "daq_device"): if hasattr(PyRelacs, "daq_device"):
log.debug("Stopping DAQ") log.debug("Stopping DAQ")
self.daq_producer.stop_aquisition() self.daq_producer.stop_aquisition()
embed()
exit()
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}")

View File

@ -15,6 +15,7 @@ class Continously:
def __init__(self, figure: pg.GraphicsLayoutWidget, buffer: CircBuffer): def __init__(self, figure: pg.GraphicsLayoutWidget, buffer: CircBuffer):
self.figure = figure self.figure = figure
self.buffer = buffer self.buffer = buffer
self.last_plotted_index = 0
def plot(self, *args, **kwargs): def plot(self, *args, **kwargs):
self.figure.setBackground("w") self.figure.setBackground("w")
@ -38,7 +39,6 @@ class Continously:
self.timer = QTimer() self.timer = QTimer()
self.CHUNK_PLOT = int(self.buffer.samplerate / 6) self.CHUNK_PLOT = int(self.buffer.samplerate / 6)
self.PLOT_HISTORY = 500_000 # The amount of data you want to keep on screen self.PLOT_HISTORY = 500_000 # The amount of data you want to keep on screen
self.last_plotted_index = 0
self.timer.setInterval(150) self.timer.setInterval(150)
self.timer.timeout.connect(self.update_plot) self.timer.timeout.connect(self.update_plot)
self.timer.start() self.timer.start()
@ -49,18 +49,20 @@ class Continously:
start_time = time.time() start_time = time.time()
if total_count - self.last_plotted_index >= self.CHUNK_PLOT: if total_count - self.last_plotted_index >= self.CHUNK_PLOT:
times, items = self.buffer.read( try:
self.last_plotted_index, times, items = self.buffer.read(
extend=self.CHUNK_PLOT, self.last_plotted_index,
) extend=self.CHUNK_PLOT,
)
self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :] self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :]
self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :] self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :]
self.line.setData( self.line.setData(
self.time, self.time,
self.data, self.data,
) )
self.last_plotted_index += self.CHUNK_PLOT self.last_plotted_index += self.CHUNK_PLOT
except IndexError:
log.error("Could not acces the data from the buffer for plotting")
end_time = time.time() end_time = time.time()
log.debug(f"total time for plotting {end_time - start_time}") log.debug(f"total time for plotting {end_time - start_time}")
else: else:
@ -68,6 +70,18 @@ class Continously:
def stop_plotting(self): def stop_plotting(self):
self.timer.stop() self.timer.stop()
total_count = self.buffer.totalcount()
times, items = self.buffer.read(
self.last_plotted_index,
extend=total_count - self.last_plotted_index,
)
self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :]
self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :]
self.line.setData(
self.time,
self.data,
)
self.last_plotted_index += total_count - self.last_plotted_index
def refresh(self): def refresh(self):
self.continous_ax.enableAutoRange() self.continous_ax.enableAutoRange()