Skip to content
Snippets Groups Projects
Commit 7ac5afc1 authored by Massimiliano Galli's avatar Massimiliano Galli
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:b1a69c95-894e-4819-b460-e6ed7a94dcf2 tags:
# Test Interactive Notebook with Bokeh
%% Cell type:code id:1eb655ad-1843-44b4-86f8-b78607465a8a tags:
``` python
from ipywidgets import interact
import numpy as np
from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
output_notebook()
```
%% Cell type:code id:1875b997-5bf8-4c29-8797-8c3850caadc1 tags:
``` python
x = np.linspace(0, 2*np.pi, 2000)
y = np.sin(x)
```
%% Cell type:code id:f3f5511c-d425-4d82-9141-ca8620dd6578 tags:
``` python
p = figure(title="simple line example")
r = p.line(x, y, color="#8888cc", line_width=1.5, alpha=0.8)
```
%% Cell type:code id:39c328c0-d38f-4662-bf6c-67c266aab39f tags:
``` python
def update(f, w=1, A=1, phi=0):
if f == "sin": func = np.sin
elif f == "cos": func = np.cos
r.data_source.data['y'] = A * func(w * x + phi)
show(p, notebook_handle=True);
```
%% Cell type:code id:da6ea44f-e295-4bc9-8d37-fe9861e5d437 tags:
``` python
interact(update, f=["sin", "cos"], w=(0,50), A=(1,10), phi=(0, 20, 0.1));
```
%% Cell type:markdown id:81f4008d-51a0-4d65-8fcd-8f5451e0c3c8 tags:
# Test Interactive Notebooks
%% Cell type:code id:0a014be6-ad32-4378-b187-cdbaa8c40446 tags:
``` python
import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np
%matplotlib widget
```
%% Cell type:code id:b8a4d872-9502-4bf8-88df-f37ea0d46039 tags:
``` python
# Non-interactive plot
mu = 0
sigma = 0.1
events = 100000
data = np.random.normal(mu, sigma, events)
fig, ax = plt.subplots()
ax.hist(data, bins=100);
```
%% Cell type:code id:cc861e62-fc77-4693-8ca6-73388573a08c tags:
``` python
# Interactive plot
x = np.linspace(0,10)
def sine_func(x, w, amp):
return amp*np.sin(w*x)
fig, ax = plt.subplots()
@widgets.interact(w=(0, 4, 0.25), amp=(0, 4, .1))
def update(w=1, amp=1):
ax.clear()
ax.set_ylim(-4, 4)
ax.plot(x, sine_func(x, w, amp))
```
%% Cell type:code id:50c499b7-af5d-419d-a05b-0cd07d4b6c9f tags:
``` python
```
# Book settings
# Learn more at https://jupyterbook.org/customize/config.html
title: Example Book
author: Massimiliano Galli
# Force re-execution of notebooks on each build.
# See https://jupyterbook.org/content/execute.html
execute:
execute_notebooks: force
# Define the name of the latex output file for PDF builds
latex:
latex_documents:
targetname: book.tex
# Add a bibtex file so that we can create citations
bibtex_bibfiles:
- references.bib
# Information about where the book exists on the web
repository:
url: https://github.com/maxgalli/PythonExamples # Online location of your book
path_to_book: jb/book # Optional path to your book, relative to the repository root
# Add GitHub buttons to your book
# See https://jupyterbook.org/customize/config.html#add-a-link-to-your-repository
html:
use_issues_button: true
use_repository_button: true
launch_buttons:
notebook_interface: jupyterlab
thebe: true
use_show_widgets_button: true
colab_url: https://colab.research.google.com
# Table of contents
# Learn more at https://jupyterbook.org/customize/toc.html
format: jb-book
root: intro
chapters:
- file: markdown
- file: notebooks
- file: interactive_notebook
- file: interactive_bokeh_notebook
%% Cell type:markdown id:b1a69c95-894e-4819-b460-e6ed7a94dcf2 tags:
# Test Interactive Notebook with Bokeh
%% Cell type:code id:1eb655ad-1843-44b4-86f8-b78607465a8a tags:
``` python
from ipywidgets import interact
import numpy as np
from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
output_notebook()
```
%% Cell type:code id:1875b997-5bf8-4c29-8797-8c3850caadc1 tags:
``` python
x = np.linspace(0, 2*np.pi, 2000)
y = np.sin(x)
```
%% Cell type:code id:f3f5511c-d425-4d82-9141-ca8620dd6578 tags:
``` python
p = figure(title="simple line example")
r = p.line(x, y, color="#8888cc", line_width=1.5, alpha=0.8)
```
%% Cell type:code id:39c328c0-d38f-4662-bf6c-67c266aab39f tags:
``` python
def update(f, w=1, A=1, phi=0):
if f == "sin": func = np.sin
elif f == "cos": func = np.cos
r.data_source.data['y'] = A * func(w * x + phi)
show(p, notebook_handle=True);
```
%% Cell type:code id:da6ea44f-e295-4bc9-8d37-fe9861e5d437 tags:
``` python
interact(update, f=["sin", "cos"], w=(0,50), A=(1,10), phi=(0, 20, 0.1));
```
%% Cell type:markdown id:81f4008d-51a0-4d65-8fcd-8f5451e0c3c8 tags:
# Test Interactive Notebooks
%% Cell type:code id:0a014be6-ad32-4378-b187-cdbaa8c40446 tags:
``` python
import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np
%matplotlib widget
```
%% Cell type:code id:b8a4d872-9502-4bf8-88df-f37ea0d46039 tags:
``` python
# Non-interactive plot
mu = 0
sigma = 0.1
events = 100000
data = np.random.normal(mu, sigma, events)
fig, ax = plt.subplots()
ax.hist(data, bins=100);
```
%% Cell type:code id:cc861e62-fc77-4693-8ca6-73388573a08c tags:
``` python
# Interactive plot
x = np.linspace(0,10)
def sine_func(x, w, amp):
return amp*np.sin(w*x)
fig, ax = plt.subplots()
@widgets.interact(w=(0, 4, 0.25), amp=(0, 4, .1))
def update(w=1, amp=1):
ax.clear()
ax.set_ylim(-4, 4)
ax.plot(x, sine_func(x, w, amp))
```
%% Cell type:code id:50c499b7-af5d-419d-a05b-0cd07d4b6c9f tags:
``` python
```
# Welcome to your Jupyter Book
This is a small sample book to give you a feel for how book content is
structured.
:::{note}
Here is a note!
:::
And here is a code block:
```
e = mc^2
```
Check out the content pages bundled with this sample book to see more.
book/logo.png

