Skip to content

Commit

Permalink
save openeo results to pickle
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegerickx committed Oct 17, 2024
1 parent 5b0d4f8 commit 88e2616
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 6 deletions.
34 changes: 34 additions & 0 deletions notebooks/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ast
import copy
import logging
import pickle
import random
from calendar import monthrange
from datetime import datetime, timedelta
Expand Down Expand Up @@ -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 #############


Expand Down
5 changes: 4 additions & 1 deletion notebooks/worldcereal_v1_demo_custom_cropland.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)"
]
},
{
Expand Down
5 changes: 4 additions & 1 deletion notebooks/worldcereal_v1_demo_custom_cropland_extended.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)"
]
},
{
Expand Down
5 changes: 4 additions & 1 deletion notebooks/worldcereal_v1_demo_custom_croptype.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)"
]
},
{
Expand Down
29 changes: 28 additions & 1 deletion notebooks/worldcereal_v1_demo_custom_croptype_extended.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@
"</div>"
]
},
{
"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,
Expand Down Expand Up @@ -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",
Expand All @@ -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)"
]
},
{
Expand Down Expand Up @@ -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": {},
Expand Down
5 changes: 4 additions & 1 deletion notebooks/worldcereal_v1_demo_default_cropland.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)"
]
},
{
Expand Down

0 comments on commit 88e2616

Please sign in to comment.