Exemplo n.º 1
0
 def test_02_03_module_info(self):
     svc = ij2.get_module_service(self.context)
     #
     # Run the methods without checks on the values to check signatures
     module_infos = svc.getModules()
     module_info = module_infos[0]
     module_info.getMenuRoot()
     module_info.getMenuPath()
     module_info.createModule()
     module_info.getTitle()
     module_info.getName()
     module_info.getClassName()
     for module_info in module_infos:
         inputs = module_info.getInputs()
         if len(inputs) > 0:
             module_item = inputs[0]
             self.assertIsNotNone(module_info.getInput(module_item.getName()))
             break
     else:
         raise AssertionError("No input items found")
     for module_info in module_infos:
         outputs = module_info.getOutputs()
         if len(outputs) > 0:
             module_item = outputs[0]
             self.assertIsNotNone(module_info.getOutput(module_item.getName()))
             break
     else:
         raise AssertionError("No output items found")
Exemplo n.º 2
0
 def execute_command(self, command, options=None):
     '''execute the named command within ImageJ'''
     module_service = IJ2.get_module_service(self.context)
     matches = [x for x in module_service.getModules()
                if x.getName() == command]
     if len(matches) != 1:
         raise ValueError("Could not find %s module" % command)
     module_service.run(matches[0].createModule())
Exemplo n.º 3
0
 def execute_command(self, command, options=None):
     '''execute the named command within ImageJ'''
     module_service = IJ2.get_module_service(self.context)
     matches = [x for x in module_service.getModules()
                if x.getName() == command]
     if len(matches) != 1:
         raise ValueError("Could not find %s module" % command)
     module_service.run(matches[0].createModule())
Exemplo n.º 4
0
 def test_02_02_02_get_module_index(self):
     svc = ij2.get_module_service(self.context)
     module_index = svc.getIndex()
     self.assertTrue(any([
         x.getClassName() == 'imagej.core.commands.assign.AddSpecifiedNoiseToDataValues'
         for x in module_index]))
     self.assertGreaterEqual(
         len(module_index.getS('imagej.command.CommandInfo')), 1)
Exemplo n.º 5
0
 def test_03_02_command_service_run(self):
     svc = ij2.get_command_service(self.context)
     module_infos = ij2.get_module_service(self.context).getModules()
     for module_info in module_infos:
         if module_info.getClassName() == \
            'imagej.core.commands.display.ShowLUT':
             d = J.get_map_wrapper(J.make_instance('java/util/HashMap', '()V'))
             future = svc.run(module_info.o, d.o)
             module = future.get()
             module = ij2.wrap_module(module)
             module.getOutput('output')
             break
     else:
         raise AssertionError("Could not find target module")
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
 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
Exemplo n.º 8
0
 def test_02_05_module(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':
             module = module_info.createModule()
             module.getInfo()
             module.getInput('stdDev')
             module.getOutput('display')
             module.setInput('stdDev', 2.5)
             module.setOutput('display', None)
             module.isResolved('display')
             module.setResolved('display', False)
             break
     else:
         raise AssertionError("Could not find target module")
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
 def test_02_04_module_item(self):
     svc = ij2.get_module_service(self.context)
     module_infos = svc.getModules()
     for module_info in module_infos:
         inputs = module_info.getInputs()
         if len(inputs) > 0:
             module_item = inputs[0]
             module_item.getType()
             module_item.getWidgetStyle()
             module_item.getMinimumValue()
             module_item.getMaximumValue()
             module_item.getStepSize()
             module_item.getName()
             module_item.getLabel()
             module_item.getDescription()
             module_item.loadValue()
             self.assertTrue(module_item.isInput())
             module_item.isOutput()
             break
Exemplo n.º 11
0
 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
Exemplo n.º 12
0
 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)
Exemplo n.º 13
0
 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")
