Skip to content

Andor SDK3: sequences, streaming reads and live view - #102

Open
kalidke wants to merge 15 commits into
mainfrom
andor-sdk3-sequence
Open

kalidke wants to merge 15 commits into
mainfrom
andor-sdk3-sequence

Conversation

@kalidke

@kalidke kalidke commented Dec 4, 2026

Copy link
Copy Markdown
Member

Second of the Andor driver pull requests: sequences, streaming reads and live view for AndorSDK3Camera, replacing
the four methods that threw (sequence, getdata, live, getlastframe), and decoding of the Mono12Packed pixel
encoding.

Until pull request #101 merges, this diff includes it. This branch carries #101's commits through merges. Once #101
is on main, main is merged in here and the diff is this change alone.

What it adds

  • sequence sets a fixed cycle of sequence_length frames and, with the internal trigger and a camera that says
    FrameRate is writable, the new frame_rate keyword (default nothing: the fastest rate the camera reports; a
    request outside its range throws ArgumentError before the rate is sent). The driver queues a ring of
    min(N, max(8, ceil(frame_rate_actual / 2))) buffers and queues each again as it is read, so a sequence of any
    length arrives in order from a ring of fixed size, every frame once.
  • getdata(camera, n), a second method of the existing getdata (no new interface function), returns the next
    n frames and leaves the sequence running, so a scan can read a long sequence in bundles. getdata(camera) returns
    every frame not yet delivered. When the driver takes a sequence's last frame, by whichever call, it stops the
    acquisition; frames not yet delivered stay readable with getdata until the next start, abort or shutdown.
  • Deadlines and overflow. Each frame gets its deadline from the previous frame; a frame that arrives after it is
    not accepted. If frames are not read or not transferred fast enough, the camera overflows its own memory; the driver
    stops the acquisition and throws, saying how many frames it had read.
  • live uses the software trigger. Each getlastframe that finds no trigger outstanding queues a buffer and sends
    the next numbered trigger; a reader that finds another reader's trigger outstanding waits for that frame and returns
    nothing if the other reader takes it. A stalled reader cannot overflow the camera, every frame is new, and no
    trigger is sent twice. live throws before any library call on a camera with no SoftwareTrigger command.
  • getlastframe during a sequence reads at most one ring's worth of finished frames per call and returns a copy of
    the newest that getdata has not delivered, taking nothing from getdata.
  • Buffers. Every buffer queued to the library is held by one process-wide root from its queuing until a flush or
    close of its handle succeeds, so the library never writes into freed memory, even when the camera object is dropped
    mid-sequence.
  • One stop rule. A stop the camera answers with AT_ERR_NOTWRITABLE (5), after which CameraAcquiring reads
    false, counts as stopped; any other failure throws. Every stop ends the acquisition for every read waiting on it.
  • Decoding. Each frame is decoded once, straight into the stack or the returned matrix, with the new
    CameraInterface.rowmajor_to_yx! (the in-place form of rowmajor_to_yx). Mono12Packed (two pixels in three
    bytes) decodes to (height, width) and needs the camera's AOIStride; a start in that encoding without it throws.
  • export_state gains frame_rate_requested and frame_rate_actual; frame_rate_actual is read at every start and
    setting.

Like the rest of the driver, it checks every library return code, cleans up with ordinary try/finally, and enforces
no safety of its own.

Tests

Against the recording fake of atcore (test/andor_sdk3_fake.jl); no test loads an Andor library. The fake now
delivers frames per software trigger, packs Mono12Packed, and models the camera's own memory: frames exposed with no
buffer queued wait there, and the camera overflows when it is full, leaving the acquisition either armed or stopped, since what a real camera
does then is not yet known. Each
rule added in review was removed in a scratch copy and its test failed.

  • Full suite, Julia 1.13: 11259 pass, 3 broken (the existing @test_broken in test/gui.jl), 0 fail.
  • Andor tests, Julia 1.11.9: 5187 of 5187 pass.

Not covered here

  • No real camera has run this code. Owed at the instrument: overflow's return code, the answer to a wait on an empty
    queue after a fixed cycle, a software-triggered start with no buffer queued, buffer queue capacity, and the reader's
    speed at full frame.
  • Follow-ups: the shared camera window's close can abort a newer acquisition (pre-existing, every camera); the default
    frame rate against the interface's transfer rate; a cap on the ring.

Version stays 0.2.7-DEV: nothing breaks for existing callers.

🤖 Generated with Claude Code

