leicacam package

Control Leica microscopes with python.

class leicacam.AsyncCAM(*args: Any, **kwargs: Any)[source]

Bases: BaseCAM

Driver for LASAF Computer Assisted Microscopy using asyncio.

close() None[source]

Close stream.

async connect() None[source]

Connect to LASAF through a CAM-socket.

async receive() list[OrderedDict[str, str]][source]

Receive message from socket interface as list of OrderedDict.

async send(commands: list[tuple[str, str]] | bytes) None[source]

Send commands to LASAF through CAM-socket.

Parameters:

commands (list of tuples or bytes string) – Commands as a list of tuples or a bytes string. cam.prefix is always prepended before sending.

Returns:

Bytes sent.

Return type:

int

Example

>>> # send list of tuples
>>> await cam.send([('cmd', 'enableall'), ('value', 'true')])

>>> # send bytes string
>>> await cam.send(b'/cmd:enableall /value:true')
async wait_for(cmd: str, value: str | None = None, timeout: float = 60) OrderedDict[str, str][source]

Hang until command is received.

If value is supplied, it will hang until cmd:value is received.

Parameters:
  • cmd (string) – Command to wait for in bytestring from microscope CAM interface. If value is falsy, value of received command does not matter.

  • value (string) – Wait until cmd:value is received.

  • timeout (int) – Minutes to wait for command. If timeout is reached, an empty OrderedDict will be returned.

Returns:

Last received message or empty message if timeout is reached.

Return type:

collections.OrderedDict

class leicacam.CAM(*args: Any, **kwargs: Any)[source]

Bases: BaseCAM

Driver for LASAF Computer Assisted Microscopy.

autofocus_scan() OrderedDict[str, str][source]

Start the autofocus job.

close() None[source]

Close the socket.

connect() None[source]

Connect to LASAF through a CAM-socket.

disable(slide: int = 0, wellx: int = 1, welly: int = 1, fieldx: int = 1, fieldy: int = 1) OrderedDict[str, str][source]

Disable a given scan field.

disable_all() OrderedDict[str, str][source]

Disable all scan fields.

enable(slide: int = 0, wellx: int = 1, welly: int = 1, fieldx: int = 1, fieldy: int = 1) OrderedDict[str, str][source]

Enable a given scan field.

enable_all() OrderedDict[str, str][source]

Enable all scan fields.

flush() None[source]

Flush incoming socket messages.

get_information(about: str = 'stage') OrderedDict[str, str][source]

Get information about given keyword. Defaults to stage.

load_template(filename: str = '{ScanningTemplate}leicacam.xml') OrderedDict[str, str][source]

Load scanning template from filename.

Template needs to exist in database, otherwise it will not load.

Parameters:

filename (str) – Filename to template to load. Filename may contain path also, in such case, the basename will be used. ‘.xml’ will be stripped from the filename if it exists because of a bug; LASAF implicit add ‘.xml’. If ‘{ScanningTemplate}’ is omitted, it will be added.

Returns:

Response from LASAF in an ordered dict.

Return type:

collections.OrderedDict

Example

>>> # load {ScanningTemplate}leicacam.xml
>>> cam.load_template('leicacam')

>>> # load {ScanningTemplate}leicacam.xml
>>> cam.load_template('{ScanningTemplate}leicacam')

>>> # load {ScanningTemplate}leicacam.xml
>>> cam.load_template('/path/to/{ScanningTemplate}leicacam.xml')
pause_scan() OrderedDict[str, str][source]

Pause the matrix scan.

receive() list[OrderedDict[str, str]][source]

Receive message from socket interface as list of OrderedDict.

save_template(filename: str = '{ScanningTemplate}leicacam.xml') OrderedDict[str, str][source]

Save scanning template to filename.

send(commands: list[tuple[str, str]] | bytes) int[source]

Send commands to LASAF through CAM-socket.

Parameters:

commands (list of tuples or bytes string) – Commands as a list of tuples or a bytes string. cam.prefix is always prepended before sending.