Exemplo n.º 14
0
    def execute_advanced_command(self, workspace, command, d):
        '''Execute an advanced command

        command - name of the command
        d - dictionary to be used to find settings
        '''
        wants_display = workspace.frame is not None
        if wants_display:
            workspace.display_data.input_images = input_images = []
            workspace.display_data.output_images = output_images = []
        key = command.get_unicode_value()
        if bioformats.USE_IJ2:
            node = command.get_selected_leaf()
            module_info = node[2]
            module = IJ2.wrap_module(module_info.createModule())
            context = self.get_context()
            display_service = IJ2.get_display_service(context)
                
            display_dictionary = {}
            for setting, module_item in d[key]:
                field_type = module_item.getType()
                if isinstance(setting, cps.ImageNameProvider):
                    continue
                if field_type == IJ2.FT_BOOL:
                    value = J.make_instance("java/lang/Boolean",
                                            "(Z)V", setting.value)
                elif field_type == IJ2.FT_INTEGER:
                    value = J.make_instance("java/lang/Integer",
                                            "(I)V", setting.value)
                elif field_type == IJ2.FT_FLOAT:
                    value = J.make_instance("java/lang/Double",
                                            "(D)V", setting.value)
                elif field_type == IJ2.FT_STRING:
                    value = setting.value
                elif field_type == IJ2.FT_COLOR:
                    value = IJ2.make_color_rgb_from_html(setting.value)
                elif field_type == IJ2.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(image_name)
                    dataset = IJ2.create_dataset(image.pixel_data,
                                                 setting.value)
                    display = display_service.createDisplay(dataset)
                    if image.has_mask:
                        overlay = IJ2.create_overlay(image.mask)
                        display.displayOverlay(overlay)
                    value = display
                    display_dictionary[module_item.getName()] = display
                    if wants_display:
                        input_images.append((image_name, image.pixel_data))
                module.setInput(module_item.getName(), value)
            module_service = IJ2.get_module_service(context)
            module_service.run(module)
            for setting, module_item in d[key]:
                if isinstance(setting, cps.ImageNameProvider):
                    name = module_item.getName()
                    output_name = setting.value
                    if display_dictionary.has_key(name):
                        display = display_dictionary[name]
                    else:
                        display = IJ2.wrap_display(module.getOutput(name))
                    ds = display_service.getActiveDataset(display)
                    pixel_data = ds.get_pixel_data()
                    image = cpi.Image(pixel_data)
                    workspace.image_set.add(output_name, image)
                    if wants_display:
                        output_images.append((output_name, pixel_data))
            for display in display_dictionary.values():
                panel = IJ2.wrap_display_panel(display.getDisplayPanel())
                panel.close()
        else:
            from imagej.imageplus import make_imageplus_from_processor
            from imagej.imageplus import get_imageplus_wrapper
            from imagej.imageprocessor import make_image_processor
            from imagej.imageprocessor import get_image
            
            command = command.value
            settings = d[command]
            classname = self.get_cached_commands()[command]
            plugin = M.get_plugin(classname)
            fp_in = P.get_input_fields_and_parameters(plugin)
            result = []
            image_set = workspace.image_set
            assert isinstance(image_set, cpi.ImageSet)
            for (field, parameter), setting in zip(fp_in, settings[:len(fp_in)]):
                field_type = P.get_field_type(field)
                label = parameter.label() or ""
                if field_type == P.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(image_name,
                                                          must_be_grayscale = True)
                    pixel_data = (image.pixel_data * 255.0).astype(np.float32)
                    if wants_display:
                        input_images.append((image_name, pixel_data / 255.0))
                    processor = make_image_processor(pixel_data)
                    image_plus = make_imageplus_from_processor(image_name, processor)
                    field.set(plugin, image_plus)
                    del image_plus
                    del processor
                elif field_type == P.FT_INTEGER:
                    field.setInt(plugin, setting.value)
                elif field_type == P.FT_FLOAT:
                    field.setFloat(plugin, setting.value)
                elif field_type == P.FT_BOOL:
                    field.setBoolean(plugin, setting.value)
                else:
                    field.set(plugin, setting.value)
            #
            # There are two ways to run this:
            # * Batch - just call plugin.run()
            # * Interactive - use PlugInFunctions.runInteractively
            #
            if self.pause_before_proceeding:
                J.execute_runnable_in_main_thread(J.run_script(
                    """new java.lang.Runnable() { run:function() {
                        importClass(Packages.imagej.plugin.PlugInFunctions);
                        PlugInFunctions.runInteractively(plugin);
                    }};""", dict(plugin=plugin)), True)
            else:
                J.execute_runnable_in_main_thread(plugin, True)
            setting_idx = len(fp_in)
            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:
                    image_name = settings[setting_idx].value
                    setting_idx += 1
                    image_plus = get_imageplus_wrapper(field.get(plugin))
                    processor = image_plus.getProcessor()
                    pixel_data = get_image(processor).astype(np.float32) / 255.0
                    if wants_display:
                        output_images.append((image_name, pixel_data))
                    image = cpi.Image(pixel_data)
                    image_set.add(image_name, image)
Exemplo n.º 15
0
 def test_02_02_01_get_modules(self):
     svc = ij2.get_module_service(self.context)
     module_infos = svc.getModules()
     self.assertTrue(J.is_instance_of(module_infos[0].o,
                                      "imagej/module/ModuleInfo"))
Exemplo n.º 16
0
 def test_02_01_get_module_service(self):
     self.assertIsNotNone(ij2.get_module_service(self.context))
