Created
May 6, 2024 07:27
-
-
Save JeroenVerstraelen/ddaf818b5ae71f0fca2dfc68271cec1b to your computer and use it in GitHub Desktop.
OpenEO randomforest training
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shapely | |
from shapely.geometry import Polygon, MultiPolygon | |
import openeo | |
from openeo.rest.job import JobResults | |
from openeo.rest.mlmodel import MlModel | |
from openeo.rest.job import BatchJob | |
feature_collection = {"type": "FeatureCollection", "features": [ | |
{ | |
"type": "Feature", | |
"properties": {"id": "b3dw-wd23", "target": 0}, | |
"geometry": {"type": "Point", "coordinates": [3.4, 51.1]} | |
}, | |
{ | |
"type": "Feature", | |
"properties": {"id": "r8dh-3jkd", "target": 1}, | |
"geometry": {"type": "Point", "coordinates": [3.6, 51.2]} | |
} | |
] | |
} | |
connection = openeo.connect("https://openeo.dataspace.copernicus.eu/").authenticate_oidc() | |
cube = connection.load_collection( | |
"SENTINEL2_L2A", | |
temporal_extent=["2020-12-30", "2022-01-01"], | |
spatial_extent={ | |
'west': 3.39, | |
'east': 3.61, | |
'south': 51.09, | |
'north': 51.21 | |
}, | |
bands=["B02", "B03", "B04"] | |
) | |
cube = cube.reduce_dimension(dimension="t", reducer="mean") | |
predictors = cube.aggregate_spatial(feature_collection, reducer="mean") | |
# predictors.download("predictors2.json") | |
# cube.download("cube.nc") | |
# exit() | |
model = predictors.fit_class_random_forest( | |
target=feature_collection | |
) | |
model = model.save_ml_model() | |
training_job = model.create_job(title="randomforest_training_job") | |
print(training_job) | |
results : JobResults = training_job.start_and_wait().get_results() | |
print(results) | |
links = results.get_metadata()['links'] | |
ml_model_metadata_url = [link for link in links if 'ml_model_metadata.json' in link['href']][0]['href'] | |
print(ml_model_metadata_url) | |
# c = openeo.connect("https://openeo.dataspace.copernicus.eu/").authenticate_oidc() | |
# job: BatchJob = c.job("j-24050486ed974fdc80e73eff53974d89") | |
# results : JobResults = job.get_results() | |
# links = results.get_metadata()['links'] | |
# ml_model_metadata_url = [link for link in links if 'ml_model_metadata.json' in link['href']][0]['href'] | |
# print(ml_model_metadata_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment