Пример #1
0
    def test_background_usage(self):
        img = prepare_image((80, 80))
        gt = np.zeros((80, 80), dtype=int)
        background = np.ones((80, 80))
        background = background * 0.5

        draw_cell(img, (10, 10), 6)
        draw_disc(gt, (10, 10), 6, 1)

        draw_cell(img, (30, 15), 8)
        draw_disc(gt, (30, 15), 6, 1)

        # background with cell looking noise
        draw_cell(img, (30, 40), 9)
        draw_cell(background, (30, 40), 9)

        cellstar = Segmentation(9, 14)
        cellstar.set_frame(img)
        segmentation, snakes = cellstar.run_segmentation()

        # background noise is returned as cell
        self.assertEqual(3, segmentation.max())
        self.assertEqual(3, len(snakes))

        cellstar = Segmentation(9, 14)
        cellstar.set_frame(img)
        cellstar.set_background(background)

        segmentation, snakes = cellstar.run_segmentation()
        self.assertEqual(2, segmentation.max())
        self.assertEqual(2, len(snakes))
Пример #2
0
    def test_mask_usage(self):
        img = prepare_image((80, 80))
        gt = np.zeros((80, 80), dtype=int)

        draw_very_weak_cell(img, (10, 10), 6)
        draw_disc(gt, (10, 10), 6, 1)

        draw_very_weak_cell(img, (30, 15), 8)
        draw_disc(gt, (30, 15), 6, 1)

        # very bright points
        img[50:70, 50:70] = 1
        img[50:70, 10:30] = 0

        cellstar = Segmentation(9, 10)
        cellstar.set_frame(img)
        segmentation, snakes = cellstar.run_segmentation()

        self.assertEqual(0, segmentation.max())
        self.assertEqual(0, len(snakes))

        ignore_mask = (img == 1) | (img == 0)
        cellstar = Segmentation(9, 10)
        cellstar.set_frame(img)
        cellstar.set_mask(ignore_mask)

        segmentation, snakes = cellstar.run_segmentation()
        self.assertEqual(2, segmentation.max())
        self.assertEqual(2, len(snakes))
Пример #3
0
    def test_no_objects(self):
        img = prepare_image((50, 50))

        cellstar = Segmentation(9, 10)
        cellstar.set_frame(img)
        segmentation, snakes = cellstar.run_segmentation()

        self.assertEqual(0, segmentation.max())
        self.assertEqual(0, len(snakes))
Пример #4
0
    def test_multiple_objects(self):
        img = prepare_image((50, 50))
        draw_cell(img, (10, 10), 6)
        draw_cell(img, (30, 15), 8)

        cellstar = Segmentation(9, 10)
        cellstar.set_frame(img)
        segmentation, snakes = cellstar.run_segmentation()

        self.assertEqual(2, segmentation.max())
        self.assertEqual(2, len(snakes))
Пример #5
0
def test_trained_parameters(image, star_params, ranking_params, precision, avg_cell_diameter, output_name=None):
    seg = Segmentation(segmentation_precision=precision, avg_cell_diameter=avg_cell_diameter)
    for k, v in star_params.iteritems():
        seg.parameters["segmentation"]["stars"][k] = v
    for k, v in ranking_params.iteritems():
        seg.parameters["segmentation"]["ranking"][k] = v

    seg.set_frame(image)
    seg.run_segmentation()
    if output_name is None:
        image_show(seg.images.segmentation, 1)
    else:
        image_save(seg.images.segmentation, output_name)
