Skip to content

Evolution of the spatial fitting to astropy model and preparation of the future spectral fitting model#55

Merged
mdebony merged 57 commits into
mainfrom
spatial_fit_astropy
Jun 25, 2026
Merged

Evolution of the spatial fitting to astropy model and preparation of the future spectral fitting model#55
mdebony merged 57 commits into
mainfrom
spatial_fit_astropy

Conversation

@mdebony

@mdebony mdebony commented Jul 17, 2025

Copy link
Copy Markdown
Owner

I have change the organization of the fitting procedure that now is in it's own class. It's to prepare for adding spectral fit procedure.

I have also change the fitting procedure to use astropy models for describing model to fit instead of our own set of functions. This point need a bit more validation.

@gabemery contributions:
Changed the spatial fit class to handle one to three dimensions (assumed energy , spatial 2D in this order if 3D).
Added a new class to define models from a function and used it to implement some models.
Added tests for the new functionnalities.

@mdebony
mdebony requested a review from gabemery July 17, 2025 15:15
@mdebony mdebony added the enhancement New feature or request label Jul 18, 2025

@gabemery gabemery left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Impressive work. It looks a bit less user friendly for users to provide functions, but if it is well documented it should not be a large issue.

Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
Comment thread baccmod/fitting.py Outdated
Comment thread baccmod/model.py Outdated
Comment thread baccmod/spatial_fit_acceptance_map_creator.py Outdated
Comment thread test/integration_test.py

@gabemery gabemery left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looking in depth to the fitting section, it seems like this implementation makes barely any use of the astropy implementation of models and, especially, fitters. In the current state, I would remove the dependency to Fitter.

Comment thread baccmod/fitting.py Outdated
Comment thread baccmod/fitting.py Outdated
Comment thread baccmod/fitting.py Outdated

# build list of free parameters
all_params = list(model_copy.param_names)
free_params = [p for p in all_params if (p not in tied) or (not tied[p])]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It could probably be done a bit easier with sets. But it works.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I'm not very familiar with sets, feel free to propose.

Comment thread baccmod/fitting.py Outdated
Comment thread baccmod/fitting.py Outdated
Comment thread baccmod/fitting.py Outdated
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
free_params = [p for p in all_params if (p not in tied) or (not tied[p]) and (not model_init.fixed.get(p, False))]

# Apply correction
for p in free_params:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should support normalising and fixing the normalisation. So not removing fixed parameters.

In that case, and if you also remove the boolean check on the tied, these lines could be replaced by :
for p in set(model_init.param_names) - set(tied.keys())) :

exemple code :
test={'a':0, 'b':0}
set(['a','c']) - set(test.keys())
-> {'c'}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

If you apply normalisation on a tied parameters it will be not be considered, as it will be replaced at the start of the fit. Instead I added a warning when a parameters in the list list_name_normalisation_parameter is not found in the indep_params list.

@gabemery gabemery left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Couple comments on the latest changes.

Comment thread baccmod/fitting.py Outdated
Comment thread baccmod/model.py Outdated
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
Comment thread baccmod/model.py
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
Comment thread baccmod/base_fit_acceptance_map_creator.py Outdated
@gabemery
gabemery force-pushed the spatial_fit_astropy branch from dbfd91e to e23c8d7 Compare February 9, 2026 14:36

@mdebony mdebony left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I've left a few comments. The main one is than we need to set some reference values and models for tests.

Comment thread test/model_unit_test.py

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Would be good to have check on the values in the test.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

done

Comment thread test/integration_test.py
# test having one bad paramenter in the list to normalise
list_name_normalisation_parameter=['amplitude', 'bad_param'])
background_model = bkg_maker.create_model(observations=self.obs_collection_pks_2155)
assert type(background_model) is Background3D

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

We need to add a reference as for the other test

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I was thinking that this is something you are the best placed to do since you did it for the other cases.

Comment thread test/integration_test.py
model_to_fit=model,
list_name_normalisation_parameter=['amplitude',])
background_model = bkg_maker.create_model(observations=self.obs_collection_pks_2155)
assert type(background_model) is Background3D

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

We need to add reference data and check on it.

Comment thread baccmod/model.py Outdated
Comment thread baccmod/model.py Outdated
return (powerlaw_inv_super_exp_cutoff_exp_cutoff(e, amplitude, index, eref, b, ecl, ech) *
gaussian2d_bilinear_gradient(x, y, 1.0, x_mean, y_mean, x_stddev, y_stddev, theta, x_gradient, y_gradient))

class PowerLawCutOffsEnergy(CustomModels):

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I think the name could be confused with the standard power law exponential cut off in the field. Not sure exactly how to name it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, without making it very long I had no better ideas. Or give it a physics related name

Comment thread baccmod/model.py
Comment thread baccmod/fit_acceptance_map_creator.py Outdated
Comment thread baccmod/fit_acceptance_map_creator.py Outdated
@gabemery
gabemery force-pushed the spatial_fit_astropy branch from 641538f to 05d90bf Compare April 8, 2026 09:49
@mdebony mdebony added this to the v0.5.0 milestone Apr 10, 2026

@gabemery gabemery left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The PR is ready for merging.

@mdebony
mdebony merged commit c5c6535 into main Jun 25, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants