Skip to content

Commit

Permalink
more process and beter registery
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Dec 10, 2024
1 parent 82147ed commit f7c90f8
Show file tree
Hide file tree
Showing 49 changed files with 2,768 additions and 258 deletions.
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ select = [
"B", # flake8-bugbear
]
ignore = [
"D103",
"D101",
"E501", # line too long, handled by black
"B008", # do not perform function calls in argument defaults
"B905", # ignore zip() without an explicit strict= parameter, only support with python >3.10
]

[tool.ruff.lint.extend-per-file-ignores]
"models.py" = ["D101"]

[tool.mypy]
no_implicit_optional = true
strict_optional = true
Expand Down
72 changes: 27 additions & 45 deletions titiler/openeo/processes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,40 @@
"""titiler.openeo.processes."""

import builtins
import importlib
import inspect
import json
import keyword
from pathlib import Path

from openeo_pg_parser_networkx import ProcessRegistry
from openeo_pg_parser_networkx.process_registry import Process

from .implementations import (
clip,
linear_scale_range,
normalized_difference,
save_result,
)
from .implementations.core import process

json_path = Path(__file__).parent / "specs"
process_json_paths = [pg_path for pg_path in (json_path).glob("*.json")] # noqa: C416
PROCESS_SPECIFICATIONS = {f.stem: json.load(open(f)) for f in process_json_paths}
json_path = Path(__file__).parent / "data"
PROCESS_SPECIFICATIONS = {}
for f in (json_path).glob("*.json"):
spec_json = json.load(open(f))
process_name = spec_json["id"]
# Make sure we don't overwrite any builtins (e.g min -> _min)
if spec_json["id"] in dir(builtins) or keyword.iskeyword(spec_json["id"]):
process_name = "_" + spec_json["id"]

PROCESS_SPECIFICATIONS[process_name] = spec_json

PROCESS_IMPLEMENTATIONS = [
func
for _, func in inspect.getmembers(
importlib.import_module("titiler.openeo.processes.implementations"),
inspect.isfunction,
)
]

# `process` is wrapped around each registered implementation
# Create Processes Registry
process_registry = ProcessRegistry(wrap_funcs=[process])
process_registry["normalized_difference"] = Process(
spec=PROCESS_SPECIFICATIONS["normalized_difference"],
implementation=normalized_difference,
)
process_registry["clip"] = process_registry["clip"] = Process(
spec=PROCESS_SPECIFICATIONS["clip"], implementation=clip
)
process_registry["linear_scale_range"] = process_registry["linear_scale_range"] = (
Process(
spec=PROCESS_SPECIFICATIONS["linear_scale_range"],
implementation=linear_scale_range,

for func in PROCESS_IMPLEMENTATIONS:
process_registry[func.__name__] = Process(
spec=PROCESS_SPECIFICATIONS[func.__name__], implementation=func
)
)
process_registry["save_result"] = process_registry["save_result"] = Process(
spec=PROCESS_SPECIFICATIONS["save_result"], implementation=save_result
)

# Import these pre-defined processes from openeo_processes_dask and register them into registry
# processes_from_module = [
# func
# for _, func in inspect.getmembers(
# importlib.import_module("openeo_processes_dask.process_implementations"),
# inspect.isfunction,
# )
# ]

# specs_module = importlib.import_module("openeo_processes_dask.specs")
# specs = {
# func.__name__: getattr(specs_module, func.__name__)
# for func in processes_from_module
# }

# for func in processes_from_module:
# process_registry[func.__name__] = Process(
# spec=specs[func.__name__], implementation=func
# )
98 changes: 98 additions & 0 deletions titiler/openeo/processes/data/absolute.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"id": "absolute",
"summary": "Absolute value",
"description": "Computes the absolute value of a real number `x`, which is the \"unsigned\" portion of x and often denoted as *|x|*.\n\nThe no-data value `null` is passed through and therefore gets propagated.",
"categories": [
"math"
],
"parameters": [
{
"name": "x",
"description": "A number.",
"schema": {
"type": [
"number",
"null"
]
}
}
],
"returns": {
"description": "The computed absolute value.",
"schema": {
"type": [
"number",
"null"
],
"minimum": 0
}
},
"examples": [
{
"arguments": {
"x": 0
},
"returns": 0
},
{
"arguments": {
"x": 3.5
},
"returns": 3.5
},
{
"arguments": {
"x": -0.4
},
"returns": 0.4
},
{
"arguments": {
"x": -3.5
},
"returns": 3.5
}
],
"links": [
{
"rel": "about",
"href": "http://mathworld.wolfram.com/AbsoluteValue.html",
"title": "Absolute value explained by Wolfram MathWorld"
}
],
"process_graph": {
"lt": {
"process_id": "lt",
"arguments": {
"x": {
"from_parameter": "x"
},
"y": 0
}
},
"multiply": {
"process_id": "multiply",
"arguments": {
"x": {
"from_parameter": "x"
},
"y": -1
}
},
"if": {
"process_id": "if",
"arguments": {
"value": {
"from_node": "lt"
},
"accept": {
"from_node": "multiply"
},
"reject": {
"from_parameter": "x"
}
},
"result": true
}
}
}
91 changes: 91 additions & 0 deletions titiler/openeo/processes/data/add.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"id": "add",
"summary": "Addition of two numbers",
"description": "Sums up the two numbers `x` and `y` (*`x + y`*) and returns the computed sum.\n\nNo-data values are taken into account so that `null` is returned if any element is such a value.\n\nThe computations follow [IEEE Standard 754](https://ieeexplore.ieee.org/document/8766229) whenever the processing environment supports it.",
"categories": [
"math"
],
"parameters": [
{
"name": "x",
"description": "The first summand.",
"schema": {
"type": [
"number",
"null"
]
}
},
{
"name": "y",
"description": "The second summand.",
"schema": {
"type": [
"number",
"null"
]
}
}
],
"returns": {
"description": "The computed sum of the two numbers.",
"schema": {
"type": [
"number",
"null"
]
}
},
"examples": [
{
"arguments": {
"x": 5,
"y": 2.5
},
"returns": 7.5
},
{
"arguments": {
"x": -2,
"y": -4
},
"returns": -6
},
{
"arguments": {
"x": 1,
"y": null
},
"returns": null
}
],
"links": [
{
"rel": "about",
"href": "http://mathworld.wolfram.com/Sum.html",
"title": "Sum explained by Wolfram MathWorld"
},
{
"rel": "about",
"href": "https://ieeexplore.ieee.org/document/8766229",
"title": "IEEE Standard 754-2019 for Floating-Point Arithmetic"
}
],
"process_graph": {
"sum": {
"process_id": "sum",
"arguments": {
"data": [
{
"from_parameter": "x"
},
{
"from_parameter": "y"
}
],
"ignore_nodata": false
},
"result": true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
{
"id": "apply",
"summary": "Apply a process to each value",
"description": "Applies a process to each value in the data cube (i.e. a local operation). In contrast, the process ``apply_dimension()`` applies a process to all values along a particular dimension.",
"description": "Applies a process to each value in the data cube/image.",
"categories": [
"cubes"
],
"parameters": [
{
"name": "data",
"description": "A data cube.",
"schema": {
"type": "object",
"subtype": "datacube"
}
"schema": [
{
"type": "object",
"subtype": "datacube"
},
{
"type": "object",
"subtype": "imagedata"
}
]
},
{
"name": "process",
"description": "A process that accepts and returns a single value and is applied on each individual value in the data cube. The process may consist of multiple sub-processes and could, for example, consist of processes such as ``absolute()`` or ``linear_scale_range()``.",
"description": "A process that accepts and returns a single value and is applied on each individual value in the data cube/image. The process may consist of multiple sub-processes and could, for example, consist of processes such as `absolute()` or `linear_scale_range()`.",
"schema": {
"type": "object",
"subtype": "process-graph",
Expand Down Expand Up @@ -58,16 +64,16 @@
],
"returns": {
"description": "A data cube with the newly computed values and the same dimensions. The dimension properties (name, type, labels, reference system and resolution) remain unchanged.",
"schema": {
"type": "object",
"subtype": "datacube"
}
"schema": [
{
"type": "object",
"subtype": "datacube"
},
{
"type": "object",
"subtype": "imagedata"
}
]
},
"links": [
{
"href": "https://openeo.org/documentation/1.0/datacubes.html#apply",
"rel": "about",
"title": "Apply explained in the openEO documentation"
}
]
"links": []
}
Loading

0 comments on commit f7c90f8

Please sign in to comment.