Skip to content

Commit

Permalink
tmp solution to lmfit bad API
Browse files Browse the repository at this point in the history
  • Loading branch information
KedoKudo committed Oct 14, 2024
1 parent bee5f6c commit 36d345c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
19 changes: 10 additions & 9 deletions src/ibeatles/core/fitting/kropff/high_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""Bragg peak detection, high lambda"""

import numpy as np
from lmfit import Model
from lmfit import Model, Parameter
from typing import Tuple, Dict, Any
from ibeatles.core.fitting.kropff.fitting_functions import (
kropff_high_lambda_transmission,
Expand Down Expand Up @@ -51,16 +51,17 @@ def fit_high_lambda(
# for fitting, but the transmission function for plotting.
model = Model(kropff_high_lambda_attenuation, independent_vars=["lda"])

# Set up parameters with initial guesses
# NOTE: we explicitly specify a0 and b0 to be the fitting parameters, in case
# the implicit behavior of lmfit changes in the future.
params = model.make_params(
a0=fitting_parameters.lambda_min, b0=fitting_parameters.lambda_max
)

# Perform the fit
# NOTE: Until this issue (https://github.com/lmfit/lmfit-py/issues/971) is resolved,
# we must define the parameters in the fit function call explicitly and avoid
# using the `make_params` method.
try:
result = model.fit(ydata, params, lda=xdata)
result = model.fit(
ydata,
lda=xdata,
a0=Parameter("a0", value=fitting_parameters.lambda_min, vary=True),
b0=Parameter("b0", value=fitting_parameters.lambda_max, vary=True),
)
except Exception as e:
raise ValueError(f"Fitting failed: {str(e)}")

Expand Down
20 changes: 11 additions & 9 deletions src/ibeatles/core/fitting/kropff/low_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,19 @@ def fit_low_lambda(
# Create lmfit Model
model = Model(kropff_low_lambda_attenuation, independent_vars=["lda"])

# Set up parameters with initial guesses
params = model.make_params(
a0=Parameter("a0", value=a0, vary=False),
b0=Parameter("b0", value=b0, vary=False),
ahkl=fitting_parameters.lambda_min,
bhkl=fitting_parameters.lambda_max,
)

# Perform the fit
# NOTE: Until this issue (https://github.com/lmfit/lmfit-py/issues/971) is resolved,
# we must define the parameters in the fit function call explicitly and avoid
# using the `make_params` method.
try:
result = model.fit(ydata, params, lda=xdata)
result = model.fit(
ydata,
lda=xdata,
a0=Parameter("a0", value=a0, vary=False),
b0=Parameter("b0", value=b0, vary=False),
ahkl=Parameter("ahkl", value=fitting_parameters.lambda_min, vary=True),
bhkl=Parameter("bhkl", value=fitting_parameters.lambda_max, vary=True),
)
except Exception as e:
raise ValueError(f"Fitting failed: {str(e)}")

Expand Down

0 comments on commit 36d345c

Please sign in to comment.