Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coruscating committed Sep 6, 2023
1 parent 5fc1017 commit b8defd2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
6 changes: 3 additions & 3 deletions qiskit_experiments/curve_analysis/composite_curve_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ def _run_analysis(

# After the quality is determined, plot can become a boolean flag for whether
# to generate the figure
plot = plot == "always" or (plot == "selective" and quality == "bad")
plot_bool = plot == "always" or (plot == "selective" and quality == "bad")

if plot:
if plot_bool:
if analysis.options.plot_raw_data:
for model in analysis.models:
sub_data = processed_data.get_subset_of(model._name)
Expand Down Expand Up @@ -359,7 +359,7 @@ def _run_analysis(
)

# Draw fit result
if plot:
if plot_bool:
x_interp = np.linspace(
np.min(formatted_data.x), np.max(formatted_data.x), num=100
)
Expand Down
6 changes: 3 additions & 3 deletions qiskit_experiments/curve_analysis/curve_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ def _run_analysis(

# After the quality is determined, plot can become a boolean flag for whether
# to generate the figure
plot = plot == "always" or (plot == "selective" and quality == "bad")
plot_bool = plot == "always" or (plot == "selective" and quality == "bad")

if plot:
if plot_bool:
self.plotter.set_supplementary_data(fit_red_chi=fit_data.reduced_chisq)
for model in self._models:
if self.options.plot_raw_data:
Expand Down Expand Up @@ -464,7 +464,7 @@ def _run_analysis(
self.plotter.set_supplementary_data(primary_results=primary_results)

# Draw fit curves and report
if plot:
if plot_bool:
for model in self._models:
sub_data = formatted_data.get_subset_of(model._name)
if sub_data.x.size == 0:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
features:
- |
The ``generate_figures`` parameter has been added to composite experiments to control figure
The ``generate_figures`` parameter has been added to :class:`.CompositeExperiment` to control figure
generation. By default, ``generate_figures`` is ``always``, meaning figures will always be generated.
If ``generate_figures`` is set to ``selective``, then only figures for analysis results of bad
quality will be generated. If ``generate_figures`` is set to ``never``, then figures will never be
generated. This behavior can be overridden for individual analyses by setting the analysis option
``plot``. In the next release, the default behavior of ``generate_figures`` will change to ``selective``.
``plot`` for :class:`.CurveAnalysis`.
fixes:
- |
Fixed a bug where setting the analysis option ``plot_raw_data`` to ``True`` still caused formatted data
Expand Down
43 changes: 42 additions & 1 deletion test/curve_analysis/test_baseclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from qiskit_experiments.data_processing import DataProcessor, Probability
from qiskit_experiments.exceptions import AnalysisError
from qiskit_experiments.framework import ExperimentData, AnalysisResultData, CompositeAnalysis
from qiskit_experiments.database_service.exceptions import ExperimentEntryNotFound


class CurveAnalysisTestCase(QiskitExperimentsTestCase):
Expand Down Expand Up @@ -405,7 +406,7 @@ def _initialize(self, experiment_data):
@unpack
def test_end_to_end_parallel_analysis(self, plot_flag, figure_flag, n_figures):
"""Integration test for running two curve analyses in parallel, including
selective figure generation."""
selective figure generation."""

analysis1 = CurveAnalysis(models=[ExpressionModel(expr="amp * exp(-x/tau)", name="test")])
analysis1.set_options(
Expand Down Expand Up @@ -450,6 +451,46 @@ def test_end_to_end_parallel_analysis(self, plot_flag, figure_flag, n_figures):

self.assertEqual(len(result._figures), n_figures)

def test_selective_figure_generation(self):
"""Test that selective figure generation based on quality works as expected."""

# analysis with intentionally bad fit
analysis1 = CurveAnalysis(models=[ExpressionModel(expr="amp * exp(-x)", name="test")])
analysis1.set_options(
data_processor=DataProcessor(input_key="counts", data_actions=[Probability("1")]),
p0={"amp": 0.7},
result_parameters=["amp"],
)
analysis2 = CurveAnalysis(models=[ExpressionModel(expr="amp * exp(-x/tau)", name="test")])
analysis2.set_options(
data_processor=DataProcessor(input_key="counts", data_actions=[Probability("1")]),
p0={"amp": 0.7, "tau": 0.5},
result_parameters=["amp", "tau"],
)
composite = CompositeAnalysis(
[analysis1, analysis2], flatten_results=True, generate_figures="selective"
)
amp1 = 0.7
tau1 = 0.5
amp2 = 0.7
tau2 = 0.5

x = np.linspace(0, 1, 100)
y1 = amp1 * np.exp(-x / tau1)
y2 = amp2 * np.exp(-x / tau2)

test_data = self.parallel_sampler(x, y1, y2)
result = composite.run(test_data)
self.assertExperimentDone(result)

for i, res in enumerate(result.analysis_results()):
# only generate a figure if the quality is bad
if res.quality == "bad":
result.figure(i)
else:
with self.assertRaises(ExperimentEntryNotFound):
result.figure(i)

def test_end_to_end_zero_yerr(self):
"""Integration test for an edge case of having zero y error.
Expand Down

0 comments on commit b8defd2

Please sign in to comment.