Python is an excellent language for automating experiments. It talks to almost any instrument, it has the best tools for analysing data, and you can grow a quick script into a reliable program for the whole lab. This page collects every guide on Python for the Lab, ordered roughly in the way a lab project develops: from talking to a device, to acquiring data without blocking, to storing it, to sharing the code with colleagues.
If you prefer to follow a single, complete project from start to finish, that is what the Python for the Lab book covers. For research groups, there are also hands-on workshops.
Controlling instruments with Python
Most lab devices communicate through a serial port, USB, or the network. Once you understand the pattern (send a command, read the answer, convert it to useful units), every new device becomes easier.
- How to program and control an Arduino with Python: write firmware that behaves like a lab instrument and talk to it with pySerial. A good first project before working with expensive equipment.
- Building the Python for the Lab DAQ: an Arduino-based acquisition device with analog inputs and outputs, the companion hardware of the book.
- Controlling a National Instruments card with Python: use a DAQ card from Python instead of LabVIEW.
- pypylon tutorial: acquiring images from Basler cameras: exposure, free-run acquisition, grab strategies, triggers and saving movies.
- How to control a device through the network: expose an instrument connected to another computer through a small web server.
- How to write a driver with Lantz: a framework that adds units, validation and logging to your drivers.
- How to use decorators to validate input: stop invalid values before they reach the device.
- The with statement and custom classes: open and close instruments safely, even when something fails.
Acquiring data without blocking: threads, processes and messaging
An experiment often needs to do several things at once: acquire, display, and save. These articles show how to do it without freezing the program.
- Implementing threads for measurements: run an acquisition in the background, plot live data, and choose between threads and processes.
- Starting and synchronizing threads: locks, events, and stopping threads cleanly.
- Handling and sharing data between threads: queues, updating a GUI from another thread, and free-threaded Python.
- Differences of multiprocessing on Windows and Linux: why code that runs on one system breaks on the other.
- Using pyZMQ for inter-process communication, part 1 and part 2: send camera frames between processes and build acquisition pipelines.
- PyNTA: nanoparticle tracking analysis: a complete desktop application built with these tools.
Building a user interface
- Step by step guide to building a GUI: a PyQt interface for a camera, using the model-view-controller pattern.
Storing experimental data
- Introduction to storing data in files: plain text files with NumPy, columns and metadata.
- Storing binary data and serializing:
np.save, pickle and JSON. - How to use HDF5 files in Python: large datasets, compression, chunking and metadata with h5py.
- HDF5 vs SQLite vs NumPy vs Zarr: which format to choose for scientific data, and how to combine them.
- Storing data with SQLite: searchable records, and how to combine SQLite with HDF5.
- Ready-to-publish Matplotlib figures: figure size, DPI and fonts for papers and presentations.
Turning scripts into lab software
Code that controls an experiment lives for years and is used by people who did not write it. These guides help you organize it.
- Virtual environments: keep the dependencies of each project separate.
- Complete guide to imports in Python: absolute and relative imports, and fixing the most common errors.
- Packaging a Python project with pyproject.toml and setup.py: install your code in any environment.
- Documenting with Sphinx and Read the Docs: documentation that the next PhD student will thank you for.
- Learning (not) to handle exceptions: deal with devices that time out or disconnect.
Learn it all in one project
The Python for the Lab book follows a single project from the first command sent to a device to a complete program with a user interface, written with the best practices discussed above. The workshops cover the same material in three days, on site, for research groups and companies.