Skip to content

Commit 724a556

Browse files
committed
Update for new version
1 parent 2206aac commit 724a556

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.ipynb_checkpoints
33
build
44
p2j.egg-info
5-
dist
5+
dist
6+
.pyc

README.md

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1-
# p2j - Python to Jupyter Notebook [![PyPI version](https://badge.fury.io/py/p2j.svg)](https://badge.fury.io/py/p2j)
1+
# p2j - Python to Jupyter Notebook Parser [![PyPI version](https://badge.fury.io/py/p2j.svg)](https://badge.fury.io/py/p2j)
22

3-
Convert your Python source code to Jupyter notebook with zero intervention.
3+
Convert your Python source code to Jupyter notebook with **zero intervention**.
44

5-
See an example of a [Python source code](p2j/examples/example2.py) and its [Jupyter notebook](p2j/examples/example2.ipynb) after converting.
5+
Here is an example.
6+
7+
```python
8+
# Evaluate the model
9+
model.evaluate()
10+
11+
# Run the model for a while.
12+
# Then we hide the model.
13+
run()
14+
hide()
15+
16+
print(type(data))
17+
18+
# This is considered as a paragraph too
19+
# It has 2 lines of comments
20+
21+
# The data that we are interested in is made of 8x8 images of digits.
22+
# Let's have a look at the first 4 images, which is of course
23+
# stored in the `images` attribute of the dataset.
24+
images = list(zip(mnist.images))
25+
```
26+
27+
which translates to the following:
28+
29+
![example](screenshot.png)
30+
31+
Here's another example of a [Python source code](p2j/examples/example2.py) and its [Jupyter notebook](p2j/examples/example2.ipynb) after converting.
632

733
The purpose of this package is to be able to run a code on Jupyter notebook without having to copy each paragraph of the code into every cell. It's also useful if we want to run our code in Google Colab. This parser isn't perfect, but you would be satisfactorily pleased with what you get.
834

@@ -65,7 +91,7 @@ optional arguments:
6591

6692
## Requirements
6793

68-
- Python 3.6
94+
- Python >= 3.6
6995

7096
No third party libraries are used.
7197

