Skip to content

Commit

Permalink
better pyplot support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdremov committed Nov 10, 2023
1 parent b3ce027 commit 00f9b4a
Show file tree
Hide file tree
Showing 13 changed files with 953 additions and 29 deletions.
42 changes: 27 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,47 @@ Decorator `@igogo.job` has several useful parameters.
Allows to set how to render output. Possible options: `text`, `markdown`, `html` Default: `text`
- `displays`\
As igogo job modify already executed cell, it needs to have spare placeholders for rich output.
This parameter specifies how many spare displays to spawn. Default: `10`
This parameter specifies how many spare displays to spawn. Default: `1`
- `name`\
User-friendly name of igogo job.
- `warn_rewrite`\
Should warn rewriting older displays? Default: `True`
- `auto_display_figures`\
Should display pyplot figures created inside igogo automatically? Default: `True`

Markdown example:

https://user-images.githubusercontent.com/25539425/227203729-af94582c-8fe2-40fe-a6f0-6489a374a88f.mov

### Display Additional Data

You can use `igogo.display` inside a job to display additional content.
For example, you can show pyplot figures.
Pyplot figures will be automatically displayed in igogo cell.

You can also use `igogo.display` inside a job to display any other content or several figures. Mind that displays must be pre-allocated by specifying displays number in `igogo.job(displays=...)`

```python
import numpy as np
import matplotlib.pyplot as plt
import igogo

def experiment(name, f, i):
x = np.linspace(0.01, i, 1000)
fig, ax = plt.subplots(figsize=(5, 1))
ax.plot(x, f(x), c='r')
ax.set_title(name)

# display figure to job's output.
# if called from outside job, falls back to
# IPython.display.display
igogo.display(fig)
# close pyplot so that it does not show content
# automatically
plt.close()
x = np.linspace(0, i / 10, 100)
fig = plt.figure()
plt.plot(
x,
f(x)
)
plt.gca().set_title(name)
igogo.display(fig)

fig = plt.figure()
plt.scatter(
x,
f(x)
)
plt.gca().set_title(name)
igogo.display(fig)
igogo.sleep(0.05)
```

As noted in "Configure jobs" section, `igogo` jobs have limited number of displays.
Expand Down Expand Up @@ -168,6 +176,10 @@ Runs but has problems with output from igogo jobs. Jobs are executed, but there

## More Examples

[**Check out pretty notebooks**](https://github.com/alexdremov/igogo/tree/main/examples)

---

### Train model and check metrics

https://user-images.githubusercontent.com/25539425/227651626-cba8a317-a986-4971-9639-84cdb388e2d3.mov
Expand Down
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.ipynb_checkpoints
170 changes: 170 additions & 0 deletions examples/chunks.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "e94f1c52-fd29-4994-8d52-6a2faa825931",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The igogo extension is already loaded. To reload it, use:\n",
" %reload_ext igogo\n"
]
}
],
"source": [
"%load_ext igogo\n",
"import igogo\n",
"import numpy as np\n",
"from tqdm.auto import tqdm\n",
"\n",
"raw_data = np.random.randn(5000000, 100)\n",
"\n",
"igogo_yield_freq = 32\n",
"igogo_first_step_cache = []\n",
"\n",
"result = []"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c9a8edfc-dd51-420a-81a5-7f6c9fb3ae63",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d733eaa11ef44ca7991070c62f13d653",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/5000000 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%igogo\n",
"\n",
"for i in tqdm(range(len(raw_data))):\n",
" processed = np.log(raw_data[i] * raw_data[i])\n",
" igogo_first_step_cache.append(processed)\n",
" \n",
" if i > 0 and i % igogo_yield_freq == 0:\n",
" igogo.yielder() # allow other jobs to execute"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9baf82be-5ef3-49ad-9006-2f026ee1a782",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1130932025094e7f9e9a43a76d49e820",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/5000000 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%igogo\n",
"\n",
"for i in tqdm(range(len(raw_data))):\n",
" while i >= len(igogo_first_step_cache): # wait for producer to process data\n",
" igogo.yielder()\n",
" \n",
" result.append(np.mean(igogo_first_step_cache[i]))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eee93ec9-491e-485a-8af8-1cc608ccd271",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
140 changes: 140 additions & 0 deletions examples/display.ipynb

Large diffs are not rendered by default.

134 changes: 134 additions & 0 deletions examples/display_custom.ipynb

Large diffs are not rendered by default.

120 changes: 120 additions & 0 deletions examples/hello_world.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "0c6f9554-c2c8-4c31-952a-4599234331c5",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <style>\n",
" div.output_text { padding: 0; }\n",
" div.output_text pre:empty { padding: 0; }\n",
" </style>\n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import igogo\n",
"\n",
"@igogo.job\n",
"def hello_world(name):\n",
" for i in range(3):\n",
" print(\"Hello, world from\", name)\n",
" \n",
" # allows other jobs to run while asleep\n",
" # also can be `igogo.yielder()`\n",
" igogo.sleep(1) \n",
" return name"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e448de2f-d7e5-4bea-9f8e-6be34ebfc7cf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"Hello, world from igogo\n",
"Hello, world from igogo\n",
"Hello, world from igogo\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"Hello, world from other igogo\n",
"Hello, world from other igogo\n",
"Hello, world from other igogo\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"hello_world('igogo'), hello_world('other igogo');"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 00f9b4a

Please sign in to comment.