<< Main Manual

Tutorial #7. Controlling Micro-Manager through scripts

If PhysImage was installed over a previously installed copy of Micro-Manager as discussed in the Installation Guide, then there a few details to know to control Micro-Manager from Python scripting.

Example #1

Micro-Manager provides its own extensive scripting mechanism for controlling hardware such as cameras, stages, etc. This is through BeanShell (i.e., '.bsh' files). BeanShell is essentially a scripting language that uses Java-language structure and syntax.

The important change to make in BeanShell scripts though, if you would like to call them from Python scripts in PhysImage, is that the following text in the Micro-Manager's Script Panel GUI webpage no longer holds true:

    "Scripts have access to three Micro-Manager specific objects: the Micro-Manager Core (mmc), the Micro-Manager GUI (gui), and the Micro-Manager Acquisition Engine (acq)."

However, these specific objects can easily be made available to the BeanShell script by simply adding the following text to the beginning of the Python-callable .bsh script:

import org.micromanager.*;
import org.micromanager.api.*;
import mmcorej.*;

MMStudioMainFrame gui = MMStudioMainFrame.getInstance();
CMMCore mmc = gui.getMMCore();
AcquisitionEngine acq = gui.getAcquisitionEngine();

Then, the BeanShell script can be written as usual and later called in the following way from Python scripts (or the Jython console) using the "BeanShellRunner_" plugin with the name of the .bsh file passed as argument.

For example:

>># Acquire the online dF/F and imaging time-series
>>IJ.runPlugIn("BeanShellRunner_", "Labjack_sequence.bsh")
>>
>># retrieve the acquired image
>>image = WindowManager.getCurrentImage()
>># do something with 'image'

Note: On a call to 'runPlugIn', the Python scripting execution flow will stop until the .bsh script completes, which is usually the desired intention.

As implied in the example above, after some new data has been acquired through Micro-Manager, the data can be collected by the Python script and processed further as described in previous tutorials.

<< Tutorial #6. Simplifications for writing/using scripts
>> Tutorial #5. Auto-generated figures using Layouts

<< Main Manual