PymeasureInstrument#
- class PymeasureInstrument(
- name: str,
- instrument: T,
- publishers: list[Publisher] | None = None,
- *,
- channel_map: ChannelMap | dict[str, Any] | str | Path | None = None,
- poll: list[str] | None = None,
- device_attr: str = 'device',
- close_device: bool = True,
- background_config: BackgroundDaemonConfig | None = None,
- **kwargs: Any,
Bases:
Instrument,Generic[T]An instro
Instrumentthat publishes the traffic of a pymeasure instrument.Every property assignment on the wrapped instrument (and its channels) publishes a
Commandon<name>.<channel>.cmd; every property read publishes aMeasurementon<name>.<channel>. This includes property access inside the driver’s own methods, such asapply_voltage().The pymeasure object keeps working exactly as before and stays an instance of its class: the wrapper swaps in a generated subclass, and
close(ordetach) swaps the original back.Wrap
instrumentso its commands and readings reachpublishers.Example:
from pymeasure.instruments.keysight import KeysightE36312A from instro.lib import FilePublisher from instro.pymeasure import PymeasureInstrument with PymeasureInstrument( name="psu", instrument=KeysightE36312A("USB0::0x2A8D::0x1102::MY123::INSTR"), publishers=[FilePublisher(format="jsonl")], channel_map="psu_map.json", poll=["ch_1.voltage", "ch_1.current"], ) as psu: psu.device.ch_1.voltage_setpoint = 5 # publishes psu.ch1.voltage_setpoint.cmd psu.start() # polls ch_1.voltage/current every second print(psu.get_channel("ch1.current", wait_for_new_samples=True).latest)
- Parameters:
name
Channel-name prefix for everything published.
instrument
The pymeasure instrument, already constructed (connected).
publishers
Publishers that receive the commands and measurements.
channel_map
Which properties publish and under which names: a
ChannelMap, its dict form, or the path of a JSON file frominstro pymeasure map.Nonepublishes every property under the names the inspection rules give it.poll
Attribute paths (
"ch_1.voltage") the background daemon reads everybackground_intervalseconds oncestartis called.device_attr
Extra attribute name for the pymeasure instrument, in addition to
device; e.g."smu"makeswrapper.smuwork.close_device
Close the pymeasure instrument’s adapter in
close.closedoesn’t call the driver’sshutdown(), which on many drivers turns outputs off; calldevice.shutdown()first if you want the instrument left in its safe state.background_config
Background-daemon settings.
**kwargs
Default tags, or
dataset_rid=(seeInstrument).
- Raises:
RuntimeError –
instrumentis already attached to anotherPymeasureInstrument.ValueError – The channel map does not fit
instrument, apollpath is unknown or not readable, ordevice_attrcollides with an existing attribute.
Attributes
Seconds between background daemon iterations (0 = no wait).
Whether the background daemon thread is running.
Attribute path to channel name (without the instrument prefix) for every published property.
Return an insertion-ordered snapshot of all published channel names.
The wrapped pymeasure instrument; use it exactly as without instro.
Methods
Append
methodto the daemon's call list.Register a publisher to receive this instrument's Measurement/Command data.
Stop the daemon, close the publishers, detach, and close the device if
close_device.Replace all daemon functions with a single
method(called with the given args).Stop the background daemon, stop publishing, and restore the original classes.
Return the most recent
lengthsamples forchannel_namefrom the in-memory buffer.Return the most recent sample for
channel_namefrom the in-memory buffer.No-op base hook.
Fan
dataout to every configured publisher;kwargspass through.Start the background daemon thread.
Signal the background daemon to stop and join it.
- property device: T#
The wrapped pymeasure instrument; use it exactly as without instro.
- property channel_map: dict[str, str]#
Attribute path to channel name (without the instrument prefix) for every published property.
- detach() None[source]#
Stop the background daemon, stop publishing, and restore the original classes. Idempotent.
- close() None[source]#
Stop the daemon, close the publishers, detach, and close the device if
close_device.
- add_background_daemon_function(
- method: Callable,
- *args,
- **kwargs,
Append
methodto the daemon’s call list. Usedefine_background_daemonto replace instead.
- add_publisher(
- publisher: Publisher,
Register a publisher to receive this instrument’s Measurement/Command data.
- property background_interval#
Seconds between background daemon iterations (0 = no wait).
- property channel_names: tuple[str, ...]#
Return an insertion-ordered snapshot of all published channel names.
Names are complete after one full daemon iteration: {name}.loop_time publishes every iteration, so call get_channel(“loop_time”, wait_for_new_samples=True) then read channel_names to ensure the complete set has arrived.
- Raises:
RuntimeError – No background buffer;
start()was not called.
- define_background_daemon(
- method: Callable,
- *args,
- **kwargs,
Replace all daemon functions with a single
method(called with the given args).
- get_channel( ) Measurement#
Return the most recent
lengthsamples forchannel_namefrom the in-memory buffer.If the channel does not exist yet, or no sample is available, the code will always block until
timeoutexpires.- Parameters:
channel_name
Name of the channel to retrieve.
length
Number of trailing samples to return.
wait_for_new_samples
Block until at least
lengthnew values arrive.timeout
Seconds to wait when insufficient data exists or
wait_for_new_samples=True.
- Raises:
RuntimeError – No background buffer;
start()was not called.ChannelNotFoundError – channel had no values and no data appeared before
timeout.wait_for_new_samples=Trueand channel did not appear withintimeout.ChannelValueTimeoutError –
wait_for_new_samples=Trueand values did not arrive withintimeout.
- get_single_channel_value(
- channel_name: str,
Return the most recent sample for
channel_namefrom the in-memory buffer.This will not wait, if data is not available then
Noneis returned.- Parameters:
channel_name
Name of the channel to retrieve.
- Raises:
RuntimeError – No background buffer;
start()was not called.
- publish(
- data: Measurement | Command,
- **kwargs,
Fan
dataout to every configured publisher;kwargspass through.
- start()#
Start the background daemon thread. No-op if already running.
- stop()#
Signal the background daemon to stop and join it. No-op if not running.