[refactor] improve exception log handling
Browse files- events/colico_prompt1.json +5 -0
- src/app.py +3 -3
- src/io/coordinates_pixel_conversion.py +6 -2
- src/io/geo_helpers.py +10 -2
- src/io/tms2geotiff.py +9 -5
- src/prediction_api/predictors.py +1 -2
events/colico_prompt1.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{"type": "point", "data": [870, 500], "label": 1},
|
| 3 |
+
{"type": "point", "data": [820, 600], "label": 0},
|
| 4 |
+
{"type": "rectangle", "data": [757, 470, 876, 616]}
|
| 5 |
+
]
|
src/app.py
CHANGED
|
@@ -43,13 +43,13 @@ def lambda_handler(event: Dict, context: LambdaContext) -> str:
|
|
| 43 |
app_logger.debug(f"output body_response:{body_response}.")
|
| 44 |
response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, body_response)
|
| 45 |
except Exception as ex2:
|
| 46 |
-
app_logger.
|
| 47 |
response = get_response(HTTPStatus.INTERNAL_SERVER_ERROR.value, start_time, context.aws_request_id, {})
|
| 48 |
except ValidationError as va1:
|
| 49 |
-
app_logger.
|
| 50 |
response = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, context.aws_request_id, {})
|
| 51 |
except Exception as ex1:
|
| 52 |
-
app_logger.
|
| 53 |
response = get_response(HTTPStatus.BAD_REQUEST.value, start_time, context.aws_request_id, {})
|
| 54 |
|
| 55 |
app_logger.debug(f"response_dumped:{response}...")
|
|
|
|
| 43 |
app_logger.debug(f"output body_response:{body_response}.")
|
| 44 |
response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, body_response)
|
| 45 |
except Exception as ex2:
|
| 46 |
+
app_logger.exception(f"exception2:{ex2}.", exc_info=True)
|
| 47 |
response = get_response(HTTPStatus.INTERNAL_SERVER_ERROR.value, start_time, context.aws_request_id, {})
|
| 48 |
except ValidationError as va1:
|
| 49 |
+
app_logger.exception(f"ValidationError:{va1}.", exc_info=True)
|
| 50 |
response = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, context.aws_request_id, {})
|
| 51 |
except Exception as ex1:
|
| 52 |
+
app_logger.exception(f"exception1:{ex1}.", exc_info=True)
|
| 53 |
response = get_response(HTTPStatus.BAD_REQUEST.value, start_time, context.aws_request_id, {})
|
| 54 |
|
| 55 |
app_logger.debug(f"response_dumped:{response}...")
|
src/io/coordinates_pixel_conversion.py
CHANGED
|
@@ -23,7 +23,8 @@ def _get_latlng2pixel_projection(latlng: LatLngDict) -> ImagePixelCoordinates:
|
|
| 23 |
|
| 24 |
return {"x": x, "y": y}
|
| 25 |
except Exception as e_get_latlng2pixel_projection:
|
| 26 |
-
app_logger.error(f'
|
|
|
|
| 27 |
raise e_get_latlng2pixel_projection
|
| 28 |
|
| 29 |
|
|
@@ -40,7 +41,10 @@ def _get_point_latlng_to_pixel_coordinates(latlng: LatLngDict, zoom: int | float
|
|
| 40 |
y=floor(world_coordinate["y"] * scale)
|
| 41 |
)
|
| 42 |
except Exception as e_format_latlng_to_pixel_coordinates:
|
| 43 |
-
app_logger.error(f'
|
|
|
|
|
|
|
|
|
|
| 44 |
raise e_format_latlng_to_pixel_coordinates
|
| 45 |
|
| 46 |
|
|
|
|
| 23 |
|
| 24 |
return {"x": x, "y": y}
|
| 25 |
except Exception as e_get_latlng2pixel_projection:
|
| 26 |
+
app_logger.error(f'args type:{type(latlng)}, {latlng}.')
|
| 27 |
+
app_logger.exception(f'e_get_latlng2pixel_projection:{e_get_latlng2pixel_projection}.', exc_info=True)
|
| 28 |
raise e_get_latlng2pixel_projection
|
| 29 |
|
| 30 |
|
|
|
|
| 41 |
y=floor(world_coordinate["y"] * scale)
|
| 42 |
)
|
| 43 |
except Exception as e_format_latlng_to_pixel_coordinates:
|
| 44 |
+
app_logger.error(f'latlng type:{type(latlng)}, {latlng}.')
|
| 45 |
+
app_logger.error(f'zoom type:{type(zoom)}, {zoom}.')
|
| 46 |
+
app_logger.exception(f'e_format_latlng_to_pixel_coordinates:{e_format_latlng_to_pixel_coordinates}.',
|
| 47 |
+
exc_info=True)
|
| 48 |
raise e_format_latlng_to_pixel_coordinates
|
| 49 |
|
| 50 |
|
src/io/geo_helpers.py
CHANGED
|
@@ -26,7 +26,9 @@ def load_affine_transformation_from_matrix(matrix_source_coefficients: list_floa
|
|
| 26 |
center = tuple.__new__(Affine, [a, b, c, d, e, f, 0.0, 0.0, 1.0])
|
| 27 |
return center * Affine.translation(-0.5, -0.5)
|
| 28 |
except Exception as e:
|
| 29 |
-
app_logger.
|
|
|
|
|
|
|
| 30 |
raise e
|
| 31 |
|
| 32 |
|
|
@@ -79,5 +81,11 @@ def get_vectorized_raster_as_geojson(mask: np_ndarray, transform: tuple_float) -
|
|
| 79 |
"n_shapes_geojson": len(shapes_list)
|
| 80 |
}
|
| 81 |
except Exception as e_shape_band:
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
raise e_shape_band
|
|
|
|
| 26 |
center = tuple.__new__(Affine, [a, b, c, d, e, f, 0.0, 0.0, 1.0])
|
| 27 |
return center * Affine.translation(-0.5, -0.5)
|
| 28 |
except Exception as e:
|
| 29 |
+
app_logger.exception(f"exception:{e}, check updates on https://github.com/rasterio/affine",
|
| 30 |
+
extra=e,
|
| 31 |
+
stack_info=True, exc_info=True)
|
| 32 |
raise e
|
| 33 |
|
| 34 |
|
|
|
|
| 81 |
"n_shapes_geojson": len(shapes_list)
|
| 82 |
}
|
| 83 |
except Exception as e_shape_band:
|
| 84 |
+
try:
|
| 85 |
+
app_logger.error(f"mask type:{type(mask)}.")
|
| 86 |
+
app_logger.error(f"transform type:{type(transform)}, {transform}.")
|
| 87 |
+
app_logger.error(f"mask shape:{mask.shape}, dtype:{mask.dtype}.")
|
| 88 |
+
except Exception as e_shape_dtype:
|
| 89 |
+
app_logger.exception(f"mask shape or dtype not found:{e_shape_dtype}.", exc_info=True)
|
| 90 |
+
app_logger.exception(f"e_shape_band:{e_shape_band}.", exc_info=True)
|
| 91 |
raise e_shape_band
|
src/io/tms2geotiff.py
CHANGED
|
@@ -125,7 +125,11 @@ def crop_raster(w: float, s: float, e: float, n: float, raster: ndarray, raster_
|
|
| 125 |
app_logger.info(f"cropped image::{cropped_image_ndarray.shape}.")
|
| 126 |
return cropped_image_ndarray, cropped_transform
|
| 127 |
except Exception as e_crop_raster:
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
app_logger.exception(f"e_crop_raster:{e_crop_raster}.", exc_info=True)
|
| 130 |
raise e_crop_raster
|
| 131 |
|
|
@@ -160,9 +164,9 @@ def get_transform_raster(raster: ndarray, raster_bbox: tuple_float) -> tuple_nda
|
|
| 160 |
transform = from_origin(x[0] - res_x / 2, y[-1] + res_y / 2, res_x, res_y)
|
| 161 |
return np_rgb, transform
|
| 162 |
except Exception as e_get_transform_raster:
|
| 163 |
-
app_logger.
|
| 164 |
-
app_logger.
|
| 165 |
-
app_logger.exception(f"e_get_transform_raster:{e_get_transform_raster}.")
|
| 166 |
raise e_get_transform_raster
|
| 167 |
|
| 168 |
|
|
@@ -172,6 +176,6 @@ def reshape_as_image(arr):
|
|
| 172 |
|
| 173 |
return swapaxes(swapaxes(arr, 0, 2), 0, 1)
|
| 174 |
except Exception as e_reshape_as_image:
|
| 175 |
-
app_logger.
|
| 176 |
app_logger.exception(f"e_reshape_as_image:{e_reshape_as_image}.", exc_info=True)
|
| 177 |
raise e_reshape_as_image
|
|
|
|
| 125 |
app_logger.info(f"cropped image::{cropped_image_ndarray.shape}.")
|
| 126 |
return cropped_image_ndarray, cropped_transform
|
| 127 |
except Exception as e_crop_raster:
|
| 128 |
+
try:
|
| 129 |
+
app_logger.error(f"raster type:{type(raster)}.")
|
| 130 |
+
app_logger.error(f"raster shape:{raster.shape}, dtype:{raster.dtype}.")
|
| 131 |
+
except Exception as e_shape_dtype:
|
| 132 |
+
app_logger.exception(f"raster shape or dtype not found:{e_shape_dtype}.", exc_info=True)
|
| 133 |
app_logger.exception(f"e_crop_raster:{e_crop_raster}.", exc_info=True)
|
| 134 |
raise e_crop_raster
|
| 135 |
|
|
|
|
| 164 |
transform = from_origin(x[0] - res_x / 2, y[-1] + res_y / 2, res_x, res_y)
|
| 165 |
return np_rgb, transform
|
| 166 |
except Exception as e_get_transform_raster:
|
| 167 |
+
app_logger.error(f"arguments raster: {type(raster)}, {raster}.")
|
| 168 |
+
app_logger.error(f"arguments raster_bbox: {type(raster_bbox)}, {raster_bbox}.")
|
| 169 |
+
app_logger.exception(f"e_get_transform_raster:{e_get_transform_raster}.", exc_info=True)
|
| 170 |
raise e_get_transform_raster
|
| 171 |
|
| 172 |
|
|
|
|
| 176 |
|
| 177 |
return swapaxes(swapaxes(arr, 0, 2), 0, 1)
|
| 178 |
except Exception as e_reshape_as_image:
|
| 179 |
+
app_logger.error(f"arguments: {type(arr)}, {arr}.")
|
| 180 |
app_logger.exception(f"e_reshape_as_image:{e_reshape_as_image}.", exc_info=True)
|
| 181 |
raise e_reshape_as_image
|
src/prediction_api/predictors.py
CHANGED
|
@@ -46,9 +46,8 @@ def samexporter_predict(
|
|
| 46 |
app_logger.debug(f"using a {model_name} instance model...")
|
| 47 |
models_instance = models_dict[model_name]["instance"]
|
| 48 |
|
| 49 |
-
app_logger.info(f'tile_source: {url_tile}!')
|
| 50 |
pt0, pt1 = bbox
|
| 51 |
-
app_logger.info(f"downloading geo-referenced raster with bbox {bbox}, zoom {zoom}.")
|
| 52 |
img, transform = download_extent(w=pt1[1], s=pt1[0], e=pt0[1], n=pt0[0], zoom=zoom, source=url_tile)
|
| 53 |
app_logger.info(
|
| 54 |
f"img type {type(img)} with shape/size:{img.size}, transform type: {type(transform)}, transform:{transform}.")
|
|
|
|
| 46 |
app_logger.debug(f"using a {model_name} instance model...")
|
| 47 |
models_instance = models_dict[model_name]["instance"]
|
| 48 |
|
|
|
|
| 49 |
pt0, pt1 = bbox
|
| 50 |
+
app_logger.info(f"tile_source: {url_tile}: downloading geo-referenced raster with bbox {bbox}, zoom {zoom}.")
|
| 51 |
img, transform = download_extent(w=pt1[1], s=pt1[0], e=pt0[1], n=pt0[0], zoom=zoom, source=url_tile)
|
| 52 |
app_logger.info(
|
| 53 |
f"img type {type(img)} with shape/size:{img.size}, transform type: {type(transform)}, transform:{transform}.")
|