Returns:

Bytes sent.

Return type:

int

Example

>>> # send list of tuples
>>> cam.send([('cmd', 'enableall'), ('value', 'true')])

>>> # send bytes string
>>> cam.send(b'/cmd:enableall /value:true')
start_scan() OrderedDict[str, str][source]

Start the matrix scan.

stop_scan() OrderedDict[str, str][source]

Stop the matrix scan.

wait_for(cmd: str, value: str | None = None, timeout: float = 60) OrderedDict[str, str][source]

Hang until command is received.

If value is supplied, it will hang until cmd:value is received.

Parameters:
  • cmd (string) – Command to wait for in bytestring from microscope CAM interface. If value is falsy, value of received command does not matter.

  • value (string) – Wait until cmd:value is received.

  • timeout (int) – Minutes to wait for command. If timeout is reached, an empty OrderedDict will be returned.

Returns:

Last received message or empty message if timeout is reached.

Return type:

collections.OrderedDict

Submodules

leicacam.async_cam module

Provide an interface using asyncio to the CAM server.

class leicacam.async_cam.AsyncCAM(*args: Any, **kwargs: Any)[source]

Bases: BaseCAM

Driver for LASAF Computer Assisted Microscopy using asyncio.

close() None[source]

Close stream.

async connect() None[source]

Connect to LASAF through a CAM-socket.

async receive() list[OrderedDict[str, str]][source]

Receive message from socket interface as list of OrderedDict.

async send(commands: list[tuple[str, str]] | bytes) None[source]

Send commands to LASAF through CAM-socket.

Parameters:

commands (list of tuples or bytes string) – Commands as a list of tuples or a bytes string. cam.prefix is always prepended before sending.

Returns:

Bytes sent.

Return type:

int

Example

>>> # send list of tuples
>>> await cam.send([('cmd', 'enableall'), ('value', 'true')])

>>> # send bytes string
>>> await cam.send(b'/cmd:enableall /value:true')
async wait_for(cmd: str, value: str | None = None, timeout: float = 60) OrderedDict[str, str][source]

Hang until command is received.

If value is supplied, it will hang until cmd:value is received.

Parameters:
  • cmd (string) – Command to wait for in bytestring from microscope CAM interface. If value is falsy, value of received command does not matter.

  • value (string) – Wait until cmd:value is received.

  • timeout (int) – Minutes to wait for command. If timeout is reached, an empty OrderedDict will be returned.

Returns:

Last received message or empty message if timeout is reached.

Return type:

collections.OrderedDict

leicacam.cam module

Provide an interface to the CAM server.

class leicacam.cam.BaseCAM(host: str = '127.0.0.1', port: int = 8895)[source]

Bases: object

Base driver for LASAF Computer Assisted Microscopy.

class leicacam.cam.CAM(*args: Any, **kwargs: Any)[source]

Bases: BaseCAM

Driver for LASAF Computer Assisted Microscopy.

autofocus_scan() OrderedDict[str, str][source]

Start the autofocus job.

close() None[source]

Close the socket.

connect() None[source]

Connect to LASAF through a CAM-socket.

disable(slide: int = 0, wellx: int = 1, welly: int = 1, fieldx: int = 1, fieldy: int = 1) OrderedDict[str, str][source]

Disable a given scan field.

disable_all() OrderedDict[str, str][source]

Disable all scan fields.

enable(slide: int = 0, wellx: int = 1, welly: int = 1, fieldx: int = 1, fieldy: int = 1) OrderedDict[str, str][source]

Enable a given scan field.

enable_all() OrderedDict[str, str][source]

Enable all scan fields.

flush() None[source]

Flush incoming socket messages.

get_information(about: str = 'stage') OrderedDict[str, str][source]

Get information about given keyword. Defaults to stage.

load_template(filename: str = '{ScanningTemplate}leicacam.xml') OrderedDict[str, str][source]

Load scanning template from filename.

Template needs to exist in database, otherwise it will not load.

Parameters:

