Exemplo n.º 1
0
def test_hnccorr_single_segment(mocker, dummy, neurofinder_data,
                                matlab_segmentation):
    seeder = LocalCorrelationSeeder(3, 0.4, 4, 5)
    postprocessor = SizePostprocessor(40, 200, 80)
    segmentor = HncParametricWrapper(0, 100000)
    positive_seed_selector = PositiveSeedSelector(0)

    # negative seed selector is mocked due to a bug in the matlab code.
    # Matlab passes the argument to sin(x)/cos(x) in degrees instead of radians.
    # This results in slightly wrong negative seeds.
    negative_seed_selector = mocker.patch("hnccorr.seeds.NegativeSeedSelector",
                                          autospec=True)(dummy, dummy)
    negative_seed_selector.select.return_value = {
        (283, 447),
        (273, 435),
        (285, 427),
        (292, 440),
        (278, 445),
        (274, 431),
        (289, 429),
        (289, 444),
        (274, 442),
        (278, 427),
    }

    edge_selector = SparseComputationEmbeddingWrapper(3,
                                                      1 / 35.0,
                                                      dimension_reducer=PCA(3))
    graph_constructor = GraphConstructor(
        edge_selector, lambda a, b: exponential_distance_decay(a, b, 1.0))
    patch_size = 31

    H = HNCcorr(
        seeder,
        postprocessor,
        segmentor,
        positive_seed_selector,
        negative_seed_selector,
        graph_constructor,
        Candidate,
        Patch,
        CorrelationEmbedding,
        patch_size,
    )

    H.movie = Movie("Neurofinder02.00", neurofinder_data)

    center_seed = (282, 436)
    c = Candidate(center_seed, H)
    best_segmentation = c.segment()

    assert best_segmentation.selection == matlab_segmentation.selection
    assert best_segmentation.weight == pytest.approx(
        matlab_segmentation.weight)
Exemplo n.º 2
0
    def test_candidate_clean_segmentations(self, dummy, mock_segmentor,
                                           mock_segmentation_class, hnccorr):
        mock_segmentor.solve.return_value = [
            mock_segmentation_class(dummy, dummy),
            mock_segmentation_class(dummy, dummy),
        ]
        mock_segmentation_class.return_value.clean.return_value = "clean"

        c = Candidate(1, hnccorr)
        assert c.clean_segmentations is None
        c.segment()
        assert c.clean_segmentations == ["clean", "clean"]
Exemplo n.º 3
0
    def test_candidate_segment(
        self,
        hnccorr,
        dummy,
        mock_movie,
        mock_postprocessor,
        mock_segmentor,
        mock_pos_seed_selector,
        mock_neg_seed_selector,
        mock_graph_constructor,
        mock_patch_class,
        mock_embedding_class,
        mock_segmentation_class,
    ):
        mock_pos_seed_selector.select.return_value = "positive_seed"
        mock_neg_seed_selector.select.return_value = "negative_seed"
        mock_embedding_class.return_value = "embedding_class"
        mock_postprocessor.select.return_value = "best segmentation"
        mock_graph_constructor.construct.return_value = "graph"
        mock_patch_class.return_value = "patch"
        mock_segmentation_class.return_value.clean.return_value = "clean"

        center_seed = 1
        mock_segmentor.solve.return_value = [
            mock_segmentation_class(dummy, dummy),
            mock_segmentation_class(dummy, dummy),
        ]

        candidate = Candidate(center_seed, hnccorr)
        assert candidate.segment() == mock_postprocessor.select.return_value
        mock_pos_seed_selector.select.assert_called_once_with(
            center_seed, mock_movie)
        mock_neg_seed_selector.select.assert_called_once_with(
            center_seed, mock_movie)
        mock_patch_class.assert_called_once_with(mock_movie, center_seed,
                                                 "patch_size")
        mock_embedding_class.assert_called_once_with(
            mock_patch_class.return_value)
        mock_graph_constructor.construct.assert_called_once_with(
            mock_patch_class.return_value, mock_embedding_class.return_value)
        mock_segmentor.solve.assert_called_once_with(
            mock_graph_constructor.construct.return_value,
            mock_pos_seed_selector.select.return_value,
            mock_neg_seed_selector.select.return_value,
        )
        mock_postprocessor.select.assert_called_once_with(["clean", "clean"])
Exemplo n.º 4
0
def candidate(H):
    return Candidate(H.seeder.return_val, H)
Exemplo n.º 5
0
 def test_candidate_best_segmentations(self, hnccorr, mock_postprocessor):
     c = Candidate(1, hnccorr)
     assert c.best_segmentation is None
     c.segment()
     assert c.best_segmentation == mock_postprocessor.select.return_value
Exemplo n.º 6
0
 def test_candidate_center_seed(self):
     assert Candidate(1, "a").center_seed == 1
Exemplo n.º 7
0
    def test_candidate_segmentations(self, hnccorr, mock_segmentor):
        c = Candidate(1, hnccorr)

        assert c.segmentations is None
        c.segment()
        assert c.segmentations == mock_segmentor.solve.return_value
Exemplo n.º 8
0
    def test_candidate_equality_wrong_class(self):
        class FakeCandidate:
            pass

        assert Candidate(1, "a") != FakeCandidate()
Exemplo n.º 9
0
 def test_candidate_equality(self):
     assert Candidate(1, "a") == Candidate(1, "a")
     assert Candidate(1, "a") != Candidate(2, "a")
     assert Candidate(1, "a") != Candidate(1, "b")