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