Core Functions
Main orchestration and workflow functionality for Quick Metric.
Provides entry points for the quick_metric framework, handling the complete workflow from YAML configuration reading to metric result generation.
Functions:
| Name | Description |
|---|---|
read_metric_instructions : Load metric configurations from YAML files |
|
interpret_metric_instructions : Execute complete metric workflow on data |
|
generate_metrics : Main public API for generating metrics from data |
|
Examples:
Load configuration from YAML file:
from pathlib import Path
from quick_metric._core import read_metric_instructions
config_path = Path('metrics.yaml')
instructions = read_metric_instructions(config_path)
Execute complete workflow:
import pandas as pd
from quick_metric._core import interpret_metric_instructions
from quick_metric._method_definitions import metric_method
@metric_method
def count_records(data):
return len(data)
data = pd.DataFrame({'category': ['A', 'B', 'A'], 'value': [1, 2, 3]})
config = {
'category_metrics': {
'method': ['count_records'],
'filter': {'category': 'A'}
}
}
results = interpret_metric_instructions(data, config)
print(results['category_metrics']['count_records']) # 2
YAML Configuration Format
See Also
filter : Data filtering functionality used by this module apply_methods : Method execution functionality used by this module method_definitions : Method registration system used by this module
read_metric_instructions(metric_config_path)
cached
Read metric_instructions dictionary from a YAML config file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_config_path
|
Path
|
Path to the YAML config file containing metric instructions. |
required |
Returns:
| Type | Description |
|---|---|
Dict
|
The 'metric_instructions' dictionary from the YAML file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the configuration file does not exist. |
MetricSpecificationError
|
If the YAML file is invalid or missing metric_instructions. |
Source code in quick_metric/_core.py
interpret_metric_instructions(data, metric_instructions, metrics_methods=None)
Apply filters and methods from metric instructions to a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The DataFrame to be processed. |
required |
metric_instructions
|
Dict
|
Dictionary containing the metrics and their filter/method conditions. |
required |
metrics_methods
|
Dict
|
Dictionary of available methods. Defaults to METRICS_METHODS. |
None
|
Returns:
| Type | Description |
|---|---|
Dict
|
Dictionary with metric names as keys and method results as values. |
Raises:
| Type | Description |
|---|---|
MetricSpecificationError
|
If metric instructions are invalid or missing required keys. |
Source code in quick_metric/_core.py
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 | |
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 | |