def test_build_parameterized_cube_load_collection_band(con100): layer = Parameter.string("layer") bands = [Parameter.string("band8"), Parameter.string("band12")] cube = con100.load_collection(layer, bands=bands) assert cube.flat_graph() == { "loadcollection1": { "process_id": "load_collection", "arguments": { "id": { "from_parameter": "layer" }, "temporal_extent": None, "spatial_extent": None, "bands": [{ "from_parameter": "band8" }, { "from_parameter": "band12" }] }, "result": True, } }
def test_build_parameterized_cube_load_collection(con100): layer = Parameter.string("layer") dates = Parameter.string("dates") bbox = Parameter("bbox", schema="object") cube = con100.load_collection(layer, spatial_extent=bbox, temporal_extent=dates) assert cube.flat_graph() == { "loadcollection1": { "process_id": "load_collection", "arguments": { "id": { "from_parameter": "layer" }, "temporal_extent": { "from_parameter": "dates" }, "spatial_extent": { "from_parameter": "bbox" } }, "result": True, } }
def test_parameter_schema_str(): p = Parameter(name="x", description="the x value.", schema="number") assert p.to_dict() == { "name": "x", "description": "the x value.", "schema": { "type": "number" } }
def test_parameter_integer(): assert Parameter.integer("iterations").to_dict() == { "name": "iterations", "description": "iterations", "schema": {"type": "integer"} } assert Parameter.integer("iterations", description="Iterations.").to_dict() == { "name": "iterations", "description": "Iterations.", "schema": {"type": "integer"} } assert Parameter.integer("iterations", default=5).to_dict() == { "name": "iterations", "description": "iterations", "schema": {"type": "integer"}, "optional": True, "default": 5 }
def test_parameter_string(): assert Parameter.string("color").to_dict() == { "name": "color", "description": "color", "schema": {"type": "string"} } assert Parameter.string("color", description="The color.").to_dict() == { "name": "color", "description": "The color.", "schema": {"type": "string"} } assert Parameter.string("color", default="red").to_dict() == { "name": "color", "description": "color", "schema": {"type": "string"}, "optional": True, "default": "red" }
def test_parameter_array(): assert Parameter.array("bands").to_dict() == { "name": "bands", "description": "bands", "schema": {"type": "array"} } assert Parameter.array("bands", description="Bands.").to_dict() == { "name": "bands", "description": "Bands.", "schema": {"type": "array"} } assert Parameter.array("bands", default=["red", "green", "blue"]).to_dict() == { "name": "bands", "description": "bands", "schema": {"type": "array"}, "optional": True, "default": ["red", "green", "blue"] }
def test_parameter_number(): assert Parameter.number("iterations").to_dict() == { "name": "iterations", "description": "iterations", "schema": {"type": "number"} } assert Parameter.number("iterations", description="Iterations.").to_dict() == { "name": "iterations", "description": "Iterations.", "schema": {"type": "number"} } assert Parameter.number("iterations", default=12.34).to_dict() == { "name": "iterations", "description": "iterations", "schema": {"type": "number"}, "optional": True, "default": 12.34 }
def test_parameter_schema_default_none(): p = Parameter(name="x", description="the x value.", schema="number", default=None) assert p.to_dict() == { "name": "x", "description": "the x value.", "schema": { "type": "number" }, "optional": True, "default": None }
def test_pgnode_parameter_fahrenheit(): from openeo.processes import divide, subtract pg = divide(x=subtract(x=Parameter.number("f", description="Fahrenheit"), y=32), y=1.8) assert pg.flat_graph() == { "subtract1": { "process_id": "subtract", "arguments": { "x": { "from_parameter": "f" }, "y": 32 } }, "divide1": { "process_id": "divide", "arguments": { "x": { "from_node": "subtract1" }, "y": 1.8 }, "result": True }, }
def build_process_dict( process_graph: Union[dict, ProcessBuilderBase], process_id: Optional[str] = None, summary: Optional[str] = None, description: Optional[str] = None, parameters: Optional[List[Union[Parameter, dict]]] = None, returns: Optional[dict] = None, ) -> dict: """ Build a dictionary describing a process with metadaa (`process_graph`, `parameters`, `description`, ...) :param process_graph: dict or builder representing a process graph :param process_id: identifier of the process :param summary: short summary of what the process does :param description: detailed description :param parameters: list of process parameters (which have name, schema, default value, ...) :param returns: description and schema of what process returns :return: dictionary in openEO "process graph with metadata" format """ process = dict_no_none(process_graph=as_flat_graph(process_graph), id=process_id, summary=summary, description=description, returns=returns) if parameters is not None: process["parameters"] = [ (p if isinstance(p, Parameter) else Parameter(**p)).to_dict() for p in parameters ] return process
def test_aggregate_spatial_parameter_polygon(connection, api_version): if api_version < ComparableVersion("1.0.0"): pytest.skip() geometries = Parameter("polygon") res = (connection.load_collection("S2").filter_bbox( 3, 6, 52, 50).aggregate_spatial(geometries=geometries, reducer="mean")) assert get_execute_graph(res) == load_json_resource( 'data/%s/aggregate_zonal_parameter.json' % api_version)
def test_pgnode_parameter_basic(): pg = openeo.processes.add(x=Parameter.number("a", description="A."), y=42) assert pg.flat_graph() == { "add1": { "process_id": "add", "arguments": {"x": {"from_parameter": "a"}, "y": 42}, "result": True } }
def test_build_parameterized_cube_single_date(con100): layer = Parameter.string("layer") date = Parameter.string("date") bbox = Parameter("bbox", schema="object") cube = con100.load_collection(layer).filter_temporal( date, date).filter_bbox(bbox) assert cube.flat_graph() == { "loadcollection1": { "process_id": "load_collection", "arguments": { "id": { "from_parameter": "layer" }, "temporal_extent": None, "spatial_extent": None }, }, "filtertemporal1": { "process_id": "filter_temporal", "arguments": { "data": { "from_node": "loadcollection1" }, "extent": [{ "from_parameter": "date" }, { "from_parameter": "date" }] }, }, "filterbbox1": { "process_id": "filter_bbox", "arguments": { "data": { "from_node": "filtertemporal1" }, "extent": { "from_parameter": "bbox" } }, "result": True, } }
def test_parameter_raster_cube(): p = Parameter.raster_cube(name="x") assert p.to_dict() == { "name": "x", "description": "A data cube.", "schema": { "type": "object", "subtype": "raster-cube" } }
def test_build_parameterized_cube_band_math(con100): layer = Parameter.string("layer") bands = [Parameter.string("band8"), Parameter.string("band12")] cube = con100.load_collection(layer, bands=bands) x = cube.band(0) * cube.band(1) assert x.flat_graph() == { "loadcollection1": { "process_id": "load_collection", "arguments": { "id": {"from_parameter": "layer"}, "spatial_extent": None, "temporal_extent": None, "bands": [{"from_parameter": "band8"}, {"from_parameter": "band12"}], }, }, "reducedimension1": { "process_id": "reduce_dimension", "arguments": { "data": {"from_node": "loadcollection1"}, "dimension": "bands", "reducer": {"process_graph": { "arrayelement1": { "process_id": "array_element", "arguments": {"data": {"from_parameter": "data"}, "index": 0}, }, "arrayelement2": { "process_id": "array_element", "arguments": {"data": {"from_parameter": "data"}, "index": 1}, }, "multiply1": { "process_id": "multiply", "arguments": { "x": {"from_node": "arrayelement1"}, "y": {"from_node": "arrayelement2"}}, "result": True } }} }, "result": True } }
def test_store_collision(con100, requests_mock): subtract = { "minusy": { "process_id": "multiply", "arguments": {"x": {"from_parameter": "y"}, "y": -1} }, "add": { "process_id": "add", "arguments": {"x": {"from_parameter": "x"}, "y": {"from_node": "minusy"}, }, "result": True, } } def check_body(request): body = request.json() assert body == { "process_graph": subtract, "parameters": [ {"name": "x", "description": "x", "schema": {"type": "number"}}, {"name": "y", "description": "y", "schema": {"type": "number"}} ], "public": False, } return True requests_mock.get(API_URL + "/processes", json={"processes": [{"id": "add"}, {"id": "subtract"}]}) adapter1 = requests_mock.put(API_URL + "/process_graphs/my_subtract", additional_matcher=check_body) adapter2 = requests_mock.put(API_URL + "/process_graphs/subtract", additional_matcher=check_body) parameters = [Parameter.number("x"), Parameter.number("y")] with pytest.warns(None) as recorder: udp = con100.save_user_defined_process("my_subtract", subtract, parameters=parameters) assert isinstance(udp, RESTUserDefinedProcess) assert adapter1.call_count == 1 assert len(recorder) == 0 with pytest.warns(UserWarning, match="same id as a pre-defined process") as recorder: udp = con100.save_user_defined_process("subtract", subtract, parameters=parameters) assert isinstance(udp, RESTUserDefinedProcess) assert adapter2.call_count == 1 assert len(recorder) == 1
def test_build_process_dict_from_pg_dict(): actual = build_process_dict(process_graph={ "add": { "process_id": "add", "arguments": { "x": { "from_parameter": "data" }, "y": 1 }, "result": True } }, process_id="increment", summary="Increment value", description="Add 1 to input.", parameters=[Parameter.number(name="data")], returns={"schema": { "type": "number" }}) expected = { "id": "increment", "description": "Add 1 to input.", "summary": "Increment value", "process_graph": { "add": { "arguments": { "x": { "from_parameter": "data" }, "y": 1 }, "process_id": "add", "result": True } }, "parameters": [{ "name": "data", "description": "data", "schema": { "type": "number" } }], "returns": { "schema": { "type": "number" } }, } assert actual == expected
def store(self, process_graph: dict, parameters: List[Union[Parameter, dict]] = None, public: bool = False): req = {'process_graph': process_graph, 'public': public} if parameters is not None: req["parameters"] = [ (p if isinstance(p, Parameter) else Parameter(**p)).to_dict() for p in parameters ] self._connection.put(path="/process_graphs/{}".format( self.user_defined_process_id), json=req)
def test_filter_bbox_parameter(con100: Connection): expected = { "process_id": "filter_bbox", "arguments": { "data": {"from_node": "loadcollection1"}, "extent": {"from_parameter": "my_bbox"} }, "result": True } bbox_param = Parameter(name="my_bbox", schema={"type": "object"}) cube = con100.load_collection("S2").filter_bbox(bbox_param) assert _get_leaf_node(cube) == expected cube = con100.load_collection("S2").filter_bbox(bbox=bbox_param) assert _get_leaf_node(cube) == expected
def test_build_process_dict_from_process(con100): from openeo.processes import add data = Parameter.number("data") proc = add(x=data, y=1) actual = build_process_dict(process_graph=proc, process_id="increment", summary="Increment value", description="Add 1 to input.", parameters=[data], returns={"schema": { "type": "number" }}) expected = { "id": "increment", "description": "Add 1 to input.", "summary": "Increment value", "process_graph": { "add1": { "arguments": { "x": { "from_parameter": "data" }, "y": 1 }, "process_id": "add", "result": True } }, "parameters": [{ "name": "data", "description": "data", "schema": { "type": "number" } }], "returns": { "schema": { "type": "number" } }, } assert actual == expected
def store( self, process_graph: Union[dict, ProcessBuilderBase], parameters: List[Union[Parameter, dict]] = None, public: bool = False, summary: str = None, description: str = None ): req = { 'process_graph': as_flat_graph(process_graph), 'public': public } if parameters is not None: req["parameters"] = [ (p if isinstance(p, Parameter) else Parameter(**p)).to_dict() for p in parameters ] if summary is not None: req["summary"] = summary if description is not None: req["description"] = description self._connection.put(path="/process_graphs/{}".format(self.user_defined_process_id), json=req)
def test_parameter_default(): p = Parameter(name="x") assert p.to_dict() == {"name": "x", "description": "x", "schema": {}}
with pytest.warns(None) as recorder: udp = con100.save_user_defined_process("my_subtract", subtract) assert isinstance(udp, RESTUserDefinedProcess) assert adapter1.call_count == 1 assert len(recorder) == 0 with pytest.warns(UserWarning, match="same id as a pre-defined process") as recorder: udp = con100.save_user_defined_process("subtract", subtract) assert isinstance(udp, RESTUserDefinedProcess) assert adapter2.call_count == 1 assert len(recorder) == 1 @pytest.mark.parametrize(["parameters", "expected_parameters"], [ ([Parameter(name="data", description="A data cube.", schema="number")], { "parameters": [{ "name": "data", "description": "A data cube.", "schema": { "type": "number" } }] }), ([ Parameter( name="data", description="A cube.", schema="number", default=42) ], { "parameters": [{ "name": "data", "description": "A cube.",