From efbcbd377a6f15bf2d4c5b49525f482ed12f2eee Mon Sep 17 00:00:00 2001 From: Aayush1104 Date: Thu, 21 Nov 2024 16:25:52 -0500 Subject: [PATCH 1/3] Added cluster estimation output logging --- modules/cluster_estimation/cluster_estimation.py | 14 +++++++++++++- .../cluster_estimation_worker.py | 15 ++++++++++++++- modules/object_in_world.py | 12 ++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/modules/cluster_estimation/cluster_estimation.py b/modules/cluster_estimation/cluster_estimation.py index 31675f47..ae7ff0b3 100644 --- a/modules/cluster_estimation/cluster_estimation.py +++ b/modules/cluster_estimation/cluster_estimation.py @@ -11,6 +11,7 @@ from .. import object_in_world from .. import detection_in_world +from ..common.modules.logger import logger class ClusterEstimation: @@ -70,7 +71,11 @@ class ClusterEstimation: @classmethod def create( - cls, min_activation_threshold: int, min_new_points_to_run: int, random_state: int + cls, + min_activation_threshold: int, + min_new_points_to_run: int, + random_state: int, + local_logger: logger.Logger, ) -> "tuple[bool, ClusterEstimation | None]": """ Data requirement conditions for estimation model to run. @@ -88,6 +93,7 @@ def create( min_activation_threshold, min_new_points_to_run, random_state, + local_logger, ) def __init__( @@ -96,6 +102,7 @@ def __init__( min_activation_threshold: int, min_new_points_to_run: int, random_state: int, + local_logger: logger.Logger, ) -> None: """ Private constructor, use create() method. @@ -121,6 +128,7 @@ def __init__( self.__min_activation_threshold = min_activation_threshold self.__min_new_points_to_run = min_new_points_to_run self.__has_ran_once = False + self.__logger = local_logger def run( self, detections: "list[detection_in_world.DetectionInWorld]", run_override: bool @@ -160,6 +168,7 @@ def run( # Check convergence if not self.__vgmm.converged_: + self.__logger.warning("Model failed to converge") return False, None # Get predictions from cluster model @@ -201,7 +210,10 @@ def run( if result: detections_in_world.append(landing_pad) + else: + self.__logger.warning("Failed to create ObjectInWorld object") + self.__logger.info(detections_in_world) return True, detections_in_world def __decide_to_run(self, run_override: bool) -> bool: diff --git a/modules/cluster_estimation/cluster_estimation_worker.py b/modules/cluster_estimation/cluster_estimation_worker.py index b4243b34..eb485787 100644 --- a/modules/cluster_estimation/cluster_estimation_worker.py +++ b/modules/cluster_estimation/cluster_estimation_worker.py @@ -2,9 +2,13 @@ Gets detections in world space and outputs estimations of objects. """ +import os +import pathlib + from utilities.workers import queue_proxy_wrapper from utilities.workers import worker_controller from . import cluster_estimation +from ..common.modules.logger import logger def cluster_estimation_worker( @@ -38,13 +42,22 @@ def cluster_estimation_worker( worker_controller: worker_controller.WorkerController How the main process communicates to this worker process. """ + worker_name = pathlib.Path(__file__).stem + process_id = os.getpid() + result, local_logger = logger.Logger.create(f"{worker_name}_{process_id}", True) + if not result: + print("ERROR: Worker failed to create logger") + return + + assert local_logger is not None + result, estimator = cluster_estimation.ClusterEstimation.create( min_activation_threshold, min_new_points_to_run, random_state, ) if not result: - print("ERROR: Worker failed to create class object") + local_logger.error("Worker failed to create class object", True) return # Get Pylance to stop complaining diff --git a/modules/object_in_world.py b/modules/object_in_world.py index 1ce9f65e..83922253 100644 --- a/modules/object_in_world.py +++ b/modules/object_in_world.py @@ -38,3 +38,15 @@ def __init__( self.location_x = location_x self.location_y = location_y self.spherical_variance = spherical_variance + + def __str__(self) -> str: + """ + To string. + """ + return f"{self.__class__}, location_x: {self.location_x}, location_y: {self.location_y}, spherical_variance: {self.spherical_variance}" + + def __repr__(self) -> str: + """ + For collections (e.g. list). + """ + return str(self) From 571335a242fd45d97e72ffdfda4327e356f03bd9 Mon Sep 17 00:00:00 2001 From: Aayush1104 Date: Thu, 21 Nov 2024 17:00:54 -0500 Subject: [PATCH 2/3] Passed logger instance to two calls --- modules/cluster_estimation/cluster_estimation_worker.py | 3 +++ tests/unit/test_cluster_detection.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/modules/cluster_estimation/cluster_estimation_worker.py b/modules/cluster_estimation/cluster_estimation_worker.py index eb485787..f10c8313 100644 --- a/modules/cluster_estimation/cluster_estimation_worker.py +++ b/modules/cluster_estimation/cluster_estimation_worker.py @@ -51,10 +51,13 @@ def cluster_estimation_worker( assert local_logger is not None + local_logger.info("Logger initialized") + result, estimator = cluster_estimation.ClusterEstimation.create( min_activation_threshold, min_new_points_to_run, random_state, + local_logger, ) if not result: local_logger.error("Worker failed to create class object", True) diff --git a/tests/unit/test_cluster_detection.py b/tests/unit/test_cluster_detection.py index 3063ab7a..c33481d9 100644 --- a/tests/unit/test_cluster_detection.py +++ b/tests/unit/test_cluster_detection.py @@ -8,6 +8,7 @@ from modules.cluster_estimation import cluster_estimation from modules import detection_in_world +from modules.common.modules.logger import logger MIN_TOTAL_POINTS_THRESHOLD = 100 @@ -26,10 +27,15 @@ def cluster_model() -> cluster_estimation.ClusterEstimation: # type: ignore """ Cluster estimation object. """ + result, test_logger = logger.Logger.create("test_logger", False) + assert result + assert test_logger is not None + result, model = cluster_estimation.ClusterEstimation.create( MIN_TOTAL_POINTS_THRESHOLD, MIN_NEW_POINTS_TO_RUN, RNG_SEED, + test_logger, ) assert result assert model is not None From fa8ef92b29033d55898bcc1daeab33578f0ea44f Mon Sep 17 00:00:00 2001 From: Aayush1104 Date: Thu, 21 Nov 2024 20:17:35 -0500 Subject: [PATCH 3/3] Rearranged lines --- tests/unit/test_cluster_detection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_cluster_detection.py b/tests/unit/test_cluster_detection.py index c33481d9..6f6da0f7 100644 --- a/tests/unit/test_cluster_detection.py +++ b/tests/unit/test_cluster_detection.py @@ -7,8 +7,8 @@ import sklearn.datasets from modules.cluster_estimation import cluster_estimation -from modules import detection_in_world from modules.common.modules.logger import logger +from modules import detection_in_world MIN_TOTAL_POINTS_THRESHOLD = 100