Пример #1
0
    def test_update_mean_var():
        """test update_mean_var."""
        labels = [np.ones([40, 40, 5]) * i for i in range(1, 4)]

        data = dict()

        for m in LabelHelper.MORPHOLOGIES:
            data[f"{m}_mean"] = np.zeros([40, 43], dtype=np.float32)
            data[f"{m}_var"] = np.zeros([40, 43], dtype=np.float32)
        data["n"] = np.zeros([40, 43], dtype=np.int32)

        batch_idxs = [(0, 0), (0, 1), (0, 2)]

        for idx, lbl in zip(batch_idxs, labels):
            LabelHelper.update_mean_var(data, [lbl], [idx])

        expected_mean = np.zeros([40, 43], dtype=np.float32)
        expected_mean[5:35, 5] = 1
        expected_mean[5:35, 6] = 1.5
        expected_mean[5:35, 7:35] = 2
        expected_mean[5:35, 35] = 2.5
        expected_mean[5:35, 36] = 3

        expected_var = np.zeros([40, 43], dtype=np.float32)
        expected_var[5:35, 6] = 0.25
        expected_var[5:35, 7] = 0.666_666
        expected_var[5:35, 8:35] = 2
        expected_var[5:35, 35] = 0.5

        for m in LabelHelper.MORPHOLOGIES:
            np.testing.assert_array_almost_equal(expected_mean,
                                                 data[f"{m}_mean"])
            np.testing.assert_array_almost_equal(expected_var,
                                                 data[f"{m}_var"])
Пример #2
0
    def test_make_n_array():
        """Test the make_n_array method."""

        expected_shape = (100, 100)
        expected_dtype = np.int16
        expected_keys = "n"

        outs = LabelHelper.make_n_array(expected_shape)

        for k in expected_keys:
            assert expected_shape == outs[k].shape
            assert np.issubdtype(expected_dtype, outs[k].dtype)
Пример #3
0
    def test_index_generator():
        """Test the index_generator method."""
        shape = (100, 100)

        arr = np.zeros(shape, dtype=np.bool)

        index_gen = LabelHelper.index_generator(*shape)

        for i, j in index_gen:
            arr[i, j] = True

        assert arr.all()
Пример #4
0
    def test_make_rank_vote_arrays():
        """Test the make_rank_vote_arrays method."""

        expected_shape = (100, 100)
        expected_dtype = np.float32
        expected_keys = LabelHelper.MORPHOLOGIES

        outs = LabelHelper.make_rank_vote_arrays(expected_shape)

        for k in expected_keys:
            assert expected_shape == outs[k].shape
            assert np.issubdtype(expected_dtype, outs[k].dtype)
Пример #5
0
    def test_update_ns():
        """test update_ns"""

        ns = {"n": np.zeros([41, 41], dtype=np.int32)}
        batch_idxs = [(0, 0), (0, 1), (1, 0)]

        expected_ns = np.zeros([41, 41], dtype=np.int32)

        # ones
        expected_ns[5, 5] = 1
        expected_ns[5:35, -6] = 1
        expected_ns[-6, 5:-6] = 1

        # twos
        expected_ns[6:-6, 5] = 2
        expected_ns[5, 6:-6] = 2

        # threes
        expected_ns[6:-6, 6:-6] = 3

        LabelHelper.update_ns(ns, batch_idxs)

        np.testing.assert_array_equal(expected_ns, ns["n"])
Пример #6
0
    def test_make_mean_var_arrays():
        """Test the make_mean_var_arrays method."""

        expected_shape = (100, 100)
        expected_dtype = np.float32

        expected_keys = []
        for morph in LabelHelper.MORPHOLOGIES:
            for t in ["mean", "var"]:
                expected_keys.append(f"{morph}_{t}")

        outs = LabelHelper.make_mean_var_arrays(expected_shape)

        for k in expected_keys:
            assert expected_shape == outs[k].shape
            assert np.issubdtype(expected_dtype, outs[k].dtype)
Пример #7
0
    def test_iterative_mean():
        """Test the iterative_mean method."""

        shape = (10, 10)
        terms = [np.ones(shape) * i for i in range(9)]
        mean = np.zeros(shape)

        for i in range(9):
            ns = np.ones(shape) * (i + 1)
            mean = LabelHelper.iterative_mean(ns, mean, terms[i],
                                              np.ones_like(mean))

        expected_mean = np.mean(terms, axis=0)
        all_same = np.equal(expected_mean, mean)

        assert all_same.all()
Пример #8
0
    def test_iterative_rank_vote():
        """Test the iterative_rank_vote method."""

        shape = (10, 10)
        prev_votes = np.zeros(shape)

        ranks = np.zeros(shape, dtype=int)
        ranks[:, 0] = 4

        update_mask = np.ones(shape)
        votes = LabelHelper.iterative_rank_vote(ranks, prev_votes, update_mask)

        expected_votes = prev_votes.copy()
        expected_votes[:, 0] = 1

        all_same = np.equal(expected_votes, votes)

        assert all_same.all()
Пример #9
0
    def test_get_final_map_complete():
        """Test the get_final_map method on complete array."""

        shape = (100, 100)
        arr = np.zeros(shape, dtype=np.bool)

        for i in range(shape[0] - LabelHelper.UPDATE_MASK_N.shape[0] + 1):
            for j in range(shape[1] - LabelHelper.UPDATE_MASK_N.shape[1] + 1):
                final_map = LabelHelper.get_final_map(shape, i, j)
                for (y, x) in final_map:
                    arr[i + y, j + x] = True

        expected_array = np.zeros(shape, dtype=np.bool)
        expected_array[5:-5, 5:-5] = True

        all_same = np.equal(expected_array, arr)

        assert all_same.all()
Пример #10
0
    def test_windowed_index_generator():
        """Test the windowed_index_generator function."""

        shape = (100, 100)

        arr = np.zeros(shape, dtype=np.bool)

        index_gen = LabelHelper.windowed_index_generator(*shape)

        for i, j in index_gen:
            arr[i, j] = True

        window_y, window_x = LabelHelper.UPDATE_MASK_N.shape
        expected_array = np.ones(
            (shape[0] - window_y + 1, shape[1] - window_x + 1))

        expected_array = np.pad(expected_array,
                                ((0, window_y - 1), (0, window_x - 1)),
                                "constant").astype(np.bool)

        np.testing.assert_array_equal(arr, expected_array)
Пример #11
0
    def test_iterative_variance_with_final():
        """Test the iterative variance with finalization."""

        shape = (10, 10)
        terms = [np.ones(shape) * i for i in range(9)]

        expected_var = np.var(terms, axis=0)

        n = np.ones(shape) * 9
        sn = expected_var * n

        final_map = []

        for i in range(shape[0]):
            for j in range(shape[1]):
                final_map.append((i, j))

        var = LabelHelper.finalize_variance(n, sn, final_map)

        all_same = np.equal(expected_var, var)

        assert all_same.all()
Пример #12
0
    def test_iterative_variance_no_final():
        """Test the iterative variance before finalization."""

        shape = (10, 10)
        terms = [np.ones(shape) * i for i in range(9)]
        s_n = np.zeros(shape)
        update_mask = np.ones((shape))

        for i in range(9):
            curr_mean = np.mean(terms[:i + 1], axis=0)
            if i > 0:
                prev_mean = np.mean(terms[:i], axis=0)
            else:
                prev_mean = curr_mean.copy()

            s_n = LabelHelper.iterative_variance(s_n, terms[i], prev_mean,
                                                 curr_mean, update_mask)

        n = np.ones(shape) * 9
        expected_sn = np.var(terms, axis=0) * n

        all_same = np.equal(expected_sn, s_n)

        assert all_same.all()