9.62 KiB

# Markdown Files
Whether you write your book's content in Jupyter Notebooks (`.ipynb`) or
in regular markdown files (`.md`), you'll write in the same flavor of markdown
called **MyST Markdown**.
## What is MyST?
MyST stands for "Markedly Structured Text". It
is a slight variation on a flavor of markdown called "CommonMark" markdown,
with small syntax extensions to allow you to write **roles** and **directives**
in the Sphinx ecosystem.
## What are roles and directives?
Roles and directives are two of the most powerful tools in Jupyter Book. They
are kind of like functions, but written in a markup language. They both
serve a similar purpose, but **roles are written in one line**, whereas
**directives span many lines**. They both accept different kinds of inputs,
and what they do with those inputs depends on the specific role or directive
that is being called.
### Using a directive
At its simplest, you can insert a directive into your book's content like so:
````
```{mydirectivename}
My directive content
```
````
This will only work if a directive with name `mydirectivename` already exists
(which it doesn't). There are many pre-defined directives associated with
Jupyter Book. For example, to insert a note box into your content, you can
use the following directive:
````
```{note}
Here is a note
```
````
This results in:
```{note}
Here is a note
```
In your built book.
For more information on writing directives, see the
[MyST documentation](https://myst-parser.readthedocs.io/).
### Using a role
Roles are very similar to directives, but they are less-complex and written
entirely on one line. You can insert a role into your book's content with
this pattern:
```
Some content {rolename}`and here is my role's content!`
```
Again, roles will only work if `rolename` is a valid role's name. For example,
the `doc` role can be used to refer to another page in your book. You can
refer directly to another page by its relative path. For example, the
role syntax `` {doc}`intro` `` will result in: {doc}`intro`.
For more information on writing roles, see the
[MyST documentation](https://myst-parser.readthedocs.io/).
### Adding a citation
You can also cite references that are stored in a `bibtex` file. For example,
the following syntax: `` {cite}`holdgraf_evidence_2014` `` will render like
this: {cite}`holdgraf_evidence_2014`.
Moreover, you can insert a bibliography into your page with this syntax:
The `{bibliography}` directive must be used for all the `{cite}` roles to
render properly.
For example, if the references for your book are stored in `references.bib`,
then the bibliography is inserted with:
````
```{bibliography}
```
````
Resulting in a rendered bibliography that looks like:
```{bibliography}
```
### Executing code in your markdown files
If you'd like to include computational content inside these markdown files,
you can use MyST Markdown to define cells that will be executed when your
book is built. Jupyter Book uses *jupytext* to do this.
First, add Jupytext metadata to the file. For example, to add Jupytext metadata
to this markdown page, run this command:
```
jupyter-book myst init markdown.md
```
Once a markdown file has Jupytext metadata in it, you can add the following
directive to run the code at build time:
````
```{code-cell}
print("Here is some code to execute")
```
````
When your book is built, the contents of any `{code-cell}` blocks will be
executed with your default Jupyter kernel, and their outputs will be displayed
in-line with the rest of your content.
For more information about executing computational content with Jupyter Book,
see [The MyST-NB documentation](https://myst-nb.readthedocs.io/).
%% Cell type:markdown id: tags:
# Content with notebooks
You can also create content with Jupyter Notebooks. This means that you can include
code blocks and their outputs in your book.
## Markdown + notebooks
As it is markdown, you can embed images, HTML, etc into your posts!
![](https://myst-parser.readthedocs.io/en/latest/_static/logo-wide.svg)
You can also $add_{math}$ and
$$
math^{blocks}
$$
or
$$
\begin{aligned}
\mbox{mean} la_{tex} \\ \\
math blocks
\end{aligned}
$$
But make sure you \$Escape \$your \$dollar signs \$you want to keep!
## MyST markdown
MyST markdown works in Jupyter Notebooks as well. For more information about MyST markdown, check
out [the MyST guide in Jupyter Book](https://jupyterbook.org/content/myst.html),
or see [the MyST markdown documentation](https://myst-parser.readthedocs.io/en/latest/).
## Code blocks and outputs
Jupyter Book will also embed your code blocks and output in your book.
For example, here's some sample Matplotlib code:
%% Cell type:code id: tags:
``` python
from matplotlib import rcParams, cycler
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
```
%% Cell type:code id: tags:
``` python
# Fixing random state for reproducibility
np.random.seed(19680801)
N = 10
data = [np.logspace(0, 1, 100) + np.random.randn(100) + ii for ii in range(N)]
data = np.array(data).T
cmap = plt.cm.coolwarm
rcParams['axes.prop_cycle'] = cycler(color=cmap(np.linspace(0, 1, N)))
from matplotlib.lines import Line2D
custom_lines = [Line2D([0], [0], color=cmap(0.), lw=4),
Line2D([0], [0], color=cmap(.5), lw=4),
Line2D([0], [0], color=cmap(1.), lw=4)]
fig, ax = plt.subplots(figsize=(10, 5))
lines = ax.plot(data)
ax.legend(custom_lines, ['Cold', 'Medium', 'Hot']);
```
%% Cell type:markdown id: tags:
There is a lot more that you can do with outputs (such as including interactive outputs)
with your book. For more information about this, see [the Jupyter Book documentation](https://jupyterbook.org)
---
---
@inproceedings{holdgraf_evidence_2014,
address = {Brisbane, Australia, Australia},
title = {Evidence for {Predictive} {Coding} in {Human} {Auditory} {Cortex}},
booktitle = {International {Conference} on {Cognitive} {Neuroscience}},
publisher = {Frontiers in Neuroscience},
author = {Holdgraf, Christopher Ramsay and de Heer, Wendy and Pasley, Brian N. and Knight, Robert T.},
year = {2014}
}
@article{holdgraf_rapid_2016,
title = {Rapid tuning shifts in human auditory cortex enhance speech intelligibility},
volume = {7},
issn = {2041-1723},
url = {http://www.nature.com/doifinder/10.1038/ncomms13654},
doi = {10.1038/ncomms13654},
number = {May},
journal = {Nature Communications},
author = {Holdgraf, Christopher Ramsay and de Heer, Wendy and Pasley, Brian N. and Rieger, Jochem W. and Crone, Nathan and Lin, Jack J. and Knight, Robert T. and Theunissen, Frédéric E.},
year = {2016},
pages = {13654},
file = {Holdgraf et al. - 2016 - Rapid tuning shifts in human auditory cortex enhance speech intelligibility.pdf:C\:\\Users\\chold\\Zotero\\storage\\MDQP3JWE\\Holdgraf et al. - 2016 - Rapid tuning shifts in human auditory cortex enhance speech intelligibility.pdf:application/pdf}
}
@inproceedings{holdgraf_portable_2017,
title = {Portable learning environments for hands-on computational instruction using container-and cloud-based technology to teach data science},
volume = {Part F1287},
isbn = {978-1-4503-5272-7},
doi = {10.1145/3093338.3093370},
abstract = {© 2017 ACM. There is an increasing interest in learning outside of the traditional classroom setting. This is especially true for topics covering computational tools and data science, as both are challenging to incorporate in the standard curriculum. These atypical learning environments offer new opportunities for teaching, particularly when it comes to combining conceptual knowledge with hands-on experience/expertise with methods and skills. Advances in cloud computing and containerized environments provide an attractive opportunity to improve the effciency and ease with which students can learn. This manuscript details recent advances towards using commonly-Available cloud computing services and advanced cyberinfrastructure support for improving the learning experience in bootcamp-style events. We cover the benets (and challenges) of using a server hosted remotely instead of relying on student laptops, discuss the technology that was used in order to make this possible, and give suggestions for how others could implement and improve upon this model for pedagogy and reproducibility.},
booktitle = {{ACM} {International} {Conference} {Proceeding} {Series}},
author = {Holdgraf, Christopher Ramsay and Culich, A. and Rokem, A. and Deniz, F. and Alegro, M. and Ushizima, D.},
year = {2017},
keywords = {Teaching, Bootcamps, Cloud computing, Data science, Docker, Pedagogy}
}
@article{holdgraf_encoding_2017,
title = {Encoding and decoding models in cognitive electrophysiology},
volume = {11},
issn = {16625137},
doi = {10.3389/fnsys.2017.00061},
abstract = {© 2017 Holdgraf, Rieger, Micheli, Martin, Knight and Theunissen. Cognitive neuroscience has seen rapid growth in the size and complexity of data recorded from the human brain as well as in the computational tools available to analyze this data. This data explosion has resulted in an increased use of multivariate, model-based methods for asking neuroscience questions, allowing scientists to investigate multiple hypotheses with a single dataset, to use complex, time-varying stimuli, and to study the human brain under more naturalistic conditions. These tools come in the form of “Encoding” models, in which stimulus features are used to model brain activity, and “Decoding” models, in which neural features are used to generated a stimulus output. Here we review the current state of encoding and decoding models in cognitive electrophysiology and provide a practical guide toward conducting experiments and analyses in this emerging field. Our examples focus on using linear models in the study of human language and audition. We show how to calculate auditory receptive fields from natural sounds as well as how to decode neural recordings to predict speech. The paper aims to be a useful tutorial to these approaches, and a practical introduction to using machine learning and applied statistics to build models of neural activity. The data analytic approaches we discuss may also be applied to other sensory modalities, motor systems, and cognitive systems, and we cover some examples in these areas. In addition, a collection of Jupyter notebooks is publicly available as a complement to the material covered in this paper, providing code examples and tutorials for predictive modeling in python. The aimis to provide a practical understanding of predictivemodeling of human brain data and to propose best-practices in conducting these analyses.},
journal = {Frontiers in Systems Neuroscience},
author = {Holdgraf, Christopher Ramsay and Rieger, J.W. and Micheli, C. and Martin, S. and Knight, R.T. and Theunissen, F.E.},
year = {2017},
keywords = {Decoding models, Encoding models, Electrocorticography (ECoG), Electrophysiology/evoked potentials, Machine learning applied to neuroscience, Natural stimuli, Predictive modeling, Tutorials}
}
@book{ruby,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
}
jupyter-book
ipywidgets
matplotlib
numpy
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment