Package: fast-matrix-market
Fast and full-featured Matrix Market file I/O package for Python.Fastest way to read and write any Matrix Market.mtxfile into a SciPy sparse matrix, sparse coordinate (triplet)ndarrays, or a densendarray.Implemented as a Python binding of the C++fast_matrix_marketlibrary.pipinstallfast_matrix_marketcondainstallfast_matrix_marketCompared to SciPy 1.12As of version 1.12,scipy.io.mmreadandscipy.io.mmreadare based on fast_matrix_market. If those methods suit your needs then there is no need to use this package.The following are extra features supported by the stand-alone FMM:Directly write CSC/CSR matriceswith no COO intermediary.Vector filesRead 1D vector files.scipy.io.mmread()throws aValueError.longdoubleRead and writelongdouble/longcomplexvalues for more floating-point precision on platforms that support it (e.g. 80-bit floats).Just passlong_type=Trueargument to any read method to uselongdoublearrays. SciPy can writelongdoublematrices but reads usedoubleprecision.Note:Many platforms do not offer any precision greater thandoubleeven if thelongdoubletype exists.
On those platformslongdouble == doubleso check your NumPy for support. As of writing only Linux tends to havelongdouble > double.Deprecation Warning:this type is going away in future versions of NumPy and SciPy.FMM also ships wheels for PyPy and for some older Python versions only supported by older versions of SciPy.Compared to classic scipy.io.mmread (version <1.12)Thefast_matrix_market.mmread()andmmwrite()methods are direct replacements forscipy.io.mmreadandmmwrite.
Compared to SciPy v1.10.0:Significant performance boostThe bytes in the plot refer to MatrixMarket file length. All cores on the system are used by default, use theparallelismargument to override. SciPy's routines are single-threaded.64-bit indices, but only if the matrix dimensions require it.scipy.io.mmread()crashes on large matrices (dimensions > 231) because it uses 32-bit indices on most platforms.See comparison with SciPy 1.12.Differencesscipy.io.mmwrite()will search the matrix for symmetry if thesymmetryargument is not specified.
This is a very slow process that significantly impacts writing time for all matrices, including non-symmetric ones.
It can be disabled by settingsymmetry="general", but that is easily forgotten.fast_matrix_market.mmwrite()only looks for symmetries if thefind_symmetry=Trueargument is passed.Usageimportfast_matrix_marketasfmmRead as scipy sparse matrix>>>a=fmm.mmread("eye3.mtx")>>>a<3x3sparsematrixoftype'<class 'numpy.float64'>'with3storedelementsinCOOrdinateformat>>>>print(a)(0,0)1.0(1,1)1.0(2,2)1.0Read as raw coordinate/triplet arrays>>>(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.]))Read as dense ndarray>>>a=fmm.read_array("eye3.mtx")>>>aarray([[1.,0.,0.],[0.,1.,0.],[0.,0.,1.]])Write any of the above to a 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'}Controlling parallelismAll methods other thanread_headerandmminfoaccept aparallelismparameter that controls the number of threads used. Default parallelism is equal to the core count of the system.mat=fmm.mmread("matrix.mtx",parallelism=2)# will use 2 threadsAlternatively, usethreadpoolctl:withthreadpoolctl.threadpool_limits(limits=2):mat=fmm.mmread("matrix.mtx")# will use 2 threadsQuick way to tryReplacescipy.io.mmreadwithfast_matrix_market.mmreadto quickly see if your scripts would benefit from a refactor:importscipy.ioimportfast_matrix_marketasfmmscipy.io.mmread=fmm.mmreadscipy.io.mmwrite=fmm.mmwriteDependenciesNo dependencies to read/write MatrixMarket headers (i.e.read_header(),mminfo()).numpyto read/write arrays (i.e.read_array()andread_coo()). SciPy isnotrequired.scipyto read/writescipy.sparsesparse matrices (i.e.read_scipy()andmmread()).Neithernumpynorscipyare listed as package dependencies, and those packages are imported only by the methods that need them.
This means that you may useread_coo()without having SciPy installed.DevelopmentThis Python binding is implemented usingpybind11and built withscikit-build-core.All code is in thepython/directory. If you make any changes simply install the package directory to build it:pipinstallpython/-v