25
25
26
26
@dataclasses .dataclass (frozen = True )
27
27
class SeriesDef :
28
- """Description of curve."""
28
+ """A dataclass to describe the definition of the curve.
29
+
30
+ Attributes:
31
+ fit_func: A callable that defines the fit model of this curve. The argument names
32
+ in the callable are parsed to create the fit parameter list, which will appear
33
+ in the analysis results. The first argument should be ``x`` that represents
34
+ X-values that the experiment sweeps.
35
+ filter_kwargs: Optional. Dictionary of properties that uniquely identifies this series.
36
+ This dictionary is used for data processing.
37
+ This must be provided when the curve analysis consists of multiple series.
38
+ name: Optional. Name of this series.
39
+ plot_color: Optional. String representation of the color that is used to draw fit data
40
+ and data points in the output figure. This depends on the drawer class
41
+ being set to the curve analysis options. Usually this conforms to the
42
+ Matplotlib color names.
43
+ plot_symbol: Optional. String representation of the marker shape that is used to draw
44
+ data points in the output figure. This depends on the drawer class
45
+ being set to the curve analysis options. Usually this conforms to the
46
+ Matplotlib symbol names.
47
+ canvas: Optional. Index of sub-axis in the output figure that draws this curve.
48
+ This option is valid only when the drawer instance provides multi-axis drawing.
49
+ model_description: Optional. Arbitrary string representation of this fit model.
50
+ This string will appear in the analysis results as a part of metadata.
51
+ """
29
52
30
- # Arbitrary callback to define the fit function. First argument should be x.
31
53
fit_func : Callable
32
-
33
- # Keyword dictionary to define the series with circuit metadata
34
54
filter_kwargs : Dict [str , Any ] = dataclasses .field (default_factory = dict )
35
-
36
- # Name of this series. This name will appear in the figure and raw x-y value report.
37
55
name : str = "Series-0"
38
-
39
- # Color of this line.
40
56
plot_color : str = "black"
41
-
42
- # Symbol to represent data points of this line.
43
57
plot_symbol : str = "o"
44
-
45
- # Latex description of this fit model
46
- model_description : Optional [str ] = None
47
-
48
- # Index of canvas if the result figure is multi-panel
49
58
canvas : Optional [int ] = None
50
-
51
- # Automatically extracted signature of the fit function
52
- signature : List [str ] = dataclasses .field (init = False )
59
+ model_description : Optional [str ] = None
60
+ signature : Tuple [str , ...] = dataclasses .field (init = False )
53
61
54
62
def __post_init__ (self ):
55
63
"""Parse the fit function signature to extract the names of the variables.
56
64
57
65
Fit functions take arguments F(x, p0, p1, p2, ...) thus the first value should be excluded.
58
66
"""
59
67
signature = list (inspect .signature (self .fit_func ).parameters .keys ())
60
- fitparams = signature [1 :]
68
+ fitparams = tuple ( signature [1 :])
61
69
62
70
# Note that this dataclass is frozen
63
71
object .__setattr__ (self , "signature" , fitparams )
64
72
65
73
66
74
@dataclasses .dataclass (frozen = True )
67
75
class CurveData :
68
- """Set of extracted experiment data."""
69
-
70
- # Name of this data set
71
- label : str
76
+ """A dataclass that manages the multiple arrays comprising the dataset for fitting.
77
+
78
+ This dataset can consist of X, Y values from multiple series.
79
+ To extract curve data of the particular series, :meth:`get_subset_of` can be used.
80
+
81
+ Attributes:
82
+ x: X-values that experiment sweeps.
83
+ y: Y-values that observed and processed by the data processor.
84
+ y_err: Uncertainty of the Y-values which is created by the data processor.
85
+ Usually this assumes standard error.
86
+ shots: Number of shots used in the experiment to obtain the Y-values.
87
+ data_allocation: List with identical size with other arrays.
88
+ The value indicates the series index of the corresponding element.
89
+ This is classified based upon the matching of :attr:`SeriesDef.filter_kwargs`
90
+ with the circuit metadata of the corresponding data index.
91
+ If metadata doesn't match with any series definition, element is filled with ``-1``.
92
+ labels: List of curve labels. The list index corresponds to the series index.
93
+ """
72
94
73
- # X data
74
95
x : np .ndarray
75
-
76
- # Y data (measured data)
77
96
y : np .ndarray
78
-
79
- # Error bar
80
97
y_err : np .ndarray
81
-
82
- # Shots number
83
98
shots : np .ndarray
99
+ data_allocation : np .ndarray
100
+ labels : List [str ]
84
101
85
- # Maping of data index to series index
86
- data_index : Union [ np . ndarray , int ]
102
+ def get_subset_of ( self , index : Union [ str , int ]) -> "CurveData" :
103
+ """Filter data by series name or index.
87
104
88
- # Metadata associated with each data point. Generated from the circuit metadata.
89
- metadata : np .ndarray = None
105
+ Args:
106
+ index: Series index of name.
107
+
108
+ Returns:
109
+ A subset of data corresponding to a particular series.
110
+ """
111
+ if isinstance (index , int ):
112
+ _index = index
113
+ _name = self .labels [index ]
114
+ else :
115
+ _index = self .labels .index (index )
116
+ _name = index
117
+
118
+ locs = self .data_allocation == _index
119
+ return CurveData (
120
+ x = self .x [locs ],
121
+ y = self .y [locs ],
122
+ y_err = self .y_err [locs ],
123
+ shots = self .shots [locs ],
124
+ data_allocation = np .full (np .count_nonzero (locs ), _index ),
125
+ labels = [_name ],
126
+ )
90
127
91
128
92
129
@dataclasses .dataclass (frozen = True )
93
130
class FitData :
94
- """Set of data generated by the fit function."""
131
+ """A dataclass to store the outcome of the fitting.
132
+
133
+ Attributes:
134
+ popt: List of optimal parameter values with uncertainties if available.
135
+ popt_keys: List of parameter names being fit.
136
+ pcov: Covariance matrix from the least square fitting.
137
+ reduced_chisq: Reduced Chi-squared value for the fit curve.
138
+ dof: Degree of freedom in this fit model.
139
+ x_data: X-values provided to the fitter.
140
+ y_data: Y-values provided to the fitter.
141
+ """
95
142
96
- # Order sensitive fit parameter values
97
143
popt : List [uncertainties .UFloat ]
98
-
99
- # Order sensitive parameter name list
100
144
popt_keys : List [str ]
101
-
102
- # Covariance matrix
103
145
pcov : np .ndarray
104
-
105
- # Reduced Chi-squared value of fit curve
106
146
reduced_chisq : float
107
-
108
- # Degree of freedom
109
147
dof : int
148
+ x_data : np .ndarray
149
+ y_data : np .ndarray
110
150
111
- # X data range
112
- x_range : Tuple [float , float ]
151
+ @property
152
+ def x_range (self ) -> Tuple [float , float ]:
153
+ """Range of x values."""
154
+ return np .min (self .x_data ), np .max (self .x_data )
113
155
114
- # Y data range
115
- y_range : Tuple [float , float ]
156
+ @property
157
+ def y_range (self ) -> Tuple [float , float ]:
158
+ """Range of y values."""
159
+ return np .min (self .y_data ), np .max (self .y_data )
116
160
117
161
def fitval (self , key : str ) -> uncertainties .UFloat :
118
162
"""A helper method to get fit value object from parameter key name.
@@ -136,7 +180,13 @@ def fitval(self, key: str) -> uncertainties.UFloat:
136
180
137
181
@dataclasses .dataclass
138
182
class ParameterRepr :
139
- """Detailed description of fitting parameter."""
183
+ """Detailed description of fitting parameter.
184
+
185
+ Attributes:
186
+ name: Original name of the fit parameter being defined in the fit model.
187
+ repr: Optional. Human-readable parameter name shown in the analysis result and in the figure.
188
+ unit: Optional. Physical unit of this parameter if applicable.
189
+ """
140
190
141
191
# Fitter argument name
142
192
name : str
0 commit comments