Skip to content

Commit 7b89edf

Browse files
davidcavazosgcf-merge-on-green[bot]dandhlee
authored andcommitted
ppai/weather: add weather forecasting sample (#8345)
* ppai/weather-foreacasting: add weather sample * remove temporary test model * remove test scripts * install local packages before testing * update datasets * add local packages to test requirements * split notebook and tests * add predictions notebook and tests * add missing requirements * update notebooks * update test names * fix tests * enable vertex test * copy file to directory * update dependencies * ignore checkpoints directory * Simplify boolean test Co-authored-by: Dan Lee <[email protected]> * Improve wording Co-authored-by: Dan Lee <[email protected]> * Improve wording Co-authored-by: Dan Lee <[email protected]> * rename go to graph_objects Co-authored-by: gcf-merge-on-green[bot] <60162190+gcf-merge-on-green[bot]@users.noreply.github.com> Co-authored-by: Dan Lee <[email protected]>
1 parent af5ebc5 commit 7b89edf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+5651
-22
lines changed

people-and-planet-ai/README.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## 🦏 [Wildlife Insights -- _image-classification_](image-classification)
44

5-
[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/image-classification/README.ipynb)
6-
5+
> [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/image-classification/README.ipynb)
6+
>
77
> [Watch the video in YouTube<br> ![thumbnail](http://img.youtube.com/vi/hUzODH3uGg0/0.jpg)](https://youtu.be/hUzODH3uGg0)
88
99
This model is trained to recognize animal species from
@@ -20,10 +20,12 @@ pictures.
2020
[AutoML]: https://cloud.google.com/vertex-ai/docs/beginner/beginners-guide
2121
[Vertex AI]: https://cloud.google.com/vertex-ai
2222

23-
## 🗺 [Global Fishing Watch -- _timeseries-classification_](timeseries-classification)
23+
---
2424

25-
[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/timeseries-classification/README.ipynb)
25+
## 🗺 [Global Fishing Watch -- _timeseries-classification_](timeseries-classification)
2626

27+
> [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/timeseries-classification/README.ipynb)
28+
>
2729
> [Watch the video in YouTube<br> ![thumbnail](http://img.youtube.com/vi/LnEhSVEJUuY/0.jpg)](https://youtu.be/LnEhSVEJUuY)
2830
2931
This model is trained to classify if a ship is fishing or not every hour from their
@@ -41,10 +43,12 @@ location data.
4143
[Keras]: https://keras.io
4244
[Vertex AI]: https://cloud.google.com/vertex-ai
4345

44-
## 🏭 [Coal Plant Predictions -- _geospatial-classification_](geospatial-classification)
46+
---
4547

46-
[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/geospatial-classification/README.ipynb)
48+
## 🏭 [Coal Plant Predictions -- _geospatial-classification_](geospatial-classification)
4749

50+
> [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/geospatial-classification/README.ipynb)
51+
>
4852
> [Watch the video in YouTube<br> ![thumbnail](http://img.youtube.com/vi/8amFK7T_n30/0.jpg)](https://youtu.be/8amFK7T_n30)
4953
5054
This model uses satellite data to predict if a coal plant is turned on and producing carbon emissions. The satellite data comes from [Google Earth Engine.](https://earthengine.google.com/)
@@ -60,10 +64,12 @@ This model uses satellite data to predict if a coal plant is turned on and produ
6064
[TensorFlow]: https://www.tensorflow.org/
6165
[Vertex AI]: https://cloud.google.com/vertex-ai
6266

63-
## 🌍 [Land cover classification -- _image segmentation_](land-cover-classification)
67+
---
6468

65-
[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/land-cover-classification/README.ipynb)
69+
## 🌍 [Land cover classification -- _image segmentation_](land-cover-classification)
6670

71+
> [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/land-cover-classification/README.ipynb)
72+
>
6773
> [Watch the video in YouTube<br> ![thumbnail](http://img.youtube.com/vi/zImQf91ffFo/0.jpg)](https://youtu.be/zImQf91ffFo)
6874
6975
This model uses satellite data to classify what is on Earth. The satellite data comes from [Google Earth Engine.](https://earthengine.google.com/)
@@ -81,3 +87,22 @@ This model uses satellite data to classify what is on Earth. The satellite data
8187
[Earth Engine]: https://earthengine.google.com/
8288
[TensorFlow]: https://www.tensorflow.org/
8389
[Vertex AI]: https://cloud.google.com/vertex-ai
90+
91+
---
92+
93+
## 🌦 Weather forecasting -- _timeseries regression_
94+
95+
> [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/weather-forecasting/README.ipynb)
96+
<!-- > [Watch the video in YouTube<br> ![thumbnail](http://img.youtube.com/vi/TODO/0.jpg)](https://youtu.be/TODO) -->
97+
98+
This model uses satellite data to forecast precipitation for the next 2 and 6 hours. The satellite data comes from [Google Earth Engine.](https://earthengine.google.com/)
99+
100+
* **Model**: 2D Fully Convolutional Network in [PyTorch]
101+
* **Creating datasets**: [Sentinel-2] satellite data and [ESA WorldCover] from [Earth Engine] with [Dataflow]
102+
* **Training the model**: [PyTorch] in [Vertex AI]
103+
* **Getting predictions**: [PyTorch] locally
104+
105+
[Dataflow]: https://cloud.google.com/dataflow
106+
[Earth Engine]: https://earthengine.google.com/
107+
[PyTorch]: https://pytorch.org/
108+
[Vertex AI]: https://cloud.google.com/vertex-ai

people-and-planet-ai/conftest.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414

1515
from __future__ import annotations
1616

17-
from datetime import datetime
18-
import multiprocessing
1917
import os
2018
import platform
2119
import re
2220
import subprocess
23-
from unittest import mock
21+
import sys
22+
import textwrap
2423
import uuid
2524
from collections.abc import Callable, Iterable
25+
from datetime import datetime
26+
from unittest import mock
2627

27-
from google.cloud import storage
2828
import pytest
29+
from google.cloud import storage
2930

3031

3132
@pytest.fixture(scope="session")
@@ -202,9 +203,9 @@ def run_notebook(
202203
skip_shell_commands: bool = False,
203204
until_end: bool = False,
204205
) -> None:
206+
import nbformat
205207
from nbclient.client import NotebookClient
206208
from nbclient.exceptions import CellExecutionError
207-
import nbformat
208209

209210
def notebook_filter_section(
210211
start: str,
@@ -262,13 +263,7 @@ def notebook_filter_section(
262263
cmd = "pass"
263264
cell["source"] = shell_command_re.sub(cmd, cell["source"])
264265
else:
265-
cmd = [
266-
"import subprocess",
267-
"_cmd = f'''\\1'''",
268-
"print(f'>> {_cmd}')",
269-
"subprocess.run(_cmd, shell=True, check=True)",
270-
]
271-
cell["source"] = shell_command_re.sub("\n".join(cmd), cell["source"])
266+
cell["source"] = shell_command_re.sub(r"_run(f'''\1''')", cell["source"])
272267

273268
# Apply variable substitutions.
274269
for regex, new_value in compiled_substitutions:
@@ -278,8 +273,38 @@ def notebook_filter_section(
278273
for old, new in replace.items():
279274
cell["source"] = cell["source"].replace(old, new)
280275

276+
# Clear outputs.
277+
cell["outputs"] = []
278+
281279
# Prepend the prelude cell.
282-
nb.cells = [nbformat.v4.new_code_cell(prelude)] + nb.cells
280+
prelude_src = textwrap.dedent(
281+
"""\
282+
def _run(cmd):
283+
import subprocess as _sp
284+
import sys as _sys
285+
_p = _sp.run(cmd, shell=True, stdout=_sp.PIPE, stderr=_sp.PIPE)
286+
_stdout = _p.stdout.decode('utf-8').strip()
287+
_stderr = _p.stderr.decode('utf-8').strip()
288+
if _stdout:
289+
print(f'➜ !{cmd}')
290+
print(_stdout)
291+
if _stderr:
292+
print(f'➜ !{cmd}', file=_sys.stderr)
293+
print(_stderr, file=_sys.stderr)
294+
if _p.returncode:
295+
raise RuntimeError('\\n'.join([
296+
f"Command returned non-zero exit status {_p.returncode}.",
297+
f"-------- command --------",
298+
f"{cmd}",
299+
f"-------- stderr --------",
300+
f"{_stderr}",
301+
f"-------- stdout --------",
302+
f"{_stdout}",
303+
]))
304+
"""
305+
+ prelude
306+
)
307+
nb.cells = [nbformat.v4.new_code_cell(prelude_src)] + nb.cells
283308

284309
# Run the notebook.
285310
error = ""
@@ -289,7 +314,16 @@ def notebook_filter_section(
289314
except CellExecutionError as e:
290315
# Remove colors and other escape characters to make it easier to read in the logs.
291316
# https://stackoverflow.com/a/33925425
292-
error = re.sub(r"(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]", "", str(e))
317+
color_chars = re.compile(r"(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]")
318+
error = color_chars.sub("", str(e))
319+
for cell in nb.cells:
320+
if cell["cell_type"] != "code":
321+
continue
322+
for output in cell["outputs"]:
323+
if output.get("name") == "stdout":
324+
print(color_chars.sub("", output["text"]))
325+
elif output.get("name") == "stderr":
326+
print(color_chars.sub("", output["text"]), file=sys.stderr)
293327

294328
if error:
295329
raise RuntimeError(
@@ -305,6 +339,8 @@ def run_notebook_parallel(
305339
replace: dict[str, str] = {},
306340
skip_shell_commands: bool = False,
307341
) -> None:
342+
import multiprocessing
343+
308344
args = [
309345
{
310346
"ipynb_file": ipynb_file,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
**/*.egg-info
2+
**/build
3+
**/dist
4+
checkpoints/
5+
data/
6+
data-training/
7+
model/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## 🌦 Weather forecasting -- _timeseries regression_
2+
3+
> [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/GoogleCloudPlatform/python-docs-samples/blob/main/people-and-planet-ai/weather-forecasting/README.ipynb)
4+
<!-- > [Watch the video in YouTube<br> ![thumbnail](http://img.youtube.com/vi/TODO/0.jpg)](https://youtu.be/TODO) -->
5+
6+
This model uses satellite data to forecast precipitation for the next 2 and 6 hours. The satellite data comes from [Google Earth Engine.](https://earthengine.google.com/)
7+
8+
* **Model**: 2D Fully Convolutional Network in [PyTorch]
9+
* **Creating datasets**: [Sentinel-2] satellite data and [ESA WorldCover] from [Earth Engine] with [Dataflow]
10+
* **Training the model**: [PyTorch] in [Vertex AI]
11+
* **Getting predictions**: [PyTorch] locally
12+
13+
[Dataflow]: https://cloud.google.com/dataflow
14+
[Earth Engine]: https://earthengine.google.com/
15+
[PyTorch]: https://pytorch.org/
16+
[Vertex AI]: https://cloud.google.com/vertex-ai

0 commit comments

Comments
 (0)