Skip to content

Lablink

Work in progress

Lablink support is currently being developed and not in a usable state. More details coming soon!

Lablink API not final

Although the examples here should mostly hold up, method names and signatures may change in future versions. Keep an eye on the changelog.

The pypalmsens.lablink module lets you connect to a Lablink server and control the instruments attached to it. You can use it to list instruments, claim them, start measurements, and retrieve previously recorded data.

Everything in this module is async, so its calls are awaited.

Connecting to an instance

A Lablink instance is a server, identified by an address, that manages one or more instruments.

Discovering instances

pypalmsens.lablink.discover returns every Lablink instance reachable on the network:

instances = await lablink.discover()

for instance in instances:
    print(instance.address, instance.name)

Connecting to a known instance

If you already know the address of the server, create an Instance directly. The default address points to the local server:

local = lablink.Instance('https://127.0.0.1/')

A newly created instance only knows its address. Call get_info to fill in the name, version, and serial number:

await local.get_info()
# LablinkInfo(name='My Lablink', version='2.1.0', special_version='Standard', os_version='Fedora Linux 43 (WSL)', model='WSL', serial_number='SERIAL_NUMBER', is_measuring=False)

print(local.name, local.version, local.serial_number)
# My Lablink, 2.1.0, SERIAL_NUMBER

Logging in

Log in with a username and password to get a Session:

session = await local.login('Alice', 'hunter2')

The session is your entry point for everything that follows: listing instruments, claiming them, starting measurements, and retrieving data.

Registering a new user

If this is enabled on the server, you can register as a new user via the API:

await local.register_new_user('Bob', 'badpassword')

Instruments

A session keeps track of the instruments attached to its Lablink instance. The Session.instruments property holds the result of the most recent list_instruments call, and may be stale or empty.

instruments = await session.list_instruments()
instrument, *_ = instruments

An Instrument references an instrument by its serial number. You can query the information you need to identify it:

print(instrument.serial_number)
# SERIAL_NUMBER

Claiming instruments

Before an instrument can be used for a measurement, you must claim it. Claiming locks the instrument to the session: while a claim is held, no one else can use it.

Use Instrument.claim and the returned InstrumentClaim as an async context manager. The claim is released when the async with block exits:

async with await instrument.claim() as claim:
    # only this session can use the instrument here
    ...

To claim several instruments at once, use claim:

async with await session.claim(instruments) as claims:
    ...

The returned InstrumentClaimBatch releases every claim when the block exits.

Starting a measurement

Start a measurement on a claimed instrument with Session.start_measurement. The measurement method is passed as a keyword argument:

method = ps.CyclicVoltammetry()
job = await session.start_measurement(claim, method=method)

The call returns a MeasurementJob for the ongoing measurement. Await the job to wait for it to finish and receive the data:

measurement = await job

You can pause, resume, and stop the measurement early with abort

await job.pause()
await job.resume()
await job.abort()

To measure on several instruments at once, use start_measurement with multiple claims. It returns one MeasurementJob per instrument.

Previous measurements

A session can also look up measurements that were recorded before. Use list_measurements to get a list of Measurement objects:

measurements = await session.list_measurements()
measurement, *_ = measurements

Measurement data

A reference only carries the measurement's metadata: name, timestamp, method ID, and number of points. Fetch the full data with get_data:

data = await ref.get_data()

The data are returned as an xarray.Dataset. or xarray.DataTree.

The measurement data API is still work in progress.

Complete example

import asyncio

import pypalmsens as ps
from pypalmsens import lablink

async def main():
    method = ps.CyclicVoltammetry()

    local = lablink.Instance('https://127.0.0.1/')
    await local.get_info()
    session = await local.login('test', 'test')

    instrument, _* = session.instruments

    async with await instrument.claim() as claim:
        job = await claim.start_measurement(method=method)
        measurement = await job

    print(measurement)

    data = measurement.get_data()
    print(data)

if __name__ == '__main__':
    asyncio.run(main())

Hardware sync

On multichannel devices that support it, hardware sync can be used to synchronize measurements between multiple channels. When synchronization is enabled the follower device will wait until the main channel enables synchronisation. After that, the follower and main will synchronize their measurement loop start and iterations.

Your pool of instruments must contain:

To start a measurement with hardware sync:

  1. Enable hardware sync on the method with use_hardware_sync = True.
  2. (optional) Configure the main/follower channels for devices that support it. Multi-instrument devices cannot be configured, channel 1 is always the main channel.
  3. Start the measurement on the follower channels. The instrument are prepared and put in a waiting state.
  4. Start the measurement on the main channel. This will trigger the other instruments to start as well.