Skip to main content

You can easily add numpy.ndarray.shape information to your script as comments.

Project description

Shape Commentator

CircleCI

About

You can easily add numpy.ndarray.shape, torch.Size, other "shape" and type information to your script as comments.

Sample

An article about this tool is here. (in Japanese) NumPyやPyTorchで使える超便利ツールを作った

Getting Started

For Jupyter Notebook User

This is a Jupyter Notebook extension of this tool. It is easier to use!

use_fig

NumPy

pip install shape_commentator numpy
echo -e "import numpy as np\na = np.array([1,2,3])" > src.py
python -m shape_commentator src.py
cat src.py.commented.py
import numpy as np
a = np.array([1,2,3])  #_ (3,)

PyTorch

pip install shape_commentator torch
echo -e "import torch\na = torch.tensor([1,2,3])" > src.py
python -m shape_commentator src.py
cat src.py.commented.py
import torch
a = torch.tensor([1,2,3])  #_ torch.Size([3])

Table Of Contents

Usage

Execute as a Module

Create Commented Script to File

  1. Run this script as a module with argument of script name. Command line arguments to the target script are available.
python -m shape_commentator src.py [args]
  1. You get the commented script. For example, you execute shape_commentator to src.py, you get src.py.commented.py.

src.py

import numpy as np
a = np.array([1,2,3,4,5,6])
b = np.array([0,1,2,3,4,5])
ab_h = np.hstack((a,b))
ab_v = np.vstack((a,b))
ab = np.dot(a,b)
AA, BB = np.meshgrid(a,b)
i = 1
f = 1.0
c = 1 + 1j
s = "string1"
class A():
    pass
a = A()

src.py.commented.py

import numpy as np
a = np.array([1,2,3,4,5,6])  #_ (6,)
b = np.array([0,1,2,3,4,5])  #_ (6,)
ab_h = np.hstack((a,b))  #_ (12,)
ab_v = np.vstack((a,b))  #_ (2, 6)
ab = np.dot(a,b)  #_ ()
AA, BB = np.meshgrid(a,b)  #_ [(6, 6),(6, 6),]
i = 1  #_ int
f = 1.0  #_ float
c = 1 + 1j  #_ complex
s = "string1"  #_ str
class A():
    pass
a = A()  #_ A

Create Commented Script to Standard Output

By using shape_commentator.print_comment module, you get the commented script in standard output. (Not src.py.commented.py)

python -m shape_commentator.print_comment src.py [args]

Clear Shape Comment

By using shape_commentator.print_clear module, you get the uncommented script in standard output.

python -m shape_commentator.print_clear src.py [args]

Jupyter Notebook

Jupyter Notebook Extension is HERE

IPython

Create Commented Script

  1. Execute the cell that you want to see shape.
  2. Run the cell below, and the commented source code will be outputted. (In[len(In)-2] is the source code in the cell that you ran just before.)
import shape_commentator
shape_commentator.comment(In[len(In)-2],globals())

ipython_comment

Clear Shape Comment

import shape_commentator
shape_commentator.clear(In[len(In)-2])

ipython_clear

Tested Python Version

Test script must be written in these version of Python.

  • 3.6.6
  • 3.5.6
  • 3.4.9
  • 2.7.15

Development

Python Main Version in Development

  • 3.6.6

Installation

pip install numpy
sudo apt install bats
sh tests/install_for_dev.sh

Sample

# Module Mode
python -m shape_commentator tests/input_scripts/numpy_compute.py
# Method Mode (Use in IPython / Jupyter Notebook.)
python tests/comment_method.py tests/input_scripts/numpy_compute.py 

Test

python -m doctest shape_commentator/shape_commentator.py
sh tests/install_for_dev.sh
bats tests/test_all.bats
python setup.py develop --uninstall

Changing Test Scripts

Remove remove_tested_scripts in tests/test_all.bats and run bats tests/test_all.bats, and you get new test script in tests/input_scripts/

Try Master Branch

The package of shape_commentator in TestPyPI is the HEAD of master branch.
You can try newest (but under development) version by running commands below.

pip uninstall -y shape-commentator
pip install --index-url https://test.pypi.org/simple/ shape-commentator

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

shape_commentator-0.6.2.tar.gz (7.4 kB view hashes)

Uploaded Source

Supported by

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