Skip to content

Commit

Permalink
Update analysis classes to use new plotter and drawer visualization m…
Browse files Browse the repository at this point in the history
…odules
  • Loading branch information
conradhaupt committed Sep 15, 2022
1 parent b45264f commit 6bb2142
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 41 deletions.
2 changes: 1 addition & 1 deletion qiskit_experiments/curve_analysis/base_curve_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from qiskit_experiments.data_processing import DataProcessor
from qiskit_experiments.data_processing.processor_library import get_processor
from qiskit_experiments.framework import BaseAnalysis, AnalysisResultData, Options, ExperimentData
from qiskit_experiments.visualization import MplDrawer, BaseDrawer, BasePlotter, CurvePlotter
from qiskit_experiments.visualization import MplDrawer, BasePlotter, CurvePlotter
from .curve_data import CurveData, ParameterRepr, CurveFitResult

PARAMS_ENTRY_PREFIX = "@Parameters_"
Expand Down
12 changes: 3 additions & 9 deletions qiskit_experiments/curve_analysis/composite_curve_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from uncertainties import unumpy as unp, UFloat

from qiskit_experiments.framework import BaseAnalysis, ExperimentData, AnalysisResultData, Options
from qiskit_experiments.visualization import MplDrawer,CurvePlotter,BasePlotter
from qiskit_experiments.visualization import MplDrawer, CurvePlotter, BasePlotter
from .base_curve_analysis import BaseCurveAnalysis, PARAMS_ENTRY_PREFIX
from .curve_data import CurveFitResult
from .utils import analysis_result_to_repr, eval_with_uncertainties
Expand Down Expand Up @@ -232,10 +232,6 @@ def _run_analysis(

analysis_results = []

# Initialize canvas
# if self.options.plot:
# self.drawer.initialize_canvas()

fit_dataset = {}
for analysis in self._analyses:
analysis._initialize(experiment_data)
Expand Down Expand Up @@ -319,7 +315,7 @@ def _run_analysis(
# Draw confidence intervals with different n_sigma
sigmas = unp.std_devs(y_data_with_uncertainty)
if np.isfinite(sigmas).all():
self.plotter.set_series_data(model._name,sigmas=sigmas)
self.plotter.set_series_data(model._name, sigmas=sigmas)

# Add raw data points
if self.options.return_data_points:
Expand Down Expand Up @@ -348,10 +344,8 @@ def _run_analysis(
for group, fit_data in fit_dataset.items():
chisqs.append(r"reduced-$\chi^2$ = " + f"{fit_data.reduced_chisq: .4g} ({group})")
report += "\n".join(chisqs)
self.plotter.set_figure_data(fit_report=report)
self.plotter.set_figure_data(report_text=report)

# Finalize canvas
# self.drawer.format_canvas()
return analysis_results, [self.plotter.figure()]

return analysis_results, []
18 changes: 8 additions & 10 deletions qiskit_experiments/curve_analysis/curve_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(
)
# pylint: disable=no-member
models = []
plot_options = {}
series_params = {}
for series_def in self.__series__:
models.append(
lmfit.Model(
Expand All @@ -149,12 +149,12 @@ def __init__(
data_sort_key=series_def.filter_kwargs,
)
)
plot_options[series_def.name] = {
series_params[series_def.name] = {
"color": series_def.plot_color,
"symbol": series_def.plot_symbol,
"canvas": series_def.canvas,
}
self.plotter.set_options(plot_options=plot_options)
self.plotter.set_plot_options(series_params=series_params)

self._models = models or []
self._name = name or self.__class__.__name__
Expand Down Expand Up @@ -467,10 +467,6 @@ def _run_analysis(
self._initialize(experiment_data)
analysis_results = []

# # Initialize canvas
# if self.options.plot:
# self.drawer.initialize_canvas()

# Run data processing
processed_data = self._run_data_processing(
raw_data=experiment_data.data(),
Expand Down Expand Up @@ -564,15 +560,18 @@ def _run_analysis(
# Draw confidence intervals with different n_sigma
sigmas = unp.std_devs(y_data_with_uncertainty)
if np.isfinite(sigmas).all():
self.plotter.set_series_data(model._name,sigmas=sigmas,)
self.plotter.set_series_data(
model._name,
sigmas=sigmas,
)

# Write fitting report
report_description = ""
for res in analysis_results:
if isinstance(res.value, (float, UFloat)):
report_description += f"{analysis_result_to_repr(res)}\n"
report_description += r"reduced-$\chi^2$ = " + f"{fit_data.reduced_chisq: .4g}"
self.plotter.set_figure_data(fit_report=report_description)
self.plotter.set_figure_data(report_text=report_description)

# Add raw data points
if self.options.return_data_points:
Expand All @@ -582,7 +581,6 @@ def _run_analysis(

# Finalize plot
if self.options.plot:
# self.drawer.format_canvas()
return analysis_results, [self.plotter.figure()]

return analysis_results, []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _default_options(cls):
input_key="counts",
data_actions=[dp.Probability("1"), dp.BasisExpectationValue()],
)
default_options.plotter.drawer.set_options(
default_options.plotter.set_plot_options(
xlabel="Flat top width",
ylabel="Pauli expectation values",
xval_unit="s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _default_options(cls):
considered as good. Defaults to :math:`\pi/2`.
"""
default_options = super()._default_options()
default_options.plotter.drawer.set_options(
default_options.plotter.set_plot_options(
xlabel="Number of gates (n)",
ylabel="Population",
ylim=(0, 1.0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(
@classmethod
def _default_options(cls) -> Options:
options = super()._default_options()
options.plotter.drawer.set_options(
options.plotter.set_plot_options(
xlabel="Frequency",
ylabel="Signal (arb. units)",
xval_unit="Hz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(
@classmethod
def _default_options(cls) -> Options:
options = super()._default_options()
options.plotter.drawer.set_options(
options.plotter.set_plot_options(
xlabel="Frequency",
ylabel="Signal (arb. units)",
xval_unit="Hz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import qiskit_experiments.curve_analysis as curve
from qiskit_experiments.framework import AnalysisResultData
from qiskit_experiments.visualization import PlotStyle


class CrossResonanceHamiltonianAnalysis(curve.CompositeCurveAnalysis):
Expand Down Expand Up @@ -60,20 +61,24 @@ def __init__(self):
def _default_options(cls):
"""Return the default analysis options."""
default_options = super()._default_options()
default_options.plotter.drawer.set_options(
default_options.plotter.set_options(
subplots=(3, 1),
style=PlotStyle(
figsize=(8, 10),
legend_loc="lower right",
report_rpos=(0.28, -0.10),
),
)
default_options.plotter.set_plot_options(
xlabel="Flat top width",
ylabel=[
r"$\langle$X(t)$\rangle$",
r"$\langle$Y(t)$\rangle$",
r"$\langle$Z(t)$\rangle$",
],
xval_unit="s",
figsize=(8, 10),
legend_loc="lower right",
fit_report_rpos=(0.28, -0.10),
ylim=(-1, 1),
plot_options={
series_params={
"x_ctrl0": {"color": "blue", "symbol": "o", "canvas": 0},
"y_ctrl0": {"color": "blue", "symbol": "o", "canvas": 1},
"z_ctrl0": {"color": "blue", "symbol": "o", "canvas": 2},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _default_options(cls):
descriptions of analysis options.
"""
default_options = super()._default_options()
default_options.plotter.drawer.set_options(
default_options.plotter.set_plot_options(
xlabel="Beta",
ylabel="Signal (arb. units)",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _default_options(cls):
descriptions of analysis options.
"""
default_options = super()._default_options()
default_options.plotter.drawer.set_options(
default_options.plotter.set_plot_options(
xlabel="Delay",
ylabel="Signal (arb. units)",
xval_unit="s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def _run_analysis(
if self.options.plot_iq_data:
axis = get_non_gui_ax()
figure = axis.get_figure()
figure.set_size_inches(*self.plotter.drawer.options.figsize)
# TODO: Move plotting to a new IQPlotter class.
figure.set_size_inches(*self.plotter.drawer.style.figsize)

iqs = []

Expand All @@ -68,12 +69,12 @@ def _run_analysis(
iqs = np.vstack(iqs)
axis.scatter(iqs[:, 0], iqs[:, 1], color="b")
axis.set_xlabel(
"In phase [arb. units]", fontsize=self.plotter.drawer.options.axis_label_size
"In phase [arb. units]", fontsize=self.plotter.drawer.style.axis_label_size
)
axis.set_ylabel(
"Quadrature [arb. units]", fontsize=self.plotter.drawer.options.axis_label_size
"Quadrature [arb. units]", fontsize=self.plotter.drawer.style.axis_label_size
)
axis.tick_params(labelsize=self.plotter.drawer.options.tick_label_size)
axis.tick_params(labelsize=self.plotter.drawer.style.tick_label_size)
axis.grid(True)

figures.append(figure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class T1Analysis(curve.DecayAnalysis):
def _default_options(cls) -> Options:
"""Default analysis options."""
options = super()._default_options()
options.plotter.drawer.set_options(
options.plotter.set_plot_options(
xlabel="Delay",
ylabel="P(1)",
xval_unit="s",
Expand Down Expand Up @@ -85,7 +85,7 @@ class T1KerneledAnalysis(curve.DecayAnalysis):
def _default_options(cls) -> Options:
"""Default analysis options."""
options = super()._default_options()
options.plotter.drawer.set_options(
options.plotter.set_plot_options(
xlabel="Delay",
ylabel="Normalized Projection on the Main Axis",
xval_unit="s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class T2HahnAnalysis(curve.DecayAnalysis):
def _default_options(cls) -> Options:
"""Default analysis options."""
options = super()._default_options()
options.plotter.drawer.set_options(
options.plotter.set_plot_options(
xlabel="Delay",
ylabel="P(0)",
xval_unit="s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class T2RamseyAnalysis(curve.DampedOscillationAnalysis):
def _default_options(cls) -> Options:
"""Default analysis options."""
options = super()._default_options()
options.plotter.drawer.set_options(
options.plotter.set_plot_options(
xlabel="Delay",
ylabel="P(1)",
xval_unit="s",
Expand Down
2 changes: 1 addition & 1 deletion qiskit_experiments/library/characterization/rabi.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(
result_parameters=[ParameterRepr("freq", self.__outcome__)],
normalization=True,
)
self.analysis.drawer.set_options(
self.analysis.plotter.set_plot_options(
xlabel="Amplitude",
ylabel="Signal (arb. units)",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _default_options(cls):
2Q RB is corrected to exclude the depolarization of underlying 1Q channels.
"""
default_options = super()._default_options()
default_options.plotter.drawer.set_options(
default_options.plotter.set_plot_options(
xlabel="Clifford Length",
ylabel="P(0)",
)
Expand Down

0 comments on commit 6bb2142

Please sign in to comment.