/
graph.py
1475 lines (1245 loc) · 48.3 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
from . import PointCloud
from .adjacency import (mask_adjacency_array, mask_adjacency_array_tree,
reindex_adjacency_array)
class Graph(object):
r"""
Abstract class for Graph definitions and manipulation.
Parameters
----------
adjacency_array : ``(n_edges, 2, )`` `ndarray`
The Adjacency Array of the graph, i.e. an array containing the sets of
the graph's edges. The numbering of vertices is assumed to start from 0.
For an undirected graph, the order of an edge's vertices doesn't matter,
for example
::
|---0---| adjacency_array = ndarray([[0, 1],
| | [0, 2],
| | [1, 2],
1-------2 [1, 3],
| | [2, 4],
| | [3, 4],
3-------4 [3, 5]])
|
5
For a directed graph, we assume that the vertices in the first column of
the adjacency_array are the fathers and the vertices in the second
column of the adjacency_array are the children, for example
::
|-->0<--| adjacency_array = ndarray([[1, 0],
| | [2, 0],
| | [1, 2],
1<----->2 [2, 1],
| | [1, 3],
v v [2, 4],
3------>4 [3, 4],
| [3, 5]])
v
5
copy : `bool`, optional
If ``False``, the ``adjacency_list`` will not be copied on assignment.
Raises
------
ValueError
You must provide at least one edge.
ValueError
Adjacency list must contain the sets of connected edges and thus must
have shape (n_edges, 2).
ValueError
The vertices must be numbered starting from 0.
"""
def __init__(self, adjacency_array, copy=True):
# check that adjacency_array has expected shape
if adjacency_array.size == 0:
raise ValueError('You must provide at least one edge.')
if adjacency_array.shape[1] != 2:
raise ValueError('Adjacency list must contain the sets of '
'connected edges and thus must have shape '
'(n_edges, 2).')
# check that numbering of vertices is zero-based
if adjacency_array.min() != 0:
raise ValueError('The vertices must be numbered starting from 0.')
# keep unique rows of adjacency_array
adjacency_array = _unique_array_rows(adjacency_array)
if copy:
self.adjacency_array = adjacency_array.copy()
else:
self.adjacency_array = adjacency_array
self.adjacency_list = self._get_adjacency_list()
@property
def n_edges(self):
r"""
Returns the number of the graph's edges.
:type: `int`
"""
return self.adjacency_array.shape[0]
@property
def n_vertices(self):
r"""
Returns the number of the graph's vertices.
:type: `int`
"""
return self.adjacency_array.max() + 1
def get_adjacency_matrix(self):
r"""
Returns the Adjacency Matrix of the graph, i.e. the boolean ndarray that
with True and False if there is an edge connecting the two vertices or
not respectively.
:type: ``(n_vertices, n_vertices, )`` `ndarray`
"""
pass
def _get_adjacency_list(self):
r"""
Returns the Adjacency List of the graph, i.e. a list of length
n_vertices that for each vertex has a list of the vertex neighbours.
If the graph is directed, the neighbours are children.
:type: `list` of `lists` of len n_vertices
"""
pass
def find_path(self, start, end, path=[]):
r"""
Returns a list with the first path (without cycles) found from start
vertex to end vertex.
Parameters
----------
start : `int`
The vertex from which the path starts.
end : `int`
The vertex from which the path ends.
Returns
-------
path : `list`
The path's vertices.
"""
path = path + [start]
if start == end:
return path
if start > self.n_vertices - 1 or start < 0:
return None
for v in self.adjacency_list[start]:
if v not in path:
newpath = self.find_path(v, end, path)
if newpath:
return newpath
return None
def find_all_paths(self, start, end, path=[]):
r"""
Returns a list of lists with all the paths (without cycles) found from
start vertex to end vertex.
Parameters
----------
start : `int`
The vertex from which the paths start.
end : `int`
The vertex from which the paths end.
Returns
-------
paths : `list` of `list`
The list containing all the paths from start to end.
"""
path = path + [start]
if start == end:
return [path]
if start > self.n_vertices - 1 or start < 0:
return []
paths = []
for v in self.adjacency_list[start]:
if v not in path:
newpaths = self.find_all_paths(v, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
def n_paths(self, start, end):
r"""
Returns the number of all the paths (without cycles) existing from
start vertex to end vertex.
Parameters
----------
start : `int`
The vertex from which the paths start.
end : `int`
The vertex from which the paths end.
Returns
-------
paths : `int`
The paths' numbers.
"""
return len(self.find_all_paths(start, end))
def find_shortest_path(self, start, end, path=[]):
r"""
Returns a list with the shortest path (without cycles) found from start
vertex to end vertex.
Parameters
----------
start : `int`
The vertex from which the path starts.
end : `int`
The vertex from which the path ends.
Returns
-------
path : `list`
The shortest path's vertices.
"""
path = path + [start]
if start == end:
return path
if start > self.n_vertices - 1 or start < 0:
return None
shortest = None
for v in self.adjacency_list[start]:
if v not in path:
newpath = self.find_shortest_path(v, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
def has_cycles(self):
r"""
Checks if the graph has at least one cycle.
"""
pass
def is_tree(self):
r"""
Checks if the graph is tree.
"""
return not self.has_cycles() and self.n_edges == self.n_vertices - 1
def _check_vertex(self, vertex):
r"""
Checks that a given vertex is valid.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
if vertex > self.n_vertices - 1 or vertex < 0:
raise ValueError('The vertex must be between '
'0 and {}.'.format(self.n_vertices-1))
def tojson(self):
r"""
Convert the graph to a dictionary JSON representation.
Returns
-------
dictionary with 'adjacency_array' key. Suitable or use in the by the
`json` standard library package.
"""
return {'adjacency_array': self.adjacency_array.tolist()}
class UndirectedGraph(Graph):
r"""
Class for Undirected Graph definition and manipulation.
Parameters
----------
adjacency_array : ``(n_edges, 2, )`` `ndarray`
The Adjacency Array of the graph, i.e. an array containing the sets of
the graph's edges. The numbering of vertices is assumed to start from 0.
For example:
::
|---0---| adjacency_array = ndarray([[0, 1],
| | [0, 2],
| | [1, 2],
1-------2 [1, 3],
| | [2, 4],
| | [3, 4],
3-------4 [3, 5]])
|
5
copy : `bool`, optional
If ``False``, the ``adjacency_list`` will not be copied on assignment.
Raises
------
ValueError
You must provide at least one edge.
ValueError
Adjacency list must contain the sets of connected edges and thus must
have shape (n_edges, 2).
ValueError
The vertices must be numbered starting from 0.
"""
def get_adjacency_matrix(self):
r"""
Returns the Adjacency Matrix of the graph, i.e. the boolean ndarray that
with True and False if there is an edge connecting the two vertices or
not respectively.
:type: ``(n_vertices, n_vertices, )`` `ndarray`
"""
adjacency_mat = np.zeros((self.n_vertices, self.n_vertices),
dtype=np.bool)
for e in range(self.n_edges):
v1 = self.adjacency_array[e, 0]
v2 = self.adjacency_array[e, 1]
adjacency_mat[v1, v2] = True
adjacency_mat[v2, v1] = True
return adjacency_mat
def _get_adjacency_list(self):
adjacency_list = [[] for _ in range(self.n_vertices)]
for e in range(self.n_edges):
v1 = self.adjacency_array[e, 0]
v2 = self.adjacency_array[e, 1]
adjacency_list[v1].append(v2)
adjacency_list[v2].append(v1)
return adjacency_list
def neighbours(self, vertex):
r"""
Returns the neighbours of the selected vertex.
Parameters
----------
vertex : `int`
The selected vertex.
Returns
-------
neighbours : `list`
The list of neighbours.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex)
return self.adjacency_list[vertex]
def n_neighbours(self, vertex):
r"""
Returns the number of neighbours of the selected vertex.
Parameters
----------
vertex : `int`
The selected vertex.
Returns
-------
n_neighbours : `int`
The number of neighbours.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex)
return len(self.neighbours(vertex))
def is_edge(self, vertex_1, vertex_2):
r"""
Returns whether there is an edge between the provided vertices.
Parameters
----------
vertex_1 : `int`
The first selected vertex.
vertex_2 : `int`
The second selected vertex.
Returns
-------
is_edge : `bool`
True if there is an edge.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex_1)
self._check_vertex(vertex_2)
return (vertex_1 in self.adjacency_list[vertex_2] or
vertex_2 in self.adjacency_list[vertex_1])
def has_cycles(self):
r"""
Whether the graph has at least on cycle.
Returns
-------
has_cycles : `bool`
True if it has at least one cycle.
"""
return _has_cycles(self.adjacency_list, False)
def minimum_spanning_tree(self, weights, root_vertex):
r"""
Returns the minimum spanning tree given weights to the graph's edges
using Kruskal's algorithm.
Parameters
----------
weights : ``(n_vertices, n_vertices, )`` `ndarray`
A matrix of the same size as the adjacency matrix that attaches a
weight to each edge of the undirected graph.
root_vertex : `int`
The vertex that will be set as root in the output MST.
Returns
-------
mst : :class:`menpo.shape.Tree`
The computed minimum spanning tree.
Raises
------
ValueError
Provided graph is not an UndirectedGraph.
ValueError
Assymetric weights provided.
"""
# compute the edges of the minimum spanning tree
from menpo.external.PADS.MinimumSpanningTree import MinimumSpanningTree
tree_edges = MinimumSpanningTree(self, weights)
# Correct the tree edges so that they have the correct format
# (i.e. ndarray of pairs in the form (parent, child)) using BFS
tree_edges = _correct_tree_edges(tree_edges, root_vertex)
return Tree(np.array(tree_edges), root_vertex)
def __str__(self):
return "Undirected graph of {} vertices and {} edges.".format(
self.n_vertices, self.n_edges)
class DirectedGraph(Graph):
r"""
Class for Directed Graph definition and manipulation.
Parameters
----------
adjacency_array : ``(n_edges, 2, )`` `ndarray`
The Adjacency Array of the graph, i.e. an array containing the sets of
the graph's edges. The numbering of vertices is assumed to start from 0.
We assume that the vertices in the first column of the `adjacency_array`
are the parents and the vertices in the second column of the
`adjacency_array` are the children, for example:
::
|-->0<--| adjacency_array = ndarray([[1, 0],
| | [2, 0],
| | [1, 2],
1<----->2 [2, 1],
| | [1, 3],
v v [2, 4],
3------>4 [3, 4],
| [3, 5]])
v
5
copy : `bool`, optional
If ``False``, the ``adjacency_list`` will not be copied on assignment.
Raises
------
ValueError
You must provide at least one edge.
ValueError
Adjacency list must contain the sets of connected edges and thus must
have shape (n_edges, 2).
ValueError
The vertices must be numbered starting from 0.
"""
def get_adjacency_matrix(self):
r"""
Returns the Adjacency Matrix of the graph, i.e. the boolean ndarray that
with True and False if there is an edge connecting the two vertices or
not respectively.
:type: ``(n_vertices, n_vertices, )`` `ndarray`
"""
adjacency_mat = np.zeros((self.n_vertices, self.n_vertices),
dtype=np.bool)
for e in range(self.n_edges):
parent = self.adjacency_array[e, 0]
child = self.adjacency_array[e, 1]
adjacency_mat[parent, child] = True
return adjacency_mat
def _get_adjacency_list(self):
adjacency_list = [[] for _ in range(self.n_vertices)]
for e in range(self.n_edges):
parent = self.adjacency_array[e, 0]
child = self.adjacency_array[e, 1]
adjacency_list[parent].append(child)
return adjacency_list
def children(self, vertex):
r"""
Returns the children of the selected vertex.
Parameters
----------
vertex : `int`
The selected vertex.
Returns
-------
children : `list`
The list of children.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex)
return self.adjacency_list[vertex]
def n_children(self, vertex):
r"""
Returns the number of children of the selected vertex.
Parameters
----------
vertex : `int`
The selected vertex.
Returns
-------
n_children : `int`
The number of children.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex)
return len(self.children(vertex))
def parent(self, vertex):
r"""
Returns the parents of the selected vertex.
Parameters
----------
vertex : `int`
The selected vertex.
Returns
-------
parent : `list`
The list of parents.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex)
adj = self.get_adjacency_matrix()
return list(np.where(adj[:, vertex])[0])
def n_parent(self, vertex):
r"""
Returns the number of parents of the selected vertex.
Parameters
----------
vertex : `int`
The selected vertex.
Returns
-------
n_parent : `int`
The number of parents.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex)
return len(self.parent(vertex))
def is_edge(self, parent, child):
r"""
Returns whether there is an edge between the provided vertices.
Parameters
----------
parent : `int`
The first selected vertex which is considered as the parent.
child : `int`
The second selected vertex which is considered as the child.
Returns
-------
is_edge : `bool`
True if there is an edge.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(parent)
self._check_vertex(child)
return child in self.adjacency_list[parent]
def has_cycles(self):
r"""
Whether the graph has at least on cycle.
Returns
-------
has_cycles : `bool`
True if it has at least one cycle.
"""
return _has_cycles(self.adjacency_list, True)
def __str__(self):
return "Directed graph of {} vertices and {} edges.".format(
self.n_vertices, self.n_edges)
class Tree(DirectedGraph):
r"""
Class for Tree definitions and manipulation.
Parameters
----------
adjacency_array : ``(n_edges, 2, )`` `ndarray`
The Adjacency Array of the tree, i.e. an array containing the sets of
the tree's edges. The numbering of vertices is assumed to start from 0.
We assume that the vertices in the first column of the `adjacency_array`
are the parents and the vertices in the second column of the
`adjacency_array` are the children, for example:
::
0 adjacency_array = ndarray([[0, 1],
| [0, 2],
___|___ [1, 3],
1 2 [1, 4],
| | [2, 5],
_|_ | [3, 6],
3 4 5 [4, 7],
| | | [5, 8]])
| | |
6 7 8
root_vertex : `int`
The vertex that will be considered as root.
copy : `bool`, optional
If ``False``, the ``adjacency_list`` will not be copied on assignment.
Raises
------
ValueError
The provided edges do not represent a tree.
ValueError
The root_vertex must be between 0 and n_vertices-1.
"""
def __init__(self, adjacency_array, root_vertex, copy=True):
super(Tree, self).__init__(adjacency_array, copy=copy)
# check if provided adjacency_array represents a tree
if not (self.is_tree() and self.n_edges == self.n_vertices - 1):
raise ValueError('The provided edges do not represent a tree.')
# check if root_vertex is valid
self._check_vertex(root_vertex)
self.root_vertex = root_vertex
self.predecessors_list = self._get_predecessors_list()
def _get_predecessors_list(self):
r"""
Returns the Predecessors List of the tree, i.e. a list of length
n_vertices that for each vertex it has its parent. The value of the
root vertex is None.
:type: `list` of len n_vertices
"""
predecessors_list = [None] * self.n_vertices
for e in range(self.n_edges):
parent = self.adjacency_array[e, 0]
child = self.adjacency_array[e, 1]
predecessors_list[child] = parent
return predecessors_list
def depth_of_vertex(self, vertex):
r"""
Returns the depth of the specified vertex.
Parameters
----------
vertex : `int`
The selected vertex.
Returns
-------
depth : `int`
The depth of the selected vertex.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex)
parent = vertex
depth = 0
while not parent == self.root_vertex:
current = parent
parent = self.predecessors_list[current]
depth += 1
return depth
@property
def maximum_depth(self):
r"""
Returns the maximum depth of the tree.
:type: `int`
"""
all_depths = [self.depth_of_vertex(v) for v in range(self.n_vertices)]
return np.max(all_depths)
def vertices_at_depth(self, depth):
r"""
Returns a list of vertices at the specified depth.
Parameters
----------
depth : `int`
The selected depth.
Returns
-------
vertices : `list`
The vertices that lie in the specified depth.
"""
ver = []
for v in range(self.n_vertices):
if self.depth_of_vertex(v) == depth:
ver.append(v)
return ver
def n_vertices_at_depth(self, depth):
r"""
Returns the number of vertices at the specified depth.
Parameters
----------
depth : `int`
The selected depth.
Returns
-------
n_vertices : `int`
The number of vertices that lie in the specified depth.
"""
n_ver = 0
for v in range(self.n_vertices):
if self.depth_of_vertex(v) == depth:
n_ver += 1
return n_ver
def is_leaf(self, vertex):
r"""
Returns whether the vertex is a leaf.
Parameters
----------
vertex : `int`
The selected vertex.
Returns
-------
is_leaf : `bool`
If True, then selected vertex is a leaf.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex)
return len(self.children(vertex)) == 0
@property
def leaves(self):
r"""
Returns a list with the all leaves of the tree.
:type: `list`
"""
leaves = []
for v in range(self.n_vertices):
if self.is_leaf(v):
leaves.append(v)
return leaves
@property
def n_leaves(self):
r"""
Returns the number of leaves of the tree.
:type: `int`
"""
n_leaves = 0
for v in range(self.n_vertices):
if self.is_leaf(v):
n_leaves += 1
return n_leaves
def parent(self, vertex):
r"""
Returns the parent of the selected vertex.
Parameters
----------
vertex : `int`
The selected vertex.
Returns
-------
parent : `int`
The parent vertex.
Raises
------
ValueError
The vertex must be between 0 and {n_vertices-1}.
"""
self._check_vertex(vertex)
return self.predecessors_list[vertex]
def __str__(self):
return "Tree of depth {} with {} vertices and {} leaves.".format(
self.maximum_depth, self.n_vertices, self.n_leaves)
class PointGraph(Graph, PointCloud):
r"""
Class for defining a graph with geometry.
Parameters
----------
points : `ndarray`
The array of point locations.
adjacency_array : ``(n_edges, 2, )`` `ndarray`
The Adjacency Array of the graph, i.e. an array containing the sets of
the graph's edges. The numbering of vertices is assumed to start from 0.
For an undirected graph, the order of an edge's vertices doesn't matter,
for example
::
|---0---| adjacency_array = ndarray([[0, 1],
| | [0, 2],
| | [1, 2],
1-------2 [1, 3],
| | [2, 4],
| | [3, 4],
3-------4 [3, 5]])
|
5
For a directed graph, we assume that the vertices in the first column of
the adjacency_array are the fathers and the vertices in the second
column of the adjacency_array are the children, for example
::
|-->0<--| adjacency_array = ndarray([[1, 0],
| | [2, 0],
| | [1, 2],
1<----->2 [2, 1],
| | [1, 3],
v v [2, 4],
3------>4 [3, 4],
| [3, 5]])
v
5
"""
def __init__(self, points, adjacency_array, copy=True):
_check_n_points(points, adjacency_array)
Graph.__init__(self, adjacency_array, copy=copy)
PointCloud.__init__(self, points, copy=copy)
def tojson(self):
r"""
Convert this :map:`PointGraph` to a dictionary representation suitable
for inclusion in the LJSON landmark format.
Returns
-------
dictionary with 'points' and 'connectivity' keys.
"""
json_dict = PointCloud.tojson(self)
json_dict['connectivity'] = self.adjacency_array.tolist()
return json_dict
def _view_2d(self, figure_id=None, new_figure=False, image_view=True,
render_lines=True, line_colour='r',
line_style='-', line_width=1.,
render_markers=True, marker_style='o', marker_size=20,
marker_face_colour='k', marker_edge_colour='k',
marker_edge_width=1., render_axes=True,
axes_font_name='sans-serif', axes_font_size=10,
axes_font_style='normal', axes_font_weight='normal',
axes_x_limits=None, axes_y_limits=None, figure_size=(10, 8),
label=None):
r"""
Visualization of the PointGraph.
Parameters
----------
figure_id : `object`, optional
The id of the figure to be used.
new_figure : `bool`, optional
If ``True``, a new figure is created.
image_view : `bool`, optional
If ``True``, the x and y axes are flipped.
render_lines : `bool`, optional
If ``True``, the edges will be rendered.
line_colour : {``r``, ``g``, ``b``, ``c``, ``m``, ``k``, ``w``} or
``(3, )`` `ndarray`, optional
The colour of the lines.
line_style : {``-``, ``--``, ``-.``, ``:``}, optional
The style of the lines.
line_width : `float`, optional
The width of the lines.
render_markers : `bool`, optional
If ``True``, the markers will be rendered.
marker_style : {``.``, ``,``, ``o``, ``v``, ``^``, ``<``, ``>``, ``+``,
``x``, ``D``, ``d``, ``s``, ``p``, ``*``, ``h``, ``H``,
``1``, ``2``, ``3``, ``4``, ``8``}, optional
The style of the markers.
marker_size : `int`, optional
The size of the markers in points^2.
marker_face_colour : {``r``, ``g``, ``b``, ``c``, ``m``, ``k``, ``w``}
or ``(3, )`` `ndarray`, optional
The face (filling) colour of the markers.
marker_edge_colour : {``r``, ``g``, ``b``, ``c``, ``m``, ``k``, ``w``}
or ``(3, )`` `ndarray`, optional
The edge colour of the markers.
marker_edge_width : `float`, optional
The width of the markers' edge.
render_axes : `bool`, optional
If ``True``, the axes will be rendered.
axes_font_name : {``serif``, ``sans-serif``, ``cursive``, ``fantasy``,
``monospace``}, optional
The font of the axes.
axes_font_size : `int`, optional
The font size of the axes.
axes_font_style : {``normal``, ``italic``, ``oblique``}, optional
The font style of the axes.
axes_font_weight : {``ultralight``, ``light``, ``normal``, ``regular``,
``book``, ``medium``, ``roman``, ``semibold``,
``demibold``, ``demi``, ``bold``, ``heavy``,
``extra bold``, ``black``}, optional
The font weight of the axes.
axes_x_limits : (`float`, `float`) or `None`, optional
The limits of the x axis.
axes_y_limits : (`float`, `float`) or `None`, optional
The limits of the y axis.
figure_size : (`float`, `float`) or `None`, optional
The size of the figure in inches.
label : `str`, optional
The name entry in case of a legend.
Returns
-------