How to use the merlin.util.spatialfeature.SpatialFeature function in merlin

To help you get started, we’ve selected a few merlin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github emanuega / MERlin / test / test_spatialfeature.py View on Github external
testCoords2 = [(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]

feature1 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords1)]], 0)
feature2 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords2)]], 0)
feature3 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords1)],
                                          [geometry.Polygon(testCoords1)]],
                                         0, zCoordinates=np.array([0, 0.5]))
feature4 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords1)],
                                          [geometry.Polygon(testCoords2)]],
                                         0, zCoordinates=np.array([0, 0.5]))

p1 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])]], 0)
p2 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 0.5), (0, 1), (1, 1), (1, 0.5)])]], 0)
p3 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 0.5), (0, 1.5), (1, 1.5), (1, 0.5)])]], 0)
p4 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 1), (0, 2), (1, 2), (1, 1)])]], 0)
p5 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(4, 4), (4, 5), (5, 5), (5, 4)])]], 0)
allCells = [p1, p2, p3, p4, p5]


def test_feature_from_label_matrix():
    testLabels = np.zeros((1, 4, 4))
    testLabels[0, 1:3, 1:3] = 1

    feature = spatialfeature.SpatialFeature.feature_from_label_matrix(
        testLabels, 0)

    assert len(feature.get_boundaries()[0]) == 1
github emanuega / MERlin / test / test_spatialfeature.py View on Github external
feature3 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords1)],
                                          [geometry.Polygon(testCoords1)]],
                                         0, zCoordinates=np.array([0, 0.5]))
feature4 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords1)],
                                          [geometry.Polygon(testCoords2)]],
                                         0, zCoordinates=np.array([0, 0.5]))

p1 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])]], 0)
p2 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 0.5), (0, 1), (1, 1), (1, 0.5)])]], 0)
p3 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 0.5), (0, 1.5), (1, 1.5), (1, 0.5)])]], 0)
p4 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 1), (0, 2), (1, 2), (1, 1)])]], 0)
p5 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(4, 4), (4, 5), (5, 5), (5, 4)])]], 0)
allCells = [p1, p2, p3, p4, p5]


def test_feature_from_label_matrix():
    testLabels = np.zeros((1, 4, 4))
    testLabels[0, 1:3, 1:3] = 1

    feature = spatialfeature.SpatialFeature.feature_from_label_matrix(
        testLabels, 0)

    assert len(feature.get_boundaries()[0]) == 1
    assert feature.get_boundaries()[0][0].equals(geometry.Polygon(
        list(zip([2.1, 2.1, 2.0, 1.0, 0.9, 0.9, 1.0, 2.0, 2.1],
                 [2.0, 1.0, 0.9, 0.9, 1.0, 2.0, 2.1, 2.1, 2.0]))))
github emanuega / MERlin / test / test_spatialfeature.py View on Github external
from merlin.util import spatialfeature


testCoords1 = [(1, 1), (1, 2), (2, 2), (2, 1), (1, 1)]
testCoords2 = [(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]

feature1 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords1)]], 0)
feature2 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords2)]], 0)
feature3 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords1)],
                                          [geometry.Polygon(testCoords1)]],
                                         0, zCoordinates=np.array([0, 0.5]))
feature4 = spatialfeature.SpatialFeature([[geometry.Polygon(testCoords1)],
                                          [geometry.Polygon(testCoords2)]],
                                         0, zCoordinates=np.array([0, 0.5]))

p1 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])]], 0)
p2 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 0.5), (0, 1), (1, 1), (1, 0.5)])]], 0)
p3 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 0.5), (0, 1.5), (1, 1.5), (1, 0.5)])]], 0)
p4 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(0, 1), (0, 2), (1, 2), (1, 1)])]], 0)
p5 = spatialfeature.SpatialFeature(
    [[geometry.Polygon([(4, 4), (4, 5), (5, 5), (5, 4)])]], 0)
allCells = [p1, p2, p3, p4, p5]


def test_feature_from_label_matrix():
    testLabels = np.zeros((1, 4, 4))
    testLabels[0, 1:3, 1:3] = 1
