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

Pre-compute pathways for fitting #242

Merged
merged 11 commits into from
May 15, 2023
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ What's new

- Support gamma angle averaging.

**Simulator**

- New instance method for the `Simulator` class -- `.optimize()` -- which pre-computes transition pathways before least-squares minimization. This improves the efficiency of least-squares minimizations.

v0.7.0
------

Expand Down
10 changes: 9 additions & 1 deletion docs/introduction/fitting_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,15 @@ minimization, and print the
:context: close-figs

from lmfit import Minimizer
minner = Minimizer(sf.LMFIT_min_function, fit_parameters, fcn_args=(sim, processor, sigma))
# Optimize fitting by pre-computing transition pathways
opt = sim.optimize()

minner = Minimizer(
sf.LMFIT_min_function,
fit_parameters,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt}
)
result = minner.minimize()
result

Expand Down
2 changes: 1 addition & 1 deletion docs/introduction/isotopomers_example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ spin systems and the list of your two methods, and run the simulations.

sim = Simulator(
spin_systems = [isotopomer1, isotopomer2, isotopomer3],
methods=[method_H, method_C]
methods = [method_H, method_C]
)
sim.run()

Expand Down
31 changes: 19 additions & 12 deletions fitting_source/1D_fitting/plot_1_31P_Na2PO4_MAS.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,6 @@
experiment=experiment, # experimental dataset
)

# A method object queries every spin system for a list of transition pathways that are
# relevant to the given method. Since the method and the number of spin systems remains
# unchanged during the least-squares analysis, a one-time query is sufficient. To avoid
# querying for the transition pathways at every iteration in a least-squares fitting,
# evaluate the transition pathways once and store it as follows
for sys in spin_systems:
sys.transition_pathways = MAS.get_transition_pathways(sys)

# %%
# **Step 3:** Create the Simulator object and add the method and spin system objects.
sim = Simulator(spin_systems=spin_systems, methods=[MAS])
Expand Down Expand Up @@ -169,13 +161,28 @@
print(params.pretty_print(columns=["value", "min", "max", "vary", "expr"]))

# %%
# **Step 7:** Perform the least-squares minimization. For the user's convenience, we
# **Step 7:** Perform the least-squares minimization.
# A method object queries every spin system for a list of transition pathways that are
# relevant to the given method. Since the method and the number of spin systems remains
# unchanged during the least-squares analysis, a one-time query is sufficient. To avoid
# querying for the transition pathways at every iteration in a least-squares fitting,
# call the :py:mth:~`mrsimulator.Simulator.optimize()` method to pre-compute the
# pathways.
#
# For the user's convenience, we
# also provide a utility function,
# :func:`~mrsimulator.utils.spectral_fitting.LMFIT_min_function`, for evaluating the
# difference vector between the simulation and experiment, based on
# the parameters update. You may use this function directly as the argument of the
# LMFIT Minimizer class, as follows,
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
# the parameters update. You may use this function directly to instantiate the
# LMFIT Minimizer class where `fcn_args` and `fcn_kws` are arguments passed to the
# function, as follows,
opt = sim.optimize() # Pre-compute transition pathways
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
13 changes: 7 additions & 6 deletions fitting_source/1D_fitting/plot_1_31p_Na2PO4_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@
experiment=experiment, # experimental dataset
)

# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = static1D.get_transition_pathways(sys)

# %%
# **Guess Model Spectrum**

Expand Down Expand Up @@ -122,7 +117,13 @@

# %%
# **Solve the minimizer using LMFIT**
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
opt = sim.optimize() # Pre-compute transition pathways
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
14 changes: 7 additions & 7 deletions fitting_source/1D_fitting/plot_2_13C_glycine_960Hz.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@
spectral_dimensions=spectral_dims,
experiment=experiment, # experimental dataset
)

# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = MAS.get_transition_pathways(sys)

# %%
# **Guess Model Spectrum**

Expand Down Expand Up @@ -126,7 +120,13 @@

# %%
# **Solve the minimizer using LMFIT**
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
opt = sim.optimize()
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,13 @@

# %%
# **Solve the minimizer using LMFIT**
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processors, sigmas))
opt = sim.optimize() # Pre-compute transition pathways
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processors, sigmas),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
12 changes: 7 additions & 5 deletions fitting_source/1D_fitting/plot_2_PASS_cross_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@
experiment=pass_cross_section, # also add the measurement to the method.
)

# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = PASS.get_transition_pathways(sys)

# %%
# **Guess Spectrum**
Expand Down Expand Up @@ -122,7 +118,13 @@

# %%
# Run the minimization using LMFIT
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
opt = sim.optimize()
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
20 changes: 10 additions & 10 deletions fitting_source/1D_fitting/plot_3_Na2SiO3.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@
experiment=experiment, # experimental dataset
)

# A method object queries every spin system for a list of transition pathways that are
# relevant for the given method. Since the method and the number of spin systems remain
# the same during the least-squares fit, a one-time query is sufficient. To avoid
# querying for the transition pathways at every iteration in a least-squares fitting,
# evaluate the transition pathways once and store it as follows
for sys in spin_systems:
sys.transition_pathways = MAS_CT.get_transition_pathways(sys)

# %%
# **Step 3:** Create the Simulator object and add the method and spin system objects.
Expand Down Expand Up @@ -177,9 +170,16 @@
# provide a utility function,
# :func:`~mrsimulator.utils.spectral_fitting.LMFIT_min_function`, for evaluating the
# difference vector between the simulation and experiment, based on
# the parameters update. You may use this function directly as the argument of the
# LMFIT Minimizer class, as follows,
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
# the parameters update. You may use this function directly to instantiate the
# LMFIT Minimizer class where `fcn_args` and `fcn_kws` are arguments passed to the
# function, as follows,
opt = sim.optimize()
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
13 changes: 7 additions & 6 deletions fitting_source/1D_fitting/plot_4_11B_Quad_NMR.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@
experiment=experiment, # add the measurement to the method.
)

# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = MAS_CT.get_transition_pathways(sys)

# %%
# **Guess Model Spectrum**

Expand Down Expand Up @@ -119,7 +114,13 @@

# %%
# **Solve the minimizer using LMFIT**
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
opt = sim.optimize() # Pre-compute transition pathways
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
13 changes: 7 additions & 6 deletions fitting_source/1D_fitting/plot_4_27Al_YAG.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@
experiment=experiment, # add the measurement to the method.
)

# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = MAS.get_transition_pathways(sys)

# %%
# **Guess Spectrum**

Expand Down Expand Up @@ -132,7 +127,13 @@

# %%
# **Solve the minimizer using LMFIT**
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
opt = sim.optimize() # Pre-compute transition pathways
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
12 changes: 7 additions & 5 deletions fitting_source/1D_fitting/plot_4_2H_quad.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@
experiment=experiment, # experimental dataset
)

# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = MAS.get_transition_pathways(sys)

# %%
# **Guess Model Spectrum**
Expand Down Expand Up @@ -120,7 +116,13 @@

# %%
# **Solve the minimizer using LMFIT**
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
opt = sim.optimize() # Pre-compute transition pathways
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
12 changes: 7 additions & 5 deletions fitting_source/1D_fitting/plot_5_119Sn_sideband.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@
experiment=experiment, # add the measurement to the method.
)

# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = MAS.get_transition_pathways(sys)

# %%
# **Guess Spectrum**
Expand Down Expand Up @@ -155,7 +151,13 @@

# %%
# **Solve the minimizer using LMFIT**
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
opt = sim.optimize() # Pre-compute transition pathways
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
13 changes: 7 additions & 6 deletions fitting_source/2D_fitting/plot_1_LHistidine_PASS.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@
experiment=mat_dataset, # add the measurement to the method.
)

# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = PASS.get_transition_pathways(sys)

# %%
# **Guess Spectrum**

Expand Down Expand Up @@ -137,7 +132,13 @@

# %%
# **Solve the minimizer using LMFIT**
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
opt = sim.optimize() # Pre-compute transition pathways
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
13 changes: 7 additions & 6 deletions fitting_source/2D_fitting/plot_1_Rb2SO4_QMAT.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@
experiment=qmat_dataset, # add the measurement to the method.
)

# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = PASS.get_transition_pathways(sys)

# %%
# **Guess Spectrum**

Expand Down Expand Up @@ -135,7 +130,13 @@

# %%
# **Solve the minimizer using LMFIT**
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
opt = sim.optimize() # Pre-compute transition pathways
minner = Minimizer(
sf.LMFIT_min_function,
params,
fcn_args=(sim, processor, sigma),
fcn_kws={"opt": opt},
)
result = minner.minimize()
result

Expand Down
Loading