class ScriptService(object): def __init__(self, o=o): self.o = o getPluginService = J.make_method( "getPluginService", "()Lorg/scijava/plugin/PluginService;") getLogService = J.make_method("getLogService", "()Lorg/scijava/log/LogService;") getIndex = J.make_method("getIndex", "()Limagej/script/ScriptLanguageIndex;") getLanguages = J.make_method( "getLanguages", "()Ljava/util/List;", doc="Return the script engine factories supported by this service", fn_post_process=lambda jlangs: [ wrap_script_engine_factory(o) for o in J.iterate_collection(jlangs) ]) getByFileExtension = J.make_method( "getByFileExtension", "(Ljava/lang/String;)Ljavax/script/ScriptEngineFactory;", fn_post_process=wrap_script_engine_factory) getByName = J.make_method( "getByName", "(Ljava/lang/String;)Ljavax/script/ScriptEngineFactory;", fn_post_process=wrap_script_engine_factory)
def get_pixel_data(self, axes=None): imgplus = self.getImgPlus() pixel_data = get_pixel_data(imgplus) script = """ var result = java.util.ArrayList(); for (i=0;i<imgplus.numDimensions();i++) result.add(imgplus.axis(i)); result""" inv_axes = J.run_script(script, dict(imgplus=imgplus)) inv_axes = list(J.iterate_collection(inv_axes)) transpose = calculate_transpose(inv_axes, axes) return pixel_data.transpose(transpose)
def get_pixel_data(self, axes = None): imgplus = self.getImgPlus() pixel_data = get_pixel_data(imgplus) script = """ var result = java.util.ArrayList(); for (i=0;i<imgplus.numDimensions();i++) result.add(imgplus.axis(i)); result""" inv_axes = J.run_script(script, dict(imgplus=imgplus)) inv_axes = list(J.iterate_collection(inv_axes)) transpose = calculate_transpose(inv_axes, axes) return pixel_data.transpose(transpose)
class ModuleInfo(object): def __init__(self): self.o = instance getInput = J.make_method( "getInput", "(Ljava/lang/String;)Limagej/module/ModuleItem;", doc = "Gets the input item with the given name.", fn_post_process=wrap_module_item) getOutput = J.make_method( "getOutput", "(Ljava/lang/String;)Limagej/module/ModuleItem;", doc = "Gets the output item with the given name.", fn_post_process=wrap_module_item) getInputs = J.make_method( "inputs", "()Ljava/lang/Iterable;", """Get the module info's input module items""", fn_post_process=lambda iterator: map(wrap_module_item, J.iterate_collection(iterator))) getOutputs = J.make_method( "outputs", "()Ljava/lang/Iterable;", """Get the module info's output module items""", fn_post_process=lambda iterator: map(wrap_module_item, J.iterate_collection(iterator))) getTitle = J.make_method( "getTitle", "()Ljava/lang/String;") createModule = J.make_method( "createModule", "()Limagej/module/Module;", fn_post_process=wrap_module) getMenuPath = J.make_method( "getMenuPath", "()Limagej/MenuPath;") getMenuRoot = J.make_method( "getMenuRoot", "()Ljava/lang/String;") getName = J.make_method("getName", "()Ljava/lang/String;") getClassName = J.make_method("getClassName", "()Ljava/lang/String;")
def get_choice_tree(self): '''Get the ImageJ command choices for the TreeChoice control The menu items are augmented with a third tuple entry which is the ModuleInfo for the command. ''' global cached_choice_tree global cached_commands if cached_choice_tree is not None: return cached_choice_tree tree = [] context = get_context() module_service = ij2.get_module_service(context) for module_info in module_service.getModules(): if module_info.getMenuRoot() != "app": continue logger.info("Processing module %s" % module_info.getClassName()) menu_path = module_info.getMenuPath() if menu_path is None or J.call(menu_path, "size", "()I") == 0: continue current_tree = tree # # The menu path is a collection of MenuEntry # for item in J.iterate_collection(menu_path): menu_entry = ij2.wrap_menu_entry(item) name = menu_entry.getName() weight = menu_entry.getWeight() matches = [node for node in current_tree if node[0] == name] if len(matches) > 0: current_node = matches[0] else: current_node = [name, [], None, weight] current_tree.append(current_node) current_tree = current_node[1] # mark the leaf. current_node[2] = module_info def sort_tree(tree): '''Recursively sort a tree in-place''' for node in tree: if node[1] is not None: sort_tree(node[1]) tree.sort(lambda node1, node2: cmp(node1[-1], node2[-1])) sort_tree(tree) cached_choice_tree = tree return cached_choice_tree
def get_choice_tree(self): '''Get the ImageJ command choices for the TreeChoice control The menu items are augmented with a third tuple entry which is the ModuleInfo for the command. ''' global cached_choice_tree global cached_commands if cached_choice_tree is not None: return cached_choice_tree tree = [] context = get_context() module_service = ij2.get_module_service(context) for module_info in module_service.getModules(): if module_info.getMenuRoot() != "app": continue logger.info("Processing module %s" % module_info.getClassName()) menu_path = module_info.getMenuPath() if menu_path is None or J.call(menu_path, "size", "()I") == 0: continue current_tree = tree # # The menu path is a collection of MenuEntry # for item in J.iterate_collection(menu_path): menu_entry = ij2.wrap_menu_entry(item) name = menu_entry.getName() weight = menu_entry.getWeight() matches = [node for node in current_tree if node[0] == name] if len(matches) > 0: current_node = matches[0] else: current_node = [name, [], None, weight] current_tree.append(current_node) current_tree = current_node[1] # mark the leaf. current_node[2] = module_info def sort_tree(tree): '''Recursively sort a tree in-place''' for node in tree: if node[1] is not None: sort_tree(node[1]) tree.sort(lambda node1, node2: cmp(node1[-1], node2[-1])) sort_tree(tree) cached_choice_tree = tree return cached_choice_tree
def get_choice_tree(self): '''Get the ImageJ command choices for the TreeChoice control The menu items are augmented with a third tuple entry which is the ModuleInfo for the command. ''' global cached_choice_tree if cached_choice_tree is not None: return cached_choice_tree context = RunImageJ.get_context() ij2_module_service = IJ2.get_module_service(context) tree = [] for module_info in ij2_module_service.getModules(): menu_path = module_info.getMenuPath() items = [ IJ2.wrap_menu_entry(x) for x in J.iterate_collection(menu_path) ] if len(items) == 0: continue current_tree = tree for item in items: name = item.getName() weight = item.getWeight() matches = [node for node in current_tree if node[0] == name] if len(matches) > 0: current_node = matches[0] else: current_node = [name, [], module_info, weight] current_tree.append(current_node) current_tree = current_node[1] # mark the leaf. current_node[1] = None def sort_tree(tree): '''Recursively sort a tree in-place''' for node in tree: if node[1] is not None: sort_tree(node[1]) tree.sort(lambda node1, node2: cmp(node1[-1], node2[-1])) sort_tree(tree) cached_choice_tree = tree return cached_choice_tree
def get_choice_tree(self): '''Get the ImageJ command choices for the TreeChoice control The menu items are augmented with a third tuple entry which is the ModuleInfo for the command. ''' global cached_choice_tree if cached_choice_tree is not None: return cached_choice_tree context = RunImageJ.get_context() ij2_module_service = IJ2.get_module_service(context) tree = [] for module_info in ij2_module_service.getModules(): menu_path = module_info.getMenuPath() items = [IJ2.wrap_menu_entry(x) for x in J.iterate_collection(menu_path)] if len(items) == 0: continue current_tree = tree for item in items: name = item.getName() weight = item.getWeight() matches = [node for node in current_tree if node[0] == name] if len(matches) > 0: current_node = matches[0] else: current_node = [name, [], module_info, weight] current_tree.append(current_node) current_tree = current_node[1] # mark the leaf. current_node[1] = None def sort_tree(tree): '''Recursively sort a tree in-place''' for node in tree: if node[1] is not None: sort_tree(node[1]) tree.sort(lambda node1, node2: cmp(node1[-1], node2[-1])) sort_tree(tree) cached_choice_tree = tree return cached_choice_tree
def test_02_06_menu_entry(self): svc = ij2.get_module_service(self.context) module_infos = svc.getModules() for module_info in module_infos: if module_info.getClassName() == \ 'imagej.core.commands.assign.AddSpecifiedNoiseToDataValues': menu_path = module_info.getMenuPath() for item in J.iterate_collection(menu_path): menu_entry = ij2.wrap_menu_entry(item) menu_entry.getName() menu_entry.setName("Foo") menu_entry.getWeight() menu_entry.setWeight(5) menu_entry.getMnemonic() menu_entry.setMnemonic("X") menu_entry.getAccelerator() menu_entry.setAccelerator(None) menu_entry.getIconPath() menu_entry.setIconPath(None) break else: raise AssertionError("Could not find target module")
def __iter__(self): return J.iterate_collection(self.o)
class DisplayService(object): def __init__(self): self.o = o def createDisplay(self, name, dataset): '''Create a display that contains the given dataset''' # # Must be run on the gui thread # jcallable = J.run_script( """new java.util.concurrent.Callable() { call:function() { return displayService.createDisplay(name, dataset); } }; """, dict(displayService=self.o, name=name, dataset=dataset.o)) future = J.make_future_task(jcallable, fn_post_process=wrap_display) return J.execute_future_in_main_thread(future) def getActiveDisplay(self, klass=None): '''Get the first display, optionally of the given type from the list klass - if not None, return the first display of this type, otherwise return the first display ''' if klass is None: return wrap_display( J.call(self.o, "getActiveDisplay", "()Limagej/display/Display;")) else: return wrap_display( J.call(self.o, "getActiveDisplay", "(Ljava/lang/Class;)Limagej/display/Display;", klass)) def getActiveImageDisplay(self): '''Get the active imagej.data.display.ImageDisplay''' return wrap_display( J.call(self.o, "getActiveDisplay", "()Limagej/display/Display;", J.class_for_name("imagej.data.display.ImageDisplay"))) def setActiveDisplay(self, display): '''Make this the active display''' # Note: has to be run in GUI thread on mac r = J.run_script( """new java.lang.Runnable() { run:function() { displayService.setActiveDisplay(display); } } """, dict(displayService=self.o, display=display.o)) J.execute_runnable_in_main_thread(r, True) getDisplays = J.make_method("getDisplays", "()Ljava/util/List;", fn_post_process=lambda x: map( wrap_display, J.iterate_collection(x))) getDisplay = J.make_method( "getDisplay", "(Ljava/lang/String;)Limagej/display/Display;", fn_post_process=wrap_display) isUniqueName = J.make_method("isUniqueName", "(Ljava/lang/String;)Z")
def get_command_settings(self, command, d): '''Get the settings associated with the current command d - the dictionary that persists the setting. None = regular ''' key = command.get_unicode_value() if not d.has_key(key): if bioformats.USE_IJ2: try: module_info = command.get_selected_leaf()[2] except cps.ValidationError: return [] result = [] inputs = module_info.getInputs() module = module_info.createModule() implied_outputs = [] for module_item in inputs: field_type = module_item.getType() label = module_item.getLabel() value = module_item.getValue(module) minimum = module_item.getMinimumValue() maximum = module_item.getMaximumValue() description = module_item.getDescription() if field_type == IJ2.FT_BOOL: setting = cps.Binary( label, J.call(value, "booleanValue", "()Z"), doc = description) elif field_type == IJ2.FT_INTEGER: if minimum is not None: minimum = J.call(minimum, "intValue", "()I") if maximum is not None: maximum = J.call(maximum, "intValue", "()I") setting = cps.Integer( label, J.call(value, "intValue", "()I"), minval = minimum, maxval = maximum, doc = description) elif field_type == IJ2.FT_FLOAT: if minimum is not None: minimum = J.call(minimum, "floatValue", "()F") if maximum is not None: maximum = J.call(maximum, "floatValue", "()F") setting = cps.Float( label, J.call(value, "floatValue", "()F"), minval = minimum, maxval = maximum, doc = description) elif field_type == IJ2.FT_STRING: choices = module_item.getChoices() value = J.to_string(value) if choices is not None: choices = [J.to_string(choice) for choice in J.iterate_collection(choices)] setting = cps.Choice( label, choices, value, doc = description) else: setting = cps.Text( label, value, doc = description) elif field_type == IJ2.FT_COLOR: if value is not None: value = IJ2.color_rgb_to_html(value) else: value = "#ffffff" setting = cps.Color(label, value, doc = description) elif field_type == IJ2.FT_IMAGE: setting = cps.ImageNameSubscriber( label, "InputImage", doc = description) # # This is a Display for ij2 - the plugin typically # scribbles all over the display's image. So # we list it as an output too. # implied_outputs.append(( cps.ImageNameProvider( label, "OutputImage", doc = description), module_item)) elif field_type == IJ2.FT_OVERLAY: setting = cps.ObjectNameSubscriber( label, "ImageJObject", doc = description) else: continue result.append((setting, module_item)) for output in module_info.getOutputs(): field_type = output.getType() if field_type == IJ2.FT_IMAGE: result.append((cps.ImageNameProvider( label, "ImageJImage", doc = description), output)) result += implied_outputs d[key] = result return [setting for setting, module_info in result] else: cc = self.get_cached_commands() if (not cc.has_key(key)) or (cc[key] is None): return [] classname = cc[key] try: plugin = M.get_plugin(classname) except: d[key] = [] return [] fp_in = P.get_input_fields_and_parameters(plugin) result = [] for field, parameter in fp_in: field_type = P.get_field_type(field) label = parameter.label() or "" if field_type == P.FT_BOOL: result += [cps.Binary(label, field.getBoolean(plugin))] elif field_type == P.FT_INTEGER: result += [cps.Integer(label, field.getLong(plugin))] elif field_type == P.FT_FLOAT: result += [cps.Float(label, field.getDouble(plugin))] elif field_type == P.FT_STRING: result += [cps.Text(label, J.to_string(field.get(plugin)))] else: assert field_type == P.FT_IMAGE result += [cps.ImageNameSubscriber(label, "None")] fp_out = P.get_output_fields_and_parameters(plugin) for field, parameter in fp_out: field_type = P.get_field_type(field) if field_type == P.FT_IMAGE: result += [cps.ImageNameProvider(parameter.label() or "", "Output")] d[key] = result elif bioformats.USE_IJ2: result = [setting for setting, module_info in d[key]] else: result = d[key] return result
def __iter__(self): return J.iterate_collection(self.o)
def get_command_settings(self, command, d): '''Get the settings associated with the current command d - the dictionary that persists the setting. None = regular ''' key = command.get_unicode_value() if not d.has_key(key): try: module_info = command.get_selected_leaf()[2] except cps.ValidationError: return [] result = [] inputs = module_info.getInputs() for module_item in inputs: field_type = module_item.getType() label = module_item.getLabel() if label is None: label = module_item.getName() if module_item.isOutput(): # if both, qualify which is for input and which for output label = "%s (Input)" % label minimum = module_item.getMinimumValue() maximum = module_item.getMaximumValue() default = module_item.loadValue() description = module_item.getDescription() if field_type == ij2.FT_BOOL: value = (J.is_instance_of(default, 'java/lang/Boolean') and J.call(default, "booleanValue", "()Z")) setting = cps.Binary( label, value = value, doc = description) elif field_type == ij2.FT_INTEGER: if J.is_instance_of(default, 'java/lang/Number'): value = J.call(default, "intValue", "()I") elif minimum is not None: value = minimum elif maximum is not None: value = maximum else: value = 0 setting = cps.Integer( label, value = value, doc = description) elif field_type == ij2.FT_FLOAT: if J.is_instance_of(default, 'java/lang/Number'): value = J.call(default, "doubleValue", "()D") elif minimum is not None: value = minimum elif maximum is not None: value = maximum else: value = 0 setting = cps.Float( label, value=value, doc = description) elif field_type == ij2.FT_STRING: choices = module_item.Choices value = J.to_string(default) if choices is not None and len(choices) > 0: choices = [J.to_string(choice) for choice in J.iterate_collection(choices)] setting = cps.Choice( label, choices, value, doc = description) else: setting = cps.Text( label, value, doc = description) elif field_type == ij2.FT_COLOR: value = "#ffffff" setting = cps.Color(label, value, doc = description) elif field_type == ij2.FT_IMAGE: setting = cps.ImageNameSubscriber( label, "InputImage", doc = description) elif field_type == ij2.FT_FILE: setting = cps.FilenameText( label, None, doc = description) else: continue result.append((setting, module_item)) for output in module_info.getOutputs(): field_type = output.getType() label = output.getLabel() if label is None: label = output.getName() if output.isInput(): # if both, qualify which is for input and which for output label = "%s (Output)" % label if field_type == ij2.FT_IMAGE: result.append((cps.ImageNameProvider( label, "ImageJImage", doc = description), output)) d[key] = result else: result = d[key] return [setting for setting, module_info in result]
def get_command_settings(self, command, d): '''Get the settings associated with the current command d - the dictionary that persists the setting. None = regular ''' key = command.get_unicode_value() if not d.has_key(key): try: module_info = command.get_selected_leaf()[2] except cps.ValidationError: return [] result = [] inputs = module_info.getInputs() for module_item in inputs: field_type = module_item.getType() label = module_item.getLabel() if label is None: label = module_item.getName() if module_item.isOutput(): # if both, qualify which is for input and which for output label = "%s (Input)" % label minimum = module_item.getMinimumValue() maximum = module_item.getMaximumValue() default = module_item.loadValue() description = module_item.getDescription() if field_type == ij2.FT_BOOL: value = (J.is_instance_of(default, 'java/lang/Boolean') and J.call(default, "booleanValue", "()Z")) setting = cps.Binary(label, value=value, doc=description) elif field_type == ij2.FT_INTEGER: if J.is_instance_of(default, 'java/lang/Number'): value = J.call(default, "intValue", "()I") elif minimum is not None: value = minimum elif maximum is not None: value = maximum else: value = 0 setting = cps.Integer(label, value=value, doc=description) elif field_type == ij2.FT_FLOAT: if J.is_instance_of(default, 'java/lang/Number'): value = J.call(default, "doubleValue", "()D") elif minimum is not None: value = minimum elif maximum is not None: value = maximum else: value = 0 setting = cps.Float(label, value=value, doc=description) elif field_type == ij2.FT_STRING: choices = module_item.Choices value = J.to_string(default) if choices is not None and len(choices) > 0: choices = [ J.to_string(choice) for choice in J.iterate_collection(choices) ] setting = cps.Choice(label, choices, value, doc=description) else: setting = cps.Text(label, value, doc=description) elif field_type == ij2.FT_COLOR: value = "#ffffff" setting = cps.Color(label, value, doc=description) elif field_type == ij2.FT_IMAGE: setting = cps.ImageNameSubscriber(label, "InputImage", doc=description) elif field_type == ij2.FT_FILE: setting = cps.FilenameText(label, None, doc=description) else: continue result.append((setting, module_item)) for output in module_info.getOutputs(): field_type = output.getType() label = output.getLabel() if label is None: label = output.getName() if output.isInput(): # if both, qualify which is for input and which for output label = "%s (Output)" % label if field_type == ij2.FT_IMAGE: result.append( (cps.ImageNameProvider(label, "ImageJImage", doc=description), output)) d[key] = result else: result = d[key] return [setting for setting, module_info in result]
def get_command_settings(self, command, d): '''Get the settings associated with the current command d - the dictionary that persists the setting. None = regular ''' key = command.get_unicode_value() if not d.has_key(key): if bioformats.USE_IJ2: try: module_info = command.get_selected_leaf()[2] except cps.ValidationError: return [] result = [] inputs = module_info.getInputs() module = module_info.createModule() implied_outputs = [] for module_item in inputs: field_type = module_item.getType() label = module_item.getLabel() value = module_item.getValue(module) minimum = module_item.getMinimumValue() maximum = module_item.getMaximumValue() description = module_item.getDescription() if field_type == IJ2.FT_BOOL: setting = cps.Binary(label, J.call(value, "booleanValue", "()Z"), doc=description) elif field_type == IJ2.FT_INTEGER: if minimum is not None: minimum = J.call(minimum, "intValue", "()I") if maximum is not None: maximum = J.call(maximum, "intValue", "()I") setting = cps.Integer(label, J.call(value, "intValue", "()I"), minval=minimum, maxval=maximum, doc=description) elif field_type == IJ2.FT_FLOAT: if minimum is not None: minimum = J.call(minimum, "floatValue", "()F") if maximum is not None: maximum = J.call(maximum, "floatValue", "()F") setting = cps.Float(label, J.call(value, "floatValue", "()F"), minval=minimum, maxval=maximum, doc=description) elif field_type == IJ2.FT_STRING: choices = module_item.getChoices() value = J.to_string(value) if choices is not None: choices = [ J.to_string(choice) for choice in J.iterate_collection(choices) ] setting = cps.Choice(label, choices, value, doc=description) else: setting = cps.Text(label, value, doc=description) elif field_type == IJ2.FT_COLOR: if value is not None: value = IJ2.color_rgb_to_html(value) else: value = "#ffffff" setting = cps.Color(label, value, doc=description) elif field_type == IJ2.FT_IMAGE: setting = cps.ImageNameSubscriber(label, "InputImage", doc=description) # # This is a Display for ij2 - the plugin typically # scribbles all over the display's image. So # we list it as an output too. # implied_outputs.append( (cps.ImageNameProvider(label, "OutputImage", doc=description), module_item)) elif field_type == IJ2.FT_OVERLAY: setting = cps.ObjectNameSubscriber(label, "ImageJObject", doc=description) else: continue result.append((setting, module_item)) for output in module_info.getOutputs(): field_type = output.getType() if field_type == IJ2.FT_IMAGE: result.append( (cps.ImageNameProvider(label, "ImageJImage", doc=description), output)) result += implied_outputs d[key] = result return [setting for setting, module_info in result] else: cc = self.get_cached_commands() if (not cc.has_key(key)) or (cc[key] is None): return [] classname = cc[key] try: plugin = M.get_plugin(classname) except: d[key] = [] return [] fp_in = P.get_input_fields_and_parameters(plugin) result = [] for field, parameter in fp_in: field_type = P.get_field_type(field) label = parameter.label() or "" if field_type == P.FT_BOOL: result += [cps.Binary(label, field.getBoolean(plugin))] elif field_type == P.FT_INTEGER: result += [cps.Integer(label, field.getLong(plugin))] elif field_type == P.FT_FLOAT: result += [cps.Float(label, field.getDouble(plugin))] elif field_type == P.FT_STRING: result += [ cps.Text(label, J.to_string(field.get(plugin))) ] else: assert field_type == P.FT_IMAGE result += [cps.ImageNameSubscriber(label, "None")] fp_out = P.get_output_fields_and_parameters(plugin) for field, parameter in fp_out: field_type = P.get_field_type(field) if field_type == P.FT_IMAGE: result += [ cps.ImageNameProvider(parameter.label() or "", "Output") ] d[key] = result elif bioformats.USE_IJ2: result = [setting for setting, module_info in d[key]] else: result = d[key] return result