another python scripting question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

another python scripting question

Aryeh Weiss
Is it possible to set up a dialog such that any change to the dialog
will be detected and acted upon, in a python script.

I want to set up a slider, where whenever I move the slider, something
will change in an image (like preview).

This is documented in the addPreviewCheckbox method, so what I am asking
is if this can also be done within python.
Alternatively, is there a different way to do this?

--aryeh
--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: another python scripting question

Jan Eglinger
Dear Aryeh,

On 21.08.2013 4:57 PM, Aryeh Weiss wrote:
> Is it possible to set up a dialog such that any change to the dialog
> will be detected and acted upon, in a python script.

You can start from the following example python script to use a
DialogListener:

from ij.gui import DialogListener, GenericDialog

class MyListener (DialogListener):
        def dialogItemChanged(self, gd, event):
                IJ.log("Something was changed.")

gd = GenericDialog("DialogListener example")
gd.addStringField("Type anything", "change this", 20)
gd.addDialogListener(MyListener())
gd.showDialog()


>
> I want to set up a slider, where whenever I move the slider, something
> will change in an image (like preview).

Starting with the above code, it should be possible to transform Wayne's
nice Java example in the following post into Python/Jython:

http://article.gmane.org/gmane.comp.java.imagej/28353

>
> This is documented in the addPreviewCheckbox method, so what I am asking
> is if this can also be done within python.
> Alternatively, is there a different way to do this?
>

You might also consider using the ExtendedPluginFilter interface, which
provides extended preview functionality.

Hope that helps,
Jan

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: another python scripting question

Aryeh Weiss
Dear Jan,

Thank you for your quick response. I sent this just before I left work,
and when I got home, your response was already here.

On 8/21/13 7:00 PM, Jan Eglinger wrote:

> Dear Aryeh,
>
> On 21.08.2013 4:57 PM, Aryeh Weiss wrote:
>> Is it possible to set up a dialog such that any change to the dialog
>> will be detected and acted upon, in a python script.
>
> You can start from the following example python script to use a
> DialogListener:
>
> from ij.gui import DialogListener, GenericDialog
>
> class MyListener (DialogListener):
> def dialogItemChanged(self, gd, event):
> IJ.log("Something was changed.")
>
> gd = GenericDialog("DialogListener example")
> gd.addStringField("Type anything", "change this", 20)
> gd.addDialogListener(MyListener())
> gd.showDialog()
>
>

This is great -- works as advertised. I have couple of questions which I
could not figure out.

1. I would really like for the action to occur only after I finish
moving the slider. This is because I will be subtracting two images, and
this takes some time. Is there an event (either dialog or mouse) that I
can test for this?

2. Where can I find a complete list of dialog events? I searched around,
but when I did not find it, I tried just printing the event argument, to
see what events are being passed. I found:
TEXT_VALUE_CHANGED when moved the sliders
ITEM_STATE_CHANGED  when I checked or unchecked a checkbox

I tried radio buttons (with the idea that I will push a button to run
the calculation), but they did not produce any event and did not invoke
the listener.

I can use the  checkbox to do what I want, but I assume there is a
better way.

(Maybe a mouse listener in the dialog window?)


>>
>> I want to set up a slider, where whenever I move the slider, something
>> will change in an image (like preview).
>
> Starting with the above code, it should be possible to transform Wayne's
> nice Java example in the following post into Python/Jython:
>
> http://article.gmane.org/gmane.comp.java.imagej/28353
>
>>
>> This is documented in the addPreviewCheckbox method, so what I am asking
>> is if this can also be done within python.
>> Alternatively, is there a different way to do this?
>>
>
> You might also consider using the ExtendedPluginFilter interface, which
> provides extended preview functionality.
>


Is there an example of this in python?

Best regards
--aryeh
--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: another python scripting question

Jan Eglinger
Dear Aryeh,

On 21.08.2013 8:58 PM, Aryeh Weiss wrote:
> Thank you for your quick response. I sent this just before I left work,
> and when I got home, your response was already here.

:)

> On 8/21/13 7:00 PM, Jan Eglinger wrote:
>> from ij.gui import DialogListener, GenericDialog
>>
>> class MyListener (DialogListener):
>>     def dialogItemChanged(self, gd, event):
>>         IJ.log("Something was changed.")
>>
>> gd = GenericDialog("DialogListener example")
>> gd.addStringField("Type anything", "change this", 20)
>> gd.addDialogListener(MyListener())
>> gd.showDialog()
>>
> This is great -- works as advertised. I have couple of questions which I
> could not figure out.
>
> 1. I would really like for the action to occur only after I finish
> moving the slider. This is because I will be subtracting two images, and
> this takes some time. Is there an event (either dialog or mouse) that I
> can test for this?

