Python client code for the OpenFlexure Microscope
Project description
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 eV 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 in eV 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 is a single-file module, so for now you can just copy the Python file. Hopefully we'll set it up as a pip-installable package in the very near future.
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 eV, 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-elementnumpy
array with the same position in it.move(position)
accepts either a 3-element array or a dictionary as returned byposition
, and performs an absolute movemove_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 aPIL
image objectgrab_image()
will return the next image the camera sends in its MJPEG preview stream (i.e. it's quicker but lower quality thancapture_image()
)grab_image_array()
returns the image as anumpy
array.autofocus()
runs the fast autofocus routine, just like clicking the "autofocus" button in eV
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"})
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
Built Distribution
Hashes for openflexure-microscope-client-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 341d2ae9bd3130515a2f2d7d0fa3649056aef9a94787b37abc8f5754a3165214 |
|
MD5 | bdd32c5530c13780333005fd80c61d49 |
|
BLAKE2b-256 | 83683703b2473ed89c4bb3f1037ae695f912a8626b6576007345e87cbf9bc7f9 |
Hashes for openflexure_microscope_client-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5415ec3192a48a4b0134eb87c31f8cdf17419617e8031ccca343472adc2848d |
|
MD5 | 10a22199f0878bdbfa1b5b2c32d78555 |
|
BLAKE2b-256 | 8ea44736ccca977d660e00390c3d3421d32453707abb7bc0f5d81bddc3130ab4 |