Skip to content

Instantly share code, notes, and snippets.

@omsai
Last active August 29, 2015 14:00
Show Gist options
  • Save omsai/754065720036acece39a to your computer and use it in GitHub Desktop.
Save omsai/754065720036acece39a to your computer and use it in GitHub Desktop.
Micro-Manager beanshell script to get time interval between frames in a time series.
// Prints time intervals from most recently selected MDA data window.
//
// Reads "ElapsedTime-ms" property from image metadata to calculate
// image intervals. Sets first frame to 0 ms.
//
// One current limitation of the MM API (and therefore this script) is
// we to the most recently selected MDA data window (it's possible the
// user most recently selected the snap window or an ImageJ image
// window), so one has to prompt the user to select an MDA data
// window. One could add a "getActiveAcquisitionName()" to the gui
// script API, and/or write an MM plugin that could e.g. refresh
// whenever the user selects an MDA data window.
// Preferences.
useHWTimestampIfAvailable = true;
// Find the current active window (can be MM MDA data, MM Snap/Live or
// ImageJ window).
import ij.WindowManager;
activeWindow = WindowManager.getActiveWindow();
if (activeWindow == null) {
print("Error: No image window is open. Please acquire or " +
"open an image series.");
return;
}
title = activeWindow.title;
print("Found most recently selected window \"" + title + "\".");
// Get number of time points (nFrames).
nFrames = activeWindow.getImagePlus().getNFrames();
// There is no easy way to get references to open MDA acquisitions, so
// we have to prompt the user to select an MDA data window (>_<).
if (nFrames == 1) {
print("Error: Image window \"" + title + "\" should have at " +
"least 2 time points to calculate frame intervals." +
" Please click on a valid MDA data window.");
return;
}
// Get MM image cache.
cache = gui.getCacheForWindow(activeWindow);
// Prefer hardware timestamps if available. The camera suppliers
// don't seem to define a consistent tag for this. For now, assume
// the hardware timestamp also contains "ElapsedTime-ms".
timestampSource = "builtin";
builtinTimestampTag = "ElapsedTime-ms";
timestampTag = builtinTimestampTag;
if (useHWTimestampIfAvailable) {
keys = cache.getImageTags(0, 0, 0, 0).keys();
for (k : keys)
if(k.contains(builtinTimestampTag) && !k.equals(builtinTimestampTag)) {
timestampTag = k;
timestampSource = "hardware";
}
}
// Get time of first image.
prevElapsedTime = cache.getImageTags(0, 0, 0, 0).get(timestampTag);
print("Reading " + timestampSource + " timestamps (Frame: Timestamp) ...");
parseAndorTimestamp(s) {
// Andor cameras use a string format, which we have to work around:
// Property="Neo 5.5 CL 3 Tap-ElapsedTime-ms"
// Value="34.0011 [0.0340011 seconds]"
spaceChar = s.indexOf(' ');
s = s.substring(0, spaceChar);
return Double.parseDouble(s);
}
if (prevElapsedTime.getClass().equals(String.class))
prevElapsedTime = parseAndorTimestamp(prevElapsedTime);
// Get intervals between frames.
for (int i = 1; i < nFrames; i++) {
elapsedTime = cache.getImageTags(0, 0, i, 0).get(timestampTag);
if (elapsedTime.getClass().equals(String.class))
elapsedTime = parseAndorTimestamp(elapsedTime);
intervalTime = elapsedTime - prevElapsedTime;
print(i + ": " + intervalTime);
prevElapsedTime = elapsedTime;
}
print("Done.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment