Convert to sparse interface as set in config. Input can be dense or sparse. If sparse, convert to sparse_interface indicated by get_config. Otherwise, return X unchanged.
(X)
| 9 | |
| 10 | |
| 11 | def _align_api_if_sparse(X): |
| 12 | """ |
| 13 | Convert to sparse interface as set in config. |
| 14 | |
| 15 | Input can be dense or sparse. |
| 16 | If sparse, convert to sparse_interface indicated by get_config. |
| 17 | Otherwise, return X unchanged. |
| 18 | """ |
| 19 | if not sp.sparse.issparse(X): |
| 20 | return X |
| 21 | |
| 22 | config_sparse_interface = get_config()["sparse_interface"] |
| 23 | |
| 24 | # there are only two sparse interfaces: sparray and spmatrix |
| 25 | if config_sparse_interface == "sparray": |
| 26 | if sp.sparse.isspmatrix(X): |
| 27 | # Fundamental code to switch to sparray in any format |
| 28 | return getattr(sp.sparse, X.format + "_array")(X) |
| 29 | return X |
| 30 | elif config_sparse_interface == "spmatrix": |
| 31 | if sp.sparse.isspmatrix(X): |
| 32 | return X |
| 33 | # Fundamental code to switch to spmatrix in any format |
| 34 | return getattr(sp.sparse, X.format + "_matrix")(X) |
| 35 | else: |
| 36 | raise ValueError( |
| 37 | f'Config "sparse_interface" is {config_sparse_interface}. ' |
| 38 | 'It should be either "sparray" or "spmatrix".' |
| 39 | ) |
searching dependent graphs…