Ejemplo n.º 1
0
    def __init__(self, lower_bound: int, upper_bound: int, data_tick: int):
        if not isinstance(lower_bound, int) or \
           not isinstance(upper_bound, int) or \
           not isinstance(data_tick, int):
            raise TypeError("All inputs must be of type int.")

        if (lower_bound > upper_bound) or \
           (data_tick > upper_bound) or \
           (data_tick < lower_bound):
            raise ValueError(
                "Invalid inputs. Check lower_/upper_bound and data_tick")

        self._lower_bound = lower_bound
        self._upper_bound = upper_bound
        self._data_tick = data_tick
        self._fraction = (data_tick - lower_bound) / (upper_bound -
                                                      lower_bound)

        self._binding = AxisBinding.valueOf(self._fraction)
 def test_valueOf_09(self):
     with self.assertRaises(ValueError):
         AxisBinding.valueOf(10)
 def test_valueOf_08(self):
     self.assertEqual(AxisBinding.CUSTOM_FRACTION, AxisBinding.valueOf(0.2))
 def test_valueOf_07(self):
     self.assertEqual(AxisBinding.MIDDLE, AxisBinding.valueOf(0.5))
 def test_valueOf_06(self):
     self.assertEqual(AxisBinding.END, AxisBinding.valueOf(1.0))
 def test_valueOf_05(self):
     self.assertEqual(AxisBinding.BEGINNING, AxisBinding.valueOf(0))
 def test_valueOf_04(self):
     with self.assertRaises(TypeError):
         AxisBinding.valueOf({})
 def test_valueOf_02(self):
     with self.assertRaises(ValueError):
         AxisBinding.valueOf("None Existing")
 def test_valueOf_01(self):
     self.assertEqual(AxisBinding.BEGINNING,
                      AxisBinding.valueOf("beGiNnIng"))
Ejemplo n.º 10
0
    def __init__(self, lower_bound: Iterable[float],
                 upper_bound: Iterable[float], **kwargs):
        """
        Initializes a ``TimeAxis`` object given the ``lower_bound``, ``upper_bound``, and either ``fraction`` or
        ``data_ticks``.

        :param lower_bound: The lower bound time stamps as a ``long`` value (or something that could be casted to
                            ``long``). The units must be in microseconds. The ``lower_bound`` values must be
                            monotonically increasing.
        :param upper_bound: The upper bound time stamps as a ``long`` value (or something that could be casted to
                            ``long``). The units must be in microseconds. The ``upper_bound`` does NOT need to be
                            monotonically increasing; however, they should not be smaller than their counter-part
                            ``lower_bound``.
        :param kwargs: One of the following keys (just one, not more) must be provided:
            - ``fraction``:
            - ``data_ticks``:
            - ``binding``:
        """
        # making sure at there is only one of the "fraction", "data_ticks", or "binding" are provided.
        if sum(
                list(
                    map(lambda e: 1 if e in kwargs else 0,
                        ['fraction', 'data_ticks', 'binding']))) != 1:
            raise ValueError(
                "You must provide exactly just one of the 'fraction', 'data_ticks', or 'binding'."
            )

        self._bounds = Axis._create_bounds(lower_bound, upper_bound)
        self._nelem = self._bounds.shape[1]

        if "fraction" in kwargs:
            self._fraction = np.asarray(kwargs["fraction"],
                                        dtype="float64").reshape((1, -1))
            Axis._fraction_sanity_check(self._fraction)
            if (self._fraction.size != 1) and (self._fraction.size !=
                                               self._nelem):
                raise ValueError(
                    "Fraction must be either a single number, or as many as there "
                    "upper/lower bound values.")

            self._data_ticks = Axis._calculate_data_ticks_fromFraction(
                self._bounds[0, :], self._bounds[1, :], self._fraction)
            self._binding = Axis._get_binding(self._fraction)

        if "data_ticks" in kwargs:
            self._data_ticks = np.asarray(kwargs["data_ticks"],
                                          dtype="int64").reshape((1, -1))
            Axis._data_ticks_sanity_check(self._data_ticks)
            if self._data_ticks.size != self._nelem:
                raise ValueError(
                    "Data Ticks must have as many elements as there are lower/upper bound values."
                )

            self._fraction = Axis._calculate_fraction_from_data_ticks(
                self._bounds[0, :], self._bounds[1, :], self._data_ticks)

            self._binding = Axis._get_binding(self._fraction)

        if "binding" in kwargs:
            self._binding = AxisBinding.valueOf(kwargs["binding"])
            if self._binding == AxisBinding.CUSTOM_FRACTION:
                raise ValueError(
                    "Can't guess the fraction for the Custom Fraction. Use the fraction option instead."
                )
            self._fraction = np.asarray(self._binding.fraction(),
                                        dtype="float64").reshape((1, -1))
            self._data_ticks = Axis._calculate_data_ticks_fromFraction(
                self._bounds[0, :], self._bounds[1, :], self._fraction)

        if self._binding in (AxisBinding.BEGINNING, AxisBinding.MIDDLE,
                             AxisBinding.END):
            self._fraction = np.asarray([self._fraction[0][0]],
                                        dtype="float64").reshape((1, -1))