Skip to content

Commit 37adb66

Browse files
committed
add comment and print statements showing whats been initialized
1 parent e585f2f commit 37adb66

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/diffpy/srfit/fitbase/fitrecipe.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,25 +1498,38 @@ def _prepare_results_for_initialization(self, results):
14981498
raise ValueError(
14991499
"results must be a FitResults object, str, or pathlib.Path"
15001500
)
1501-
# Remove metrics like Rw from the dict, leaving only parameter values
1501+
# Remove metrics like Rw from the dict, leaving only parameter values.
1502+
# If a non-refinable quantity is ever stored as a tuple in the
1503+
# results dictionary, it will be incorrectly treated as a parameter
1504+
# and included here, which could lead to errors.
15021505
parameters_dict = {
15031506
k: v for k, v in results_dict.items() if isinstance(v, tuple)
15041507
}
15051508
return parameters_dict
15061509

1507-
def initialize_recipe_from_results(self, results):
1510+
def initialize_recipe_from_results(self, results, verbose=True):
15081511
"""Initialize the variable values from a previous fit result.
15091512
15101513
Parameters
15111514
----------
15121515
results : FitResults object, str, or pathlib.Path object
15131516
The results from a previous fit. These results should
15141517
have the same parameters as the current FitRecipe.
1518+
verbose : bool, optional
1519+
If True, print the initialized parameter values. Default is True.
15151520
"""
15161521
parameters_dict = self._prepare_results_for_initialization(results)
1522+
1523+
if verbose:
1524+
print("\nInitializing FitRecipe from results")
1525+
print("-" * 40)
1526+
if verbose and parameters_dict:
1527+
width = max(len(name) for name in parameters_dict)
15171528
for name, (value, uncertainty) in parameters_dict.items():
15181529
if name in self._parameters:
15191530
self._parameters[name].setValue(value)
1531+
if verbose:
1532+
print(f"{name:<{width}} = {value}")
15201533

15211534

15221535
# End of file

src/diffpy/srfit/fitbase/fitresults.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,11 @@ def initializeRecipe(recipe, results):
918918
919919
::
920920
921-
from diffpy.srfit.fitbase import FitRecipe, FitResults
922921
from scipy.optimize import leastsq
922+
import numpy as np
923+
from diffpy.srfit.fitbase import (
924+
Profile, FitRecipe, FitResults, FitContribution
925+
)
923926
924927
def optimize_recipe(recipe):
925928
residuals = recipe.residual
@@ -928,8 +931,8 @@ def optimize_recipe(recipe):
928931
929932
930933
profile = Profile()
931-
x = linspace(0, pi, 10)
932-
y = sin(x)
934+
x = np.linspace(0, np.pi, 10)
935+
y = np.sin(x)
933936
profile.set_observed_profile(x, y)
934937
contribution = FitContribution("c1")
935938
contribution.set_profile(profile)
@@ -944,6 +947,10 @@ def optimize_recipe(recipe):
944947
results = FitResults(recipe)
945948
946949
new_recipe = FitRecipe("my_recipe")
950+
new_recipe.add_contribution(contribution)
951+
new_recipe.add_variable(contribution.amplitude, 0)
952+
new_recipe.add_variable(contribution.wave_number, 0)
953+
new_recipe.add_variable(contribution.phase_shift, 0)
947954
new_recipe.initialize_recipe_from_results(results)
948955
"""
949956
mpairs = resultsDictionary(results)

0 commit comments

Comments
 (0)