From 6be7bc7af88d80c2a9a26c6c4890be5583c424a7 Mon Sep 17 00:00:00 2001 From: Brandon Istenes Date: Tue, 28 May 2024 23:30:50 -0400 Subject: [PATCH] Add typing to functions --- docs/html/api.html | 10 +++++----- docs/html/index.html | 10 +++++----- docs/html/searchindex.js | 2 +- graphpca/__init__.py | 22 +++++++++++++++------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/docs/html/api.html b/docs/html/api.html index 4c97a10..210ae87 100644 --- a/docs/html/api.html +++ b/docs/html/api.html @@ -35,9 +35,9 @@

API

-graphpca.draw_graph(nx_graph)
+graphpca.draw_graph(nx_graph: Graph | DiGraph)

Draws the input graph on two axes with lines between the nodes

-

Positions of the nodes are determined with reduce_graph, of course.

+

Positions of the nodes are determined with reduce_graph.

Parameters:

nx_graph (nx.Graph or nx.DiGraph) – The graph to be plotted

@@ -47,7 +47,7 @@
-graphpca.reduce_graph(nx_graph, output_dim)
+graphpca.reduce_graph(nx_graph: Graph | DiGraph, output_dim: int) ndarray

Run PCA on the ETCD of the input NetworkX graph

The best algorithm and parameters for doing so are selected dynamically, based on the size of the graph. A graph G with number of nodes n < 50 will @@ -67,7 +67,7 @@

-graphpca.reduce_graph_efficiently(nx_graph, output_dim, add_supernode=False, eigendecomp_strategy='smart')
+graphpca.reduce_graph_efficiently(nx_graph: Graph | DiGraph, output_dim: int, add_supernode: bool = False, eigendecomp_strategy: Literal['smart', 'sparse', 'exact'] = 'smart') ndarray

Run PCA on the ETCD of the input NetworkX graph

We skip calculating the actual ETCD for efficiency. The ETCD is given by the Moore-Penrose pseudoinverse of the Laplacian of the input graph. The @@ -120,7 +120,7 @@

-graphpca.reduce_graph_naively(nx_graph, output_dim, eigendecomp_strategy='exact')
+graphpca.reduce_graph_naively(nx_graph: Graph | DiGraph, output_dim: int, eigendecomp_strategy: Literal['exact', 'sparse', 'smart'] = 'exact') ndarray

Run PCA on the ETCD of a NetworkX graph using a slow but precise method

This is the method that calculates the actual ETCD. It calculates the Moore-Penrose pseudoinverse of the Laplacian of the input graph. We return diff --git a/docs/html/index.html b/docs/html/index.html index b238e3c..dc3268b 100644 --- a/docs/html/index.html +++ b/docs/html/index.html @@ -35,9 +35,9 @@

API

-graphpca.draw_graph(nx_graph)
+graphpca.draw_graph(nx_graph: Graph | DiGraph)

Draws the input graph on two axes with lines between the nodes

-

Positions of the nodes are determined with reduce_graph, of course.

+

Positions of the nodes are determined with reduce_graph.

Parameters:

nx_graph (nx.Graph or nx.DiGraph) – The graph to be plotted

@@ -47,7 +47,7 @@
-graphpca.reduce_graph(nx_graph, output_dim)
+graphpca.reduce_graph(nx_graph: Graph | DiGraph, output_dim: int) ndarray

Run PCA on the ETCD of the input NetworkX graph

The best algorithm and parameters for doing so are selected dynamically, based on the size of the graph. A graph G with number of nodes n < 50 will @@ -67,7 +67,7 @@

-graphpca.reduce_graph_efficiently(nx_graph, output_dim, add_supernode=False, eigendecomp_strategy='smart')
+graphpca.reduce_graph_efficiently(nx_graph: Graph | DiGraph, output_dim: int, add_supernode: bool = False, eigendecomp_strategy: Literal['smart', 'sparse', 'exact'] = 'smart') ndarray

Run PCA on the ETCD of the input NetworkX graph