Exemplo n.º 17
0
    def execute_advanced_command(self, workspace, command, d):
        '''Execute an advanced command

        command - name of the command
        d - dictionary to be used to find settings
        '''
        wants_display = workspace.frame is not None
        if wants_display:
            workspace.display_data.input_images = input_images = []
            workspace.display_data.output_images = output_images = []
        key = command.get_unicode_value()
        if bioformats.USE_IJ2:
            node = command.get_selected_leaf()
            module_info = node[2]
            module = IJ2.wrap_module(module_info.createModule())
            context = self.get_context()
            display_service = IJ2.get_display_service(context)

            display_dictionary = {}
            for setting, module_item in d[key]:
                field_type = module_item.getType()
                if isinstance(setting, cps.ImageNameProvider):
                    continue
                if field_type == IJ2.FT_BOOL:
                    value = J.make_instance("java/lang/Boolean", "(Z)V",
                                            setting.value)
                elif field_type == IJ2.FT_INTEGER:
                    value = J.make_instance("java/lang/Integer", "(I)V",
                                            setting.value)
                elif field_type == IJ2.FT_FLOAT:
                    value = J.make_instance("java/lang/Double", "(D)V",
                                            setting.value)
                elif field_type == IJ2.FT_STRING:
                    value = setting.value
                elif field_type == IJ2.FT_COLOR:
                    value = IJ2.make_color_rgb_from_html(setting.value)
                elif field_type == IJ2.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(image_name)
                    dataset = IJ2.create_dataset(image.pixel_data,
                                                 setting.value)
                    display = display_service.createDisplay(dataset)
                    if image.has_mask:
                        overlay = IJ2.create_overlay(image.mask)
                        display.displayOverlay(overlay)
                    value = display
                    display_dictionary[module_item.getName()] = display
                    if wants_display:
                        input_images.append((image_name, image.pixel_data))
                module.setInput(module_item.getName(), value)
            module_service = IJ2.get_module_service(context)
            module_service.run(module)
            for setting, module_item in d[key]:
                if isinstance(setting, cps.ImageNameProvider):
                    name = module_item.getName()
                    output_name = setting.value
                    if display_dictionary.has_key(name):
                        display = display_dictionary[name]
                    else:
                        display = IJ2.wrap_display(module.getOutput(name))
                    ds = display_service.getActiveDataset(display)
                    pixel_data = ds.get_pixel_data()
                    image = cpi.Image(pixel_data)
                    workspace.image_set.add(output_name, image)
                    if wants_display:
                        output_images.append((output_name, pixel_data))
            for display in display_dictionary.values():
                panel = IJ2.wrap_display_panel(display.getDisplayPanel())
                panel.close()
        else:
            from imagej.imageplus import make_imageplus_from_processor
            from imagej.imageplus import get_imageplus_wrapper
            from imagej.imageprocessor import make_image_processor
            from imagej.imageprocessor import get_image

            command = command.value
            settings = d[command]
            classname = self.get_cached_commands()[command]
            plugin = M.get_plugin(classname)
            fp_in = P.get_input_fields_and_parameters(plugin)
            result = []
            image_set = workspace.image_set
            assert isinstance(image_set, cpi.ImageSet)
            for (field, parameter), setting in zip(fp_in,
                                                   settings[:len(fp_in)]):
                field_type = P.get_field_type(field)
                label = parameter.label() or ""
                if field_type == P.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(
                        image_name, must_be_grayscale=True)
                    pixel_data = (image.pixel_data * 255.0).astype(np.float32)
                    if wants_display:
                        input_images.append((image_name, pixel_data / 255.0))
                    processor = make_image_processor(pixel_data)
                    image_plus = make_imageplus_from_processor(
                        image_name, processor)
                    field.set(plugin, image_plus)
                    del image_plus
                    del processor
                elif field_type == P.FT_INTEGER:
                    field.setInt(plugin, setting.value)
                elif field_type == P.FT_FLOAT:
                    field.setFloat(plugin, setting.value)
                elif field_type == P.FT_BOOL:
                    field.setBoolean(plugin, setting.value)
                else:
                    field.set(plugin, setting.value)
            #
            # There are two ways to run this:
            # * Batch - just call plugin.run()
            # * Interactive - use PlugInFunctions.runInteractively
            #
            if self.pause_before_proceeding:
                J.static_call('imagej/plugin/PlugInFunctions',
                              'runInteractively', '(Ljava/lang/Runnable)V',
                              plugin)
            else:
                J.call(plugin, 'run', '()V')
            setting_idx = len(fp_in)
            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:
                    image_name = settings[setting_idx].value
                    setting_idx += 1
                    image_plus = get_imageplus_wrapper(field.get(plugin))
                    processor = image_plus.getProcessor()
                    pixel_data = get_image(processor).astype(
                        np.float32) / 255.0
                    if wants_display:
                        output_images.append((image_name, pixel_data))
                    image = cpi.Image(pixel_data)
                    image_set.add(image_name, image)