def test_as_discrete(self): table = self.data domain = table.domain tr = AsCategorical() dtr = [] for v in domain.variables: vtr = apply_reinterpret(v, tr, table_column_data(table, v)) dtr.append(vtr) tdomain = Domain(dtr) ttable = table.transform(tdomain) assert_array_equal( ttable.X, np.array([ [0, 2, 2, 1], [1, 1, 3, 2], [2, 0, 1, 3], [1, 0, 0, 0], ], dtype=float) ) self.assertEqual(tdomain["A"].values, ("a", "b", "c")) self.assertEqual(tdomain["B"].values, ("0", "1", "2")) self.assertEqual(tdomain["C"].values, ("0.0", "0.2", "0.25", "1.25")) self.assertEqual( tdomain["D"].values, ("1970-01-01 00:00:00", "1970-01-01 00:03:00", "1970-01-01 00:06:00", "1970-01-01 00:12:00") )
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))
def test_reinterpret_string(self): table = self.data_str domain = table.domain tvars = [] for v in domain.metas: for tr in [AsContinuous(), AsCategorical(), AsTime(), AsString()]: tr = apply_reinterpret(v, tr, table_column_data(table, v)) tvars.append(tr) 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))
def test_reinterpret(self): var = String("T", ()) for tr in (AsContinuous(), AsCategorical(), AsTime()): t = report_transform(var, [tr]) self.assertIn("→ (", t)