alessandro trinca tornidor commited on
Commit
49ff57f
·
1 Parent(s): 61ea8aa

[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
- expected_output_geojson = shapely.from_geojson(input_output["output"]["geojson"])
81
- assert shapely.equals_exact(output_geojson, expected_output_geojson, tolerance=0.000006)
 
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
- @patch.object(predictors, "SegmentAnythingONNX")
12
- def test_get_raster_inference(segment_anything_onnx_mocked):
13
- name_fn = "samexporter_predict"
 
14
 
15
- with open(TEST_EVENTS_FOLDER / f"{name_fn}.json") as tst_json:
16
- inputs_outputs = json.load(tst_json)
17
- for k, input_output in inputs_outputs.items():
18
- model_mocked = segment_anything_onnx_mocked()
19
 
20
- img = np.load(TEST_EVENTS_FOLDER / f"{name_fn}" / k / "img.npy")
21
- inference_out = np.load(TEST_EVENTS_FOLDER / f"{name_fn}" / k / "inference_out.npy")
22
- mask = np.load(TEST_EVENTS_FOLDER / f"{name_fn}" / k / "mask.npy")
23
- prompt = input_output["input"]["prompt"]
24
- model_name = input_output["input"]["model_name"]
25
 
26
- model_mocked.embed.return_value = np.array(img)
27
- model_mocked.embed.side_effect = None
28
- model_mocked.predict_masks.return_value = inference_out
29
- model_mocked.predict_masks.side_effect = None
30
- print(f"k:{k}.")
31
- output_mask, len_inference_out = get_raster_inference(
32
- img=img,
33
- prompt=prompt,
34
- models_instance=model_mocked,
35
- model_name=model_name
36
- )
37
- assert np.array_equal(output_mask, mask)
38
- assert len_inference_out == input_output["output"]["n_predictions"]
39
 
40
-
41
- @patch.object(predictors, "get_raster_inference")
42
- @patch.object(predictors, "SegmentAnythingONNX")
43
- @patch.object(predictors, "download_extent")
44
- @patch.object(predictors, "get_vectorized_raster_as_geojson")
45
- def test_samexporter_predict(
46
- get_vectorized_raster_as_geojson_mocked,
47
- download_extent_mocked,
48
- segment_anything_onnx_mocked,
49
- get_raster_inference_mocked
50
- ):
51
- """
52
- model_instance = SegmentAnythingONNX()
53
- img, matrix = download_extent(DEFAULT_TMS, pt0[0], pt0[1], pt1[0], pt1[1], zoom)
54
- transform = get_affine_transform_from_gdal(matrix)
55
- mask, n_predictions = get_raster_inference(img, prompt, models_instance, model_name)
56
- get_vectorized_raster_as_geojson(mask, matrix)
57
- """
58
- aff = 1, 2, 3, 4, 5, 6
59
- segment_anything_onnx_mocked.return_value = "SegmentAnythingONNX_instance"
60
- download_extent_mocked.return_value = np.zeros((10, 10)), aff
61
- get_raster_inference_mocked.return_value = np.ones((10, 10)), 1
62
- get_vectorized_raster_as_geojson_mocked.return_value = {"geojson": "{}", "n_shapes_geojson": 2}
63
- output = samexporter_predict(bbox=[[1, 2], [3, 4]], prompt=[{}], zoom=10, model_name="fastsam")
64
- assert output == {"n_predictions": 1, "geojson": "{}", "n_shapes_geojson": 2}
 
 
 
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}