def test_link(self): p1 = Parameter('p1', value=1) p2 = Parameter('p2') lp = LinkedParameter(p1, p2) assert lp.parameters == (p1, p2) assert lp.name == p1.name assert lp.get() == 1 lp.set(2) assert (p1.get() == 2) and (p2.get() == 2)
def test_binary_operators_readable(self): ''' test operators (with constant arguments) normal operators: p' = p operator x reverse operators: p' = x operator p when getting p is given and we solve for p', when setting p' is given and we solve for p ''' p = Parameter('p', value=3.) # forward == easy assert (p + 2.).get() == 3. + 2. assert (2. + p).get() == 2. + 3. assert (p - 2.).get() == 3. - 2. assert (2. - p).get() == 2. - 3. assert (p * 2.).get() == 3. * 2. assert (2. * p).get() == 2. * 3. assert (p / 2.).get() == 3. / 2. assert (2. / p).get() == 2. / 3. assert (p // 2.).get() == 3. // 2. assert (2. // p).get() == 2. // 3. # backward == wicked (p + 2.).set(3.) assert p.get() == 3. - 2. (2. + p).set(3.) assert p.get() == 3. - 2. (p - 2.).set(3.) assert p.get() == 2. + 3. (2. - p).set(3.) assert p.get() == 2. - 3. (p * 2.).set(3.) assert p.get() == 3. / 2. (2. * p).set(3.) assert p.get() == 3. / 2. (p / 2.).set(3.) assert p.get() == 3. * 2. (2. / p).set(3.) assert p.get() == 2. / 3. (p // 2.).set(3.) assert p.get() == 3. * 2. (2. // p).set(3.) assert p.get() == 2. / 3. with raises(ZeroDivisionError): (p * 0.).set(3.) with raises(ZeroDivisionError): (0. * p).set(3.) with raises(ZeroDivisionError): (p / 0.).get()
def test_popt_out(self, method): px = Parameter('x') popt_out = {px: 'c0'} # pass as arg to __init__ or assign to property if method == 'arg': measurement = self.klass(popt_out=popt_out, **self.kwargs()) elif method == 'property': measurement = self.klass(**self.kwargs()) measurement.popt_out = popt_out # run measurement() assert np.isclose(px.get(), 2.)
class CountingMeasurement(Measurement): def __init__(self, raises=None, raise_count=0, **kwargs): ''' A measurement that returns incrementing integers. Parameters: raises (Exception, optional) - Exception to raise when the counter reaches raise_count. raise_count (int, default 0) - Counter value that triggers the Exception. ''' super(CountingMeasurement, self).__init__(**kwargs) self.counter = Parameter('count', value=-1) self.prepare_count = 0 self.setup_count = 0 self.measure_count = 0 self.teardown_count = 0 self.values.append(self.counter) self.raises = raises self.raise_count = raise_count def _setup(self): self.setup_count += 1 def _prepare(self): self.prepare_count += 1 def _measure(self, **kwargs): self.measure_count += 1 self.counter.set(self.counter.get() + 1) if (self.raises is not None) and (self.counter.get() == self.raise_count): raise self.raises() frame = pd.DataFrame.from_dict( {self.counter.name: [self.counter.get()]}) self.store.append(frame) return frame def _teardown(self): self.teardown_count += 1
def test_get_func(self): p = Parameter('test', get_func=lambda: -1) assert p.get() == -1
def test_init_value(self): p = Parameter('test', value=-1) assert p.get() == -1