def test_encode_decode_auto_params(self):
        parameters = params_util.default_parameters(10, 30)
        encoded = "[[1.31, 6.2, -8.3, 4.4000000000000004], [4.1, 2.88, 773.3, 14, -34]]"
        self.assertEqual(True, Segmentation.decode_auto_params_into(parameters, encoded))

        encoded_back = Segmentation.encode_auto_params_from_all_params(parameters)
        self.assertEqual(encoded, encoded_back)
Beispiel #2
0
    def test_encode_auto_params(self):
        parameters = params_util.default_parameters(10, 30)

        star_parameters = parameters['segmentation']['stars']
        star_parameters['brightnessWeight'] = 1.1
        star_parameters['cumBrightnessWeight'] = 2.2
        star_parameters['gradientWeight'] = 3.3
        star_parameters['sizeWeight'] = 4.4

        rank_parameters = parameters['segmentation']['ranking']
        rank_parameters['avgBorderBrightnessWeight'] = 11.1
        rank_parameters['avgInnerBrightnessWeight'] = 12.2
        rank_parameters['avgInnerDarknessWeight'] = 13.3
        rank_parameters['logAreaBonus'] = 14.4
        rank_parameters['maxInnerBrightnessWeight'] = 15.5

        encoded = Segmentation.encode_auto_params_from_all_params(parameters)
        self.assertEqual(
            "[[1.1, 2.2, 3.3, 4.4000000000000004], [11.1, 12.2, 13.3, 14.4, 15.5]]",
            encoded)

        # check list size weight
        star_parameters['sizeWeight'] = [2, 5, 11]
        encoded2 = Segmentation.encode_auto_params_from_all_params(parameters)
        self.assertEqual(
            "[[1.1, 2.2, 3.3, 6.0], [11.1, 12.2, 13.3, 14.4, 15.5]]", encoded2)
Beispiel #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))
Beispiel #4
0
    def test_encode_decode_auto_params(self):
        parameters = params_util.default_parameters(10, 30)
        encoded = "[[1.31, 6.2, -8.3, 4.4000000000000004], [4.1, 2.88, 773.3, 14, -34]]"
        self.assertEqual(
            True, Segmentation.decode_auto_params_into(parameters, encoded))

        encoded_back = Segmentation.encode_auto_params_from_all_params(
            parameters)
        self.assertEqual(encoded, encoded_back)
Beispiel #5
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))
Beispiel #6
0
 def test_decode_auto_params_invalid(self):
     parameters = params_util.default_parameters(10, 30)
     encoded_params_invalid = "[[11.1, 12.2, 13.3, 14.4, 15.5], [1.1, 2.2, 3.3, 4.4]]"
     self.assertEqual(
         False,
         Segmentation.decode_auto_params_into(parameters,
                                              encoded_params_invalid))
Beispiel #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)
Beispiel #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))
Beispiel #9
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))
Beispiel #10
0
    def test_encode_auto_params(self):
        parameters = params_util.default_parameters(10, 30)

        star_parameters = parameters['segmentation']['stars']
        star_parameters['brightnessWeight'] = 1.1
        star_parameters['cumBrightnessWeight'] = 2.2
        star_parameters['gradientWeight'] = 3.3
        star_parameters['sizeWeight'] = 4.4

        rank_parameters = parameters['segmentation']['ranking']
        rank_parameters['avgBorderBrightnessWeight'] = 11.1
        rank_parameters['avgInnerBrightnessWeight'] = 12.2
        rank_parameters['avgInnerDarknessWeight'] = 13.3
        rank_parameters['logAreaBonus'] = 14.4
        rank_parameters['maxInnerBrightnessWeight'] = 15.5

        encoded = Segmentation.encode_auto_params_from_all_params(parameters)
        self.assertEqual("[[1.1, 2.2, 3.3, 4.4000000000000004], [11.1, 12.2, 13.3, 14.4, 15.5]]", encoded)

        # check list size weight
        star_parameters['sizeWeight'] = [2, 5, 11]
        encoded2 = Segmentation.encode_auto_params_from_all_params(parameters)
        self.assertEqual("[[1.1, 2.2, 3.3, 6.0], [11.1, 12.2, 13.3, 14.4, 15.5]]", encoded2)
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)
Beispiel #12
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)
Beispiel #13
0
    def test_decode_auto_params_valid(self):
        parameters = params_util.default_parameters(10, 30)
        encoded_params = "[[1.1, 2.2, 3.3, 4.4], [11.1, 12.2, 13.3, 14.4, 15.5]]"
        self.assertEqual(True, Segmentation.decode_auto_params_into(parameters, encoded_params))

        # check all the fields
        star_parameters = parameters['segmentation']['stars']
        self.assertEqual(1.1, star_parameters['brightnessWeight'])
        self.assertEqual(2.2, star_parameters['cumBrightnessWeight'])
        self.assertEqual(3.3, star_parameters['gradientWeight'])
        self.assertEqual(4.4, np.mean(star_parameters['sizeWeight']))

        rank_parameters = parameters['segmentation']['ranking']
        self.assertEqual(11.1, rank_parameters['avgBorderBrightnessWeight'])
        self.assertEqual(12.2, rank_parameters['avgInnerBrightnessWeight'])
        self.assertEqual(13.3, rank_parameters['avgInnerDarknessWeight'])
        self.assertEqual(14.4, rank_parameters['logAreaBonus'])
        self.assertEqual(15.5, rank_parameters['maxInnerBrightnessWeight'])
Beispiel #14
0
    def test_decode_auto_params_valid(self):
        parameters = params_util.default_parameters(10, 30)
        encoded_params = "[[1.1, 2.2, 3.3, 4.4], [11.1, 12.2, 13.3, 14.4, 15.5]]"
        self.assertEqual(
            True,
            Segmentation.decode_auto_params_into(parameters, encoded_params))

        # check all the fields
        star_parameters = parameters['segmentation']['stars']
        self.assertEqual(1.1, star_parameters['brightnessWeight'])
        self.assertEqual(2.2, star_parameters['cumBrightnessWeight'])
        self.assertEqual(3.3, star_parameters['gradientWeight'])
        self.assertEqual(4.4, np.mean(star_parameters['sizeWeight']))

        rank_parameters = parameters['segmentation']['ranking']
        self.assertEqual(11.1, rank_parameters['avgBorderBrightnessWeight'])
        self.assertEqual(12.2, rank_parameters['avgInnerBrightnessWeight'])
        self.assertEqual(13.3, rank_parameters['avgInnerDarknessWeight'])
        self.assertEqual(14.4, rank_parameters['logAreaBonus'])
        self.assertEqual(15.5, rank_parameters['maxInnerBrightnessWeight'])
Beispiel #15
0
 def __init__(self, image, images, ui, cell_star, params=None):
     """
     @type cell_star: Segmentation
     """
     self.image = image
     self.images = images
     self.ui = ui
     self.ui.onclick = self.manage_click
     self.ui.press = self.manage_press
     if cell_star is None:
         self.cell_star = Segmentation(
             11, params["segmentation"]["avgCellDiameter"])
         self.cellstar.parameters = params
         self.cellstar.set_frame(image)
         self.cellstar.images = images
         self.image = self.cellstar.images.image
         self.images = self.cellstar.images
     else:
         self.cell_star = cell_star
     self.clear()
     self.smoothing = self.cell_star_smoothing
     self.stick_seeds = []
     self.stick_snakes = []
Beispiel #16
0
        image_path,
        mask_path,
        precision,
        avg_cell_diameter,
        method,
        options=options,
        callback_progress=callback_progress)
    complete_params, _, _ = test_rank_pf(image_path,
                                         mask_path,
                                         precision,
                                         avg_cell_diameter,
                                         method,
                                         initial_params=full_params_contour,
                                         options=options,
                                         callback_progress=callback_progress)

    #complete_params["segmentation"]["ranking"]["stickingWeight"] = 60.0

    print "Best_params:", complete_params
    print
    print "CellProfiler autoparams:", Segmentation.encode_auto_params_from_all_params(
        complete_params)

    debug_util.DEBUGING = True
    test_pf.test_parameters(image_path,
                            mask_path,
                            precision,
                            avg_cell_diameter,
                            complete_params,
                            image_result_path,
                            options=options)
Beispiel #17
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))
Beispiel #18
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))
Beispiel #19
0
    # from cellstar.core.snake import Snake
    # from cellstar.tests.experiments import smooth_contour_turns
    # Snake.smooth_contour = smooth_contour_turns

    pf_rank.get_max_workers = lambda: 2
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    logger = logging.getLogger('cellstar.parameter_fitting')
    logger.setLevel(logging.DEBUG)
    logger.addHandler(ch)
    test_pf.default_data_path = sys.argv[1]

    precision = int(sys.argv[4])
    avg_cell_diameter = float(sys.argv[5])

    image_result_path = None
    if len(sys.argv) >= 8:
        image_result_path = sys.argv[7]

    # complete_params = default_parameters(segmentation_precision=precision, avg_cell_diameter=avg_cell_diameter)
    complete_params, _, _ = test_rank_pf(sys.argv[2], sys.argv[3], precision, avg_cell_diameter, sys.argv[6])

    print "Best_params:", complete_params
    print
    print "CellProfiler autoparams:", Segmentation.encode_auto_params_from_all_params(complete_params)

    debug_util.DEBUGING = True
    if image_result_path is not None:
        test_pf.test_parameters(sys.argv[2], sys.argv[3], precision, avg_cell_diameter, complete_params,
                                image_result_path)
Beispiel #20
0
    if len(sys.argv) < 2:
        print "Usage: <script> input_path [cell_diameter]"
        print "Given: " + " ".join(sys.argv)

    average_cell_diameter = 35
    if len(sys.argv) > 2:
        average_cell_diameter = int(sys.argv[2])

    debug_util.DEBUGING = True
    input_path = sys.argv[1]
    # image2 = load_image("D:\Fafa\Drozdze\CellStarTesting\Data\Benchmark1\TestSet6\BF_frame001_invert.tif")

    image = load_image(input_path)
    # image3 = 1 - image
    # p2 = debug_util.image_save(image2, "Image2_Invert")

    # p1 = debug_util.image_save(image, "Image1")
    # read2 = load_image(p2)

    # read1 = load_image(p1)

    cell_star = Segmentation(12, average_cell_diameter)
    cellstar.set_frame(image)
    cellstar.pre_process()

    app = wx.App(0)
    explorer_ui = ExplorerFrame(cellstar.images)
    explorer = Explorer(image, cellstar.images, explorer_ui, cell_star)
    app.MainLoop()
Beispiel #21
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))
Beispiel #22
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)
Beispiel #23
0
 def test_decode_auto_params_invalid(self):
     parameters = params_util.default_parameters(10, 30)
     encoded_params_invalid = "[[11.1, 12.2, 13.3, 14.4, 15.5], [1.1, 2.2, 3.3, 4.4]]"
     self.assertEqual(False, Segmentation.decode_auto_params_into(parameters, encoded_params_invalid))