Skip to main content

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 .

Release files for pshape 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pshape 0.2.1
File Size Uploaded
pshape-0.2.1.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pshape 0.2.1
File Interpreter ABI Platform
pshape-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.5 kB

Release files / pshape-0.2.1.tar.gz

Download URL pshape-0.2.1.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
622fc899578028582f6da586215b597438e83815db683a16fb5891e8f8e7f112
BLAKE2b-256 checksum
How to use checksums
5a7679d1569ffb9b1bcf497051e881cd065bb143ed09de5f5df3c9a2d6f028ce
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / pshape-0.2.1-py3-none-any.whl

Download URL pshape-0.2.1-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e26622564d3c43b3a3d34d5801ffc54f1b421e6d777f5b2409b16288fccb7c5
BLAKE2b-256 checksum
How to use checksums
2679236f661dda91f43458c66b93c6a4b380cc11961582f5f7527224a9525235
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page