Compare commits
191 Commits
841c3efb0a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f6c9b1e5e | ||
|
|
574e9a8110 | ||
|
|
75619cf1c8 | ||
|
|
02911e57f8 | ||
|
|
557535ffa4 | ||
|
|
110629dae0 | ||
|
|
7c4b5098c1 | ||
|
|
e2b7ed3a61 | ||
|
|
038327bfeb | ||
|
|
4f7ebbe8c3 | ||
|
|
12e82dceee | ||
|
|
e4e86cbc49 | ||
|
|
e36db5e7b0 | ||
|
|
33f046c072 | ||
|
|
6d2eb09c65 | ||
|
|
4029034174 | ||
|
|
9fd4892325 | ||
|
|
85b5a71ccb | ||
|
|
5d62cb0384 | ||
|
|
64cd1b00ad | ||
|
|
f703687ed7 | ||
|
|
4e0a1f0ac5 | ||
|
|
4864538213 | ||
|
|
28dd0b7080 | ||
|
|
b94078634d | ||
|
|
e3c867f4fd | ||
|
|
13897f29e3 | ||
|
|
afc37adb1a | ||
|
|
b87e7f1ffa | ||
|
|
f576f33cc5 | ||
|
|
e16211b988 | ||
|
|
b22ef04317 | ||
|
|
d2f7d0e966 | ||
|
|
cc9089f503 | ||
|
|
351850e05c | ||
|
|
e44e021982 | ||
|
|
93fe2951cd | ||
|
|
452c3dcecb | ||
|
|
e999d25951 | ||
|
|
30bd705e1a | ||
|
|
38123cdff3 | ||
|
|
0e0aaf9f04 | ||
|
|
1332e067c6 | ||
|
|
f7cfc96dde | ||
|
|
71837dffc9 | ||
|
|
1b889312ce | ||
|
|
042826721b | ||
|
|
7510d04f8d | ||
|
|
0f09c948ec | ||
|
|
ff2d8ebb50 | ||
|
|
57d3a83243 | ||
|
|
2539fa7e25 | ||
|
|
b5f8d7663d | ||
|
|
fe3e142452 | ||
|
|
bbc8def460 | ||
|
|
25d16cc5fd | ||
|
|
523f5dc346 | ||
|
|
b6bd9e23a0 | ||
|
|
e100dac5ea | ||
|
|
22b899e723 | ||
|
|
3ca48d11fe | ||
|
|
32c79ff47b | ||
|
|
e3ed301252 | ||
|
|
0b36e31135 | ||
|
|
b7d92259a9 | ||
|
|
8378add874 | ||
|
|
aaa42db2ae | ||
|
|
52a0821601 | ||
|
|
5ec5cc8644 | ||
|
|
bf72f90009 | ||
|
|
949767c45e | ||
|
|
b251ee13c4 | ||
|
|
cd936b1ed1 | ||
|
|
85fd70f8ca | ||
|
|
8910305262 | ||
|
|
e43bb16bd4 | ||
|
|
e555573f09 | ||
|
|
4d53a0f51d | ||
|
|
d127750e7b | ||
|
|
b18f870a6b | ||
|
|
0b067df69c | ||
|
|
e3ed2fcc75 | ||
|
|
ff84d63fe1 | ||
| 19c6b90d5c | |||
| a7b62c5b3a | |||
|
|
54f0d61fc9 | ||
|
|
815838eab7 | ||
|
|
314e609472 | ||
|
|
2719d49eb0 | ||
|
|
a0c524326d | ||
|
|
c2285f3750 | ||
|
|
9327d1bac9 | ||
|
|
565d6e5318 | ||
|
|
b35a9212ac | ||
|
|
56c8b59ccd | ||
|
|
cbc86598b0 | ||
|
|
031b5098d5 | ||
|
|
c33e4cc32f | ||
|
|
4868b0e196 | ||
|
|
00f6b11740 | ||
|
|
e0491c5917 | ||
|
|
9c80091d16 | ||
| b0897bf52d | |||
| 09dd7f3d51 | |||
|
|
2e264fb582 | ||
| 0a00875d2e | |||
|
|
ab51fa7475 | ||
|
|
6a3a610cd3 | ||
|
|
8b02b9083f | ||
|
|
5dadf1bd7c | ||
|
|
9e8dc06c26 | ||
|
|
bf8f3f5cb7 | ||
| 0378317d7b | |||
| b2f223168c | |||
| 58decf0283 | |||
| fe6e438189 | |||
| d241d88168 | |||
| a1b0e723f6 | |||
| a818bf75a4 | |||
|
|
13d4db25fa | ||
|
|
5c274c713d | ||
|
|
85c9637ce3 | ||
|
|
7cf9683744 | ||
|
|
2110286abb | ||
|
|
f04f28dd11 | ||
|
|
e9a509c0f7 | ||
|
|
bdd323ad20 | ||
|
|
cadf2e5dde | ||
|
|
a7b73fa09a | ||
|
|
3433ef7132 | ||
|
|
a16fe0b735 | ||
|
|
5c3c2c407a | ||
|
|
8ef8ac7506 | ||
|
|
7d3224f351 | ||
|
|
a748385335 | ||
|
|
cd6bc0dc04 | ||
|
|
cb5c08bc94 | ||
| 45a267c8c7 | |||
| 291ed8859c | |||
| aa792bcb1d | |||
| 6a73f38fba | |||
| 6008cc03d6 | |||
| 938d70fbac | |||
|
|
dd3e0d045d | ||
| 8d616847f5 | |||
| 836d6dc3d9 | |||
|
|
43e0d4b75a | ||
|
|
66ea22fb4a | ||
|
|
d3800ddfa2 | ||
|
|
1dc72d00bb | ||
|
|
26f43151a2 | ||
|
|
a9be09dc06 | ||
|
|
06f5a6ae46 | ||
|
|
b912159b76 | ||
|
|
9cd6aadb3b | ||
|
|
1a2185d5e4 | ||
|
|
deb60fa84c | ||
|
|
1579c947c9 | ||
|
|
d6e2f8c5ba | ||
|
|
8e73b2ae1f | ||
|
|
9f7d28ccf8 | ||
|
|
3865bb8216 | ||
|
|
2317fd73c8 | ||
|
|
fbb4d3b81d | ||
|
|
7d02cb994f | ||
|
|
31bbffc480 | ||
|
|
fa3c704497 | ||
|
|
ebf7fe89bf | ||
|
|
2100a0205f | ||
|
|
971c1f4347 | ||
|
|
a298b48bdd | ||
|
|
cd414da6de | ||
|
|
0e0262a3af | ||
|
|
558e0de315 | ||
|
|
4d210831c8 | ||
|
|
dc96359f1d | ||
|
|
bb88053aaa | ||
|
|
1a642c6783 | ||
|
|
3766ea0ea2 | ||
|
|
e00259b15f | ||
|
|
e19c147059 | ||
|
|
e975bcd7e1 | ||
|
|
25cd1c0585 | ||
|
|
281640e80c | ||
|
|
74007668a9 | ||
|
|
bb2b7979b7 | ||
|
|
dadd061147 | ||
|
|
2c963c5085 | ||
|
|
b0fe6c5723 | ||
|
|
d2cd4f5c02 | ||
|
|
638cf4995e |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -161,3 +161,8 @@ cython_debug/
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
# ignore created data files
|
||||
*.nix
|
||||
|
||||
# ignore reource.py as it is created by pyside6-rcc resources.qrc -o resources.py
|
||||
resources.py
|
||||
@@ -5,9 +5,13 @@ Relaxed ELectrophysiology Acquisition, Control, and Stimulation in python
|
||||
Implementing [relacs](https://github.com/relacs/relacs) with MCC USB 1608GX-2AO / 1808X devices ([multifunction-usb-daq-devices](https://digilent.com/shop/mcc-daq/data-acquisition/low-cost-daq/))
|
||||
|
||||
# Installation
|
||||
You have to install the MCC library (follow the installing instructions for [linux](https://github.com/mccdaq/uldaq) or [windows](https://github.com/mccdaq/mcculw)).
|
||||
You have to install the MCC library (follow the installing instructions for [linux/macOS](https://github.com/mccdaq/uldaq) or [windows](https://github.com/mccdaq/mcculw)).
|
||||
|
||||
After successful installing, you can use clone the reposity and install it with
|
||||
For MacOs if you run into problems with the libusb library if installed with homebrew, there is an issue thread on the uldaq repository.
|
||||
|
||||
[https://github.com/mccdaq/uldaq/issues/44](https://github.com/mccdaq/uldaq/issues/44)
|
||||
|
||||
After successful installing, you can use clone the repository and install it with
|
||||
|
||||
```sh
|
||||
pip install -e .
|
||||
|
||||
BIN
docs/AttCS3310.pdf
Normal file
BIN
docs/AttCS3310.pdf
Normal file
Binary file not shown.
1133
poetry.lock
generated
1133
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,16 +1,48 @@
|
||||
[project]
|
||||
organization = "de.uni-tuebingen.neuroetho"
|
||||
copyright = "(c) 2020, Neuroethology lab, Uni Tuebingen"
|
||||
|
||||
[tool.poetry]
|
||||
name = "pyrelacs"
|
||||
version = "0.1.0"
|
||||
description = "Relaxed ELectrophysiology Acquisition, Control, and Stimulation in python"
|
||||
authors = ["wendtalexander <wendtalexander@protonmail.com>"]
|
||||
repository = "https://whale.am28.uni-tuebingen.de/git/awendt/pyrelacs"
|
||||
readme = "README.md"
|
||||
license = "MIT"
|
||||
classifiers = [
|
||||
"Topic :: Scientific/Engineering",
|
||||
"Intended Audience :: Science/Research",
|
||||
"Intended Audience :: End Users/Desktop",
|
||||
]
|
||||
include = [
|
||||
{ path = "pyproject.toml" },
|
||||
"pyrelacs/resources.py"
|
||||
]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.12"
|
||||
python = ">=3.11, <3.13"
|
||||
uldaq = "^1.2.3"
|
||||
typer = "^0.12.5"
|
||||
matplotlib = "^3.9.2"
|
||||
numpy = "^1.9"
|
||||
pyqt6 = "^6.7.1"
|
||||
tomlkit = "^0.13.2"
|
||||
scipy = "^1.14.1"
|
||||
nixio = "^1.5.3"
|
||||
pyqtgraph = "^0.13.7"
|
||||
pytest = "^8.3.3"
|
||||
pglive = "^0.7.6"
|
||||
pyyaml = "^6.0.2"
|
||||
dacite = "^1.8.1"
|
||||
quantities = "^0.16.0"
|
||||
|
||||
[tool.poetry.scripts]
|
||||
pyrelacs = "pyrelacs.app:main"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
ipython = "^8.27.0"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
requires = ["poetry-core>=1.0.0"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
0
pyrelacs/__init__.py
Normal file
0
pyrelacs/__init__.py
Normal file
52
pyrelacs/app.py
Normal file
52
pyrelacs/app.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import sys
|
||||
|
||||
from PyQt6.QtCore import QSettings
|
||||
from PyQt6.QtWidgets import QApplication
|
||||
from IPython import embed
|
||||
|
||||
from pyrelacs import info
|
||||
from pyrelacs.config.config_loader import load_config
|
||||
from pyrelacs.ui.mainwindow import PyRelacs
|
||||
|
||||
from pyrelacs import (
|
||||
resources,
|
||||
) # best created with pyside6-rcc resources.qrc -o resources.py (rcc produces an error...)
|
||||
|
||||
|
||||
def main():
|
||||
app = QApplication(sys.argv)
|
||||
app.setApplicationName(info.NAME)
|
||||
app.setApplicationVersion(str(info.VERSION))
|
||||
app.setOrganizationDomain(info.ORGANIZATION)
|
||||
# app.setAttribute(Qt.ApplicationAttribute.AA_DontShowIconsInMenus, False)
|
||||
|
||||
# read window settings
|
||||
settings = QSettings(info.ORGANIZATION, info.NAME)
|
||||
width = int(settings.value("app/width", 1024))
|
||||
height = int(settings.value("app/height", 768))
|
||||
x = int(settings.value("app/pos_x", 100))
|
||||
y = int(settings.value("app/pos_y", 100))
|
||||
|
||||
# load the config
|
||||
|
||||
config = load_config()
|
||||
# start the app
|
||||
window = PyRelacs(config)
|
||||
window.setMinimumWidth(200)
|
||||
window.setMinimumHeight(200)
|
||||
window.resize(width, height)
|
||||
window.move(x, y)
|
||||
window.show()
|
||||
exit_code = app.exec()
|
||||
|
||||
# store window position and size
|
||||
pos = window.pos()
|
||||
settings.setValue("app/width", window.width())
|
||||
settings.setValue("app/height", window.height())
|
||||
settings.setValue("app/pos_x", pos.x())
|
||||
settings.setValue("app/pos_y", pos.y())
|
||||
sys.exit(exit_code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
76
pyrelacs/config.yaml
Normal file
76
pyrelacs/config.yaml
Normal file
@@ -0,0 +1,76 @@
|
||||
settings:
|
||||
# If true daq should be used, else starts without daq
|
||||
daq: False
|
||||
# class names of the repros to run
|
||||
repros: [Calibration, Sinus]
|
||||
path: ~/projects/pyrelacs/test/
|
||||
|
||||
metadata:
|
||||
SetupName : Setup1
|
||||
Maintainer : Your name
|
||||
Creator : Whoever
|
||||
SetupLocation : virtual
|
||||
Lab : XYZ-Lab
|
||||
Institute : Your institute
|
||||
University : Your university
|
||||
Address : Your institute's address
|
||||
|
||||
pyrelacs:
|
||||
data:
|
||||
input:
|
||||
inputsamplerate : 20
|
||||
# Unit is rescaled to Hz
|
||||
inputsamplerateunit : kHz
|
||||
# BufferSize
|
||||
inputtracecapacity : 600
|
||||
# Unit is rescaled to s
|
||||
inputtracecapacityunit : s
|
||||
inputunipolar : false
|
||||
inputtraceid : [ V-1, EOD, LocalEOD-1, GlobalEFieldStimulus ]
|
||||
inputtracescale : [ 100, 1, 10, 1 ]
|
||||
inputtraceunit : [ mV, mV, mV, mV ]
|
||||
inputtracedevice : [ ai-1, ai-1, ai-1, ai-1 ]
|
||||
inputtracechannel : [ 0, 2, 4, 6 ]
|
||||
inputtracereference : [ ground, ground, ground, ground ]
|
||||
inputtracemaxvalue : [ 100, 2, 2, 10 ]
|
||||
inputtracecenter : [ true, false, false, false ]
|
||||
output:
|
||||
outputtraceid : [ GlobalEField, GlobalEFieldAM, LocalEField, I ]
|
||||
outputtracedevice : [ ao-1, ao-1, ao-1, ao-1 ]
|
||||
outputtracechannel : [ 0, 1, 2, 3 ]
|
||||
outputtracescale : [ 1, 1, 1, 1 ]
|
||||
outputtraceunit : [ V, V, V, V ]
|
||||
outputtracemaxrate : [ 40, 40, 40, 40]
|
||||
outputtracemaxrateunit : [kHz, kHz, kHz, kHz]
|
||||
outputtracemodality : [ electric, electric, electric, current ]
|
||||
|
||||
sinus:
|
||||
inputsamplerate : 20
|
||||
inputsamplerateunit : kHz
|
||||
# BufferSize
|
||||
inputtracecapacity : 600
|
||||
inputtracecapacityunit : s
|
||||
outputtraceid : [ Sinus ]
|
||||
outputtracedevice : [ ao-0 ]
|
||||
outputtracechannel : [ 0 ]
|
||||
outputtracescale : [ 1 ]
|
||||
outputtraceunit : [ V ]
|
||||
outputtracemaxrate : [ 40 ]
|
||||
outputtracemaxrateunit : [kHz]
|
||||
outputtracemodality : [ electric ]
|
||||
|
||||
devices:
|
||||
DAQFlexCore:
|
||||
analogoutputpins : [0, 1]
|
||||
analoginputpinshigh : [0, 1, 2, 3,4,5,6,7]
|
||||
analoginputpinslow : [1,2]
|
||||
digitalpins : [0,1,2,3]
|
||||
|
||||
CS3310DIO:
|
||||
ident : attdev-1
|
||||
strobepin : 6
|
||||
datainpin : 5
|
||||
dataoutpin : -1
|
||||
cspin : 4
|
||||
mutepin : 7
|
||||
zcenpin : -1
|
||||
132
pyrelacs/config/config_loader.py
Normal file
132
pyrelacs/config/config_loader.py
Normal file
@@ -0,0 +1,132 @@
|
||||
from typing import TypedDict, Union
|
||||
from dataclasses import dataclass
|
||||
import pathlib
|
||||
|
||||
|
||||
import dacite
|
||||
import yaml
|
||||
from dacite import from_dict
|
||||
from IPython import embed
|
||||
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
log = config_logging()
|
||||
|
||||
|
||||
@dataclass
|
||||
class ValueUnit:
|
||||
value: int
|
||||
unit: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class Settings:
|
||||
daq: bool
|
||||
repros: list[str]
|
||||
path: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class Metadata:
|
||||
SetupName: str
|
||||
Maintainer: str
|
||||
Creator: str
|
||||
SetupLocation: str
|
||||
Lab: str
|
||||
Institute: str
|
||||
University: str
|
||||
Address: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class Input:
|
||||
inputsamplerate: int
|
||||
inputsamplerateunit: str
|
||||
# BufferSize
|
||||
inputtracecapacity: int
|
||||
inputtracecapacityunit: str
|
||||
inputunipolar: bool
|
||||
inputtraceid: list[str]
|
||||
inputtracescale: list[int]
|
||||
inputtraceunit: list[str]
|
||||
inputtracedevice: list[str]
|
||||
inputtracechannel: list[int]
|
||||
inputtracereference: list[str]
|
||||
inputtracemaxvalue: list[int]
|
||||
inputtracecenter: list[bool]
|
||||
|
||||
|
||||
@dataclass
|
||||
class Output:
|
||||
outputtraceid: list[str]
|
||||
outputtracedevice: list[str]
|
||||
outputtracechannel: list[int]
|
||||
outputtracescale: list[int]
|
||||
outputtraceunit: list[str]
|
||||
outputtracemaxrate: list[int]
|
||||
outputtracemaxrateunit: list[str]
|
||||
outputtracemodality: list[str]
|
||||
|
||||
|
||||
@dataclass
|
||||
class Data:
|
||||
input: Input
|
||||
output: Output
|
||||
|
||||
|
||||
@dataclass
|
||||
class Sinus:
|
||||
inputsamplerate: int
|
||||
inputsamplerateunit: str
|
||||
# BufferSize
|
||||
inputtracecapacity: int
|
||||
inputtracecapacityunit: str
|
||||
outputtraceid: list[str]
|
||||
outputtracedevice: list[str]
|
||||
outputtracechannel: list[int]
|
||||
outputtracescale: list[int]
|
||||
outputtraceunit: list[str]
|
||||
outputtracemaxrate: list[int]
|
||||
outputtracemaxrateunit: list[str]
|
||||
outputtracemodality: list[str]
|
||||
|
||||
|
||||
@dataclass
|
||||
class PyRelacs:
|
||||
data: Data
|
||||
sinus: Sinus
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
settings: Settings
|
||||
metadata: Metadata
|
||||
pyrelacs: PyRelacs
|
||||
|
||||
|
||||
def load_config():
|
||||
pyrelacs_config_path = pathlib.Path(__file__).parent.parent / "config.yaml"
|
||||
log.debug(pyrelacs_config_path)
|
||||
if not pyrelacs_config_path.is_file():
|
||||
log.error("Config File was not found")
|
||||
with open(pyrelacs_config_path, "r") as config_file:
|
||||
try:
|
||||
data = yaml.full_load(config_file)
|
||||
try:
|
||||
config = from_dict(data_class=Config, data=data)
|
||||
return config
|
||||
except dacite.DaciteError as e:
|
||||
log.error(f"Invalid Config, {e}")
|
||||
except yaml.YAMLError as e:
|
||||
raise yaml.YAMLError(f"Error parsing YAML file: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pyrelacs_config_path = pathlib.Path(__file__).parent.parent / "config.yaml"
|
||||
log.debug(pyrelacs_config_path)
|
||||
if not pyrelacs_config_path.is_file():
|
||||
log.error("Config File was not found")
|
||||
with open(pyrelacs_config_path, "r") as config_file:
|
||||
data = yaml.full_load(config_file)
|
||||
embed()
|
||||
exit()
|
||||
81
pyrelacs/dataio/buffer.py
Normal file
81
pyrelacs/dataio/buffer.py
Normal 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)
|
||||
192
pyrelacs/dataio/circbuffer.py
Normal file
192
pyrelacs/dataio/circbuffer.py
Normal file
@@ -0,0 +1,192 @@
|
||||
from typing import Tuple
|
||||
import numpy as np
|
||||
from IPython import embed
|
||||
from pyqtgraph.Qt.QtCore import QMutex
|
||||
|
||||
|
||||
class CircBuffer:
|
||||
def __init__(
|
||||
self,
|
||||
size: int,
|
||||
channels: int = 1,
|
||||
samplerate: float = 40_000.0,
|
||||
mutex: QMutex = QMutex(),
|
||||
):
|
||||
self.__size = size
|
||||
self.__channels = channels
|
||||
self.__samplereate = samplerate
|
||||
self.__buffer = np.zeros(
|
||||
(channels, size), dtype=np.double
|
||||
) # or dtype of your choice
|
||||
self.__time = np.zeros((channels, size), dtype=np.double)
|
||||
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)]
|
||||
self.mutex = mutex
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
return self.__size
|
||||
|
||||
@property
|
||||
def samplerate(self):
|
||||
return self.__samplereate
|
||||
|
||||
@property
|
||||
def channel_count(self):
|
||||
return self.__channels
|
||||
|
||||
def totalcount(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.mutex.lock()
|
||||
self.__buffer[channel, self.write_index(channel)] = item
|
||||
self.__index[channel] = (self.write_index(channel) + 1) % self.__size
|
||||
self.__totalcount[channel] += 1
|
||||
self.__time[channel, self.write_index(channel)] = (
|
||||
self.__time[channel, self.write_index(channel) - 1] + 1 / self.__samplereate
|
||||
)
|
||||
if self.__index[channel] == 0:
|
||||
self.__is_full[channel] = True
|
||||
self.__overflows[channel] += 1
|
||||
self.mutex.unlock()
|
||||
|
||||
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) -> Tuple[np.double, float]:
|
||||
# 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], self.__time[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], self.__time[channel, index])
|
||||
else:
|
||||
raise IndexError(
|
||||
f"Invalid index {index} on ring buffer for channel{channel}"
|
||||
)
|
||||
|
||||
def read(self, start, extend=1, channel=0):
|
||||
"""Reads a numpy array from buffer"""
|
||||
if extend < 0:
|
||||
raise IndexError(f"Invalid extend ({extend}) for channel {channel}")
|
||||
if not self.is_full(channel):
|
||||
if start < 0:
|
||||
raise IndexError(f"Invalid start ({start}) for channel {channel}")
|
||||
else:
|
||||
if start < 0:
|
||||
start = start + self.size
|
||||
|
||||
if extend == 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 extend > vc:
|
||||
extend = vc
|
||||
|
||||
if (start + extend) > self.__totalcount[channel]:
|
||||
raise IndexError(
|
||||
f" Invalid range, extended over the totalcount of the buffer {self.__totalcount[channel]}"
|
||||
)
|
||||
|
||||
if (start + extend) < self.size:
|
||||
return (
|
||||
self.__time[channel, start : start + extend],
|
||||
self.__buffer[channel, start : start + extend],
|
||||
)
|
||||
else:
|
||||
return (
|
||||
np.concatenate(
|
||||
(
|
||||
self.__time[channel, start:],
|
||||
self.__time[channel, : extend - self.size + start],
|
||||
)
|
||||
),
|
||||
np.concatenate(
|
||||
(
|
||||
self.__buffer[channel, start:],
|
||||
self.__buffer[channel, : extend - self.size + start],
|
||||
)
|
||||
),
|
||||
)
|
||||
175
pyrelacs/dataio/daq_producer.py
Normal file
175
pyrelacs/dataio/daq_producer.py
Normal file
@@ -0,0 +1,175 @@
|
||||
import time
|
||||
import faulthandler
|
||||
|
||||
import uldaq
|
||||
import numpy as np
|
||||
from IPython import embed
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from pyrelacs.dataio.circbuffer import CircBuffer
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
log = config_logging()
|
||||
faulthandler.enable()
|
||||
|
||||
|
||||
class DaqProducer:
|
||||
def __init__(
|
||||
self, buffer: CircBuffer, device: uldaq.DaqDevice, channels: list[int]
|
||||
):
|
||||
self.buffer = buffer
|
||||
self.device = device
|
||||
self.ai_device = self.device.get_ai_device()
|
||||
self.channels = channels
|
||||
|
||||
self.stop = False
|
||||
|
||||
def read_analog_continously(
|
||||
self,
|
||||
*args,
|
||||
**kwargs,
|
||||
):
|
||||
log.debug("starting acquisition")
|
||||
if self.channels[0] == self.channels[1]:
|
||||
channel_range = np.arange(1)
|
||||
else:
|
||||
channel_range = np.arange(self.channels[0], self.channels[1] + 1)
|
||||
|
||||
assert channel_range.size == self.buffer.channel_count, ValueError(
|
||||
f"Missmatch in channel count,\n daq_channel: "
|
||||
f"{channel_range.size}\n buffer_channel: {self.buffer.channel_count}"
|
||||
)
|
||||
|
||||
# let the buffer for the daq device hold 5 seconds of data
|
||||
daq_buffer_size = int(self.buffer.samplerate * 5)
|
||||
|
||||
data_in = uldaq.create_float_buffer(channel_range.size, daq_buffer_size)
|
||||
log.debug(f"Buffersize for daq {len(data_in)}")
|
||||
log.debug(f"Buffersize {self.buffer.size}")
|
||||
|
||||
er = self.ai_device.a_in_scan(
|
||||
self.channels[0],
|
||||
self.channels[1],
|
||||
uldaq.AiInputMode.SINGLE_ENDED,
|
||||
uldaq.Range.BIP10VOLTS,
|
||||
daq_buffer_size,
|
||||
self.buffer.samplerate,
|
||||
uldaq.ScanOption.CONTINUOUS,
|
||||
uldaq.AInScanFlag.DEFAULT,
|
||||
data=data_in,
|
||||
)
|
||||
|
||||
chunk_size = int(daq_buffer_size / 10)
|
||||
wrote_chunk = False
|
||||
|
||||
start_time = time.time()
|
||||
daq_status = uldaq.ScanStatus.IDLE
|
||||
while daq_status == uldaq.ScanStatus.IDLE:
|
||||
daq_status = self.ai_device.get_scan_status()[0]
|
||||
while daq_status != uldaq.ScanStatus.IDLE:
|
||||
prev_count = 0
|
||||
prev_index = 0
|
||||
while not self.stop:
|
||||
daq_status, transfer_status = self.ai_device.get_scan_status()
|
||||
# The index into the data buffer immediately following the last sample transferred.
|
||||
current_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
|
||||
|
||||
new_data_count = total_samples - prev_count
|
||||
# check if counts if new data is bigger than the buffer
|
||||
# if that happends stop the acquisition
|
||||
if new_data_count > len(data_in):
|
||||
self.ai_device.scan_stop()
|
||||
log.error("A Buffer overrun occurred")
|
||||
break
|
||||
|
||||
if new_data_count > chunk_size:
|
||||
wrote_chunk = True
|
||||
# index wraps around the buffer
|
||||
if prev_index + chunk_size > len(data_in) - 1:
|
||||
log.debug("Chunk wraps around buffersize")
|
||||
first_chunk = len(data_in) - prev_index
|
||||
[
|
||||
self.buffer.append(data_in[prev_index + i])
|
||||
for i in range(first_chunk)
|
||||
]
|
||||
second_chunk = chunk_size - first_chunk
|
||||
[
|
||||
self.buffer.append(data_in[i])
|
||||
for i in range(second_chunk)
|
||||
]
|
||||
else:
|
||||
log.debug("Writing chunk to buffer")
|
||||
[
|
||||
self.buffer.append(data_in[prev_index + i])
|
||||
for i in range(chunk_size)
|
||||
]
|
||||
|
||||
self.buffer.append(data_in[current_index])
|
||||
|
||||
if total_samples - prev_count > len(data_in):
|
||||
self.ai_device.scan_stop()
|
||||
log.error("A Buffer overrun occurred")
|
||||
break
|
||||
|
||||
else:
|
||||
wrote_chunk = False
|
||||
if wrote_chunk:
|
||||
prev_count += chunk_size
|
||||
prev_index += chunk_size
|
||||
prev_index %= daq_buffer_size
|
||||
|
||||
self.ai_device.scan_stop()
|
||||
daq_status, transfer_status = self.ai_device.get_scan_status()
|
||||
current_index = transfer_status.current_index
|
||||
log.debug(daq_status)
|
||||
|
||||
log.debug(transfer_status.current_index)
|
||||
log.debug(transfer_status.current_total_count)
|
||||
log.debug(transfer_status.current_scan_count)
|
||||
log.debug(self.buffer.totalcount())
|
||||
log.debug("Appending last chunk")
|
||||
|
||||
if prev_index + chunk_size > len(data_in) - 1:
|
||||
log.debug("Chunk wraps around buffersize")
|
||||
first_chunk = len(data_in) - prev_index
|
||||
[
|
||||
self.buffer.append(data_in[prev_index + i])
|
||||
for i in range(first_chunk)
|
||||
]
|
||||
second_chunk = chunk_size - first_chunk
|
||||
[self.buffer.append(data_in[i]) for i in range(second_chunk)]
|
||||
else:
|
||||
log.debug("Writing chunk to buffer")
|
||||
[
|
||||
self.buffer.append(data_in[prev_index + i])
|
||||
for i in range(chunk_size)
|
||||
]
|
||||
|
||||
self.buffer.append(data_in[current_index])
|
||||
log.info("stopping")
|
||||
|
||||
break
|
||||
break
|
||||
return "Done. "
|
||||
|
||||
def stop_aquisition(self):
|
||||
self.stop = True
|
||||
|
||||
|
||||
# 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 = CircBuffer(size=1_000_000, samplerate=100)
|
||||
# producer = DaqProducer(buf, daq_device, [1, 1])
|
||||
# producer.read_analog_continously()
|
||||
72
pyrelacs/dataio/nix_writer.py
Normal file
72
pyrelacs/dataio/nix_writer.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import time
|
||||
from IPython import embed
|
||||
from PyQt6.QtCore import QMutex
|
||||
import nixio
|
||||
|
||||
from pyrelacs.dataio.circbuffer import CircBuffer
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
log = config_logging()
|
||||
|
||||
|
||||
class NixWriter:
|
||||
def __init__(self, buffer: CircBuffer) -> None:
|
||||
self.buffer = buffer
|
||||
|
||||
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(channel=channel)
|
||||
if total_count - index >= chunk_size:
|
||||
mutex.lock()
|
||||
log.debug(index)
|
||||
try:
|
||||
_, 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}")
|
||||
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 {index}")
|
||||
|
||||
def _write_header(self):
|
||||
self.nix_file = nixio.File.open(path="data.nix", mode=nixio.FileMode.Overwrite)
|
||||
self.block = self.nix_file.create_block("recording", "testfile")
|
||||
self.data_array = self.block.create_data_array(
|
||||
"Analog1", "ndarray", shape=(1000,), dtype=nixio.DataType.Double
|
||||
)
|
||||
|
||||
def stop_writing(self):
|
||||
self.write = False
|
||||
50
pyrelacs/dataio/sin_producer.py
Normal file
50
pyrelacs/dataio/sin_producer.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import time
|
||||
import numpy as np
|
||||
from IPython import embed
|
||||
from pyrelacs.dataio.circbuffer import CircBuffer
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
|
||||
log = config_logging()
|
||||
|
||||
|
||||
class SinProducer:
|
||||
def __init__(
|
||||
self,
|
||||
buffer: CircBuffer,
|
||||
) -> None:
|
||||
self.buffer = buffer
|
||||
self.stop = False
|
||||
|
||||
def produce_sin(
|
||||
self,
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
AMPLITUDE = 2
|
||||
FREQUENCY = 10
|
||||
|
||||
self.stop = False
|
||||
log.debug("producing Sin")
|
||||
start_time = time.time()
|
||||
t = 0
|
||||
while not self.stop:
|
||||
s = AMPLITUDE * np.sin(2 * np.pi * FREQUENCY * t)
|
||||
self.buffer.append(s)
|
||||
t += 1 / self.buffer.samplerate
|
||||
time.sleep(1 / self.buffer.samplerate)
|
||||
|
||||
end_time = time.time()
|
||||
|
||||
log.debug(f"duration sinus {end_time-start_time}")
|
||||
log.debug(f"Stimulation time {t}")
|
||||
log.debug(f"Total samples produced {self.buffer.totalcount()}")
|
||||
|
||||
def stop_request(self):
|
||||
self.stop = True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
buf = CircBuffer(1_000_000, 1, samplerate=10_000)
|
||||
pro_sin = SinProducer(buf)
|
||||
pro_sin.produce_sin()
|
||||
0
pyrelacs/devices/__init__.py
Normal file
0
pyrelacs/devices/__init__.py
Normal file
431
pyrelacs/devices/mccdaq.py
Normal file
431
pyrelacs/devices/mccdaq.py
Normal file
@@ -0,0 +1,431 @@
|
||||
from ctypes import Array, c_double
|
||||
import time
|
||||
from typing import Union
|
||||
from IPython import embed
|
||||
import numpy.typing as npt
|
||||
import uldaq
|
||||
import numpy as np
|
||||
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
log = config_logging()
|
||||
|
||||
|
||||
class MccDaq:
|
||||
"""
|
||||
Represents the Digital/Analog Converter from Meassuring Computing.
|
||||
provides methods for writing and reading the Analog / Digital input and output.
|
||||
|
||||
Connects to the DAC device.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
daq_device : uldaq.DaqDevice
|
||||
DaqDevice for handling connecting, releasing and disconnecting
|
||||
ai_device : uldaq.AiDevice
|
||||
The Analog input Device
|
||||
ao_device :
|
||||
Analog output Device
|
||||
dio_device :
|
||||
Digital Input Output
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
||||
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
|
||||
try:
|
||||
self.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
|
||||
try:
|
||||
self.daq_device.connect()
|
||||
except uldaq.ul_exception.ULException:
|
||||
self.disconnect_daq()
|
||||
self.connect_dac()
|
||||
|
||||
self.ai_device = self.daq_device.get_ai_device()
|
||||
self.ao_device = self.daq_device.get_ao_device()
|
||||
self.dio_device = self.daq_device.get_dio_device()
|
||||
|
||||
log.debug("Connected to MccDaq")
|
||||
log.debug("Activating the Attenuator")
|
||||
self.activate_attenuator()
|
||||
|
||||
def connect_dac(self):
|
||||
"""
|
||||
Connecting to the DAQ device
|
||||
|
||||
"""
|
||||
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
||||
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
|
||||
if len(devices) == 0:
|
||||
log.error("Did not found daq devices, please connect one")
|
||||
exit(1)
|
||||
self.daq_device = uldaq.DaqDevice(devices[0])
|
||||
self.daq_device.connect()
|
||||
self.ai_device = self.daq_device.get_ai_device()
|
||||
self.ao_device = self.daq_device.get_ao_device()
|
||||
self.dio_device = self.daq_device.get_dio_device()
|
||||
log.debug("Connected")
|
||||
|
||||
def read_analog(
|
||||
self,
|
||||
channels: list[int],
|
||||
duration: int,
|
||||
samplerate: float,
|
||||
AiInputMode: uldaq.AiInputMode = uldaq.AiInputMode.SINGLE_ENDED,
|
||||
Range: uldaq.Range = uldaq.Range.BIP10VOLTS,
|
||||
ScanOption: uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO,
|
||||
AInScanFlag: uldaq.AInScanFlag = uldaq.AInScanFlag.DEFAULT,
|
||||
) -> Array[c_double]:
|
||||
"""
|
||||
Reading the analog input of the DAC device
|
||||
Creates a c_double Array for storing the acquired data
|
||||
|
||||
Parameters
|
||||
----------
|
||||
channels : list[int]
|
||||
channels to read from, provide only two int's in a list (ex [0, 1] or [0, 4])
|
||||
for sampling from the range(channel0, channel4)
|
||||
|
||||
duration : int
|
||||
duration of sampling period
|
||||
|
||||
samplerate : float
|
||||
samplerate for the duration of sampling
|
||||
|
||||
AiInputMode : uldaq.AiInputMode = uldaq.AiInputMode.SINGLE_ENDED
|
||||
Contains attributes indicating A/D channel input modes.
|
||||
Compares to Ground
|
||||
|
||||
Range : uldaq.Range = uldaq.Range.BIP10VOLTS
|
||||
Range of the output
|
||||
|
||||
ScanOption : uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO
|
||||
Specific Flags for acuiring the input
|
||||
|
||||
AInScanFlag : uldaq.AInScanFlag = uldaq.AInScanFlag.DEFAULT
|
||||
Scaling of the data
|
||||
|
||||
Returns
|
||||
-------
|
||||
Array[c_double]
|
||||
|
||||
"""
|
||||
assert len(channels) == 2, log.error("You can only provide two channels [0, 1]")
|
||||
|
||||
if channels[0] != channels[1]:
|
||||
buffer_len_channels = 2
|
||||
else:
|
||||
buffer_len_channels = 1
|
||||
|
||||
buffer_len = np.shape(np.arange(0, duration, 1 / samplerate))[0]
|
||||
data_analog_input = uldaq.create_float_buffer(buffer_len_channels, buffer_len)
|
||||
|
||||
er = self.ai_device.a_in_scan(
|
||||
channels[0],
|
||||
channels[1],
|
||||
AiInputMode,
|
||||
Range,
|
||||
buffer_len,
|
||||
samplerate,
|
||||
ScanOption,
|
||||
AInScanFlag,
|
||||
data=data_analog_input,
|
||||
)
|
||||
|
||||
return data_analog_input
|
||||
|
||||
def write_analog(
|
||||
self,
|
||||
data: Union[list, npt.NDArray],
|
||||
channels: list[int],
|
||||
samplerate: float,
|
||||
Range: uldaq.Range = uldaq.Range.BIP10VOLTS,
|
||||
ScanOption: uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO,
|
||||
AOutScanFlag: uldaq.AOutScanFlag = uldaq.AOutScanFlag.DEFAULT,
|
||||
) -> Array[c_double]:
|
||||
"""
|
||||
Writes data to the DAC device.
|
||||
Creates a c_double Array for writing the data
|
||||
|
||||
Parameters
|
||||
----------
|
||||
data : Union[list, npt.NDArray]
|
||||
data which should be written to the DAC
|
||||
|
||||
channels : list[int]
|
||||
channels to read from, provide only two int's in a list (ex [0, 1])
|
||||
for sampling from the range(channel0, channel1)
|
||||
DAC USB 1608GX-2AO has only 2 output channels
|
||||
|
||||
samplerate : float
|
||||
samplerate for the duration of sampling
|
||||
|
||||
Range : uldaq.Range = uldaq.Range.BIP10VOLTS
|
||||
Range of the output
|
||||
|
||||
ScanOption : uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO
|
||||
Specific Flags for acuiring the input
|
||||
|
||||
AOutScanFlag : uldaq.AOutScanFlag = uldaq.AOutScanFlag.DEFAULT
|
||||
For Scaling the data
|
||||
|
||||
Returns
|
||||
-------
|
||||
Array[c_double]
|
||||
"""
|
||||
|
||||
assert len(channels) == 2, log.error("You can only provide two channels [0, 1]")
|
||||
|
||||
buffer = c_double * len(data)
|
||||
data_analog_output = buffer(*data)
|
||||
log.debug(f"Created C_double data {data_analog_output}")
|
||||
|
||||
try:
|
||||
err = self.ao_device.a_out_scan(
|
||||
channels[0],
|
||||
channels[1],
|
||||
Range,
|
||||
int(len(data)),
|
||||
samplerate,
|
||||
ScanOption,
|
||||
AOutScanFlag,
|
||||
data_analog_output,
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"{e}")
|
||||
self.set_analog_to_zero()
|
||||
self.disconnect_daq()
|
||||
|
||||
return data_analog_output
|
||||
|
||||
def set_analog_to_zero(self, channels: list[int] = [0, 1]) -> None:
|
||||
"""
|
||||
Sets all analog outputs to zero
|
||||
|
||||
Parameters
|
||||
----------
|
||||
channels : list[int]
|
||||
channels to read from, provide only two int's in a list (ex [0, 1])
|
||||
for sampling from the range(channel0, channel1)
|
||||
DAC USB 1608GX-2AO has only 2 output channels
|
||||
|
||||
"""
|
||||
try:
|
||||
err = self.ao_device.a_out_list(
|
||||
channels[0],
|
||||
channels[1],
|
||||
[
|
||||
uldaq.Range.BIP10VOLTS,
|
||||
uldaq.Range.BIP10VOLTS,
|
||||
],
|
||||
uldaq.AOutListFlag.DEFAULT,
|
||||
[0, 0],
|
||||
)
|
||||
except Exception as er:
|
||||
log.error(f"{er}")
|
||||
log.error("disconnection dac")
|
||||
# self.disconnect_daq()
|
||||
|
||||
def digital_trigger(self, ch: int = 0) -> None:
|
||||
"""
|
||||
Writes a 1 to a specified digital channel, if the channel is already on 1 switches it to
|
||||
0 and after Nano second it writes a 1 to the specified digital channel
|
||||
|
||||
Parameters
|
||||
----------
|
||||
ch : int
|
||||
Channel to trigger
|
||||
"""
|
||||
data = self.read_bit(channel=ch)
|
||||
if data:
|
||||
self.write_bit(channel=ch, bit=0)
|
||||
time.time_ns()
|
||||
self.write_bit(channel=ch, bit=1)
|
||||
else:
|
||||
self.write_bit(channel=ch, bit=1)
|
||||
|
||||
def write_bit(self, channel: int = 0, bit: int = 1) -> None:
|
||||
"""
|
||||
Writes a 0 / 1 to a specified digitial channel
|
||||
|
||||
Parameters
|
||||
----------
|
||||
channel : int
|
||||
Digital channel to write
|
||||
|
||||
bit : int
|
||||
0 / 1 for writing to the digital channel
|
||||
|
||||
"""
|
||||
self.dio_device.d_config_bit(
|
||||
uldaq.DigitalPortType.AUXPORT, channel, uldaq.DigitalDirection.OUTPUT
|
||||
)
|
||||
self.dio_device.d_bit_out(
|
||||
uldaq.DigitalPortType.AUXPORT, bit_number=channel, data=bit
|
||||
)
|
||||
|
||||
def read_bit(self, channel: int = 0) -> int:
|
||||
"""
|
||||
Reads a 0 / 1 from the specified digital channel
|
||||
|
||||
Parameters
|
||||
----------
|
||||
channel : int
|
||||
Digital channel to read from
|
||||
|
||||
Returns
|
||||
-------
|
||||
bit : int
|
||||
0 or 1 from the digital channel
|
||||
"""
|
||||
bit = self.dio_device.d_bit_in(uldaq.DigitalPortType.AUXPORT, channel)
|
||||
return bit
|
||||
|
||||
def disconnect_daq(self):
|
||||
log.debug("Disconnecting DAQ")
|
||||
self.deactivate_attenuator()
|
||||
self.daq_device.disconnect()
|
||||
self.daq_device.release()
|
||||
|
||||
def check_attenuator(self) -> None:
|
||||
"""
|
||||
For checking the attenuator in the DAC device that was implemented to attenuate the
|
||||
analog signal to mV.
|
||||
|
||||
Writes to Channel 0 of the analog output with different attenuation levels
|
||||
0, 0, -2, -5, -10, -20, -50 dB and the second 0 has a software mute
|
||||
|
||||
"""
|
||||
SAMPLERATE = 40_000.0
|
||||
DURATION = 5
|
||||
AMPLITUDE = 1
|
||||
SINFREQ = 1
|
||||
t = np.arange(0, DURATION, 1 / SAMPLERATE)
|
||||
data = AMPLITUDE * np.sin(2 * np.pi * SINFREQ * t)
|
||||
# data_channels = np.concatenate((data, data))
|
||||
|
||||
db_values = [0, 0, -2, -5, -10, -20, -50]
|
||||
for i, db_value in enumerate(db_values):
|
||||
log.info(f"Attenuating the Channels, with {db_value}")
|
||||
if i == 1:
|
||||
log.info("Muting the Channels")
|
||||
self.set_attenuation_level(
|
||||
db_value, db_value, mute_channel1=True, mute_channel2=True
|
||||
)
|
||||
else:
|
||||
self.set_attenuation_level(db_value, db_value)
|
||||
|
||||
_ = self.write_analog(
|
||||
data,
|
||||
[0, 0],
|
||||
SAMPLERATE,
|
||||
ScanOption=uldaq.ScanOption.EXTTRIGGER,
|
||||
Range=uldaq.Range.BIP10VOLTS,
|
||||
)
|
||||
self.digital_trigger()
|
||||
|
||||
try:
|
||||
self.ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 15)
|
||||
self.write_bit(channel=0, bit=0)
|
||||
self.set_analog_to_zero()
|
||||
except uldaq.ul_exception.ULException:
|
||||
log.debug("Operation timed out")
|
||||
self.write_bit(channel=0, bit=0)
|
||||
self.disconnect_daq()
|
||||
self.connect_dac()
|
||||
self.set_analog_to_zero()
|
||||
finally:
|
||||
self.write_bit(channel=0, bit=0)
|
||||
self.disconnect_daq()
|
||||
self.connect_dac()
|
||||
self.set_analog_to_zero()
|
||||
|
||||
log.info("Sleeping for 1 second, before next attenuation")
|
||||
time.sleep(1)
|
||||
|
||||
def set_attenuation_level(
|
||||
self,
|
||||
db_channel1: float = 5.0,
|
||||
db_channel2: float = 5.0,
|
||||
mute_channel1: bool = False,
|
||||
mute_channel2: bool = False,
|
||||
):
|
||||
"""
|
||||
Setting the attenuation level of the chip that is connected to the DAQ
|
||||
The attenuation level is set by writing to the connected digital output pin 5
|
||||
where the strobepin 6 is signaling the when the bit was send.
|
||||
The cspin is set from 1 to 0 for the start and 0 to 1 for signaling the end
|
||||
of the data write process.
|
||||
|
||||
The mute pin should be set to 1 for the device to be working.
|
||||
|
||||
More information in the AttCS3310.pdf in the doc
|
||||
|
||||
ident : attdev-1
|
||||
strobepin : 6
|
||||
datainpin : 5
|
||||
dataoutpin: -1
|
||||
cspin : 4
|
||||
mutepin : 7
|
||||
zcenpin : -1
|
||||
|
||||
Parameters
|
||||
----------
|
||||
db_channel1 : float
|
||||
dB Attenuation level for the first channel
|
||||
|
||||
db_channel2 : float
|
||||
dB Attenuation level for the second channel
|
||||
|
||||
mute_channel1 : bool
|
||||
Software mute for the first channel
|
||||
|
||||
mute_channel2 : bool
|
||||
Software mute for the second channel
|
||||
|
||||
|
||||
"""
|
||||
self.activate_attenuator()
|
||||
hardware_possible_db = np.arange(-95.5, 32.0, 0.5)
|
||||
byte_number = np.arange(1, 256)
|
||||
byte_number_db1 = byte_number[hardware_possible_db == db_channel1][0]
|
||||
binary_db1 = np.binary_repr(byte_number_db1, width=8)
|
||||
byte_number_db2 = byte_number[hardware_possible_db == db_channel2][0]
|
||||
binary_db2 = np.binary_repr(byte_number_db2, width=8)
|
||||
if mute_channel1:
|
||||
log.info("Muting channel one")
|
||||
binary_db1 = "00000000"
|
||||
if mute_channel2:
|
||||
log.info("Muting channel one")
|
||||
binary_db2 = "00000000"
|
||||
|
||||
channels_db = binary_db2 + binary_db1
|
||||
self.write_bit(channel=4, bit=0)
|
||||
for b in channels_db:
|
||||
self.write_bit(channel=5, bit=int(b))
|
||||
time.time_ns()
|
||||
self.write_bit(channel=6, bit=1)
|
||||
time.time_ns()
|
||||
self.write_bit(channel=6, bit=0)
|
||||
time.time_ns()
|
||||
self.write_bit(channel=4, bit=1)
|
||||
|
||||
def activate_attenuator(self):
|
||||
"""
|
||||
Activation of the attenuator, where the cspin and mute pin is set to 1,
|
||||
and the datapin and strobpin to 0
|
||||
|
||||
"""
|
||||
for ch, b in zip([4, 5, 6, 7], [1, 0, 0, 1]):
|
||||
self.write_bit(channel=ch, bit=b)
|
||||
|
||||
def deactivate_attenuator(self):
|
||||
"""
|
||||
Writes a 0 to the mute pin, which is deactivating the attenuator
|
||||
|
||||
"""
|
||||
# mute should be enabled for starting calibration
|
||||
self.write_bit(channel=7, bit=0)
|
||||
BIN
pyrelacs/icons/connect.png
Normal file
BIN
pyrelacs/icons/connect.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.9 KiB |
BIN
pyrelacs/icons/disconnect.png
Normal file
BIN
pyrelacs/icons/disconnect.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.3 KiB |
BIN
pyrelacs/icons/exit.png
Normal file
BIN
pyrelacs/icons/exit.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.0 KiB |
BIN
pyrelacs/icons/record.png
Normal file
BIN
pyrelacs/icons/record.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.9 KiB |
BIN
pyrelacs/icons/relacstuxheader.png
Normal file
BIN
pyrelacs/icons/relacstuxheader.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 255 KiB |
BIN
pyrelacs/icons/stop.png
Normal file
BIN
pyrelacs/icons/stop.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
36
pyrelacs/info.py
Normal file
36
pyrelacs/info.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import tomllib
|
||||
import pathlib
|
||||
|
||||
|
||||
def load_project_settings(project_root):
|
||||
# Read the pyproject.toml file
|
||||
pyproject_path = pathlib.Path.joinpath(project_root, "pyproject.toml")
|
||||
with open(pyproject_path, "rb") as f:
|
||||
pyproject_content = tomllib.load(f)
|
||||
|
||||
info_dict = {
|
||||
"name": pyproject_content["tool"]["poetry"]["name"],
|
||||
"version": pyproject_content["tool"]["poetry"]["version"],
|
||||
"description": pyproject_content["tool"]["poetry"]["description"],
|
||||
"authors": pyproject_content["tool"]["poetry"]["authors"],
|
||||
"readme": pyproject_content["tool"]["poetry"]["authors"],
|
||||
"licence": pyproject_content["tool"]["poetry"]["license"],
|
||||
"organization": pyproject_content["project"]["organization"],
|
||||
"classifiers": pyproject_content["tool"]["poetry"]["classifiers"],
|
||||
"copyright": pyproject_content["project"]["copyright"],
|
||||
"repository": pyproject_content["tool"]["poetry"]["repository"],
|
||||
}
|
||||
return info_dict
|
||||
|
||||
|
||||
_root = pathlib.Path(__file__).parent.parent
|
||||
_infodict = load_project_settings(_root)
|
||||
|
||||
NAME = _infodict["name"]
|
||||
VERSION = _infodict["version"]
|
||||
AUTHORS = _infodict["authors"]
|
||||
COPYRIGHT = _infodict["copyright"]
|
||||
HOMEPAGE = _infodict["repository"]
|
||||
CLASSIFIERS = _infodict["classifiers"]
|
||||
DESCRIPTION = _infodict["description"]
|
||||
ORGANIZATION = _infodict["organization"]
|
||||
@@ -1,19 +0,0 @@
|
||||
import uldaq
|
||||
from IPython import embed
|
||||
import typer
|
||||
|
||||
|
||||
def connect():
|
||||
try:
|
||||
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
||||
daq_device = uldaq.DaqDevice(devices[0])
|
||||
daq_device.connect()
|
||||
print(f"Connected to daq_device {devices[0].product_name}")
|
||||
|
||||
except uldaq.ULException as e:
|
||||
print('\n', e)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
typer.run(connect)
|
||||
|
||||
0
pyrelacs/repros/__init__.py
Normal file
0
pyrelacs/repros/__init__.py
Normal file
275
pyrelacs/repros/calibration/calibration.py
Normal file
275
pyrelacs/repros/calibration/calibration.py
Normal file
@@ -0,0 +1,275 @@
|
||||
import faulthandler
|
||||
import time
|
||||
|
||||
import nixio as nix
|
||||
import uldaq
|
||||
from IPython import embed
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from scipy.signal import welch, find_peaks
|
||||
import pyqtgraph as pg
|
||||
|
||||
from pyrelacs.devices.mccdaq import MccDaq
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
log = config_logging()
|
||||
# for more information on seg faults
|
||||
faulthandler.enable()
|
||||
|
||||
|
||||
class Calibration:
|
||||
def __init__(self, config, mccdaq: MccDaq) -> None:
|
||||
self.config = config
|
||||
self.mccdaq = mccdaq
|
||||
self.SAMPLERATE = 40_000.0
|
||||
self.DURATION = 5
|
||||
self.AMPLITUDE = 1
|
||||
self.SINFREQ = 750
|
||||
|
||||
@staticmethod
|
||||
def run(*args, **kwargs):
|
||||
nix_block = args[0]
|
||||
figure = args[1]
|
||||
mccdaq = args[2]
|
||||
config = args[3]
|
||||
calb = Calibration(config, mccdaq)
|
||||
calb.check_beat(nix_block)
|
||||
calb.plot(figure, nix_block)
|
||||
|
||||
return "finished"
|
||||
|
||||
def check_amplitude(self):
|
||||
db_values = [0.0, -5.0, -10.0, -20.0, -50.0]
|
||||
colors = ["red", "green", "blue", "black", "yellow"]
|
||||
self.mccdaq.set_attenuation_level(db_channel1=0.0, db_channel2=0.0)
|
||||
# write to ananlog 1
|
||||
t = np.arange(0, self.DURATION, 1 / self.SAMPLERATE)
|
||||
data = self.AMPLITUDE * np.sin(2 * np.pi * self.SINFREQ * t)
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
for i, db_value in enumerate(db_values):
|
||||
self.mccdaq.set_attenuation_level(
|
||||
db_channel1=db_value, db_channel2=db_value
|
||||
)
|
||||
log.debug(f"{db_value}")
|
||||
|
||||
stim = self.mccdaq.write_analog(
|
||||
data,
|
||||
[0, 0],
|
||||
self.SAMPLERATE,
|
||||
ScanOption=uldaq.ScanOption.EXTTRIGGER,
|
||||
)
|
||||
|
||||
data_channel_one = self.mccdaq.read_analog(
|
||||
[0, 0],
|
||||
self.DURATION,
|
||||
self.SAMPLERATE,
|
||||
ScanOption=uldaq.ScanOption.EXTTRIGGER,
|
||||
)
|
||||
time.sleep(1)
|
||||
|
||||
log.debug("Starting the Scan")
|
||||
self.mccdaq.digital_trigger()
|
||||
|
||||
try:
|
||||
self.mccdaq.ao_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, 15)
|
||||
log.debug("Scan finished")
|
||||
self.mccdaq.write_bit(channel=0, bit=0)
|
||||
time.sleep(1)
|
||||
self.mccdaq.set_analog_to_zero()
|
||||
except uldaq.ul_exception.ULException:
|
||||
log.debug("Operation timed out")
|
||||
# reset the diggital trigger
|
||||
self.mccdaq.write_bit(channel=0, bit=0)
|
||||
time.sleep(1)
|
||||
self.mccdaq.set_analog_to_zero()
|
||||
# self.mccdaq.disconnect_daq()
|
||||
|
||||
if i == 0:
|
||||
ax.plot(t, stim, label=f"Input_{db_value}", color=colors[i])
|
||||
ax.plot(t, data_channel_one, label=f"Reaout {db_value}", color=colors[i])
|
||||
|
||||
ax.legend()
|
||||
plt.show()
|
||||
|
||||
# self.mccdaq.disconnect_daq()
|
||||
|
||||
def check_beat(self, nix_block: nix.Block):
|
||||
self.mccdaq.set_attenuation_level(db_channel1=-10.0, db_channel2=0.0)
|
||||
t = np.arange(0, self.DURATION, 1 / self.SAMPLERATE)
|
||||
data = self.AMPLITUDE * np.sin(2 * np.pi * self.SINFREQ * t)
|
||||
# data = np.concatenate((data, data))
|
||||
db_values = [0.0, -5.0, -8.5, -10.0]
|
||||
colors = ["red", "blue", "black", "green"]
|
||||
colors_in = ["lightcoral", "lightblue", "grey", "lightgreen"]
|
||||
# fig, axes = plt.subplots(2, 2, sharex="col")
|
||||
for i, db_value in enumerate(db_values):
|
||||
self.mccdaq.set_attenuation_level(db_channel1=db_value)
|
||||
stim = self.mccdaq.write_analog(
|
||||
data,
|
||||
[0, 0],
|
||||
self.SAMPLERATE,
|
||||
ScanOption=uldaq.ScanOption.EXTTRIGGER,
|
||||
)
|
||||
readout = self.mccdaq.read_analog(
|
||||
[0, 1],
|
||||
self.DURATION,
|
||||
self.SAMPLERATE,
|
||||
ScanOption=uldaq.ScanOption.EXTTRIGGER,
|
||||
)
|
||||
self.mccdaq.digital_trigger()
|
||||
log.info(self.mccdaq.ao_device)
|
||||
ai_status = uldaq.ScanStatus.RUNNING
|
||||
ao_status = uldaq.ScanStatus.RUNNING
|
||||
|
||||
log.debug(
|
||||
f"Status Analog_output {ao_status}\n, Status Analog_input {ai_status}"
|
||||
)
|
||||
while (ai_status != uldaq.ScanStatus.IDLE) and (
|
||||
ao_status != uldaq.ScanStatus.IDLE
|
||||
):
|
||||
# log.debug("Scanning")
|
||||
time.time_ns()
|
||||
ai_status = self.mccdaq.ai_device.get_scan_status()[0]
|
||||
ao_status = self.mccdaq.ao_device.get_scan_status()[0]
|
||||
|
||||
self.mccdaq.write_bit(channel=0, bit=0)
|
||||
log.debug(
|
||||
f"Status Analog_output {ao_status}\n, Status Analog_input {ai_status}"
|
||||
)
|
||||
|
||||
channel1 = np.array(readout[::2])
|
||||
channel2 = np.array(readout[1::2])
|
||||
|
||||
stim_data = nix_block.create_data_array(
|
||||
f"stimulus_{db_value}",
|
||||
"nix.regular_sampled",
|
||||
shape=data.shape,
|
||||
data=channel1,
|
||||
label="Voltage",
|
||||
unit="V",
|
||||
)
|
||||
stim_data.append_sampled_dimension(
|
||||
self.SAMPLERATE,
|
||||
label="time",
|
||||
unit="s",
|
||||
)
|
||||
fish_data = nix_block.create_data_array(
|
||||
f"fish_{db_value}",
|
||||
"Array",
|
||||
shape=data.shape,
|
||||
data=channel2,
|
||||
label="Voltage",
|
||||
unit="V",
|
||||
)
|
||||
fish_data.append_sampled_dimension(
|
||||
self.SAMPLERATE,
|
||||
label="time",
|
||||
unit="s",
|
||||
)
|
||||
|
||||
time.time_ns()
|
||||
self.mccdaq.set_analog_to_zero()
|
||||
|
||||
def plot(self, figure, block):
|
||||
self.figure = figure
|
||||
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)
|
||||
|
||||
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 = 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 = 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 = 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(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
|
||||
0
pyrelacs/repros/calibration/config.yaml
Normal file
0
pyrelacs/repros/calibration/config.yaml
Normal file
82
pyrelacs/repros/repros.py
Normal file
82
pyrelacs/repros/repros.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import sys
|
||||
import ast
|
||||
import pathlib
|
||||
from typing import Tuple
|
||||
|
||||
from IPython import embed
|
||||
import nixio as nix
|
||||
import importlib.util
|
||||
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
log = config_logging()
|
||||
|
||||
|
||||
class Repro:
|
||||
"""
|
||||
Repro Class that searches in the repro folder for classes instances and executes the
|
||||
the run function in the searched class
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def run_repro(self, name: str, file: pathlib.Path, *args, **kwargs) -> None:
|
||||
spec = importlib.util.spec_from_file_location("rep", file)
|
||||
if not spec:
|
||||
log.error("Could not load the file")
|
||||
else:
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
if not module:
|
||||
log.error("Could not load the module of the repro")
|
||||
else:
|
||||
sys.modules[name] = module
|
||||
if spec.loader is not None:
|
||||
spec.loader.exec_module(module)
|
||||
else:
|
||||
log.error(f"{spec.loader} is None")
|
||||
if hasattr(module, name):
|
||||
rep_class = getattr(module, name)
|
||||
rep_class.run(*args, **kwargs)
|
||||
else:
|
||||
raise AttributeError(f"{file.name} has no {name} class")
|
||||
|
||||
def names_of_repros(self, include_repros: list[str]) -> Tuple[list, list]:
|
||||
"""
|
||||
Searches for class names in the repro folder in all python files
|
||||
|
||||
Parameters
|
||||
----------
|
||||
include_repros : list[str]
|
||||
List of repros to include in the pyrelacs instance
|
||||
|
||||
Returns
|
||||
-------
|
||||
Tuple[list, list]
|
||||
list of class names
|
||||
list of file names from the class names
|
||||
"""
|
||||
|
||||
file_path_cur = pathlib.Path(__file__).parent
|
||||
python_files = list(file_path_cur.glob("**/*.py"))
|
||||
exclude_files = ["repros.py", "__init__.py"]
|
||||
python_files = [f for f in python_files if f.name not in exclude_files]
|
||||
repro_names = []
|
||||
file_names = []
|
||||
for python_file in python_files:
|
||||
with open(python_file, "r") as file:
|
||||
file_content = file.read()
|
||||
tree = ast.parse(file_content)
|
||||
class_name = [
|
||||
node.name
|
||||
for node in ast.walk(tree)
|
||||
if isinstance(node, ast.ClassDef)
|
||||
]
|
||||
repro_names.extend(class_name)
|
||||
file_names.append(python_file)
|
||||
file.close()
|
||||
|
||||
repro_names = [r for r in repro_names if r in include_repros]
|
||||
file_names = [f for r, f in zip(repro_names, file_names) if r in include_repros]
|
||||
return repro_names, file_names
|
||||
17
pyrelacs/repros/sinus/sinus.py
Normal file
17
pyrelacs/repros/sinus/sinus.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import nixio
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
|
||||
log = config_logging()
|
||||
|
||||
|
||||
class Sinus:
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def run(config, mccdaq, nix_block: nixio.Block, figure) -> None:
|
||||
log.debug(config)
|
||||
log.debug(mccdaq)
|
||||
log.debug(nix_block)
|
||||
log.debug(figure)
|
||||
10
pyrelacs/resources.qrc
Normal file
10
pyrelacs/resources.qrc
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource>
|
||||
<file>icons/exit.png</file>
|
||||
<file>icons/connect.png</file>
|
||||
<file>icons/disconnect.png</file>
|
||||
<file>icons/record.png</file>
|
||||
<file>icons/stop.png</file>
|
||||
<file>icons/relacstuxheader.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
0
pyrelacs/ui/__init__.py
Normal file
0
pyrelacs/ui/__init__.py
Normal file
54
pyrelacs/ui/about.py
Normal file
54
pyrelacs/ui/about.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from PyQt6.QtGui import QPixmap
|
||||
from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QLabel, QVBoxLayout, QWidget
|
||||
from PyQt6.QtCore import Qt
|
||||
|
||||
|
||||
class AboutDialog(QDialog):
|
||||
|
||||
def __init__(self, parent=None) -> None:
|
||||
super().__init__(parent=parent)
|
||||
self.setModal(True)
|
||||
about = About(self)
|
||||
self.setLayout(QVBoxLayout())
|
||||
self.layout().addWidget(about)
|
||||
bbox = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok)
|
||||
bbox.accepted.connect(self.accept)
|
||||
self.layout().addWidget(bbox)
|
||||
|
||||
|
||||
class About(QWidget):
|
||||
|
||||
def __init__(self, parent=None) -> None:
|
||||
super().__init__(parent=parent)
|
||||
self.setLayout(QVBoxLayout())
|
||||
|
||||
heading = QLabel("pyRelacs")
|
||||
font = heading.font()
|
||||
font.setPointSize(18)
|
||||
font.setBold(True)
|
||||
heading.setFont(font)
|
||||
heading.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
subheading = QLabel("relacsed electrophysiological recordings")
|
||||
subheading.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
nix_link = QLabel("https://github.com/relacs")
|
||||
nix_link.setOpenExternalLinks(True)
|
||||
nix_link.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
rtd_link = QLabel("https://relacs.net")
|
||||
rtd_link.setOpenExternalLinks(True)
|
||||
rtd_link.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
|
||||
iconlabel = QLabel()
|
||||
pixmap = QPixmap(":/icons/relacstuxheader.png")
|
||||
s = pixmap.size()
|
||||
new_height = int(s.height() * 300/s.width())
|
||||
pixmap = pixmap.scaled(300, new_height, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.FastTransformation)
|
||||
iconlabel.setPixmap(pixmap)
|
||||
iconlabel.setMaximumWidth(300)
|
||||
iconlabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
iconlabel.setScaledContents(True)
|
||||
|
||||
self.layout().addWidget(heading)
|
||||
self.layout().addWidget(subheading)
|
||||
self.layout().addWidget(iconlabel)
|
||||
self.layout().addWidget(nix_link)
|
||||
self.layout().addWidget(rtd_link)
|
||||
391
pyrelacs/ui/mainwindow.py
Normal file
391
pyrelacs/ui/mainwindow.py
Normal file
@@ -0,0 +1,391 @@
|
||||
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
|
||||
from PyQt6.QtWidgets import (
|
||||
QGridLayout,
|
||||
QPushButton,
|
||||
QTabWidget,
|
||||
QToolBar,
|
||||
QVBoxLayout,
|
||||
QWidget,
|
||||
QMainWindow,
|
||||
QPlainTextEdit,
|
||||
QMenuBar,
|
||||
QStatusBar,
|
||||
)
|
||||
|
||||
|
||||
import nixio
|
||||
import pyqtgraph as pg
|
||||
import quantities as pq
|
||||
|
||||
from pyrelacs.devices.mccdaq import MccDaq
|
||||
|
||||
from pyrelacs.dataio.circbuffer import CircBuffer
|
||||
from pyrelacs.dataio.daq_producer import DaqProducer
|
||||
from pyrelacs.dataio.nix_writer import NixWriter
|
||||
from pyrelacs.dataio.sin_producer import SinProducer
|
||||
|
||||
from pyrelacs.worker import Worker
|
||||
from pyrelacs.repros.repros import Repro
|
||||
|
||||
from pyrelacs.ui.about import AboutDialog
|
||||
from pyrelacs.ui.plots.calibration import CalibrationPlot
|
||||
from pyrelacs.ui.plots.continously import Continously
|
||||
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
log = config_logging()
|
||||
_root = path(__file__).parent.parent
|
||||
|
||||
from IPython import embed
|
||||
|
||||
|
||||
class PyRelacs(QMainWindow):
|
||||
def __init__(self, config):
|
||||
super().__init__()
|
||||
# loaded config
|
||||
self.config = config
|
||||
if self.config.settings.daq:
|
||||
start = time.time()
|
||||
self.mccdaq = MccDaq()
|
||||
end = time.time()
|
||||
log.debug(f"Connection to DAQ took {end - start}")
|
||||
else:
|
||||
self.mccdaq = None
|
||||
|
||||
self.repros = Repro()
|
||||
|
||||
self.setToolButtonStyle(
|
||||
Qt.ToolButtonStyle.ToolButtonTextBesideIcon
|
||||
) # Ensure icons are displayed with text
|
||||
self.setWindowTitle("PyRelacs")
|
||||
|
||||
self.create_nix_file(f"{_root}/test.nix", self.config.metadata)
|
||||
|
||||
self.mutex = QMutex()
|
||||
|
||||
self.figure = pg.GraphicsLayoutWidget()
|
||||
|
||||
self.threadpool = QThreadPool()
|
||||
|
||||
self.text = QPlainTextEdit()
|
||||
self.text.setReadOnly(True)
|
||||
|
||||
self.setMenuBar(QMenuBar(self))
|
||||
self.setStatusBar(QStatusBar(self))
|
||||
self.create_actions()
|
||||
self.create_toolbars()
|
||||
self.repro_tabs = QTabWidget()
|
||||
self.create_repros_tabs()
|
||||
|
||||
layout = QGridLayout()
|
||||
layout.addWidget(self.figure, 0, 0, 2, 2)
|
||||
layout.addWidget(self.repro_tabs, 2, 0, 2, 2)
|
||||
layout.addWidget(self.text, 4, 0, 1, 1)
|
||||
|
||||
widget = QWidget()
|
||||
widget.setLayout(layout)
|
||||
self.setCentralWidget(widget)
|
||||
|
||||
SAMPLERATE = pq.Quantity(
|
||||
self.config.pyrelacs.data.input.inputsamplerate,
|
||||
self.config.pyrelacs.data.input.inputsamplerateunit,
|
||||
).rescale("Hz")
|
||||
|
||||
INPUTTRACECAPACITY = pq.Quantity(
|
||||
self.config.pyrelacs.data.input.inputtracecapacity,
|
||||
self.config.pyrelacs.data.input.inputtracecapacityunit,
|
||||
).rescale("s")
|
||||
start = time.time()
|
||||
BUFFERSIZE = (SAMPLERATE * INPUTTRACECAPACITY).simplified
|
||||
end = time.time()
|
||||
log.debug(f"Buffer allocation took {end - start}")
|
||||
|
||||
self.buffer = CircBuffer(
|
||||
size=int(BUFFERSIZE.base),
|
||||
samplerate=float(SAMPLERATE.base),
|
||||
mutex=self.mutex,
|
||||
)
|
||||
self.continously_plot = Continously(self.figure, self.buffer)
|
||||
# self.continously_plot.plot()
|
||||
|
||||
if self.mccdaq:
|
||||
log.debug("Creating Daq Generator")
|
||||
self.daq_producer = DaqProducer(self.buffer, self.mccdaq.daq_device, [1, 1])
|
||||
log.debug("Creating Sinus Generator")
|
||||
self.sinus_producer = SinProducer(self.buffer)
|
||||
|
||||
self.nix_writer = NixWriter(self.buffer)
|
||||
|
||||
def create_actions(self):
|
||||
self._rlx_exitaction = QAction(QIcon(":/icons/exit.png"), "Exit", self)
|
||||
self._rlx_exitaction.setStatusTip("Close relacs")
|
||||
self._rlx_exitaction.setShortcut(QKeySequence("Alt+q"))
|
||||
self._rlx_exitaction.triggered.connect(self.on_exit)
|
||||
|
||||
self._rlx_aboutaction = QAction("about")
|
||||
self._rlx_aboutaction.setStatusTip("Show about dialog")
|
||||
self._rlx_aboutaction.setEnabled(True)
|
||||
self._rlx_aboutaction.triggered.connect(self.on_about)
|
||||
|
||||
self._daq_connectaction = QAction(
|
||||
QIcon(":icons/connect.png"), "Connect DAQ", self
|
||||
)
|
||||
if self.mccdaq:
|
||||
self._daq_connectaction.setStatusTip("Connect to daq device")
|
||||
# self._daq_connectaction.setShortcut(QKeySequence("Alt+d"))
|
||||
self._daq_connectaction.triggered.connect(self.mccdaq.connect_dac)
|
||||
|
||||
self._daq_disconnectaction = QAction(
|
||||
QIcon(":/icons/disconnect.png"), "Disconnect DAQ", self
|
||||
)
|
||||
self._daq_disconnectaction.setStatusTip("Disconnect the DAQ device")
|
||||
# self._daq_connectaction.setShortcut(QKeySequence("Alt+d"))
|
||||
self._daq_disconnectaction.triggered.connect(self.mccdaq.disconnect_daq)
|
||||
|
||||
# self._daq_calibaction = QAction(
|
||||
# QIcon(":/icons/calibration.png"), "Plot calibration", self
|
||||
# )
|
||||
# self._daq_calibaction.setStatusTip("Calibrate the attenuator device")
|
||||
# # self._daq_calibaction.setShortcut(QKeySequence("Alt+d"))
|
||||
# self._daq_calibaction.triggered.connect(self.calibration_plot.plot)
|
||||
|
||||
self._run_action = QAction(QIcon(":/icons/record.png"), "RunDAQ", self)
|
||||
self._run_action.triggered.connect(self.run_daq)
|
||||
|
||||
self._run_sinus_action = QAction(QIcon(":/icons/record.png"), "Sinus", self)
|
||||
self._run_sinus_action.triggered.connect(self.run_sinus)
|
||||
|
||||
self._stop_recording = QAction(QIcon(":/icons/stop.png"), "Stop", self)
|
||||
self._stop_recording.triggered.connect(self.stop_recording)
|
||||
|
||||
self._recenter_plot = QAction("Recenter", self)
|
||||
self._recenter_plot.triggered.connect(self.recenter_continously_plot)
|
||||
self._recenter_plot.setShortcut(QKeySequence("Alt+r"))
|
||||
|
||||
self._record = QAction(QIcon(":/icons/record.png"), "Record", self)
|
||||
self._record.triggered.connect(self.record)
|
||||
|
||||
self.create_menu()
|
||||
|
||||
def create_menu(self):
|
||||
menu = self.menuBar()
|
||||
if menu is not None:
|
||||
file_menu = menu.addMenu("&File")
|
||||
device_menu = menu.addMenu("&DAQ")
|
||||
help_menu = menu.addMenu("&Help")
|
||||
|
||||
if file_menu is not None:
|
||||
file_menu.addAction(self._rlx_exitaction)
|
||||
file_menu.addAction(self._rlx_aboutaction)
|
||||
|
||||
if device_menu is not None:
|
||||
if self.config.settings.daq:
|
||||
device_menu.addAction(self._daq_connectaction)
|
||||
device_menu.addAction(self._daq_disconnectaction)
|
||||
device_menu.addSeparator()
|
||||
# device_menu.addAction(self._daq_calibaction)
|
||||
device_menu.addAction(self._run_action)
|
||||
|
||||
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()
|
||||
|
||||
self.setMenuBar(menu)
|
||||
|
||||
def create_toolbars(self):
|
||||
rlx_toolbar = QToolBar("Relacs")
|
||||
rlx_toolbar.addAction(self._rlx_exitaction)
|
||||
rlx_toolbar.setIconSize(QSize(24, 24))
|
||||
|
||||
self.addToolBar(Qt.ToolBarArea.TopToolBarArea, rlx_toolbar)
|
||||
daq_toolbar = QToolBar("DAQ")
|
||||
if self.config.settings.daq:
|
||||
daq_toolbar.addAction(self._daq_connectaction)
|
||||
daq_toolbar.addAction(self._daq_disconnectaction)
|
||||
# daq_toolbar.addAction(self._daq_calibaction)
|
||||
daq_toolbar.addAction(self._run_action)
|
||||
daq_toolbar.addAction(self._run_sinus_action)
|
||||
daq_toolbar.addAction(self._stop_recording)
|
||||
daq_toolbar.addAction(self._recenter_plot)
|
||||
daq_toolbar.addSeparator()
|
||||
daq_toolbar.addAction(self._record)
|
||||
self.addToolBar(Qt.ToolBarArea.TopToolBarArea, daq_toolbar)
|
||||
|
||||
def create_repros_tabs(self):
|
||||
repro_names, file_names = self.repros.names_of_repros(
|
||||
include_repros=self.config.settings.repros
|
||||
)
|
||||
nix_blocks = {
|
||||
rep: self.nix_file.create_block(f"{rep}", "Data Repro")
|
||||
for rep in repro_names
|
||||
}
|
||||
figures_repros = {rep: pg.GraphicsLayoutWidget() for rep in repro_names}
|
||||
for rep, fn in zip(repro_names, file_names):
|
||||
tab = QWidget()
|
||||
tab_layout = QGridLayout()
|
||||
|
||||
run_repro_button = QPushButton(f"Run {rep}")
|
||||
run_repro_button.setCheckable(True)
|
||||
run_repro_button.clicked.connect(
|
||||
lambda checked, n=rep, f=fn: self.run_repro(
|
||||
n,
|
||||
f,
|
||||
nix_blocks,
|
||||
figures_repros,
|
||||
self.mccdaq,
|
||||
self.config,
|
||||
)
|
||||
)
|
||||
tab_layout.addWidget(run_repro_button, 0, 0, 1, 0)
|
||||
tab_layout.addWidget(figures_repros[rep], 1, 0, 1, 1)
|
||||
tab.setLayout(tab_layout)
|
||||
self.repro_tabs.addTab(tab, f"{rep}")
|
||||
|
||||
def run_repro(
|
||||
self,
|
||||
name_of_repro: str,
|
||||
file_of_repro: str,
|
||||
nix_block,
|
||||
figures,
|
||||
*args,
|
||||
):
|
||||
self.text.appendPlainText(f"started Repro {name_of_repro}, {file_of_repro}")
|
||||
nix_block_repro = nix_block[name_of_repro]
|
||||
figure_repro = figures[name_of_repro]
|
||||
worker = Worker(
|
||||
self.repros.run_repro,
|
||||
name_of_repro,
|
||||
file_of_repro,
|
||||
nix_block_repro,
|
||||
figure_repro,
|
||||
*args[-2:],
|
||||
)
|
||||
worker.signals.result.connect(self.print_output)
|
||||
worker.signals.finished.connect(self.thread_complete)
|
||||
worker.signals.progress.connect(self.progress_fn)
|
||||
|
||||
self.threadpool.start(worker)
|
||||
|
||||
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()
|
||||
|
||||
def plot_continously(self):
|
||||
plot_con = Worker(self.continously_plot.plot)
|
||||
plot_con.signals.result.connect(self.print_output)
|
||||
plot_con.signals.finished.connect(self.thread_complete)
|
||||
plot_con.signals.progress.connect(self.progress_fn)
|
||||
self.threadpool.start(plot_con)
|
||||
|
||||
def run_daq(self):
|
||||
read_daq = Worker(self.daq_producer.read_analog_continously)
|
||||
read_daq.signals.result.connect(self.print_output)
|
||||
read_daq.signals.finished.connect(self.thread_complete)
|
||||
read_daq.signals.progress.connect(self.progress_fn)
|
||||
self.threadpool.start(read_daq)
|
||||
|
||||
self.continously_plot.plot()
|
||||
|
||||
def run_sinus(self):
|
||||
sinus_pro = Worker(self.sinus_producer.produce_sin)
|
||||
sinus_pro.signals.result.connect(self.print_output)
|
||||
sinus_pro.signals.finished.connect(self.thread_complete)
|
||||
sinus_pro.signals.progress.connect(self.progress_fn)
|
||||
self.threadpool.start(sinus_pro)
|
||||
|
||||
self.continously_plot.plot()
|
||||
|
||||
def record(self):
|
||||
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()
|
||||
self.nix_writer.stop_writing()
|
||||
|
||||
log.debug("Stopping acquisiton")
|
||||
try:
|
||||
self.sinus_producer.stop_request()
|
||||
log.debug("Stopping Sinus")
|
||||
except AttributeError:
|
||||
log.debug("Did not generate Sinus")
|
||||
|
||||
if hasattr(PyRelacs, "daq_device"):
|
||||
log.debug("Stopping DAQ")
|
||||
self.daq_producer.stop_aquisition()
|
||||
|
||||
def add_to_textfield(self, s: str):
|
||||
self.text.appendPlainText(s)
|
||||
|
||||
def on_exit(self):
|
||||
log.info("exit button!")
|
||||
self.stop_recording()
|
||||
self.add_to_textfield("exiting")
|
||||
if self.mccdaq:
|
||||
self.mccdaq.disconnect_daq()
|
||||
log.info("closing GUI")
|
||||
self.close()
|
||||
|
||||
def on_about(self, e):
|
||||
about = AboutDialog(self)
|
||||
about.show()
|
||||
|
||||
def print_output(self, s):
|
||||
log.info(s)
|
||||
self.add_to_textfield(s)
|
||||
|
||||
def thread_complete(self):
|
||||
log.info("Thread complete!")
|
||||
self.add_to_textfield("Thread complete!")
|
||||
|
||||
def progress_fn(self, n):
|
||||
print("%d%% done" % n)
|
||||
113
pyrelacs/ui/plots/calibration.py
Normal file
113
pyrelacs/ui/plots/calibration.py
Normal 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
|
||||
88
pyrelacs/ui/plots/continously.py
Normal file
88
pyrelacs/ui/plots/continously.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import time
|
||||
|
||||
import pyqtgraph as pg
|
||||
from IPython import embed
|
||||
import numpy as np
|
||||
from pyqtgraph.Qt.QtCore import QTimer
|
||||
|
||||
from pyrelacs.dataio.circbuffer import CircBuffer
|
||||
from pyrelacs.util.logging import config_logging
|
||||
|
||||
log = config_logging()
|
||||
|
||||
|
||||
class Continously:
|
||||
def __init__(self, figure: pg.GraphicsLayoutWidget, buffer: CircBuffer):
|
||||
self.figure = figure
|
||||
self.buffer = buffer
|
||||
self.last_plotted_index = 0
|
||||
self.timer = QTimer()
|
||||
|
||||
def plot(self, *args, **kwargs):
|
||||
self.figure.setBackground("w")
|
||||
|
||||
prev_plot = self.figure.getItem(row=0, col=0)
|
||||
if prev_plot:
|
||||
self.figure.removeItem(prev_plot)
|
||||
self.continous_ax = self.figure.addPlot(row=0, col=0)
|
||||
|
||||
pen = pg.mkPen("red")
|
||||
self.time = np.zeros(self.buffer.size)
|
||||
self.data = np.empty(self.buffer.size)
|
||||
self.line = self.continous_ax.plot(
|
||||
self.time,
|
||||
self.data,
|
||||
pen=pen,
|
||||
# symbol="o",
|
||||
)
|
||||
|
||||
# self.plot_index = 0
|
||||
self.CHUNK_PLOT = int(self.buffer.samplerate / 6)
|
||||
self.PLOT_HISTORY = 500_000 # The amount of data you want to keep on screen
|
||||
self.timer.setInterval(150)
|
||||
self.timer.timeout.connect(self.update_plot)
|
||||
self.timer.start()
|
||||
|
||||
def update_plot(self):
|
||||
current_index = self.buffer.write_index()
|
||||
total_count = self.buffer.totalcount()
|
||||
|
||||
start_time = time.time()
|
||||
if total_count - 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:
|
||||
pass
|
||||
|
||||
def stop_plotting(self):
|
||||
self.timer.stop()
|
||||
if self.last_plotted_index > 0:
|
||||
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()
|
||||
0
pyrelacs/util/__init__.py
Normal file
0
pyrelacs/util/__init__.py
Normal file
25
pyrelacs/util/logging.py
Normal file
25
pyrelacs/util/logging.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import pathlib
|
||||
import logging
|
||||
from rich.logging import RichHandler
|
||||
|
||||
logger = logging.getLogger("pyrelacs")
|
||||
|
||||
|
||||
def config_logging():
|
||||
if logger.hasHandlers():
|
||||
logger.handlers.clear()
|
||||
|
||||
stream_handler = RichHandler()
|
||||
logger.setLevel(level="DEBUG")
|
||||
stream_handler.setLevel(level="DEBUG")
|
||||
|
||||
fmt_shell = "%(message)s"
|
||||
|
||||
shell_formatter = logging.Formatter(fmt_shell)
|
||||
|
||||
# here we hook everything together
|
||||
stream_handler.setFormatter(shell_formatter)
|
||||
|
||||
logger.addHandler(stream_handler)
|
||||
|
||||
return logger
|
||||
75
pyrelacs/worker.py
Normal file
75
pyrelacs/worker.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
from PyQt6.QtCore import QRunnable, pyqtSlot, QObject, pyqtSignal
|
||||
|
||||
|
||||
class WorkerSignals(QObject):
|
||||
"""
|
||||
Defines the signals available from a running worker thread.
|
||||
|
||||
Supported signals are:
|
||||
|
||||
finished
|
||||
No data
|
||||
|
||||
error
|
||||
tuple (exctype, value, traceback.format_exc() )
|
||||
|
||||
result
|
||||
object data returned from processing, anything
|
||||
|
||||
progress
|
||||
int indicating % progress
|
||||
|
||||
"""
|
||||
|
||||
finished = pyqtSignal()
|
||||
error = pyqtSignal(tuple)
|
||||
result = pyqtSignal(object)
|
||||
progress = pyqtSignal(int)
|
||||
|
||||
|
||||
class Worker(QRunnable):
|
||||
"""
|
||||
Worker thread
|
||||
|
||||
Inherits from QRunnable to handler worker thread setup, signals and wrap-up.
|
||||
|
||||
:param callback: The function callback to run on this worker thread. Supplied args and
|
||||
kwargs will be passed through to the runner.
|
||||
:type callback: function
|
||||
:param args: Arguments to pass to the callback function
|
||||
:param kwargs: Keywords to pass to the callback function
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, fn, *args, **kwargs):
|
||||
super(Worker, self).__init__()
|
||||
|
||||
# Store constructor arguments (re-used for processing)
|
||||
self.fn = fn
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
self.signals = WorkerSignals()
|
||||
|
||||
# Add the callback to our kwargs
|
||||
self.kwargs["progress_callback"] = self.signals.progress
|
||||
|
||||
@pyqtSlot()
|
||||
def run(self):
|
||||
"""
|
||||
Initialise the runner function with passed args, kwargs.
|
||||
"""
|
||||
|
||||
# Retrieve args/kwargs here; and fire processing using them
|
||||
try:
|
||||
result = self.fn(*self.args, **self.kwargs)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
exctype, value = sys.exc_info()[:2]
|
||||
self.signals.error.emit((exctype, value, traceback.format_exc()))
|
||||
else:
|
||||
self.signals.result.emit(result) # Return the result of the processing
|
||||
finally:
|
||||
self.signals.finished.emit() # Done
|
||||
98
test/test_buffer.py
Normal file
98
test/test_buffer.py
Normal 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()
|
||||
Reference in New Issue
Block a user