[docs]classParameterWithSetpoints(Parameter):""" A parameter that has associated setpoints. The setpoints is nothing more than a list of other parameters that describe the values, names and units of the setpoint axis for this parameter. In most cases this will probably be a parameter that returns an array. It is expected that the setpoint arrays are 1D arrays such that the combined shape of the parameter e.g. if parameter is of shape (m,n) `setpoints` is a list of parameters of shape (m,) and (n,) In all other ways this is identical to :class:`Parameter`. See the documentation of :class:`Parameter` for more details. """def__init__(self,name:str,*,vals:Validator[Any]|None=None,setpoints:Sequence[ParameterBase]|None=None,snapshot_get:bool=False,snapshot_value:bool=False,**kwargs:Any,)->None:ifnotisinstance(vals,Arrays):raiseValueError(f"A ParameterWithSetpoints must have an Arrays "f"validator got {type(vals)}")ifvals.shape_unevaluatedisNone:raiseRuntimeError("A ParameterWithSetpoints must have a shape ""defined for its validator.")super().__init__(name=name,vals=vals,snapshot_get=snapshot_get,snapshot_value=snapshot_value,**kwargs,)ifsetpointsisNone:self.setpoints=[]else:self.setpoints=setpointsself._validate_on_get=True@propertydefsetpoints(self)->Sequence[ParameterBase]:""" Sequence of parameters to use as setpoints for this parameter. :getter: Returns a list of parameters currently used for setpoints. :setter: Sets the parameters to be used as setpoints from a sequence. The combined shape of the parameters supplied must be consistent with the data shape of the data returned from get on the parameter. """returnself._setpoints@setpoints.setterdefsetpoints(self,setpoints:Sequence[ParameterBase])->None:forsetpointarrayinsetpoints:ifnotisinstance(setpointarray,Parameter):raiseTypeError(f"Setpoints is of type {type(setpointarray)}"f" expected a QCoDeS parameter")self._setpoints=setpoints
[docs]defvalidate_consistent_shape(self)->None:""" Verifies that the shape of the Array Validator of the parameter is consistent with the Validator of the Setpoints. This requires that both the setpoints and the actual parameters have validators of type Arrays with a defined shape. """ifnotisinstance(self.vals,Arrays):raiseValueError(f"Can only validate shapes for parameters "f"with Arrays validator. {self.name} does "f"not have an Arrays validator.")output_shape=self.vals.shape_unevaluatedsetpoints_shape_list:list[int|Callable[[],int]|None]=[]forspinself.setpoints:ifnotisinstance(sp.vals,Arrays):raiseValueError(f"Can only validate shapes for parameters "f"with Arrays validator. {sp.name} is "f"a setpoint vector but does not have an "f"Arrays validator")ifsp.vals.shape_unevaluatedisnotNone:setpoints_shape_list.extend(sp.vals.shape_unevaluated)else:setpoints_shape_list.append(sp.vals.shape_unevaluated)setpoints_shape=tuple(setpoints_shape_list)ifoutput_shapeisNone:raiseValueError(f"Trying to validate shape but parameter "f"{self.name} does not define a shape")ifNoneinoutput_shapeorNoneinsetpoints_shape:raiseValueError(f"One or more dimensions have unknown shape "f"when comparing output: {output_shape} to "f"setpoints: {setpoints_shape}")ifoutput_shape!=setpoints_shape:raiseValueError(f"Shape of output is not consistent with "f"setpoints. Output is shape {output_shape} and "f"setpoints are shape {setpoints_shape}")LOG.debug(f"For parameter {self.full_name} verified "f"that {output_shape} matches {setpoints_shape}")
[docs]defvalidate(self,value:ParamDataType)->None:""" Overwrites the standard ``validate`` method to also check the the parameter has consistent shape with its setpoints. This only makes sense if the parameter has an Arrays validator Arguments are passed to the super method """ifisinstance(self.vals,Arrays):self.validate_consistent_shape()super().validate(value)
[docs]defexpand_setpoints_helper(parameter:ParameterWithSetpoints,results:ParamDataType|None=None)->list[tuple[ParameterBase,ParamDataType]]:""" A helper function that takes a :class:`.ParameterWithSetpoints` and acquires the parameter along with it's setpoints. The data is returned in a format prepared to insert into the dataset. Args: parameter: A :class:`.ParameterWithSetpoints` to be acquired and expanded results: The data for the given parameter. Typically the output of `parameter.get()`. If None this function will call `parameter.get` Returns: A list of tuples of parameters and values for the specified parameter and its setpoints. """ifnotisinstance(parameter,ParameterWithSetpoints):raiseTypeError(f"Expanding setpoints only works for ParameterWithSetpoints. "f"Supplied a {type(parameter)}")res=[]setpoint_params=[]setpoint_data=[]forsetpointparaminparameter.setpoints:these_setpoints=setpointparam.get()setpoint_params.append(setpointparam)setpoint_data.append(these_setpoints)output_grids=np.meshgrid(*setpoint_data,indexing="ij")forparam,gridinzip(setpoint_params,output_grids):res.append((param,grid))ifresultsisNone:data=parameter.get()else:data=resultsres.append((parameter,data))returnres