14 Commits

Author SHA1 Message Date
wendtalexander
d127750e7b [buffer] removing logging, adding samplerate, adding check for getting the item at current index 2024-10-03 08:52:25 +02:00
wendtalexander
b18f870a6b [buffer] adding True for fixing test 2024-10-02 15:55:00 +02:00
wendtalexander
0b067df69c [project] adding pytest 2024-10-02 15:52:41 +02:00
wendtalexander
e3ed2fcc75 [test] adding testcases for get method of buffer 2024-10-02 15:52:29 +02:00
wendtalexander
ff84d63fe1 [buffer] adding edge cases 2024-10-02 15:51:57 +02:00
19c6b90d5c [test] add buffer test stub 2024-10-02 10:28:50 +02:00
a7b62c5b3a [ringbuffer] first implementation 2024-10-02 10:28:36 +02:00
wendtalexander
54f0d61fc9 [dataio] commenting out databuffer 2024-10-02 08:10:08 +02:00
wendtalexander
815838eab7 [dataio] adding class for buffer 2024-10-02 08:09:45 +02:00
wendtalexander
314e609472 [refactoring] removing imports 2024-10-01 18:16:33 +02:00
wendtalexander
2719d49eb0 [refacorting] adding DataBuffer 2024-10-01 12:04:47 +02:00
wendtalexander
a0c524326d [refactroring] renaming class 2024-10-01 12:04:06 +02:00
wendtalexander
c2285f3750 [project] adding buffer class 2024-10-01 12:03:41 +02:00
wendtalexander
9327d1bac9 [refactoring] renaming file 2024-10-01 12:03:25 +02:00
8 changed files with 422 additions and 14 deletions

81
poetry.lock generated
View File

