Skip to main content

No project description provided

Project description

LiberTEM-dectris-rs

LiberTEM-dectris-rs on GitHub

This is a Python package for efficiently receiving data from DECTRIS detectors with the zeromq interface. The low-level, high-frequency operations are performed in a background thread implemented in rust, and multiple frames are batched together for further processing in Python.

It is built for LiberTEM-live, but can also be used stand-alone.

Usage

import numpy as np
import libertem_dectris
from libertem_live.detectors.dectris.DEigerClient import DEigerClient

# trigger acquisition via the REST API, needs `libertem-live`
nimages = 256 * 256
ec = DEigerClient('localhost', 8910)  # hostname and port of the DCU REST API
ec.setDetectorConfig('ntrigger', 1)
ec.setDetectorConfig('nimages', 1)
ec.setDetectorConfig('trigger_mode', 'exte')
ec.setDetectorConfig('ntrigger', nimages)
result = ec.sendDetectorCommand('arm')
sequence_id = result['sequence id'] 

conn = libertem_dectris.DectrisConnection(
    uri="tcp://localhost:9999",
    handle_path="/tmp/dectris_shm",
    frame_stack_size=32,
    num_slots=2000,
    bytes_per_frame=512*512,
    huge=False,
)

# as we have armed the detector above, we know the sequence number
# that we should expect:
# (in other cases, can also call
# `conn.start_passive` and `conn.wait_for_arm` to passively wait for
# the detector to be armed)
conn.start(sequence_id)

# any other process can use a `CamClient` to use data
# stored in the SHM:
cam_client = libertem_dectris.CamClient(conn.get_socket_path())

try:
    while True:
        # get at most `max_size` frames as a stack
        # (might get less at the end of the acquisition)
        stack_handle = conn.get_next_stack(max_size=32)

        # if the receiver is idle, stack_handle will be None here:
        if stack_handle is None:
            break

        # the expected shape and data type:
        frame_shape = tuple(reversed(stack_handle.get_shape()))
        dtype = np.dtype(stack_handle.get_pixel_type()).newbyteorder(
            stack_handle.get_endianess()
        )

        # pre-allocate some memory for the pixel data:
        # (would be pulled out of the loop in real code)
        buf = np.zeros((len(stack_handle),) + frame_shape, dtype=dtype)

        # decompress into the pre-allocated buffer
        cam_client.decompress_frame_stack(stack_handle, out=buf)

        # free up the shared memory slot for this frame stack:
        cam_client.done(stack_handle)

        # we can still use the decompressed data:
        buf.sum()
finally:
    conn.close()  # clean up background thread etc.
    cam_client.close()

Changelog

v0.2.13

  • Updated dependencies and improved error handling

v0.2.12

  • Refactored and unified interface - allows for more code sharing between crates
  • Some robustness changes around adding timeout parameters for many top-level operations

v0.2.11

  • Re-release for CI changes

v0.2.10

  • Add functionality to generate mock data for sending via the simulator
  • Add missing numpy dependency
  • Make debug output a bit more reliable

v0.2.9

  • Add more debug and trace output

v0.2.7

  • Log more details in DectrisConnection.log_shm_stats and change log level to INFO
  • Increase timeout for sending headers and frames

v0.2.4 - v0.2.6

  • Updated examples and CI configuration

v0.2.3

  • Add env_logger: set environment variable LIBERTEM_DECTRIS_LOG_LEVEL to e.g. 'INFO' to enable logging
  • Improved error handling: raise an exception instead of panicing on serialization errors
  • Ignore messages with mismatching series ID
  • Add explicit checks for the correct header_detail levels
  • Move code into monorepo

v0.2.2

  • Vendor bitshuffle and add Frame.decompress_into method, PR #10

v0.2.1

  • Catch frame ID mismatch, PR #9

v0.2.0

  • Added libertem_dectris.headers submodule that exports header classes
  • Added ways to create libertem_dectris.Frame and libertem_dectris.FrameStack objects from Python, mostly useful for testing
  • Added binding to random port for the simulator
  • Properly parametrize with zmq endpoint URI
  • Fix many clippy complaints

v0.1.0

Initial release!

Development

This package is using pyo3 with maturin to create the Python bindings. First, make sure maturin is installed in your Python environment:

(venv) $ pip install maturin

Then, after each change to the rust code, run maturin develop -r to build and install a new version of the wheel.

As we vendor bitshuffle, make sure to clone with git clone --recursive ..., or manually take care of initializing and updating submodules.

Release

  • update changelog above
  • bump version in Cargo.toml if not already bumped, and push
  • create a release from the GitHub UI, creating a new tag vX.Y.Z
  • done!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libertem_dectris-0.2.14.tar.gz (2.2 MB view details)

Uploaded Source

Built Distributions

libertem_dectris-0.2.14-cp37-abi3-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.7+ Windows x86-64

libertem_dectris-0.2.14-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (28.6 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

libertem_dectris-0.2.14-cp37-abi3-macosx_11_0_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.7+ macOS 11.0+ x86-64

libertem_dectris-0.2.14-cp37-abi3-macosx_11_0_arm64.whl (2.3 MB view details)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

File details

Details for the file libertem_dectris-0.2.14.tar.gz.

File metadata

  • Download URL: libertem_dectris-0.2.14.tar.gz
  • Upload date:
  • Size: 2.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.7.4

File hashes

Hashes for libertem_dectris-0.2.14.tar.gz
Algorithm Hash digest
SHA256 02b49e72fa46af475ad375e30e844bfe02d8be89ac7eafc7577aa6b37e341648
MD5 59d520e3b1da7d8faf764c5f7ab9f54d
BLAKE2b-256 c1174e66f2421ea22e5b2e49e37c125b00ef8f7fdc5dc810df5e54c53e5bfaf6

See more details on using hashes here.

File details

Details for the file libertem_dectris-0.2.14-cp37-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for libertem_dectris-0.2.14-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 5d3b0421745bb66b510415ada2657f7eec7b8bd41d6e520010197574ba47e50a
MD5 93b7493976fcae23c1a1b6bdca425af3
BLAKE2b-256 d2684b6edec07e21a627d90a8f25b4400b53cb656c03294a67c4bd42fedefc51

See more details on using hashes here.

File details

Details for the file libertem_dectris-0.2.14-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libertem_dectris-0.2.14-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5dc174d8d4b96c27a01c53d45eec095f49d80014591ff0391dbc84fca380e0f8
MD5 8d6bf754dd730cedf0edf9a2b7f11240
BLAKE2b-256 ce4f41be249ab704c7518c998b33f78cd4b22f2e4e9f0cb2240871fbf16d9ef6

See more details on using hashes here.

File details

Details for the file libertem_dectris-0.2.14-cp37-abi3-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for libertem_dectris-0.2.14-cp37-abi3-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 46f63f422bdebe6824c030c52cce52ff1cea9c56599dcc0076cabdd1df43e0dd
MD5 4e96de56e59903da01edbd9afefb4352
BLAKE2b-256 42a4aa7b18aad45ac47235302a17128971f45503600f74040eb609beab97bc13

See more details on using hashes here.

File details

Details for the file libertem_dectris-0.2.14-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for libertem_dectris-0.2.14-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 22be827cf63b91aa96b37f92f6274a0c5182f3916ddd6bab1bbcd8f553543750
MD5 b2ac67201fcff0249c3e1d468645f204
BLAKE2b-256 c1e88ac3e2d5a65555f2324b0abd1e2aae0969e1e98ccd2441113a4d1fbccb1f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page