Compare commits

...

3 Commits

3 changed files with 109 additions and 29 deletions

View File

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

View File

@ -1,5 +1,7 @@
import time
from pathlib import Path as path
from datetime import datetime
from dataclasses import asdict
from PyQt6.QtGui import QAction, QIcon, QKeySequence
from PyQt6.QtCore import Qt, QSize, QThreadPool, QMutex
@ -13,9 +15,8 @@ from PyQt6.QtWidgets import (
QStatusBar,
)
from pyqtgraph.Qt.QtCore import QThread
import uldaq
import nixio as nix
import nixio
import pyqtgraph as pg
import quantities as pq
@ -110,6 +111,7 @@ class PyRelacs(QMainWindow):
self.sinus_producer = SinProducer(self.buffer)
self.nix_writer = NixWriter(self.buffer)
self.create_nix_file(f"{_root}/test.nix", self.config.metadata)
def create_actions(self):
self._rlx_exitaction = QAction(QIcon(":/icons/exit.png"), "Exit", self)
@ -222,6 +224,22 @@ class PyRelacs(QMainWindow):
repro_toolbar.addAction(repro_action)
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):
self.continously_plot.refresh()
@ -251,12 +269,33 @@ class PyRelacs(QMainWindow):
self.continously_plot.plot()
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.finished.connect(self.thread_complete)
nix_writer.signals.progress.connect(self.progress_fn)
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):
self.add_to_textfield("Stopping the recording")
self.continously_plot.stop_plotting()
@ -272,6 +311,8 @@ class PyRelacs(QMainWindow):
if hasattr(PyRelacs, "daq_device"):
log.debug("Stopping DAQ")
self.daq_producer.stop_aquisition()
embed()
exit()
def run_repro(self, 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):
self.figure = figure
self.buffer = buffer
self.last_plotted_index = 0
def plot(self, *args, **kwargs):
self.figure.setBackground("w")
@ -38,7 +39,6 @@ class Continously:
self.timer = QTimer()
self.CHUNK_PLOT = int(self.buffer.samplerate / 6)
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.timeout.connect(self.update_plot)
self.timer.start()
@ -49,18 +49,20 @@ class Continously:
start_time = time.time()
if total_count - self.last_plotted_index >= self.CHUNK_PLOT:
times, items = self.buffer.read(
self.last_plotted_index,
extend=self.CHUNK_PLOT,
)
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 += self.CHUNK_PLOT
try:
times, items = self.buffer.read(
self.last_plotted_index,
extend=self.CHUNK_PLOT,
)
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 += self.CHUNK_PLOT
except IndexError:
log.error("Could not acces the data from the buffer for plotting")
end_time = time.time()
log.debug(f"total time for plotting {end_time - start_time}")
else:
@ -68,6 +70,18 @@ class Continously:
def stop_plotting(self):
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):
self.continous_ax.enableAutoRange()