kalidke and others added 15 commits December 3, 2026 12:28
Adds sequence, getdata(camera), getdata(camera, n), live and getlastframe to
the SDK3 driver, replacing the four methods that threw, and decodes the
Mono12Packed pixel encoding. Tested against the recording fake of atcore;
no test loads an Andor library.

- sequence sets a fixed cycle and, with the internal trigger and a writable
  FrameRate, the new frame_rate keyword (default nothing: the camera's
  maximum), read back into frame_rate_actual. It queues a ring of
  min(N, max(8, ceil(frame_rate_actual / 2))) buffers and queues each one
  again as it is read, so frames arrive in order and each is delivered once.
- getdata(camera, n) is a second method of the existing getdata: the next n
  frames, the sequence still running. getdata(camera) returns the rest and
  stops; a stop that fails after the frames were read throws. Each frame
  gets its own deadline. An overflow of the camera's memory stops the
  acquisition and throws, saying how many frames were read.
- live uses the software trigger: each getlastframe queues a buffer, sends
  SoftwareTrigger and returns that frame. It refuses before any library call
  on a camera without a SoftwareTrigger command. During a sequence
  getlastframe returns the newest unread-by-getdata frame and takes nothing
  from getdata; it returns nothing when abort replaces its acquisition.
- capture, sequence and live share one start; one read path takes frames
  from the ring for all of them.
- export_state adds frame_rate_requested and frame_rate_actual.
- The fake delivers frames per software trigger, packs Mono12Packed, stamps
  each frame with its index and can overflow after k frames.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The driver counted holders of the library (cameras, and list_cameras while it
ran) and finalized it when the last gave it back, so a listing followed by
initialize, or shutdown followed by initialize, called AT_InitialiseLibrary a
second time in one process. On the real library (SDK3 3.15.30175, USB 3 Zyla,
Linux) the second call never returns: it waits inside the USB transport for a
control transfer to the camera. The manual initializes and finalizes once per
application.

The driver now calls AT_InitialiseLibrary at most once per binding per
process, at the first use, and never finalizes the library while the process
runs. The real binding registers an atexit hook that finalizes it once if it
was initialized; a fake registers none. The holder count, LibraryUse, the
camera's library_held field and the release paths in shutdown, initialize and
list_cameras are removed.

The fake records an AT_InitialiseLibrary after an AT_FinaliseLibrary as a
violation, since a test cannot hang. New tests: a listing, initialize, capture,
shutdown and initialize again on one fake make exactly one initialisation and
no finalise; the exit hook finalizes once and does nothing when the library was
never initialized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sured

Found with a USB 3 Zyla, SDK3 3.15.30175: with TriggerMode "External
Exposure" the camera reports ExposureTime not writable, and
AT_SetFloat("ExposureTime") answers 5 (AT_ERR_NOTWRITABLE); the pulse sets the
exposure. The driver sent the exposure, then the region, then the trigger
mode, at initialize and at every capture start, so with trigger_mode
EXTERNAL_EXPOSURE the second start, and an initialize of a camera already in
that mode, threw.

The trigger mode is now sent before the exposure, and no ExposureTime is sent
when the mode being sent is EXTERNAL_EXPOSURE; exposure_time_actual is still
read back. setexposuretime! in that mode throws and says the exposure comes
from the trigger pulse, with no library call.

The stand-in's Zyla profile answers as the real camera did: ExposureTime is
not writable in External Exposure; AcquisitionStop on an idle camera answers 5;
after a fixed cycle ends by itself CameraAcquiring stays true and
AcquisitionStart answers 5 until AcquisitionStop. The simulated-camera profile
keeps the old answers, and the knobs (idle_stop_code, needs_stop) remain.
Comments record the other facts from the same camera: a second AT_Close of a
closed handle answers 12, a second AT_Open of a held index succeeds with a new
handle (the driver never does it), and ImageSizeBytes equals AOIStride times
AOIHeight at full frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in the library fix: AT_InitialiseLibrary once per process and
AT_FinaliseLibrary only at exit, the trigger mode sent before the exposure
with no ExposureTime in External Exposure, setexposuretime! refused in that
mode, and the stand-in's Zyla profile answering as the real camera did.

Conflicts resolved so both changes hold: capture's start is this branch's
shared start_acquisition!, which now sends the trigger mode first (live's
"Software" included), then the exposure (none in External Exposure), then
the region; the sequence call-order test asserts that order. The camera
struct keeps this branch's acquisition fields and loses library_held. The
Zyla profile's idle_stop_code = 5 and needs_stop = true apply to every
sequence and live test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ished after it

