bonej-org / BoneJ2

Plugins for bone image analysis
BSD 2-Clause "Simplified" License
20 stars 12 forks source link

Cannot use macro to save Java windows in Fiji #142

Open kld123509945 opened 5 years ago

kld123509945 commented 5 years ago

Hi all,

I'm using macro to record the results of the Fractal dimension for a series of stacks. After I run the Fractal dimension, two Java windows jump out to inform me the results. However, there's no function or menu to save the results. This become a problem when I'm batching a series of CT images.

The Java window cannot be recognised by Fiji, as I ran the macro: getList("window.titles") to detect the non-image window titles, and the results missed the two Java window.

http://imglf5.nosdn0.126.net/img/M3B0VGdGQVdIRGh6eWhWQVdOdkNOODYveUFIUlcvN09GUStxaGVtcVlWcU9YYXR0Y2JUVVBBPT0.png?imageView&thumbnail=1921y1729&type=png&quality=96&stripmeta=0

Also I cannot simulate the Ctrl+A and Ctrl+C in Fiji, which means that during batching I cannot copy the results in the Java window into the clipboard. I used to try to open a Results table in Fiji to paste them so that I could save the Results table, but this attempt failed as when I pressed "Ctrl+V" and nothing happened.

This problem exists in all command expect "Particle analyser" in BoneJ. So could I automatically save the results? Or do I currently have to run the command and save them by my hands? Thanks in advance.

rimadoma commented 5 years ago

@alessandrofelder You know how to get the results in a Python script, and how to run the modern plug-ins from there, right?

alessandrofelder commented 5 years ago

Hi @kld123509945

Thanks for helping us by using the experimental version of BoneJ!

As @rimadoma suggests, I have written some python scripts to extract results from BoneJ2 results tables - the python code can be found here

In particular, take a look at lines 55-75 that show you how to call a BoneJ2 command (also called a wrapper, in this case, it is the IntertrabecularAngleWrapper), access the results table (in IntertrabecularAngleWrapper, the results table is called "anglesTable", but in the AnisotropyWrapper it is called "resultsTable"), and write the results table into a csv file.

I hope this helps - please keep asking questions if it doesn't!

Best, Alessandro

kld123509945 commented 5 years ago

@alessandrofelder Thanks so much for your help! I have copy the lines 55-75 into the Plugins-New-text window, but it cannot run. Also I fail to see a choice to test the Python code in the Plugins. So did you advise me to use code of lines 55-75 to save the BoneJ results? e.g. If now I have run the macro to the stage of:

http://imglf4.nosdn0.126.net/img/M3B0VGdGQVdIRGlvc1hoOWNtRU83aC9SVkJMR3dzS1hDTGRaK0JMcERlcDR0STc2cUhNaC9nPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0

What should I write in the window to continue to save the result? Should I add the lines 55-75 there? I'm so sorry that I'm not a expert at Python, so I currently using the IJ1 macro to batch the images. Thanks in advance for your detailed reply!

alessandrofelder commented 5 years ago

Hi @kld123509945 ,

did you advise me to use code of lines 55-75 to save the BoneJ results?

I was thinking you could use those lines as an example of how to write a separate python script (replacing your macro) that can call a BoneJ2 plug-in on many images and save the results.

so I currently using the IJ1 macro to batch the images

If you have an IJ macro that works and it is not too annoying to copy paste the results manually after the results are listed, I suggest you keep it that way - stick with what works :)

If you do want to try python, you need a separate python (.py) file. You can call it something like computeFractalDimensionOnMultipleImages.py, for example, and paste lines 55-75 into it, but you will have to adapt this code to your situation: you want to call a different BoneJ2 plug-in, the results table has a different name (it is called "resultsTable", as you can see here) and you probably want to call your files a different name as well.

Some help to understand the code better maybe: Lines 55-57 call the BoneJ2 plug-in. Line 60 gets the results table. Lines 61/62 prepare a file to get written. Lines 63-75 write the results to this file.

I think if you have written some IJ macros before and understand for-loops and if-statements, it should be possible for you to get it to work with some effort, but feel free to ask more questions about it - if you do, attach the python code and any outputs and error messages you get, so I can help you more easily.

Hope this helps, Alessandro

PedroACFerreira commented 4 years ago

@alessandrofelder I had to comment just to let you know you are my savior, I couldn't for the life of me find any documentation explaining how to do this. I'm not too experienced with coding ImageJ scripts, and I was trying to get BoneJ2 Fractal Dimension running on 200+ files on different folders, your example made things considerably easier! If anyone needs the script message me, although you'd need to modify it a bit!

eliamson commented 3 years ago

Hi @alessandrofelder, many thanks for these explanations, which were very useful to me, too. I've applied the solution to the SurfaceFractionWrapper. But strangely I get slightly different values of BV and TV than when I run the plugin through the GUI...any idea why?

Thanks for your incredible work with BoneJ!

All the best, Eli

alessandrofelder commented 3 years ago

Thanks @eliamson !

It's hard to tell what's happening with the limited info you provide. Would you be able to provide example code, one example image and the outputs you get through the GUI and through the script?

Best, Alessandro

eliamson commented 3 years ago

Many thanks for looking into this @alessandrofelder. In the table, the GUI results are on the second row. ArchiveForAlessandro.zip

All the best, Eli

alessandrofelder commented 3 years ago

Hey @eliamson ,

I get the same results both with the GUI and the script for SurfaceFractionWrapper, and they match the first row of your results. I get the second row of your results if I run BoneJ > Fraction > Area/Volume fraction instead of BoneJ > Fraction > Surface Fraction. So I believe the difference is because you are running two distinct (albeit in some ways similar) commands.

I've linked to the documentation for each command below:

Hope this helps! PS In the interest of people with similar questions in the future, I would encourage you to ask on the image.sc forum with the #bonej tag next time - it will be easier to find.

Best, Alessandro

eliamson commented 3 years ago

Thank you so much for clearing this out @alessandrofelder...I obviously did not realise I was using different commands.

All the best, Eli