Exemplo n.º 1
0
 def do_imagej(self, workspace, when=None):
     if when == D_FIRST_IMAGE_SET:
         choice = self.prepare_group_choice.value
         command = self.prepare_group_command
         macro = self.prepare_group_macro.value
         d = self.pre_command_settings_dictionary
     elif when == D_LAST_IMAGE_SET:
         choice = self.post_group_choice.value
         command = self.post_group_command
         macro = self.post_group_macro.value
         d = self.pre_command_settings_dictionary
     else:
         choice = self.command_or_macro.value
         command = self.command
         macro  = self.macro.value
         d = self.command_settings_dictionary
         
     if choice == CM_COMMAND:
         self.execute_advanced_command(workspace, command, d)
     elif choice == CM_MACRO:
         macro = workspace.measurements.apply_metadata(macro)
         script_service = ij2.get_script_service(get_context())
         factory = script_service.getByName(self.macro_language.value)
         engine = factory.getScriptEngine()
         engine.put("ImageJ", get_context())
         result = engine.evalS(macro)
         
     if (choice != CM_NOTHING and 
         (not cpprefs.get_headless()) and 
         self.pause_before_proceeding):
         import wx
         wx.MessageBox("Please edit the image in ImageJ and hit OK to proceed",
                       "Waiting for ImageJ")
Exemplo n.º 2
0
 def test_10_01_get_script_service(self):
     svc = ij2.get_script_service(self.context)
     svc.getPluginService()
     svc.getLogService()
     svc.getIndex()
     for factory in svc.getLanguages():
         factory.getLanguageName()
Exemplo n.º 3
0
 def test_10_08_eval_with_bindings(self):
     svc = ij2.get_script_service(self.context)
     factory = svc.getByName("ECMAScript")
     engine = factory.getScriptEngine()
     engine.put("a", 2)
     engine.evalS("var b = a+a;")
     self.assertEqual(J.call(engine.get("b"), "intValue", "()I"), 4)
Exemplo n.º 4
0
 def test_10_07_evalS(self):
     svc = ij2.get_script_service(self.context)
     factory = svc.getByName("ECMAScript")
     engine = factory.getScriptEngine()
     result = engine.evalS("2+2")
     if isinstance(result, J.JB_Object):
         result = J.call(result, "intValue", "()I")
     self.assertEqual(result, 4)
Exemplo n.º 5
0
 def test_10_04_engine_factory_wrapper(self):
     svc = ij2.get_script_service(self.context)
     factory = svc.getByName("ECMAScript")
     factory.getEngineName()
     factory.getEngineVersion()
     factory.getExtensions()
     factory.getMimeTypes()
     factory.getNames()
     factory.getLanguageName()
     factory.getLanguageVersion()
     factory.getParameter(J.get_static_field('javax/script/ScriptEngine',
                                             'NAME', 'Ljava/lang/String;'))
     factory.getMethodCallSyntax("myobject", "mymethod", ["param1", "param2"])
     factory.getOutputStatement("Hello, world")
     factory.getProgram(["I.do.this()", "I.do.that()"])
     factory.getScriptEngine()
Exemplo n.º 6
0
 def test_10_05_script_engine(self):
     svc = ij2.get_script_service(self.context)
     factory = svc.getByName("ECMAScript")
     engine = factory.getScriptEngine()
     engine.ARGV
     engine.ENGINE
     engine.FILENAME
     engine.ENGINE_VERSION
     engine.NAME
     engine.LANGUAGE
     engine.LANGUAGE_VERSION
     engine.ENGINE_SCOPE
     engine.GLOBAL_SCOPE
     engine.createBindings()
     engine.setBindings(engine.getBindings(engine.ENGINE_SCOPE), 
                        engine.ENGINE_SCOPE)
     engine.setContext(engine.getContext())
     engine.getFactory()
Exemplo n.º 7
0
 def test_02_03_set_and_get_image(self):
     image = np.zeros((15, 17))
     image[3:6, 10:13] = 1
     workspace, module = self.make_workspace(image)
     self.assertTrue(isinstance(module, R.RunImageJ))
     module.command_or_macro.value = R.CM_SCRIPT
     module.macro_language.value = "ECMAScript"
     script_svc = ij2.get_script_service(R.get_context())
     factory = script_svc.getByName(module.macro_language.value)
     output_statement = factory.getOutputStatement("Hello, world!")
     module.macro.value = output_statement
     module.wants_to_set_current_image.value = True
     module.current_input_image_name.value = INPUT_IMAGE_NAME
     module.wants_to_get_current_image.value = True
     module.current_output_image_name.value = OUTPUT_IMAGE_NAME
     module.run(workspace)
     output_image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
     output_pixel_data = output_image.pixel_data
     np.testing.assert_array_equal(output_pixel_data, image)
Exemplo n.º 8
0
    def create_settings(self):
        '''Create the settings for the module'''
        logger.debug("Creating RunImageJ module settings")
        J.activate_awt()
        logger.debug("Activated AWT")
        
        self.command_or_macro = cps.Choice(
            "Run an ImageJ command or macro?", 
            [CM_COMMAND, CM_MACRO],doc = """
            This setting determines whether <b>RunImageJ</b> runs either a:
            <ul>
            <li><i>%(CM_COMMAND)s:</i> Select from a list of available ImageJ commands
            (those items contained in the ImageJ menus); or</li>
            <li><i>%(CM_MACRO)s:</i> A series of ImageJ commands/plugins that you write yourself.</li>
            </ul>"""%globals())
        #
        # Load the commands in visible_settings so that we don't call
        # ImageJ unless someone tries the module
        #
        self.command = self.make_command_choice(
            "Command",doc = """
            <i>(Used only if running a %(CM_COMMAND)s)</i><br>
            The command to execute when the module runs."""%globals())
                                                
        self.command_settings_dictionary = {}
        self.command_settings = []
        self.command_settings_count = cps.HiddenCount(
            self.command_settings, "Command settings count")
        self.pre_command_settings_dictionary = {}
        self.pre_command_settings = []
        self.pre_command_settings_count = cps.HiddenCount(
            self.pre_command_settings, "Prepare group command settings count")
        self.post_command_settings_dictionary = {}
        self.post_command_settings = []
        self.post_command_settings_count = cps.HiddenCount(
            self.post_command_settings, "Post-group command settings count")

        self.macro = cps.Text(
            "Macro", 
            """import imagej.command.CommandService;
cmdSvcClass = CommandService.class;
cmdSvc = ImageJ.getService(cmdSvcClass);
cmdSvc.run("imagej.core.commands.assign.InvertDataValues", new Object [] {"allPlanes", true}).get();""",
            multiline = True,doc="""
            <i>(Used only if running a %(CM_MACRO)s)</i><br>
            This is the ImageJ macro to be executed. The syntax for ImageJ
            macros depends on the scripting language engine chosen.
            We suggest that you use the Beanshell scripting language
            <a href="http://www.beanshell.org/manual/contents.html">
            (Beanshell documentation)</a>."""%globals())
        
        all_engines = ij2.get_script_service(get_context()).getLanguages()
        self.language_dictionary = dict(
            [(engine.getLanguageName(), engine) for engine in all_engines])
            
        self.macro_language = cps.Choice(
            "Macro language",
            choices = self.language_dictionary.keys(),doc = """
            This setting chooses the scripting language used to execute
            any macros in this module""")
        
        self.wants_to_set_current_image = cps.Binary(
            "Input the currently active image in ImageJ?", True,doc="""
            Check this setting if you want to set the currently 
            active ImageJ image using an image from a 
            prior CellProfiler module.
            <p>Leave it unchecked to use the currently 
            active image in ImageJ. You may want to do this if you
            have an output image from a prior <b>RunImageJ</b>
            that you want to perform further operations upon
            before retrieving the final result back to CellProfiler.</p>""")

        self.current_input_image_name = cps.ImageNameSubscriber(
            "Select the input image",doc="""
            <i>(Used only if setting the currently active image)</i><br>
            This is the CellProfiler image that will become 
            ImageJ's currently active image.
            The ImageJ commands and macros in this module will perform 
            their operations on this image. You may choose any image produced
            by a prior CellProfiler module.""")

        self.wants_to_get_current_image = cps.Binary(
            "Retrieve the currently active image from ImageJ?", True,doc="""
            Check this setting if you want to retrieve ImageJ's
            currently active image after running the command or macro. 
            <p>Leave the setting unchecked if the pipeline does not need to access
            the current ImageJ image. For example, you might want to run
            further ImageJ operations with additional <b>RunImageJ</b>
            upon the current image prior to retrieving the final image 
            back to CellProfiler.</p>""")

        self.current_output_image_name = cps.ImageNameProvider(
            "Name the current output image", "ImageJImage",doc="""
            <i>(Used only if retrieving the currently active image from ImageJ)</i><br>
            This is the CellProfiler name for ImageJ's current image after
            processing by the command or macro. The image will be a
            snapshot of the current image after the command has run, and
            will be available for processing by subsequent CellProfiler modules.""")
        
        self.pause_before_proceeding = cps.Binary(
            "Wait for ImageJ before continuing?", False,doc = """
            Some ImageJ commands and macros are interactive; you
            may want to adjust the image in ImageJ before continuing. Check
            this box to stop CellProfiler while you adjust the image in
            ImageJ. Leave the box unchecked to immediately use the image.
            <p>This command will not wait if CellProfiler is executed in
            batch mode. See <i>%(BATCH_PROCESSING_HELP_REF)s</i> for more
            details on batch processing.</p>"""%globals())
        
        self.prepare_group_choice = cps.Choice(
            "Function to run before each group of images?", 
            [CM_NOTHING, CM_COMMAND, CM_MACRO],doc="""
            You can run an ImageJ macro or a command <i>before</i> each group of
            images. This can be useful in order to set up ImageJ before
            processing a stack of images. Choose <i>%(CM_NOTHING)s</i> if
            you do not want to run a command or macro, <i>%(CM_COMMAND)s</i>
            to choose a command to run or <i>%(CM_MACRO)s</i> to run a macro.
            """ % globals())
        
        logger.debug("Finding ImageJ commands")
        
        self.prepare_group_command = self.make_command_choice(
            "Command", doc = """
            <i>(Used only if running a command before an image group)</i><br>
            Select the command to execute before processing a group of images.""")

        self.prepare_group_macro = cps.Text(
            "Macro", 'run("Invert");',
            multiline = True,doc="""
            <i>(Used only if running a macro before an image group)</i><br>
            This is the ImageJ macro to be executed before processing
            a group of images. For help on writing macros, see 
            <a href="http://rsb.info.nih.gov/ij/developer/macro/macros.html">here</a>.""")
        
        self.post_group_choice = cps.Choice(
            "Function to run after each group of images?", 
            [CM_NOTHING, CM_COMMAND, CM_MACRO],doc="""
            You can run an ImageJ macro or a command <i>after</i> each group of
            images. This can be used to do some sort of operation on a whole
            stack of images that have been accumulated by the group operation.
            Choose <i>%(CM_NOTHING)s</i> if you do not want to run a command or 
            macro, <i>%(CM_COMMAND)s</i> to choose a command to run or 
            <i>%(CM_MACRO)s</i> to run a macro.
            """ % globals())
        
        self.post_group_command = self.make_command_choice(
            "Command", doc = """
            <i>(Used only if running a command after an image group)</i><br>
            The command to execute after processing a group of images.""")
        
        self.post_group_macro = cps.Text(
            "Macro", 'run("Invert");',
            multiline = True,doc="""
            <i>(Used only if running a macro after an image group)</i><br>
            This is the ImageJ macro to be executed after processing
            a group of images. For help on writing macros, see 
            <a href="http://rsb.info.nih.gov/ij/developer/macro/macros.html">here</a>.""")
        
        self.wants_post_group_image = cps.Binary(
            "Retrieve the image output by the group operation?", False,doc="""
            You can retrieve the image that is currently active in ImageJ
            at the end of macro processing and use it later in CellProfiler.
            The image will only be available during the last cycle of the
            image group. Check this setting to use the active image in CellProfiler
            or leave it unchecked if you do not want to use the active image.
            """)
        
        self.post_group_output_image = cps.ImageNameProvider(
            "Name the group output image", "ImageJGroupImage",doc="""
            <i>(Used only if retrieving an image after an image group operation)</i><br>
            This setting names the output image produced by the
            ImageJ command or macro that CellProfiler runs after processing
            all images in the group. The image is only available at the
            last cycle in the group""",
            provided_attributes={cps.AGGREGATE_IMAGE_ATTRIBUTE: True,
                                 cps.AVAILABLE_ON_LAST_ATTRIBUTE: True } )
           
        self.show_imagej_button = cps.DoSomething(
            "Show ImageJ", "Show", self.on_show_imagej,doc="""
            Press this button to show the ImageJ user interface.
            You can use the user interface to run ImageJ commands or
            set up ImageJ before a CellProfiler run.""")
        
        logger.debug("Finished creating settings")
