GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Consider MatRepr for `__repr__` instead of Pandas · Issue #494 · python-graphblas/python-graphblas

Consider MatRepr for __repr__ instead of Pandas #494

New issue
Copy link
New issue
Copy link
Open
Open
Consider MatRepr for __repr__ instead of Pandas#494
Copy link

Description

@alugowski
alugowski
opened on Aug 25, 2023
Issue body actions

https://pypi.org/project/matrepr/

Benefits:

  • output looks like a matrix, not a dataframe
  • faster, especially on first run where the pandas import is slow
  • small code

See it in action with python-graphblas:
https://nbviewer.org/github/alugowski/matrepr/blob/main/doc/demo-python-graphblas.ipynb

To try yourself, use matrepr's jupyter integration (causes Jupyter to use matrepr's formatter instead of the native one):

import matrepr.jupyter

For the Python REPL, a __repr__ monkey patch does the same:

import matrepr.patch.graphblas
Reacted by Sultan Orazbayev and Erik Welch

Activity

  1. eriknw commented on Aug 25, 2023

    @eriknw
    eriknw
    on Aug 25, 2023
    Member
    More actions

    I like it! I/we will definitely take a closer look. Thanks @alugowski

    It definitely looks more like a matrix, and I was thinking just the other day that pandas is a rather heavy dependency that we only use for our repr.

    How well does (or will or can) it handle more advanced numpy dtypes? For example:

    • np.dtype([("x", np.bool_), ("y", np.int64)], align=True)
      • We currently render an element of this dtype as e.g. (False, 2)
    • np.dtype("(3,)uint16")
      • We currently render an element of this dtype as e.g. [1, 2, 3]
  2. alugowski commented on Aug 26, 2023

    @alugowski
    alugowski
    on Aug 26, 2023
    ContributorAuthor
    More actions

    How well does (or will or can) it handle more advanced numpy dtypes? For example:

    * `np.dtype([("x", np.bool_), ("y", np.int64)], align=True)`
      
      * We currently render an element of this dtype as e.g. `(False, 2)`
    
    * `np.dtype("(3,)uint16")`
      
      * We currently render an element of this dtype as e.g. `[1, 2, 3]`
    

    I had to do a small patch, but both those now render the same in all three output types (html, latex, string). It just calls str(), which I assume is what you do too.

  3. alugowski commented on Aug 26, 2023

    @alugowski
    alugowski
    on Aug 26, 2023
    ContributorAuthor
    More actions
    * `np.dtype("(3,)uint16")`
      
      * We currently render an element of this dtype as e.g. `[1, 2, 3]`
    

    One question about this one.

    Do you prefer it to be rendered like that string, or as a nested submatrix?

    For reference, here is how I currently render some interesting values:
    image

    Row 3 is nested matrices of types matrepr supports (lists, lists of lists, scipy matrices. a graphblas matrix would look like that too).

    I ask because I'm considering adding dense numpy arrays as a supported package. Since AFAIK objects of type np.dtype("(3,)uint16") appear just like any other numpy array, one option is to render it like a submatrix instead of str().

  4. eriknw commented on Aug 26, 2023

    @eriknw
    eriknw
    on Aug 26, 2023
    Member
    More actions

    Ooh, nested sub-matrix is pretty cool!

    Right now we just do what is easiest (hence using pandas in the first place). For submatrix dtypes, the easiest is to turn them into lists, vals.tolist(), and let pandas render each entry as a Python list.

    I'm happy to revisit how we render elements and to try what you think is nicest :)

  5. alugowski commented on Aug 28, 2023

    @alugowski
    alugowski
    on Aug 28, 2023
    ContributorAuthor
    More actions

    Sounds good!

    I added ndarray support so numpy submatricies get rendered all pretty now, too:

    image

    Let me know how it looks on your datasets. Happy to make tweaks!

  6. alugowski commented on Aug 28, 2023

    @alugowski
    alugowski
    on Aug 28, 2023
    ContributorAuthor
    More actions

    BTW this is all that is required to use MatRepr with python-graphblas:

    def _str_(mat):
        from matrepr import to_str
        from matrepr.adapters.graphblas_driver import GraphBLASDriver
        # Enable terminal width detection
        return to_str(GraphBLASDriver.adapt(mat), width_str=0, max_cols=9999)
    
    
    graphblas.Matrix.__repr__ = _str_
    graphblas.Vector.__repr__ = _str_
    Reacted by Sultan Orazbayev
  7. alugowski commented on Sep 7, 2023

    @alugowski
    alugowski
    on Sep 7, 2023
    ContributorAuthor
    More actions

    One newer feature is to support custom row/column labels. This is handy for something like an adjacency matrix where the matrix elements are edge weights and row/column labels are the vertex weights.

    image
    Reacted by Sultan Orazbayev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.