Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/scipy/sparse/csgraph/_validation.py: 19%
32 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-22 06:44 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-22 06:44 +0000
1import numpy as np
2from scipy.sparse import csr_matrix, issparse
3from scipy.sparse._sputils import convert_pydata_sparse_to_scipy
4from scipy.sparse.csgraph._tools import (
5 csgraph_to_dense, csgraph_from_dense,
6 csgraph_masked_from_dense, csgraph_from_masked
7)
9DTYPE = np.float64
12def validate_graph(csgraph, directed, dtype=DTYPE,
13 csr_output=True, dense_output=True,
14 copy_if_dense=False, copy_if_sparse=False,
15 null_value_in=0, null_value_out=np.inf,
16 infinity_null=True, nan_null=True):
17 """Routine for validation and conversion of csgraph inputs"""
18 if not (csr_output or dense_output):
19 raise ValueError("Internal: dense or csr output must be true")
21 csgraph = convert_pydata_sparse_to_scipy(csgraph)
23 # if undirected and csc storage, then transposing in-place
24 # is quicker than later converting to csr.
25 if (not directed) and issparse(csgraph) and csgraph.format == "csc":
26 csgraph = csgraph.T
28 if issparse(csgraph):
29 if csr_output:
30 csgraph = csr_matrix(csgraph, dtype=DTYPE, copy=copy_if_sparse)
31 else:
32 csgraph = csgraph_to_dense(csgraph, null_value=null_value_out)
33 elif np.ma.isMaskedArray(csgraph):
34 if dense_output:
35 mask = csgraph.mask
36 csgraph = np.array(csgraph.data, dtype=DTYPE, copy=copy_if_dense)
37 csgraph[mask] = null_value_out
38 else:
39 csgraph = csgraph_from_masked(csgraph)
40 else:
41 if dense_output:
42 csgraph = csgraph_masked_from_dense(csgraph,
43 copy=copy_if_dense,
44 null_value=null_value_in,
45 nan_null=nan_null,
46 infinity_null=infinity_null)
47 mask = csgraph.mask
48 csgraph = np.asarray(csgraph.data, dtype=DTYPE)
49 csgraph[mask] = null_value_out
50 else:
51 csgraph = csgraph_from_dense(csgraph, null_value=null_value_in,
52 infinity_null=infinity_null,
53 nan_null=nan_null)
55 if csgraph.ndim != 2:
56 raise ValueError("compressed-sparse graph must be 2-D")
58 if csgraph.shape[0] != csgraph.shape[1]:
59 raise ValueError("compressed-sparse graph must be shape (N, N)")
61 return csgraph