3333matplotlib .use ("Agg" )
3434
3535
36+ def build_recipe_for_init_testing ():
37+ """A helper to build a FitRecipe with one contribution and three
38+ variables.
39+
40+ This is identical to the build_recipe_one_contribution fixture, but
41+ is a function instead of a fixture so we can call it to get unique
42+ recipe objects in a singular test.
43+ """
44+ profile = Profile ()
45+ x = linspace (0 , pi , 11 )
46+ y = sin (x )
47+ profile .set_observed_profile (x , y )
48+ contribution = FitContribution ("c1" )
49+ contribution .set_profile (profile )
50+ contribution .set_equation ("amplitude*sin(wave_number*x + phase_shift)" )
51+ recipe = FitRecipe ()
52+ recipe .add_contribution (contribution )
53+ recipe .add_variable (contribution .amplitude , 4 )
54+ recipe .add_variable (contribution .wave_number , 3 )
55+ recipe .add_variable (contribution .phase_shift , 2 )
56+ return recipe
57+
58+
3659class TestFitRecipe (unittest .TestCase ):
3760
3861 def setUp (self ):
@@ -530,48 +553,44 @@ def test_initialize_recipe_from_recipe_bad(build_recipe_two_contributions):
530553def test_initialize_recipe_from_results_object (build_recipe_one_contribution ):
531554 # Case: User initializes a FitRecipe from a FitResults object
532555 # expected: recipe is initialized with variables from previous fit
533- recipe1 = build_recipe_one_contribution ()
556+
557+ # create unique recipe1
558+ recipe1 = build_recipe_for_init_testing ()
534559 optimize_recipe (recipe1 )
535560 results1 = FitResults (recipe1 )
536561 expected_values = np .round (results1 .varvals , 5 )
537562 expected_names = results1 .varnames
538563
539- recipe2 = build_recipe_one_contribution ()
540- recipe2 .create_new_variable (
541- "extra_var" , 5
542- ) # should be included in the initialized recipe
564+ # create unique recipe2 with same contributions
565+ # and variables as recipe1 but not optimized yet
566+ recipe2 = build_recipe_for_init_testing ()
567+ assert recipe1 != recipe2
568+ # create a new var that should be include in the initialized recipe
569+ recipe2 .create_new_variable ("extra_var" , 5 )
543570 actual_values_before_init = [val for val in recipe2 .get_values ()]
544- actual_names_before_init = recipe2 .get_names ()
545- expected_names_before_init = [
546- "amplitude" ,
547- "extra_var" ,
548- "phase_shift" ,
549- "wave_number" ,
550- ]
551- expected_values_before_init = [
552- 4 ,
553- 3 ,
554- 2 ,
555- 5 ,
556- ] # the three variables + the extra_var
557-
558- assert actual_values_before_init == expected_values_before_init
559- assert sorted (actual_names_before_init ) == sorted (
560- expected_names_before_init
571+ actual_names_before_init = sorted (recipe2 .get_names ())
572+ # the three variables + the extra_var
573+ expected_names_before_init = sorted (
574+ [
575+ "amplitude" ,
576+ "extra_var" ,
577+ "phase_shift" ,
578+ "wave_number" ,
579+ ]
561580 )
562-
581+ expected_values_before_init = [4 , 3 , 2 , 5 ]
582+ assert actual_values_before_init == expected_values_before_init
583+ assert actual_names_before_init == expected_names_before_init
563584 recipe2 .initialize_recipe_with_results (results1 )
564585 optimize_recipe (recipe2 )
565586 results2 = FitResults (recipe2 )
566587 actual_values = np .round (results2 .varvals , 5 )
567588 actual_names = results2 .varnames
568589
569- expected_names = expected_names + [
570- "extra_var"
571- ] # add the new variable name to expected names
572- expected_values = list (expected_values ) + [
573- 5
574- ] # add the value of the new variable to expected values
590+ # add the new variable name to expected names
591+ expected_names = expected_names + ["extra_var" ]
592+ # add the value of the new variable to expected values
593+ expected_values = list (expected_values ) + [5 ]
575594 assert sorted (expected_names ) == sorted (actual_names )
576595 assert sorted (expected_values ) == sorted (list (actual_values ))
577596
@@ -585,7 +604,7 @@ def test_initialize_recipe_from_results_file(
585604 expected_names = ["amplitude" , "phase_shift" , "wave_number" ]
586605 expected_values = [1 , 1 , 0 ]
587606
588- recipe = build_recipe_one_contribution ()
607+ recipe = build_recipe_one_contribution
589608 recipe .initialize_recipe_with_results (results_file )
590609 results = FitResults (recipe )
591610 actual_values = np .round (results .varvals , 5 )
@@ -601,7 +620,7 @@ def test_initialize_recipe_from_results_file_bad(
601620 # Case: User tries to initialize a recipe with something that
602621 # isn't a path, str, or FitResults object
603622 # Expected: raised ValueError with message
604- recipe = build_recipe_one_contribution ()
623+ recipe = build_recipe_one_contribution
605624 bad_input = 12345 # not a valid input type
606625 msg = (
607626 "The input results must be a FitResults object or a path to a "
@@ -695,7 +714,7 @@ def build_recipe_from_datafile_deprecated(datafile):
695714
696715
697716def test_plot_recipe_bad_display (build_recipe_one_contribution ):
698- recipe = build_recipe_one_contribution ()
717+ recipe = build_recipe_one_contribution
699718 # Case: All plots are disabled
700719 # expected: raised ValueError with message
701720 plt .close ("all" )
@@ -725,7 +744,7 @@ def test_plot_recipe_before_refinement(capsys, build_recipe_one_contribution):
725744 # Case: User tries to plot recipe before refinement
726745 # expected: Data plotted without fit line or difference curve
727746 # and warning message printed
728- recipe = build_recipe_one_contribution ()
747+ recipe = build_recipe_one_contribution
729748 plt .close ("all" )
730749 before = set (plt .get_fignums ())
731750 # include fit_label="nothing" to make sure fit line is not plotted
@@ -753,7 +772,7 @@ def test_plot_recipe_before_refinement(capsys, build_recipe_one_contribution):
753772def test_plot_recipe_after_refinement (build_recipe_one_contribution ):
754773 # Case: User refines recipe and then plots
755774 # expected: Plot generates with no problem
756- recipe = build_recipe_one_contribution ()
775+ recipe = build_recipe_one_contribution
757776 optimize_recipe (recipe )
758777 plt .close ("all" )
759778 before = set (plt .get_fignums ())
@@ -790,7 +809,7 @@ def test_plot_recipe_two_contributions(build_recipe_two_contributions):
790809def test_plot_recipe_on_existing_plot (build_recipe_one_contribution ):
791810 # Case: User passes axes to plot_recipe to plot on existing figure
792811 # expected: User modifications are present in the final figure
793- recipe = build_recipe_one_contribution ()
812+ recipe = build_recipe_one_contribution
794813 optimize_recipe (recipe )
795814 plt .close ("all" )
796815 fig , ax = plt .subplots ()
@@ -810,7 +829,7 @@ def test_plot_recipe_on_existing_plot(build_recipe_one_contribution):
810829def test_plot_recipe_add_new_data (build_recipe_one_contribution ):
811830 # Case: User wants to add data to figure generated by plot_recipe
812831 # Expected: New data is added to existing figure (check with labels)
813- recipe = build_recipe_one_contribution ()
832+ recipe = build_recipe_one_contribution
814833 optimize_recipe (recipe )
815834 plt .close ("all" )
816835 before = set (plt .get_fignums ())
@@ -854,7 +873,7 @@ def test_plot_recipe_add_new_data_two_figs(build_recipe_two_contributions):
854873def test_plot_recipe_set_title (build_recipe_one_contribution ):
855874 # Case: User sets title via plot_recipe
856875 # Expected: Title is set correctly
857- recipe = build_recipe_one_contribution ()
876+ recipe = build_recipe_one_contribution
858877 optimize_recipe (recipe )
859878 plt .close ("all" )
860879 expected_title = "Custom Recipe Title"
@@ -868,7 +887,7 @@ def test_plot_recipe_set_title(build_recipe_one_contribution):
868887def test_plot_recipe_set_defaults (build_recipe_one_contribution ):
869888 # Case: user sets default plot options with set_plot_defaults
870889 # Expected: plot_recipe uses the default options for all calls
871- recipe = build_recipe_one_contribution ()
890+ recipe = build_recipe_one_contribution
872891 optimize_recipe (recipe )
873892 plt .close ("all" )
874893 # set new defaults
@@ -896,7 +915,7 @@ def test_plot_recipe_set_defaults(build_recipe_one_contribution):
896915def test_plot_recipe_set_defaults_bad (capsys , build_recipe_one_contribution ):
897916 # Case: user tries to set kwargs that are not valid plot_recipe options
898917 # Expected: Plot is shown and warning is printed
899- recipe = build_recipe_one_contribution ()
918+ recipe = build_recipe_one_contribution
900919 optimize_recipe (recipe )
901920 plt .close ("all" )
902921 recipe .set_plot_defaults (
@@ -1006,7 +1025,7 @@ def test_plot_recipe_reset_all_defaults(build_recipe_one_contribution):
10061025 "show" : True ,
10071026 }
10081027
1009- recipe = build_recipe_one_contribution ()
1028+ recipe = build_recipe_one_contribution
10101029 optimize_recipe (recipe )
10111030 plt .close ("all" )
10121031
0 commit comments