Skip to content

Commit

Permalink
Add secondary_raster_id parameter to run_detector
Browse files Browse the repository at this point in the history
This enables running change detectors.

Also improved a bit the test helpers.
  • Loading branch information
stephanerestani-picterra committed Jul 9, 2024
1 parent 4813674 commit 9e36c78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/picterra/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,22 +737,26 @@ def delete_detector(self, detector_id: str):
if not resp.ok:
raise APIError(resp.text)

def run_detector(self, detector_id: str, raster_id: str) -> str:
def run_detector(
self, detector_id: str, raster_id: str, secondary_raster_id: str | None = None
) -> str:
"""
Runs a detector on a raster: predictions are subject to a minimum charge
of 10 MP.
Args:
detector_id: The id of the detector
raster_id: The id of the raster
secondary_raster_id: The id of the secondary raster. This needs to be provided to
run change detectors.
Returns:
operation_id: The id of the operation. You typically want to pass this
to `download_result_to_feature_collection`
"""
resp = self.sess.post(
self._api_url("detectors/%s/run/" % detector_id),
json={"raster_id": raster_id},
json={"raster_id": raster_id, "secondary_raster_id": secondary_raster_id},
)
if not resp.ok:
raise APIError(resp.text)
Expand Down
28 changes: 20 additions & 8 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,20 @@ def add_mock_remote_import_responses(upload_id, post_body):
)


def add_mock_detector_run_responses(detector_id):
op_id = 43
_add_api_response("detectors/%s/run/" % detector_id, responses.POST, OP_RESP)
def add_mock_detector_run_responses(detector_id, raster_id, secondary_raster_id=None):
_add_api_response(
"detectors/%s/run/" % detector_id, responses.POST, OP_RESP,
match=responses.matchers.json_params_matcher({
"raster_id": raster_id,
"secondary_raster_id": secondary_raster_id,
}),
)
# First status check
data = {"status": "running"}
_add_api_response("operations/%s/" % op_id, json=data)
_add_api_response("operations/%s/" % OPERATION_ID, json=data)
# Second status check
data = {"status": "success"}
_add_api_response("operations/%s/" % op_id, json=data)
_add_api_response("operations/%s/" % OPERATION_ID, json=data)


def add_mock_vector_layer_responses(upload_id, raster_id, name, color):
Expand Down Expand Up @@ -895,11 +900,18 @@ def test_set_raster_detection_areas_from_file():

@responses.activate
def test_run_detector():
add_mock_detector_run_responses(1)
add_mock_operations_responses("success")
add_mock_detector_run_responses(1, 2)
client = _client()
client.run_detector(1, 2)
assert len(responses.calls) == 2
assert len(responses.calls) == 3


@responses.activate
def test_run_detector_secondary_raster():
add_mock_detector_run_responses(1, 2, 3)
client = _client()
client.run_detector(1, 2, 3)
assert len(responses.calls) == 3


@responses.activate
Expand Down

0 comments on commit 9e36c78

Please sign in to comment.