Pipeline Integration
Pipeline integration for oops-its-a-pipeline framework.
Pipeline stages that wrap quick_metric functionality for use within oops-its-a-pipeline workflows with flexible input/output mapping and comprehensive error handling.
Classes:
| Name | Description |
|---|---|
GenerateMetricsStage : Pipeline stage class for metrics generation |
|
Functions:
| Name | Description |
|---|---|
create_metrics_stage : Convenience factory function for creating stages |
|
Examples:
Basic pipeline stage creation:
from quick_metric.pipeline import create_metrics_stage
from oops_its_a_pipeline import Pipeline
stage = create_metrics_stage()
pipeline = Pipeline(config).add_stage(stage)
results = pipeline.run("pipeline_run")
Custom input/output mapping:
stage = create_metrics_stage(
data_input="processed_data",
config_input="metric_definitions",
metrics_output="business_metrics"
)
GenerateMetricsStage
Bases: PipelineStage
Pipeline stage for generating metrics using quick_metric framework.
This stage wraps the generate_metrics function to be used within
oops-its-a-pipeline workflows. It takes a DataFrame and configuration
as inputs and produces calculated metrics as output.
The stage handles multiple configuration formats, validates inputs, and provides comprehensive error reporting. It's designed to be thread-safe and integrates seamlessly with the pipeline logging system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_input
|
str
|
Name of the context variable containing the pandas DataFrame. The DataFrame should contain the data to be analyzed. |
"data"
|
config_input
|
str
|
Name of the context variable containing the metrics configuration. Can be a Path to YAML file, dict with config, or PipelineConfig object. |
"config"
|
metrics_methods_input
|
str
|
Name of the context variable containing custom metrics methods. If provided, these methods will be available in addition to globally registered methods. If None, uses only registered methods. |
None
|
metrics_output
|
str
|
Name to assign to the generated metrics results in the context. Results are stored as a nested dictionary structure. |
"metrics"
|
name
|
str
|
Custom name for this stage for logging and identification. If None, uses "generate_metrics". |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
data_input |
str
|
The name of the input data variable |
config_input |
str
|
The name of the configuration variable |
metrics_methods_input |
str or None
|
The name of the custom methods variable |
metrics_output |
str
|
The name of the output metrics variable |
Raises:
| Type | Description |
|---|---|
PipelineStageValidationError
|
If input data is not a pandas DataFrame, if configuration is invalid, or if metrics generation fails for any reason. |
Notes
The stage automatically handles different configuration object types: - If config has a 'config' attribute, extracts it - If config is a Path, passes it through for YAML loading - If config is a dict, uses it directly - Otherwise raises a validation error
Thread Safety
This stage is thread-safe and can be used in concurrent pipeline execution environments. The underlying metrics generation is also thread-safe through the MetricRegistry locking mechanism.
Examples:
Creating a stage with default parameters:
Creating a stage with custom input/output mapping:
stage = GenerateMetricsStage(
data_input="raw_data",
config_input="analysis_config",
metrics_output="business_metrics",
name="business_analysis"
)
Using with custom methods:
Complete pipeline example:
from oops_its_a_pipeline import Pipeline, PipelineConfig
import pandas as pd
class MetricsConfig(PipelineConfig):
data: pd.DataFrame = pd.DataFrame({
'category': ['A', 'B'], 'value': [1, 2]
})
config: dict = {
'test_metric': {'method': ['count'], 'filter': {}}
}
stage = GenerateMetricsStage()
pipeline = Pipeline(MetricsConfig())
pipeline.add_stage(stage)
results = pipeline.run("metrics_run")
print(results['metrics'])
See Also
create_metrics_stage : Convenience factory function quick_metric._core.generate_metrics : Underlying metrics function oops_its_a_pipeline.PipelineStage : Base class
Source code in quick_metric/pipeline.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
__init__(data_input='data', config_input='config', metrics_methods_input=None, metrics_output='metrics', name=None)
Initialize the GenerateMetricsStage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_input
|
str
|
Name of the context variable containing the pandas DataFrame. |
"data"
|
config_input
|
str
|
Name of the context variable containing the metrics configuration (either a Path to YAML file or a dictionary). |
"config"
|
metrics_methods_input
|
str
|
Name of the context variable containing custom metrics methods. If None, uses the default registered methods. |
None
|
metrics_output
|
str
|
Name to assign to the generated metrics results in the context. |
"metrics"
|
name
|
str
|
Custom name for this stage. If None, uses "generate_metrics". |
None
|
Source code in quick_metric/pipeline.py
run(context)
Execute the metrics generation stage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
PipelineContext
|
Runtime context containing input data and configuration. |
required |
Returns:
| Type | Description |
|---|---|
PipelineContext
|
Updated context with metrics results. |
Raises:
| Type | Description |
|---|---|
PipelineStageValidationError
|
If input data is not a pandas DataFrame or if metrics generation fails. |
Source code in quick_metric/pipeline.py
create_metrics_stage(data_input='data', config_input='config', metrics_methods_input=None, metrics_output='metrics', name=None)
Convenience function to create a GenerateMetricsStage.
This function provides a more concise way to create a metrics generation stage for use in pipeline method chaining. It's the recommended way to create metrics stages as it provides a clean, functional interface.
The function acts as a factory, creating and configuring a GenerateMetricsStage instance with the specified parameters. This is particularly useful in method-chaining pipeline construction patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_input
|
str
|
Name of the context variable containing the pandas DataFrame. This should reference a variable in the pipeline context that contains the data to be analyzed. |
"data"
|
config_input
|
str
|
Name of the context variable containing the metrics configuration. Can reference a Path to YAML file, a dictionary with metric definitions, or a PipelineConfig object with a 'config' attribute. |
"config"
|
metrics_methods_input
|
str
|
Name of the context variable containing custom metrics methods. If provided, these methods will be merged with globally registered methods. The variable should contain a dict mapping method names to callable functions. If None, only uses registered methods. |
None
|
metrics_output
|
str
|
Name to assign to the generated metrics results in the context. The results will be stored as a nested dictionary structure where keys are metric names and values are method results. |
"metrics"
|
name
|
str
|
Custom name for this stage for logging and pipeline visualization. If None, the stage will use "generate_metrics" as its name. |
None
|
Returns:
| Type | Description |
|---|---|
GenerateMetricsStage
|
Configured metrics generation stage ready to be added to a pipeline. The stage is fully initialized and can be used immediately. |
Notes
This function is the preferred way to create metrics stages as it: - Provides a clean, functional interface - Works well with method chaining - Reduces boilerplate code - Maintains consistency across projects
The returned stage can be used in both declarative and method-chaining pipeline construction patterns.
Examples:
Basic usage with default parameters:
from quick_metric.pipeline import create_metrics_stage
stage = create_metrics_stage()
pipeline.add_stage(stage)
Custom input/output mapping:
stage = create_metrics_stage(
data_input="processed_data",
config_input="metric_definitions",
metrics_output="business_metrics"
)
Method chaining pipeline construction:
from oops_its_a_pipeline import Pipeline
pipeline = (Pipeline(config)
.add_function_stage(load_data, outputs="data")
.add_function_stage(load_config, outputs="metrics_config")
.add_stage(create_metrics_stage(
config_input="metrics_config",
metrics_output="calculated_metrics"
))
.add_function_stage(save_results, inputs="calculated_metrics"))
With custom methods and naming:
stage = create_metrics_stage(
metrics_methods_input="domain_methods",
name="domain_analysis",
metrics_output="domain_metrics"
)
Multiple metrics stages in one pipeline:
pipeline = (Pipeline(config)
.add_stage(create_metrics_stage(
config_input="basic_config",
metrics_output="basic_metrics",
name="basic_analysis"
))
.add_stage(create_metrics_stage(
config_input="advanced_config",
metrics_output="advanced_metrics",
name="advanced_analysis"
)))
See Also
GenerateMetricsStage : The underlying stage class quick_metric._core.generate_metrics : Core metrics generation function oops_its_a_pipeline.Pipeline : Pipeline construction quick_metric._method_definitions.metric_method : Method registration
Source code in quick_metric/pipeline.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |