1
1
"""Lite graph objects used by pecanpy."""
2
- from typing import Dict
3
- from typing import Iterator
4
- from typing import List
5
- from typing import Optional
6
- from typing import Tuple
7
-
8
2
import numpy as np
9
3
4
+ from .typing import AdjMat
5
+ from .typing import AdjNonZeroMat
6
+ from .typing import CSR
7
+ from .typing import Dict
8
+ from .typing import Float32Array
9
+ from .typing import Iterator
10
+ from .typing import List
11
+ from .typing import Optional
12
+ from .typing import Tuple
13
+ from .typing import Uint32Array
14
+
10
15
11
16
class BaseGraph :
12
17
"""Base Graph object.
@@ -78,8 +83,8 @@ class AdjlstGraph(BaseGraph):
78
83
Python data structures like list and dict.
79
84
80
85
Examples:
81
- Read ``.edg`` file and create ``SparseGraph`` object using ``.read_edg``
82
- method.
86
+ Read ``.edg`` file and create ``SparseGraph`` object using
87
+ ``.read_edg`` method.
83
88
84
89
>>> from pecanpy.graph import AdjlstGraph
85
90
>>>
@@ -277,7 +282,7 @@ def save(self, path: str, unweighted: bool = False, delimiter: str = "\t"):
277
282
terms = (h_id , t_id ) if unweighted else (h_id , t_id , str (w ))
278
283
f .write (f"{ delimiter .join (terms )} \n " )
279
284
280
- def to_csr (self ) -> Tuple [ np . ndarray , np . ndarray , np . ndarray ] :
285
+ def to_csr (self ) -> CSR :
281
286
"""Construct compressed sparse row matrix."""
282
287
indptr = np .zeros (len (self .nodes ) + 1 , dtype = np .uint32 )
283
288
for i , row_data in enumerate (self ._data ):
@@ -297,16 +302,16 @@ def to_csr(self) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
297
302
298
303
return indptr , indices , data
299
304
300
- def to_dense (self ) -> np . ndarray :
305
+ def to_dense (self ) -> AdjMat :
301
306
"""Construct dense adjacency matrix.
302
307
303
308
Note:
304
309
This method does not return DenseGraph object, but instead return
305
- dense adjacency matrix as ``numpy.ndarray`` , the index is the same
310
+ dense adjacency matrix as NDArray , the index is the same
306
311
as that of ``nodes``.
307
312
308
313
Return:
309
- numpy.ndarray : Full adjacency matrix as 2d numpy array.
314
+ NDArray : Full adjacency matrix as 2d numpy array.
310
315
311
316
"""
312
317
n_nodes = len (self .nodes )
@@ -319,11 +324,11 @@ def to_dense(self) -> np.ndarray:
319
324
return mat
320
325
321
326
@classmethod
322
- def from_mat (cls , adj_mat : np . ndarray , node_ids : List [str ], ** kwargs ):
327
+ def from_mat (cls , adj_mat : AdjMat , node_ids : List [str ], ** kwargs ):
323
328
"""Construct graph using adjacency matrix and node IDs.
324
329
325
330
Args:
326
- adj_mat(:obj:`numpy.ndarray` ): 2D numpy array of adjacency matrix
331
+ adj_mat(NDArray ): 2D numpy array of adjacency matrix
327
332
node_ids(:obj:`list` of str): node ID list
328
333
329
334
Return:
@@ -347,8 +352,8 @@ class SparseGraph(BaseGraph):
347
352
"""Sparse Graph object that stores graph as adjacency list.
348
353
349
354
Examples:
350
- Read ``.edg`` file and create ``SparseGraph`` object using ``.read_edg``
351
- method.
355
+ Read ``.edg`` file and create ``SparseGraph`` object using
356
+ ``.read_edg`` method.
352
357
353
358
>>> from pecanpy.graph import SparseGraph
354
359
>>>
@@ -366,9 +371,9 @@ class SparseGraph(BaseGraph):
366
371
def __init__ (self ):
367
372
"""Initialize SparseGraph object."""
368
373
super ().__init__ ()
369
- self .data : Optional [np . ndarray ] = None
370
- self .indptr : Optional [np . ndarray ] = None
371
- self .indices : Optional [np . ndarray ] = None
374
+ self .data : Optional [Float32Array ] = None
375
+ self .indptr : Optional [Uint32Array ] = None
376
+ self .indices : Optional [Uint32Array ] = None
372
377
373
378
@property
374
379
def num_edges (self ) -> int :
@@ -457,14 +462,14 @@ def from_adjlst_graph(cls, adjlst_graph, **kwargs):
457
462
return g
458
463
459
464
@classmethod
460
- def from_mat (cls , adj_mat : np . ndarray , node_ids : List [str ], ** kwargs ):
465
+ def from_mat (cls , adj_mat : AdjMat , node_ids : List [str ], ** kwargs ):
461
466
"""Construct csr graph using adjacency matrix and node IDs.
462
467
463
468
Note:
464
469
Only consider positive valued edges.
465
470
466
471
Args:
467
- adj_mat(:obj:`numpy.ndarray` ): 2D numpy array of adjacency matrix
472
+ adj_mat(NDArray ): 2D numpy array of adjacency matrix
468
473
node_ids(:obj:`list` of str): node ID list
469
474
470
475
"""
@@ -479,15 +484,15 @@ class DenseGraph(BaseGraph):
479
484
"""Dense Graph object that stores graph as array.
480
485
481
486
Examples:
482
- Read ``.npz`` files and create ``DenseGraph`` object using ``read_npz``.
487
+ Read ``.npz`` files and create ``DenseGraph`` object using ``read_npz``
483
488
484
489
>>> from pecanpy.graph import DenseGraph
485
490
>>>
486
491
>>> g = DenseGraph() # initialize DenseGraph object
487
492
>>>
488
493
>>> g.read_npz(paht_to_npz_file, weighted=True, directed=False)
489
494
490
- Read ``.edg`` files and create ``DenseGraph`` object using ``read_edg``.
495
+ Read ``.edg`` files and create ``DenseGraph`` object using ``read_edg``
491
496
492
497
>>> from pecanpy.graph import DenseGraph
493
498
>>>
@@ -505,8 +510,8 @@ class DenseGraph(BaseGraph):
505
510
def __init__ (self ):
506
511
"""Initialize DenseGraph object."""
507
512
super ().__init__ ()
508
- self ._data : Optional [np . ndarray ] = None
509
- self ._nonzero : Optional [np . ndarray ] = None
513
+ self ._data : Optional [AdjMat ] = None
514
+ self ._nonzero : Optional [AdjNonZeroMat ] = None
510
515
511
516
@property
512
517
def num_edges (self ) -> int :
@@ -517,18 +522,18 @@ def num_edges(self) -> int:
517
522
raise ValueError ("Empty graph." )
518
523
519
524
@property
520
- def data (self ) -> Optional [np . ndarray ]:
525
+ def data (self ) -> Optional [AdjMat ]:
521
526
"""Return the adjacency matrix."""
522
527
return self ._data
523
528
524
529
@data .setter
525
- def data (self , data : np . ndarray ):
530
+ def data (self , data : AdjMat ):
526
531
"""Set adjacency matrix and the corresponding nonzero matrix."""
527
532
self ._data = data .astype (float )
528
533
self ._nonzero = np .array (self ._data != 0 , dtype = bool )
529
534
530
535
@property
531
- def nonzero (self ) -> Optional [np . ndarray ]:
536
+ def nonzero (self ) -> Optional [AdjNonZeroMat ]:
532
537
"""Return the nonzero mask for the adjacency matrix."""
533
538
return self ._nonzero
534
539
@@ -580,11 +585,11 @@ def from_adjlst_graph(cls, adjlst_graph, **kwargs):
580
585
return g
581
586
582
587
@classmethod
583
- def from_mat (cls , adj_mat : np . ndarray , node_ids : List [str ], ** kwargs ):
588
+ def from_mat (cls , adj_mat : AdjMat , node_ids : List [str ], ** kwargs ):
584
589
"""Construct dense graph using adjacency matrix and node IDs.
585
590
586
591
Args:
587
- adj_mat(:obj:`numpy.ndarray` ): 2D numpy array of adjacency matrix
592
+ adj_mat(NDArray ): 2D numpy array of adjacency matrix
588
593
node_ids(:obj:`list` of str): node ID list
589
594
590
595
"""
0 commit comments