Dynamically Removing Models From A Composite Model Object

37 Views Asked by At

I'm working on a script that automatically deduces the number of peaks in spectroscopic data and attempts to fit them.

It first tries a lorentzian at the highest point in the data, and calculates the residuals. It then looks for the consecutively highest points in the residuals to try fits there.

At every iteration, I am checking the LnL against the previous LnL's iteration. My current issue is that there appears to be no way to remove a model from a composite model once it has been added. I can't simply skip adding the model to begin with, because I have to have the model added to see whether the LnL improves or not.

As for alternative peak finding methods, I have tried find_peaks, but it didn't work out - too much reliance on manual inputting and fiddling with prominence and the likes.

Is there any way to delete a model after adding it to the composite model? I have tried various loops with del, and managed to get a loop that deletes the params, but I don't have a way to remove the model itself.

Thanks in advance.

UPDATE:

This seemed to do the trick. Note that (globals()[f"gauss{CI}"])] is here because I create variables dynamically with my script, it may not be necessary for you if you do it manually. Gaussi is my main model.

new_components = [model for model in gaussi.components if model != (globals()[f"gauss{CI}"])]
        print("BEGONE...!")
        # Create a new composite model without the removed model
        new_composite_model = new_components[0]
        for model in new_components[1:]:
            new_composite_model += model
        gaussi = new_composite_model
0

There are 0 best solutions below