Skip to main content

Prints NumPy-like arrays' shapes, mins, means, and maxes, as well as the names of the input variable outside the functions' scope

Project description

Pshape

PyPI version

Prints shapes of NumPy-like arrays, as well as many more useful debugging metrics, along with the names of the input variable outside the functions' scope.

This comes in very handy when debugging complex programs that manipulate huge ndarrays (aka Tensors) where shape (dimensions) and value ranges vary widely and are hard to inspect.

I got tired of writing tons of print("my_tensor", my_tensor.shape, my_tensor.min(), my_tensor.max()) over and over, so I made that utility, but then I got tired of copy/pasting it into every new projects from my Gist of it, so here I finally made it a library that I can pip install everywhere.

Getting started

pip3 install pshape

pshape works with Numpy, PyTorch and Tensorflow, though TF is the least stable of the three since I don't test it extensively, but feel free to contribute ! You can mix and match pshape calls containing Numpy, Pytorch and Tensorflow arrays.

I try to make pshape as "safe" as possible, so that it never crashes your code, because it's not crucial that it work as a tool and it'd be silly to risk the whole program execution over a stupid bug in a debugging tool.

Caveats

Because pshape uses the inpect built-in Python module, it can't analyse source code from the REPL interpreter, and hence it won't be able to see the variable names if you try something like:

$ python3
>>> import numpy as np
>>> from pshape import pshape
>>> pshape(np.arange(10))
arr_1 (10,) 0 4.5 9

As you can see the name used is not np.arange(10) as it should be, and defaults instead to arr_1, arr_2, etc.

The only way that the inspect module can display source code is if the code came from a file that it can access. Source typed at an interactive prompt is discarded as soon as it is parsed, there's simply no way for inspect to access it. – @jasonharper

Usage

>>> from pshape import pshape
>>> import numpy as np
>>> import torch
>>> 
>>> pshape(np.arange(10).reshape(5,2,1), heading=True)

name                         shape     dtype min    mean   max
np.arange(10).reshape(5,2,1) (5, 2, 1) int64 0.0000 4.5000 9.0000

>>> pshape(np.eye(4), np.arange(10).reshape(5,2,1), heading=True)

name                         shape     dtype   min    mean   max
np.eye(4)                    (4, 4)    float64 0.0000 0.2500 1.0000
np.arange(10).reshape(5,2,1) (5, 2, 1) int64   0.0000 4.5000 9.0000

>>> cool_arr1 = np.random.rand(123,4,2,1)
>>> cool_arr2 = np.random.rand(123,4,2,2)
>>> cool_arr3 = np.random.rand(123,4,2,3)
>>> pshape(cool_arr1, cool_arr2, cool_arr3)

cool_arr1 (123, 4, 2, 1) float64 0.0004 0.4961 1.0000
cool_arr2 (123, 4, 2, 2) float64 0.0006 0.4947 1.0000
cool_arr3 (123, 4, 2, 3) float64 0.0017 0.4997 0.9996

>>> pshape(cool_arr1, np.arange(12).reshape(3,4), cool_arr3)

cool_arr1                  (123, 4, 2, 1) float64 0.0004 0.4961 1.0000
np.arange(12).reshape(3,4) (3, 4)         int64   0.0000 5.5000 11.0000
cool_arr3                  (123, 4, 2, 3) float64 0.0017 0.4997 0.9996

>>> pshape(torch.arange(12).view(3,4), np.arange(12).reshape(3,4), cool_arr3)

name                       shape          device dtype       min    mean   max
torch.arange(12).view(3,4) (3, 4)         cpu    torch.int64 0.0000 5.5000 11.0000
np.arange(12).reshape(3,4) (3, 4)         N/A    int64       0.0000 5.5000 11.0000
cool_arr3                  (123, 4, 2, 3) N/A    float64     0.0017 0.4997 0.9996

To get a similar display, you can run the demo_pshape.py script at the root of this repo.

Development

To install the latest version from Github, run:

git clone git@github.com:sam1902/pshape.git pshape
cd pshape
pip3 install -U .

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

pshape-0.2.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pshape-0.2.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file pshape-0.2.1.tar.gz.

File metadata

  • Download URL: pshape-0.2.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for pshape-0.2.1.tar.gz
Algorithm Hash digest
SHA256 622fc899578028582f6da586215b597438e83815db683a16fb5891e8f8e7f112
MD5 e1ae7e5c544893480513d8ae12f9c0cb
BLAKE2b-256 5a7679d1569ffb9b1bcf497051e881cd065bb143ed09de5f5df3c9a2d6f028ce

See more details on using hashes here.

File details

Details for the file pshape-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pshape-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for pshape-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e26622564d3c43b3a3d34d5801ffc54f1b421e6d777f5b2409b16288fccb7c5
MD5 b6856c5a495bd707a96affc1e3abb9a8
BLAKE2b-256 2679236f661dda91f43458c66b93c6a4b380cc11961582f5f7527224a9525235

See more details on using hashes here.

Supported by

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