openeo-gfmap 0.4.3__py3-none-any.whl → 0.4.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -57,7 +57,7 @@ def resample_reproject(
57
57
  CRS.from_epsg(int(epsg_code))
58
58
  except (CRSError, ValueError) as exc:
59
59
  raise ValueError(
60
- f"Specified target_crs: {epsg_code} is not a valid " "EPSG code."
60
+ f"Specified target_crs: {epsg_code} is not a valid EPSG code."
61
61
  ) from exc
62
62
  return datacube.resample_spatial(
63
63
  resolution=resolution, projection=epsg_code, method=method
@@ -132,49 +132,20 @@ def _load_collection(
132
132
  temporal_extent = [temporal_extent.start_date, temporal_extent.end_date]
133
133
 
134
134
  if fetch_type == FetchType.TILE:
135
- assert isinstance(
136
- spatial_extent, BoundingBoxExtent
137
- ), "Please provide only a bounding box for tile based fetching."
138
- spatial_extent = dict(spatial_extent)
139
- cube = load_collection_method(
140
- connection=connection,
141
- bands=bands,
142
- spatial_extent=spatial_extent,
143
- temporal_extent=temporal_extent,
144
- properties=load_collection_parameters,
145
- )
146
- elif fetch_type == FetchType.POINT:
147
- if isinstance(spatial_extent, GeoJSON):
148
- assert (
149
- spatial_extent["type"] == "FeatureCollection"
150
- ), "Please provide a FeatureCollection type of GeoJSON"
151
- elif isinstance(spatial_extent, str):
152
- assert spatial_extent.startswith("https://") or spatial_extent.startswith(
153
- "http://"
154
- ), "Please provide a valid URL or a path to a GeoJSON file."
155
- else:
135
+ if isinstance(spatial_extent, BoundingBoxExtent):
136
+ spatial_extent = dict(spatial_extent)
137
+ elif spatial_extent is not None:
156
138
  raise ValueError(
157
- "Please provide a valid URL to a GeoParquet or GeoJSON file."
139
+ "`spatial_extent` should be either None or an instance of BoundingBoxExtent for tile-based fetching."
158
140
  )
159
141
  cube = load_collection_method(
160
142
  connection=connection,
161
143
  bands=bands,
144
+ spatial_extent=spatial_extent,
162
145
  temporal_extent=temporal_extent,
163
146
  properties=load_collection_parameters,
164
147
  )
165
- elif fetch_type == FetchType.POLYGON:
166
- if isinstance(spatial_extent, GeoJSON):
167
- assert (
168
- spatial_extent["type"] == "FeatureCollection"
169
- ), "Please provide a FeatureCollection type of GeoJSON"
170
- elif isinstance(spatial_extent, str):
171
- assert spatial_extent.startswith("https://") or spatial_extent.startswith(
172
- "http://"
173
- ), "Please provide a valid URL or a path to a GeoJSON file."
174
- else:
175
- raise ValueError(
176
- "Please provide a valid URL to a GeoParquet or GeoJSON file."
177
- )
148
+ elif fetch_type == FetchType.POINT or fetch_type == FetchType.POLYGON:
178
149
  cube = load_collection_method(
179
150
  connection=connection,
180
151
  bands=bands,
@@ -190,7 +161,7 @@ def _load_collection(
190
161
  pre_mask = params.get("pre_mask", None)
191
162
  if pre_mask is not None:
192
163
  assert isinstance(pre_mask, openeo.DataCube), (
193
- f"The 'pre_mask' parameter must be an openeo datacube, " f"got {pre_mask}."
164
+ f"The 'pre_mask' parameter must be an openeo datacube, got {pre_mask}."
194
165
  )
195
166
  cube = cube.mask(pre_mask)
196
167
 
@@ -205,7 +176,7 @@ def _load_collection(
205
176
  pre_merge_cube = pre_merge_cube.mask(pre_mask)
206
177
  cube = cube.merge_cubes(pre_merge_cube)
207
178
 
208
- if fetch_type == FetchType.POLYGON:
179
+ if fetch_type == FetchType.POLYGON and spatial_extent is not None:
209
180
  if isinstance(spatial_extent, str):
210
181
  geometry = connection.load_url(
211
182
  spatial_extent,
@@ -2,6 +2,7 @@
2
2
  of inference models on an UDF.
3
3
  """
4
4
 
5
+ import copy
5
6
  import functools
6
7
  import inspect
7
8
  import logging
@@ -16,6 +17,7 @@ import numpy as np
16
17
  import openeo
17
18
  import requests
18
19
  import xarray as xr
20
+ from openeo.metadata import CollectionMetadata
19
21
  from openeo.udf import XarrayDataCube
20
22
  from openeo.udf import inspect as udf_inspect
21
23
  from openeo.udf.udf_data import UdfData
@@ -123,6 +125,16 @@ class ModelInference(ABC):
123
125
  arr = self.execute(arr).transpose("bands", "y", "x")
124
126
  return XarrayDataCube(arr)
125
127
 
128
+ def _apply_metadata(
129
+ self, metadata: CollectionMetadata, parameters: dict
130
+ ) -> CollectionMetadata:
131
+ """Apply metadata to the output data. This method will be executed at the very end of the
132
+ process.
133
+ """
134
+ self._parameters = parameters
135
+
136
+ return metadata.rename_labels(dimension="bands", target=self.output_labels())
137
+
126
138
  @property
127
139
  def epsg(self) -> int:
128
140
  """EPSG code of the input data."""
@@ -211,14 +223,14 @@ class ONNXModelInference(ModelInference):
211
223
  )
212
224
 
213
225
 
214
- def apply_udf_data(udf_data: UdfData) -> XarrayDataCube:
226
+ def apply_udf_data(udf_data: UdfData) -> UdfData:
215
227
  model_inference_class = "<model_inference_class>"
216
228
 
217
229
  model_inference = model_inference_class()
218
230
 
219
231
  # User-defined, model inference class initialized here
220
232
  cube = udf_data.datacube_list[0]
221
- parameters = udf_data.user_context
233
+ parameters = copy.deepcopy(udf_data.user_context)
222
234
 
223
235
  proj = udf_data.proj
224
236
  if proj is not None:
@@ -233,6 +245,14 @@ def apply_udf_data(udf_data: UdfData) -> XarrayDataCube:
233
245
  return udf_data
234
246
 
235
247
 
248
+ def apply_metadata(metadata: CollectionMetadata, context: dict) -> CollectionMetadata:
249
+ model_inference_class = "<model_inference_class>"
250
+
251
+ model_inference = model_inference_class()
252
+
253
+ return model_inference._apply_metadata(metadata, parameters=copy.deepcopy(context))
254
+
255
+
236
256
  def _get_imports() -> str:
237
257
  with open(__file__, "r", encoding="UTF-8") as f:
238
258
  script_source = f.read()
@@ -260,6 +280,13 @@ def _get_apply_udf_data(model_inference: ModelInference) -> str:
260
280
  return source.replace('"<model_inference_class>"', model_inference.__name__)
261
281
 
262
282
 
283
+ def _get_apply_metadata(model_inference: ModelInference) -> str:
284
+ source_lines = inspect.getsource(apply_metadata)
285
+ source = "".join(source_lines)
286
+ # replace in the source function the `model_inference_class`
287
+ return source.replace('"<model_inference_class>"', model_inference.__name__)
288
+
289
+
263
290
  def _generate_udf_code(
264
291
  model_inference_class: ModelInference, dependencies: list
265
292
  ) -> openeo.UDF:
@@ -286,7 +313,9 @@ def _generate_udf_code(
286
313
  udf_code += _get_imports() + "\n\n"
287
314
  udf_code += f"{inspect.getsource(ModelInference)}\n\n"
288
315
  udf_code += f"{inspect.getsource(model_inference_class)}\n\n"
289
- udf_code += _get_apply_udf_data(model_inference_class)
316
+ udf_code += f"{_get_apply_udf_data(model_inference_class)}\n\n"
317
+ udf_code += _get_apply_metadata(model_inference_class)
318
+
290
319
  return udf_code
291
320
 
292
321
 
@@ -304,15 +333,13 @@ def apply_model_inference(
304
333
  """
305
334
  model_inference = model_inference_class()
306
335
  model_inference._parameters = parameters
307
- output_labels = model_inference.output_labels()
308
336
  dependencies = model_inference.dependencies()
309
337
 
310
338
  udf_code = _generate_udf_code(model_inference_class, dependencies)
311
339
 
312
340
  udf = openeo.UDF(code=udf_code, context=parameters)
313
341
 
314
- cube = cube.apply_neighborhood(process=udf, size=size, overlap=overlap)
315
- return cube.rename_labels(dimension="bands", target=output_labels)
342
+ return cube.apply_neighborhood(process=udf, size=size, overlap=overlap)
316
343
 
317
344
 
318
345
  def apply_model_inference_local(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: openeo_gfmap
3
- Version: 0.4.3
3
+ Version: 0.4.5
4
4
  Summary: OpenEO General Framework for Mapping
5
5
  Project-URL: Homepage, https://github.com/Open-EO/openeo-gfmap
6
6
  Project-URL: Bug Tracker, https://github.com/Open-EO/openeo-gfmap/issues
@@ -6,13 +6,13 @@ openeo_gfmap/temporal.py,sha256=qhKCgSoOc0CrscPTru-d7acaxsCVhftyYrb_8UVU1S4,583
6
6
  openeo_gfmap/features/__init__.py,sha256=UtQUPnglF9uURn9FqtegnazuE4_CgQP6a2Cdx1TOuZ0,419
7
7
  openeo_gfmap/features/feature_extractor.py,sha256=ApTCCbD1-S4VOOSEZh9i-Gqxso87xwe_z7CN35fP15A,14719
8
8
  openeo_gfmap/fetching/__init__.py,sha256=KFDwqKTwXUDhFqPqeaIg5uCL2xp7lXmNzcbAph-EU1c,938
9
- openeo_gfmap/fetching/commons.py,sha256=uug-BfbySbCPfQAoeBOLjDFt6nOiV41Tmat8VDvGuBo,7871
9
+ openeo_gfmap/fetching/commons.py,sha256=S6w9I5zdksPJPFfadDgAblrX3s2D47V9saymhzGLmig,6668
10
10
  openeo_gfmap/fetching/fetching.py,sha256=dHeOMzN6OPgD8YFfZtcCzEOwQqo47IeBgIdS2mrx3MY,3674
11
11
  openeo_gfmap/fetching/generic.py,sha256=ojSO52cnLsWpC6FAnLRoXxfQmTiC839DzFH8MAok2B8,5851
12
12
  openeo_gfmap/fetching/s1.py,sha256=Ek9Ek-GExyKdb-9Ijja6I-izOmVvPY2C9w9gyyGGjII,6360
13
13
  openeo_gfmap/fetching/s2.py,sha256=ytjrZiZIwXxrdiky2V0bAKLBU9Dpaa5b2XsHvI6jl1M,7718
14
14
  openeo_gfmap/inference/__init__.py,sha256=M6NnKGYCpHNYmRL9OkHi5GmfCtWoJ0wCNR6VXRuDgjE,165
15
- openeo_gfmap/inference/model_inference.py,sha256=0qPUgrjI1hy5ZnyGwuuvvw5oxnMGdgvvu9Go6-e9LZQ,12550
15
+ openeo_gfmap/inference/model_inference.py,sha256=aBxvjeqCJJATDx6zLYBAEASYs7nolIozOa01fI76Qvc,13511
16
16
  openeo_gfmap/manager/__init__.py,sha256=2bckkPiDQBgoBWD9spk1BKXy2UGkWKe50A3HmIwmqrA,795
17
17
  openeo_gfmap/manager/job_manager.py,sha256=-MZJBfF_wV94FejoYbFPNviEQx3jLmJXb6XLeHg7egE,27221
18
18
  openeo_gfmap/manager/job_splitters.py,sha256=kkGxgiudY3LrA40Ro_9q2eFW_Pjdm5a5VaNOXk2w5qo,10694
@@ -34,7 +34,7 @@ openeo_gfmap/utils/intervals.py,sha256=V6l3ofww50fN_pvWC4NuGQ2ZsyGdhAlTZTiRcC0fo
34
34
  openeo_gfmap/utils/netcdf.py,sha256=KkAAxnq-ZCMjDMd82638noYwxqNpMsnpiU04Q-qX26A,698
35
35
  openeo_gfmap/utils/split_stac.py,sha256=asjT0jx6ic8GJFqqAisaWxOvQ_suSRv4sxyFOyHFvpI,3895
36
36
  openeo_gfmap/utils/tile_processing.py,sha256=QZ9bi5tPmyTVyyNvFZgd26s5dSnMl1grTKq2veK1C90,2068
37
- openeo_gfmap-0.4.3.dist-info/METADATA,sha256=Lj7HiV-92WCJQTqlyZWZKyc8SNGvbFyzTDMyiWCK8FY,4374
38
- openeo_gfmap-0.4.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
39
- openeo_gfmap-0.4.3.dist-info/licenses/LICENSE,sha256=aUuGpjieWiscTNtyLcSaeVsJ4pb6J9c4wUq1bR0e4t4,11349
40
- openeo_gfmap-0.4.3.dist-info/RECORD,,
37
+ openeo_gfmap-0.4.5.dist-info/METADATA,sha256=5jnfj1lXVeQDH5Kf8uXNzquF2r8f7pWFAQYQQi5NxNY,4374
38
+ openeo_gfmap-0.4.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
39
+ openeo_gfmap-0.4.5.dist-info/licenses/LICENSE,sha256=aUuGpjieWiscTNtyLcSaeVsJ4pb6J9c4wUq1bR0e4t4,11349
40
+ openeo_gfmap-0.4.5.dist-info/RECORD,,