From 34d4d1f8a03141d1a603fefb153a2c43f9f0ee1c Mon Sep 17 00:00:00 2001 From: crnbaker Date: Wed, 26 Jun 2024 11:44:46 +0100 Subject: [PATCH] #62 raw voltage option in convenience acquisition methods --- setup.cfg | 2 +- .../digitiser/abstract_spectrum_digitiser.py | 23 +++++++++++++------ .../devices/digitiser/digitiser_interface.py | 8 +++---- src/spectrumdevice/measurement.py | 10 +++++--- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/setup.cfg b/setup.cfg index 8cecb65..c0db2ea 100644 --- a/setup.cfg +++ b/setup.cfg @@ -34,7 +34,7 @@ package_dir = = src include_package_data = True install_requires = - numpy>=1.21.4 + numpy>=1.26.2 python_requires = >=3.8 [options.packages.find] diff --git a/src/spectrumdevice/devices/digitiser/abstract_spectrum_digitiser.py b/src/spectrumdevice/devices/digitiser/abstract_spectrum_digitiser.py index 3e445ae..d9e9382 100644 --- a/src/spectrumdevice/devices/digitiser/abstract_spectrum_digitiser.py +++ b/src/spectrumdevice/devices/digitiser/abstract_spectrum_digitiser.py @@ -7,7 +7,7 @@ from abc import ABC from typing import List -from spectrumdevice.measurement import Measurement +from spectrumdevice.measurement import Measurement, RawWaveformType, VoltageWaveformType from spectrumdevice.devices.abstract_device import AbstractSpectrumDevice from spectrumdevice.devices.digitiser.digitiser_interface import ( SpectrumDigitiserAnalogChannelInterface, @@ -76,7 +76,7 @@ def configure_acquisition(self, settings: AcquisitionSettings) -> None: if settings.timestamping_enabled: self.enable_timestamping() - def execute_standard_single_acquisition(self) -> Measurement: + def execute_standard_single_acquisition(self, raw: bool = False) -> Measurement: """Carry out a single measurement in standard single mode and return the acquired waveforms. This method automatically carries out a standard single mode acquisition, including handling the creation @@ -84,6 +84,10 @@ def execute_standard_single_acquisition(self) -> Measurement: trigger event is received before carrying out the acquisition and then transferring and returning the acquired waveforms. The device must be configured in SPC_REC_STD_SINGLE acquisition mode. + Args: + raw (bool, optional): Set to true to obtain raw (i.e. 16-bit integer) waveforms, instead of floating point + voltage waveforms. + Returns: measurement (Measurement): A Measurement object. The `.waveforms` attribute of `measurement` will be a list of 1D NumPy arrays, each array containing the waveform data received on one channel, in channel order. @@ -101,11 +105,13 @@ def execute_standard_single_acquisition(self) -> Measurement: self.define_transfer_buffer() self.start_transfer() self.wait_for_transfer_chunk_to_complete() - waveforms = self.get_waveforms()[0] + waveforms: list[RawWaveformType] | list[VoltageWaveformType] = ( + self.get_raw_waveforms()[0] if raw else self.get_waveforms()[0] + ) self.stop() # Only strictly required for Mock devices. Should not affect hardware. return Measurement(waveforms=waveforms, timestamp=self.get_timestamp()) - def execute_finite_fifo_acquisition(self, num_measurements: int) -> List[Measurement]: + def execute_finite_fifo_acquisition(self, num_measurements: int, raw: bool = False) -> List[Measurement]: """Carry out a finite number of FIFO mode measurements and then stop the acquisitions. This method automatically carries out a defined number of measurement in Multi FIFO mode, including handling the @@ -118,6 +124,8 @@ def execute_finite_fifo_acquisition(self, num_measurements: int) -> List[Measure Args: num_measurements (int): The number of measurements to carry out. + raw (bool, optional): Set to true to obtain raw (i.e. 16-bit integer) waveforms, instead of floating point + voltage waveforms. Returns: measurements (List[Measurement]): A list of Measurement objects with length `num_measurements`. Each Measurement object has a `waveforms` attribute containing a list of 1D NumPy arrays. Each array is a @@ -133,9 +141,10 @@ def execute_finite_fifo_acquisition(self, num_measurements: int) -> List[Measure self.execute_continuous_fifo_acquisition() measurements = [] for _ in range(num_measurements // self.batch_size): - measurements += [ - Measurement(waveforms=frame, timestamp=self.get_timestamp()) for frame in self.get_waveforms() - ] + waveforms: list[list[RawWaveformType]] | list[list[VoltageWaveformType]] = ( + self.get_raw_waveforms() if raw else self.get_waveforms() + ) + measurements += [Measurement(waveforms=frame, timestamp=self.get_timestamp()) for frame in waveforms] self.stop() return measurements diff --git a/src/spectrumdevice/devices/digitiser/digitiser_interface.py b/src/spectrumdevice/devices/digitiser/digitiser_interface.py index 4f1bf5a..160a9a4 100644 --- a/src/spectrumdevice/devices/digitiser/digitiser_interface.py +++ b/src/spectrumdevice/devices/digitiser/digitiser_interface.py @@ -8,14 +8,14 @@ from datetime import datetime from typing import List, Optional -from numpy import float_, int16, ndarray -from numpy.typing import NDArray +from numpy import ndarray from spectrumdevice.devices.abstract_device.device_interface import SpectrumDeviceInterface from spectrumdevice.devices.abstract_device.channel_interfaces import ( SpectrumAnalogChannelInterface, SpectrumIOLineInterface, ) +from spectrumdevice.measurement import VoltageWaveformType, RawWaveformType from spectrumdevice.settings import AcquisitionMode, AcquisitionSettings from spectrumdevice import Measurement from spectrumdevice.settings.channel import InputImpedance, InputCoupling, InputPath @@ -105,11 +105,11 @@ def execute_continuous_fifo_acquisition(self) -> None: raise NotImplementedError() @abstractmethod - def get_raw_waveforms(self) -> List[List[NDArray[int16]]]: + def get_raw_waveforms(self) -> List[List[RawWaveformType]]: raise NotImplementedError() @abstractmethod - def get_waveforms(self) -> List[List[NDArray[float_]]]: + def get_waveforms(self) -> List[List[VoltageWaveformType]]: raise NotImplementedError() @abstractmethod diff --git a/src/spectrumdevice/measurement.py b/src/spectrumdevice/measurement.py index afc4756..0f1f755 100644 --- a/src/spectrumdevice/measurement.py +++ b/src/spectrumdevice/measurement.py @@ -2,15 +2,19 @@ from dataclasses import dataclass from typing import List, Optional -from numpy import float_ +from numpy import float_, int16 from numpy.typing import NDArray +VoltageWaveformType = NDArray[float_] +RawWaveformType = NDArray[int16] + + @dataclass class Measurement: """Measurement is a dataclass for storing a set of waveforms generated by a single acquisition, with a timestamp.""" - waveforms: List[NDArray[float_]] - """Contains the acquired waveforms as a list of 1D NumPy arrays""" + waveforms: List[VoltageWaveformType] | List[RawWaveformType] + """Contains the acquired waveforms as a list of 1D NumPy arrays or either floats or ints""" timestamp: Optional[datetime] """The time at which the acquisition was triggered, as a datetime.datetime object"""