| 659 | static constexpr bool rowMajor = Type::IsRowMajor; |
| 660 | |
| 661 | bool load(handle src, bool) { |
| 662 | if (!src) { |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | auto obj = reinterpret_borrow<object>(src); |
| 667 | object sparse_module = module_::import("scipy.sparse"); |
| 668 | object matrix_type = sparse_module.attr(rowMajor ? "csr_matrix" : "csc_matrix"); |
| 669 | |
| 670 | if (!type::handle_of(obj).is(matrix_type)) { |
| 671 | try { |
| 672 | obj = matrix_type(obj); |
| 673 | } catch (const error_already_set &) { |
| 674 | return false; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | auto values = array_t<Scalar>((object) obj.attr("data")); |
| 679 | auto innerIndices = array_t<StorageIndex>((object) obj.attr("indices")); |
| 680 | auto outerIndices = array_t<StorageIndex>((object) obj.attr("indptr")); |
| 681 | auto shape = pybind11::tuple((pybind11::object) obj.attr("shape")); |
| 682 | auto nnz = obj.attr("nnz").cast<Index>(); |
| 683 | |
| 684 | if (!values || !innerIndices || !outerIndices) { |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | value = EigenMapSparseMatrix<Scalar, |
| 689 | Type::Flags &(Eigen::RowMajor | Eigen::ColMajor), |
| 690 | StorageIndex>(shape[0].cast<Index>(), |
| 691 | shape[1].cast<Index>(), |
| 692 | std::move(nnz), |
| 693 | outerIndices.mutable_data(), |
| 694 | innerIndices.mutable_data(), |
| 695 | values.mutable_data()); |
| 696 | |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) { |
| 701 | const_cast<Type &>(src).makeCompressed(); |
nothing calls this directly
no test coverage detected