github emanuega / MERlin / merlin / util / spatialfeature.py View on Github external
def read_features(self, fov: int = None) -> List[SpatialFeature]:
        if fov is None:
            raise NotImplementedError

        features = [SpatialFeature.from_json_dict(x)
                    for x in self._dataSet.load_json_analysis_result(
                'feature_metadata', self._analysisTask, fov, 'features')]

        return features
github emanuega / MERlin / merlin / util / spatialfeature.py View on Github external
def _load_feature_from_hdf5_group(h5Group):
        zCount = len([x for x in h5Group.keys() if x.startswith('zIndex_')])
        boundaryList = []
        for z in range(zCount):
            zBoundaryList = []
            zGroup = h5Group['zIndex_' + str(z)]
            pCount = len([x for x in zGroup.keys() if x[:2] == 'p_'])
            for p in range(pCount):
                zBoundaryList.append(
                    HDF5SpatialFeatureDB._load_geometry_from_hdf5_group(
                        zGroup['p_' + str(p)]))
            boundaryList.append(zBoundaryList)

        loadedFeature = SpatialFeature(
            boundaryList,
            h5Group.attrs['fov'],
            np.array(h5Group['z_coordinates']),
            int(h5Group.attrs['id']))

        return loadedFeature
github emanuega / MERlin / merlin / util / spatialfeature.py View on Github external
transformationMatrix: a 3x3 numpy array specifying the
                transformation from fov to global coordinates. If None,
                the feature coordinates are not transformed.
            zCoordinates: the z position for each of the z indexes. If not
                specified, each z index is assumed to have unit height.
        Returns: the new feature
        """

        boundaries = [SpatialFeature._extract_boundaries(x)
                      for x in labelMatrix]

        if transformationMatrix is not None:
            boundaries = [SpatialFeature._transform_boundaries(
                x, transformationMatrix) for x in boundaries]

        return SpatialFeature([SpatialFeature._remove_invalid_boundaries(
            SpatialFeature._remove_interior_boundaries(
                [geometry.Polygon(x) for x in b if len(x) > 2]))
                               for b in boundaries], fov, zCoordinates)
github emanuega / MERlin / merlin / util / spatialfeature.py View on Github external
feature should have a value of False.
            fov: the index of the field of view corresponding to the
                label matrix.
            transformationMatrix: a 3x3 numpy array specifying the
                transformation from fov to global coordinates. If None,
                the feature coordinates are not transformed.
            zCoordinates: the z position for each of the z indexes. If not
                specified, each z index is assumed to have unit height.
        Returns: the new feature
        """

        boundaries = [SpatialFeature._extract_boundaries(x)
                      for x in labelMatrix]

        if transformationMatrix is not None:
            boundaries = [SpatialFeature._transform_boundaries(
                x, transformationMatrix) for x in boundaries]

        return SpatialFeature([SpatialFeature._remove_invalid_boundaries(
            SpatialFeature._remove_interior_boundaries(
                [geometry.Polygon(x) for x in b if len(x) > 2]))
                               for b in boundaries], fov, zCoordinates)
github emanuega / MERlin / merlin / analysis / segment.py View on Github external
membraneImages,
                            self.parameters['compartment_channel_name'],
                            self.parameters['membrane_channel_name'])

        # perform watershed in individual z positions
        watershedOutput = segmentation.apply_cv2_watershed(compartmentImages,
                                                           watershedMarkers)

        # combine all z positions in watershed
        watershedCombinedOutput = segmentation \
            .combine_2d_segmentation_masks_into_3d(watershedOutput)

        # get features from mask. This is the slowestart (6 min for the
        # previous part, 15+ for the rest, for a 7 frame Image.
        zPos = np.array(self.dataSet.get_data_organization().get_z_positions())
        featureList = [spatialfeature.SpatialFeature.feature_from_label_matrix(
            (watershedCombinedOutput == i), fragmentIndex,
            globalTask.fov_to_global_transform(fragmentIndex), zPos)
            for i in np.unique(watershedCombinedOutput) if i != 0]

        featureDB = self.get_feature_database()
        featureDB.write_features(featureList, fragmentIndex)