filename (str) – Filename to template to load. Filename may contain path also, in such case, the basename will be used. ‘.xml’ will be stripped from the filename if it exists because of a bug; LASAF implicit add ‘.xml’. If ‘{ScanningTemplate}’ is omitted, it will be added.

Returns:

Response from LASAF in an ordered dict.

Return type:

collections.OrderedDict

Example

>>> # load {ScanningTemplate}leicacam.xml
>>> cam.load_template('leicacam')

>>> # load {ScanningTemplate}leicacam.xml
>>> cam.load_template('{ScanningTemplate}leicacam')

>>> # load {ScanningTemplate}leicacam.xml
>>> cam.load_template('/path/to/{ScanningTemplate}leicacam.xml')
pause_scan() OrderedDict[str, str][source]

Pause the matrix scan.

receive() list[OrderedDict[str, str]][source]

Receive message from socket interface as list of OrderedDict.

save_template(filename: str = '{ScanningTemplate}leicacam.xml') OrderedDict[str, str][source]

Save scanning template to filename.

send(commands: list[tuple[str, str]] | bytes) int[source]

Send commands to LASAF through CAM-socket.

Parameters:

commands (list of tuples or bytes string) – Commands as a list of tuples or a bytes string. cam.prefix is always prepended before sending.

Returns:

Bytes sent.

Return type:

int

Example

>>> # send list of tuples
>>> cam.send([('cmd', 'enableall'), ('value', 'true')])

>>> # send bytes string
>>> cam.send(b'/cmd:enableall /value:true')
start_scan() OrderedDict[str, str][source]

Start the matrix scan.

stop_scan() OrderedDict[str, str][source]

Stop the matrix scan.

wait_for(cmd: str, value: str | None = None, timeout: float = 60) OrderedDict[str, str][source]

Hang until command is received.

If value is supplied, it will hang until cmd:value is received.

Parameters:
  • cmd (string) – Command to wait for in bytestring from microscope CAM interface. If value is falsy, value of received command does not matter.

  • value (string) – Wait until cmd:value is received.

  • timeout (int) – Minutes to wait for command. If timeout is reached, an empty OrderedDict will be returned.

Returns:

Last received message or empty message if timeout is reached.

Return type:

collections.OrderedDict

leicacam.cam.bytes_as_dict(msg: bytes) OrderedDict[str, str][source]

Parse CAM message to OrderedDict based on format /key:val.

Parameters:

msg (bytes) – Sequence of /key:val.

Returns:

With /key:val => dict[key] = val.

Return type:

collections.OrderedDict

leicacam.cam.check_messages(msgs: list[OrderedDict[str, str]], cmd: str, value: str | None = None) OrderedDict[str, str] | None[source]

Check if specific message is present.

Parameters:
  • cmd (string) – Command to check for in bytestring from microscope CAM interface. If value is falsy, value of received command does not matter.

  • value (string) – Check if cmd:value is received.

Returns:

Correct message or None if no correct message if found.

Return type:

collections.OrderedDict

leicacam.cam.logger(function: Callable[[P], R]) Callable[[P], R][source]

Decorate passed in function and log message to module logger.

leicacam.cam.tuples_as_bytes(cmds: list[tuple[str, str]]) bytes[source]

Format list of tuples to CAM message with format /key:val.

Parameters:

cmds (list of tuples) – List of commands as tuples.

Returns:

Sequence of /key:val.

Return type:

bytes

Example

>>> tuples_as_bytes([('cmd', 'val'), ('cmd2', 'val2')])
b'/cmd:val /cmd2:val2'
leicacam.cam.tuples_as_dict(_list: list[tuple[str, str]]) OrderedDict[str, str][source]

Translate a list of tuples to OrderedDict with key and val as strings.

Parameters:

_list (list of tuples)

Return type:

collections.OrderedDict

Example

>>> tuples_as_dict([('cmd', 'val'), ('cmd2', 'val2')])
OrderedDict([('cmd', 'val'), ('cmd2', 'val2')])