We skip calculating the actual ETCD for efficiency. The ETCD is given by the Moore-Penrose pseudoinverse of the Laplacian of the input graph. The @@ -120,7 +120,7 @@

-graphpca.reduce_graph_naively(nx_graph, output_dim, eigendecomp_strategy='exact')
+graphpca.reduce_graph_naively(nx_graph: Graph | DiGraph, output_dim: int, eigendecomp_strategy: Literal['exact', 'sparse', 'smart'] = 'exact') ndarray

Run PCA on the ETCD of a NetworkX graph using a slow but precise method

This is the method that calculates the actual ETCD. It calculates the Moore-Penrose pseudoinverse of the Laplacian of the input graph. We return diff --git a/docs/html/searchindex.js b/docs/html/searchindex.js index 7dc1525..3027976 100644 --- a/docs/html/searchindex.js +++ b/docs/html/searchindex.js @@ -1 +1 @@ -Search.setIndex({"alltitles": {"API": [[0, "module-graphpca"], [1, "module-graphpca"]]}, "docnames": ["api", "index"], "envversion": {"sphinx": 61, "sphinx.domains.c": 3, "sphinx.domains.changeset": 1, "sphinx.domains.citation": 1, "sphinx.domains.cpp": 9, "sphinx.domains.index": 1, "sphinx.domains.javascript": 3, "sphinx.domains.math": 2, "sphinx.domains.python": 4, "sphinx.domains.rst": 2, "sphinx.domains.std": 2}, "filenames": ["api.rst", "index.rst"], "indexentries": {"draw_graph() (in module graphpca)": [[0, "graphpca.draw_graph", false], [1, "graphpca.draw_graph", false]], "graphpca": [[0, "module-graphpca", false], [1, "module-graphpca", false]], "module": [[0, "module-graphpca", false], [1, "module-graphpca", false]], "reduce_graph() (in module graphpca)": [[0, "graphpca.reduce_graph", false], [1, "graphpca.reduce_graph", false]], "reduce_graph_efficiently() (in module graphpca)": [[0, "graphpca.reduce_graph_efficiently", false], [1, "graphpca.reduce_graph_efficiently", false]], "reduce_graph_naively() (in module graphpca)": [[0, "graphpca.reduce_graph_naively", false], [1, "graphpca.reduce_graph_naively", false]]}, "objects": {"": [[1, 0, 0, "-", "graphpca"]], "graphpca": [[1, 1, 1, "", "draw_graph"], [1, 1, 1, "", "reduce_graph"], [1, 1, 1, "", "reduce_graph_efficiently"], [1, 1, 1, "", "reduce_graph_naively"]]}, "objnames": {"0": ["py", "module", "Python module"], "1": ["py", "function", "Python function"]}, "objtypes": {"0": "py:module", "1": "py:function"}, "terms": {"0": [0, 1], "1": [0, 1], "1000": [0, 1], "20": [0, 1], "50": [0, 1], "A": [0, 1], "For": [0, 1], "If": [0, 1], "It": [0, 1], "The": [0, 1], "about": [0, 1], "abov": [0, 1], "actual": [0, 1], "add": [0, 1], "add_supernod": [0, 1], "algorithm": [0, 1], "all": [0, 1], "amaz": [0, 1], "an": [0, 1], "approxim": [0, 1], "ar": [0, 1], "associ": [0, 1], "ax": [0, 1], "base": [0, 1], "behaviour": [0, 1], "best": [0, 1], "between": [0, 1], "bool": [0, 1], "calcul": [0, 1], "care": [0, 1], "case": [0, 1], "check": [0, 1], "choos": [0, 1], "compon": [0, 1], "comput": [0, 1], "connect": [0, 1], "conveni": [0, 1], "correspond": [0, 1], "cost": [0, 1], "count": [0, 1], "cours": [0, 1], "data": [0, 1], "decreas": [0, 1], "dens": [0, 1], "determin": [0, 1], "digraph": [0, 1], "dimens": [0, 1], "do": [0, 1], "draw": [0, 1], "draw_graph": [0, 1], "due": [0, 1], "dynam": [0, 1], "e": [0, 1], "edg": [0, 1], "effici": [0, 1], "eig": [0, 1], "eigendecomp": [0, 1], "eigendecomp_strategi": [0, 1], "eigenpair": [0, 1], "eigenvalu": [0, 1], "eigenvector": [0, 1], "eigh": [0, 1], "eigsh": [0, 1], "eigval": [0, 1], "els": [0, 1], "equal": [0, 1], "especi": [0, 1], "etcd": [0, 1], "everi": [0, 1], "exact": [0, 1], "fact": [0, 1], "fals": [0, 1], "fewer": [0, 1], "first": [0, 1], "further": [0, 1], "g": [0, 1], "github": 1, "given": [0, 1], "graph": [0, 1], "graphpca": [0, 1], "ha": [0, 1], "i": [0, 1], "includ": [0, 1], "inform": [0, 1], "input": [0, 1], "int": [0, 1], "invers": [0, 1], "iter": [0, 1], "its": [0, 1], "just": [0, 1], "l": [0, 1], "laplacian": [0, 1], "linalg": [0, 1], "line": [0, 1], "long": [0, 1], "loss": [0, 1], "low": [0, 1], "make": [0, 1], "mani": [0, 1], "matrix": [0, 1], "method": [0, 1], "minor": [0, 1], "moor": [0, 1], "more": [0, 1], "n": [0, 1], "naiv": [0, 1], "ndarrai": [0, 1], "necessari": [0, 1], "need": [0, 1], "networkx": [0, 1], "node": [0, 1], "nonzero": [0, 1], "nulliti": [0, 1], "nullspac": [0, 1], "number": [0, 1], "numpi": [0, 1], "nx": [0, 1], "nx_graph": [0, 1], "ones": [0, 1], "onli": [0, 1], "order": [0, 1], "other": [0, 1], "otherwis": [0, 1], "output_dim": [0, 1], "paramet": [0, 1], "pca": [0, 1], "penros": [0, 1], "pinv": [0, 1], "plot": [0, 1], "posit": [0, 1], "precis": [0, 1], "pseudoinvers": [0, 1], "reach": [0, 1], "reduc": [0, 1], "reduce_graph": [0, 1], "reduce_graph_effici": [0, 1], "reduce_graph_na": [0, 1], "return": [0, 1], "run": [0, 1], "same": [0, 1], "scipi": [0, 1], "select": [0, 1], "size": [0, 1], "skip": [0, 1], "slow": [0, 1], "sm": [0, 1], "smallest": [0, 1], "smart": [0, 1], "so": [0, 1], "sometim": [0, 1], "spars": [0, 1], "stabl": [0, 1], "start": [0, 1], "strategi": [0, 1], "strip": [0, 1], "take": [0, 1], "therefor": [0, 1], "thi": [0, 1], "thing": [0, 1], "thousand": [0, 1], "time": [0, 1], "top": [0, 1], "trick": [0, 1], "true": [0, 1], "two": [0, 1], "type": [0, 1], "us": [0, 1], "veri": [0, 1], "we": [0, 1], "which": [0, 1], "yield": [0, 1], "zero": [0, 1]}, "titles": ["API", "API"], "titleterms": {"api": [0, 1]}}) \ No newline at end of file +Search.setIndex({"alltitles": {"API": [[0, "module-graphpca"], [1, "module-graphpca"]]}, "docnames": ["api", "index"], "envversion": {"sphinx": 61, "sphinx.domains.c": 3, "sphinx.domains.changeset": 1, "sphinx.domains.citation": 1, "sphinx.domains.cpp": 9, "sphinx.domains.index": 1, "sphinx.domains.javascript": 3, "sphinx.domains.math": 2, "sphinx.domains.python": 4, "sphinx.domains.rst": 2, "sphinx.domains.std": 2}, "filenames": ["api.rst", "index.rst"], "indexentries": {"draw_graph() (in module graphpca)": [[0, "graphpca.draw_graph", false], [1, "graphpca.draw_graph", false]], "graphpca": [[0, "module-graphpca", false], [1, "module-graphpca", false]], "module": [[0, "module-graphpca", false], [1, "module-graphpca", false]], "reduce_graph() (in module graphpca)": [[0, "graphpca.reduce_graph", false], [1, "graphpca.reduce_graph", false]], "reduce_graph_efficiently() (in module graphpca)": [[0, "graphpca.reduce_graph_efficiently", false], [1, "graphpca.reduce_graph_efficiently", false]], "reduce_graph_naively() (in module graphpca)": [[0, "graphpca.reduce_graph_naively", false], [1, "graphpca.reduce_graph_naively", false]]}, "objects": {"": [[1, 0, 0, "-", "graphpca"]], "graphpca": [[1, 1, 1, "", "draw_graph"], [1, 1, 1, "", "reduce_graph"], [1, 1, 1, "", "reduce_graph_efficiently"], [1, 1, 1, "", "reduce_graph_naively"]]}, "objnames": {"0": ["py", "module", "Python module"], "1": ["py", "function", "Python function"]}, "objtypes": {"0": "py:module", "1": "py:function"}, "terms": {"0": [0, 1], "1": [0, 1], "1000": [0, 1], "20": [0, 1], "50": [0, 1], "A": [0, 1], "For": [0, 1], "If": [0, 1], "It": [0, 1], "The": [0, 1], "about": [0, 1], "abov": [0, 1], "actual": [0, 1], "add": [0, 1], "add_supernod": [0, 1], "algorithm": [0, 1], "all": [0, 1], "amaz": [0, 1], "an": [0, 1], "approxim": [0, 1], "ar": [0, 1], "associ": [0, 1], "ax": [0, 1], "base": [0, 1], "behaviour": [0, 1], "best": [0, 1], "between": [0, 1], "bool": [0, 1], "calcul": [0, 1], "care": [0, 1], "case": [0, 1], "check": [0, 1], "choos": [0, 1], "compon": [0, 1], "comput": [0, 1], "connect": [0, 1], "conveni": [0, 1], "correspond": [0, 1], "cost": [0, 1], "count": [0, 1], "cours": [], "data": [0, 1], "decreas": [0, 1], "dens": [0, 1], "determin": [0, 1], "digraph": [0, 1], "dimens": [0, 1], "do": [0, 1], "draw": [0, 1], "draw_graph": [0, 1], "due": [0, 1], "dynam": [0, 1], "e": [0, 1], "edg": [0, 1], "effici": [0, 1], "eig": [0, 1], "eigendecomp": [0, 1], "eigendecomp_strategi": [0, 1], "eigenpair": [0, 1], "eigenvalu": [0, 1], "eigenvector": [0, 1], "eigh": [0, 1], "eigsh": [0, 1], "eigval": [0, 1], "els": [0, 1], "equal": [0, 1], "especi": [0, 1], "etcd": [0, 1], "everi": [0, 1], "exact": [0, 1], "fact": [0, 1], "fals": [0, 1], "fewer": [0, 1], "first": [0, 1], "further": [0, 1], "g": [0, 1], "github": 1, "given": [0, 1], "graph": [0, 1], "graphpca": [0, 1], "ha": [0, 1], "i": [0, 1], "includ": [0, 1], "inform": [0, 1], "input": [0, 1], "int": [0, 1], "invers": [0, 1], "iter": [0, 1], "its": [0, 1], "just": [0, 1], "l": [0, 1], "laplacian": [0, 1], "linalg": [0, 1], "line": [0, 1], "liter": [0, 1], "long": [0, 1], "loss": [0, 1], "low": [0, 1], "make": [0, 1], "mani": [0, 1], "matrix": [0, 1], "method": [0, 1], "minor": [0, 1], "moor": [0, 1], "more": [0, 1], "n": [0, 1], "naiv": [0, 1], "ndarrai": [0, 1], "necessari": [0, 1], "need": [0, 1], "networkx": [0, 1], "node": [0, 1], "nonzero": [0, 1], "nulliti": [0, 1], "nullspac": [0, 1], "number": [0, 1], "numpi": [0, 1], "nx": [0, 1], "nx_graph": [0, 1], "ones": [0, 1], "onli": [0, 1], "order": [0, 1], "other": [0, 1], "otherwis": [0, 1], "output_dim": [0, 1], "paramet": [0, 1], "pca": [0, 1], "penros": [0, 1], "pinv": [0, 1], "plot": [0, 1], "posit": [0, 1], "precis": [0, 1], "pseudoinvers": [0, 1], "reach": [0, 1], "reduc": [0, 1], "reduce_graph": [0, 1], "reduce_graph_effici": [0, 1], "reduce_graph_na": [0, 1], "return": [0, 1], "run": [0, 1], "same": [0, 1], "scipi": [0, 1], "select": [0, 1], "size": [0, 1], "skip": [0, 1], "slow": [0, 1], "sm": [0, 1], "smallest": [0, 1], "smart": [0, 1], "so": [0, 1], "sometim": [0, 1], "spars": [0, 1], "stabl": [0, 1], "start": [0, 1], "strategi": [0, 1], "strip": [0, 1], "take": [0, 1], "therefor": [0, 1], "thi": [0, 1], "thing": [0, 1], "thousand": [0, 1], "time": [0, 1], "top": [0, 1], "trick": [0, 1], "true": [0, 1], "two": [0, 1], "type": [0, 1], "us": [0, 1], "veri": [0, 1], "we": [0, 1], "which": [0, 1], "yield": [0, 1], "zero": [0, 1]}, "titles": ["API", "API"], "titleterms": {"api": [0, 1]}}) \ No newline at end of file diff --git a/graphpca/__init__.py b/graphpca/__init__.py index 352b7ec..61365a6 100644 --- a/graphpca/__init__.py +++ b/graphpca/__init__.py @@ -9,12 +9,13 @@ import scipy.linalg import scipy.sparse.linalg from scipy.sparse.linalg import ArpackNoConvergence +from typing import Literal logging.basicConfig(level=logging.WARNING) LOG = logging.getLogger(__name__) -def reduce_graph(nx_graph, output_dim): +def reduce_graph(nx_graph: nx.Graph | nx.DiGraph, output_dim: int) -> np.ndarray: """ Run PCA on the ETCD of the input NetworkX graph @@ -43,8 +44,11 @@ def reduce_graph(nx_graph, output_dim): def reduce_graph_efficiently( - nx_graph, output_dim, add_supernode=False, eigendecomp_strategy="smart" -): + nx_graph: nx.Graph | nx.DiGraph, + output_dim: int, + add_supernode: bool = False, + eigendecomp_strategy: Literal["smart", "sparse", "exact"] = "smart", +) -> np.ndarray: """ Run PCA on the ETCD of the input NetworkX graph @@ -133,7 +137,11 @@ def reduce_graph_efficiently( return X -def reduce_graph_naively(nx_graph, output_dim, eigendecomp_strategy="exact"): +def reduce_graph_naively( + nx_graph: nx.Graph | nx.DiGraph, + output_dim: int, + eigendecomp_strategy: Literal["exact", "sparse", "smart"] = "exact", +) -> np.ndarray: """ Run PCA on the ETCD of a NetworkX graph using a slow but precise method @@ -299,7 +307,7 @@ def _sparse_eigendecomp(M, output_dim, which, tol=0.000000001, _attempt=0, **kwa ) -def plot_2d(pca_output_2d, colormap_name="winter"): +def plot_2d(pca_output_2d: np.ndarray, colormap_name="winter"): import matplotlib.pyplot as plt x = pca_output_2d[0, :] @@ -311,11 +319,11 @@ def plot_2d(pca_output_2d, colormap_name="winter"): return plt -def draw_graph(nx_graph): +def draw_graph(nx_graph: nx.Graph | nx.DiGraph): """ Draws the input graph on two axes with lines between the nodes - Positions of the nodes are determined with reduce_graph, of course. + Positions of the nodes are determined with reduce_graph. Parameters ----------