Update version and enhance metrics calculation in Laborious system - Updated image tag in values.yaml from 1.1.0 to 1.1.1. - Modified GITHUB_BRANCH environment variable for consistency. - Added a new method `calculate_simple_metrics` in model_metrics.py to compute various model performance metrics including RMSE, MSE, MAE, and R2. - Integrated the new metrics calculation into the worker setup, allowing for concurrent processing of simple metrics. - Updated tests to cover the new metrics calculation functionality, ensuring comprehensive validation of the implementation.
95 lines
3.1 KiB
Python
95 lines
3.1 KiB
Python
from temporalio import workflow
|
|
|
|
with workflow.unsafe.imports_passed_through():
|
|
from datetime import timedelta
|
|
from typing import Any
|
|
|
|
from sientia_do.temporal.policies import retry_policy
|
|
|
|
from laborious.activities.activities import Activities
|
|
from sientia_do.temporal.constants import DATETIME_FORMAT_WITH_TZ
|
|
|
|
|
|
@workflow.defn(name='simple_metrics')
|
|
class SimpleMetrics:
|
|
@workflow.run
|
|
async def run(self, input_data: dict[str, Any]):
|
|
"""
|
|
Execute the simple metrics workflow.
|
|
"""
|
|
metadata = {
|
|
'metadata': {
|
|
'schedule_name': input_data['schedule_name'],
|
|
'model_name': input_data['model_name'],
|
|
'model_id': input_data['model_id'],
|
|
'workflow_name': 'simple_metrics',
|
|
}
|
|
}
|
|
|
|
model_id = input_data['model_id']
|
|
interval_minutes = input_data['interval_minutes']
|
|
|
|
model_config = input_data['model_config']
|
|
target_name = model_config['target']
|
|
|
|
query = f"""
|
|
select p."timestamp", p.prediction, ld.value as "target"
|
|
from {input_data['schema']}.{input_data['predictions_table_name']} p
|
|
inner join {input_data['schema']}.{input_data['data_table_name']} ld
|
|
on p."timestamp" = ld."timestamp"
|
|
where
|
|
p.model_id = {model_id} and
|
|
p.prediction is not null and
|
|
ld.variable = '{target_name}' and
|
|
ld.value is not null and
|
|
p."timestamp" >= NOW() - INTERVAL '{interval_minutes} minutes'
|
|
order by
|
|
p."timestamp" desc;
|
|
"""
|
|
|
|
target_data = await workflow.execute_local_activity_method(
|
|
Activities.load_custom_query,
|
|
{
|
|
**metadata,
|
|
'query': query,
|
|
'datetime_columns': ['timestamp'],
|
|
},
|
|
retry_policy=retry_policy,
|
|
start_to_close_timeout=timedelta(seconds=300),
|
|
)
|
|
|
|
if not target_data:
|
|
return
|
|
|
|
simple_metrics = await workflow.execute_local_activity_method(
|
|
Activities.calculate_simple_metrics,
|
|
{
|
|
**metadata,
|
|
'model_id': model_id,
|
|
'target_data': target_data,
|
|
'metrics': input_data.get('metrics', ['rmse', 'mse', 'mae', 'r2']),
|
|
'interval_minutes': interval_minutes,
|
|
},
|
|
retry_policy=retry_policy,
|
|
start_to_close_timeout=timedelta(seconds=300),
|
|
)
|
|
|
|
|
|
if not simple_metrics:
|
|
return
|
|
|
|
await workflow.execute_activity_method(
|
|
Activities.export_data_to_postgres,
|
|
{
|
|
**metadata,
|
|
'data': simple_metrics,
|
|
'schema': input_data['schema'],
|
|
'table_name': input_data['target_table_name'],
|
|
'timestamp_conversion': {
|
|
'column': 'timestamp',
|
|
'format': DATETIME_FORMAT_WITH_TZ,
|
|
},
|
|
},
|
|
retry_policy=retry_policy,
|
|
start_to_close_timeout=timedelta(seconds=300),
|
|
) |