@@ -175,6 +201,10 @@ print(type(data))
175201
images = list(zip(mnist.images))
176202
```
177203

204+
which translates to the following:
205+
206+
![example](screenshot.png)
207+
178208
### 4. Indentation
179209

180210
Any line of code or comment that is indented by a multiple of 4 spaces is considered code, and will stay in the same code cell as the previous non-empty line. This ensures that function and class definitions, loops and multi-line code stay in one cell.

p2j/examples/example.ipynb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["\nTrains a denoising autoencoder on MNIST dataset.<br>\n", "Denoising is one of the classic applications of autoencoders.<br>\n", "The denoising process removes unwanted noise that corrupted the<br>\n", "true signal.<br>\n", "Noise + Data ---> Denoising Autoencoder ---> Data<br>\n", "Given a training dataset of corrupted data as input and<br>\n", "true signal as output, a denoising autoencoder can recover the<br>\n", "hidden structure to generate clean data.<br>\n", "This example has modular design. The encoder, decoder and autoencoder<br>\n", "are 3 models that share weights. For example, after training the<br>\n", "autoencoder, the encoder can be used to generate latent vectors<br>\n", "of input data for low-dim visualization like PCA or TSNE.<br>\n", ""]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from __future__ import absolute_import\n", "from __future__ import division\n", "from __future__ import print_function\n", "import keras\n", "from keras.layers import Activation, Dense, Input\n", "from keras.layers import Conv2D, Flatten\n", "from keras.layers import Reshape, Conv2DTranspose\n", "from keras.models import Model\n", "from keras import backend as K\n", "from keras.datasets import mnist\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from PIL import Image"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["np.random.seed(1337)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["MNIST dataset"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["(x_train, _), (x_test, _) = mnist.load_data()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["image_size = x_train.shape[1]\n", "x_train = np.reshape(x_train, [-1, image_size, image_size, 1])\n", "x_test = np.reshape(x_test, [-1, image_size, image_size, 1])\n", "x_train = x_train.astype('float32') / 255\n", "x_test = x_test.astype('float32') / 255"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Generate corrupted MNIST images by adding noise with normal dist<br>\n", "centered at 0.5 and std=0.5"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["noise = np.random.normal(loc=0.5, scale=0.5, size=x_train.shape)\n", "x_train_noisy = x_train + noise\n", "noise = np.random.normal(loc=0.5, scale=0.5, size=x_test.shape)\n", "x_test_noisy = x_test + noise"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["x_train_noisy = np.clip(x_train_noisy, 0., 1.)\n", "x_test_noisy = np.clip(x_test_noisy, 0., 1.)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Network parameters"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["input_shape = (image_size, image_size, 1)\n", "batch_size = 128\n", "kernel_size = 3\n", "latent_dim = 16\n", "# Encoder/Decoder number of CNN layers and filters per layer\n", "layer_filters = [32, 64]"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Build the Autoencoder Model<br>\n", "First build the Encoder Model"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["inputs = Input(shape=input_shape, name='encoder_input')\n", "x = inputs\n", "# Stack of Conv2D blocks\n", "# Notes:\n", "# 1) Use Batch Normalization before ReLU on deep networks\n", "# 2) Use MaxPooling2D as alternative to strides>1\n", "# - faster but not as good as strides>1\n", "for filters in layer_filters:\n", " x = Conv2D(filters=filters,\n", " kernel_size=kernel_size,\n", " strides=2,\n", " activation='relu',\n", " padding='same')(x)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Shape info needed to build Decoder Model"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["shape = K.int_shape(x)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Generate the latent vector"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["x = Flatten()(x)\n", "latent = Dense(latent_dim, name='latent_vector')(x)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Instantiate Encoder Model"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["encoder = Model(inputs, latent, name='encoder')\n", "encoder.summary()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Build the Decoder Model"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["latent_inputs = Input(shape=(latent_dim,), name='decoder_input')\n", "x = Dense(shape[1] * shape[2] * shape[3])(latent_inputs)\n", "x = Reshape((shape[1], shape[2], shape[3]))(x)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Stack of Transposed Conv2D blocks<br>\n", "Notes:<br>\n", "1) Use Batch Normalization before ReLU on deep networks<br>\n", "2) Use UpSampling2D as alternative to strides>1<br>\n", "- faster but not as good as strides>1"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["for filters in layer_filters[::-1]:\n", " x = Conv2DTranspose(filters=filters,\n", " kernel_size=kernel_size,\n", " strides=2,\n", " activation='relu',\n", " padding='same')(x)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["x = Conv2DTranspose(filters=1,\n", " kernel_size=kernel_size,\n", " padding='same')(x)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["outputs = Activation('sigmoid', name='decoder_output')(x)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Instantiate Decoder Model"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["decoder = Model(latent_inputs, outputs, name='decoder')\n", "decoder.summary()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Autoencoder = Encoder + Decoder<br>\n", "Instantiate Autoencoder Model"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["autoencoder = Model(inputs, decoder(encoder(inputs)), name='autoencoder')\n", "autoencoder.summary()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["autoencoder.compile(loss='mse', optimizer='adam')"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Train the autoencoder"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["autoencoder.fit(x_train_noisy,\n", " x_train,\n", " validation_data=(x_test_noisy, x_test),\n", " epochs=30,\n", " batch_size=batch_size)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Predict the Autoencoder output from corrupted test images"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["x_decoded = autoencoder.predict(x_test_noisy)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Display the 1st 8 corrupted and denoised images"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["rows, cols = 10, 30\n", "num = rows * cols\n", "imgs = np.concatenate([x_test[:num], x_test_noisy[:num], x_decoded[:num]])\n", "imgs = imgs.reshape((rows * 3, cols, image_size, image_size))\n", "imgs = np.vstack(np.split(imgs, rows, axis=1))\n", "imgs = imgs.reshape((rows * 3, -1, image_size, image_size))\n", "imgs = np.vstack([np.hstack(i) for i in imgs])\n", "imgs = (imgs * 255).astype(np.uint8)\n", "plt.figure()\n", "plt.axis('off')\n", "plt.title('Original images: top rows, '\n", " 'Corrupted Input: middle rows, '\n", " 'Denoised Input: third rows')\n", "plt.imshow(imgs, interpolation='none', cmap='gray')\n", "Image.fromarray(imgs).save('corrupted_and_denoised.png')\n", "plt.show()"]}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4"}}, "nbformat": 4, "nbformat_minor": 2}

p2j/p2j.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This code translate .py files to .ipynb
2+
This code translates .py files to .ipynb
33
"""
44
# Standard imports for file handling and JSON files
55
import argparse

screenshot.png

131 KB
Loading

setup.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Package meta-data.
88
NAME = "p2j"
9-
VERSION = "1.2.1"
9+
VERSION = "1.2.2"
1010
DESCRIPTION = "p2j: Convert Python scripts to Jupyter notebook with minimal intervention"
1111
URL = "https://github.com/raibosome/python2jupyter"
1212
AUTHOR = "Raimi bin Karim"
@@ -18,16 +18,16 @@
1818
HERE = os.path.abspath(os.path.dirname(__file__))
1919
try:
2020
with io.open(os.path.join(HERE, 'README.md'), encoding='utf-8') as f:
21-
long_description = '\n' + f.read()
21+
LONG_DESCRIPTION = '\n' + f.read()
2222
except FileNotFoundError:
23-
long_description = DESCRIPTION
23+
LONG_DESCRIPTION = DESCRIPTION
2424

2525
# This call to setup() does all the work
2626
setup(
2727
name=NAME,
2828
version=VERSION,
2929
description=DESCRIPTION,
30-
long_description=long_description,
30+
long_description=LONG_DESCRIPTION,
3131
long_description_content_type="text/markdown",
3232
url=URL,
3333
author=AUTHOR,

0 commit comments

Comments
 (0)