From 4e437b860d0d6f1800b0f1631d3d75d1973595b3 Mon Sep 17 00:00:00 2001 From: "askerosted@gmail.com" Date: Mon, 26 Jan 2026 01:01:13 -0600 Subject: [PATCH 1/2] optional timing of extractors --- src/graphnet/data/extractors/extractor.py | 28 +++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/graphnet/data/extractors/extractor.py b/src/graphnet/data/extractors/extractor.py index a61855496..32c23b92f 100644 --- a/src/graphnet/data/extractors/extractor.py +++ b/src/graphnet/data/extractors/extractor.py @@ -23,7 +23,9 @@ class Extractor(ABC, Logger): An extractor is used in conjunction with a specific `FileReader`. """ - def __init__(self, extractor_name: str, exclude: list = [None]): + def __init__( + self, extractor_name: str, exclude: list = [None], timed: bool = False + ) -> None: """Construct Extractor. Args: @@ -32,10 +34,12 @@ def __init__(self, extractor_name: str, exclude: list = [None]): data, and to name tables to which this data is saved. E.g. "mc_truth". exclude: List of keys to exclude from the extracted data. + timed: Whether to time the execution of the __call__ method. """ # Member variable(s) self._extractor_name: str = extractor_name self._exclude = exclude + self._timed = timed # Base class constructor super().__init__(name=__name__, class_name=self.__class__.__name__) @@ -72,4 +76,24 @@ def name(self) -> str: def __init_subclass__(cls) -> None: """Initialize subclass and apply the exclude decorator to __call__.""" super().__init_subclass__() - cls.__call__ = cls.exclude(cls.__call__) # type: ignore + if not cls._timed: + cls.__call__ = cls.exclude(cls.__call__) # type: ignore + else: + import time + + cls.__call__ = cls.exclude(cls.__call__) # type: ignore + # wrap with time logging + original_call = cls.__call__ + + def timed_call( + cls: "Extractor", *args: Any + ) -> Union[dict, pd.DataFrame]: + start_time = time.time() + result = original_call(cls, *args) + end_time = time.time() + cls._logger.info( + f"Extractor {cls.name} took {end_time - start_time:.4f} seconds." + ) + return result + + cls.__call__ = timed_call # type: ignore From 9734029eac75c1c34aecfcb613e4e442c1867a12 Mon Sep 17 00:00:00 2001 From: "askerosted@gmail.com" Date: Mon, 26 Jan 2026 02:12:42 -0600 Subject: [PATCH 2/2] time if debugging --- src/graphnet/data/extractors/extractor.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/graphnet/data/extractors/extractor.py b/src/graphnet/data/extractors/extractor.py index 32c23b92f..651536468 100644 --- a/src/graphnet/data/extractors/extractor.py +++ b/src/graphnet/data/extractors/extractor.py @@ -23,9 +23,7 @@ class Extractor(ABC, Logger): An extractor is used in conjunction with a specific `FileReader`. """ - def __init__( - self, extractor_name: str, exclude: list = [None], timed: bool = False - ) -> None: + def __init__(self, extractor_name: str, exclude: list = [None]): """Construct Extractor. Args: @@ -34,12 +32,10 @@ def __init__( data, and to name tables to which this data is saved. E.g. "mc_truth". exclude: List of keys to exclude from the extracted data. - timed: Whether to time the execution of the __call__ method. """ # Member variable(s) self._extractor_name: str = extractor_name self._exclude = exclude - self._timed = timed # Base class constructor super().__init__(name=__name__, class_name=self.__class__.__name__) @@ -76,7 +72,7 @@ def name(self) -> str: def __init_subclass__(cls) -> None: """Initialize subclass and apply the exclude decorator to __call__.""" super().__init_subclass__() - if not cls._timed: + if cls._get_root_logger().getEffectiveLevel() > 10: cls.__call__ = cls.exclude(cls.__call__) # type: ignore else: import time @@ -91,7 +87,7 @@ def timed_call( start_time = time.time() result = original_call(cls, *args) end_time = time.time() - cls._logger.info( + cls._logger.debug( f"Extractor {cls.name} took {end_time - start_time:.4f} seconds." ) return result