Beispiel #1
0
 def _test_common(self, var):
     tr = [Rename(var.name + "_copy"), Annotate((("A", "1"), ))]
     XX = apply_transform_var(var, tr)
     self.assertEqual(XX.name, var.name + "_copy")
     self.assertEqual(XX.attributes, {"A": 1})
     self.assertIsInstance(XX.compute_value, Identity)
     self.assertIs(XX.compute_value.variable, var)
 def test_reinterpret_string(self):
     table = self.data_str
     domain = table.domain
     tvars = []
     for v in domain.metas:
         for i, tr in enumerate(
             [AsContinuous(),
              AsCategorical(),
              AsTime(),
              AsString()]):
             vtr = apply_reinterpret(v, tr, table_column_data(
                 table, v)).renamed(f"{v.name}_{i}")
             if isinstance(tr, AsTime):
                 strp = StrpTime("Detect automatically", None, 1, 1)
                 vtr = apply_transform_var(vtr, [strp])
             tvars.append(vtr)
     tdomain = Domain([], metas=tvars)
     ttable = table.transform(tdomain)
     assert_array_nanequal(
         ttable.metas,
         np.array([
             [0.1, 0., np.nan, "0.1", 2010., 0., 1262304000., "2010"],
             [1.0, 1., np.nan, "1.0", 2020., 1., 1577836800., "2020"],
         ],
                  dtype=object))
Beispiel #3
0
 def test_discrete_reorder(self):
     D = DiscreteVariable("D", values=("2", "3", "1", "0"))
     DD = apply_transform_var(D, [
         CategoriesMapping((("0", "0"), ("1", "1"), ("2", "2"), ("3", "3")))
     ])
     self.assertSequenceEqual(DD.values, ["0", "1", "2", "3"])
     self._assertLookupEquals(DD.compute_value,
                              Lookup(D, np.array([2, 3, 1, 0])))
Beispiel #4
0
 def test_discrete_merge(self):
     D = DiscreteVariable("D", values=("2", "3", "1", "0"))
     mapping = (
         ("0", "x"),
         ("1", "y"),
         ("2", "x"),
         ("3", "y"),
     )
     tr = [CategoriesMapping(mapping)]
     DD = apply_transform_var(D, tr)
     self.assertSequenceEqual(DD.values, ["x", "y"])
     self._assertLookupEquals(DD.compute_value,
                              Lookup(D, np.array([0, 1, 1, 0])))
Beispiel #5
0
 def test_discrete_add_drop(self):
     D = DiscreteVariable("D", values=("2", "3", "1", "0"))
     mapping = (
         ("0", None),
         ("1", "1"),
         ("2", "2"),
         ("3", None),
         (None, "A"),
     )
     tr = [CategoriesMapping(mapping)]
     DD = apply_transform_var(D, tr)
     self.assertSequenceEqual(DD.values, ["1", "2", "A"])
     self._assertLookupEquals(DD.compute_value,
                              Lookup(D, np.array([1, np.nan, 0, np.nan])))
    def test_as_time(self):
        # this test only test type of format that can be string, continuous and discrete
        # correctness of time formats is already tested in TimeVariable module
        d = TimeVariable("_").parse_exact_iso
        times = (
            ["07.02.2022", "18.04.2021"],  # date only
            ["07.02.2022 01:02:03", "18.04.2021 01:02:03"],  # datetime
            ["010203", "010203"],  # time
            ["02-07", "04-18"],
        )
        formats = ["25.11.2021", "25.11.2021 00:00:00", "000000", "11-25"]
        expected = [
            [d("2022-02-07"), d("2021-04-18")],
            [d("2022-02-07 01:02:03"),
             d("2021-04-18 01:02:03")],
            [d("01:02:03"), d("01:02:03")],
            [d("1900-02-07"), d("1900-04-18")],
        ]
        variables = [StringVariable(f"s{i}") for i in range(len(times))]
        variables += [
            DiscreteVariable(f"d{i}", values=t) for i, t in enumerate(times)
        ]
        domain = Domain([], metas=variables)
        metas = [t for t in times] + [list(range(len(x))) for x in times]
        table = Table(domain,
                      np.empty((len(times[0]), 0)),
                      metas=np.array(metas).transpose())

        tr = AsTime()
        dtr = []
        for v, f in zip(domain.metas, chain(formats, formats)):
            strp = StrpTime(f, *TimeVariable.ADDITIONAL_FORMATS[f])
            vtr = apply_transform_var(
                apply_reinterpret(v, tr, table_column_data(table, v)), [strp])
            dtr.append(vtr)

        ttable = table.transform(Domain([], metas=dtr))
        assert_array_equal(
            ttable.metas,
            np.array(list(chain(expected, expected)), dtype=float).transpose())
Beispiel #7
0
 def test_ordered_change(self):
     D = DiscreteVariable("D", values=("a", "b"), ordered=True)
     Do = apply_transform_var(D, [ChangeOrdered(False)])
     self.assertFalse(Do.ordered)
Beispiel #8
0
 def test_discrete_rename(self):
     D = DiscreteVariable("D", values=("a", "b"))
     DD = apply_transform_var(D,
                              [CategoriesMapping((("a", "A"), ("b", "B")))])
     self.assertSequenceEqual(DD.values, ["A", "B"])
     self.assertIs(DD.compute_value.variable, D)