def __init__(self): _defaults = [ Descriptor('amplitude', np.random.uniform(3.0, 4.0)), Descriptor('period', np.random.uniform(np.pi * 0.9, np.pi * 1.1)), Descriptor('x_shift', np.random.uniform(-np.pi * 0.25, np.pi * 0.25)), Descriptor('y_shift', np.random.uniform(-0.5, 0.5)) ] super(DummySin, self).__init__(self.__class__.__name__, *_defaults) self.x_data = np.linspace(0, 10, 100)
def setup_pars(): d = { 'name': 'test', 'par1': Parameter('p1', 0.1, fixed=True), 'des1': Descriptor('d1', 0.1), 'par2': Parameter('p2', 0.1), 'des2': Descriptor('d2', 0.1), 'par3': Parameter('p3', 0.1), } return d
def __init__(self, *args, linked_experiment=None, **kwargs): """ Initialisation routine called from super(). Each background has to be linked with an experiment :param args: Optional elements which are making up the background :param linked_experiment: which experiment this background should be linked to :type linked_experiment: str :param kwargs: For serialisation """ # Convert `linked_experiment` to a Descriptor if linked_experiment is None: raise AttributeError( 'Backgrounds need to be associated with an experiment. ' 'Use the `linked_experiment` key word argument.') elif isinstance(linked_experiment, str): linked_experiment = Descriptor('linked_experiment', linked_experiment) if not isinstance(linked_experiment, Descriptor): raise ValueError( 'The `linked_experiment` key word argument must be a string or Descriptor' ) # Initialise super(Background, self).__init__(*args, **kwargs) self._linked_experiment = linked_experiment
def test_descriptor_as_dict(): d = Descriptor('test', 1) result = d.as_dict() expected = { '@module': 'easyCore.Objects.Base', '@class': 'Descriptor', '@version': '0.0.1', 'name': 'test', 'value': 1, 'units': 'dimensionless', 'description': '', 'url': '', 'display_name': 'test', 'callback': None } for key in expected.keys(): if key == 'callback': continue assert result[key] == expected[key]
def from_pars(cls, x: float, y: float): """ Construct a background point from x, y floats. :param x: background x-position. :type x: float :param y: background intensity/y-position :type y: float :return: Constructed background point :rtype: BackgroundPoint """ x = Descriptor('x', x) y = Parameter('intensity', y, fixed=True) return cls(x, y)
def from_pars(cls, power: int, amp: float): """ Construct a background factor from a power and amplitude as an integer/float respectively. :param power: Power to which x will be raised. :type power: int :param amp: Amplitude for which x will be multiplied by :type amp: float :return: Constructed background factor :rtype: BackgroundFactor """ power = Descriptor('power', power) amp = Parameter('amplitude', amp, fixed=True) return cls(power, amp)
def __init__(self, x: Descriptor, y: Parameter, name: str = None): """ Construct a background point from a x-Descriptor any y-parameter. :param x: x-position of the background point :type x: Descriptor :param y: Intensity/y-position of the background point :type y: Parameter :param name: Override the default naming. :type name: str """ if name is None: name = '{:.1f}_deg'.format(x.raw_value).replace(".", ",") x._callback = property( fget=None, fset=lambda x_value: self._modify_x_label(x_value), fdel=None) super(BackgroundPoint, self).__init__(name, x=x, y=y)
def test_descriptor_repr(): d = Descriptor('test', 1) assert repr(d) == f'<{d.__class__.__name__} \'test\' = 1>' d = Descriptor('test', 1, units='cm') assert repr(d) == f'<{d.__class__.__name__} \'test\' = 1 centimeter>'
def test_Descriptor_value_get(element, expected): d = Descriptor('test', 1, units=element) assert str(d.value) == expected