58 lines
2.2 KiB
Python
Executable File
58 lines
2.2 KiB
Python
Executable File
import pandas as pd
|
|
import io
|
|
from flask import send_file
|
|
|
|
|
|
def update_forecast_history(session, train_percent, test_percent, forecast_periods, model_type, metrics,
|
|
add_to_existing=False):
|
|
new_entry = {
|
|
'train_percent': train_percent * 100,
|
|
'test_percent': test_percent * 100,
|
|
'forecast_periods': forecast_periods,
|
|
'mae': metrics['MAE'] if metrics else None,
|
|
'mse': metrics['MSE'] if metrics else None,
|
|
'rmse': metrics['RMSE'] if metrics else None,
|
|
'model_type': model_type
|
|
}
|
|
forecast_history = session.get('forecast_history', [])
|
|
if not any(entry['train_percent'] == new_entry['train_percent'] and
|
|
entry['test_percent'] == new_entry['test_percent'] and
|
|
entry['forecast_periods'] == new_entry['forecast_periods'] and
|
|
entry['model_type'] == new_entry['model_type']
|
|
for entry in forecast_history):
|
|
forecast_history.append(new_entry)
|
|
session['forecast_history'] = forecast_history
|
|
if add_to_existing:
|
|
session['selected_indices'] = session.get('selected_indices', []) + [len(forecast_history) - 1]
|
|
else:
|
|
session['selected_indices'] = [len(forecast_history) - 1]
|
|
session.modified = True
|
|
|
|
|
|
def download_forecast_history(session):
|
|
forecast_history = session.get('forecast_history', [])
|
|
if not forecast_history:
|
|
return None, 'No forecast history available'
|
|
|
|
# Create DataFrame for forecast history
|
|
df = pd.DataFrame(forecast_history)
|
|
df = df.rename(columns={
|
|
'train_percent': 'Train Percent (%)',
|
|
'test_percent': 'Test Percent (%)',
|
|
'forecast_periods': 'Forecast Periods',
|
|
'mae': 'MAE',
|
|
'mse': 'MSE',
|
|
'rmse': 'RMSE',
|
|
'model_type': 'Model'
|
|
})
|
|
df.insert(0, 'Run', range(1, len(df) + 1))
|
|
|
|
# Save to Excel
|
|
output = io.BytesIO()
|
|
df.to_excel(output, index=False)
|
|
output.seek(0)
|
|
|
|
return send_file(output,
|
|
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
as_attachment=True,
|
|
download_name='forecast_history.xlsx'), None |