From 88e2616bb18f9714f5f4adc819bc9c979c1556d9 Mon Sep 17 00:00:00 2001 From: DEGERICJ Date: Thu, 17 Oct 2024 07:39:48 +0200 Subject: [PATCH] save openeo results to pickle --- notebooks/utils.py | 34 +++++++++++++++++++ .../worldcereal_v1_demo_custom_cropland.ipynb | 5 ++- ...eal_v1_demo_custom_cropland_extended.ipynb | 5 ++- .../worldcereal_v1_demo_custom_croptype.ipynb | 5 ++- ...eal_v1_demo_custom_croptype_extended.ipynb | 29 +++++++++++++++- ...worldcereal_v1_demo_default_cropland.ipynb | 5 ++- ...al_v1_demo_default_cropland_extended.ipynb | 5 ++- 7 files changed, 82 insertions(+), 6 deletions(-) diff --git a/notebooks/utils.py b/notebooks/utils.py index 146a11fa..256392d6 100644 --- a/notebooks/utils.py +++ b/notebooks/utils.py @@ -1,6 +1,7 @@ import ast import copy import logging +import pickle import random from calendar import monthrange from datetime import datetime, timedelta @@ -849,6 +850,39 @@ def prepare_visualization(results): return final_paths +def _results_to_pickle(results, output_dir): + """Save the results of an openeo inference run to a pickle file. + + Parameters + ---------- + results : WorldCereal InferenceResults object + Results object containing the results to save. + output_dir : Path + Directory where the results will be saved. + """ + output_dir.mkdir(parents=True, exist_ok=True) + with open(output_dir / "results.pkl", "wb") as f: + pickle.dump(results, f) + + +def _results_from_pickle(output_dir): + """Load the results from a pickle file. + + Parameters + ---------- + output_dir : Path + Path to the output directory containing the results. + + Returns + ------- + WorldCereal InferenceResults + Results object containing the loaded results from an openeo inference run. + """ + with open(output_dir / "results.pkl", "rb") as f: + results = pickle.load(f) + return results + + ############# PRODUCT VISUALIZATION ############# diff --git a/notebooks/worldcereal_v1_demo_custom_cropland.ipynb b/notebooks/worldcereal_v1_demo_custom_cropland.ipynb index 61c682e4..94d0ad1b 100644 --- a/notebooks/worldcereal_v1_demo_custom_cropland.ipynb +++ b/notebooks/worldcereal_v1_demo_custom_cropland.ipynb @@ -309,6 +309,7 @@ "outputs": [], "source": [ "from worldcereal.job import generate_map, CropLandParameters, PostprocessParameters\n", + "from utils import _results_to_pickle\n", "\n", "# Initializes default parameters\n", "parameters = CropLandParameters()\n", @@ -327,7 +328,9 @@ " output_dir=output_dir,\n", " cropland_parameters=parameters,\n", " postprocess_parameters=PostprocessParameters(),\n", - ")" + ")\n", + "# Write results to pickle file\n", + "_results_to_pickle(job_results, output_dir)" ] }, { diff --git a/notebooks/worldcereal_v1_demo_custom_cropland_extended.ipynb b/notebooks/worldcereal_v1_demo_custom_cropland_extended.ipynb index ce41661c..5d6976e8 100644 --- a/notebooks/worldcereal_v1_demo_custom_cropland_extended.ipynb +++ b/notebooks/worldcereal_v1_demo_custom_cropland_extended.ipynb @@ -331,6 +331,7 @@ "outputs": [], "source": [ "from worldcereal.job import generate_map, CropLandParameters\n", + "from utils import _results_to_pickle\n", "\n", "# Initializes default parameters\n", "parameters = CropLandParameters()\n", @@ -349,7 +350,9 @@ " output_dir=output_dir,\n", " cropland_parameters=parameters,\n", " postprocess_parameters=postprocess_parameters,\n", - ")" + ")\n", + "# Write results to pickle file\n", + "_results_to_pickle(job_results, output_dir)" ] }, { diff --git a/notebooks/worldcereal_v1_demo_custom_croptype.ipynb b/notebooks/worldcereal_v1_demo_custom_croptype.ipynb index 5c4e0ffb..39c5a89b 100644 --- a/notebooks/worldcereal_v1_demo_custom_croptype.ipynb +++ b/notebooks/worldcereal_v1_demo_custom_croptype.ipynb @@ -314,6 +314,7 @@ "outputs": [], "source": [ "from worldcereal.job import PostprocessParameters, WorldCerealProductType, generate_map, CropTypeParameters\n", + "from utils import _results_to_pickle\n", "\n", "# Initializes default parameters\n", "parameters = CropTypeParameters()\n", @@ -334,7 +335,9 @@ " product_type=WorldCerealProductType.CROPTYPE,\n", " croptype_parameters=parameters,\n", " postprocess_parameters=PostprocessParameters(),\n", - ")" + ")\n", + "# Write results to pickle file\n", + "_results_to_pickle(job_results, output_dir)" ] }, { diff --git a/notebooks/worldcereal_v1_demo_custom_croptype_extended.ipynb b/notebooks/worldcereal_v1_demo_custom_croptype_extended.ipynb index e44731df..2a9f55ca 100644 --- a/notebooks/worldcereal_v1_demo_custom_croptype_extended.ipynb +++ b/notebooks/worldcereal_v1_demo_custom_croptype_extended.ipynb @@ -66,6 +66,19 @@ "" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# TEMPORARY CELL\n", + "import sys\n", + "sys.path.append('/home/jeroendegerickx/git/worldcereal/worldcereal-classification/notebooks')\n", + "%load_ext autoreload\n", + "%autoreload 2" + ] + }, { "cell_type": "code", "execution_count": null, @@ -339,6 +352,7 @@ "outputs": [], "source": [ "from worldcereal.job import WorldCerealProductType, generate_map, CropTypeParameters\n", + "from utils import _results_to_pickle\n", "\n", "# Initializes default parameters\n", "parameters = CropTypeParameters()\n", @@ -359,7 +373,9 @@ " product_type=WorldCerealProductType.CROPTYPE,\n", " croptype_parameters=parameters,\n", " postprocess_parameters=postprocess_parameters,\n", - ")" + ")\n", + "# Write results to pickle file\n", + "_results_to_pickle(job_results, output_dir)" ] }, { @@ -416,6 +432,17 @@ "print(filepaths)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from utils import visualize_classification\n", + "\n", + "visualize_classification(filepaths, \"croptype\")" + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/worldcereal_v1_demo_default_cropland.ipynb b/notebooks/worldcereal_v1_demo_default_cropland.ipynb index 801c2e1e..b1fac5f7 100644 --- a/notebooks/worldcereal_v1_demo_default_cropland.ipynb +++ b/notebooks/worldcereal_v1_demo_default_cropland.ipynb @@ -181,6 +181,7 @@ "outputs": [], "source": [ "from worldcereal.job import generate_map, PostprocessParameters\n", + "from utils import _results_to_pickle\n", "\n", "processing_period = slider.get_processing_period()\n", "processing_extent = map.get_processing_extent()\n", @@ -191,7 +192,9 @@ " processing_period,\n", " output_dir=output_dir,\n", " postprocess_parameters=PostprocessParameters(),\n", - ")" + ")\n", + "# Write results to pickle file\n", + "_results_to_pickle(results, output_dir)" ] }, { diff --git a/notebooks/worldcereal_v1_demo_default_cropland_extended.ipynb b/notebooks/worldcereal_v1_demo_default_cropland_extended.ipynb index 37f0d254..cfb11c6b 100644 --- a/notebooks/worldcereal_v1_demo_default_cropland_extended.ipynb +++ b/notebooks/worldcereal_v1_demo_default_cropland_extended.ipynb @@ -203,6 +203,7 @@ "outputs": [], "source": [ "from worldcereal.job import generate_map\n", + "from utils import _results_to_pickle\n", "\n", "processing_period = slider.get_processing_period()\n", "processing_extent = map.get_processing_extent()\n", @@ -213,7 +214,9 @@ " processing_period,\n", " output_dir=output_dir,\n", " postprocess_parameters=postprocess_parameters,\n", - ")" + ")\n", + "# Write results to pickle file\n", + "_results_to_pickle(results, output_dir)" ] }, {