Exemplo n.º 9
0
 def test_10_06_get_put(self):
     svc = ij2.get_script_service(self.context)
     factory = svc.getByName("ECMAScript")
     engine = factory.getScriptEngine()
     engine.put("Foo", "Bar")
     self.assertEqual(engine.get("Foo"), "Bar")
Exemplo n.º 10
0
 def test_10_03_get_by_file_extension(self):
     svc = ij2.get_script_service(self.context)
     self.assertIsNotNone(svc.getByFileExtension("foo.js"))
Exemplo n.º 11
0
 def test_10_02_get_by_name(self):
     svc = ij2.get_script_service(self.context)
     factory = svc.getByName("ECMAScript")
     self.assertIsNotNone(factory)
Exemplo n.º 12
0
 def test_10_07_evalS(self):
     svc = ij2.get_script_service(self.context)
     factory = svc.getByName("ECMAScript")
     engine = factory.getScriptEngine()
     result = engine.evalS("2+2")
     self.assertEqual(J.call(result, "intValue", "()I"), 4)
Exemplo n.º 13
0
 def test_10_06_get_put(self):
     svc = ij2.get_script_service(self.context)
     factory = svc.getByName("ECMAScript")
     engine = factory.getScriptEngine()
     engine.put("Foo", "Bar")
     self.assertEqual(engine.get("Foo"), "Bar")
Exemplo n.º 14
0
 def test_10_03_get_by_file_extension(self):
     svc = ij2.get_script_service(self.context)
     self.assertIsNotNone(svc.getByFileExtension("foo.js"))
Exemplo n.º 15
0
 def test_10_02_get_by_name(self):
     svc = ij2.get_script_service(self.context)
     factory = svc.getByName("ECMAScript")
     self.assertIsNotNone(factory)