sofa-framework / sofa

Real-time multi-physics simulation with an emphasis on medical simulation.
https://www.sofa-framework.org
GNU Lesser General Public License v2.1
871 stars 297 forks source link

Adding new features for AdvancedTimer. Can now return two different o… #360

Closed ggrosshenny closed 6 years ago

ggrosshenny commented 6 years ago

This PR is en enhancement of the AdvancedTimer. We have added the possibility to get two different JSON ouputs. The first one represent all informations without deep limitation and the other is easier to use but does not represent all the componants tree of the simulation.

You can now use the timer in C++ and in python, change dynamically the output type of each timer separately and use the output of the AdvancedTimer to create graphs and/or compare datas from two different scene timers.

You can find an exemple of JSON output here : JSON_example.txt And an exemple of Light JSON output here : LJSON_example.txt

This work was done with the help of @untereiner and @chpaulus. A documentation will come at the end of my internship.

suggested tag : enhancement


This PR:

Reviewers will merge only if all these checks are true.

sofabot commented 6 years ago

Thank you for your pull request! Someone will soon check it and start the builds.

hugtalbot commented 6 years ago

Could you let us know about the json dependency and its related license?

damienmarchal commented 6 years ago

@ggrosshenny Thank you for you PR. Being able to save timing in JSON is a very nice idea and as more and more sofa component are using JSON so it make sense to have that.

Now, some feedback about the implementation:

So, it should be like that:

if(!PyArg_ParseTuple(args, "sO", &id, &tempNode))
    return NULL;

Instead of:

if(!PyArg_ParseTuple(args, "sO", &id, &tempNode))
{
        PyErr_BadArgument();
        Py_RETURN_NONE;
}
ggrosshenny commented 6 years ago

Thank you for your feedback @damienmarchal !

I've done most of your enhancements, but I can't figure out what tests we can create for this PR. Do you have some suggestions ?

damienmarchal commented 6 years ago

Hi @ggrosshenny

For the tests I think that the ideal target of testing to have a good code coverage (https://en.wikipedia.org/wiki/Code_coverage). A very strong level of testing may requires "path coverage". This means that all the execution path are at least executed once. This is very hard to achieve an so a more manageable target is to be close to "function coverage"+"branch coverage" in which each line of the function is at least executed once. I found that in sofa a succesfull strategy is to make tests with "Parameter value coverage" in which in a method taking parameters, all the common values for such parameters be considered. This give good results and is really easy to do.

More concretely in your PR you could:

And in Sofa_end:

   Sofa_end("validId", aNode)
   Sofa_end("", aNode)
   Sofa_end("validId", 1.0)      # This shouldn't crash but report a python exception    
   Sofa_end("validId", None)  # This shouldn't crash but report a python exception

A weaker approach of testing (if you really lack of time) is to only test "the good" case and not invalid values. To me this is only acceptable if the function is not accessible by the users and never process data provided by users.

More feedback:

Hope this will help,

ggrosshenny commented 6 years ago

Hello all,

I've added some modifications on this PR.

First I've created a new Python script in PythonSofa to make the use of the AdvancedTimer easier. Now, if you want to use it in a Python scene, the easiest way to do it is to use the script with the method measureAnimationTime. You will have to add this line:

    from SofaPython import PythonAdvancedTimer

at the beginning of your script. Then you have to add the method bwdInitGraph(self, node) method at least as the following:

    def bwdInitGraph(self, node):
        # It will call the simulationStep method too
        PythonAdvancedTimer.measureAnimationTime(node, "timerPoutre", 
        2, "ljson", "poutre_grid_sofa_timerLog", 0.1, 1000)
        return 0

If you already have defined a bwdInitGraph, you juste have to add the measureAnimationTime() method to get your scene analyzed.

To use it by your own way, you can take a look at the PythonAdvancedTimer.py script. You'll find it in the Sofa project at SofaPython/python/SofaPython/PythonAdvancedTimer.py.

Second, I've added two scripts to use the returned light JSON files of the AdvancedTimer : TimerLJSONPlot.py and timerLjsonManyFilesPlot.py. I've also added the documentation to informe future users how to use the AdvancedTimer and the plotting scripts.

The tests will come soon. Thank you for your feedback @damienmarchal .

chrijopa commented 6 years ago

I tested your work yesterday in the evening and I really appreciated the easy usage! I would like to add some remarks that might be interesting for future improvements:

  1. If there is a possibility to use the advanced timer at initialization (see #357), where do we need to perform changes to use your code? I would guess the main changes have to be made in PythonAdvancedTimer.py and in the reading of the json file, since the first time step has the identifier 0.
  2. Currently -c and your time evaluation exist side by side. Is it possible to combine your work with the command line argument -c?
  3. Similarly, your time evaluation exists side by side with the command -n (this variable is used in batch mode when launching the simulation for a certain number of steps). When running my simulation, I realized, your number of steps and the number of steps given by the command line add up. Again here, it would be valuable to know: how is it possible to directly use the command line argument?

More ideas might follow, thank you for your investment of time in this nice feature that I will use certainly!

untereiner commented 6 years ago

Hi @damienmarchal

I need a review of the first version of the unit tests.

guparan commented 6 years ago

[ci-build]

untereiner commented 6 years ago

What do I(we) do now ? Should I add other tests ? @damienmarchal @guparan

damienmarchal commented 6 years ago

@untereiner sorry I didn't saw you expected feedback on the tests. I will do that next monday.

untereiner commented 6 years ago

Hi @damienmarchal ! Do you have any comments on the unit tests ?

damienmarchal commented 6 years ago

Thanks.

To me it is enough ;) So unless someone complain... and the CI is ok let's go for the ready tag. @hugtalbot , @bcarrez

damienmarchal commented 6 years ago

[ci-build]

untereiner commented 6 years ago

I changed a little bit the unit tests. I will add a last commit in a few minutes

untereiner commented 6 years ago

Lets [ci-build] again

untereiner commented 6 years ago

last [ci-build] 🙏

damienmarchal commented 6 years ago

[ci-build] 

damienmarchal commented 6 years ago

It does not compile anymore.

guparan commented 6 years ago

But why? 😢 [ci-build]

guparan commented 6 years ago

Build is OK now but there are still a bunch of failing tests. [ci-build]

untereiner commented 6 years ago

[ci-build]