def get_commands(): '''Return a list of the available command strings''' hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: # # This is a little bogus, but works - trick IJ into initializing # execute_command("pleaseignorethis") hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: return [] keys = J.call(hashtable, "keys", "()Ljava/util/Enumeration;") keys = J.jenumeration_to_string_list(keys) values = J.call(hashtable, "values", "()Ljava/util/Collection;") values = [ J.to_string(x) for x in J.iterate_java( J.call(values, 'iterator', "()Ljava/util/Iterator;")) ] class CommandList(list): def __init__(self): super(CommandList, self).__init__(keys) self.values = values return CommandList()
def get_commands(): """Return a list of the available command strings""" script = """ new java.util.concurrent.Callable() { call: function() { importClass(Packages.ij.Menus, Packages.ij.IJ); var hashtable=Menus.getCommands(); if (hashtable==null) { IJ.run("pleaseignorethis"); hashtable = Menus.getCommands(); } return hashtable; } }; """ c = J.run_script(script, class_loader=get_user_loader()) hashtable = J.execute_callable_in_main_thread(c) keys = J.call(hashtable, "keys", "()Ljava/util/Enumeration;") keys = J.jenumeration_to_string_list(keys) values = J.call(hashtable, "values", "()Ljava/util/Collection;") values = [J.to_string(x) for x in J.iterate_java(J.call(values, "iterator", "()Ljava/util/Iterator;"))] class CommandList(list): def __init__(self): super(CommandList, self).__init__(keys) self.values = values return CommandList()
def getModules(self): modules = J.call(o, "getModules", "()Ljava/util/List;") if modules is None: return [] module_iterator = J.call(modules, "iterator", "()Ljava/util/Iterator;") return [wrap_module_info(x) for x in J.iterate_java(module_iterator)]
def getModules(self): modules = J.call(o, "getModules", "()Ljava/util/List;") if modules is None: return [] module_iterator = J.call(modules, "iterator", "()Ljava/util/Iterator;") return [ModuleInfo(x) for x in J.iterate_java(module_iterator)]
def get_fields_and_parameters_from_iterator(parameters): iterator = J.call(parameters, 'iterator', '()L%(ITERATOR_CLASS)s;' % globals()) result = [] for jfield in J.iterate_java(iterator): field = J.get_field_wrapper(jfield) parameter = field.getAnnotation(PARAMETER_CLASS.replace('/','.')) if parameter is not None: parameter = get_parameter_wrapper(parameter) result.append((field, parameter)) return result
def get_fields_and_parameters_from_iterator(parameters): iterator = J.call(parameters, 'iterator', '()L%(ITERATOR_CLASS)s;' % globals()) result = [] for jfield in J.iterate_java(iterator): field = J.get_field_wrapper(jfield) parameter = field.getAnnotation(PARAMETER_CLASS.replace('/', '.')) if parameter is not None: parameter = get_parameter_wrapper(parameter) result.append((field, parameter)) return result
def fn(): hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: # # This is a little bogus, but works - trick IJ into initializing # execute_command("pleaseignorethis") hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: return [] keys = J.call(hashtable, "keys", "()Ljava/util/Enumeration;") keys = J.jenumeration_to_string_list(keys) values = J.call(hashtable, "values", "()Ljava/util/Collection;") values = [J.to_string(x) for x in J.iterate_java( J.call(values, 'iterator', "()Ljava/util/Iterator;"))] class CommandList(list): def __init__(self): super(CommandList, self).__init__(keys) self.values = values return CommandList()
def prepare_run(self, workspace): '''Initialize the pipeline's metadata''' if workspace.pipeline.in_batch_mode(): return True file_list = workspace.file_list pipeline = workspace.pipeline ipds = pipeline.get_filtered_image_plane_details(workspace) extractor = self.build_extractor() max_series = 0 max_index = 0 for ipd in ipds: if ipd.series is not None: max_series = max(max_series, ipd.series) if ipd.index is not None: max_index = max(max_index, ipd.index) if max_series > 0: series_digits = int(np.log10(max_series)) + 1 else: series_digits = 1 if max_index > 0: index_digits = int(np.log10(max_index)) + 1 else: index_digits = 1 if max_series > 0 or max_index > 0: script = """ importPackage(Packages.org.cellprofiler.imageset); extractor.addImagePlaneExtractor(new SeriesIndexMetadataExtractor( seriesDigits, indexDigits)); """ J.run_script(script, dict(extractor = extractor, seriesDigits = series_digits, indexDigits = index_digits)) env = J.get_env() entry_set_class = env.find_class("java/util/Map$Entry") get_key_id = env.get_method_id(entry_set_class, "getKey", "()Ljava/lang/Object;") get_value_id = env.get_method_id(entry_set_class, "getValue", "()Ljava/lang/Object;") def wrap_entry_set(o): return (env.get_string_utf(env.call_method(o, get_key_id)), env.get_string_utf(env.call_method(o, get_value_id))) # # Much of what appears below is optimized to avoid the cost of # "getting nice arguments" for the Java bridge. The IPDs should be # in alphabetical order which means that, for stacks, we can # save the results of OME-XML parsing in the Java ImageFile object. # extractor_class = env.find_class( "org/cellprofiler/imageset/ImagePlaneMetadataExtractor") extract_metadata_id = env.get_method_id( extractor_class, "extractMetadata", "(Ljava/lang/String;IILjava/lang/String;" "[Lorg/cellprofiler/imageset/filter/ImagePlaneDetails;" "[Lorg/cellprofiler/imageset/ImageFile;" ")Ljava/util/Iterator;") extract_metadata_if_id = env.get_method_id( extractor_class, "extractMetadata", "(Lorg/cellprofiler/imageset/ImageFile;II" "[Lorg/cellprofiler/imageset/filter/ImagePlaneDetails;" ")Ljava/util/Iterator;") ipd_class = env.find_class("org/cellprofiler/imageset/filter/ImagePlaneDetails") if_class = env.find_class("org/cellprofiler/imageset/ImageFile") clear_xml_document_id = env.get_method_id( if_class, "clearXMLDocument", "()V") pIPD = env.make_object_array(1, ipd_class) pIF = env.make_object_array(1, if_class) last_url = None last_if = None if_has_metadata = False for ipd in ipds: series, index = [x if x is not None else 0 for x in ipd.series, ipd.index] if ipd.url != last_url: if if_has_metadata: env.call_method(last_if, clear_xml_document_id) x = env.exception_occurred() if x is not None: raise J.JavaException(x) if_has_metadata = False xmlmetadata = file_list.get_metadata(ipd.url) if xmlmetadata is not None: xmlmetadata = env.new_string_utf(xmlmetadata) if_has_metadata = True metadata = env.call_method(extractor, extract_metadata_id, env.new_string_utf(ipd.url), int(series), int(index), xmlmetadata, pIPD, pIF) x = env.exception_occurred() if x is not None: raise J.JavaException(x) last_url = ipd.url last_if = env.get_object_array_elements(pIF)[0] else: metadata = env.call_method( extractor, extract_metadata_if_id, last_if, int(series), int(index), pIPD) x = env.exception_occurred() if x is not None: raise J.JavaException(x) ipd.metadata.update(J.iterate_java(metadata, wrap_entry_set)) ipd.jipd = env.get_object_array_elements(pIPD)[0] if if_has_metadata: env.call_method(last_if, clear_xml_document_id) x = env.exception_occurred() if x is not None: raise J.JavaException(x) return True
def getOutputs(self): outputs = J.call(self.o, "outputs", "()Ljava/lang/Iterable;") output_iterator = J.call(outputs, "iterator", "()Ljava/util/Iterator;") return [ModuleItem(o) for o in J.iterate_java(output_iterator)]
def prepare_run(self, workspace): '''Initialize the pipeline's metadata''' if workspace.pipeline.in_batch_mode(): return True file_list = workspace.file_list pipeline = workspace.pipeline ipds = pipeline.get_filtered_image_plane_details(workspace) extractor = self.build_extractor() max_series = 0 max_index = 0 for ipd in ipds: if ipd.series is not None: max_series = max(max_series, ipd.series) if ipd.index is not None: max_index = max(max_index, ipd.index) if max_series > 0: series_digits = int(np.log10(max_series)) + 1 else: series_digits = 1 if max_index > 0: index_digits = int(np.log10(max_index)) + 1 else: index_digits = 1 if max_series > 0 or max_index > 0: script = """ importPackage(Packages.org.cellprofiler.imageset); extractor.addImagePlaneExtractor(new SeriesIndexMetadataExtractor( seriesDigits, indexDigits)); """ J.run_script( script, dict(extractor=extractor, seriesDigits=series_digits, indexDigits=index_digits)) env = J.get_env() entry_set_class = env.find_class("java/util/Map$Entry") get_key_id = env.get_method_id(entry_set_class, "getKey", "()Ljava/lang/Object;") get_value_id = env.get_method_id(entry_set_class, "getValue", "()Ljava/lang/Object;") def wrap_entry_set(o): return (env.get_string_utf(env.call_method(o, get_key_id)), env.get_string_utf(env.call_method(o, get_value_id))) # # Much of what appears below is optimized to avoid the cost of # "getting nice arguments" for the Java bridge. The IPDs should be # in alphabetical order which means that, for stacks, we can # save the results of OME-XML parsing in the Java ImageFile object. # extractor_class = env.find_class( "org/cellprofiler/imageset/ImagePlaneMetadataExtractor") extract_metadata_id = env.get_method_id( extractor_class, "extractMetadata", "(Ljava/lang/String;IILjava/lang/String;" "[Lorg/cellprofiler/imageset/filter/ImagePlaneDetails;" "[Lorg/cellprofiler/imageset/ImageFile;" ")Ljava/util/Iterator;") extract_metadata_if_id = env.get_method_id( extractor_class, "extractMetadata", "(Lorg/cellprofiler/imageset/ImageFile;II" "[Lorg/cellprofiler/imageset/filter/ImagePlaneDetails;" ")Ljava/util/Iterator;") ipd_class = env.find_class( "org/cellprofiler/imageset/filter/ImagePlaneDetails") if_class = env.find_class("org/cellprofiler/imageset/ImageFile") clear_xml_document_id = env.get_method_id(if_class, "clearXMLDocument", "()V") pIPD = env.make_object_array(1, ipd_class) pIF = env.make_object_array(1, if_class) last_url = None last_if = None if_has_metadata = False for ipd in ipds: series, index = [ x if x is not None else 0 for x in ipd.series, ipd.index ] if ipd.url != last_url: if if_has_metadata: env.call_method(last_if, clear_xml_document_id) x = env.exception_occurred() if x is not None: raise J.JavaException(x) if_has_metadata = False xmlmetadata = file_list.get_metadata(ipd.url) if xmlmetadata is not None: xmlmetadata = env.new_string_utf(xmlmetadata) if_has_metadata = True metadata = env.call_method(extractor, extract_metadata_id, env.new_string_utf(ipd.url), int(series), int(index), xmlmetadata, pIPD, pIF) x = env.exception_occurred() if x is not None: raise J.JavaException(x) last_url = ipd.url last_if = env.get_object_array_elements(pIF)[0] else: metadata = env.call_method(extractor, extract_metadata_if_id, last_if, int(series), int(index), pIPD) x = env.exception_occurred() if x is not None: raise J.JavaException(x) ipd.metadata.update(J.iterate_java(metadata, wrap_entry_set)) ipd.jipd = env.get_object_array_elements(pIPD)[0] if if_has_metadata: env.call_method(last_if, clear_xml_document_id) x = env.exception_occurred() if x is not None: raise J.JavaException(x) return True