コード例 #1
0
def testParameterNumericalDisplayValues():
    dv = {1: 'one', 2: 'two', 3: 'three'}
    p = Parameter("testpar", 1, valueRange = (1, 5),
                     displayValues = dv)()
    for key, value in dv.items():
        assert key in p.displayValues()
        assert p.displayValues(key) == value
コード例 #2
0
def testParameterBaseCopy():
    p1 = Parameter(name = "p", value = "a", displayName = "displayname",
                   onValueUpdate = Dummy().dummyFunc)()
    p2 = p1.copy()
    assert isinstance(p1, ParameterBase)
    assert isinstance(p2, ParameterBase)
    assert p1 == p2
    p1.setValue("b")
    assert p1.value() != p2.value()
    p1.setDisplayName("q")
    assert p1.displayName() != p2.displayName()
コード例 #3
0
def testParameterNumerical():
    ptype = Parameter("testpar", 3, valueRange = (1, 5),
                         suffix = "mm", stepping = 1)
    p = ptype()
    assert p.value() == 3
    assert p.valueRange() == (1, 5)
    assert p.suffix() == "mm"
    assert p.stepping() == 1
    assert p.displayValues() is None
    p.setValue(4)
    assert p.value() == 4
    assert ptype.value() == 3
    p.setValueRange((2,3))
    assert ptype.valueRange() == (1, 5)
    assert p.valueRange() == (2, 3)
コード例 #4
0
class DummyAlgo(AlgorithmBase):
    shortName = "Dummy"
    parameters = (TestPar, Parameter(name="test", value=3.4,
                                     valueRange=(1, 5)))

    def __init__(self):
        super(DummyAlgo, self).__init__()
        self.test.setOnValueUpdate(self.dummy)

    def dummy(self, v):
        pass
コード例 #5
0
def testParameterFloatCopy():
    p1 = Parameter(name = "p", value = 1.0, displayName = "displayname",
                      valueRange = (1, 5), suffix = "suf", stepping = 1,
                      displayValues = {}, generator = NumberGenerator,
                      decimals = 2
                      )()
    p2 = p1.copy()
    assert isinstance(p1, ParameterBase)
    assert isinstance(p2, ParameterBase)
    assert p1 == p2
    p1.setDecimals(4)
    assert p1.decimals() != p2.decimals()
コード例 #6
0
ファイル: dataconfig.py プロジェクト: tangzheng1104/test
class DataConfig(AlgorithmBase, CallbackRegistry):
    _is2d = False
    _sampleName = None
    _x0seen, _x1seen = None, None  # remembers data sets seen
    parameters = (
        Parameter("x0Low",
                  0.,
                  unit=NoUnit(),
                  displayName="lower {x0} cut-off",
                  valueRange=(0., numpy.inf),
                  decimals=10),
        Parameter("x0High",
                  numpy.inf,
                  unit=NoUnit(),
                  displayName="upper {x0} cut-off",
                  valueRange=(0., numpy.inf),
                  decimals=10),
        Parameter("x1Low",
                  0.,
                  unit=NoUnit(),
                  displayName="lower {x1} cut-off",
                  valueRange=(0., numpy.inf),
                  decimals=10),
        Parameter("x1High",
                  numpy.inf,
                  unit=NoUnit(),
                  displayName="upper {x1} cut-off",
                  valueRange=(0., numpy.inf),
                  decimals=10),
        Parameter(
            "fMaskZero",
            False,
            unit=NoUnit(),
            displayName="Mask {f} values of 0",
            description=
            "Renders intensity values that are zero invalid for fitting"),
        Parameter(
            "fMaskNeg",
            False,
            unit=NoUnit(),
            displayName="Mask negative {f} values",
            description="Renders negative intensity values invalid for fitting"
        ),
        Parameter("fuMin",
                  Fraction(u"%").toSi(1.),
                  unit=Fraction(u"%"),
                  displayName="minimum uncertainty estimate",
                  valueRange=(0., 1.),
                  decimals=9),
        Parameter(
            "nBin",
            100,
            unit=NoUnit(),
            displayName="target number of bins \n (0 = no re-binning)",
            description=
            "Sets the number of bins to rebin the data into. \n May be smaller than target value.",
            valueRange=(0., 1000)),
    )

    @property
    def showParams(self):
        lst = super(DataConfig, self).showParams
        if not self.is2d:  # put psi settings right behind q settings
            lst.remove("x1Low")
            lst.remove("x1High")
        return lst

    @property
    def callbackSlots(self):
        return set(("x0limits", "x1limits", "fMasks", "fuMin"))

    def updateFuMin(self):
        self.callback("fuMin", self.fuMin())

    @property
    def is2d(self):
        return self._is2d

    @is2d.setter
    def is2d(self, isit):
        self._is2d = isit

    @property
    def sampleName(self):
        return self._sampleName

    @sampleName.setter
    def sampleName(self, newName):
        self._sampleName = newName

    def __init__(self):
        super(DataConfig, self).__init__()
        self.x0Low.setOnValueUpdate(self.updateX0Limits)
        self.x0High.setOnValueUpdate(self.updateX0Limits)
        self.x1Low.setOnValueUpdate(self.updateX1Limits)
        self.x1High.setOnValueUpdate(self.updateX1Limits)
        self.fMaskZero.setOnValueUpdate(self.updateFMasks)
        self.fMaskNeg.setOnValueUpdate(self.updateFMasks)
        self.fuMin.setOnValueUpdate(self.updateFuMin)

    def updateX0Limits(self):
        self._onLimitUpdate("x0limits", self.x0Low, self.x0High)

    def updateX1Limits(self):
        self._onLimitUpdate("x1limits", self.x1Low, self.x1High)

    def _onLimitUpdate(self, callbackName, pLow, pHigh):
        if not pLow() <= pHigh():
            temp = pLow()
            pLow.setValue(pHigh())
            pHigh.setValue(temp)
        self.callback(callbackName, (pLow(), pHigh()))

    def updateFMasks(self):
        self.callback("fMasks", (self.fMaskZero(), self.fMaskNeg()))

    def updateX0Unit(self, newUnit):
        """Sets the unit of the x0 vector."""
        # No callback this time because it is updated top-down from the
        # DataVectors in DataObj. The unit is not expected the be updated
        # in the Parameter only and has to bubble upwards to the DataVector
        # (atm!).
        self.x0Low.setUnit(newUnit)
        self.x0High.setUnit(newUnit)

    def updateX1Unit(self, newUnit):
        self.x1Low.setUnit(newUnit)
        self.x1High.setUnit(newUnit)

    def onUpdatedX0(self, x0):
        """Sets available range of loaded data."""
        if self._x0seen is None:
            # on the first data, set the param limits to the exact value range
            limits = (x0.min(), x0.max())
            self._x0seen = id(x0)  # just store something for now
        else:  # there were other data sets already, the value range grows
            # alternatives: (1) shrinking means another range for broader
            # datasets can not be selected in the UI;
            limits = self.x0Low.valueRange()
            limits = min(x0.min(), limits[0]), max(x0.max(), limits[1])
        self.x0Low.setValueRange(limits)
        self.x0High.setValueRange(limits)

    def onUpdatedX1(self, x1):
        pass

    def hdfWrite(self, hdf):
        super(DataConfig, self).hdfWrite(hdf)
        hdf.writeMembers(self, 'sampleName', 'is2d')
コード例 #7
0
 def testDecimals(newDecimals):
     p = Parameter("testpar", 1.0, valueRange = (1, 5),
                      decimals = newDecimals)
コード例 #8
0
 def testStepping(stepping):
     p = Parameter("testpar", 1, valueRange = (1, 5),
                      stepping = "bla")
コード例 #9
0
 def testSuffix(newSuffix):
     p = Parameter("testpar", 1, valueRange = (1, 5),
                                 suffix = newSuffix)
コード例 #10
0
 def testValueRange(newRange):
     p = Parameter("testpar", 1, valueRange = newRange)
コード例 #11
0
def testParameterDefaultValue2():
    p = Parameter("testpar", None)
コード例 #12
0
def testParameterDefaultValue1():
    p = Parameter("testpar")
コード例 #13
0
 def testName(newName):
     p = Parameter(name, 0)
コード例 #14
0
 def compareParameters(pType, kwargs):
     p1 = Parameter(**kwargs)()
     p2 = Parameter(**kwargs)()
     assert isinstance(p1, pType)
     assert p1 == p2
コード例 #15
0
def testParameterNumericalCopy():
    p1 = Parameter(name = "p", value = 1, displayName = "displayname",
                      valueRange = (1, 5), suffix = "suf", stepping = 1,
                      displayValues = {}, generator = NumberGenerator
                      )()
    p2 = p1.copy()
    assert isinstance(p1, ParameterNumerical)
    assert isinstance(p2, ParameterNumerical)
    assert p1 == p2
    p1.setValueRange((2, 3))
    assert p1.valueRange() != p2.valueRange()
    p1.setSuffix("suv")
    assert p1.suffix() != p2.suffix()
    p1.setStepping(2)
    assert p1.stepping() != p2.stepping()
    p1.setDisplayValues({1: "suv"})
    assert p1.displayValues() != p2.displayValues()
    p1.setGenerator(RandomUniform)
    assert p1.generator() != p2.generator()
コード例 #16
0
# -*- coding: utf-8 -*-
# bases/algorithm/algorithmbase_test.py

from __future__ import absolute_import  # PEP328
from builtins import zip
from nose.tools import raises, assert_raises
from numpy import array as np_array
from numpy import uint32, float64, dtype
from bases.algorithm.algorithmbase import (AlgorithmNameError,
                                           AlgorithmParameterError)
from bases.algorithm import (AlgorithmBase, Parameter)

TestPar = Parameter("testPar", 5.0, valueRange=(4.0, 13.0))


@raises(AlgorithmNameError)
def testName():
    """name is mandatory"""
    class DummyAlgo(AlgorithmBase):
        pass

    t = DummyAlgo()


def testParam():
    """Algorithm without parameters allowed"""
    class DummyAlgo(AlgorithmBase):
        pass

    atype = DummyAlgo.factory("testalgo")
    t = atype()