Skip to main content

Fast and full-featured Matrix Market file I/O

Project description

Fast and full-featured Matrix Market file I/O package for Python.

Fastest way to read and write any Matrix Market .mtx file into a dense ndarray, sparse coordinate (triplet) arrays, or SciPy sparse matrix.

Implemented as a Python binding of the C++ fast_matrix_market Matrix Market I/O library.

pip install fast_matrix_market

Usage

import fast_matrix_market as fmm
# SciPy-like
>>> a = fmm.mmread("eye3.mtx")
>>> a
<3x3 sparse matrix of type '<class 'numpy.float64'>'
        with 3 stored elements in COOrdinate format>
>>> print(a)
(0, 0)	1.0
(1, 1)	1.0
(2, 2)	1.0
# Coordinate/Triplet
>>> (data, (rows, cols)), shape = fmm.read_coo("eye3.mtx")
>>> rows, cols, data
(array([0, 1, 2], dtype=int32), array([0, 1, 2], dtype=int32), array([1., 1., 1.]))
# Dense Array
>>> a = fmm.read_array("eye3.mtx")
>>> a
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
# Write to file
>>> fmm.mmwrite("matrix_out.mtx", a)
# Write to streams (read from streams too)
>>> bio = io.BytesIO()
>>> fmm.mmwrite(bio, a)
# Read only the header
>>> header = fmm.read_header("eye3.mtx")
header(shape=(3, 3), nnz=3, comment="3-by-3 identity matrix", object="matrix", format="coordinate", field="real", symmetry="general")

>>> header.shape
(3, 3)

>>> header.to_dict()
{'shape': (3, 3), 'nnz': 3, 'comment': '3-by-3 identity matrix', 'object': 'matrix', 'format': 'coordinate', 'field': 'real', 'symmetry': 'general'}

Note: SciPy is only a runtime dependency for the mmread and mmwrite methods. All others depend only on NumPy.

Compared to scipy.io.mmread

fast_matrix_market includes mmread() and mmwrite() methods that are direct replacements for their respective SciPy versions.

Compared to SciPy version 1.10.0:

Significant performance boost

read write

All cores on the system are used by default, use the parallelism argument to override. SciPy's routines are single-threaded.

64-bit indices

scipy.io.mmread() crashes on large matrices (dimensions > 231) because it uses 32-bit indices on most platforms.

fast_matrix_market switches to int64 if the matrix dimensions require it.

Directly write CSC/CSR matrices

scipy.io.mmwrite converts CSC and CSR matrices to COO first. fast_matrix_market can write them directly with no conversion.

longdouble

Read and write longdouble/longcomplex values for more floating-point precision on platforms that support it (e.g. 80-bit floats).

Just pass long_type=True argument to any read method to use longdouble arrays. SciPy can write longdouble matrices but reads use double precision.

Note: Many platforms do not offer any precision greater than double even if the longdouble type exists. On those platforms longdouble == double so check your Numpy for support. As of writing only Linux tends to have longdouble > double.

Vector files

Read vector files. scipy.io.mmread() throws a ValueError.

Differences

Differences between fast_matrix_market.mmwrite and scipy.io.mmwrite:

  • If no symmetry is specified scipy.io.mmwrite will search the matrix for one. This is a very slow process that significantly impacts writing time. It can be disabled by setting symmetry="general", but that is easily forgotten. fast_matrix_market.mmwrite() only looks for symmetries if the find_symmetry=True argument is passed.
  • precision argument is currently ignored. Floats may be written with more precision than desired.

Quick way to try

Replace scipy.io.mmread with fast_matrix_market.mmread to quickly see if your scripts would benefit:

import scipy.io
import fast_matrix_market as fmm

scipy.io.mmread = fmm.mmread
scipy.io.mmwrite = fmm.mmwrite

Dependencies

  • No dependencies to read/write MatrixMarket headers.
  • numpy to read/write arrays (i.e. read_array() and read_coo()). SciPy is not required.
  • scipy to read/write scipy.sparse sparse matrices (i.e. read_scipy() and mmread()).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fast-matrix-market-1.3.0.tar.gz (47.6 kB view hashes)

Uploaded Source

Built Distributions

fast_matrix_market-1.3.0-pp39-pypy39_pp73-win_amd64.whl (262.4 kB view hashes)

Uploaded PyPy Windows x86-64

fast_matrix_market-1.3.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (543.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

fast_matrix_market-1.3.0-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (541.8 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

fast_matrix_market-1.3.0-pp38-pypy38_pp73-win_amd64.whl (262.0 kB view hashes)

Uploaded PyPy Windows x86-64

fast_matrix_market-1.3.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (543.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

fast_matrix_market-1.3.0-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (541.8 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

fast_matrix_market-1.3.0-pp37-pypy37_pp73-win_amd64.whl (262.1 kB view hashes)

Uploaded PyPy Windows x86-64

fast_matrix_market-1.3.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (542.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

fast_matrix_market-1.3.0-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (541.2 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

fast_matrix_market-1.3.0-cp311-cp311-win_amd64.whl (262.9 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

fast_matrix_market-1.3.0-cp311-cp311-musllinux_1_1_x86_64.whl (1.0 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

fast_matrix_market-1.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (542.3 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

fast_matrix_market-1.3.0-cp311-cp311-macosx_11_0_arm64.whl (513.8 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

fast_matrix_market-1.3.0-cp311-cp311-macosx_10_9_x86_64.whl (541.3 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

fast_matrix_market-1.3.0-cp310-cp310-win_amd64.whl (262.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

fast_matrix_market-1.3.0-cp310-cp310-musllinux_1_1_x86_64.whl (1.0 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

fast_matrix_market-1.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (542.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

fast_matrix_market-1.3.0-cp310-cp310-macosx_11_0_arm64.whl (513.7 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

fast_matrix_market-1.3.0-cp310-cp310-macosx_10_9_x86_64.whl (541.2 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

fast_matrix_market-1.3.0-cp39-cp39-win_amd64.whl (262.9 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

fast_matrix_market-1.3.0-cp39-cp39-musllinux_1_1_x86_64.whl (1.0 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

fast_matrix_market-1.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (542.7 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

fast_matrix_market-1.3.0-cp39-cp39-macosx_11_0_arm64.whl (513.8 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

fast_matrix_market-1.3.0-cp39-cp39-macosx_10_9_x86_64.whl (541.3 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

fast_matrix_market-1.3.0-cp38-cp38-win_amd64.whl (262.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

fast_matrix_market-1.3.0-cp38-cp38-musllinux_1_1_x86_64.whl (1.0 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

fast_matrix_market-1.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (542.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

fast_matrix_market-1.3.0-cp38-cp38-macosx_11_0_arm64.whl (513.6 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

fast_matrix_market-1.3.0-cp38-cp38-macosx_10_9_x86_64.whl (541.3 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

fast_matrix_market-1.3.0-cp37-cp37m-win_amd64.whl (263.4 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

fast_matrix_market-1.3.0-cp37-cp37m-musllinux_1_1_x86_64.whl (1.0 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

fast_matrix_market-1.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (553.4 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

fast_matrix_market-1.3.0-cp37-cp37m-macosx_10_9_x86_64.whl (538.5 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page