Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Interface for MaCh3 #176

Merged
merged 28 commits into from
Oct 21, 2024
Merged

Python Interface for MaCh3 #176

merged 28 commits into from
Oct 21, 2024

Conversation

ewanwm
Copy link
Collaborator

@ewanwm ewanwm commented Oct 21, 2024

Pull request description

Have now added python bindings for the main objects that the experiments need to interface with. This means that MaCh3 fitters (MCMC and Minuit) can be run purely in python. Additionally these objects can be extended using python so for example someone could quickly prototype some new exotic MCMC algorithm and test it in python before implementing a c++ version.

My initial goal was to get the python interface to a point where it could reproduce everything that can currently be done in the MaCh3Tutorial. I ended up going a bit beyond that and (partially) implementing samplePDF too (see note below).

I have written documentation for "pyMaCh3" which covers high level usage of the bound objects and points to the wiki where helpful. This can be updated further in future with more usage examples.

pyMach3 sphinx example

The API is also fully documented as part of this:

pyMach3 sphinx api example

This documentation can be generated using the instructions given in the readme in Doc/sphinx/README.md. In the future these will be automatically generated and can be linked with the existing doxygen docs. This should be a pretty easy job but I will leave it to it's own pull request to avoid clutter.

Note on Sample PDF

At the moment only samplePDFBase has been fully implemented in python. samplePDFFDBase has not yet been fully implemented. The plan is to do this in the future once the interface for this has been more fully settled and samplePDFFDBase and samplePDFBase have been unified.

At the moment however there is enough that a user can implement their own samplePDF object, but need to deal with the reweighting and binning of events themselves and can't make use of the more "automated" ways of doing these things that are provided in samplePDFFDBase.

Changes or fixes

There are very few changes to the underlying c++ code other than the addition of a few getters and setters which are needed to access some private members of the c++ classes (python binding is complicated and the python objects are not able to directly access the private or protected members of the bound c++ class)

Have also moved the existing python plotting interface to be a part of the larger pyMaCh3 module. Have also restructured so that all of the pybind11 code lives in the python/ folder to keep things tidier and keep the cmake cleaner.

Other than this the only changes are the addition of pybind11 binding code.

Examples

I've written and ran some python test scripts:

sample-pdf-test.py.txt

basic-fitter-test.py.txt

spline-monolith-test.py.txt

All of these work and once this PR is merged these can be added to the MaCh3Tutorial as both tutorial examples and as part of the CI

Just to be sure, I've ran a 10M step stat only chain in python and the same using the MaCh3Tutorial and compared:

Python_vs_cpp_10M_step.pdf

ewanwm added 27 commits October 9, 2024 06:26
…using memory weirdness when building TSpline3s. We can get around this by... not doing that
…riance and samplePDF objects. Can now juuuust about build a samplePDF class in python!
@github-actions github-actions bot added the Documentation Improvements or additions to documentation label Oct 21, 2024
@github-actions github-actions bot added Nu Osc/Xsec Related with neutrino interactions or oscialtions python Samples Cmake labels Oct 21, 2024
Copy link

Hi @ewanwm, thank you for contributing to MaCh3! Please wait for MaCh3 developers to review your PR. If no one answers within a week, please message people from this list: https://github.com/orgs/mach3-software/teams/mach3admin . While waiting, please enjoy this

meme

Use this action on your projects. Use jokes on issues instead.


if (length == 1)
{
_float_ *pars[3];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this isn't an std::vector?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope

Copy link
Contributor

@henry-wallace-phys henry-wallace-phys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, a few minor fixes. Have you tested this in some dummy app?

@ewanwm
Copy link
Collaborator Author

ewanwm commented Oct 21, 2024

Yeah tested with these

sample-pdf-test.py.txt

basic-fitter-test.py.txt

spline-monolith-test.py.txt

once this is merged will move them to the MaCh3Tutorial to be part of the CI as well

@henry-wallace-phys
Copy link
Contributor

.py.txt is incredibly cursed but looks good!

@ewanwm
Copy link
Collaborator Author

ewanwm commented Oct 21, 2024

.py.txt is incredibly cursed but looks good!

😆 agreed

Copy link
Contributor

@henry-wallace-phys henry-wallace-phys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fantastic, very exciting to see

@ewanwm ewanwm merged commit dce53c0 into develop Oct 21, 2024
10 checks passed
@ewanwm ewanwm deleted the feature_fitter_python_interface branch October 21, 2024 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cmake Documentation Improvements or additions to documentation Nu Osc/Xsec Related with neutrino interactions or oscialtions python Samples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants