コード例 #1
0
ファイル: test_features.py プロジェクト: abuchin/uncertainpy
    def test_add_features(self):
        def feature_function(time, values):
            return "t", "U"

        def feature_function2(time, values):
            return "t2", "U2"

        features = Features()

        features.add_features([feature_function, feature_function2],
                              labels={"feature_function": ["x", "y"]})

        time, values = features.feature_function(None, None)
        self.assertEqual(time, "t")
        self.assertEqual(values, "U")

        time, values = features.feature_function2(None, None)
        self.assertEqual(time, "t2")
        self.assertEqual(values, "U2")

        self.assertEqual(features.implemented_features(),
                         ["feature_function", "feature_function2"])

        self.assertEqual(features.features_to_run, [])

        self.assertEqual(features.labels, {"feature_function": ["x", "y"]})
コード例 #2
0
    def test_intitNewFeatures(self):
        def feature_function(time, values):
            return "t", "U"

        def feature_function2(time, values):
            return "t2", "U2"

        features = Features(new_features=[feature_function, feature_function2],
                            labels={"feature_function": ["x", "y"]},
                            logger_level="error")

        time, values = features.feature_function(None, None)
        self.assertEqual(time, "t")
        self.assertEqual(values, "U")

        time, values = features.feature_function2(None, None)
        self.assertEqual(time, "t2")
        self.assertEqual(values, "U2")

        self.assertEqual(features.implemented_features(),
                         ["feature_function", "feature_function2"])

        self.assertEqual(features.features_to_run,
                         ["feature_function", "feature_function2"])

        self.assertEqual(features.labels, {"feature_function": ["x", "y"]})
コード例 #3
0
ファイル: test_features.py プロジェクト: abuchin/uncertainpy
class TestFeatures(unittest.TestCase):
    def setUp(self):
        self.time = np.arange(0, 10)
        self.values = np.arange(0, 10) + 1
        self.info = {"info": 1}

        self.features = Features(verbose_level="error")

    def test_initNone(self):
        features = Features()

        self.assertIsInstance(features, Features)

    def test_initUtility(self):
        new_utility_methods = ["new"]

        features = Features(new_utility_methods=new_utility_methods)

        self.assertIn("new", features.utility_methods)

    def test_preprocess(self):
        features = Features()
        time, values = features.preprocess(self.time, self.values)

        self.assertTrue(np.array_equal(time, self.time))
        self.assertTrue(np.array_equal(values, self.values))

    def test_preprocess_args(self):
        features = Features()
        time, values, a, b = features.preprocess(self.time, self.values, 1, 2)

        self.assertTrue(np.array_equal(time, self.time))
        self.assertTrue(np.array_equal(values, self.values))
        self.assertEqual(a, 1)
        self.assertEqual(b, 2)

    def test_preprocess_assign(self):
        def preprocess(time, values):
            return "time", "values"

        features = Features(preprocess=preprocess)

        time, values = features.preprocess(self.time, self.values)

        self.assertEqual(time, "time")
        self.assertEqual(values, "values")

        features = Features(preprocess=None)
        features.preprocess = preprocess

        time, values = features.preprocess(self.time, self.values)

        self.assertEqual(time, "time")
        self.assertEqual(values, "values")

        features = Features(preprocess=None)
        with self.assertRaises(TypeError):
            features.preprocess = 12

    def test_calculate_featureNotImplemented(self):
        with self.assertRaises(AttributeError):
            self.features.calculate_feature("not_in_class", self.time,
                                            self.values)

    def test_calculate_featureUtilityMethod(self):
        with self.assertRaises(TypeError):
            self.features.calculate_feature("preprocess", self.time,
                                            self.values)

    def test_implemented_features(self):
        self.assertEqual(self.features.implemented_features(), [])

    def test_calculate_all_features(self):
        self.assertEqual(
            self.features.calculate_all_features(self.time, self.values), {})

    # def test_calculate(self):
    #     self.assertEqual(self.features.calculate(self.time, self.values), {})

    def test_intitFeatureList(self):
        features = Features(features_to_run=None)
        self.assertEqual(features.features_to_run, [])

        features = Features(features_to_run=["feature1d", "feature2"])
        self.assertEqual(features.features_to_run, ["feature1d", "feature2"])

        features = Features(features_to_run="all")
        self.assertEqual(features.features_to_run, [])

    def test_intitNewFeatures(self):
        def feature_function(time, values):
            return "t", "U"

        def feature_function2(time, values):
            return "t2", "U2"

        features = Features(new_features=[feature_function, feature_function2],
                            labels={"feature_function": ["x", "y"]})

        time, values = features.feature_function(None, None)
        self.assertEqual(time, "t")
        self.assertEqual(values, "U")

        time, values = features.feature_function2(None, None)
        self.assertEqual(time, "t2")
        self.assertEqual(values, "U2")

        self.assertEqual(features.implemented_features(),
                         ["feature_function", "feature_function2"])

        self.assertEqual(features.features_to_run,
                         ["feature_function", "feature_function2"])

        self.assertEqual(features.labels, {"feature_function": ["x", "y"]})

    def test_intitAdaptiveList(self):
        features = Features(adaptive=None)
        self.assertEqual(features.adaptive, [])

        features = Features(adaptive=["feature1d", "feature2"])
        self.assertEqual(features.adaptive, ["feature1d", "feature2"])

        features = Features(adaptive="all")
        self.assertEqual(features.adaptive, [])

    def test_add_feature(self):
        def feature_function(time, values):
            return "t", "U"

        features = Features()

        features.add_features(feature_function,
                              labels={"feature_function": ["x", "y"]})

        time, values = features.feature_function(None, None)
        self.assertEqual(time, "t")
        self.assertEqual(values, "U")

        features.features_to_run = "all"
        self.assertEqual(features.features_to_run, ["feature_function"])

        self.assertEqual(features.labels, {"feature_function": ["x", "y"]})

    def test_add_features(self):
        def feature_function(time, values):
            return "t", "U"

        def feature_function2(time, values):
            return "t2", "U2"

        features = Features()

        features.add_features([feature_function, feature_function2],
                              labels={"feature_function": ["x", "y"]})

        time, values = features.feature_function(None, None)
        self.assertEqual(time, "t")
        self.assertEqual(values, "U")

        time, values = features.feature_function2(None, None)
        self.assertEqual(time, "t2")
        self.assertEqual(values, "U2")

        self.assertEqual(features.implemented_features(),
                         ["feature_function", "feature_function2"])

        self.assertEqual(features.features_to_run, [])

        self.assertEqual(features.labels, {"feature_function": ["x", "y"]})

    def test_add_features_error(self):
        with self.assertRaises(TypeError):
            self.features.add_features({"test": 1})
            self.features.add_features([{"test": 1}])

    def test_reference_feature(self):
        time, values = self.features.reference_feature(self.time, self.values,
                                                       self.info)

        self.assertIsNone(time)
        self.assertIsNone(values)