pytweezer.drivers.camera_base module

Generic camera abstraction shared by every real camera driver.

Rather than each camera module hand-rolling its own ROI/trigger/acquisition logic and its own simulated twin, this module defines:

  • Camera – an abstract base that owns everything generic to a camera server (broadcasting frames onto an image stream, TIFF autosave, the autosave/broadcast acquisition loop, connection lifecycle) and declares the small set of hardware-specific hooks a concrete driver must implement (connect/disconnect, ROI, trigger, exposure, acquisition start/stop, and the raw frame read).

  • SimulatedCamera – a single, one-size-fits-all synthetic backend that implements those hooks with generated frames, usable in place of any real camera in simulation mode.

  • simulated_camera_for() – adapts SimulatedCamera to a specific real driver so that driver’s extra methods (e.g. the ImagEM’s EM-gain setters) are auto-stubbed via pytweezer.servers.simulated_device.simulate(), keeping the simulated surface interface-complete without drift.

A concrete driver (“shim”) is therefore just the thin translation layer between its vendor SDK (dcam, Spinnaker, …) and the abstract hooks below.

class pytweezer.drivers.camera_base.Camera(image_dir=None, timeout=5.0, stream_name=None)[source]

Bases: ABC

Abstract base for a single-camera RPC target.

Subclasses (“shims”) implement the abstract hardware hooks; everything generic – image broadcasting, TIFF autosave, the acquisition loop, and the connection lifecycle – lives here so each driver stays a thin translation layer over its vendor SDK.

The connected hardware handle is stored on _backend; None means the camera has been relinquished. Subclasses set it in _connect() and clear it (implicitly, via close()) on teardown.

Parameters:
  • image_dir (str | None)

  • timeout (float)

  • stream_name (str | None)

acquire_n_frames(nframes, start_frame=0, autosave=False, broadcast=False)[source]
Parameters:
Return type:

ndarray

acquire_single_frame(autosave=False, broadcast=False, start_frame=0)[source]
Parameters:
Return type:

ndarray

broadcast_image(image, info)[source]
Parameters:

image (ndarray)

Return type:

None

close()[source]
Return type:

None

image_prefix: str = 'image'

Filename prefix used by save_tiff(); override per driver.

is_connected()[source]
Return type:

bool

reacquire_camera()[source]
Return type:

dict

relinquish_camera()[source]
Return type:

dict

classmethod save_tiff(image, image_dir=None, run_no=0)[source]
Parameters:
Return type:

None

abstractmethod set_exposure_time(exposure)[source]

Set the per-frame exposure time in seconds.

Parameters:

exposure (float)

Return type:

None

abstractmethod set_roi(x0, width, y0, height)[source]

Restrict readout to the given region of interest (pixels).

Parameters:
Return type:

None

abstractmethod set_trigger_source(source)[source]

Select the trigger mode/source (e.g. "int"/"ext").

Parameters:

source (str)

Return type:

None

abstractmethod setup_acquisition(acq_mode, nframes)[source]

Configure an acquisition of nframes in acq_mode.

Parameters:
  • acq_mode (str)

  • nframes (int)

Return type:

None

abstractmethod start_acquisition()[source]

Begin acquiring frames.

Return type:

None

abstractmethod stop_acquisition()[source]

Stop acquiring frames.

Return type:

None

class pytweezer.drivers.camera_base.SimulatedCamera(image_dir=None, timeout=5.0, stream_name=None)[source]

Bases: Camera

One-size-fits-all synthetic camera backend.

Implements every Camera hardware hook with generated frames, so it can stand in for any real camera in simulation mode. Driver-specific extra methods (beyond the Camera interface) are not defined here; use simulated_camera_for() to auto-stub those for a particular driver.

Parameters:
  • image_dir (str | None)

  • timeout (float)

  • stream_name (str | None)

set_exposure_time(exposure)[source]

Set the per-frame exposure time in seconds.

Parameters:

exposure (float)

Return type:

None

set_roi(x0, width, y0, height)[source]

Restrict readout to the given region of interest (pixels).

Parameters:
Return type:

None

set_trigger_source(source)[source]

Select the trigger mode/source (e.g. "int"/"ext").

Parameters:

source (str)

Return type:

None

setup_acquisition(acq_mode, nframes)[source]

Configure an acquisition of nframes in acq_mode.

Parameters:
  • acq_mode (str)

  • nframes (int)

Return type:

None

start_acquisition()[source]

Begin acquiring frames.

Return type:

None

stop_acquisition()[source]

Stop acquiring frames.

Return type:

None

pytweezer.drivers.camera_base.requires_camera(func)[source]

Guard a method so it raises cleanly if the camera has been relinquished.

pytweezer.drivers.camera_base.simulated_camera_for(real_cls)[source]

Return a SimulatedCamera subclass tailored to real_cls.

The returned class has the full generic simulated behaviour, plus a logging no-op stub for every public method real_cls adds beyond the Camera interface (e.g. a driver’s EM-gain setters), courtesy of simulate(). This keeps the simulated surface interface-complete with the real driver without any hand-written per-driver simulated class.

Parameters:

real_cls (type)

Return type:

type