-
Notifications
You must be signed in to change notification settings - Fork 23
feat: Initialize a fit recipe with a previous recipe #162
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
Changes from all commits
bc39b5a
1bb14ec
acfb674
a934f42
9c9fdc3
4b60d8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| **Added:** | ||
|
|
||
| * Added initialize_recipe_from_recipe to ``FitRecipe``. | ||
|
|
||
| **Changed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Deprecated:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Removed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Fixed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Security:** | ||
|
|
||
| * <news item> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1140,6 +1140,50 @@ def getBounds2(self): | |
| """ | ||
| return self.get_bounds_array() | ||
|
|
||
| def initialize_recipe_with_recipe(self, recipe_object): | ||
| """Initialize a FitRecipe with another FitRecipe. | ||
|
|
||
| This is used to initialize a FitRecipe with the contribution(s), | ||
| parameters, constraints and restraints of another FitRecipe. | ||
| If a duplicate contribution, parameter, constraint, or restraint | ||
| is added to the FitRecipe you are initializing, the value from the | ||
| added object will be used. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| recipe_object : FitRecipe | ||
| The FitRecipe to initialize with. | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If the object passed is not a FitRecipe. | ||
| """ | ||
| if not isinstance(recipe_object, FitRecipe): | ||
| raise ValueError( | ||
| "The input recipe_object must be a FitRecipe, " | ||
| f"but got {type(recipe_object)}." | ||
| ) | ||
|
|
||
| for contrib_object in recipe_object._contributions.values(): | ||
| if contrib_object not in self._contributions.values(): | ||
| self.add_contribution(contrib_object) | ||
|
|
||
| for param_name, param_object in recipe_object._parameters.items(): | ||
| if param_name not in self._parameters: | ||
| self._parameters.update({param_name: param_object}) | ||
|
|
||
| for ( | ||
| parameter_object, | ||
| constraint_object, | ||
| ) in recipe_object._constraints.items(): | ||
| if parameter_object not in self._constraints: | ||
| self._constraints.update({parameter_object: constraint_object}) | ||
|
|
||
| for restraint in recipe_object._restraints: | ||
| if restraint not in self._restraints: | ||
| self._restraints.add(restraint) | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| def set_plot_defaults(self, **kwargs): | ||
| """Set default plotting options for all future plots. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,7 +146,7 @@ def _capturestdout(f, *args, **kwargs): | |
| return _capturestdout | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| @pytest.fixture() | ||
| def build_recipe_one_contribution(): | ||
| "helper to build a simple recipe" | ||
| profile = Profile() | ||
|
|
@@ -164,32 +164,45 @@ def build_recipe_one_contribution(): | |
| return recipe | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| @pytest.fixture() | ||
| def build_recipe_two_contributions(): | ||
| "helper to build a recipe with two contributions" | ||
| """Helper to build a recipe with two physically related contributions.""" | ||
| profile1 = Profile() | ||
| x = linspace(0, pi, 10) | ||
| y1 = sin(x) | ||
| x = linspace(0, pi, 50) | ||
| y1 = sin(x) # amplitude=1, freq=1 | ||
| profile1.set_observed_profile(x, y1) | ||
|
|
||
| contribution1 = FitContribution("c1") | ||
| contribution1.set_profile(profile1) | ||
| contribution1.set_equation("A*sin(k*x + c)") | ||
|
|
||
| profile2 = Profile() | ||
| y2 = 0.5 * sin(2 * x) | ||
| y2 = 0.5 * sin(2 * x) # amplitude=0.5, freq=2 | ||
| profile2.set_observed_profile(x, y2) | ||
|
|
||
| contribution2 = FitContribution("c2") | ||
| contribution2.set_profile(profile2) | ||
| contribution2.set_equation("B*sin(m*x + d)") | ||
|
|
||
| recipe = FitRecipe() | ||
| recipe.add_contribution(contribution1) | ||
| recipe.add_contribution(contribution2) | ||
| recipe.add_variable(contribution1.A, 1) | ||
| recipe.add_variable(contribution1.k, 1) | ||
| recipe.add_variable(contribution1.c, 1) | ||
| recipe.add_variable(contribution2.B, 0.5) | ||
| recipe.add_variable(contribution2.m, 2) | ||
| recipe.add_variable(contribution2.d, 0) | ||
|
|
||
| # Add variables with reasonable initial guesses | ||
| recipe.add_variable(contribution1.A, 0.8) | ||
| recipe.add_variable(contribution1.k, 1.0) | ||
| recipe.add_variable(contribution1.c, 0.1) | ||
|
|
||
| recipe.add_variable(contribution2.B, 0.4) | ||
| recipe.add_variable(contribution2.m, 2.0) | ||
| recipe.add_variable(contribution2.d, 0.1) | ||
|
|
||
| # ---- Meaningful constraints ---- | ||
| recipe.constrain(contribution2.m, "2*k") | ||
| recipe.constrain(contribution2.d, contribution1.c) | ||
| recipe.constrain(contribution2.B, "0.5*A") | ||
| recipe.restrain(contribution1.A, 0.5, 1.5) | ||
| recipe.restrain(contribution1.k, 0.8, 1.2) | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improved this so we can actually test for constrain and restrain |
||
| return recipe | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Used
.add_contribution()since it actually takes a contribution object.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's what I am talkin' about. Much better than parsing a results file string.