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

Gaussian Fitting Tool #116

Merged
merged 17 commits into from
Dec 11, 2023
Merged

Gaussian Fitting Tool #116

merged 17 commits into from
Dec 11, 2023

Conversation

phys-cgarnier
Copy link
Collaborator

Initial pull request of gaussian fit tool, this tool receives an array of normalized data and attempts to fit it to three different methods: standard gaussian, super gaussian, and double gaussian. Then returns the fit parameters in a dictionary

Copy link
Member

@nneveu nneveu left a comment

Choose a reason for hiding this comment

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

Move fitting tool and test directly into data_analysis.

lcls_tools/common/data_analysis/fitting/fitting_tool.py Outdated Show resolved Hide resolved
lcls_tools/common/data_analysis/fitting/fitting_tool.py Outdated Show resolved Hide resolved
lcls_tools/common/data_analysis/fitting/fitting_tool.py Outdated Show resolved Hide resolved
lcls_tools/common/data_analysis/fitting/fitting_tool.py Outdated Show resolved Hide resolved
offset = initial_guess.pop("offset", np.mean(y[-10:]))
amplitude = initial_guess.pop("amplitude", y.max() - offset)
mu = initial_guess.pop("mu", np.argmax(y))
sigma = initial_guess.pop("sigma", y.shape[0] / 5)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe reconsider dividing by 5 there

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

At the moment I don't have a more mathematical approach to determine sigma but it is something I have been thinking about.

@staticmethod
def gaussian_with_linear_background(x, amp, mu, sig, offset, slope):
return (
amp * np.exp(-np.power(x - mu, 2.0) / (2 * np.power(sig, 2.0)))
Copy link
Member

Choose a reason for hiding this comment

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

repeated code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In the pydantic model version, I was going to make a model the gaussian functions. It would simply this. I can also just edit this code for the initial PR

Copy link
Member

Choose a reason for hiding this comment

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

ok so this will get updated in the next PR?

lcls_tools/common/data_analysis/fitting/fitting_tool.py Outdated Show resolved Hide resolved
) + offset

@staticmethod
def two_dim_gaussian(x, y, A, x0, y0, sigma_x, sigma_y):
Copy link
Member

Choose a reason for hiding this comment

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

add comment here about possible use with images

@roussel-ryan
Copy link
Collaborator

@nneveu and I discussed today and I think we should change this to use scipy.minimize instead of scipy.curve_fit before this PR is accepted. @phys-cgarnier let me know if you need help w making this change

@phys-cgarnier
Copy link
Collaborator Author

phys-cgarnier commented Nov 30, 2023 via email

@nneveu
Copy link
Member

nneveu commented Dec 6, 2023

@phys-cgarnier I think the load issue might be with the test. If you run the test locally does it load sklearn ok? You might need to import it in the test as well.

Copy link
Member

@nneveu nneveu left a comment

Choose a reason for hiding this comment

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

I wonder if we can use scipy.stats in some places here.
https://docs.scipy.org/doc/scipy/tutorial/stats.html

General comments for next PR, let's try to have fewer inputs without names/dicts and a few more comments.

Thanks for working on this @phys-cgarnier !

param_names = method.__code__.co_varnames
param_names = list(param_names)
for i in range(len(param_names)):
if param_names[i] == "x":
Copy link
Member

Choose a reason for hiding this comment

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

Why the 'x' here in these two functions?

@nneveu nneveu merged commit 218dfb6 into slaclab:main Dec 11, 2023
1 check passed
Derikka pushed a commit to Derikka/lcls-tools that referenced this pull request Mar 17, 2024
* initial commit of fitting tool

* initial submission of gaussian fit tool, this tool takes a normalized distribution and fits to three different guassian distributions then returns the fit params in a dictionary

* reformatted scripts with black

* did more reformatting then used black for final check

* updated for pr

* fixed flake8 errors

* fixed flake8

* attempt to fix flake8 errors

* hopefully last flake8 fix....

* added sklearn to requirements.txt

* changed sklearn to scikit-learn

* black on fitting_tool.py

---------

Co-authored-by: Nicole <nneveu@stanford.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants