[test] update test cases
Browse files
tests/io/test_geo_helpers.py
CHANGED
|
@@ -77,8 +77,9 @@ class TestGeoHelpers(unittest.TestCase):
|
|
| 77 |
output = get_vectorized_raster_as_geojson(mask=mask, transform=transform)
|
| 78 |
assert output["n_shapes_geojson"] == input_output["output"]["n_shapes_geojson"]
|
| 79 |
output_geojson = shapely.from_geojson(output["geojson"])
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
def test_get_vectorized_raster_as_geojson_fail(self):
|
| 84 |
from samgis.io.geo_helpers import get_vectorized_raster_as_geojson
|
|
|
|
| 77 |
output = get_vectorized_raster_as_geojson(mask=mask, transform=transform)
|
| 78 |
assert output["n_shapes_geojson"] == input_output["output"]["n_shapes_geojson"]
|
| 79 |
output_geojson = shapely.from_geojson(output["geojson"])
|
| 80 |
+
assert isinstance(output_geojson, shapely.GeometryCollection)
|
| 81 |
+
output_geojson_dict = json.loads(output["geojson"])
|
| 82 |
+
assert len(output_geojson_dict["features"]) > 0
|
| 83 |
|
| 84 |
def test_get_vectorized_raster_as_geojson_fail(self):
|
| 85 |
from samgis.io.geo_helpers import get_vectorized_raster_as_geojson
|
tests/prediction_api/test_predictors.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import json
|
|
|
|
| 2 |
from unittest.mock import patch
|
| 3 |
|
| 4 |
import numpy as np
|
|
@@ -8,57 +9,60 @@ from samgis.prediction_api.predictors import get_raster_inference, samexporter_p
|
|
| 8 |
from tests import TEST_EVENTS_FOLDER
|
| 9 |
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
@patch.object(predictors, "
|
| 42 |
-
@patch.object(predictors, "
|
| 43 |
-
@patch.object(predictors, "
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
import unittest
|
| 3 |
from unittest.mock import patch
|
| 4 |
|
| 5 |
import numpy as np
|
|
|
|
| 9 |
from tests import TEST_EVENTS_FOLDER
|
| 10 |
|
| 11 |
|
| 12 |
+
class TestPredictors(unittest.TestCase):
|
| 13 |
+
@patch.object(predictors, "SegmentAnythingONNX")
|
| 14 |
+
def test_get_raster_inference(self, segment_anything_onnx_mocked):
|
| 15 |
+
name_fn = "samexporter_predict"
|
| 16 |
|
| 17 |
+
with open(TEST_EVENTS_FOLDER / f"{name_fn}.json") as tst_json:
|
| 18 |
+
inputs_outputs = json.load(tst_json)
|
| 19 |
+
for k, input_output in inputs_outputs.items():
|
| 20 |
+
model_mocked = segment_anything_onnx_mocked()
|
| 21 |
|
| 22 |
+
img = np.load(TEST_EVENTS_FOLDER / f"{name_fn}" / k / "img.npy")
|
| 23 |
+
inference_out = np.load(TEST_EVENTS_FOLDER / f"{name_fn}" / k / "inference_out.npy")
|
| 24 |
+
mask = np.load(TEST_EVENTS_FOLDER / f"{name_fn}" / k / "mask.npy")
|
| 25 |
+
prompt = input_output["input"]["prompt"]
|
| 26 |
+
model_name = input_output["input"]["model_name"]
|
| 27 |
|
| 28 |
+
model_mocked.embed.return_value = np.array(img)
|
| 29 |
+
model_mocked.embed.side_effect = None
|
| 30 |
+
model_mocked.predict_masks.return_value = inference_out
|
| 31 |
+
model_mocked.predict_masks.side_effect = None
|
| 32 |
+
print(f"k:{k}.")
|
| 33 |
+
output_mask, len_inference_out = get_raster_inference(
|
| 34 |
+
img=img,
|
| 35 |
+
prompt=prompt,
|
| 36 |
+
models_instance=model_mocked,
|
| 37 |
+
model_name=model_name
|
| 38 |
+
)
|
| 39 |
+
assert np.array_equal(output_mask, mask)
|
| 40 |
+
assert len_inference_out == input_output["output"]["n_predictions"]
|
| 41 |
|
| 42 |
+
@patch.object(predictors, "get_raster_inference_with_embedding_from_dict")
|
| 43 |
+
@patch.object(predictors, "SegmentAnythingONNX")
|
| 44 |
+
@patch.object(predictors, "download_extent")
|
| 45 |
+
@patch.object(predictors, "get_vectorized_raster_as_geojson")
|
| 46 |
+
def test_samexporter_predict(
|
| 47 |
+
self,
|
| 48 |
+
get_vectorized_raster_as_geojson_mocked,
|
| 49 |
+
download_extent_mocked,
|
| 50 |
+
segment_anything_onnx_mocked,
|
| 51 |
+
get_raster_inference_with_embedding_from_dict_mocked
|
| 52 |
+
):
|
| 53 |
+
"""
|
| 54 |
+
model_instance = SegmentAnythingONNX()
|
| 55 |
+
img, matrix = download_extent(DEFAULT_TMS, pt0[0], pt0[1], pt1[0], pt1[1], zoom)
|
| 56 |
+
transform = get_affine_transform_from_gdal(matrix)
|
| 57 |
+
mask, n_predictions = get_raster_inference(img, prompt, models_instance, model_name)
|
| 58 |
+
get_vectorized_raster_as_geojson(mask, matrix)
|
| 59 |
+
"""
|
| 60 |
+
aff = 1, 2, 3, 4, 5, 6
|
| 61 |
+
segment_anything_onnx_mocked.return_value = "SegmentAnythingONNX_instance"
|
| 62 |
+
download_extent_mocked.return_value = np.zeros((10, 10)), aff
|
| 63 |
+
get_raster_inference_with_embedding_from_dict_mocked.return_value = np.ones((10, 10)), 1
|
| 64 |
+
get_vectorized_raster_as_geojson_mocked.return_value = {"geojson": "{}", "n_shapes_geojson": 2}
|
| 65 |
+
output = samexporter_predict(
|
| 66 |
+
bbox=[[1, 2], [3, 4]], prompt=[{}], zoom=10, model_name="fastsam", source_name="localtest"
|
| 67 |
+
)
|
| 68 |
+
assert output == {"n_predictions": 1, "geojson": "{}", "n_shapes_geojson": 2}
|