|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import os |
2 | 4 | import random |
3 | 5 | from abc import ABC, abstractmethod |
4 | | -from typing import Any, Dict, Generator, List, Optional, Tuple, Union |
| 6 | +from typing import Any, Dict, Generator, List, Optional, Tuple, Type, Union |
5 | 7 |
|
6 | 8 | import lightning as pl |
7 | 9 | import networkx as nx |
|
20 | 22 | from chebai.preprocessing import reader as dr |
21 | 23 |
|
22 | 24 |
|
23 | | -class XYBaseDataModule(LightningDataModule): |
| 25 | +class _InitMeta(type): |
| 26 | + """ |
| 27 | + Metaclass that ensures a specific method (`_call_data_processing_methods`) |
| 28 | + is called after an instance (meaning the most derived class instance) is fully initialized. |
| 29 | +
|
| 30 | + Purpose: |
| 31 | + - Automatically calls `_call_data_processing_methods(**kwargs)` on instances |
| 32 | + of classes that use this metaclass, if the method is defined. |
| 33 | + - Ensures additional processing logic is executed immediately after object instantiation. |
| 34 | + - Useful in cases where post-initialization processing is required across multiple subclasses. |
| 35 | + """ |
| 36 | + |
| 37 | + def __call__( |
| 38 | + cls: Type[XYBaseDataModule], *args: Any, **kwargs: Any |
| 39 | + ) -> XYBaseDataModule: |
| 40 | + """ |
| 41 | + Overrides the instance creation process to call `_after_init` after the most derived class instance |
| 42 | + is initialized. |
| 43 | +
|
| 44 | + Args: |
| 45 | + cls (Type[XYBaseDataModule]): The class being instantiated. |
| 46 | + *args (Any): Positional arguments for the class constructor. |
| 47 | + **kwargs (Any): Keyword arguments for the class constructor. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + XYBaseDataModule: The initialized instance of the class. |
| 51 | + """ |
| 52 | + instance = super().__call__(*args, **kwargs) # Create the instance |
| 53 | + if hasattr(instance, "_after_init"): |
| 54 | + instance._after_init(**kwargs) # Call the method if defined |
| 55 | + return instance |
| 56 | + |
| 57 | + |
| 58 | +class XYBaseDataModule(LightningDataModule, metaclass=_InitMeta): |
24 | 59 | """ |
25 | 60 | Base class for data modules. |
26 | 61 |
|
@@ -122,9 +157,22 @@ def __init__( |
122 | 157 | self._prepare_data_flag = 1 |
123 | 158 | self._setup_data_flag = 1 |
124 | 159 |
|
125 | | - # Skips data setup in the constructor; methods will be called later according to the CLI workflow. |
| 160 | + def _after_init(self, **kwargs): |
| 161 | + """ |
| 162 | + This method is called after the instantiation of most derived class is completed. |
| 163 | + Refer the `_InitMeta` metaclass for more details. |
| 164 | + """ |
| 165 | + self._call_data_processing_methods(**kwargs) |
| 166 | + |
| 167 | + def _call_data_processing_methods(self, **kwargs) -> None: |
| 168 | + """ |
| 169 | + Calls data processing methods unless explicitly skipped. |
| 170 | +
|
| 171 | + - Skips execution if `_skip_data_methods_on_init` is `True` (e.g., for unit tests). |
| 172 | + - Otherwise, calls `prepare_data()` and `setup()` for data preparation. |
| 173 | +
|
| 174 | + """ |
126 | 175 | if kwargs.get("_skip_data_methods_on_init", False): |
127 | | - # This change enables to skip these methods during initialization for unit-testing GitHub CI/CD |
128 | 176 | return |
129 | 177 |
|
130 | 178 | self.prepare_data() |
|
0 commit comments