[test] refactor code, fix some tests
Browse files- src/io/geo_helpers.py +4 -1
- src/prediction_api/predictors.py +28 -6
- tests/events/get_raster_inference/oceania/inference_out.npy +0 -0
- tests/events/{get_raster_inference.json β samexporter_predict.json} +2 -2
- tests/events/{get_raster_inference β samexporter_predict}/europe/img.npy +0 -0
- tests/events/{get_raster_inference β samexporter_predict}/europe/inference_out.npy +0 -0
- tests/events/{get_raster_inference β samexporter_predict}/europe/mask.npy +0 -0
- tests/events/{get_raster_inference β samexporter_predict}/north_america/img.npy +0 -0
- tests/events/{get_raster_inference β samexporter_predict}/north_america/inference_out.npy +0 -0
- tests/events/{get_raster_inference β samexporter_predict}/north_america/mask.npy +0 -0
- tests/events/{get_raster_inference β samexporter_predict}/oceania/img.npy +1 -1
- tests/events/samexporter_predict/oceania/inference_out.npy +3 -0
- tests/events/{get_raster_inference β samexporter_predict}/oceania/mask.npy +1 -1
- tests/events/{get_raster_inference β samexporter_predict}/south_america/img.npy +0 -0
- tests/events/{get_raster_inference β samexporter_predict}/south_america/inference_out.npy +0 -0
- tests/events/{get_raster_inference β samexporter_predict}/south_america/mask.npy +0 -0
- tests/io/test_coordinates_pixel_conversion.py +47 -4
- tests/io/test_geo_helpers.py +23 -0
- tests/prediction_api/test_predictors.py +1 -1
- tests/utilities_testing.py +0 -26
src/io/geo_helpers.py
CHANGED
@@ -28,11 +28,14 @@ def get_affine_transform_from_gdal(matrix):
|
|
28 |
return Affine.from_gdal(*matrix)
|
29 |
|
30 |
|
31 |
-
def get_vectorized_raster_as_geojson(mask,
|
32 |
try:
|
33 |
from rasterio.features import shapes
|
34 |
from geopandas import GeoDataFrame
|
35 |
|
|
|
|
|
|
|
36 |
# mask = band != 0
|
37 |
shapes_generator = ({
|
38 |
'properties': {'raster_val': v}, 'geometry': s}
|
|
|
28 |
return Affine.from_gdal(*matrix)
|
29 |
|
30 |
|
31 |
+
def get_vectorized_raster_as_geojson(mask, matrix):
|
32 |
try:
|
33 |
from rasterio.features import shapes
|
34 |
from geopandas import GeoDataFrame
|
35 |
|
36 |
+
transform = get_affine_transform_from_gdal(matrix)
|
37 |
+
app_logger.info(f"transform to consume with rasterio.shapes: {type(transform)}, {transform}.")
|
38 |
+
|
39 |
# mask = band != 0
|
40 |
shapes_generator = ({
|
41 |
'properties': {'raster_val': v}, 'geometry': s}
|
src/prediction_api/predictors.py
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
# Press the green button in the gutter to run the script.
|
|
|
|
|
|
|
2 |
import numpy as np
|
3 |
|
4 |
from src import app_logger, MODEL_FOLDER
|
@@ -6,11 +9,13 @@ from src.io.geo_helpers import get_vectorized_raster_as_geojson, get_affine_tran
|
|
6 |
from src.io.tms2geotiff import download_extent
|
7 |
from src.prediction_api.sam_onnx import SegmentAnythingONNX
|
8 |
from src.utilities.constants import MODEL_ENCODER_NAME, MODEL_DECODER_NAME, DEFAULT_TMS
|
9 |
-
|
|
|
10 |
|
11 |
models_dict = {"fastsam": {"instance": None}}
|
12 |
|
13 |
|
|
|
14 |
def samexporter_predict(bbox, prompt: list[dict], zoom: float, model_name: str = "fastsam") -> dict:
|
15 |
try:
|
16 |
if models_dict[model_name]["instance"] is None:
|
@@ -29,14 +34,17 @@ def samexporter_predict(bbox, prompt: list[dict], zoom: float, model_name: str =
|
|
29 |
img, matrix = download_extent(DEFAULT_TMS, pt0[0], pt0[1], pt1[0], pt1[1], zoom)
|
30 |
app_logger.info(f"img type {type(img)} with shape/size:{img.size}, matrix:{matrix}.")
|
31 |
|
|
|
|
|
|
|
32 |
transform = get_affine_transform_from_gdal(matrix)
|
33 |
-
app_logger.
|
34 |
|
35 |
mask, n_predictions = get_raster_inference(img, prompt, models_instance, model_name)
|
36 |
app_logger.info(f"created {n_predictions} masks, preparing conversion to geojson...")
|
37 |
return {
|
38 |
"n_predictions": n_predictions,
|
39 |
-
**get_vectorized_raster_as_geojson(mask,
|
40 |
}
|
41 |
except ImportError as e_import_module:
|
42 |
app_logger.error(f"Error trying import module:{e_import_module}.")
|
@@ -50,17 +58,31 @@ def get_raster_inference(img, prompt, models_instance, model_name):
|
|
50 |
app_logger.debug(f"onnxruntime input shape (NUMPY) {np_img.shape}.")
|
51 |
except Exception as e_shape:
|
52 |
app_logger.error(f"e_shape:{e_shape}.")
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
app_logger.info(f"instantiated model {model_name}, ENCODER {MODEL_ENCODER_NAME}, "
|
54 |
f"DECODER {MODEL_DECODER_NAME} from {MODEL_FOLDER}: Creating embedding...")
|
55 |
embedding = models_instance.encode(np_img)
|
56 |
app_logger.debug(f"embedding created, running predict_masks with prompt {prompt}...")
|
57 |
inference_out = models_instance.predict_masks(embedding, prompt)
|
58 |
-
|
59 |
-
app_logger.info(f"Created {
|
60 |
f"shape:{inference_out.shape}, dtype:{inference_out.dtype}.")
|
61 |
mask = np.zeros((inference_out.shape[2], inference_out.shape[3]), dtype=np.uint8)
|
62 |
for n, m in enumerate(inference_out[0, :, :, :]):
|
63 |
app_logger.debug(f"{n}th of prediction_masks shape {inference_out.shape}"
|
64 |
f" => mask shape:{mask.shape}, {mask.dtype}.")
|
65 |
mask[m > 0.0] = 255
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# Press the green button in the gutter to run the script.
|
2 |
+
import json
|
3 |
+
import tempfile
|
4 |
+
|
5 |
import numpy as np
|
6 |
|
7 |
from src import app_logger, MODEL_FOLDER
|
|
|
9 |
from src.io.tms2geotiff import download_extent
|
10 |
from src.prediction_api.sam_onnx import SegmentAnythingONNX
|
11 |
from src.utilities.constants import MODEL_ENCODER_NAME, MODEL_DECODER_NAME, DEFAULT_TMS
|
12 |
+
from src.utilities.serialize import serialize
|
13 |
+
from src.utilities.utilities import LogArgumentsDecorator
|
14 |
|
15 |
models_dict = {"fastsam": {"instance": None}}
|
16 |
|
17 |
|
18 |
+
@LogArgumentsDecorator.log_arguments_decorator
|
19 |
def samexporter_predict(bbox, prompt: list[dict], zoom: float, model_name: str = "fastsam") -> dict:
|
20 |
try:
|
21 |
if models_dict[model_name]["instance"] is None:
|
|
|
34 |
img, matrix = download_extent(DEFAULT_TMS, pt0[0], pt0[1], pt1[0], pt1[1], zoom)
|
35 |
app_logger.info(f"img type {type(img)} with shape/size:{img.size}, matrix:{matrix}.")
|
36 |
|
37 |
+
with tempfile.NamedTemporaryFile(mode='w', prefix=f"matrix_", delete=False) as temp_f1:
|
38 |
+
json.dump({"matrix": serialize(matrix)}, temp_f1)
|
39 |
+
|
40 |
transform = get_affine_transform_from_gdal(matrix)
|
41 |
+
app_logger.debug(f"transform to consume with rasterio.shapes: {type(transform)}, {transform}.")
|
42 |
|
43 |
mask, n_predictions = get_raster_inference(img, prompt, models_instance, model_name)
|
44 |
app_logger.info(f"created {n_predictions} masks, preparing conversion to geojson...")
|
45 |
return {
|
46 |
"n_predictions": n_predictions,
|
47 |
+
**get_vectorized_raster_as_geojson(mask, matrix)
|
48 |
}
|
49 |
except ImportError as e_import_module:
|
50 |
app_logger.error(f"Error trying import module:{e_import_module}.")
|
|
|
58 |
app_logger.debug(f"onnxruntime input shape (NUMPY) {np_img.shape}.")
|
59 |
except Exception as e_shape:
|
60 |
app_logger.error(f"e_shape:{e_shape}.")
|
61 |
+
try:
|
62 |
+
with tempfile.NamedTemporaryFile(mode='w', prefix=f"get_raster_inference__img_", delete=False) as temp_f0:
|
63 |
+
np.save(str(temp_f0.file.name), np_img)
|
64 |
+
except Exception as e_save:
|
65 |
+
app_logger.error(f"e_save:{e_save}.")
|
66 |
+
raise e_save
|
67 |
app_logger.info(f"instantiated model {model_name}, ENCODER {MODEL_ENCODER_NAME}, "
|
68 |
f"DECODER {MODEL_DECODER_NAME} from {MODEL_FOLDER}: Creating embedding...")
|
69 |
embedding = models_instance.encode(np_img)
|
70 |
app_logger.debug(f"embedding created, running predict_masks with prompt {prompt}...")
|
71 |
inference_out = models_instance.predict_masks(embedding, prompt)
|
72 |
+
len_inference_out = len(inference_out[0, :, :, :])
|
73 |
+
app_logger.info(f"Created {len_inference_out} prediction_masks,"
|
74 |
f"shape:{inference_out.shape}, dtype:{inference_out.dtype}.")
|
75 |
mask = np.zeros((inference_out.shape[2], inference_out.shape[3]), dtype=np.uint8)
|
76 |
for n, m in enumerate(inference_out[0, :, :, :]):
|
77 |
app_logger.debug(f"{n}th of prediction_masks shape {inference_out.shape}"
|
78 |
f" => mask shape:{mask.shape}, {mask.dtype}.")
|
79 |
mask[m > 0.0] = 255
|
80 |
+
try:
|
81 |
+
with tempfile.NamedTemporaryFile(mode='w', prefix=f"get_raster_inference__mask_", delete=False) as temp_f1:
|
82 |
+
np.save(temp_f1.file.name, mask)
|
83 |
+
with tempfile.NamedTemporaryFile(mode='w', prefix=f"get_raster_inference__inference_out_", delete=False) as temp_f2:
|
84 |
+
np.save(temp_f2.file.name, inference_out)
|
85 |
+
except Exception as e_save1:
|
86 |
+
app_logger.error(f"e_save1:{e_save1}.")
|
87 |
+
raise e_save1
|
88 |
+
return mask, len_inference_out
|
tests/events/get_raster_inference/oceania/inference_out.npy
DELETED
File without changes
|
tests/events/{get_raster_inference.json β samexporter_predict.json}
RENAMED
@@ -8,8 +8,8 @@
|
|
8 |
"output": {"n_predictions": 1, "geojson": "{\"type\": \"FeatureCollection\", \"features\": [{\"id\": \"0\", \"type\": \"Feature\", \"properties\": {\"raster_val\": 255.0}, \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [[[-114.19189453125001, 47.99727386804473], [-114.10400390625001, 47.99727386804473], [-114.10400390625001, 47.98992166741417], [-114.09301757812501, 47.98992166741417], [-114.09301757812501, 47.96050238891508], [-114.10400390625001, 47.96050238891508], [-114.10400390625001, 47.93842692948105], [-114.09301757812501, 47.93842692948105], [-114.09301757812501, 47.87951293397049], [-114.08203125000001, 47.87951293397049], [-114.08203125000001, 47.77625204393234], [-114.071044921875, 47.77625204393234], [-114.071044921875, 47.70976154266638], [-114.08203125000001, 47.70976154266638], [-114.08203125000001, 47.70236846657371], [-114.09301757812501, 47.70236846657371], [-114.09301757812501, 47.69497434186281], [-114.10400390625001, 47.69497434186281], [-114.10400390625001, 47.68757916850812], [-114.13696289062501, 47.68757916850812], [-114.13696289062501, 47.69497434186281], [-114.158935546875, 47.69497434186281], [-114.158935546875, 47.70236846657371], [-114.18090820312501, 47.70236846657371], [-114.18090820312501, 47.70976154266638], [-114.19189453125001, 47.70976154266638], [-114.19189453125001, 47.724544549099654], [-114.20288085937501, 47.724544549099654], [-114.20288085937501, 47.73932336136855], [-114.21386718750001, 47.73932336136855], [-114.21386718750001, 47.74671119475599], [-114.23583984375, 47.74671119475599], [-114.23583984375, 47.75409797968002], [-114.24682617187501, 47.75409797968002], [-114.24682617187501, 47.76148371616668], [-114.2578125, 47.76148371616668], [-114.2578125, 47.78363463526376], [-114.26879882812501, 47.78363463526376], [-114.26879882812501, 47.791016178262595], [-114.27978515625001, 47.791016178262595], [-114.27978515625001, 47.79839667295523], [-114.30175781250001, 47.79839667295523], [-114.30175781250001, 47.82053186746052], [-114.31274414062501, 47.82053186746052], [-114.31274414062501, 47.82790816919328], [-114.30175781250001, 47.82790816919328], [-114.30175781250001, 47.83528342275263], [-114.29077148437501, 47.83528342275263], [-114.29077148437501, 47.842657628165355], [-114.27978515625001, 47.842657628165355], [-114.27978515625001, 47.850030785458266], [-114.26879882812501, 47.850030785458266], [-114.26879882812501, 47.85740289465823], [-114.2578125, 47.85740289465823], [-114.2578125, 47.86477395579223], [-114.24682617187501, 47.86477395579223], [-114.24682617187501, 47.87214396888729], [-114.23583984375, 47.87214396888729], [-114.23583984375, 47.87951293397049], [-114.22485351562503, 47.87951293397049], [-114.22485351562503, 47.89424772020997], [-114.21386718750001, 47.89424772020997], [-114.21386718750001, 47.91634204016117], [-114.20288085937501, 47.91634204016117], [-114.20288085937501, 47.98992166741417], [-114.19189453125001, 47.98992166741417], [-114.19189453125001, 47.99727386804473]]]}}, {\"id\": \"1\", \"type\": \"Feature\", \"properties\": {\"raster_val\": 0.0}, \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [[[-124.46411132812499, 51.62483746174321], [-124.46411132812499, 46.71726868507395], [-113.214111328125, 46.71726868507395], [-113.214111328125, 51.62483746174321], [-124.46411132812499, 51.62483746174321]], [[-114.19189453125001, 47.99727386804473], [-114.19189453125001, 47.98992166741417], [-114.20288085937501, 47.98992166741417], [-114.20288085937501, 47.91634204016117], [-114.21386718750001, 47.91634204016117], [-114.21386718750001, 47.89424772020997], [-114.22485351562503, 47.89424772020997], [-114.22485351562503, 47.87951293397049], [-114.23583984375, 47.87951293397049], [-114.23583984375, 47.87214396888729], [-114.24682617187501, 47.87214396888729], [-114.24682617187501, 47.86477395579223], [-114.2578125, 47.86477395579223], [-114.2578125, 47.85740289465823], [-114.26879882812501, 47.85740289465823], [-114.26879882812501, 47.850030785458266], [-114.27978515625001, 47.850030785458266], [-114.27978515625001, 47.842657628165355], [-114.29077148437501, 47.842657628165355], [-114.29077148437501, 47.83528342275263], [-114.30175781250001, 47.83528342275263], [-114.30175781250001, 47.82790816919328], [-114.31274414062501, 47.82790816919328], [-114.31274414062501, 47.82053186746052], [-114.30175781250001, 47.82053186746052], [-114.30175781250001, 47.79839667295523], [-114.27978515625001, 47.79839667295523], [-114.27978515625001, 47.791016178262595], [-114.26879882812501, 47.791016178262595], [-114.26879882812501, 47.78363463526376], [-114.2578125, 47.78363463526376], [-114.2578125, 47.76148371616668], [-114.24682617187501, 47.76148371616668], [-114.24682617187501, 47.75409797968002], [-114.23583984375, 47.75409797968002], [-114.23583984375, 47.74671119475599], [-114.21386718750001, 47.74671119475599], [-114.21386718750001, 47.73932336136855], [-114.20288085937501, 47.73932336136855], [-114.20288085937501, 47.724544549099654], [-114.19189453125001, 47.724544549099654], [-114.19189453125001, 47.70976154266638], [-114.18090820312501, 47.70976154266638], [-114.18090820312501, 47.70236846657371], [-114.158935546875, 47.70236846657371], [-114.158935546875, 47.69497434186281], [-114.13696289062501, 47.69497434186281], [-114.13696289062501, 47.68757916850812], [-114.10400390625001, 47.68757916850812], [-114.10400390625001, 47.69497434186281], [-114.09301757812501, 47.69497434186281], [-114.09301757812501, 47.70236846657371], [-114.08203125000001, 47.70236846657371], [-114.08203125000001, 47.70976154266638], [-114.071044921875, 47.70976154266638], [-114.071044921875, 47.77625204393234], [-114.08203125000001, 47.77625204393234], [-114.08203125000001, 47.87951293397049], [-114.09301757812501, 47.87951293397049], [-114.09301757812501, 47.93842692948105], [-114.10400390625001, 47.93842692948105], [-114.10400390625001, 47.96050238891508], [-114.09301757812501, 47.96050238891508], [-114.09301757812501, 47.98992166741417], [-114.10400390625001, 47.98992166741417], [-114.10400390625001, 47.99727386804473], [-114.19189453125001, 47.99727386804473]]]}}]}", "n_shapes_geojson": 2}
|
9 |
},
|
10 |
"oceania": {
|
11 |
-
"input": {"matrix": [7269467.138033403, 9783.93962050256, 0, -166326.9735485418, 0, -9783.939620502566], "bbox": [[-1.4939713066293112, 155.30273437500003], [-52.32191088594772, 65.30273437500001]], "prompt": [{"type": "point", "data": [
|
12 |
-
"output": {"n_predictions": 1, "geojson": "{\"type\": \"FeatureCollection\", \"features\": [{\"id\": \"0\", \"type\": \"Feature\", \"properties\": {\"raster_val\": 255.0}, \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [[[148.
|
13 |
},
|
14 |
"south_america": {
|
15 |
"input": {"matrix": [-7922544.351904369, 305.74811314070394, 0, -5432228.234830927, 0, -305.7481131407035], "bbox": [[-43.78498531802787, -68.35692680430485], [-45.12587626673896, -71.16942680430483]], "prompt": [{"type": "point", "data": [917, 492], "label": 0}], "zoom": 9, "model_name": "fastsam"},
|
|
|
8 |
"output": {"n_predictions": 1, "geojson": "{\"type\": \"FeatureCollection\", \"features\": [{\"id\": \"0\", \"type\": \"Feature\", \"properties\": {\"raster_val\": 255.0}, \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [[[-114.19189453125001, 47.99727386804473], [-114.10400390625001, 47.99727386804473], [-114.10400390625001, 47.98992166741417], [-114.09301757812501, 47.98992166741417], [-114.09301757812501, 47.96050238891508], [-114.10400390625001, 47.96050238891508], [-114.10400390625001, 47.93842692948105], [-114.09301757812501, 47.93842692948105], [-114.09301757812501, 47.87951293397049], [-114.08203125000001, 47.87951293397049], [-114.08203125000001, 47.77625204393234], [-114.071044921875, 47.77625204393234], [-114.071044921875, 47.70976154266638], [-114.08203125000001, 47.70976154266638], [-114.08203125000001, 47.70236846657371], [-114.09301757812501, 47.70236846657371], [-114.09301757812501, 47.69497434186281], [-114.10400390625001, 47.69497434186281], [-114.10400390625001, 47.68757916850812], [-114.13696289062501, 47.68757916850812], [-114.13696289062501, 47.69497434186281], [-114.158935546875, 47.69497434186281], [-114.158935546875, 47.70236846657371], [-114.18090820312501, 47.70236846657371], [-114.18090820312501, 47.70976154266638], [-114.19189453125001, 47.70976154266638], [-114.19189453125001, 47.724544549099654], [-114.20288085937501, 47.724544549099654], [-114.20288085937501, 47.73932336136855], [-114.21386718750001, 47.73932336136855], [-114.21386718750001, 47.74671119475599], [-114.23583984375, 47.74671119475599], [-114.23583984375, 47.75409797968002], [-114.24682617187501, 47.75409797968002], [-114.24682617187501, 47.76148371616668], [-114.2578125, 47.76148371616668], [-114.2578125, 47.78363463526376], [-114.26879882812501, 47.78363463526376], [-114.26879882812501, 47.791016178262595], [-114.27978515625001, 47.791016178262595], [-114.27978515625001, 47.79839667295523], [-114.30175781250001, 47.79839667295523], [-114.30175781250001, 47.82053186746052], [-114.31274414062501, 47.82053186746052], [-114.31274414062501, 47.82790816919328], [-114.30175781250001, 47.82790816919328], [-114.30175781250001, 47.83528342275263], [-114.29077148437501, 47.83528342275263], [-114.29077148437501, 47.842657628165355], [-114.27978515625001, 47.842657628165355], [-114.27978515625001, 47.850030785458266], [-114.26879882812501, 47.850030785458266], [-114.26879882812501, 47.85740289465823], [-114.2578125, 47.85740289465823], [-114.2578125, 47.86477395579223], [-114.24682617187501, 47.86477395579223], [-114.24682617187501, 47.87214396888729], [-114.23583984375, 47.87214396888729], [-114.23583984375, 47.87951293397049], [-114.22485351562503, 47.87951293397049], [-114.22485351562503, 47.89424772020997], [-114.21386718750001, 47.89424772020997], [-114.21386718750001, 47.91634204016117], [-114.20288085937501, 47.91634204016117], [-114.20288085937501, 47.98992166741417], [-114.19189453125001, 47.98992166741417], [-114.19189453125001, 47.99727386804473]]]}}, {\"id\": \"1\", \"type\": \"Feature\", \"properties\": {\"raster_val\": 0.0}, \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [[[-124.46411132812499, 51.62483746174321], [-124.46411132812499, 46.71726868507395], [-113.214111328125, 46.71726868507395], [-113.214111328125, 51.62483746174321], [-124.46411132812499, 51.62483746174321]], [[-114.19189453125001, 47.99727386804473], [-114.19189453125001, 47.98992166741417], [-114.20288085937501, 47.98992166741417], [-114.20288085937501, 47.91634204016117], [-114.21386718750001, 47.91634204016117], [-114.21386718750001, 47.89424772020997], [-114.22485351562503, 47.89424772020997], [-114.22485351562503, 47.87951293397049], [-114.23583984375, 47.87951293397049], [-114.23583984375, 47.87214396888729], [-114.24682617187501, 47.87214396888729], [-114.24682617187501, 47.86477395579223], [-114.2578125, 47.86477395579223], [-114.2578125, 47.85740289465823], [-114.26879882812501, 47.85740289465823], [-114.26879882812501, 47.850030785458266], [-114.27978515625001, 47.850030785458266], [-114.27978515625001, 47.842657628165355], [-114.29077148437501, 47.842657628165355], [-114.29077148437501, 47.83528342275263], [-114.30175781250001, 47.83528342275263], [-114.30175781250001, 47.82790816919328], [-114.31274414062501, 47.82790816919328], [-114.31274414062501, 47.82053186746052], [-114.30175781250001, 47.82053186746052], [-114.30175781250001, 47.79839667295523], [-114.27978515625001, 47.79839667295523], [-114.27978515625001, 47.791016178262595], [-114.26879882812501, 47.791016178262595], [-114.26879882812501, 47.78363463526376], [-114.2578125, 47.78363463526376], [-114.2578125, 47.76148371616668], [-114.24682617187501, 47.76148371616668], [-114.24682617187501, 47.75409797968002], [-114.23583984375, 47.75409797968002], [-114.23583984375, 47.74671119475599], [-114.21386718750001, 47.74671119475599], [-114.21386718750001, 47.73932336136855], [-114.20288085937501, 47.73932336136855], [-114.20288085937501, 47.724544549099654], [-114.19189453125001, 47.724544549099654], [-114.19189453125001, 47.70976154266638], [-114.18090820312501, 47.70976154266638], [-114.18090820312501, 47.70236846657371], [-114.158935546875, 47.70236846657371], [-114.158935546875, 47.69497434186281], [-114.13696289062501, 47.69497434186281], [-114.13696289062501, 47.68757916850812], [-114.10400390625001, 47.68757916850812], [-114.10400390625001, 47.69497434186281], [-114.09301757812501, 47.69497434186281], [-114.09301757812501, 47.70236846657371], [-114.08203125000001, 47.70236846657371], [-114.08203125000001, 47.70976154266638], [-114.071044921875, 47.70976154266638], [-114.071044921875, 47.77625204393234], [-114.08203125000001, 47.77625204393234], [-114.08203125000001, 47.87951293397049], [-114.09301757812501, 47.87951293397049], [-114.09301757812501, 47.93842692948105], [-114.10400390625001, 47.93842692948105], [-114.10400390625001, 47.96050238891508], [-114.09301757812501, 47.96050238891508], [-114.09301757812501, 47.98992166741417], [-114.10400390625001, 47.98992166741417], [-114.10400390625001, 47.99727386804473], [-114.19189453125001, 47.99727386804473]]]}}]}", "n_shapes_geojson": 2}
|
9 |
},
|
10 |
"oceania": {
|
11 |
+
"input": {"matrix": [7269467.138033403, 9783.93962050256, 0, -166326.9735485418, 0, -9783.939620502566], "bbox": [[-1.4939713066293112, 155.30273437500003], [-52.32191088594772, 65.30273437500001]], "prompt": [{"type": "point", "data": [932, 514], "label": 0}], "zoom": 4, "model_name": "fastsam"},
|
12 |
+
"output": {"n_predictions": 1, "geojson": "{\"type\": \"FeatureCollection\", \"features\": [{\"id\": \"0\", \"type\": \"Feature\", \"properties\": {\"raster_val\": 255.0}, \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [[[148.18359375, -39.97712009843964], [148.447265625, -39.97712009843964], [148.447265625, -40.38002840251183], [148.53515625, -40.38002840251183], [148.53515625, -40.51379915504414], [148.623046875, -40.51379915504414], [148.623046875, -40.847060356071225], [148.7109375, -40.847060356071225], [148.7109375, -41.04621681452063], [148.623046875, -41.04621681452063], [148.623046875, -42.682435398386225], [148.53515625, -42.682435398386225], [148.53515625, -43.004647127794435], [148.447265625, -43.004647127794435], [148.447265625, -43.197167282501276], [148.359375, -43.197167282501276], [148.359375, -43.32517767999296], [148.27148437500003, -43.32517767999296], [148.27148437500003, -43.38908193911751], [148.18359375, -43.38908193911751], [148.18359375, -43.516688535029076], [148.095703125, -43.516688535029076], [148.095703125, -43.58039085560785], [147.919921875, -43.58039085560785], [147.919921875, -43.6440258476995], [147.83203125, -43.6440258476995], [147.83203125, -43.70759350405295], [147.74414062500003, -43.70759350405295], [147.74414062500003, -43.77109381775651], [147.568359375, -43.77109381775651], [147.568359375, -43.834526782236836], [147.48046875000003, -43.834526782236836], [147.48046875000003, -43.89789239125797], [147.3046875, -43.89789239125797], [147.3046875, -43.96119063892026], [147.12890625, -43.96119063892026], [147.12890625, -44.02442151965934], [146.6015625, -44.02442151965934], [146.6015625, -43.96119063892026], [146.25, -43.96119063892026], [146.25, -43.89789239125797], [146.07421875000003, -43.89789239125797], [146.07421875000003, -43.834526782236836], [145.986328125, -43.834526782236836], [145.986328125, -43.77109381775651], [145.8984375, -43.77109381775651], [145.8984375, -43.70759350405295], [145.810546875, -43.70759350405295], [145.810546875, -43.6440258476995], [145.72265625, -43.6440258476995], [145.72265625, -43.58039085560785], [145.634765625, -43.58039085560785], [145.634765625, -43.45291889355466], [145.54687500000003, -43.45291889355466], [145.54687500000003, -43.38908193911751], [145.458984375, -43.38908193911751], [145.458984375, -43.261206124799784], [145.37109375000003, -43.261206124799784], [145.37109375000003, -43.197167282501276], [145.283203125, -43.197167282501276], [145.283203125, -43.068887774169625], [145.1953125, -43.068887774169625], [145.1953125, -42.94033923363183], [145.107421875, -42.94033923363183], [145.107421875, -42.8115217450979], [145.01953125, -42.8115217450979], [145.01953125, -42.61779143282346], [144.931640625, -42.61779143282346], [144.931640625, -42.22851735620852], [144.84375000000003, -42.22851735620852], [144.84375000000003, -41.83682786072714], [144.755859375, -41.83682786072714], [144.755859375, -41.70572851523752], [144.66796875000003, -41.70572851523752], [144.66796875000003, -41.64007838467893], [144.580078125, -41.64007838467893], [144.580078125, -41.508577297439345], [144.4921875, -41.508577297439345], [144.4921875, -41.17865397233169], [144.404296875, -41.17865397233169], [144.404296875, -40.91351257612758], [144.31640625, -40.91351257612758], [144.31640625, -40.71395582628605], [144.228515625, -40.71395582628605], [144.228515625, -40.64730356252252], [144.31640625, -40.64730356252252], [144.31640625, -40.580584664127635], [144.404296875, -40.580584664127635], [144.404296875, -40.51379915504414], [144.580078125, -40.51379915504414], [144.580078125, -40.4469470596005], [144.66796875000003, -40.4469470596005], [144.66796875000003, -40.51379915504414], [145.01953125, -40.51379915504414], [145.01953125, -40.580584664127635], [145.37109375000003, -40.580584664127635], [145.37109375000003, -40.64730356252252], [145.634765625, -40.64730356252252], [145.634765625, -40.71395582628605], [145.72265625, -40.71395582628605], [145.72265625, -40.78054143186032], [145.8984375, -40.78054143186032], [145.8984375, -40.847060356071225], [146.07421875000003, -40.847060356071225], [146.07421875000003, -40.91351257612758], [146.689453125, -40.91351257612758], [146.689453125, -40.847060356071225], [146.953125, -40.847060356071225], [146.953125, -40.78054143186032], [147.12890625, -40.78054143186032], [147.12890625, -40.71395582628605], [147.3046875, -40.71395582628605], [147.3046875, -40.64730356252252], [147.392578125, -40.64730356252252], [147.392578125, -40.580584664127635], [147.74414062500003, -40.580584664127635], [147.74414062500003, -40.51379915504414], [147.919921875, -40.51379915504414], [147.919921875, -40.4469470596005], [148.0078125, -40.4469470596005], [148.0078125, -40.04443758460857], [148.18359375, -40.04443758460857], [148.18359375, -39.97712009843964]]]}}, {\"id\": \"1\", \"type\": \"Feature\", \"properties\": {\"raster_val\": 0.0}, \"geometry\": {\"type\": \"Polygon\", \"coordinates\": [[[65.30273437500001, -1.4939713066293043], [65.30273437500001, -52.32191088594773], [155.302734375, -52.32191088594773], [155.302734375, -1.4939713066293043], [65.30273437500001, -1.4939713066293043]], [[148.18359375, -39.97712009843964], [148.18359375, -40.04443758460857], [148.0078125, -40.04443758460857], [148.0078125, -40.4469470596005], [147.919921875, -40.4469470596005], [147.919921875, -40.51379915504414], [147.74414062500003, -40.51379915504414], [147.74414062500003, -40.580584664127635], [147.392578125, -40.580584664127635], [147.392578125, -40.64730356252252], [147.3046875, -40.64730356252252], [147.3046875, -40.71395582628605], [147.12890625, -40.71395582628605], [147.12890625, -40.78054143186032], [146.953125, -40.78054143186032], [146.953125, -40.847060356071225], [146.689453125, -40.847060356071225], [146.689453125, -40.91351257612758], [146.07421875000003, -40.91351257612758], [146.07421875000003, -40.847060356071225], [145.8984375, -40.847060356071225], [145.8984375, -40.78054143186032], [145.72265625, -40.78054143186032], [145.72265625, -40.71395582628605], [145.634765625, -40.71395582628605], [145.634765625, -40.64730356252252], [145.37109375000003, -40.64730356252252], [145.37109375000003, -40.580584664127635], [145.01953125, -40.580584664127635], [145.01953125, -40.51379915504414], [144.66796875000003, -40.51379915504414], [144.66796875000003, -40.4469470596005], [144.580078125, -40.4469470596005], [144.580078125, -40.51379915504414], [144.404296875, -40.51379915504414], [144.404296875, -40.580584664127635], [144.31640625, -40.580584664127635], [144.31640625, -40.64730356252252], [144.228515625, -40.64730356252252], [144.228515625, -40.71395582628605], [144.31640625, -40.71395582628605], [144.31640625, -40.91351257612758], [144.404296875, -40.91351257612758], [144.404296875, -41.17865397233169], [144.4921875, -41.17865397233169], [144.4921875, -41.508577297439345], [144.580078125, -41.508577297439345], [144.580078125, -41.64007838467893], [144.66796875000003, -41.64007838467893], [144.66796875000003, -41.70572851523752], [144.755859375, -41.70572851523752], [144.755859375, -41.83682786072714], [144.84375000000003, -41.83682786072714], [144.84375000000003, -42.22851735620852], [144.931640625, -42.22851735620852], [144.931640625, -42.61779143282346], [145.01953125, -42.61779143282346], [145.01953125, -42.8115217450979], [145.107421875, -42.8115217450979], [145.107421875, -42.94033923363183], [145.1953125, -42.94033923363183], [145.1953125, -43.068887774169625], [145.283203125, -43.068887774169625], [145.283203125, -43.197167282501276], [145.37109375000003, -43.197167282501276], [145.37109375000003, -43.261206124799784], [145.458984375, -43.261206124799784], [145.458984375, -43.38908193911751], [145.54687500000003, -43.38908193911751], [145.54687500000003, -43.45291889355466], [145.634765625, -43.45291889355466], [145.634765625, -43.58039085560785], [145.72265625, -43.58039085560785], [145.72265625, -43.6440258476995], [145.810546875, -43.6440258476995], [145.810546875, -43.70759350405295], [145.8984375, -43.70759350405295], [145.8984375, -43.77109381775651], [145.986328125, -43.77109381775651], [145.986328125, -43.834526782236836], [146.07421875000003, -43.834526782236836], [146.07421875000003, -43.89789239125797], [146.25, -43.89789239125797], [146.25, -43.96119063892026], [146.6015625, -43.96119063892026], [146.6015625, -44.02442151965934], [147.12890625, -44.02442151965934], [147.12890625, -43.96119063892026], [147.3046875, -43.96119063892026], [147.3046875, -43.89789239125797], [147.48046875000003, -43.89789239125797], [147.48046875000003, -43.834526782236836], [147.568359375, -43.834526782236836], [147.568359375, -43.77109381775651], [147.74414062500003, -43.77109381775651], [147.74414062500003, -43.70759350405295], [147.83203125, -43.70759350405295], [147.83203125, -43.6440258476995], [147.919921875, -43.6440258476995], [147.919921875, -43.58039085560785], [148.095703125, -43.58039085560785], [148.095703125, -43.516688535029076], [148.18359375, -43.516688535029076], [148.18359375, -43.38908193911751], [148.27148437500003, -43.38908193911751], [148.27148437500003, -43.32517767999296], [148.359375, -43.32517767999296], [148.359375, -43.197167282501276], [148.447265625, -43.197167282501276], [148.447265625, -43.004647127794435], [148.53515625, -43.004647127794435], [148.53515625, -42.682435398386225], [148.623046875, -42.682435398386225], [148.623046875, -41.04621681452063], [148.7109375, -41.04621681452063], [148.7109375, -40.847060356071225], [148.623046875, -40.847060356071225], [148.623046875, -40.51379915504414], [148.53515625, -40.51379915504414], [148.53515625, -40.38002840251183], [148.447265625, -40.38002840251183], [148.447265625, -39.97712009843964], [148.18359375, -39.97712009843964]]]}}]}", "n_shapes_geojson": 2}
|
13 |
},
|
14 |
"south_america": {
|
15 |
"input": {"matrix": [-7922544.351904369, 305.74811314070394, 0, -5432228.234830927, 0, -305.7481131407035], "bbox": [[-43.78498531802787, -68.35692680430485], [-45.12587626673896, -71.16942680430483]], "prompt": [{"type": "point", "data": [917, 492], "label": 0}], "zoom": 9, "model_name": "fastsam"},
|
tests/events/{get_raster_inference β samexporter_predict}/europe/img.npy
RENAMED
File without changes
|
tests/events/{get_raster_inference β samexporter_predict}/europe/inference_out.npy
RENAMED
File without changes
|
tests/events/{get_raster_inference β samexporter_predict}/europe/mask.npy
RENAMED
File without changes
|
tests/events/{get_raster_inference β samexporter_predict}/north_america/img.npy
RENAMED
File without changes
|
tests/events/{get_raster_inference β samexporter_predict}/north_america/inference_out.npy
RENAMED
File without changes
|
tests/events/{get_raster_inference β samexporter_predict}/north_america/mask.npy
RENAMED
File without changes
|
tests/events/{get_raster_inference β samexporter_predict}/oceania/img.npy
RENAMED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 2101376
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cdf88681e56a3ea0731f44f8e2f95c30f2d5128fe8b08f218334893a1a9c9dc6
|
3 |
size 2101376
|
tests/events/samexporter_predict/oceania/inference_out.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cfd4d9c88463dad5a0d93c9aff912934cb7f9111c03671d9942904de4c58cf99
|
3 |
+
size 2801792
|
tests/events/{get_raster_inference β samexporter_predict}/oceania/mask.npy
RENAMED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 700544
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:03d395e62a03fc952746e05c4682d92952d4d66f2825545afd83e7a7f077e5c0
|
3 |
size 700544
|
tests/events/{get_raster_inference β samexporter_predict}/south_america/img.npy
RENAMED
File without changes
|
tests/events/{get_raster_inference β samexporter_predict}/south_america/inference_out.npy
RENAMED
File without changes
|
tests/events/{get_raster_inference β samexporter_predict}/south_america/mask.npy
RENAMED
File without changes
|
tests/io/test_coordinates_pixel_conversion.py
CHANGED
@@ -1,13 +1,56 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
|
4 |
def test_get_latlng2pixel_projection():
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
def test_get_point_latlng_to_pixel_coordinates():
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
|
12 |
def test_get_latlng_to_pixel_coordinates():
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
from aws_lambda_powertools.utilities.parser import parse
|
4 |
+
|
5 |
+
from src.io.coordinates_pixel_conversion import get_latlng2pixel_projection, get_point_latlng_to_pixel_coordinates, \
|
6 |
+
get_latlng_to_pixel_coordinates
|
7 |
+
from src.utilities.type_hints import LatLngDict
|
8 |
+
from tests import TEST_EVENTS_FOLDER
|
9 |
|
10 |
|
11 |
def test_get_latlng2pixel_projection():
|
12 |
+
name_fn = "get_latlng2pixel_projection"
|
13 |
+
|
14 |
+
with open(TEST_EVENTS_FOLDER / f"{name_fn}.json") as tst_json:
|
15 |
+
inputs_outputs = json.load(tst_json)
|
16 |
+
for k, input_output in inputs_outputs.items():
|
17 |
+
print(f"k:{k}")
|
18 |
+
current_input = input_output["input"]
|
19 |
+
latlng_input = parse(event=current_input["latlng"], model=LatLngDict)
|
20 |
+
output = get_latlng2pixel_projection(latlng_input)
|
21 |
+
assert output == input_output["output"]
|
22 |
|
23 |
|
24 |
def test_get_point_latlng_to_pixel_coordinates():
|
25 |
+
name_fn = "get_point_latlng_to_pixel_coordinates"
|
26 |
+
|
27 |
+
with open(TEST_EVENTS_FOLDER / f"{name_fn}.json") as tst_json:
|
28 |
+
inputs_outputs = json.load(tst_json)
|
29 |
+
for k, input_output in inputs_outputs.items():
|
30 |
+
print(f"k:{k}")
|
31 |
+
current_input = input_output["input"]
|
32 |
+
latlng_input = parse(event=current_input["latlng"], model=LatLngDict)
|
33 |
+
output = get_point_latlng_to_pixel_coordinates(latlng=latlng_input, zoom=current_input["zoom"])
|
34 |
+
assert output == input_output["output"]
|
35 |
|
36 |
|
37 |
def test_get_latlng_to_pixel_coordinates():
|
38 |
+
name_fn = "get_latlng_to_pixel_coordinates"
|
39 |
+
|
40 |
+
with open(TEST_EVENTS_FOLDER / f"{name_fn}.json") as tst_json:
|
41 |
+
inputs_outputs = json.load(tst_json)
|
42 |
+
for k, input_output in inputs_outputs.items():
|
43 |
+
print(f"k:{k}")
|
44 |
+
current_input = input_output["input"]
|
45 |
+
zoom = current_input["zoom"]
|
46 |
+
latlng_origin_ne = parse(event=current_input['latlng_origin_ne'], model=LatLngDict)
|
47 |
+
latlng_origin_sw = parse(event=current_input['latlng_origin_sw'], model=LatLngDict)
|
48 |
+
latlng_current_point = parse(event=current_input['latlng_current_point'], model=LatLngDict)
|
49 |
+
output = get_latlng_to_pixel_coordinates(
|
50 |
+
latlng_origin_ne=latlng_origin_ne,
|
51 |
+
latlng_origin_sw=latlng_origin_sw,
|
52 |
+
latlng_current_point=latlng_current_point,
|
53 |
+
zoom=zoom,
|
54 |
+
k=k
|
55 |
+
)
|
56 |
+
assert output == input_output["output"]
|
tests/io/test_geo_helpers.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import numpy as np
|
3 |
+
import shapely
|
4 |
+
|
5 |
+
from tests import TEST_EVENTS_FOLDER
|
6 |
+
|
7 |
+
|
8 |
+
def test_get_vectorized_raster_as_geojson():
|
9 |
+
from src.io.geo_helpers import get_vectorized_raster_as_geojson
|
10 |
+
|
11 |
+
name_fn = "samexporter_predict"
|
12 |
+
|
13 |
+
with open(TEST_EVENTS_FOLDER / f"{name_fn}.json") as tst_json:
|
14 |
+
inputs_outputs = json.load(tst_json)
|
15 |
+
for k, input_output in inputs_outputs.items():
|
16 |
+
print(f"k:{k}.")
|
17 |
+
mask = np.load(TEST_EVENTS_FOLDER / name_fn / k / "mask.npy")
|
18 |
+
|
19 |
+
output = get_vectorized_raster_as_geojson(mask=mask, matrix=input_output["input"]["matrix"])
|
20 |
+
assert output["n_shapes_geojson"] == input_output["output"]["n_shapes_geojson"]
|
21 |
+
output_geojson = shapely.from_geojson(output["geojson"])
|
22 |
+
expected_output_geojson = shapely.from_geojson(input_output["output"]["geojson"])
|
23 |
+
assert shapely.equals_exact(output_geojson, expected_output_geojson, tolerance=0.000006)
|
tests/prediction_api/test_predictors.py
CHANGED
@@ -12,7 +12,7 @@ from tests import TEST_EVENTS_FOLDER
|
|
12 |
def test_get_raster_inference(
|
13 |
segment_anything_onnx_mocked
|
14 |
):
|
15 |
-
name_fn = "
|
16 |
|
17 |
with open(TEST_EVENTS_FOLDER / f"{name_fn}.json") as tst_json:
|
18 |
inputs_outputs = json.load(tst_json)
|
|
|
12 |
def test_get_raster_inference(
|
13 |
segment_anything_onnx_mocked
|
14 |
):
|
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)
|
tests/utilities_testing.py
DELETED
@@ -1,26 +0,0 @@
|
|
1 |
-
import json
|
2 |
-
|
3 |
-
from src.io.coordinates_pixel_conversion import get_latlng2pixel_projection, get_point_latlng_to_pixel_coordinates, \
|
4 |
-
get_latlng_to_pixel_coordinates
|
5 |
-
from src.io.lambda_helpers import get_parsed_bbox_points
|
6 |
-
|
7 |
-
from tests import TEST_EVENTS_FOLDER
|
8 |
-
|
9 |
-
|
10 |
-
names_fn_dict = {
|
11 |
-
"get_latlng2pixel_projection": get_latlng2pixel_projection,
|
12 |
-
"get_point_latlng_to_pixel_coordinates": get_point_latlng_to_pixel_coordinates,
|
13 |
-
"get_latlng_to_pixel_coordinates": get_latlng_to_pixel_coordinates,
|
14 |
-
"get_parsed_bbox_points": get_parsed_bbox_points
|
15 |
-
}
|
16 |
-
|
17 |
-
|
18 |
-
def fn_reading_json_inputs_outputs__test(name_fn):
|
19 |
-
fn = names_fn_dict[name_fn]
|
20 |
-
|
21 |
-
with open(TEST_EVENTS_FOLDER / f"{name_fn}.json") as tst_json:
|
22 |
-
inputs_outputs = json.load(tst_json)
|
23 |
-
for k, input_output in inputs_outputs.items():
|
24 |
-
print(f"k:{k}.")
|
25 |
-
output = fn(**input_output["input"])
|
26 |
-
assert output == input_output["output"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|