The exit hook called AT_FinaliseLibrary whenever the library was up, so a
process that exited with a camera open finalized the library under that
camera, and a later exit hook (an application's, registered before the
library's first use, which Julia runs after the driver's) then reached a
finalized library: its shutdown failed, and its list_cameras initialized the
library again, which never returned on the real library.

- The hook finalizes only when the process holds no camera in the library.
  With one open it leaves the library up, so a later hook's shutdown works.
- The library state is :down, :up or :finalized. After the finalize, every
  use throws, saying the library was finalized at exit and cannot be
  initialized again in this process, with no library call. A failed first
  AT_InitialiseLibrary stays :down and is tried again.
- initialize initializes the library inside find_camera!'s hold of the
  registry lock, so the hook cannot finalize between that and the open.
- Whether a binding's library is finalized at exit is a method,
  registers_exit_hook (true for AtCore). The fake can stand for the real
  library (exit_hook), so a test sees the hook registered in
  Base.atexit_hooks once, at the first successful initialization, never for
  a fake or after a failed first one, and runs it. A child process checks
  the exit order with an application hook registered before the first use.
- The comment on the library cites the manual for what it says (a second
  initialization is allowed, M3 p.27) and says what the real library did:
  an initialization after a finalize never returned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…alize reads it once

setexposuretime! refused by the trigger_mode field, which is a request: with
the field set back from EXTERNAL_EXPOSURE by hand, or after a settriggermode!
the camera refused, the camera was still in External Exposure and the setter
sent ExposureTime, which the camera refuses in that mode.

- setexposuretime! still refuses with no library call when trigger_mode is
  EXTERNAL_EXPOSURE. Otherwise it reads the camera's TriggerMode first and,
  in External Exposure, throws, saying settriggermode! or the next start
  sends trigger_mode and changes it.
- initialize reads trigger_mode once and passes it to the trigger and the
  exposure senders, as capture does, so a field edited between the two
  cannot send an exposure the camera refuses, or skip one it needs.
- Tests: both ways into the stale mode; the field edited during the trigger
  mode's read-back, in both directions; and in External Exposure the
  exposure the camera reports changes between captures, so a readback reused
  from the first start fails. These tests deliver the frame at the first
  poll, so they never compare the clock with a deadline.
- The struct and export_state docstrings say no exposure is sent in
  External Exposure, and that exposure_time_actual is then the value the
  camera reports, not the width of the trigger pulse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… profile tests

- The simulated camera's profile is checked by the raw calls: a start, its
  one frame delivered, and a second start with no stop between. capture
  sends a stop between its starts, so the old check passed with a simulated
  camera that refused the second start.
- The profile testset delivers the frame at the first poll, so its captures
  never compare the clock with a deadline.
- The stop subtest that modeled a library neither profile is (a finished
  cycle that reads idle and refuses the stop) is gone. The rule it also
  guarded, that capture flushes after a failed AcquisitionStop and then lets
  the buffer go, is now asserted in the walk of a code at every call of
  capture, on the Zyla profile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…for it

The Andor SDK3 driver has never been released, so its two Fixed entries
are folded into its Added entry, which now says:

- every start sends every setting, the trigger mode first, except the
  exposure in EXTERNAL_EXPOSURE, where setexposuretime! throws while the
  field asks for that mode or the camera is still in it, and
  exposure_time_actual is the value the camera gives, not the pulse width;
- the library is initialized once per process and not finalized while it
  runs; the exit hook finalizes it only with no camera open, and after
  that, initialize and list_cameras throw with no library call;
- the library lifecycle and what is sent in External Exposure follow how a
  real Zyla answered, in place of the claim that no Andor library or camera
  was used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The comment said the driver never makes a second AT_InitialiseLibrary with
no finalize between. It does, to retry a first one that failed; whether
that hangs on the real library was not tried.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Every buffer queued to the library is held by one process-wide root
  (LIBRARY_BUFFERS, under its own lock) from its queuing until an AT_Flush
  or AT_Close of its handle succeeds, so a camera dropped while a sequence
  runs never leaves the library writing into freed memory. leak_buffers!
  and LEAKED_BUFFERS are gone.
- getdata allocates its stack inside the scope whose failure stops the
  acquisition, so a failed allocation stops it and is thrown.
- A frame's deadline is exact: each sleep ends at the deadline, and a poll
  that returns after it throws, whether or not a frame came.
- getdata is planned once (plan_read) and read from that plan (run_read):
  in a live view it reads the live view it was called on.
- A sequence stops when the driver takes its last frame, by whichever call;
  the frames not yet delivered stay readable with getdata until the next
  start, abort or shutdown. A stop that fails there throws from that call.
