如何在MicroManager的BeanScript中为ImageJ结果记录时间戳?
解决MicroManager BeanScript中时间戳记录失效问题
我完全不懂编程,现在要记录受规律刺激的心肌细胞中Fluo-4荧光染料随时间变化的强度并生成yz图,用的是基于ImageJ的MicroManager工具,用类似Java的BeanScript写了脚本,多次尝试后得到如下代码:
import ij.*; numberOfImages = 25; //the image window must be already open in the GUI (press "Snap Image" at least once) before running this script IJ.run("Set Measurements...", "integrated redirect=None decimal=3"); for (int i=0; i < numberOfImages; i++) { images = mm.live().snap(true); img = images.get(0); IJ.run("Measure"); IJ.timeStamp = getTime(); IJ.setResult("Time", i, timeStamp); Thread.sleep(10); }
问题是这两行记录时间的代码在ImageJ里去掉前缀“IJ.”能正常运行,但在MicroManager的BeanScript里不行,去掉这两行脚本能跑,但每次循环时长有毫秒级波动,必须记录实际时间才行。脚本管理器报错如下:
Line 14: evaluation error : Sourced file: inline evaluation of: ``import ij.*; numberOfImages = 25; //the image window must be already open in t . . . '' : Field access: bsh.ReflectError: No such field: timeStamp
解决方法
问题核心是MicroManager的BeanScript环境中,IJ类并没有timeStamp这个字段,而且getTime()也不是该环境下的可用函数。直接用Java标准的时间获取方法即可解决:
- 若需要从1970年开始的绝对毫秒时间戳,把原脚本中那两行时间记录代码替换为:
long currentTime = System.currentTimeMillis(); IJ.setResult("Time", i, currentTime);
- 更适合实验场景的是记录相对开始时间的毫秒数,数据更直观,修改方式如下:
- 在循环前先记录初始时间
- 循环内计算已流逝的时间
修改后的完整脚本:
import ij.*; numberOfImages = 25; long startTime = System.currentTimeMillis(); // 记录实验开始时间 //the image window must be already open in the GUI (press "Snap Image" at least once) before running this script IJ.run("Set Measurements...", "integrated redirect=None decimal=3"); for (int i=0; i < numberOfImages; i++) { images = mm.live().snap(true); img = images.get(0); IJ.run("Measure"); long elapsedTime = System.currentTimeMillis() - startTime; // 计算从开始到当前的毫秒数 IJ.setResult("Time", i, elapsedTime); Thread.sleep(10); }
这样就能准确记录每次拍照的实际时间,消除循环波动带来的误差。
内容的提问来源于stack exchange,提问作者goose7
相关产品推荐
相关产品推荐

