Exemplo n.º 1
0
    def testRegression(self):
        pred = np.asarray([0, 1, 2, 3])[:, np.newaxis] / 4.0
        gt = np.asarray([0, 1, 0, 2.99])[:, np.newaxis] / 4.0
        valid = np.ones(pred.shape[:1], dtype=np.uint8)
        desc = np.zeros((len(gt),), dtype=np.int)
        desc[:2] = SplitTypes.TRAIN
        desc[2:] = SplitTypes.TEST

        d = {"levels": 5}

        op = OpRegressionReport.build(d, graph=self.g, workingdir=self.wd)
        op.All.resize(2)
        op.All[0].setValue(pred)
        op.All[1].setValue(gt)
        op.Valid.resize(2)
        op.Valid[0].setValue(valid)
        op.Valid[1].setValue(valid)
        op.Description.setValue(desc)

        assert op.Output.value

        with open(os.path.join(self.wd, "report.json"), "r") as f:
            report = loads(f.read())

        print(report)

        mse_all = np.square(pred - gt).mean()
        mse_test = np.square(pred[2:] - gt[2:]).mean()
        np.testing.assert_almost_equal(report["all_MSE"], mse_all)
        np.testing.assert_almost_equal(report["test_MSE"], mse_test)
        np.testing.assert_almost_equal(report["all_misclass"], .25)
        np.testing.assert_almost_equal(report["test_misclass"], .5)
Exemplo n.º 2
0
    def testSerialization(self):
        from lazyflow.operator import Operator
        from tsdl.data import OpPickleCache
        d = {"class": Operator,
             "cache": {"class": OpPickleCache},
             "answer": 42,
             "a_list": [OpPickleCache, {"class": OpPickleCache,
                                        "tuple_in_dict": (1, 2, 3)}],
             "a_tuple": (OpPickleCache, {"class": OpPickleCache}),
             "nested": (1, [2, (3, 4, 5)]),
             "subdict": {"a": 1},
             "a_string": "asdf"}
        s = dumps(d)
        from pprint import pprint
        pprint(d)
        print("serialized to: \n{}".format(s))
        d2 = loads(s)
        print("")
        pprint(d2)
        assert d == d2

        class Custom(object):
            A = 1

        d = {"key": Custom()}
        with self.assertRaises(TypeError):
            dumps(d)
Exemplo n.º 3
0
    def testClassification(self):
        pred = np.asarray([[0, 1], [0, 1], [1, 0], [1, 0]])
        gt = np.asarray([[0, 1], [0, 1], [0, 1], [1, 0]])
        valid = np.ones(pred.shape[:1], dtype=np.uint8)
        desc = np.zeros((len(gt),), dtype=np.int)
        desc[:2] = SplitTypes.TRAIN
        desc[2:] = SplitTypes.TEST

        op = OpClassificationReport.build({}, graph=self.g, workingdir=self.wd)
        op.All.resize(2)
        op.All[0].setValue(pred)
        op.All[1].setValue(gt)
        op.Valid.resize(2)
        op.Valid[0].setValue(valid)
        op.Valid[1].setValue(valid)
        op.Description.setValue(desc)

        assert op.Output.value

        with open(os.path.join(self.wd, "report.json"), "r") as f:
            report = loads(f.read())

        print(report)

        assert report["all_true"] == 3
        assert report["test_true"] == 1
        assert report["all_false"] == 1
        assert report["test_false"] == 1

        op.All[0].setValue(1-pred)

        assert op.Output.value

        with open(os.path.join(self.wd, "report.json"), "r") as f:
            report = loads(f.read())

        assert report["all_true"] == 1
        assert report["test_true"] == 1
        assert report["all_false"] == 3
        assert report["test_false"] == 1