- Mono12Packed needs AOIStride: without it a start throws before anything
  is queued.
- One stop rule: AcquisitionStop answering AT_ERR_NOTWRITABLE, then
  CameraAcquiring reading false, counts as stopped; any other failure
  throws. abort no longer reads CameraAcquiring first.
- Every stop changes acquisition_id, so a read waiting between polls makes
  no further library call once another read's failure stopped the camera.
- Live software triggers are numbered: a reader sends the next trigger only
  when none is outstanding, takes only its trigger's frame, and returns
  nothing when another reader took it.
- frame_rate_actual is read in refresh_actuals!, at initialize and every
  start and setting.
- Each frame is decoded once, straight into the stack slice or the returned
  matrix: CameraInterface.rowmajor_to_yx! (the in-place form of
  rowmajor_to_yx) for the 16-bit encodings, and a packed decode that writes
  [y, x] directly over blocks of rows.
- live takes no frame count. getlastframe in a sequence still reads at most
  one ring's worth per call, now documented and tested.
- Text: the overflow message says frames were not read or not transferred
  fast enough; the claim that a sequence that fits the ring cannot overflow
  is gone; getlastframe's live-view throws are in the interface docstring
  and mc-acquire; phase and is_running are the driver's state; the window's
  memory cost is in start_sequence's docstring; the changelog no longer says
  an out-of-range rate sends nothing.
- The test stand-in models the camera's own memory (a first-in, first-out
  store of memory_frames frames, written into a buffer the moment one is
  queued; an overflow when it is full) and what an overflow leaves behind
  (overflow_leaves_armed), and can return a known address twice
  (duplicate_after). overflow_after is gone. Every sequence and live test
  runs on the Zyla profile; tests added for each fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…state, trigger mode read by the camera)

Conflicts resolved so both branches hold:
- shutdown docstring: the exit hook finalizes the library if no camera is
  open then, and a finished sequence's undelivered frames are dropped.
- AndorSDK3Camera docstring: the trigger-mode and External Exposure text,
  with frame_rate_actual among the reported values and the frame_rate
  paragraph kept.
- CHANGELOG: the driver entry as the library fix now describes it, followed
  by the sequences, streaming and live view entry.
- test: the stop testset keeps the library fix's version, without the case
  that set needs_stop = false on a Zyla; its title now says the abort on the
  idle camera does not throw, since abort always sends the stop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…from initialization through open

The failure walk injects an error by call number, and its fake delivered on the second poll against a real deadline:
a descheduled runner could end the wait first and move the injection from AcquisitionStop to AT_Flush. The trace run
and each injected run now use a fake whose clock stands still, with both polls still covered.

A new test runs a competing task, as the exit hook of another thread would, just as use_library! returns for the fake.
It takes the registry lock if it is free; with use_library! outside find_camera!'s hold the camera is finalized before
it is opened.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…each capture; formatting

Each capture sends the trigger mode, the exposure (none in External Exposure), the region and the one-frame cycle;
features are sent only at initialize. The CHANGELOG no longer says the rule to finalize at exit only with no camera
open follows the camera: review set it. Overlong lines in the Andor entry and an unevenly rewrapped docstring are
wrapped to the file's width.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…vers the whole span; text

The capture failure walk runs on a clock that advances 1 ms per read and asserts that no more than two polls follow
each other, so a driver that swallows an injected failure and waits for a frame never queued fails the test at once
instead of polling until the deadline. Two reworked testsets that captured on the default fake use first-poll delivery,
so no reworked test reads the wall clock.

The lock test now counts its probe's runs and asserts it ran, probes both initialize and list_cameras, and builds the
camera before installing the shared callback so a failed construction cannot leave it set. The existing registry-lock
test also records the lock at the DeviceCount read between the library's initialization and the opens.

Text: the frame timeout is checked and bounds the driver's own polling; it is not sent to the camera (CHANGELOG and the
capture docstring). The capture docstring is rewrapped, a code span is no longer split across lines, and the comment
on the probe says what point it guards.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Conflicts resolved: the initialize, shutdown and capture docstrings keep
this branch's additions (SoftwareTrigger read, frames dropped at
shutdown, the stop rule, frame_rate_actual, late frames refused) in the
library fix's wording (settings sent again at each capture; features
only at initialize). The CHANGELOG's driver entry takes the library
fix's text with this branch's stop rule, process-wide buffer root and
Mono12Packed decoding. The idle-abort test keeps this branch's name and
the library fix's first-poll fake.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant