def run(self, module_info, pre=None, post=None, **kwargs): '''Run a module module_info - the module_info of the module to run pre - list of PreprocessorPlugins to run before running module post - list of PostprocessorPlugins to run after running module *kwargs - names and values for input parameters ''' input_map = J.make_map(kwargs) if pre is not None: pre = J.static_call("java/util/Arrays", "asList", "([Ljava/lang/Object;)Ljava/util/List;", pre) if post is not None: post = J.static_call("java/util/Arrays", "asList", "([Ljava/lang/Object;)Ljava/util/List;", post) future = J.call( self.o, "run", "(Limagej/module/ModuleInfo;" "Ljava/util/List;" "Ljava/util/List;" "Ljava/util/Map;)" "Ljava/util/concurrent/Future;", module_info, pre, post, input_map) return J.call( self.o, "waitFor", "(Ljava/util/concurrent/Future;)Limagej/module/Module;", future)
def run(self, module_info, pre = None, post = None, **kwargs): '''Run a module module_info - the module_info of the module to run pre - list of PreprocessorPlugins to run before running module post - list of PostprocessorPlugins to run after running module *kwargs - names and values for input parameters ''' input_map = J.make_map(kwargs) if pre is not None: pre = J.static_call("java/util/Arrays", "asList", "([Ljava/lang/Object;)Ljava/util/List;", pre) if post is not None: post = J.static_call("java/util/Arrays", "asList", "([Ljava/lang/Object;)Ljava/util/List;", post) future = J.call( self.o, "run", "(Limagej/module/ModuleInfo;" "Ljava/util/List;" "Ljava/util/List;" "Ljava/util/Map;)" "Ljava/util/concurrent/Future;", module_info, pre, post, input_map) return J.call( self.o, "waitFor", "(Ljava/util/concurrent/Future;)Limagej/module/Module;", future)
def run_command(self, command_class, inputs, outputs): '''Run an imageJ command command_class - the command's dotted class name inputs - a dictionary of key/value pairs for the command's inputs outputs - a dictionary of keys to be filled with the command's outputs ''' module_index = ij2.get_module_service(self.context).getIndex() module_infos = module_index.getS('imagej.command.CommandInfo') module_info = filter(lambda x:x.getClassName() == command_class, module_infos)[0] svc = ij2.get_command_service(self.context) input_map = J.make_map(**inputs) future = svc.run(module_info.o, input_map.o) module = future.get() for key in list(outputs): module_item = module_info.getOutput(key) outputs[key] = module_item.getValue(module)
def do_teest(self, module, channels, expected_tags, expected_metadata, additional=None): '''Ensure that NamesAndTypes recreates the column layout when run module - instance of NamesAndTypes, set up for the test channels - a dictionary of channel name to list of "ipds" for that channel where "ipd" is a tuple of URL and metadata dictionary Entries may appear multiple times (e.g. illumination function) expected_tags - the metadata tags that should have been generated by prepare_run. expected_metadata - a sequence of two-tuples of metadata key and the channel from which the metadata is extracted. additional - if present, these are added as ImagePlaneDetails in order to create errors. Format is same as a single channel of channels. ''' ipds = [] urls = set() url_root = "file:" + urllib.pathname2url(os.path.abspath(os.path.curdir)) channels = dict(channels) if additional is not None: channels["Additional"] = additional for channel_name in list(channels): channel_data = [(url_root + "/" + path, metadata) for path, metadata in channels[channel_name]] channels[channel_name] = channel_data for url, metadata in channel_data: if url in urls: continue urls.add(url) ipd = cpp.ImagePlaneDetails(url, None, None, None, **metadata) jmetadata = J.make_map(**metadata) ipd.jipd = J.run_script(""" importPackage(Packages.org.cellprofiler.imageset); importPackage(Packages.org.cellprofiler.imageset.filter); var imageFile=new ImageFile(new java.net.URI(url)); var imagePlane=new ImagePlane(imageFile); new ImagePlaneDetails(imagePlane, metadata); """, dict(url=url, metadata=jmetadata)) ipds.append(ipd) if additional is not None: del channels["Additional"] ipds.sort(key = lambda x: x.url) pipeline = cpp.Pipeline() del pipeline.image_plane_details[:] pipeline.image_plane_details.extend(ipds) module.module_num = 1 pipeline.add_module(module) m = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, m, None, m, None) self.assertTrue(module.prepare_run(workspace)) tags = m.get_metadata_tags() self.assertEqual(len(tags), len(expected_tags)) for tag, expected_tag in zip(tags, expected_tags): for et in expected_tag: ftr = et if et == cpmeas.IMAGE_NUMBER else \ "_".join((cpmeas.C_METADATA, et)) if ftr == tag: break else: self.fail("%s not in %s" % (tag, ",".join(expected_tag))) iscds = m.get_channel_descriptors() self.assertEqual(len(iscds), len(channels)) for channel_name in channels.keys(): iscd_match = filter(lambda x:x.name == channel_name, iscds) self.assertEquals(len(iscd_match), 1) iscd = iscd_match[0] assert isinstance(iscd, cpp.Pipeline.ImageSetChannelDescriptor) for i, (expected_url, metadata) in enumerate(channels[channel_name]): image_number = i+1 if iscd.channel_type == iscd.CT_OBJECTS: url_ftr = "_".join((cpmeas.C_OBJECTS_URL, channel_name)) else: url_ftr = "_". join((cpmeas.C_URL, channel_name)) self.assertEqual(expected_url, m[cpmeas.IMAGE, url_ftr, image_number]) for key, channel in expected_metadata: if channel != channel_name: continue md_ftr = "_".join((cpmeas.C_METADATA, key)) self.assertEqual(metadata[key], m[cpmeas.IMAGE, md_ftr, image_number])
def do_teest(self, module, channels, expected_tags, expected_metadata, additional=None): '''Ensure that NamesAndTypes recreates the column layout when run module - instance of NamesAndTypes, set up for the test channels - a dictionary of channel name to list of "ipds" for that channel where "ipd" is a tuple of URL and metadata dictionary Entries may appear multiple times (e.g. illumination function) expected_tags - the metadata tags that should have been generated by prepare_run. expected_metadata - a sequence of two-tuples of metadata key and the channel from which the metadata is extracted. additional - if present, these are added as ImagePlaneDetails in order to create errors. Format is same as a single channel of channels. ''' ipds = [] urls = set() url_root = "file:" + urllib.pathname2url( os.path.abspath(os.path.curdir)) channels = dict(channels) if additional is not None: channels["Additional"] = additional for channel_name in list(channels): channel_data = [(url_root + "/" + path, metadata) for path, metadata in channels[channel_name]] channels[channel_name] = channel_data for url, metadata in channel_data: if url in urls: continue urls.add(url) ipd = cpp.ImagePlaneDetails(url, None, None, None, **metadata) jmetadata = J.make_map(**metadata) ipd.jipd = J.run_script( """ importPackage(Packages.org.cellprofiler.imageset); importPackage(Packages.org.cellprofiler.imageset.filter); var imageFile=new ImageFile(new java.net.URI(url)); var imagePlane=new ImagePlane(imageFile); new ImagePlaneDetails(imagePlane, metadata); """, dict(url=url, metadata=jmetadata)) ipds.append(ipd) if additional is not None: del channels["Additional"] ipds.sort(key=lambda x: x.url) pipeline = cpp.Pipeline() del pipeline.image_plane_details[:] pipeline.image_plane_details.extend(ipds) module.module_num = 1 pipeline.add_module(module) m = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, m, None, m, None) self.assertTrue(module.prepare_run(workspace)) tags = m.get_metadata_tags() self.assertEqual(len(tags), len(expected_tags)) for tag, expected_tag in zip(tags, expected_tags): for et in expected_tag: ftr = et if et == cpmeas.IMAGE_NUMBER else \ "_".join((cpmeas.C_METADATA, et)) if ftr == tag: break else: self.fail("%s not in %s" % (tag, ",".join(expected_tag))) iscds = m.get_channel_descriptors() self.assertEqual(len(iscds), len(channels)) for channel_name in channels.keys(): iscd_match = filter(lambda x: x.name == channel_name, iscds) self.assertEquals(len(iscd_match), 1) iscd = iscd_match[0] assert isinstance(iscd, cpp.Pipeline.ImageSetChannelDescriptor) for i, (expected_url, metadata) in enumerate(channels[channel_name]): image_number = i + 1 if iscd.channel_type == iscd.CT_OBJECTS: url_ftr = "_".join((cpmeas.C_OBJECTS_URL, channel_name)) else: url_ftr = "_".join((cpmeas.C_URL, channel_name)) self.assertEqual(expected_url, m[cpmeas.IMAGE, url_ftr, image_number]) for key, channel in expected_metadata: if channel != channel_name: continue md_ftr = "_".join((cpmeas.C_METADATA, key)) self.assertEqual(metadata[key], m[cpmeas.IMAGE, md_ftr, image_number])