Пример #6
0
    def test_rank_fitting(self):
        img = prepare_image((80, 80))
        gt = np.zeros((80, 80), dtype=int)
        draw_weak_cell(img, (60, 20), 12)
        draw_disc(gt, (60, 20), 10, 1)
        draw_weak_cell(img, (40, 25), 8)
        draw_disc(gt, (40, 25), 6, 2)
        draw_weak_cell(img, (30, 40), 9)
        draw_disc(gt, (30, 40), 7, 3)
        img = finish_image(img)

        cellstar = Segmentation(11, 20)

        # break parameters
        encoded = params.pf_rank_parameters_encode(cellstar.parameters)
        one_params = [0.1 for k in encoded]
        one_decoded = params.pf_rank_parameters_decode(one_params)
        broken_params = PFRankSnake.merge_rank_parameters(
            cellstar.parameters, one_decoded)

        cellstar.parameters = broken_params

        cellstar.set_frame(img)
        segmentation, snakes = cellstar.run_segmentation()

        # fail miserably (best snakes are nowhere close)
        best3 = get_best_mask(segmentation, 3)
        segmentation_quality = calculate_diff_fraction(best3, gt)
        self.assertLessEqual(segmentation_quality, 0.01)

        new_params, _ = run_rank_pf(img, None, None, gt, cellstar.parameters)
        cellstar = Segmentation(11, 20)
        cellstar.parameters = new_params
        cellstar.set_frame(img)

        segmentation2, snakes2 = cellstar.run_segmentation()

        self.assertLessEqual(3, segmentation2.max())
        self.assertLessEqual(3, len(snakes2))

        # find best 3 objects
        best3 = get_best_mask(segmentation2, 3)
        snake3 = snakes2[:3]
        segmentation_quality = calculate_diff_fraction(best3, gt)
        self.assertLessEqual(
            0.3, segmentation_quality)  # there was no contour fitting

        object_diffs = calculate_diffs_per_object(best3, gt)
        self.assertLessEqual(0.3, min(object_diffs))
Пример #7
0
    def test_contour_fitting(self):
        process.SEARCH_LENGTH_NORMAL = 20

        img = prepare_image((80, 80))
        gt = np.zeros((80, 80), dtype=int)
        draw_weak_cell(img, (60, 20), 12)
        draw_disc(gt, (60, 20), 14, 1)
        draw_weak_cell(img, (40, 25), 8)
        draw_disc(gt, (40, 25), 8, 2)
        draw_weak_cell(img, (30, 40), 9)
        draw_disc(gt, (30, 40), 9, 3)

        img = finish_image(img)

        cellstar = Segmentation(11, 20)

        # break parameters
        weights = cellstar.parameters["segmentation"]["stars"]["sizeWeight"]
        encoded = params.pf_parameters_encode(cellstar.parameters)
        one_params = [0.5 for k in encoded]
        one_params[3] = 0.001
        one_decoded = params.pf_parameters_decode(one_params, weights)
        broken_params = PFSnake.merge_parameters(cellstar.parameters,
                                                 one_decoded)

        cellstar.parameters = broken_params

        cellstar.set_frame(img)
        segmentation, snakes = cellstar.run_segmentation()

        # fail miserably (not all snakes are found)
        self.assertGreater(3, segmentation.max())
        self.assertGreater(3, len(snakes))

        new_params, _ = run_pf(img, None, None, gt, cellstar.parameters, 11,
                               20)
        cellstar = Segmentation(11, 20)
        cellstar.parameters = new_params
        cellstar.set_frame(img)

        segmentation2, snakes2 = cellstar.run_segmentation()

        self.assertLessEqual(3, segmentation2.max())
        self.assertLessEqual(3, len(snakes2))

        # find best 3 objects
        best3 = get_best_mask(segmentation2, 3)
        segmentation_quality = calculate_diff_fraction(best3, gt)
        self.assertLessEqual(0.7, segmentation_quality)
Пример #8
0
    def test_rank_fitting(self):
        img = prepare_image((80, 80))
        gt = np.zeros((80, 80), dtype=int)
        draw_weak_cell(img, (60, 20), 12)
        draw_disc(gt, (60, 20), 10, 1)
        draw_weak_cell(img, (40, 25), 8)
        draw_disc(gt, (40, 25), 6, 2)
        draw_weak_cell(img, (30, 40), 9)
        draw_disc(gt, (30, 40), 7, 3)
        img = finish_image(img)

        cellstar = Segmentation(11, 20)

        # break parameters
        encoded = params.pf_rank_parameters_encode(cellstar.parameters)
        one_params = [0.1 for k in encoded]
        one_decoded = params.pf_rank_parameters_decode(one_params)
        broken_params = PFRankSnake.merge_rank_parameters(cellstar.parameters, one_decoded)

        cellstar.parameters = broken_params

        cellstar.set_frame(img)
        segmentation, snakes = cellstar.run_segmentation()

        # fail miserably (best snakes are nowhere close)
        best3 = get_best_mask(segmentation, 3)
        segmentation_quality = calculate_diff_fraction(best3, gt)
        self.assertLessEqual(segmentation_quality, 0.01)

        new_params, _ = run_rank_pf(img, None, None, gt, cellstar.parameters)
        cellstar = Segmentation(11, 20)
        cellstar.parameters = new_params
        cellstar.set_frame(img)

        segmentation2, snakes2 = cellstar.run_segmentation()

        self.assertLessEqual(3, segmentation2.max())
        self.assertLessEqual(3, len(snakes2))

        # find best 3 objects
        best3 = get_best_mask(segmentation2, 3)
        snake3 = snakes2[:3]
        segmentation_quality = calculate_diff_fraction(best3, gt)
        self.assertLessEqual(0.3, segmentation_quality)  # there was no contour fitting

        object_diffs = calculate_diffs_per_object(best3, gt)
        self.assertLessEqual(0.3, min(object_diffs))
Пример #9
0
    def test_contour_fitting(self):
        process.SEARCH_LENGTH_NORMAL = 20

        img = prepare_image((80, 80))
        gt = np.zeros((80,80), dtype=int)
        draw_weak_cell(img, (60, 20), 12)
        draw_disc(gt, (60, 20), 14, 1)
        draw_weak_cell(img, (40, 25), 8)
        draw_disc(gt, (40, 25), 8, 2)
        draw_weak_cell(img, (30, 40), 9)
        draw_disc(gt, (30, 40), 9, 3)

        img = finish_image(img)

        cellstar = Segmentation(11, 20)

        # break parameters
        weights = cellstar.parameters["segmentation"]["stars"]["sizeWeight"]
        encoded = params.pf_parameters_encode(cellstar.parameters)
        one_params = [0.5 for k in encoded]
        one_params[3] = 0.001
        one_decoded = params.pf_parameters_decode(one_params, weights)
        broken_params = PFSnake.merge_parameters(cellstar.parameters, one_decoded)

        cellstar.parameters = broken_params

        cellstar.set_frame(img)
        segmentation, snakes = cellstar.run_segmentation()

        # fail miserably (not all snakes are found)
        self.assertGreater(3, segmentation.max())
        self.assertGreater(3, len(snakes))

        new_params, _ = run_pf(img, None, None, gt, cellstar.parameters, 11, 20)
        cellstar = Segmentation(11, 20)
        cellstar.parameters = new_params
        cellstar.set_frame(img)

        segmentation2, snakes2 = cellstar.run_segmentation()

        self.assertLessEqual(3, segmentation2.max())
        self.assertLessEqual(3, len(snakes2))

        # find best 3 objects
        best3 = get_best_mask(segmentation2, 3)
        segmentation_quality = calculate_diff_fraction(best3, gt)
        self.assertLessEqual(0.7, segmentation_quality)
Пример #10
0
def test_trained_parameters(image,
                            star_params,
                            ranking_params,
                            precision,
                            avg_cell_diameter,
                            output_name=None):
    seg = Segmentation(segmentation_precision=precision,
                       avg_cell_diameter=avg_cell_diameter)
    for k, v in star_params.iteritems():
        seg.parameters["segmentation"]["stars"][k] = v
    for k, v in ranking_params.iteritems():
        seg.parameters["segmentation"]["ranking"][k] = v

    seg.set_frame(image)
    seg.run_segmentation()
    if output_name is None:
        image_show(seg.images.segmentation, 1)
    else:
        image_save(seg.images.segmentation, output_name)
Пример #11
0
    def test_large_image(self):
        img = prepare_image((300, 300))
        gt = np.zeros((300, 300), dtype=int)

        draw_cell(img, (40, 40), 30)
        draw_disc(gt, (40, 40), 30, 1)
        draw_cell(img, (150, 90), 25)
        draw_disc(gt, (150, 90), 25, 2)
        draw_cell(img, (180, 70), 20)
        draw_disc(gt, (180, 70), 20, 3)

        cellstar = Segmentation(11, 60)
        cellstar.set_frame(img)
        segmentation, snakes = cellstar.run_segmentation()

        self.assertEqual(3, segmentation.max())
        self.assertEqual(3, len(snakes))

        object_diffs = calculate_diffs_per_object(segmentation, gt)
        self.assertLessEqual(0.8, min(object_diffs))