{"id":15284527,"url":"https://github.com/alugowski/matrepr","last_synced_at":"2025-04-12T23:36:41.293Z","repository":{"id":189998199,"uuid":"681530010","full_name":"alugowski/matrepr","owner":"alugowski","description":"Format matrices and tensors to HTML, string, and LaTeX, with Jupyter integration.","archived":false,"fork":false,"pushed_at":"2024-11-18T00:48:59.000Z","size":970,"stargazers_count":13,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T23:35:46.660Z","etag":null,"topics":["data-science","data-visualization","data-viz","graphblas","jupyter","numpy","numpy-matrix","pytorch","scipy","sparse","sparse-data","sparse-matrices","sparse-matrix","sparse-representations","tensor","tensorflow","torch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alugowski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-22T08:01:09.000Z","updated_at":"2025-04-06T03:54:10.000Z","dependencies_parsed_at":"2023-09-22T06:50:49.728Z","dependency_job_id":"cbd3f47f-848a-4531-b396-4d66828cfcd2","html_url":"https://github.com/alugowski/matrepr","commit_stats":null,"previous_names":["alugowski/matrepr"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alugowski%2Fmatrepr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alugowski%2Fmatrepr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alugowski%2Fmatrepr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alugowski%2Fmatrepr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alugowski","download_url":"https://codeload.github.com/alugowski/matrepr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647257,"owners_count":21139081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-science","data-visualization","data-viz","graphblas","jupyter","numpy","numpy-matrix","pytorch","scipy","sparse","sparse-data","sparse-matrices","sparse-matrix","sparse-representations","tensor","tensorflow","torch"],"created_at":"2024-09-30T14:58:04.092Z","updated_at":"2025-04-12T23:36:41.260Z","avatar_url":"https://github.com/alugowski.png","language":"Python","readme":"[![tests](https://github.com/alugowski/matrepr/actions/workflows/tests.yml/badge.svg)](https://github.com/alugowski/matrepr/actions/workflows/tests.yml)\n[![codecov](https://codecov.io/gh/alugowski/matrepr/graph/badge.svg?token=e9QEAd57gz)](https://codecov.io/gh/alugowski/matrepr)\n[![PyPI version](https://badge.fury.io/py/matrepr.svg)](https://pypi.org/project/matrepr/)\n[![Conda Version](https://img.shields.io/conda/vn/conda-forge/matrepr.svg)](https://anaconda.org/conda-forge/matrepr)\n\n# MatRepr\n\nMake every matrix beautiful.\n\nMatRepr formats matrices and tensors to HTML, string, and LaTeX, with Jupyter integration.\nSee [Jupyter notebook for examples.](https://nbviewer.org/github/alugowski/matrepr/blob/main/doc/demo.ipynb)\n\nBrief examples:\n\n```python\nfrom matrepr import mdisplay, mprint\n```\n\n`mprint(A)` or `to_str(A)`:\n```\n\u003c1000×1000, 212345 'float64' elements, coo\u003e\n      0       1       2       3       4       5        6       7\n  ┌                                                                      ┐\n0 │                                 0.3876                           ... │\n1 │ 0.5801  0.5085          0.8927                           0.629   ... │\n2 │                                                                  ... │\n3 │                 0.7142                                           ... │\n4 │                                         0.8631                   ... │\n5 │ 0.7863  0.1298  0.9918   0.71                            0.3444  ... │\n6 │                 0.9481                          0.9609           ... │\n7 │                                                 0.09361  0.1679  ... │\n8 │                                 0.4023                           ... │\n  │   :       :       :       :       :       :        :       :     ... │\n  └                                                                      ┘\n```\n\n`mdisplay(A)` or `to_html(A)`:\n\n\u003cimg src=\"doc/images/html.png\" width=400 alt=\"HTML screenshot\"/\u003e\u003cbr\u003e\n\nSupports:\n* **SciPy** - sparse matrices and arrays like `csr_matrix` and `coo_array`\n* **NumPy** - `ndarray` [(demo)](https://nbviewer.org/github/alugowski/matrepr/blob/main/doc/demo-numpy.ipynb)\n* **[PyTorch](https://pytorch.org/docs/stable/sparse.html)** - dense and sparse `torch.Tensor` [(demo)](https://nbviewer.org/github/alugowski/matrepr/blob/main/doc/demo-pytorch.ipynb)\n* **[TensorFlow](https://www.tensorflow.org/guide/sparse_tensor)** - `tf.Tensor` and `tf.SparseTensor` [(demo)](https://nbviewer.org/github/alugowski/matrepr/blob/main/doc/demo-tensorflow.ipynb)\n* **[Python-graphblas](https://github.com/python-graphblas/python-graphblas)** - `gb.Matrix` and `gb.Vector` [(demo)](https://nbviewer.org/github/alugowski/matrepr/blob/main/doc/demo-python-graphblas.ipynb)\n* **[PyData/Sparse](https://sparse.pydata.org/)** - `COO`, `DOK`, `GCXS` [(demo)](https://nbviewer.org/github/alugowski/matrepr/blob/main/doc/demo-pydata-sparse.ipynb)\n* `list`, `tuple`, including multi-dimensional and ragged\n\nFeatures:\n* Jupyter extension to format matrices in cell outputs.\n* Configurable float precision or format string.\n* Toggle row and column indices or set your own labels.\n* Nested sub-matrices of any supported type, including mixing packages.\n* Toggle matrix description or set your own title.\n* String output can optionally autodetect terminal width.\n* Methods to directly display a matrix (`mprint`, `mdisplay` for Jupyter)\n* Methods to convert to string (`to_html`, `to_latex`, `to_str`).\n* Configurable per method call or set defaults with `matrepr.params`.\n* A `__repr__` monkey patch to format matrices in the Python shell.\n* Fast.\n\nInstall:\n```shell\npip install matrepr\n```\n\nor\n\n```shell\nconda install matrepr\n```\n\n## More Examples\n\n### HTML\n\n\u003cimg src=\"doc/images/html-cities.png\" width=400 alt=\"HTML screenshot\"/\u003e\u003cbr\u003e\n\u003cimg src=\"doc/images/html-4d.png\" width=400 alt=\"4D NumPy Array\"/\u003e\u003cbr\u003e\n\u003cimg src=\"doc/images/html-adjacency.png\" width=290 alt=\"Adjacency Matrix\"/\u003e\u003cbr\u003e  \n\n`mdisplay(A)`, `to_html(A)`  \nor simply `A` with Jupyter extension `%load_ext matrepr`\n\n\n### LaTeX\n\u003cimg src=\"doc/images/latex.png\" width=400 alt=\"LaTeX screenshot\"/\u003e\u003cbr\u003e\n\u003cimg src=\"doc/images/latex-edgecases.png\" width=600 alt=\"LaTeX edgecases screenshot\"/\u003e\n\n`mdisplay(A, 'latex')`, `to_latex(A)`  \nor simply `A` with Jupyter extension `%load_ext matrepr.latex`\n\n## Jupyter Extension\n\nMatRepr's Jupyter extension registers with [Jupyter's formatter](https://ipython.readthedocs.io/en/stable/config/integrating.html)\nto format supported matrices with MatRepr. Simply:\n\n```jupyter\n%load_ext matrepr\n```\n\nOr if you prefer LaTeX:\n```jupyter\n%load_ext matrepr.latex\n```\n\nExample:\n\n\u003cimg src=\"doc/images/jupyter_register.png\" width=600 alt=\"Jupyter extension screenshot\"/\u003e\n\n\n## Methods\n* `to_str(A)`: Format `A` as a text string.\n* `to_html(A)`: Format `A` as a plain or notebook-styled HTML table. Returns a string.\n* `to_latex(A)`: Format `A` as a LaTeX matrix. Returns a string.\n* `mprint(A)`: print `A` as a string to stdout.\n* `mdisplay(A)`: Displays the output of `to_html`, `to_latex`, or `to_str` in Jupyter.\n\n**Note:** For Spy plots see [MatSpy](https://github.com/alugowski/matspy).\n\n### Arguments\n\nAll methods take the same arguments. Apart from the matrix itself:\n\n* `title`: string label. If `True`, then a matrix description is auto generated that contains matrix shape, number and type of nonzeros, etc.\n* `indices`: Whether to show matrix indices.\n* `max_rows`, `max_rows`: size of table. Matrices larger than this are truncated with ellipses.\n* `precision`: floating-point precision\n* `num_after_dots`: How many rows/columns to show from the end of the matrix if the entire matrix does not fit.\n* `fill_value`: Value to fill empty cells.\n\n#### Overriding defaults\n`matrepr.params` contains the default values for all arguments.\n\nFor example, to always disable the title, disable indices, and only show the top-left part of the matrix:\n\n```python\nmatrepr.params.title = False\nmatrepr.params.indices = False\nmatrepr.params.num_after_dots = 0\n```\n\n## Interactive Python: Monkey Patching `__repr__`\n\nThe interactive Python REPL does not have a nice way to register a formatter.\n\nWe can monkey patch a `__repl__` method into supported matrix classes for a similar effect.\n\nThis is implemented in the [matrepr.patch](matrepr/patch) module. Simply import the patch you want:\n\n* `import matrepr.patch.scipy`\n* `import matrepr.patch.graphblas`\n* `import matrepr.patch.sparse`\n\nExample:\n\n```\n\u003e\u003e\u003e a = scipy.sparse.random(4, 4, density=0.5)\n\u003e\u003e\u003e a\n\u003c4x4 sparse matrix of type '\u003cclass 'numpy.float64'\u003e'\n\twith 8 stored elements in COOrdinate format\u003e\n\u003e\u003e\u003e import matrepr.patch.scipy\n\u003e\u003e\u003e a\n\u003c4×4, 8 'float64' elements, coo\u003e\n      0       1        2        3\n  ┌                                  ┐\n0 │ 0.6536          0.008388  0.6564 │\n1 │                                  │\n2 │         0.2987   0.8098          │\n3 │ 0.1064  0.9613   0.7477          │\n  └                                  ┘\n```\n\n## Edge Cases\n\nMatRepr gracefully handles:\n * multiple elements with the same coordinates (i.e. duplicates)\n * nested matrices\n * complex values\n * string values (including multiline)\n * LaTeX scientific notation as $`\\times 10^{power}`$\n\nSee [demo-edgecases notebook](https://nbviewer.org/github/alugowski/matrepr/blob/main/doc/demo-edgecases.ipynb) for more.\n\n## How does it work?\n\nEach package that MatRepr supports implements two classes:\n\n* `Driver`: Declares what types are supported and supplies an adapter.\n  * `get_supported_types`: This declares what types are supported, as strings to avoid unnecessary imports.\n  * `adapt(A)`: Returns a `MatrixAdapter` for a matrix that this driver supports.\n* Implement any of these `MatrixAdapter` classes:\n  * `MatrixAdapterRow`: for structs able to efficiently read a selected row.\n  * `MatrixAdapterCol`: for structs able to efficiently read a selected column.\n  * `MatrixAdapterCoo`: for structs able to extract a portion of the matrix as tuples.\n\nSee [matrepr.adapters](matrepr/adapters) module for details.\n\nYou may use `matspy.register_driver` to register a Driver for your own matrix class.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falugowski%2Fmatrepr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falugowski%2Fmatrepr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falugowski%2Fmatrepr/lists"}