Maybe you'd have to implement a mouse listener for that, reacting on
mouse button release. If you find out, let me know :)

>
> 2. Where can I find a complete list of dialog events? I searched around,
> but when I did not find it, I tried just printing the event argument, to
> see what events are being passed. I found:
> TEXT_VALUE_CHANGED when moved the sliders
> ITEM_STATE_CHANGED  when I checked or unchecked a checkbox

I guess the possible events are all subclasses of java.awt.AWTEvent:

http://docs.oracle.com/javase/6/docs/api/index.html?java/awt/AWTEvent.html

(and they're all in the java.awt.event package)
>
> I tried radio buttons (with the idea that I will push a button to run
> the calculation), but they did not produce any event and did not invoke
> the listener.
>
> I can use the  checkbox to do what I want, but I assume there is a
> better way.
>
> (Maybe a mouse listener in the dialog window?)

Yes. I think this is the way to go, but don't know of any example
implementation.

>
>>>
>>> I want to set up a slider, where whenever I move the slider, something
>>> will change in an image (like preview).
>>
>> Starting with the above code, it should be possible to transform Wayne's
>> nice Java example in the following post into Python/Jython:
>>
>> http://article.gmane.org/gmane.comp.java.imagej/28353
>>
>>> This is documented in the addPreviewCheckbox method, so what I am asking
>>> is if this can also be done within python.
>>> Alternatively, is there a different way to do this?
>>>
>> You might also consider using the ExtendedPluginFilter interface, which
>> provides extended preview functionality.
>>
> Is there an example of this in python?

I don't know of any Python example, but there are plenty of Java
examples in the ImageJ source:

http://fiji.sc/git/?p=ImageJA.git&a=search&h=HEAD&st=grep&s=ExtendedPlugInFilter

Oh, while searching I found this nice python example:

http://fiji.sc/Find_Dimension_of_Raw_Image.py

which at least also implements a listener.

If you finish developing your own python script implementing
ExtendedPlugInFilter, it would be great if you could add it to Fiji's
Jython scripting page as an example!

http://fiji.sc/Jython_Scripting

Cheers,
Jan

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: another python scripting question

Aryeh Weiss
Dear Jan,

Thanks again for your very helpful response.

Besides my replies to your comments (interleaved below), I have some
questions that came up as I progress.

1. The dialog event are of the form:
java.awt.event.TextEvent[TEXT_VALUE_CHANGED] on textfield2
or
java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=Force an
event,stateChange=DESELECTED] on checkbox2

checkbox2 was actually checkbox0 the first time I ran it. Each time I
run, that index increases (eg textfield2 was textfield0 the first time.

I would like these these to be reset each time the script runs, since
then I could test which box or field triggered the event.  However, they
only reset if I leave Fiji. I looked for a method like gd.reset() or
gd.close(), but I did not find this. I also tried "del gd" thinking I
could "erase" all memory of gd (my generic dialog).

Is there a way to "reset" the dialog?

2. How does one cause an image to be the "active" image. I tried
imp.show() (imp is an imagePlus). Is this correct way?


This is a whole new world -- and it is very powerful, and there is a lot
to learn...

On 8/22/13 12:05 PM, Jan Eglinger wrote:
>>> You might also consider using the ExtendedPluginFilter interface, which
>>> provides extended preview functionality.
>>>
>> Is there an example of this in python?
>
> I don't know of any Python example, but there are plenty of Java
> examples in the ImageJ source:
>

Would an ExtendedPluginFilter in python look something like this?

import ij.plugin.filter
class myPlugin(ExtendedPluginFilter)
        def setup(arguments, imp)
                .
                .
                .
        def showDialog(imp, command, pfr)
                .
                .
                .
        def run(ip)
                .
                .
                .

I dont really understand the idea of "implementing an interface".
       
>
> If you finish developing your own python script implementing
> ExtendedPlugInFilter, it would be great if you could add it to Fiji's
> Jython scripting page as an example!
>
> http://fiji.sc/Jython_Scripting
>

How do I post the script?
Actually, my script is very rough, but if I post it, perhaps the experts
can point out to me the many things that are either awkward or wrong,
and it can "evolve" into something much improved. This "evolution",
might provide a useful educational tool in the future.

--aryeh
--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html