Skip to content
Snippets Groups Projects
Commit ac8b4e10 authored by Etienne Kornobis's avatar Etienne Kornobis
Browse files

Adding interactivity material

parent b38c7ab6
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:a173b63d-52c5-4ce7-8445-9104c5a225d5 tags:
%% Cell type:markdown id:6066486e-1c4d-4da3-bbdd-d55bff7f64ca tags:
<center><b>Hands-on</b></center>
<b><p style="text-align:center;font-size:50px">Interactivity</p></b>
<div style="text-align:center">
<div>
Etienne Kornobis, François Laurent, Bertrand Néron
<br />
<a src=" https://research.pasteur.fr/en/team/bioinformatics-and-biostatistics-hub/">Bioinformatics and Biostatistics HUB</a>
<br />
© Institut Pasteur, 2022
</div>
</div>
%% Cell type:code id:4d12349e-6d72-4fb1-8bd7-775816af97e4 tags:
``` python
%matplotlib inline
import pandas as pd
import seaborn as sns
from ipywidgets import interact, widgets
```
%% Cell type:markdown id:ac77aa2a-ab03-4d18-88dc-8bed1a231d0c tags:
%% Cell type:markdown id:c4cfe182-0b6d-4325-8d53-459ad69ba161 tags:
This demo need ipywidgets module
```
pip install ipywidgets
```
Using the `data/city_temperature.csv` dataset, we are going to practice interactivity using ipywidgets.
- Make a decorated function which returns a dataframe with only the data for a country selected by the user (through a dropdown widget for example):
And enabling the widget backend (see the first cell of this notbook).
There is other ways, but the `unique` method can be of use as well:
> https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.unique.html#pandas.Series.unique
%% Cell type:code id:c4504712-9c32-4198-af1e-6887e4da3acc tags:
%% Cell type:code id:3eba1eea-b75d-4a85-b1e0-404a89a3f6f2 tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets
from ipywidgets import interact
```
%% Cell type:markdown id:f32dbf0d-8468-4386-ac3b-520a3330c84c tags:
## doc
Link to offical ipywidget interact
https://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html
%% Cell type:markdown id:3841dd4e-c97d-4df7-b501-6dc385cb38c3 tags:
%% Cell type:markdown id:bb2ec7a4-acc8-46d0-acd5-61db4ef1d85d tags:
- Make a decorated function which returns a graph of the temperatures with only the data for a city selected by the user.
## first widget
%% Cell type:code id:0ca0db23-4ff5-45d6-b7c4-469cfa454013 tags:
%% Cell type:code id:f8599e2c-3b5d-4c52-a959-372572669253 tags:
``` python
slider = ipywidgets.IntSlider(2, min=0, max=10, step=2)
display(slider)
```
%% Output
%% Cell type:markdown id:83c1db00-c52e-4f02-a922-a572329be2b1 tags:
%% Cell type:markdown id:03ef9efd-47be-47cb-9f93-4f64ddbf7da4 tags:
These temperatures in Fahrenheit are cumbersome especially in Europe, let's convert them to Celsius. Here is the formula to apply:
There are lot mmore widget than sliders
$$°C = \frac{°F - 32}{1.8}$$
- Create a new column to the dataframe with the temperatures as Celsius and redraw the last graph. We saw in the course that mathematical operations can be directly performed on Series, but it is also a good occasion to practice the `apply` method, using a `farhenheit_to_celsius` function for the conversion).
%% Cell type:markdown id:b89ba6db-4aa5-4964-8a05-60c0b07c0c93 tags:
> https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html
## How to link widget to a figure
%% Cell type:code id:d01dab50-0b8b-4fcc-a6e8-9d0d47dcd1b6 tags:
%% Cell type:code id:aa8ec056-256f-4a95-bc9e-8ba3a5e13a15 tags:
``` python
def sinewave(c=1):
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x*c)
plt.plot(x, y)
plt.ylabel('sin(x)')
plt.xlabel('x')
plt.title('Sinwave function')
plt.show()
slider = ipywidgets.FloatSlider(.5, min=0, max=4, step=.5)
interact(sinewave, c=slider);
```
%% Output
%% Cell type:markdown id:ace3cbd1-95ea-42a8-a763-4e6ecc481c6e tags:
%% Cell type:code id:5e6312df-92da-40b9-8321-7b42932570fa tags:
``` python
```
As a side note `apply` can be usefull to access indices and columns names but `itertuples` is a much faster method to deal with the same kind of operations.
%% Cell type:markdown id:1a02d93a-13dd-4cca-89cd-3d7926662506 tags:
> https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.itertuples.html
a tutorial
%% Cell type:markdown id:99e9b153-c168-4192-a18e-0c12fb3a61c4 tags:
https://towardsdatascience.com/bring-your-jupyter-notebook-to-life-with-interactive-widgets-bc12e03f0916
- Using your interactive plot, you should see something strange checking the 10 first cities. What is happening in Addis Ababa ?
%% Cell type:code id:411ced85-689d-43a9-9ac5-4f7a69646fd8 tags:
%% Cell type:code id:1e1fc7e2-8d86-4ef3-bd77-866b68d6dc48 tags:
``` python
```
......
This diff is collapsed.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
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