def init_from_edges(cls, points, edges, labels_to_masks, copy=True, skip_checks=False): r""" Construct a :map:`LabelledPointUndirectedGraph` from an edges array. See :map:`PointUndirectedGraph` for more information. Parameters ---------- points : ``(n_vertices, n_dims, )`` `ndarray` The array of point locations. edges : ``(n_edges, 2, )`` `ndarray` or ``None`` The `ndarray` of edges, i.e. all the pairs of vertices that are connected with an edge. If ``None``, then an empty adjacency matrix is created. labels_to_masks : `ordereddict` `{str -> bool ndarray}` For each label, the mask that specifies the indices in to the points that belong to the label. copy : `bool`, optional If ``False``, the `adjacency_matrix` will not be copied on assignment. skip_checks : `bool`, optional If ``True``, no checks will be performed. """ adjacency_matrix = _convert_edges_to_symmetric_adjacency_matrix( edges, points.shape[0]) return cls(points, adjacency_matrix, labels_to_masks, copy=copy, skip_checks=skip_checks)
def init_from_edges(cls, points, edges, labels_to_masks, copy=True, skip_checks=False): r""" Construct a :map:`LabelledPointUndirectedGraph` from an edges array. See :map:`PointUndirectedGraph` for more information. Parameters ---------- points : ``(n_vertices, n_dims, )`` `ndarray` The array of point locations. edges : ``(n_edges, 2, )`` `ndarray` or ``None`` The `ndarray` of edges, i.e. all the pairs of vertices that are connected with an edge. If ``None``, then an empty adjacency matrix is created. labels_to_masks : `ordereddict` `{str -> bool ndarray}` For each label, the mask that specifies the indices in to the points that belong to the label. copy : `bool`, optional If ``False``, the `adjacency_matrix` will not be copied on assignment. skip_checks : `bool`, optional If ``True``, no checks will be performed. """ adjacency_matrix = _convert_edges_to_symmetric_adjacency_matrix( edges, points.shape[0]) return cls(points, adjacency_matrix, labels_to_masks, copy=copy, skip_checks=skip_checks)
def __setstate__(self, state_dict): # TODO: Deprecate this - this handles importing old-style LandmarkGroup if '_pointcloud' in state_dict: from menpo.base import MenpoDeprecationWarning warnings.warn('menpo.landmark.LandmarkGroup is now deprecated and ' 'has been moved to menpo.shape.LandmarkGroup.', MenpoDeprecationWarning) _pointcloud = state_dict.pop('_pointcloud') state_dict['points'] = _pointcloud.points if type(_pointcloud) == PointCloud: adj_mat = _convert_edges_to_symmetric_adjacency_matrix( [], _pointcloud.shape[0]) elif isinstance(_pointcloud, PointGraph): a = _pointcloud.adjacency_matrix # Ensure that the matrix is symmetric adj_mat = a.maximum(a.T) elif isinstance(_pointcloud, TriMesh): warnings.warn('menpo.landmark.LandmarkGroup is now deprecated.' 'The underlying ._pointcloud was a ' 'menpo.shape.TriMesh and this has been cast down ' 'to an UndirectedPointGraph subclass.') adj_mat = _pointcloud.as_pointgraph(copy=False).adjacency_matrix else: raise ValueError('Unexpected PointCloud type ({})'.format( type(_pointcloud))) state_dict['adjacency_matrix'] = adj_mat self.__dict__.update(state_dict)
def init_from_indices_mapping(cls, points, adjacency, labels_to_indices, copy=True): r""" Static constructor to create a :map:`LabelledPointUndirectedGraph` from an ordered dictionary that maps a set of indices . Parameters ---------- points : :map:`PointCloud` The points representing the landmarks. adjacency : ``(n_vertices, n_vertices, )`` `ndarray`, `csr_matrix` or `list` of edges The adjacency matrix of the graph, or a list of edges representing adjacency. labels_to_indices : `ordereddict` {`str` -> `int ndarray`} For each label, the indices in to the points that belong to the label. copy : `boolean`, optional If ``True``, a copy of the data is stored on the group. Returns ------- labelled_pointgraph : :map:`LabelledPointUndirectedGraph` Labelled point undirected graph wrapping the given points with the given semantic labels applied. Raises ------ ValueError If `dict` passed instead of `OrderedDict` ValueError If any of the label masks differs in size to the points. ValueError If there exists any point in the points that is not covered by a label. """ adjacency = np.array(adjacency) if adjacency.shape[0] != adjacency.shape[1] and adjacency.shape[1] == 2: adjacency = _convert_edges_to_symmetric_adjacency_matrix( adjacency, points.shape[0]) labels_to_masks = indices_to_masks(labels_to_indices, points.shape[0]) return LabelledPointUndirectedGraph(points, adjacency, labels_to_masks, copy=copy)
def init_from_indices_mapping(cls, points, adjacency, labels_to_indices, copy=True): r""" Static constructor to create a :map:`LabelledPointUndirectedGraph` from an ordered dictionary that maps a set of indices . Parameters ---------- points : :map:`PointCloud` The points representing the landmarks. adjacency : ``(n_vertices, n_vertices, )`` `ndarray`, `csr_matrix` or `list` of edges The adjacency matrix of the graph, or a list of edges representing adjacency. labels_to_indices : `ordereddict` {`str` -> `int ndarray`} For each label, the indices in to the points that belong to the label. copy : `boolean`, optional If ``True``, a copy of the data is stored on the group. Returns ------- labelled_pointgraph : :map:`LabelledPointUndirectedGraph` Labelled point undirected graph wrapping the given points with the given semantic labels applied. Raises ------ ValueError If `dict` passed instead of `OrderedDict` ValueError If any of the label masks differs in size to the points. ValueError If there exists any point in the points that is not covered by a label. """ adjacency = np.array(adjacency) if adjacency.shape[0] != adjacency.shape[1] and adjacency.shape[1] == 2: adjacency = _convert_edges_to_symmetric_adjacency_matrix( adjacency, points.shape[0]) labels_to_masks = indices_to_masks(labels_to_indices, points.shape[0]) return LabelledPointUndirectedGraph(points, adjacency, labels_to_masks, copy=copy)
def __setstate__(self, state_dict): # TODO: Deprecate this - this handles importing old-style LandmarkGroup if "_pointcloud" in state_dict: from menpo.base import MenpoDeprecationWarning warnings.warn( "menpo.landmark.LandmarkGroup is now deprecated and " "has been moved to menpo.shape.LandmarkGroup.", MenpoDeprecationWarning, ) _pointcloud = state_dict.pop("_pointcloud") state_dict["points"] = _pointcloud.points # the shape on old landmarks *itself* was allowed to have landmarks # (of course it was very frequently None though, see # https://github.com/menpo/menpo/blob/v0.7.7/menpo/landmark/base.py#L24) # In the new word, self has the same behavior, so move the # landmarks across here. # In the vast majority of cases, this will simply be None. state_dict["_landmarks"] = _pointcloud._landmarks if type(_pointcloud) == PointCloud: adj_mat = _convert_edges_to_symmetric_adjacency_matrix( [], _pointcloud.n_points) elif isinstance(_pointcloud, PointGraph): a = _pointcloud.adjacency_matrix # Ensure that the matrix is symmetric adj_mat = a.maximum(a.T) elif isinstance(_pointcloud, TriMesh): warnings.warn( "menpo.landmark.LandmarkGroup is now deprecated." "The underlying ._pointcloud was a " "menpo.shape.TriMesh and this has been cast down " "to an UndirectedPointGraph subclass.") adj_mat = _pointcloud.as_pointgraph( copy=False).adjacency_matrix else: raise ValueError("Unexpected PointCloud type ({})".format( type(_pointcloud))) state_dict["adjacency_matrix"] = adj_mat self.__dict__.update(state_dict)
def __setstate__(self, state_dict): # TODO: Deprecate this - this handles importing old-style LandmarkGroup if '_pointcloud' in state_dict: from menpo.base import MenpoDeprecationWarning warnings.warn('menpo.landmark.LandmarkGroup is now deprecated and ' 'has been moved to menpo.shape.LandmarkGroup.', MenpoDeprecationWarning) _pointcloud = state_dict.pop('_pointcloud') state_dict['points'] = _pointcloud.points # the shape on old landmarks *itself* was allowed to have landmarks # (of course it was very frequently None though, see # https://github.com/menpo/menpo/blob/v0.7.7/menpo/landmark/base.py#L24) # In the new word, self has the same behavior, so move the # landmarks across here. # In the vast majority of cases, this will simply be None. state_dict['_landmarks'] = _pointcloud._landmarks if type(_pointcloud) == PointCloud: adj_mat = _convert_edges_to_symmetric_adjacency_matrix( [], _pointcloud.n_points) elif isinstance(_pointcloud, PointGraph): a = _pointcloud.adjacency_matrix # Ensure that the matrix is symmetric adj_mat = a.maximum(a.T) elif isinstance(_pointcloud, TriMesh): warnings.warn('menpo.landmark.LandmarkGroup is now deprecated.' 'The underlying ._pointcloud was a ' 'menpo.shape.TriMesh and this has been cast down ' 'to an UndirectedPointGraph subclass.') adj_mat = _pointcloud.as_pointgraph(copy=False).adjacency_matrix else: raise ValueError('Unexpected PointCloud type ({})'.format( type(_pointcloud))) state_dict['adjacency_matrix'] = adj_mat self.__dict__.update(state_dict)