Skip to main content

A Python client for the OpenFlexure Microscope.

The OpenFlexure Microscope is most often controlled by two pieces of software - the OpenFlexure Microscope Server, which is written in Python and runs on the embedded Raspberry Pi, and OpenFlexure Connect which is a graphical interface written in Electron, that can be run either on the Raspberry Pi, or on another computer via a network connection. However, if you want to write your own scripts to perform particular experiments or protocols, it's useful not to have to embed these into the the OpenFlexure Microscope Server or package them up as plugins. This library exists to make it easy to control your microscope from a simple Python script that can run either on the Raspberry Pi, or over the network. I particularly like to use it from a Jupyter notebook on my laptop, because it allows me to plot graphs and display images as I go.

Installation

This module can be installed with pip install openflexure-microscope-client, or by cloning the repository and running poetry install.

Usage

Connect to your microscope

You can connect to the microscope either by specifying a hostname or IP address, or using mDNS. If your network is relatively simple, and if you can see your microscope in the "nearby devices" section of Connect, then mDNS is a zero-faff way of connecting to your microscope:

import openflexure_microscope_client as ofm_client
microscope = ofm_client.find_first_microscope()

If your network is more complicated, or you know the address of your microscope, you can connect using the hostname or IP address. By default, your microscope will declare itself as microscope.local though this also relies on mDNS so if the method above doesn't work, microscope.local may also not work.

microscope = ofm_client.MicroscopeClient("example.host.name")

Check the connection

Usually, I run a few commands to check my microscope is working properly:

pos = microscope.position
starting_pos = pos.copy()
pos['x'] += 100
microscope.move(pos)
assert microscope.position == pos
pos['x'] -= 100
microscope.move(pos)
assert microscope.position == starting_pos

# Check the microscope will autofocus
ret = microscope.autofocus()

# Acquire an image for sanity-checking too
image = microscope.grab_image()
f, ax = plt.subplots(1,1)
ax.imshow(np.array(image))
#print(image.metadata)
print("Active microscope extensions")
for k in microscope.extensions.keys():
    print(k)

After running this block, you should see a list of extensions that are currently active, and a picture taken by the microscope. Given that we just autofocused, this image should be nice and sharp!

Basic commands

There are a few basic commands built in as methods of the MicroscopeClient object:

  • position is a property that returns a dictionary with 'x', 'y', and 'z' components, giving the position of the stage.
  • get_position_array() returns a 3-element numpy array with the same position in it.
  • move(position) accepts either a 3-element array or a dictionary as returned by position, and performs an absolute move
  • move_rel(displacement) performs a relative move (i.e. supplying 0 will not move that axis)
  • capture_image() will take a new image from the camera and return it as a PIL image object
  • grab_image() will return the next image the camera sends in its MJPEG preview stream (i.e. it's quicker but lower quality than capture_image())
  • grab_image_array() returns the image as a numpy array.
  • autofocus() runs the fast autofocus routine, just like clicking the "autofocus" button in Connect

Extensions

To run methods provided by the microscope extensions, you can use the extensions dictionary, to make get or post requests:

microscope.extensions["your.extension.name"]["link_name"].get()
microscope.extensions["your.extension.name"]["link_name"].post_json({"key":"value"})

Arguments and documentation

There is not currently much documentation beyond this README for the Python client. However, the microscope API is pretty well documented. This is the place to go to find out the details of what each command does, and what the arguments mean, particularly for any of the commands that take a payload argument; that's converted to JSON and passed directly to the corresponding HTTP POST request. Currently, there is a static version of the API documentation linked from the server readthedocs page, or you can access a live interactive version from http://microscope.local:5000/api/v2/docs/swagger-ui, replacing microscope.local with the IP or hostname of your microscope.

Development

Installation

The dependencies are managed using poetry, so once you have cloned the repository, you can set up a virtual environment with poetry install.

Tests

There are some basic tests that can be run using pytest. This needs to be within the environment installed above, so use poetry run pytest. These mostly need to connect to a microscope; the easiest way to test this automatically is to build openflexure-microscope-server and run it locally - this will create a local dummy microscope server. Some tests will be skipped because they need microscope hardware, but it should at least verify most of the URLs. Running the tests requires you to have started that server already - it will not be started automatically.

Metadata

Release files for openflexure-microscope-client 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openflexure-microscope-client 0.1.8
File Size Uploaded Provenance Actions
openflexure_microscope_client-0.1.8.tar.gz 7.9 kB Without provenance Details for openflexure_microscope_client-0.1.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for openflexure-microscope-client 0.1.8
File Interpreter ABI Platform Actions
openflexure_microscope_client-0.1.8-py3-none-any.whl Python 3 none any Details for openflexure_microscope_client-0.1.8-py3-none-any.whl

Total release size: 16.7 kB

Release files / openflexure_microscope_client-0.1.8.tar.gz

Download URL openflexure_microscope_client-0.1.8.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
37bc92a210ae94842bf9c04723909f4c70c87188c7d6cca8ac9174f43b7879ca
BLAKE2b-256 checksum
How to use checksums
4f68e03646b36c53b1f1f3e4205314ca876728f9e15763ee479c621367b60cd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/5.4.109+

Release files / openflexure_microscope_client-0.1.8-py3-none-any.whl

Download URL openflexure_microscope_client-0.1.8-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e72d3678e18efe05006d87838cdc4f02d200bc7a2a28fbea2225f81283c6e350
BLAKE2b-256 checksum
How to use checksums
2ee8cf15168b63a29a5714ebe055e38f383ebc30ffe15831461dc0cd3f2d12eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.17 Linux/5.4.109+

Security for openflexure-microscope-client 0.1.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page