Apply Methods
Method application and execution functionality for Quick Metric.
Handles the application of registered metric methods to filtered pandas DataFrames with error handling for missing methods.
Functions:
| Name | Description |
|---|---|
apply_method : Apply a single metric method to data |
|
apply_methods : Apply multiple metric methods to data and collect results |
|
Examples:
Apply a single method:
import pandas as pd
from quick_metric._method_definitions import metric_method
from quick_metric._apply_methods import apply_method
@metric_method
def count_rows(data):
return len(data)
data = pd.DataFrame({'a': [1, 2, 3]})
result = apply_method('count_rows', data)
print(result) # 3
Apply multiple methods:
from quick_metric._apply_methods import apply_methods
@metric_method
def sum_column(data, column='a'):
return data[column].sum()
methods = ['count_rows', 'sum_column']
results = apply_methods(methods, data)
print(results) # {'count_rows': 3, 'sum_column': 6}
apply_method(data, method_spec, metrics_methods=None)
Apply the specified method to the filtered data from metrics methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The DataFrame containing the filtered data. |
required |
method_spec
|
str or dict
|
The method specification. Can be either: - str: The name of the method to be applied - dict: A dictionary with method name as key and parameters as value e.g., {'method_name': {'param1': value1, 'param2': value2}} |
required |
metrics_methods
|
Dict[str, Callable]
|
A dictionary of metrics methods. Defaults to METRICS_METHODS. This dictionary maps method names to their corresponding functions. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[str, Any]
|
A tuple containing (result_key, result) where result_key is the key to use in the results dictionary and result is the method output. |
Raises:
| Type | Description |
|---|---|
MetricsMethodNotFoundError
|
If the specified method is not found in the metrics methods. |
Source code in quick_metric/_apply_methods.py
apply_methods(data, method_specs, metrics_methods=None)
Apply multiple methods to the data. The methods are specified by their names or as parameter dictionaries and are looked up in the metrics_methods dictionary. The results are returned in a dictionary where the keys are method names (potentially with parameters) and the values are the results of applying the methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The DataFrame containing the filtered data. |
required |
method_specs
|
List[str | dict]
|
A list of method specifications. Each can be either: - str: The name of the method to be applied - dict: A dictionary with method name as key and parameters as value |
required |
metrics_methods
|
Dict[str, Callable]
|
A dictionary of metrics methods. Defaults to METRICS_METHODS. This dictionary maps method names to their corresponding functions. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary where the keys are the method names (with parameters if any) and the values are the results of applying the methods. |