예제 #1
0
    def test_bad_calls(self):
        with self.assertRaises(AttributeError):
            make_sweep(1, 3, num=3, step=1)

        with self.assertRaises(ValueError):
            make_sweep(1, 3)

        # this first one should succeed
        make_sweep(1, 3, step=1)
        # but if we change step slightly (more than the tolerance of
        # 1e-10 steps) it will fail.
        with self.assertRaises(ValueError):
            make_sweep(1, 3, step=1.00000001)
        with self.assertRaises(ValueError):
            make_sweep(1, 3, step=0.99999999)
예제 #2
0
    def test_good_calls(self):
        swp = make_sweep(1, 3, num=6)
        self.assertEqual(swp, [1, 1.4, 1.8, 2.2, 2.6, 3])

        swp = make_sweep(1, 3, step=0.5)
        self.assertEqual(swp, [1, 1.5, 2, 2.5, 3])

        # with step, test a lot of combinations with weird fractions
        # to make sure we don't fail on a rounding error
        for r in np.linspace(1, 4, 15):
            for steps in range(5, 55, 6):
                step = r / steps
                swp = make_sweep(1, 1 + r, step=step)
                self.assertEqual(len(swp), steps + 1)
                self.assertEqual(swp[0], 1)
                self.assertEqual(swp[-1], 1 + r)
예제 #3
0
def test_good_calls():
    swp = make_sweep(1, 3, num=6)
    assert swp == [1, 1.4, 1.8, 2.2, 2.6, 3]

    swp = make_sweep(1, 3, step=0.5)
    assert swp == [1, 1.5, 2, 2.5, 3]

    # with step, test a lot of combinations with weird fractions
    # to make sure we don't fail on a rounding error
    for r in np.linspace(1, 4, 15):
        for steps in range(5, 55, 6):
            step = r / steps
            swp = make_sweep(1, 1 + r, step=step)
            assert len(swp) == steps + 1
            assert swp[0] == 1
            assert swp[-1] == 1 + r
예제 #4
0
    def test_bad_calls(self):
        with self.assertRaises(AttributeError):
            make_sweep(1, 3, num=3, step=1)

        with self.assertRaises(ValueError):
            make_sweep(1, 3)

        # this first one should succeed
        make_sweep(1, 3, step=1)
예제 #5
0
    def __init__(
        self,
        parameter: "ParameterBase",
        keys: Optional[Any] = None,
        start: Optional[float] = None,
        stop: Optional[float] = None,
        step: Optional[float] = None,
        num: Optional[int] = None,
    ):
        super().__init__(parameter)
        self._snapshot: Dict[str, Any] = {}
        self._value_snapshot: List[Dict[str, Any]] = []

        if keys is None:
            if start is None:
                raise ValueError("If keys is None, start needs to be not None.")
            if stop is None:
                raise ValueError("If keys is None, stop needs to be not None.")
            keys = make_sweep(start=start, stop=stop, step=step, num=num)
            self._values = keys
            self._add_linear_snapshot(self._values)

        elif isinstance(keys, slice):
            self._add_slice(keys)
            self._add_linear_snapshot(self._values)

        elif is_sequence(keys):
            for key in keys:
                if isinstance(key, slice):
                    self._add_slice(key)
                elif is_sequence(key):
                    # not sure if we really need to support this (and I'm not
                    # going to recurse any more!) but we will get nested lists
                    # if for example someone does `p[list1, list2]`
                    self._values.extend(key)
                else:
                    # assume a single value
                    self._values.append(key)
            # we dont want the snapshot to go crazy on big data
            if self._values:
                self._add_sequence_snapshot(self._values)

        else:
            # assume a single value
            self._values.append(keys)
            self._value_snapshot.append({"item": keys})

        self.validate(self._values)
예제 #6
0
    def __init__(self,
                 parameter,
                 keys=None,
                 start=None,
                 stop=None,
                 step=None,
                 num=None):
        super().__init__(parameter)
        self._snapshot = {}
        self._value_snapshot = []

        if keys is None:
            keys = make_sweep(start=start, stop=stop, step=step, num=num)
            self._values = keys
            self._add_linear_snapshot(self._values)

        elif isinstance(keys, slice):
            self._add_slice(keys)
            self._add_linear_snapshot(self._values)

        elif is_sequence(keys):
            for key in keys:
                if isinstance(key, slice):
                    self._add_slice(key)
                elif is_sequence(key):
                    # not sure if we really need to support this (and I'm not
                    # going to recurse any more!) but we will get nested lists
                    # if for example someone does `p[list1, list2]`
                    self._values.extend(key)
                else:
                    # assume a single value
                    self._values.append(key)
            # we dont want the snapshot to go crazy on big data
            if self._values:
                self._add_sequence_snapshot(self._values)

        else:
            # assume a single value
            self._values.append(keys)
            self._value_snapshot.append({'item': keys})

        self.validate(self._values)