# Repository: https://gitlab.com/quantify-os/quantify-core
# Licensed according to the LICENCE file on the main branch
# pylint: disable=all
"""Module containing the analysis abstract base class and several basic analyses."""
from __future__ import annotations
import json
import logging
import os
import warnings
from abc import ABCMeta
from copy import deepcopy
from dataclasses import dataclass
from enum import Enum
from functools import wraps
from pathlib import Path
from textwrap import wrap
from typing import TYPE_CHECKING, overload
import lmfit
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
from IPython.display import display
from matplotlib.collections import QuadMesh
from methodtools import lru_cache
from qcodes.utils import NumpyJSONEncoder
from uncertainties import ufloat
from quantify_core.data.handling import (
DATASET_NAME,
PROCESSED_DATASET_NAME,
QUANTITIES_OF_INTEREST_NAME,
create_exp_folder,
get_datadir,
get_latest_tuid,
load_dataset,
locate_experiment_container,
to_gridded_dataset,
write_dataset,
)
from quantify_core.data.types import TUID
from quantify_core.visualization import mpl_plotting as qpl
from quantify_core.visualization.SI_utilities import adjust_axeslabels_SI, set_cbarlabel
from .types import AnalysisSettings
if TYPE_CHECKING:
from matplotlib.colors import Colormap
FIGURES_LRU_CACHE_SIZE = 8
# global configurations at the level of the analysis module
settings = AnalysisSettings(
{
"mpl_dpi": 450, # define resolution of some matplotlib output formats
# svg is superior but at least OneNote does not support it
"mpl_fig_formats": ["png", "svg"],
"mpl_exclude_fig_titles": False,
"mpl_transparent_background": True,
}
)
"""
For convenience the analysis framework provides a set of global settings.
For available settings see :class:`~BaseAnalysis`.
These can be overwritten for each instance of an analysis.
Examples
--------
>>> from quantify_core.analysis import base_analysis as ba
... ba.settings["mpl_dpi"] = 300 # set resolution of matplotlib figures
"""
[docs]
class AnalysisSteps(Enum):
"""
An enumerate of the steps executed by the :class:`~BaseAnalysis` (and the default
for subclasses).
The involved steps are:
- ``AnalysisSteps.STEP_1_PROCESS_DATA`` (:meth:`BaseAnalysis.process_data`)
- ``AnalysisSteps.STEP_2_RUN_FITTING`` (:meth:`BaseAnalysis.run_fitting`)
- ``AnalysisSteps.STEP_3_ANALYZE_FIT_RESULTS`` (:meth:`BaseAnalysis.analyze_fit_results`)
- ``AnalysisSteps.STEP_4_CREATE_FIGURES`` (:meth:`BaseAnalysis.create_figures`)
- ``AnalysisSteps.STEP_5_ADJUST_FIGURES`` (:meth:`BaseAnalysis.adjust_figures`)
- ``AnalysisSteps.STEP_6_SAVE_FIGURES`` (:meth:`BaseAnalysis.save_figures`)
- ``AnalysisSteps.STEP_7_SAVE_QUANTITIES_OF_INTEREST`` (:meth:`BaseAnalysis.save_quantities_of_interest`)
- ``AnalysisSteps.STEP_8_SAVE_PROCESSED_DATASET`` (:meth:`BaseAnalysis.save_processed_dataset`)
- ``AnalysisSteps.STEP_9_SAVE_FIT_RESULTS`` (:meth:`BaseAnalysis.save_fit_results`)
A custom analysis flow (e.g. inserting new steps) can be created by implementing
an object similar to this one and overriding the
:obj:`~BaseAnalysis.analysis_steps`.
""" # noqa: E501
# Variables must start with a letter but we want them to have sorted names
# for auto-complete to indicate the execution order
STEP_1_PROCESS_DATA = "process_data"
STEP_2_RUN_FITTING = "run_fitting"
STEP_3_ANALYZE_FIT_RESULTS = "analyze_fit_results"
STEP_4_CREATE_FIGURES = "create_figures"
STEP_5_ADJUST_FIGURES = "adjust_figures"
STEP_6_SAVE_FIGURES = "save_figures"
STEP_7_SAVE_QUANTITIES_OF_INTEREST = "save_quantities_of_interest"
STEP_8_SAVE_PROCESSED_DATASET = "save_processed_dataset"
STEP_9_SAVE_FIT_RESULTS = "save_fit_results"
[docs]
class BaseAnalysis(metaclass=AnalysisMeta):
"""A template for analysis classes."""
html_header_template = (
'<h1>{name}</h1><p style="font-family: monospace">TUID: {tuid}</p>'
)
[docs]
def _repr_html_(self):
"""
An html representation of the analysis class.
Shows the name of the analysis and TUID as well as the
(.svg) figures generated by this analysis.
"""
html = self.html_header_template.format(name=self.name, tuid=self.tuid)
# If self.TUID is not None, we assume the analysis was run and
# figures have been generated.
# if self.TUID is not None, the self.analysis_dir exists/can be created.
try:
if self.tuid is not None:
html += "<div>"
figdir = os.path.join(self.analysis_dir, "figs_mpl")
files = os.listdir(figdir)
for fn in files:
if ".svg" in fn:
svg_path = os.path.join(figdir, fn)
with open(svg_path) as file:
svg_content = file.read()
html += f"<div>{svg_content}</div>"
html += "</div>"
except FileNotFoundError:
# If there are no files generated because e.g., the analysis
# never ran, we would like to avoid an error being raised.
pass
return html
[docs]
def __init__(
self,
dataset: xr.Dataset = None,
tuid: TUID | str = None,
label: str = "",
settings_overwrite: dict = None,
plot_figures: bool = True,
):
"""
Initializes the variables used in the analysis and to which data is stored.
.. warning::
We highly discourage overriding the class initialization.
If the analysis requires the user passing in any arguments, the
:meth:`~quantify_core.analysis.base_analysis.BaseAnalysis.run()` should be
overridden and extended (see its docstring for an example).
.. rubric:: Settings schema:
.. jsonschema:: schemas/AnalysisSettings.json#/configurations
Parameters
----------
dataset:
an unprocessed (raw) quantify dataset to perform the analysis on.
tuid:
if no dataset is specified, will look for the dataset with the matching tuid
in the data directory.
label:
if no dataset and no tuid is provided, will look for the most recent dataset
that contains "label" in the name.
settings_overwrite:
A dictionary containing overrides for the global
`base_analysis.settings` for this specific instance.
See `Settings schema` above for available settings.
plot_figures:
Option to create and save figures for analysis.
"""
# NB at least logging.basicConfig() needs to be called in the python kernel
# in order to see the logger messages
self.logger = logging.getLogger(self.name)
self.label = label
self.tuid = tuid
# Allows individual setting per analysis instance
# with defaults from global settings
self.settings_overwrite = deepcopy(settings)
# NB this also runs validation against the corresponding schema
self.settings_overwrite.update(settings_overwrite or {})
# Used to have access to a reference of the raw dataset, see also
# self.extract_data
self.dataset = dataset
# Initialize an empty dataset for the processed data.
# This dataset will be overwritten during the analysis.
self.dataset_processed = xr.Dataset()
# A dictionary to contain the outputs of any custom analysis
self.analysis_result = {}
# To be populated by a subclass
self.quantities_of_interest = {}
self.fit_results = {}
self.plot_figures = plot_figures
analysis_steps = AnalysisSteps
"""
Defines the steps of the analysis specified as an :class:`~enum.Enum`.
Can be overridden in a subclass in order to define a custom analysis flow.
See :class:`~quantify_core.analysis.base_analysis.AnalysisSteps` for a template.
"""
[docs]
@classmethod
def load_fit_result(cls, tuid: TUID, fit_name: str) -> lmfit.model.ModelResult:
"""
Load a saved :code:`lmfit.model.ModelResult` object from file. For analyses
that use custom fit functions, the :code:`cls.fit_function_definitions` object
must be defined in the subclass for that analysis.
Parameters
----------
tuid:
The TUID reference of the saved analysis.
fit_name:
The name of the fit result to be loaded.
Returns
-------
:
The lmfit model result object.
"""
analysis_dir = cls._get_analysis_dir(
tuid=tuid, name=cls.__name__, create_missing=False
)
if not os.path.isdir(analysis_dir):
raise FileNotFoundError(
f"Analysis not found for this experiment ({analysis_dir} not found)."
)
results_dir = cls._get_results_dir(
analysis_dir=analysis_dir, create_missing=False
)
if not os.path.isdir(results_dir):
raise FileNotFoundError(
f"No fit results found for this analysis ({results_dir} not found)."
)
result = lmfit.model.load_modelresult(
os.path.join(results_dir, f"{fit_name}.txt")
)
return result
@property
def name(self):
"""The name of the analysis, used in data saving."""
# used to store data and figures resulting from the analysis. Can be overwritten
return self.__class__.__name__
[docs]
@staticmethod
def _get_analysis_dir(tuid: TUID, name: str, create_missing: bool = True):
"""
Generate an analysis dir based on a given tuid and analysis class name.
Parameters
----------
tuid:
TUID of the analysis dir.
name:
The name of the analysis class.
create_missing:
If True, create the analysis dir if it does not already exist.
"""
exp_folder = Path(locate_experiment_container(tuid, get_datadir()))
analysis_dir = os.path.join(exp_folder, f"analysis_{name}")
if create_missing and not os.path.isdir(analysis_dir):
os.makedirs(analysis_dir)
return analysis_dir
@property
def analysis_dir(self):
"""
Analysis dir based on the tuid of the analysis class instance.
Will create a directory if it does not exist.
"""
if self.tuid is None:
raise ValueError("Unknown TUID, cannot determine the analysis directory.")
return self._get_analysis_dir(tuid=self.tuid, name=self.name)
[docs]
@staticmethod
def _get_results_dir(analysis_dir: str, create_missing: bool = True):
"""
Generate an results dir based on a given analysis dir path.
Parameters
----------
analysis_dir:
The path of the analysis directory.
create_missing:
If True, create the analysis dir if it does not already exist.
"""
results_dir = os.path.join(analysis_dir, "fit_results")
if create_missing and not os.path.isdir(results_dir):
os.makedirs(results_dir)
return results_dir
@lru_cache(maxsize=FIGURES_LRU_CACHE_SIZE)
def _analyses_figures_cache(self):
return _FiguresMplCache({}, {}, False)
@property
def results_dir(self):
"""
Analysis dirrectory for this analysis.
Will create a directory if it does not exist.
"""
return self._get_results_dir(analysis_dir=self.analysis_dir)
[docs]
def run(self) -> BaseAnalysis:
"""
Execute analysis.
This function is at the core of all analysis. It calls
:meth:`~quantify_core.analysis.base_analysis.BaseAnalysis.execute_analysis_steps`
which executes all the methods defined in the.
First step of any analysis is always extracting data, that is not configurable.
Errors in `extract_data()` are considered fatal for analysis.
Later steps are configurable by overriding
:attr:`~quantify_core.analysis.base_analysis.BaseAnalysis.analysis_steps`.
Exceptions in these steps are logged and suppressed and analysis is considered
partially successful.
This function is typically called right after instantiating an analysis class.
.. admonition:: Implementing a custom analysis that requires user input
:class: dropdown, note
When implementing your own custom analysis you might need to pass in a few
configuration arguments. That should be achieved by overriding this
function as show below.
.. code-block:: python
from quantify_core.analysis.base_analysis import BaseAnalysis
class MyAnalysis(BaseAnalysis):
def run(self, optional_argument_one: float = 3.5e9):
# Save the value to be used in some step of the analysis
self.optional_argument_one = optional_argument_one
# Execute the analysis steps
self.execute_analysis_steps()
# Return the analysis object
return self
# ... other relevant methods ...
Returns
-------
:
The instance of the analysis object so that
:meth:`~quantify_core.analysis.base_analysis.BaseAnalysis.run()`
returns an analysis object.
You can initialize, run and assign it to a variable on a
single line:, e.g. :code:`a_obj = MyAnalysis().run()`.
"""
# The following two lines must be included when when implementing a custom
# analysis that requires passing in some (optional) arguments.
self.execute_analysis_steps()
return self
[docs]
def execute_analysis_steps(self):
"""
Executes the methods corresponding to the analysis steps as defined by the
:attr:`~quantify_core.analysis.base_analysis.BaseAnalysis.analysis_steps`.
Intended to be called by `.run` when creating a custom analysis that requires
passing analysis configuration arguments to
:meth:`~quantify_core.analysis.base_analysis.BaseAnalysis.run`.
"""
self.logger.info(f"Executing `.analysis_steps` of {self.name}")
self.logger.info(f"extracting data: {self.extract_data}")
self.extract_data()
for i, method in enumerate(self.get_flow(), start=1):
self.logger.info(f"executing step {i}: {method}")
try:
method()
except Exception:
self.logger.exception(
f"Exception was raised while executing analysis step {i} "
f'("{method}"). Terminating analysis and returning partial result.'
)
return
[docs]
def get_flow(self) -> tuple:
"""
Returns a tuple with the ordered methods to be called by run analysis.
Only return the figures methods if :code:`self.plot_figures` is :code:`True`.
"""
if self.plot_figures:
return tuple(getattr(self, elm.value) for elm in self.analysis_steps)
return tuple(
getattr(self, elm.value)
for elm in self.analysis_steps
if "figures" not in elm.value
)
[docs]
def process_data(self):
"""
To be implemented by subclasses.
Should process, e.g., reshape, filter etc. the data
before starting the analysis.
"""
[docs]
def run_fitting(self):
"""
To be implemented by subclasses.
Should create fitting model(s) and fit data to the model(s) adding the result
to the :code:`.fit_results` dictionary.
"""
def _add_fit_res_to_qoi(self):
if len(self.fit_results) > 0:
self.quantities_of_interest["fit_result"] = {}
for fr_name, fit_result in self.fit_results.items():
res = flatten_lmfit_modelresult(fit_result)
self.quantities_of_interest["fit_result"][fr_name] = res
[docs]
def analyze_fit_results(self):
"""
To be implemented by subclasses.
Should analyze and process the :code:`.fit_results` and add the quantities of
interest to the :code:`.quantities_of_interest` dictionary.
"""
[docs]
def save_processed_dataset(self):
"""
Saves a copy of the processed :code:`.dataset_processed` in the analysis folder
of the experiment.
"""
if self.dataset_processed is not None:
write_dataset(
Path(self.analysis_dir) / PROCESSED_DATASET_NAME, self.dataset_processed
)
[docs]
def save_quantities_of_interest(self):
"""
Saves the :code:`.quantities_of_interest` as a JSON file in the analysis
directory.
The file is written using :func:`json.dump` with the
:class:`qcodes.utils.NumpyJSONEncoder` custom encoder.
"""
self._add_fit_res_to_qoi()
with open(
os.path.join(self.analysis_dir, QUANTITIES_OF_INTEREST_NAME),
"w",
encoding="utf-8",
) as file:
json.dump(self.quantities_of_interest, file, cls=NumpyJSONEncoder, indent=4)
[docs]
def save_fit_results(self):
"""
Saves the :code:`lmfit.model.model_result` objects for each fit in a
sub-directory within the analysis directory.
"""
for fr_name, fit_result in self.fit_results.items():
path = os.path.join(self.results_dir, f"{fr_name}.txt")
lmfit.model.save_modelresult(fit_result, path)
[docs]
def display_figs_mpl(self):
"""Displays figures in :code:`.figs_mpl` in all frontends."""
for fig in self.figs_mpl.values():
display(fig)
[docs]
def adjust_ylim(
self,
ymin: float = None,
ymax: float = None,
ax_ids: list[str] = None,
) -> None:
"""
Adjust the ylim of matplotlib figures generated by analysis object.
Parameters
----------
ymin
The bottom ylim in data coordinates. Passing :code:`None` leaves the
limit unchanged.
ymax
The top ylim in data coordinates. Passing None leaves the limit unchanged.
ax_ids
A list of ax_ids specifying what axes to adjust. Passing None results in
all axes of an analysis object being adjusted.
"""
axs = self.axs_mpl
if ax_ids is None:
ax_ids = axs.keys()
for ax_id, ax in axs.items():
if ax_id in ax_ids:
ax.set_ylim(ymin, ymax)
[docs]
def adjust_xlim(
self,
xmin: float = None,
xmax: float = None,
ax_ids: list[str] = None,
) -> None:
"""
Adjust the xlim of matplotlib figures generated by analysis object.
Parameters
----------
xmin
The bottom xlim in data coordinates. Passing :code:`None` leaves the limit
unchanged.
xmax
The top xlim in data coordinates. Passing None leaves the limit unchanged.
ax_ids
A list of ax_ids specifying what axes to adjust. Passing None results in
all axes of an analysis object being adjusted.
"""
axs = self.axs_mpl
if ax_ids is None:
ax_ids = axs.keys()
for ax_id, ax in axs.items():
if ax_id in ax_ids:
ax.set_xlim(xmin, xmax)
[docs]
def adjust_clim(
self,
vmin: float,
vmax: float,
ax_ids: list[str] = None,
) -> None:
"""
Adjust the clim of matplotlib figures generated by analysis object.
Parameters
----------
vmin
The bottom vlim in data coordinates. Passing :code:`None` leaves the limit
unchanged.
vmax
The top vlim in data coordinates. Passing None leaves the limit unchanged.
ax_ids
A list of ax_ids specifying what axes to adjust. Passing None results in
all axes of an analysis object being adjusted.
"""
axs = self.axs_mpl
if ax_ids is None:
ax_ids = axs.keys()
for ax_id, ax in axs.items():
if ax_id in ax_ids:
# For plots created with `imshow` or `pcolormesh`
for image_or_collection in (
*ax.get_images(),
*(c for c in ax.collections if isinstance(c, QuadMesh)),
):
image_or_collection.set_clim(vmin, vmax)
[docs]
def adjust_cmap(self, cmap: Colormap | str | None, ax_ids: list[str] = None):
"""
Adjust the cmap of matplotlib figures generated by analysis object.
Parameters
----------
cmap
The colormap to set for the axis
ax_ids
A list of ax_ids specifying what axes to adjust. Passing None results in
all axes of an analysis object being adjusted.
"""
axs = self.axs_mpl
if ax_ids is None:
ax_ids = axs.keys()
for ax_id, ax in axs.items():
if ax_id in ax_ids:
# For plots created with `imshow` or `pcolormesh`
for image_or_collection in (
*ax.get_images(),
*(c for c in ax.collections if isinstance(c, QuadMesh)),
):
image_or_collection.set_cmap(cmap)
[docs]
class BasicAnalysis(BaseAnalysis):
"""
A basic analysis that extracts the data from the latest file matching the label
and plots and stores the data in the experiment container.
"""
[docs]
class Basic1DAnalysis(BasicAnalysis):
"""
Deprecated. Alias of :class:`~quantify_core.analysis.base_analysis.BasicAnalysis`
for backwards compatibility.
"""
[docs]
def run(self) -> BaseAnalysis: # noqa: D102
warnings.warn("Use `BasicAnalysis`", category=FutureWarning)
return super().run()
[docs]
class Basic2DAnalysis(BaseAnalysis):
"""
A basic analysis that extracts the data from the latest file matching the label
and plots and stores the data in the experiment container.
"""
[docs]
def flatten_lmfit_modelresult(model):
"""
Flatten an lmfit model result to a dictionary in order to be able to save
it to disk.
Notes
-----
We use this method as opposed to :func:`~lmfit.model.save_modelresult` as the
corresponding :func:`~lmfit.model.load_modelresult` cannot handle loading data with
a custom fit function.
"""
assert isinstance(model, (lmfit.model.ModelResult, lmfit.minimizer.MinimizerResult))
dic = {}
dic["success"] = model.success
dic["message"] = model.message
dic["params"] = {}
for param_name in model.params:
dic["params"][param_name] = {}
param = model.params[param_name]
for k in param.__dict__:
if not k.startswith("_") and k not in ["from_internal"]:
dic["params"][param_name][k] = getattr(param, k)
dic["params"][param_name]["value"] = getattr(param, "value")
return dic
[docs]
def lmfit_par_to_ufloat(param: lmfit.parameter.Parameter):
"""
Safe conversion of an :class:`lmfit.parameter.Parameter` to
:code:`uncertainties.ufloat(value, std_dev)`.
This function is intended to be used in custom analyses to avoid errors when an
`lmfit` fails and the `stderr` is :code:`None`.
Parameters
----------
param:
The :class:`~lmfit.parameter.Parameter` to be converted
Returns
-------
:class:`!uncertainties.UFloat` :
An object representing the value and the uncertainty of the parameter.
"""
value = param.value
stderr = np.nan if param.stderr is None else param.stderr
return ufloat(value, stderr)
[docs]
def check_lmfit(fit_res: lmfit.model.ModelResult) -> str:
"""
Check that `lmfit` was able to successfully return a valid fit, and give
a warning if not.
The function looks at `lmfit`'s success parameter, and also checks whether
the fit was able to obtain valid error bars on the fitted parameters.
Parameters
----------
fit_res:
The :class:`~lmfit.model.ModelResult` object output by `lmfit`
Returns
-------
:
A warning message if there is a problem with the fit.
"""
if not fit_res.success:
fit_warning = "fit failed. lmfit was not able to fit the data."
warnings.warn(fit_warning)
return "Warning: " + fit_warning
errorbars_failed = not fit_res.errorbars
if errorbars_failed:
fit_warning = (
"lmfit could not find a good fit. Fitted parameters may not be accurate."
)
warnings.warn(fit_warning)
return "Warning: " + fit_warning
return None
@overload
def wrap_text(
text: str, width: int = 35, replace_whitespace: bool = True, **kwargs
) -> str: ...
@overload
def wrap_text(
text: None, width: int = 35, replace_whitespace: bool = True, **kwargs
) -> None: ...
[docs]
def wrap_text(
text: str | None, width: int = 35, replace_whitespace: bool = True, **kwargs
) -> str | None:
"""
A text wrapping (braking over multiple lines) utility.
Intended to be used with
:func:`~quantify_core.visualization.mpl_plotting.plot_textbox` in order to avoid
too wide figure when, e.g.,
:func:`~quantify_core.analysis.base_analysis.check_lmfit` fails and
a warning message is generated.
For usage see, for example, source code of
:meth:`~quantify_core.analysis.single_qubit_timedomain.T1Analysis.create_figures`.
Parameters
----------
text
The text string to be wrapped over several lines.
width
Maximum line width in characters.
replace_whitespace
Passed to :func:`textwrap.wrap` and documented
`here <https://docs.python.org/3/library/textwrap.html#textwrap.TextWrapper.replace_whitespace>`_.
kwargs
Any other keyword arguments to be passed to :func:`textwrap.wrap`.
Returns
-------
:
The wrapped text (or :code:`None` if text is :code:`None`).
"""
if text is not None:
# make sure existing line breaks are preserved
text_lines = text.split("\n")
wrapped_text = "\n".join(
"\n".join(
wrap(line, width=width, replace_whitespace=replace_whitespace, **kwargs)
)
for line in text_lines
)
return wrapped_text
[docs]
def analysis_steps_to_str(
analysis_steps: Enum, class_name: str = BaseAnalysis.__name__
) -> str:
"""
A utility for generating the docstring for the analysis steps.
Parameters
----------
analysis_steps:
An :class:`~enum.Enum` similar to
:class:`quantify_core.analysis.base_analysis.AnalysisSteps`.
class_name:
The class name that has the `analysis_steps` methods and for which the
`analysis_steps` are intended.
Returns
-------
:
A formatted string version of the `analysis_steps` and corresponding methods.
"""
col0 = tuple(element.name for element in analysis_steps)
col1 = tuple(element.value for element in analysis_steps)
header_r = "# <STEP>"
header_l = "<corresponding class method>"
sep = " # "
col0_len = max(map(len, col0 + (header_r,)))
col0_len += len(analysis_steps.__name__) + 1
string = f"{header_r:<{col0_len}}{sep}{header_l}\n\n"
string += "\n".join( # NB the `+ '.' +` is not redundant
f"{analysis_steps.__name__ + '.' + name:<{col0_len}}{sep}{class_name}.{value}"
for name, value in zip(col0, col1)
)
return string