|
4 | 4 | from loguru import logger |
5 | 5 |
|
6 | 6 | from kdp.layers_factory import PreprocessorLayerFactory |
| 7 | +from kdp.dynamic_pipeline import DynamicPreprocessingPipeline |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class ProcessingStep: |
@@ -87,44 +88,77 @@ def transform(self, input_data: tf.Tensor) -> tf.Tensor: |
87 | 88 |
|
88 | 89 |
|
89 | 90 | class FeaturePreprocessor: |
90 | | - def __init__(self, name: str) -> None: |
91 | | - """Initialize a feature preprocessor. |
| 91 | + def __init__(self, name: str, use_dynamic: bool = False) -> None: |
| 92 | + """ |
| 93 | + Initializes a feature preprocessor. |
92 | 94 |
|
93 | 95 | Args: |
94 | 96 | name (str): The name of the feature preprocessor. |
| 97 | + use_dynamic (bool): Whether to use the dynamic preprocessing pipeline. |
95 | 98 | """ |
96 | 99 | self.name = name |
97 | | - self.pipeline = Pipeline(name=name) |
| 100 | + self.use_dynamic = use_dynamic |
| 101 | + if not self.use_dynamic: |
| 102 | + self.pipeline = Pipeline(name=name) |
| 103 | + else: |
| 104 | + self.layers = [] # for dynamic pipeline |
98 | 105 |
|
99 | 106 | def add_processing_step( |
100 | 107 | self, layer_creator: Callable[..., tf.keras.layers.Layer] = None, **layer_kwargs |
101 | 108 | ) -> None: |
102 | | - """Add a processing step to the feature preprocessor. |
| 109 | + """ |
| 110 | + Add a preprocessing layer to the feature preprocessor pipeline. |
| 111 | + If using the standard pipeline, a ProcessingStep is added. |
| 112 | + Otherwise, the layer is added to a list for dynamic handling. |
103 | 113 |
|
104 | 114 | Args: |
105 | 115 | layer_creator (Callable[..., tf.keras.layers.Layer]): A callable that creates a layer. |
106 | 116 | If not provided, the default layer creator is used. |
107 | 117 | **layer_kwargs: Additional keyword arguments for the layer creator. |
108 | 118 | """ |
109 | 119 | layer_creator = layer_creator or PreprocessorLayerFactory.create_layer |
110 | | - step = ProcessingStep(layer_creator=layer_creator, **layer_kwargs) |
111 | | - self.pipeline.add_step(step=step) |
| 120 | + if self.use_dynamic: |
| 121 | + layer = layer_creator(**layer_kwargs) |
| 122 | + logger.info(f"Adding {layer.name} to dynamic preprocessing pipeline") |
| 123 | + self.layers.append(layer) |
| 124 | + else: |
| 125 | + step = ProcessingStep(layer_creator=layer_creator, **layer_kwargs) |
| 126 | + self.pipeline.add_step(step=step) |
112 | 127 |
|
113 | 128 | def chain(self, input_layer) -> tf.keras.layers.Layer: |
114 | | - """Chain the preprocessor's pipeline steps starting from the input layer. |
| 129 | + """ |
| 130 | + Chains the processing steps starting from the given input_layer. |
115 | 131 |
|
116 | | - Args: |
117 | | - input_layer: The input layer to start the chain from. |
| 132 | + For a static pipeline, this delegates to the internal Pipeline's chain() method. |
| 133 | + For the dynamic pipeline, it constructs the dynamic pipeline on the fly. |
118 | 134 | """ |
119 | | - return self.pipeline.chain(input_layer) |
| 135 | + if not self.use_dynamic: |
| 136 | + return self.pipeline.chain(input_layer) |
| 137 | + else: |
| 138 | + dynamic_pipeline = DynamicPreprocessingPipeline(self.layers) |
| 139 | + # In the dynamic case, we use a dict for the input. |
| 140 | + output_dict = dynamic_pipeline.initialize_and_transform( |
| 141 | + {"input": input_layer} |
| 142 | + ) |
| 143 | + # Return the transformed data at key "input" (or adjust as needed). |
| 144 | + return output_dict.get("input", input_layer) |
120 | 145 |
|
121 | 146 | def transform(self, input_data: tf.Tensor) -> tf.Tensor: |
122 | | - """Apply the feature preprocessor to the input data. |
| 147 | + """ |
| 148 | + Process the input data through the pipeline. |
| 149 | + For the dynamic pipeline, wrap input in a dictionary and extract final output. |
123 | 150 |
|
124 | 151 | Args: |
125 | 152 | input_data: The input data to process. |
126 | 153 |
|
127 | 154 | Returns: |
128 | 155 | tf.Tensor: The processed data. |
129 | 156 | """ |
130 | | - return self.pipeline.transform(input_data) |
| 157 | + if not self.use_dynamic: |
| 158 | + return self.pipeline.transform(input_data) |
| 159 | + else: |
| 160 | + dynamic_pipeline = DynamicPreprocessingPipeline(self.layers) |
| 161 | + output_dict = dynamic_pipeline.initialize_and_transform( |
| 162 | + {"input": input_data} |
| 163 | + ) |
| 164 | + return output_dict.get("input", input_data) |
0 commit comments