def test_edge_mask_2d(self):
     np.random.seed(42)
     edge_mask = np.random.random((len(self.offsets_2d), ) +
                                  self.shape_2d) > 0.5
     g1 = ngraph.undirectedLongRangeGridGraph(
         list(self.shape_2d),
         self.offsets_2d,
         offsets_probabilities=self.offsets_probabilities_2d,
     )
     g2 = ngraph.undirectedLongRangeGridGraph(
         list(self.shape_2d),
         self.offsets_2d,
         edge_mask=edge_mask,
     )
    def test_undirected_long_range_grid_graph_2d(self):
        g = ngraph.undirectedLongRangeGridGraph(list(self.shape_2d),
                                                self.offsets_2d)
        self.assertTrue(g.numberOfNodes == 9)
        self.assertTrue(g.numberOfEdges == 9)

        # Call some extra methods:
        edges_ID = g.projectEdgesIDToPixels()
        nodes_ID = g.projectNodesIDToPixels()

        edge_values = g.edgeValues(
            np.random.random(self.shape_2d +
                             (len(self.offsets_2d), )).astype('float32'))
        node_values = g.nodeValues(
            np.random.random(self.shape_2d).astype('float32'))
Example #3
0
def build_pixel_lifted_graph_from_offsets(image_shape,
                                          offsets,
                                          label_image=None,
                                          GT_label_image=None,
                                          offsets_probabilities=None,
                                          offsets_weights=None,
                                          strides=None,
                                          nb_local_offsets=3,
                                          downscaling_factor=None,
                                          mask_used_edges=None):
    # TODO: why label_image...?
    """
    :param offsets: At the moment local offsets should be the first ones
    :param nb_local_offsets: UPDATE AND GENERALIZE!
    :param downscaling_factor: If a list [1,2,2] is given, then the image resolution is scaled down first
    """
    raise DeprecationWarning("User updated function in GASP repo")
    if downscaling_factor is not None:
        raise NotImplementedError()

    image_shape = tuple(image_shape) if not isinstance(image_shape,
                                                       tuple) else image_shape

    is_local_offset = np.zeros(offsets.shape[0], dtype='bool')
    is_local_offset[:nb_local_offsets] = True
    warnings.warn(
        "First {} offsets are assumed to be direct neighbors and the remaining ones long-range"
        .format(nb_local_offsets))
    if label_image is not None:
        assert image_shape == label_image.shape
        if offsets_weights is not None:
            print("Offset weights ignored...!")

    # TODO: change name offsets_probabilities
    # print("Actually building graph...")
    tick = time.time()
    graph = ngraph.undirectedLongRangeGridGraph(
        image_shape,
        offsets,
        is_local_offset,
        offsets_probabilities=offsets_probabilities,
        labels=label_image,
        strides=strides,
        mask_used_edges=mask_used_edges)
    nb_nodes = graph.numberOfNodes
    # print(label_image)
    # print(is_local_offset)
    # print(offsets_probabilities)

    if label_image is None:
        # print("Getting edge index...")
        offset_index = graph.edgeOffsetIndex()
        # print(np.unique(offset_index, return_counts=True))
        is_local_edge = np.empty_like(offset_index, dtype='bool')
        w = np.where(offset_index < nb_local_offsets)
        warnings.warn(
            "First {} offsets are assumed to be direct neighbors and the remaining ones long-range"
            .format(nb_local_offsets))
        is_local_edge[:] = 0
        is_local_edge[w] = 1
        # print("Nb. local edges: {} out of {}".format(is_local_edge.sum(), graph.numberOfEdges))
    else:
        # print("Took {} s!".format(time.time() - tick))
        # print("Checking edge locality...")
        is_local_edge = graph.findLocalEdges(label_image).astype('int32')

    if offsets_weights is None or label_image is not None:
        edge_sizes = np.ones(graph.numberOfEdges, dtype='int32')
    else:
        if isinstance(offsets_weights, (list, tuple)):
            offsets_weights = np.array(offsets_weights)
        assert offsets_weights.shape[0] == offsets.shape[0]

        if offsets_weights.ndim == len(image_shape) + 1:
            edge_sizes = graph.edgeValues(np.rollaxis(offsets_weights, 0, 4))
        else:
            if all([w >= 1.0 for w in offsets_weights]):
                # Take the inverse:
                offsets_weights = 1. / offsets_weights
            else:
                assert all([w <= 1.0 for w in offsets_weights]) and all(
                    [w >= 0.0 for w in offsets_weights])

            # print("Edge weights...")
            edge_sizes = offsets_weights[offset_index.astype('int32')]

    if GT_label_image is None:
        GT_labels_nodes = np.zeros(nb_nodes, dtype=np.int64)
    else:
        assert GT_label_image.shape == image_shape
        GT_labels_image = GT_label_image.astype(np.uint64)
        GT_labels_nodes = graph.nodeValues(GT_labels_image)

    return graph, is_local_edge, GT_labels_nodes, edge_sizes
Example #4
0
def build_pixel_lifted_graph_from_offsets(image_shape,
                                          offsets,
                                          label_image=None,
                                          GT_label_image=None,
                                          offsets_probabilities=None,
                                          offsets_weights=None,
                                          strides=None,
                                          nb_local_offsets=3,
                                          downscaling_factor=None,
                                          mask_used_edges=None):
    if downscaling_factor is not None:
        raise NotImplementedError()

    image_shape = tuple(image_shape) if not isinstance(image_shape,
                                                       tuple) else image_shape

    is_local_offset = np.zeros(offsets.shape[0], dtype='bool')
    is_local_offset[:nb_local_offsets] = True
    if label_image is not None:
        assert image_shape == label_image.shape
        if offsets_weights is not None:
            print("Offset weights ignored...!")

    graph = ngraph.undirectedLongRangeGridGraph(
        image_shape,
        offsets,
        is_local_offset,
        offsets_probabilities=offsets_probabilities,
        labels=label_image,
        strides=strides,
        mask_used_edges=mask_used_edges)
    nb_nodes = graph.numberOfNodes

    if label_image is None:
        offset_index = graph.edgeOffsetIndex()
        is_local_edge = np.empty_like(offset_index, dtype='bool')
        w = np.where(offset_index < nb_local_offsets)
        is_local_edge[:] = 0
        is_local_edge[w] = 1
    else:
        is_local_edge = graph.findLocalEdges(label_image).astype('int32')

    if offsets_weights is None or label_image is not None:
        edge_sizes = np.ones(graph.numberOfEdges, dtype='int32')
    else:
        if isinstance(offsets_weights, (list, tuple)):
            offsets_weights = np.array(offsets_weights)
        assert offsets_weights.shape[0] == offsets.shape[0]

        if offsets_weights.ndim == len(image_shape) + 1:
            edge_sizes = graph.edgeValues(np.rollaxis(offsets_weights, 0, 4))
        else:
            if all([w >= 1.0 for w in offsets_weights]):
                # Take the inverse:
                offsets_weights = 1. / offsets_weights
            else:
                assert all([w <= 1.0 for w in offsets_weights]) and all(
                    [w >= 0.0 for w in offsets_weights])

            edge_sizes = offsets_weights[offset_index.astype('int32')]

    if GT_label_image is None:
        GT_labels_nodes = np.zeros(nb_nodes, dtype=np.int64)
    else:
        assert GT_label_image.shape == image_shape
        GT_labels_image = GT_label_image.astype(np.uint64)
        GT_labels_nodes = graph.nodeValues(GT_labels_image)

    return graph, is_local_edge, GT_labels_nodes, edge_sizes