API Reference
This section provides detailed API documentation for Quick Metric, covering core functions, method definitions, output formats, and pipeline integration.
Module Overview
The main quick_metric module provides the complete public API for the framework. For detailed documentation of individual components, see the specific pages:
- Core Functions -
generate_metrics()and core processing functions - Method Definitions -
@metric_methoddecorator and method management - Output Formats - Result formatting and transformation utilities
- Filter - Data filtering and selection functions
- Apply Methods - Method application and execution utilities
- Pipeline Integration - Pipeline processing and workflow functions
- Exceptions - Custom exception classes
API Documentation
Note
The documentation below and on following pages is automatically generated from the module docstrings and provides comprehensive coverage of all public functions, classes, and methods
Quick Metric framework for creating metrics from pandas DataFrames.
A framework for applying filters and methods to pandas DataFrames using YAML configurations. Register custom metric methods with decorators and configure data filtering through declarative specifications.
Functions:
| Name | Description |
|---|---|
generate_metrics : Apply metric configurations to pandas DataFrames |
|
metric_method : Decorator to register custom metric functions |
|
get_method : Retrieve a registered metric method by name |
|
list_method_names : List all registered metric method names |
|
get_registered_methods : Get dictionary of all registered methods |
|
clear_methods : Clear all registered methods from registry |
|
Examples:
Basic usage:
import pandas as pd
from quick_metric import metric_method, generate_metrics
@metric_method
def count_records(data):
return len(data)
@metric_method
def mean_value(data, column='value'):
return data[column].mean()
data = pd.DataFrame({'category': ['A', 'B', 'A'], 'value': [10, 20, 30]})
config = {
'category_a_metrics': {
'method': ['count_records', 'mean_value'],
'filter': {'category': 'A'}
}
}
results = generate_metrics(data, config)
print(results['category_a_metrics']['count_records']) # 2
print(results['category_a_metrics']['mean_value']) # 20.0
generate_metrics(data, config, metrics_methods=None, output_format='nested')
Generate metrics from data using configuration (main entry point).
This is the primary entry point for the quick_metric framework. It provides a simple interface for generating metrics from pandas DataFrames using either YAML configuration files or dictionary configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The DataFrame to process and generate metrics from. |
required |
config
|
Path or Dict
|
Either a Path object pointing to a YAML configuration file or a dictionary containing metric instructions. If a Path, the YAML file should contain a 'metric_instructions' key with the configuration. |
required |
metrics_methods
|
Dict
|
Dictionary of available methods. If None, uses the default registered methods from METRICS_METHODS. |
None
|
output_format
|
str or OutputFormat
|
Format for the output. Options: - "nested": Current dict of dicts format {'metric': {'method': result}} - "dataframe": Pandas DataFrame with columns [metric, method, value, value_type] - "records": List of dicts [{'metric': '...', 'method': '...', 'value': ...}] |
"nested"
|
Returns:
| Type | Description |
|---|---|
Union[dict, DataFrame, list[dict]]
|
Results in the specified format. The exact type depends on output_format: - dict: When output_format="nested" (default) - pd.DataFrame: When output_format="dataframe" - list[dict]: When output_format="records" |
Examples:
Using a dictionary configuration (default nested format):
import pandas as pd
from quick_metric import generate_metrics, metric_method
@metric_method
def count_records(data):
return len(data)
data = pd.DataFrame({'category': ['A', 'B', 'A'], 'value': [1, 2, 3]})
config = {
'category_a_count': {
'method': ['count_records'],
'filter': {'category': 'A'}
}
}
results = generate_metrics(data, config)
# Returns: {'category_a_count': {'count_records': 2}}
Using DataFrame output format:
df_results = generate_metrics(data, config, output_format="dataframe")
# Returns: DataFrame with columns [metric, method, value, value_type]
Using records output format:
records = generate_metrics(data, config, output_format="records")
# Returns: [{'metric': 'category_a_count', 'method': 'count_records', 'value': 2}]
Using a YAML file:
from pathlib import Path
config_path = Path('my_metrics.yaml')
results = generate_metrics(data, config_path)
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the config path does not exist. |
MetricSpecificationError
|
If a YAML file doesn't contain 'metric_instructions' key or is invalid. If config parameter or output_format is not a valid type. |
Source code in quick_metric/_core.py
271 272 273 274 275 276 277 278 279 280 281 282 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 | |
clear_methods()
get_method(name)
get_registered_methods()
list_method_names()
metric_method(func_or_name=None)
Decorator to register a user function as a metric method, or query registered methods.
Can be used in three ways: 1. As a decorator: @metric_method 2. To get all methods: metric_method() 3. To get a specific method: metric_method('method_name')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func_or_name
|
Callable or str
|
User function to register when used as decorator, or method name to retrieve. |
None
|
Returns:
| Type | Description |
|---|---|
Callable or dict
|
When used as decorator: returns the original function unchanged. When called without args: returns dict of all registered methods. When called with method name: returns the specific method. |
Examples:
As a decorator:
To get all methods:
To get a specific method: