top bar

Difference between revisions of "Using the Micro-Manager python library"

(Add currently supported NumPy version)
(Python programming introduction)
Line 1: Line 1:
'''MMCorePy''' is a python library that wraps the Micro-Manager C++ core (on both Mac and Windows). The library allows you to control microscope hardware from python scripts or a python command line.  
+
'''MMCorePy''' is a wrapper that allows you to control microscope hardware from python interactive session or scripts. It's support  Windows, Mac and Linux.
  
Here is how to get started with the Micro-Manager python library:
+
Micromanager's main parts:
 +
* CMMCore - basic module, written in C++. Script languages like python just wrap it by [http://www.swig.org swig].
 +
* Device adapters - various libraries that allow support for various hardware. If you want to built one and extend MM devise support, feel free to use [[Micro-Manager Device Developer's Kit for Windows|SDK]].
 +
* MMCorePy - python wrapper. MM build scripts are support both python 2 and 3, but windows version ships with python 2 bindings only.
 +
* MMCoreJ - java wrapper
 +
* MMStudio - Micromanager GUI (technically it is ImageJ plugin).
  
# [[Download_Micro-Manager_Latest_Release|Download]] and install Micro-Manager on your computer.
+
== Environment setup ==
# Familiarize yourself with Micro-Manager and learn how to connect it to your hardware by using the existing Java-based GUI (graphical user interface).
+
You must install python2 and numpy. Windows users may prefer using an python distribution instead manual separate installation.
# Install Python and Numpy:
+
 
#* Option 1: Python distribution<br>It's convenient to install a distribution which includes Python, numpy, scientific libraries, GUI frameworks and IDEs:
+
=== Manual ===
#** [http://www.enthought.com/products/epd.php Enthought's python distribution (EPD)] is [http://www.enthought.com/products/edudownload.php free for academic use]
+
* [http://python.org/ python 2.7.x] (python2 is default for windows now)
#* Option 2: Manual<br>Individually install packages for python and other modules
+
* [http://scipy.org/ numpy 1.7.x] Micormanager represent imaging data as multidimensional numpy arrays.
#** Required:
+
 
#*** [http://python.org/ python 2.7.x] (python 3.x is not yet supported)
+
=== Using python distributions ===
#*** [http://numpy.org/ numpy 1.7.x]
+
It's convenient to install a distribution which includes Python, numpy, scientific libraries, GUI frameworks and IDEs. All distributions have a free version, some of them have extended paid version, but you can request free academic license.
#** Optional:
+
* [https://www.enthought.com/products/epd/free/ Enthought's python distribution (EPD)]
#*** [http://scipy.org/ scipy] for advanced image processing
+
* [http://continuum.io/downloads Anaconda], has package manager.
#*** [http://matplotlib.sourceforge.net/ matplotlib] useful for displaying images
+
* [https://code.google.com/p/pythonxy PythonXY] totally free.
#*** [http://ipython.scipy.org/moin/ ipython] a nice interactive python environment
+
 
#*** [http://www.pythonware.com/products/pil/ PIL (Python Imaging Library)] for image processing and file saving
+
=== Useful libraries ===
# MMCorePy.py will be located in the main Micro-Manager directory; it requires many library files in that directory (with suffixes like .dll, .so, .pyd) to operate. At present it is necessary to run python from inside the Micro-Manager directory so that these files can be discovered.<!-- You can make the Micro-Manager python wrapper library work from any directory by adding your micro-manager directory to the [http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH PYTHONPATH] environment variable. -->
+
* Scipy - scientific algorithms, multidimensional image processing toolbox.
 +
* Matplotlib - fastest way to show your image data.
 +
* Opencv - computer vision and image processing library. Sometimes faster than scipy.
 +
* Pillow - very basic image processing. Scipy uses it for image loading and writing.
 +
* Scikit-image - "pythonic" scientific-oriented image processing algorithms collection.
 +
* [http://ipython.org/ IPython] - improved interactive python environment
 +
 
 +
== Micromanager installation ==
 +
 
 +
=== Windows & Mac ===
 +
[[Download_Micro-Manager_Latest_Release|Download]] and install Micro-Manager on your computer. Add Micromanager installation folder to [https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH PYTHONPATH] (i.e. "''C:\Program Files\Micro-Manager-1.4''", it should contain <small>''MMCorePy.py''</small> and <small>''_MMCorePy.pyd''</small> files). Create variable if it not exist. At now you can import MMCorePy without an error.
 +
 
 +
=== Linux ===
 +
MM package from your distribution repository, in most cases, ships with CMMCore, MMCorePy (python 2 or 3 wrapper) and MMCoreJ, but without GUI. At least your system variables should be already configured properly. Python and Numpy would be installed by your package manager as dependency.
 +
 
 +
== First steps ==
 +
Familiarize yourself with Micro-Manager and learn how to connect it to your hardware by MMStudio GUI.
 +
* Find your device on [[Device Support|this page]] and figure out what adapter you need.
 +
* [[Micro-Manager Configuration Guide|Micro-Manager Configuration Guide]] help you to understand how properties work.
 +
* Read the general [[Micro-Manager Programming Guide|Micro-Manager Programming Guide]]
  
 
Images returned by calls to an instance of the pythonized Micro-Manager core class ([http://www.micro-manager.org/content/doc/mmcore_api/html/class_c_m_m_core.html CMMCore]) are stored in numpy arrays for convenience, and can be easily displayed by pylab (matplotlib) commands. Here is a short example (using matplotlib) that demonstrates use of the python wrapper to acquire and display an image.
 
Images returned by calls to an instance of the pythonized Micro-Manager core class ([http://www.micro-manager.org/content/doc/mmcore_api/html/class_c_m_m_core.html CMMCore]) are stored in numpy arrays for convenience, and can be easily displayed by pylab (matplotlib) commands. Here is a short example (using matplotlib) that demonstrates use of the python wrapper to acquire and display an image.
Line 42: Line 66:
 
</code>
 
</code>
  
A longer example script, MMCoreWrapDemo.py, is available in the Micro-Manager root directory.
+
A longer example script, [https://github.com/mdcurtis/micromanager-upstream/blob/master/bindist/any-platform/MMCoreWrapDemo.py MMCoreWrapDemo.py], is available in the Micro-Manager root directory.
 +
 
 +
Also check out [https://github.com/radioxoma/micromanager-samples micromanager-samples repo] for python code samples (live video acquisition, property discovery etc).
 +
 
 +
== Further reading ==
 +
* [http://scipy-lectures.github.io/advanced/image_processing/index.html Image manipulation and processing using Numpy and Scipy] by Python Scientific Lecture Notes.
 +
* [http://scikit-image.org/docs/dev/auto_examples Scikits-image gallery]
 +
* [https://github.com/jrjohansson/scientific-python-lectures Lectures on scientific computing with Python]
 +
* [http://docs.opencv.org/trunk/doc/py_tutorials/py_tutorials.html OpenCV-Python Tutorials]
 +
 
 +
== Micromanager code ==
 +
 
 +
* [https://github.com/mdcurtis/micromanager-upstream Micromaanger SVN-tracking repository on Github]
 +
 
 +
Written by Eugene Dvoretsky -- [[User:Radioxoma|Radioxoma]] ([[User talk:Radioxoma|talk]]) 10:00, 8 June 2014 (PDT)
  
 
{{Programming_Sidebar}}
 
{{Programming_Sidebar}}

Revision as of 10:00, 8 June 2014

MMCorePy is a wrapper that allows you to control microscope hardware from python interactive session or scripts. It's support Windows, Mac and Linux.

Micromanager's main parts:

  • CMMCore - basic module, written in C++. Script languages like python just wrap it by swig.
  • Device adapters - various libraries that allow support for various hardware. If you want to built one and extend MM devise support, feel free to use SDK.
  • MMCorePy - python wrapper. MM build scripts are support both python 2 and 3, but windows version ships with python 2 bindings only.
  • MMCoreJ - java wrapper
  • MMStudio - Micromanager GUI (technically it is ImageJ plugin).

Environment setup

You must install python2 and numpy. Windows users may prefer using an python distribution instead manual separate installation.

Manual

  • python 2.7.x (python2 is default for windows now)
  • numpy 1.7.x Micormanager represent imaging data as multidimensional numpy arrays.

Using python distributions

It's convenient to install a distribution which includes Python, numpy, scientific libraries, GUI frameworks and IDEs. All distributions have a free version, some of them have extended paid version, but you can request free academic license.

Useful libraries

  • Scipy - scientific algorithms, multidimensional image processing toolbox.
  • Matplotlib - fastest way to show your image data.
  • Opencv - computer vision and image processing library. Sometimes faster than scipy.
  • Pillow - very basic image processing. Scipy uses it for image loading and writing.
  • Scikit-image - "pythonic" scientific-oriented image processing algorithms collection.
  • IPython - improved interactive python environment

Micromanager installation

Windows & Mac

Download and install Micro-Manager on your computer. Add Micromanager installation folder to PYTHONPATH (i.e. "C:\Program Files\Micro-Manager-1.4", it should contain MMCorePy.py and _MMCorePy.pyd files). Create variable if it not exist. At now you can import MMCorePy without an error.

Linux

MM package from your distribution repository, in most cases, ships with CMMCore, MMCorePy (python 2 or 3 wrapper) and MMCoreJ, but without GUI. At least your system variables should be already configured properly. Python and Numpy would be installed by your package manager as dependency.

First steps

Familiarize yourself with Micro-Manager and learn how to connect it to your hardware by MMStudio GUI.

Images returned by calls to an instance of the pythonized Micro-Manager core class (CMMCore) are stored in numpy arrays for convenience, and can be easily displayed by pylab (matplotlib) commands. Here is a short example (using matplotlib) that demonstrates use of the python wrapper to acquire and display an image.

# Create a Micro-Manager core object:
import MMCorePy
mmc = MMCorePy.CMMCore()

#  Load and initialize the demo camera device:
mmc.loadDevice("cam","DemoCamera","DCam")
mmc.initializeDevice("cam")

# Snap and retrieve an image:
mmc.snapImage()
im1 = mmc.getImage()

# Display the image:
from pylab import *
ion() # Activate interactive mode
figure()
imshow(im1,cmap = cm.gray)

A longer example script, MMCoreWrapDemo.py, is available in the Micro-Manager root directory.

Also check out micromanager-samples repo for python code samples (live video acquisition, property discovery etc).

Further reading

Micromanager code

Written by Eugene Dvoretsky -- Radioxoma (talk) 10:00, 8 June 2014 (PDT)

© Micro-Manager : Vale Lab, UCSF 2006-2011 | All Rights Reserved | Contact