Commit 00f6896f authored by Remi Cresson's avatar Remi Cresson
Browse files

Initial commit

parents
No related merge requests found
Showing with 647 additions and 0 deletions
+647 -0
README.md 0 → 100644
# Exemples de notebooks
Exemples de notebooks présentés le 13/06/2023 lors de la réunion
ATTOS/Ingénierie.
## Pré-requis
- Docker
- Docker-compose
## Build
```
docker-compose build
```
## Démarrer
```
docker-compose up
```
Puis ouvrir le lien indiqué dans le navigateur (par exemple
http://9c59ca961122:8888/?token=7dc529f527de418431449ff7473075c0044f3b757984f83a
)
## Eteindre
```
docker-compose down
```
## Contact
Rémi
services:
notebook:
build:
context: jupyter/.
image: notebook-cdos:demo
volumes:
- ./examples:/examples
ports:
- "8888:8888"
jupyter notebook --ip 0.0.0.0
ls
pwd
jupyter notebook --ip 0.0.0.0
jupyter notebook --help
%% Cell type:code id:e358e01e tags:
``` python
import pystac_client
import dinamis_sdk
api = pystac_client.Client.open(
'https://stacapi-dinamis.apps.okd.crocc.meso.umontpellier.fr',
modifier=dinamis_sdk.sign_inplace
)
```
%% Cell type:code id:6cf149db tags:
``` python
"""
L'exemple suivant montre comment calculer une différence de NDVI
sur une zone donnée entre deux années consécutives
"""
import pyotb
def mosa(year):
"""Return a pyotb application that perform a mosaic."""
res = api.search(
bbox=[4, 42.99, 5, 44.05],
datetime=[f"{year}-01-01", f"{year}-12-25"],
collections=["spot-6-7-drs"]
)
urls = [f"/vsicurl/{r.assets['src_xs'].href}" for r in res.items()]
return pyotb.Mosaic({"il": urls})
def ndvi(xs):
"""Return a pyotb application that computes NDVI."""
return pyotb.BandMath({"il": [xs], "exp": "(im1b4-im1b1)/(im1b4+im1b1)"})
mosa_22 = mosa("2022") # mosaique XS 2022
mosa_21 = mosa("2021") # mosaique XS 2021
ndvi_22 = ndvi(mosa_22) # NDVI 2022
ndvi_21 = ndvi(mosa_21) # NDVI 2021
delta_ndvi = ndvi_22 - pyotb.Superimpose({"inr": ndvi_22, "inm": ndvi_21})
#delta_ndvi.write("ndvi_loss.tif?&box=5000:5000:4096:4096")
```
%% Cell type:code id:390e81b7 tags:
``` python
"""
L'exemple suivant montre comment on peut extraire des patches de
128 x 128 pixels dans les images `delta_ndvi` et `mosa_22`
et afficher ces patches dans le notebook avec un widget
"""
import matplotlib.pyplot as plt
import numpy as np
def nice_rgb(img): return np.minimum(np.maximum(0, (img - np.mean(img))/(3 * np.std(img))), 1)
mosa_subset = mosa_22[1000:1600, 1000:1800, 0:3]
mosa_arr = mosa_subset.to_numpy()
plt.imshow(nice_rgb(mosa_arr))
plt.show()
```
%% Cell type:code id:4060aeb7 tags:
``` python
plt.imshow(delta_ndvi[1000:1600, 1000:1800, :].to_numpy())
plt.show()
```
%% Cell type:code id:a9f1a7e5 tags:
``` python
vec = "patches_pos.gpkg"
inp = delta_ndvi[1000:5000, 1000:5000]
pyotb.PatchesSelection({
"in": inp,
"grid.step": 512,
"grid.psize": 128,
"strategy": "all",
"outtrain": vec
})
patches = pyotb.PatchesExtraction({
"source1.il": [inp],
"source1.patchsizex": 128,
"source1.patchsizey": 128,
"source2.il": [mosa_22],
"source2.patchsizex": 128,
"source2.patchsizey": 128,
"vec": vec,
"field": "id"
}, n_sources=2)
```
%% Cell type:code id:27d84235 tags:
``` python
from ipywidgets import interact, widgets
from IPython.display import display
import matplotlib.pyplot as plt
import numpy as np
x_arr = patches["source1.out"].to_numpy()
y_arr = patches["source2.out"].to_numpy()
patches_sz = x_arr.shape[1]
patches_nb = int(x_arr.shape[0] / patches_sz)
x_arr = np.reshape(x_arr, (patches_nb, patches_sz, patches_sz, 1))
y_arr = np.reshape(y_arr, (patches_nb, patches_sz, patches_sz, 4))
fig = plt.figure(figsize=(6, 4))
ndvi = fig.add_subplot(121)
xs = fig.add_subplot(122)
def show(idx):
ndvi.imshow(x_arr[idx, :])
xs.imshow(nice_rgb(y_arr[idx, :, :, 0:3]))
def f(idx):
show(idx)
display(fig)
show(0)
interact(f, idx=widgets.IntSlider(min=0, max=patches_nb-1, step=1, value=0));
```
%% Cell type:code id:45d21c57 tags:
``` python
```
%% Cell type:code id:c1c4b746 tags:
``` python
import dinamis_sdk
import pystac_client
api = pystac_client.Client.open(
'https://stacapi-dinamis.apps.okd.crocc.meso.umontpellier.fr',
modifier=dinamis_sdk.sign_inplace,
)
```
%% Cell type:code id:cca4ac13 tags:
``` python
res = api.search(
bbox=[3.75, 43.58, 3.95, 43.67],
datetime=["2020-04-01", "2020-08-31"],
collections=["super-sentinel-2-l2a"]
)
```
%% Cell type:code id:b8ac244c tags:
``` python
import pyotb
def to_rgb(item):
roi = pyotb.ExtractROI({
"in": f"/vsicurl/{item.assets['img'].href}",
"mode": "fit",
"mode.fit.vect": "roi.gpkg",
})
rgb = pyotb.BandMathX({
"il": [roi],
"exp": "{im1b1 / 2258, im1b2 / 1863, im1b3 / 1479}"
})
clipped = pyotb.clip(rgb, 0, 1)
return clipped.to_numpy()
```
%% Cell type:code id:45791b20 tags:
``` python
import numpy as np
from tqdm.notebook import tqdm
def empty(img): return np.amin(img) == np.amax(img)
images = [img for item in tqdm(res.items()) if not empty(img := to_rgb(item))]
```
%% Cell type:code id:1f87430c tags:
``` python
from ipywidgets import interact, widgets
from IPython.display import display
import matplotlib.pyplot as plt
def f(idx):
plt.imshow(images[idx])
plt.show()
interact(f, idx=widgets.IntSlider(min=0, max=len(images)-1, step=1, value=0));
```
2023-06-12T19:35:47.075947+00:00
\ No newline at end of file
AVBZyWzCIMgJRD8oCzZXvUvo2dZn/Wek8SUANwRVK44=
%% Cell type:code id:c1c4b746 tags:
``` python
import dinamis_sdk
import pystac_client
api = pystac_client.Client.open(
'https://stacapi-dinamis.apps.okd.crocc.meso.umontpellier.fr',
modifier=dinamis_sdk.sign_inplace,
)
```
%% Cell type:code id:cca4ac13 tags:
``` python
res = api.search(
bbox=[3.75, 43.58, 3.95, 43.67],
datetime=["2020-04-01", "2020-08-31"],
collections=["super-sentinel-2-l2a"]
)
```
%% Cell type:code id:b8ac244c tags:
``` python
import pyotb
def to_rgb(item):
roi = pyotb.ExtractROI({
"in": f"/vsicurl/{item.assets['img'].href}",
"mode": "fit",
"mode.fit.vect": "roi.gpkg",
})
rgb = pyotb.BandMathX({
"il": [roi],
"exp": "{im1b1 / 2258, im1b2 / 1863, im1b3 / 1479}"
})
clipped = pyotb.clip(rgb, 0, 1)
return clipped.to_numpy()
```
%% Cell type:code id:45791b20 tags:
``` python
import numpy as np
from tqdm.notebook import tqdm
def empty(img): return np.amin(img) == np.amax(img)
images = [img for item in tqdm(res.items()) if not empty(img := to_rgb(item))]
```
%% Cell type:code id:1f87430c tags:
``` python
from ipywidgets import interact, widgets
from IPython.display import display
import matplotlib.pyplot as plt
def f(idx):
plt.imshow(images[idx])
plt.show()
interact(f, idx=widgets.IntSlider(min=0, max=len(images)-1, step=1, value=0));
```
%% Cell type:code id:e358e01e tags:
``` python
import pystac_client
import dinamis_sdk
api = pystac_client.Client.open(
'https://stacapi-dinamis.apps.okd.crocc.meso.umontpellier.fr',
modifier=dinamis_sdk.sign_inplace
)
```
%% Cell type:code id:6cf149db tags:
``` python
"""
L'exemple suivant montre comment calculer une différence de NDVI
sur une zone donnée entre deux années consécutives
"""
import pyotb
def mosa(year):
"""Return a pyotb application that perform a mosaic."""
res = api.search(
bbox=[4, 42.99, 5, 44.05],
datetime=[f"{year}-01-01", f"{year}-12-25"],
collections=["spot-6-7-drs"]
)
urls = [f"/vsicurl/{r.assets['src_xs'].href}" for r in res.items()]
return pyotb.Mosaic({"il": urls})
def ndvi(xs):
"""Return a pyotb application that computes NDVI."""
return pyotb.BandMath({"il": [xs], "exp": "(im1b4-im1b1)/(im1b4+im1b1)"})
mosa_22 = mosa("2022") # mosaique XS 2022
mosa_21 = mosa("2021") # mosaique XS 2021
ndvi_22 = ndvi(mosa_22) # NDVI 2022
ndvi_21 = ndvi(mosa_21) # NDVI 2021
delta_ndvi = ndvi_22 - pyotb.Superimpose({"inr": ndvi_22, "inm": ndvi_21})
#delta_ndvi.write("ndvi_loss.tif?&box=5000:5000:4096:4096")
```
%% Cell type:code id:390e81b7 tags:
``` python
"""
L'exemple suivant montre comment on peut extraire des patches de
128 x 128 pixels dans les images `delta_ndvi` et `mosa_22`
et afficher ces patches dans le notebook avec un widget
"""
import matplotlib.pyplot as plt
import numpy as np
def nice_rgb(img): return np.minimum(np.maximum(0, (img - np.mean(img))/(3 * np.std(img))), 1)
mosa_subset = mosa_22[1000:1600, 1000:1800, 0:3]
mosa_arr = mosa_subset.to_numpy()
plt.imshow(nice_rgb(mosa_arr))
plt.show()
```
%% Cell type:code id:4060aeb7 tags:
``` python
plt.imshow(delta_ndvi[1000:1600, 1000:1800, :].to_numpy())
plt.show()
```
%% Cell type:code id:a9f1a7e5 tags:
``` python
vec = "patches_pos.gpkg"
inp = delta_ndvi[1000:5000, 1000:5000]
pyotb.PatchesSelection({
"in": inp,
"grid.step": 512,
"grid.psize": 128,
"strategy": "all",
"outtrain": vec
})
patches = pyotb.PatchesExtraction({
"source1.il": [inp],
"source1.patchsizex": 128,
"source1.patchsizey": 128,
"source2.il": [mosa_22],
"source2.patchsizex": 128,
"source2.patchsizey": 128,
"vec": vec,
"field": "id"
}, n_sources=2)
```
%% Cell type:code id:27d84235 tags:
``` python
from ipywidgets import interact, widgets
from IPython.display import display
import matplotlib.pyplot as plt
import numpy as np
x_arr = patches["source1.out"].to_numpy()
y_arr = patches["source2.out"].to_numpy()
patches_sz = x_arr.shape[1]
patches_nb = int(x_arr.shape[0] / patches_sz)
x_arr = np.reshape(x_arr, (patches_nb, patches_sz, patches_sz, 1))
y_arr = np.reshape(y_arr, (patches_nb, patches_sz, patches_sz, 4))
fig = plt.figure(figsize=(6, 4))
ndvi = fig.add_subplot(121)
xs = fig.add_subplot(122)
def show(idx):
ndvi.imshow(x_arr[idx, :])
xs.imshow(nice_rgb(y_arr[idx, :, :, 0:3]))
def f(idx):
show(idx)
display(fig)
show(0)
interact(f, idx=widgets.IntSlider(min=0, max=patches_nb-1, step=1, value=0));
```
%% Cell type:code id:45d21c57 tags:
``` python
```
File added
FROM gitlab-registry.irstea.fr/remi.cresson/otbtf:4.1.0-cpu
LABEL description="Notebook demo image"
LABEL maintainer="Remi Cresson [at] inrae [dot] fr"
USER root
RUN apt update && apt install -y curl && curl -fsSL https://deb.nodesource.com/setup_lts.x | bash && apt install nodejs
RUN python3 -m pip install --upgrade setuptools pip
RUN python3 -m pip install --upgrade click matplotlib pydot pyotb jupyter jupyterlab jupyterhub ipyleaflet dinamis-sdk planetary-computer pystac-client
RUN echo "otbuser:otbuser" | chpasswd
USER otbuser
CMD jupyter notebook --ip 0.0.0.0 --notebook-dir=/examples
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment