Пример #1
0
        def _split_disjoint_labels(label_pairs):
            """Helper function: map subvolumes so disconnected bodies are different labels.

            Function preserves partitioner.

            Args:
                label_pairs (rdd): RDD is of (subvolume id, data)
       
            Returns:
                Original RDD including mappings for gt and the test seg.
        
            """
            from DVIDSparkServices.reconutils.morpho import split_disconnected_bodies
            from DVIDSparkServices.sparkdvid.CompressedNumpyArray import CompressedNumpyArray
            
            subvolume, labelgtc, label2c = label_pairs

            # extract numpy arrays
            labelgt = labelgtc.deserialize()
            label2 = label2c.deserialize()

            # split bodies up
            labelgt_split, labelgt_map = split_disconnected_bodies(labelgt)
            label2_split, label2_map = split_disconnected_bodies(label2)
            
            # compress results
            return (subvolume, labelgt_map, label2_map,
                    CompressedNumpyArray(labelgt_split),
                    CompressedNumpyArray(label2_split))
        def connected_components(seg_chunk):
            from DVIDSparkServices.reconutils.morpho import split_disconnected_bodies

            _sid, (subvolume, seg) = seg_chunk
            seg_split, split_mapping = split_disconnected_bodies(seg)
            
            # If any zero bodies were split, don't give them new labels.
            # Make them zero again
            zero_split_mapping = dict( [k_v for k_v in split_mapping.items() if k_v[1] == 0] )
            if zero_split_mapping:
                vigra.analysis.applyMapping(seg_split, zero_split_mapping, allow_incomplete_mapping=True, out=seg_split)

            # renumber from one
            #
            # FIXME: The next version of vigra will have a function to do this in one step, like this:
            #        seg2 = vigra.analysis.relabelConsecutive( seg2,
            #                                                  start_label=1,
            #                                                  keep_zeros=True,
            #                                                  out=np.empty_like(seg_split, dtype=np.uint32) )
            seg_split = vigra.taggedView(seg_split, 'zyx')
            vals = numpy.sort( vigra.analysis.unique(seg_split) )
            if vals[0] == 0:
                # Leave zero-pixels alone
                remap = dict(zip(vals, range(len(vals))))
            else:
                remap = dict(zip(vals, range(1, 1+len(vals))))

            out_seg = numpy.empty_like(seg_split, dtype=numpy.uint32)
            vigra.analysis.applyMapping(seg_split, remap, out=out_seg)
            return (subvolume, (out_seg, out_seg.max()))
Пример #3
0
 def test_new_bodies_start_with_the_max(self):
     x = np.array([[[4, 7, 4]]], dtype=np.dtype("uint32"))
     array_result, map_result = split_disconnected_bodies(x)
     expected_array = np.array([[[4, 7, 8]]], dtype=np.dtype("uint32"))
     np.testing.assert_array_equal(expected_array, array_result)
     expected_map = {4: 4, 8: 4}
     self.assertDictEqual(expected_map, map_result)
Пример #4
0
 def test_splits_bodies(self):
     x = np.array([[[1, 0, 1]]], dtype=np.dtype("uint32"))
     array_result, map_result = split_disconnected_bodies(x)
     expected_array = np.array([[[1, 0, 2]]], dtype=np.dtype("uint32"))
     np.testing.assert_array_equal(expected_array, array_result)
     expected_map = {1: 1, 2: 1}
     self.assertDictEqual(expected_map, map_result)
Пример #5
0
        def _split_disjoint_labels(label_pairs):
            """Helper function: map subvolumes so disconnected bodies are different labels.

            Function preserves partitioner.

            Args:
                label_pairs (rdd): RDD is of (subvolume id, data)
       
            Returns:
                Original RDD including mappings for gt and the test seg.
        
            """
            from DVIDSparkServices.reconutils.morpho import split_disconnected_bodies
            
            subvolume, labelgt, label2 = label_pairs

            # split bodies up
            labelgt_split, labelgt_map = split_disconnected_bodies(labelgt)
            label2_split, label2_map = split_disconnected_bodies(label2)
            
            # compress results
            return (subvolume, labelgt_map, label2_map, labelgt_split, label2_split)
Пример #6
0
    def test_basic(self):

        _ = 2  # for readability in the array below

        # Note that we multiply these by 10 for this test!
        orig = [
            [1, 1, 1, 1, _, _, 3, 3, 3, 3],
            [1, 1, 1, 1, _, _, 3, 3, 3, 3],
            [1, 1, 1, 1, _, _, 3, 3, 3, 3],
            [1, 1, 1, 1, _, _, 3, 3, 3, 3],
            [_, _, _, _, _, _, _, _, _, _],
            [0, 0, _, _, 4, 4, _, _, 0,
             0],  # Note that the zeros here will not be touched.
            [_, _, _, _, 4, 4, _, _, _, _],
            [1, 1, 1, _, _, _, _, 3, 3, 3],
            [1, 1, 1, _, 1, 1, _, 3, 3, 3],
            [1, 1, 1, _, 1, 1, _, 3, 3, 3]
        ]

        orig = np.array(orig).astype(np.uint64)
        orig *= 10

        split, mapping = split_disconnected_bodies(orig)

        # Due to undocumented requirements in Evaluate.py, the mapping
        # returned by split_disconnected_bodies() must contain plain Python integers,
        # not np.uint64, etc.
        assert all(type(k) == int and type(v) == int for k,v in mapping.items()), \
            "Returned mapping does not contain plain Python int types"

        assert ((orig == 20) == (split == 20)).all(), \
            "Label 2 is a single component and therefore should remain untouched in the output"

        assert ((orig == 40) == (split == 40)).all(), \
            "Label 4 is a single component and therefore should remain untouched in the output"

        assert (split[:4,:4] == 10).all(), \
            "The largest segment in each split object is supposed to keep it's label"
        assert (split[:4,-4:] == 30).all(), \
            "The largest segment in each split object is supposed to keep it's label"

        lower_left_label = split[-1, 1]
        lower_right_label = split[-1, -1]
        bottom_center_label = split[-1, 5]

        assert lower_left_label != 10, "Split object was not relabeled"
        assert bottom_center_label != 10, "Split object was not relabeled"
        assert lower_right_label != 30, "Split object was not relabeled"

        assert lower_left_label in (
            41, 42,
            43), "New labels are supposed to be consecutive with the old"
        assert lower_right_label in (
            41, 42,
            43), "New labels are supposed to be consecutive with the old"
        assert bottom_center_label in (
            41, 42,
            43), "New labels are supposed to be consecutive with the old"

        assert (split[-3:, :3] == lower_left_label).all()
        assert (split[-3:, -3:] == lower_right_label).all()
        assert (split[-2:, 4:6] == bottom_center_label).all()

        assert set(mapping.keys()) == set([10, 30, 41, 42,
                                           43]), "mapping: {}".format(mapping)

        assert (vigra.analysis.applyMapping(split, mapping, allow_incomplete_mapping=True) == orig).all(), \
            "Applying mapping to the relabeled image did not recreate the original image."
Пример #7
0
 def test_works_with_big_values(self):
     BIG_INT = int(2 ** 50)
     x = np.array([[[BIG_INT, 0, BIG_INT]]], dtype=np.dtype("uint64"))
     array_result, map_result = split_disconnected_bodies(x)
     expected_array = np.array([[[BIG_INT, 0, BIG_INT + 1]]], dtype=np.dtype("uint64"))
     np.testing.assert_array_equal(expected_array, array_result)