[refactor] apply suggestion from sonarlint
Browse files- src/app.py +2 -2
- src/io/geo_helpers.py +4 -4
- src/io/lambda_helpers.py +1 -1
- src/prediction_api/sam_onnx.py +0 -1
- tests/io/test_tms2geotiff.py +6 -6
- tests/prediction_api/test_sam_onnx.py +2 -7
src/app.py
CHANGED
@@ -29,9 +29,9 @@ def lambda_handler(event: dict, context: LambdaContext):
|
|
29 |
app_logger.info(f"event version: {event['version']}.")
|
30 |
|
31 |
try:
|
32 |
-
app_logger.info(
|
33 |
request_input = get_parsed_request_body(event)
|
34 |
-
app_logger.info(
|
35 |
body_request = get_parsed_bbox_points(request_input)
|
36 |
|
37 |
try:
|
|
|
29 |
app_logger.info(f"event version: {event['version']}.")
|
30 |
|
31 |
try:
|
32 |
+
app_logger.info("try get_parsed_event...")
|
33 |
request_input = get_parsed_request_body(event)
|
34 |
+
app_logger.info("event parsed: ok")
|
35 |
body_request = get_parsed_bbox_points(request_input)
|
36 |
|
37 |
try:
|
src/io/geo_helpers.py
CHANGED
@@ -61,7 +61,7 @@ def get_vectorized_raster_as_geojson(mask: np.ndarray, matrix: Tuple[float]) ->
|
|
61 |
transform = get_affine_transform_from_gdal(matrix)
|
62 |
app_logger.info(f"transform to consume with rasterio.shapes: {type(transform)}, {transform}.")
|
63 |
|
64 |
-
# mask
|
65 |
shapes_generator = ({
|
66 |
'properties': {'raster_val': v}, 'geometry': s}
|
67 |
for i, (s, v)
|
@@ -69,13 +69,13 @@ def get_vectorized_raster_as_geojson(mask: np.ndarray, matrix: Tuple[float]) ->
|
|
69 |
# use mask=None to avoid using source
|
70 |
in enumerate(shapes(mask, mask=None, transform=transform))
|
71 |
)
|
72 |
-
app_logger.info(
|
73 |
shapes_list = list(shapes_generator)
|
74 |
app_logger.info(f"created {len(shapes_list)} polygons.")
|
75 |
gpd_polygonized_raster = GeoDataFrame.from_features(shapes_list, crs="EPSG:3857")
|
76 |
-
app_logger.info(
|
77 |
geojson = gpd_polygonized_raster.to_json(to_wgs84=True)
|
78 |
-
app_logger.info(
|
79 |
return {
|
80 |
"geojson": geojson,
|
81 |
"n_shapes_geojson": len(shapes_list)
|
|
|
61 |
transform = get_affine_transform_from_gdal(matrix)
|
62 |
app_logger.info(f"transform to consume with rasterio.shapes: {type(transform)}, {transform}.")
|
63 |
|
64 |
+
# old value for mask => band != 0
|
65 |
shapes_generator = ({
|
66 |
'properties': {'raster_val': v}, 'geometry': s}
|
67 |
for i, (s, v)
|
|
|
69 |
# use mask=None to avoid using source
|
70 |
in enumerate(shapes(mask, mask=None, transform=transform))
|
71 |
)
|
72 |
+
app_logger.info("created shapes_generator, transform it to a polygon list...")
|
73 |
shapes_list = list(shapes_generator)
|
74 |
app_logger.info(f"created {len(shapes_list)} polygons.")
|
75 |
gpd_polygonized_raster = GeoDataFrame.from_features(shapes_list, crs="EPSG:3857")
|
76 |
+
app_logger.info("created a GeoDataFrame, export to geojson...")
|
77 |
geojson = gpd_polygonized_raster.to_json(to_wgs84=True)
|
78 |
+
app_logger.info("created geojson, preparing API response...")
|
79 |
return {
|
80 |
"geojson": geojson,
|
81 |
"n_shapes_geojson": len(shapes_list)
|
src/io/lambda_helpers.py
CHANGED
@@ -80,7 +80,7 @@ def get_parsed_bbox_points(request_input: RawRequestInput) -> Dict:
|
|
80 |
app_logger.debug(f"bbox => {bbox}.")
|
81 |
app_logger.debug(f'request_input-prompt updated => {new_prompt_list}.')
|
82 |
|
83 |
-
app_logger.info(
|
84 |
return {
|
85 |
"bbox": [ne_latlng, sw_latlng],
|
86 |
"prompt": new_prompt_list,
|
|
|
80 |
app_logger.debug(f"bbox => {bbox}.")
|
81 |
app_logger.debug(f'request_input-prompt updated => {new_prompt_list}.')
|
82 |
|
83 |
+
app_logger.info("unpacking elaborated request...")
|
84 |
return {
|
85 |
"bbox": [ne_latlng, sw_latlng],
|
86 |
"prompt": new_prompt_list,
|
src/prediction_api/sam_onnx.py
CHANGED
@@ -175,7 +175,6 @@ class SegmentAnythingONNX:
|
|
175 |
app_logger.debug(f"mask_shape transform_masks:{mask.shape}, dtype:{mask.dtype}.")
|
176 |
except Exception as e_mask_shape_transform_masks:
|
177 |
app_logger.error(f"e_mask_shape_transform_masks:{e_mask_shape_transform_masks}.")
|
178 |
-
# raise e_mask_shape_transform_masks
|
179 |
mask = cv2.warpAffine(
|
180 |
mask,
|
181 |
transform_matrix[:2],
|
|
|
175 |
app_logger.debug(f"mask_shape transform_masks:{mask.shape}, dtype:{mask.dtype}.")
|
176 |
except Exception as e_mask_shape_transform_masks:
|
177 |
app_logger.error(f"e_mask_shape_transform_masks:{e_mask_shape_transform_masks}.")
|
|
|
178 |
mask = cv2.warpAffine(
|
179 |
mask,
|
180 |
transform_matrix[:2],
|
tests/io/test_tms2geotiff.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import unittest
|
2 |
|
3 |
import numpy as np
|
@@ -28,7 +29,7 @@ class TestTms2geotiff(unittest.TestCase):
|
|
28 |
)
|
29 |
server_thread = Thread(target=server.serve_forever, name="http_server")
|
30 |
server_thread.start()
|
31 |
-
|
32 |
|
33 |
try:
|
34 |
yield
|
@@ -49,6 +50,7 @@ class TestTms2geotiff(unittest.TestCase):
|
|
49 |
np_img = np.array(img)
|
50 |
output_hash = hash_calculate(np_img)
|
51 |
assert output_hash == b'LJNhEuMMp2nRclFJfF6oM3iMVbnZnWDmZqWzrs3T4Hs='
|
|
|
52 |
|
53 |
def test_download_extent_io_error1(self):
|
54 |
|
@@ -91,9 +93,7 @@ if __name__ == '__main__':
|
|
91 |
from tests import TEST_ROOT_FOLDER
|
92 |
|
93 |
main_listen_port = 8000
|
94 |
-
|
95 |
with TestTms2geotiff.http_server("127.0.0.1", main_listen_port, directory=TEST_ROOT_FOLDER):
|
96 |
-
|
97 |
-
|
98 |
-
# time.sleep(10)
|
99 |
-
print("Http server stopped.")
|
|
|
1 |
+
import logging
|
2 |
import unittest
|
3 |
|
4 |
import numpy as np
|
|
|
29 |
)
|
30 |
server_thread = Thread(target=server.serve_forever, name="http_server")
|
31 |
server_thread.start()
|
32 |
+
logging.info(f"listen:: host {host}, port {port}.")
|
33 |
|
34 |
try:
|
35 |
yield
|
|
|
50 |
np_img = np.array(img)
|
51 |
output_hash = hash_calculate(np_img)
|
52 |
assert output_hash == b'LJNhEuMMp2nRclFJfF6oM3iMVbnZnWDmZqWzrs3T4Hs='
|
53 |
+
assert matrix == (1628916.673269823, 76.42413122132838, 0, 4584983.258896411, 0, -76.45836079647981)
|
54 |
|
55 |
def test_download_extent_io_error1(self):
|
56 |
|
|
|
93 |
from tests import TEST_ROOT_FOLDER
|
94 |
|
95 |
main_listen_port = 8000
|
96 |
+
logging.info(f"http_basedir_serve: {TEST_ROOT_FOLDER}.")
|
97 |
with TestTms2geotiff.http_server("127.0.0.1", main_listen_port, directory=TEST_ROOT_FOLDER):
|
98 |
+
logging.info("""import time; time.sleep(10)""")
|
99 |
+
logging.info("Http server stopped.")
|
|
|
|
tests/prediction_api/test_sam_onnx.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
import logging
|
2 |
import unittest
|
3 |
-
from unittest.mock import patch
|
4 |
|
5 |
import numpy as np
|
6 |
|
7 |
from src import MODEL_FOLDER
|
8 |
-
from src.prediction_api import sam_onnx
|
9 |
from src.prediction_api.sam_onnx import SegmentAnythingONNX
|
10 |
from src.utilities.constants import MODEL_ENCODER_NAME, MODEL_DECODER_NAME
|
11 |
from src.utilities.utilities import hash_calculate
|
@@ -58,15 +56,12 @@ class TestSegmentAnythingONNX(unittest.TestCase):
|
|
58 |
try:
|
59 |
assert 1 not in perc
|
60 |
except AssertionError:
|
61 |
-
|
|
|
62 |
# try to assert that the % of different pixels are minor than 5%
|
63 |
assert perc[1] < 5
|
64 |
|
65 |
def test_encode_predict_masks_ex1(self):
|
66 |
-
instance_sam_onnx = SegmentAnythingONNX(
|
67 |
-
encoder_model_path=MODEL_FOLDER / MODEL_ENCODER_NAME,
|
68 |
-
decoder_model_path=MODEL_FOLDER / MODEL_DECODER_NAME
|
69 |
-
)
|
70 |
with self.assertRaises(Exception):
|
71 |
try:
|
72 |
np_input = np.zeros((10, 10))
|
|
|
1 |
import logging
|
2 |
import unittest
|
|
|
3 |
|
4 |
import numpy as np
|
5 |
|
6 |
from src import MODEL_FOLDER
|
|
|
7 |
from src.prediction_api.sam_onnx import SegmentAnythingONNX
|
8 |
from src.utilities.constants import MODEL_ENCODER_NAME, MODEL_DECODER_NAME
|
9 |
from src.utilities.utilities import hash_calculate
|
|
|
56 |
try:
|
57 |
assert 1 not in perc
|
58 |
except AssertionError:
|
59 |
+
n_pixels = perc[1]
|
60 |
+
logging.error(f"found {n_pixels:.2%} different pixels between expected masks and output mask.")
|
61 |
# try to assert that the % of different pixels are minor than 5%
|
62 |
assert perc[1] < 5
|
63 |
|
64 |
def test_encode_predict_masks_ex1(self):
|
|
|
|
|
|
|
|
|
65 |
with self.assertRaises(Exception):
|
66 |
try:
|
67 |
np_input = np.zeros((10, 10))
|