@@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. # This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand.
[[package]] [[package]]
name = "asttokens" name = "asttokens"
@@ -153,6 +153,20 @@ files = [
{file = "decorator-5.1.1.tar.gz", hash = "sha256:637996211036b6385ef91435e4fae22989472f9d571faba8927ba8253acbc330"}, {file = "decorator-5.1.1.tar.gz", hash = "sha256:637996211036b6385ef91435e4fae22989472f9d571faba8927ba8253acbc330"},
] ]
[[package]]
name = "exceptiongroup"
version = "1.2.2"
description = "Backport of PEP 654 (exception groups)"
optional = false
python-versions = ">=3.7"
files = [
{file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"},
{file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"},
]
[package.extras]
test = ["pytest (>=6)"]
[[package]] [[package]]
name = "executing" name = "executing"
version = "2.1.0" version = "2.1.0"
@@ -270,6 +284,17 @@ files = [
[package.dependencies] [package.dependencies]
numpy = ">=1.19.3" numpy = ">=1.19.3"
[[package]]
name = "iniconfig"
version = "2.0.0"
description = "brain-dead simple config-ini parsing"
optional = false
python-versions = ">=3.7"
files = [
{file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"},
{file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"},
]
[[package]] [[package]]
name = "ipython" name = "ipython"
version = "8.27.0" version = "8.27.0"
@@ -284,6 +309,7 @@ files = [
[package.dependencies] [package.dependencies]
colorama = {version = "*", markers = "sys_platform == \"win32\""} colorama = {version = "*", markers = "sys_platform == \"win32\""}
decorator = "*" decorator = "*"
exceptiongroup = {version = "*", markers = "python_version < \"3.11\""}
jedi = ">=0.16" jedi = ">=0.16"
matplotlib-inline = "*" matplotlib-inline = "*"
pexpect = {version = ">4.3", markers = "sys_platform != \"win32\" and sys_platform != \"emscripten\""} pexpect = {version = ">4.3", markers = "sys_platform != \"win32\" and sys_platform != \"emscripten\""}
@@ -291,6 +317,7 @@ prompt-toolkit = ">=3.0.41,<3.1.0"
pygments = ">=2.4.0" pygments = ">=2.4.0"
stack-data = "*" stack-data = "*"
traitlets = ">=5.13.0" traitlets = ">=5.13.0"
typing-extensions = {version = ">=4.6", markers = "python_version < \"3.12\""}
[package.extras] [package.extras]
all = ["ipython[black,doc,kernel,matplotlib,nbconvert,nbformat,notebook,parallel,qtconsole]", "ipython[test,test-extra]"] all = ["ipython[black,doc,kernel,matplotlib,nbconvert,nbformat,notebook,parallel,qtconsole]", "ipython[test,test-extra]"]
@@ -758,6 +785,21 @@ tests = ["check-manifest", "coverage", "defusedxml", "markdown2", "olefile", "pa
typing = ["typing-extensions"] typing = ["typing-extensions"]
xmp = ["defusedxml"] xmp = ["defusedxml"]
[[package]]
name = "pluggy"
version = "1.5.0"
description = "plugin and hook calling mechanisms for python"
optional = false
python-versions = ">=3.8"
files = [
{file = "pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669"},
{file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"},
]
[package.extras]
dev = ["pre-commit", "tox"]
testing = ["pytest", "pytest-benchmark"]
[[package]] [[package]]
name = "prompt-toolkit" name = "prompt-toolkit"
version = "3.0.48" version = "3.0.48"
@@ -903,6 +945,28 @@ files = [
[package.dependencies] [package.dependencies]
numpy = ">=1.22.0" numpy = ">=1.22.0"
[[package]]
name = "pytest"
version = "8.3.3"
description = "pytest: simple powerful testing with Python"
optional = false
python-versions = ">=3.8"
files = [
{file = "pytest-8.3.3-py3-none-any.whl", hash = "sha256:a6853c7375b2663155079443d2e45de913a911a11d669df02a50814944db57b2"},
{file = "pytest-8.3.3.tar.gz", hash = "sha256:70b98107bd648308a7952b06e6ca9a50bc660be218d53c257cc1fc94fda10181"},
]
[package.dependencies]
colorama = {version = "*", markers = "sys_platform == \"win32\""}
exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""}
iniconfig = "*"
packaging = "*"
pluggy = ">=1.5,<2"
tomli = {version = ">=1", markers = "python_version < \"3.11\""}
[package.extras]
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
[[package]] [[package]]
name = "python-dateutil" name = "python-dateutil"
version = "2.9.0.post0" version = "2.9.0.post0"
@@ -1026,6 +1090,17 @@ pure-eval = "*"
[package.extras] [package.extras]
tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"] tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"]
[[package]]
name = "tomli"
version = "2.0.1"
description = "A lil' TOML parser"
optional = false
python-versions = ">=3.7"
files = [
{file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"},
{file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"},
]
[[package]] [[package]]
name = "tomlkit" name = "tomlkit"
version = "0.13.2" version = "0.13.2"
@@ -1104,5 +1179,5 @@ files = [
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.12" python-versions = "^3.10"
content-hash = "b1076b7f750e8f7e66542918ec746e74544fbfdb376875158f083fb573d18107" content-hash = "c661a83b9d93748d39d3e4577f20e3b534a35f270231b6f48a9f13498a3658bd"

View File

@@ -31,6 +31,7 @@ tomlkit = "^0.13.2"
scipy = "^1.14.1" scipy = "^1.14.1"
nixio = "^1.5.3" nixio = "^1.5.3"
pyqtgraph = "^0.13.7" pyqtgraph = "^0.13.7"
pytest = "^8.3.3"
[tool.poetry.scripts] [tool.poetry.scripts]
pyrelacs = "pyrelacs.app:main" pyrelacs = "pyrelacs.app:main"

81
pyrelacs/dataio/buffer.py Normal file
View File

@@ -0,0 +1,81 @@
import time
import faulthandler
from collections import deque
from pyqtgraph import transformToArray
import uldaq
import numpy as np
from IPython import embed
import matplotlib.pyplot as plt
from pyrelacs.util.logging import config_logging
log = config_logging()
faulthandler.enable()
class DataBuffer:
def __init__(self, channels, samples):
self.channels = channels
self.samples = samples
def read_analog_continously(
self,
device: uldaq.DaqDevice,
samplerate: float = 40_000.0,
):
data_array = []
max_len_buffer = self.channels * self.samples
self.buffer = deque(maxlen=max_len_buffer)
samples_per_channel = 40_000
self.device = device
self.ai_device = self.device.get_ai_device()
data_analog_input = uldaq.create_float_buffer(
self.channels, samples_per_channel
)
er = self.ai_device.a_in_scan(
0,
1,
uldaq.AiInputMode.SINGLE_ENDED,
uldaq.Range.BIP10VOLTS,
samples_per_channel,
samplerate,
uldaq.ScanOption.CONTINUOUS,
uldaq.AInScanFlag.DEFAULT,
data=data_analog_input,
)
daq_status = uldaq.ScanStatus.IDLE
while daq_status == uldaq.ScanStatus.IDLE:
daq_status = self.ai_device.get_scan_status()[0]
prev_count = 0
prev_index = 0
while daq_status != uldaq.ScanStatus.IDLE:
daq_status, transfer_status = self.ai_device.get_scan_status()
# The index into the data buffer immediately following the last sample transferred.
curren_index = transfer_status.current_index
# total samples since start of the scan
total_samples = transfer_status.current_total_count
# The number of samples per channel transferred since the scan started
channel_samples = transfer_status.current_scan_count
self.ai_device.scan_stop()
if __name__ == "__main__":
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
try:
daq_device = uldaq.DaqDevice(devices[0])
except uldaq.ul_exception.ULException as e:
log.error("Did not found daq devices, please connect one")
raise e
daq_device.connect()
buf = DataBuffer(channels=2, samples=100_000)
buf.read_analog_continously(daq_device)

View File

@@ -0,0 +1,155 @@
from typing import Tuple
import numpy as np
from IPython import embed
class CircBuffer:
def __init__(self, size: int, channels: int = 1, samplerate: float = 40_000):
self.__size = size
self.__channels = channels
self.__samplereate = samplerate
self.__buffer = np.zeros(
(channels, size), dtype=np.double
) # or dtype of your choice
self.__index = [0 for i in range(channels)]
self.__is_full = [False for i in range(channels)]
self.__totalcount = [0 for i in range(channels)]
self.__overflows = [0 for i in range(channels)]
@property
def size(self):
return self.__size
@property
def channel_count(self):
return self.__channels
def totocount(self, channel: int = 0):
return self.__totalcount[channel]
def is_full(self, channel: int = 0):
return self.__is_full[channel]
def write_index(self, channel: int = 0):
return self.__index[channel]
def append(self, item, channel: int = 0):
self.__buffer[channel, self.write_index(channel)] = item
self.__index[channel] = (self.write_index(channel) + 1) % self.__size
self.__totalcount[channel] += 1
if self.__index[channel] == 0:
self.__is_full[channel] = True
self.__overflows[channel] += 1
def get_all(self, channel: int = 0):
"""
Return all valid values from the specified channel
"""
if self.__is_full[channel]:
return np.concatenate(
(
self.__buffer[channel, self.__index[channel] :],
self.__buffer[channel, : self.__index[channel]],
)
)
else:
return self.__buffer[channel, : self.__index[channel]]
def has_value(self, index, channel):
if index <= 0 and self.is_full(channel):
return True
elif index < 0 and not self.is_full(channel):
return False
if index >= self.size:
return False
# test if the ring buffer is at the start but
# and the index is greater than the write index
if index > self.write_index(channel) and self.is_full(channel):
return True
elif index >= self.write_index(channel) and not self.is_full(channel):
raise IndexError("Index has no value, not written")
if index == self.write_index(channel) and self.__totalcount[channel] == 0:
return False
return True
def valid_range(self, channel: int = 0) -> Tuple[int, int]:
"""
Return the start index and the extend that are valid within the buffer
Parameters
----------
channel : int
channel of the buffer
Returns
-------
Tuple[int, int]
start, extend of the valid range
"""
start = 0
extend = 0
if self.__totalcount[channel] == 0:
return start, extend
if not self.is_full(channel):
extend = self.__totalcount[channel]
else:
extend = self.size
return start, extend
def get(self, index: int = -1, channel: int = 0):
# easy case first, we can spare the effort of further checking
if index >= 0 and index <= self.write_index(channel):
if self.has_value(index, channel):
return self.__buffer[channel, index]
else:
raise IndexError(
f"Invalid index {index} on ring buffer for channel{channel}"
)
if index < 0:
index = self.write_index() - 1
if self.has_value(index, channel):
return self.__buffer[channel, index]
else:
raise IndexError(
f"Invalid index {index} on ring buffer for channel{channel}"
)
def read(self, start, count=1, channel=0):
"""Reads a numpy array from buffer"""
if start < 0 or count < 0:
raise IndexError(
f"Invalid start ({start}) or count ({count}) for channel{channel}"
)
if count == 1:
return np.array(self.get(start, channel))
vs, vc = self.valid_range(channel)
if start > self.__totalcount[channel]:
raise IndexError(
f"Invalid start index {start} is invalid with totalcount {self.__totalcount[channel]} for channel{channel}"
)
if start > self.size:
raise IndexError(
f"Invalid start index {start} for buffer with size {self.size}"
)
if count > self.size:
count = self.size
if count > vc:
count = vc
if (start + count) < self.size:
return self.__buffer[channel, start : start + count]
else:
return np.concatenate(
(
self.__buffer[channel, start:],
self.__buffer[channel, : count - self.size + start],
)
)

View File

@@ -11,7 +11,7 @@ from pyrelacs.util.logging import config_logging
log = config_logging() log = config_logging()
class MccDac: class MccDaq:
""" """
Represents the Digital/Analog Converter from Meassuring Computing. Represents the Digital/Analog Converter from Meassuring Computing.
provides methods for writing and reading the Analog / Digital input and output. provides methods for writing and reading the Analog / Digital input and output.
@@ -43,6 +43,7 @@ class MccDac:
except uldaq.ul_exception.ULException: except uldaq.ul_exception.ULException:
self.disconnect_dac() self.disconnect_dac()
self.connect_dac() self.connect_dac()
self.ai_device = self.daq_device.get_ai_device() self.ai_device = self.daq_device.get_ai_device()
self.ao_device = self.daq_device.get_ao_device() self.ao_device = self.daq_device.get_ao_device()
self.dio_device = self.daq_device.get_dio_device() self.dio_device = self.daq_device.get_dio_device()

View File

@@ -6,10 +6,8 @@ import uldaq
from IPython import embed from IPython import embed
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from scipy.signal import welch
from scipy.signal import find_peaks
from pyrelacs.devices.mccdac import MccDac from pyrelacs.devices.mccdaq import MccDaq
from pyrelacs.util.logging import config_logging from pyrelacs.util.logging import config_logging
log = config_logging() log = config_logging()
@@ -17,7 +15,7 @@ log = config_logging()
faulthandler.enable() faulthandler.enable()
class Calibration(MccDac): class Calibration(MccDaq):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
self.SAMPLERATE = 40_000.0 self.SAMPLERATE = 40_000.0

View File

@@ -11,19 +11,20 @@ from PyQt6.QtWidgets import (
QStatusBar, QStatusBar,
) )
import uldaq import uldaq
import numpy as np
import nixio as nix import nixio as nix
import pyqtgraph as pg import pyqtgraph as pg
from pathlib import Path as path from pathlib import Path as path
from scipy.signal import welch, find_peaks
from pyrelacs.worker import Worker from pyrelacs.worker import Worker
from pyrelacs.repros.repros import Repro from pyrelacs.repros.repros import Repro
from pyrelacs.util.logging import config_logging from pyrelacs.dataio.buffer import DataBuffer
from pyrelacs.ui.about import AboutDialog from pyrelacs.ui.about import AboutDialog
from pyrelacs.ui.plots.calibration import CalibrationPlot from pyrelacs.ui.plots.calibration import CalibrationPlot
from pyrelacs.util.logging import config_logging
log = config_logging() log = config_logging()
_root = path(__file__).parent.parent _root = path(__file__).parent.parent
@@ -51,6 +52,7 @@ class PyRelacs(QMainWindow):
self.threadpool = QThreadPool() self.threadpool = QThreadPool()
self.repros = Repro() self.repros = Repro()
# self.buffers = DataBuffer(channels=1, samples=100_000)
self.text = QPlainTextEdit() self.text = QPlainTextEdit()
self.text.setReadOnly(True) self.text.setReadOnly(True)
@@ -179,9 +181,6 @@ class PyRelacs(QMainWindow):
except uldaq.ul_exception.ULException as e: except uldaq.ul_exception.ULException as e:
log.error(f"Could not Connect to DAQ: {e}") log.error(f"Could not Connect to DAQ: {e}")
self.daq_connect_button.setDisabled(True) self.daq_connect_button.setDisabled(True)
else:
log.debug("Already handeld the error")
pass
def disconnect_dac(self): def disconnect_dac(self):
try: try:

98
test/test_buffer.py Normal file
View File

@@ -0,0 +1,98 @@
import pytest
import numpy as np
from IPython import embed
from pyrelacs.dataio.circbuffer import CircBuffer
def test_init():
buff = CircBuffer(1000, 2)
assert buff.size == 1000
assert buff.channel_count == 2
def test_hasvalue():
buff = CircBuffer(1000, 2)
assert buff.has_value(0, 0) == False
assert buff.has_value(-1, 0) == False
buff.append(10, 0)
assert buff.write_index(0) == 1
assert buff.write_index(1) == 0
assert buff.has_value(0, 0) == True
assert buff.has_value(0, 1) == False
buff.append(10, 1)
assert buff.write_index(1) == 1
assert buff.has_value(0, 1) == True
for i in range(1100):
buff.append(i, 0)
buff.append(i, 1)
assert buff.write_index(0) == buff.write_index(1)
assert buff.has_value(0, 0) == True
assert buff.has_value(0, 1) == True
assert buff.has_value(buff.write_index(0), 0) == True
assert buff.has_value(buff.write_index(1), 1) == True
def test_validrange():
buff = CircBuffer(1000, 2)
# without any values the range is (0, 0)
assert buff.valid_range() == (0, 0)
buff.append(0, 0)
assert buff.valid_range() == (0, 1)
for i in range(100):
buff.append(i, 0)
assert buff.valid_range() == (0, 101)
for i in range(1000):
buff.append(i, 0)
assert buff.valid_range() == (0, 1000)
def test_get():
buff = CircBuffer(1000, 2)
# with no items written to the buffer
with pytest.raises(IndexError):
item = buff.get(index=-1)
buff.append(10, 0)
item = buff.get(index=-1)
assert item == 10
# Check if index is not written jet
with pytest.raises(IndexError):
item = buff.get(index=10)
for i in range(1000):
buff.append(i, 0)
item = buff.get(index=-1)
# the first item should be 999.0 because of we append a value in the earlier test
assert item == 999.0
with pytest.raises(IndexError):
item = buff.get(10001)
def test_read():
pass
def test_write():
buff = CircBuffer(1000, 2)
samplecount = 1000
if __name__ == "__main__":
test_get()