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..f10c8313 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,25 @@ 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 + + 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: - 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) diff --git a/tests/unit/test_cluster_detection.py b/tests/unit/test_cluster_detection.py index 3063ab7a..6f6da0f7 100644 --- a/tests/unit/test_cluster_detection.py +++ b/tests/unit/test_cluster_detection.py @@ -7,6 +7,7 @@ import sklearn.datasets from modules.cluster_estimation import cluster_estimation +from modules.common.modules.logger import logger from modules import detection_in_world @@ -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