File size: 2,102 Bytes
fa10c3d
 
 
 
 
699b778
 
 
 
fa10c3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699b778
fa10c3d
699b778
fa10c3d
699b778
 
 
 
 
 
 
 
 
fa10c3d
699b778
 
fa10c3d
699b778
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pickle
import os
import numpy as np
from datetime import datetime
import pandas as pd
from src.utils.mail import Mail
from config import Config

config = vars(Config)

def save_models(models, model_type, directory):

    print('Saving models...')

    for i, model in enumerate(models):

        with open(f'{directory}/{model_type}_FOLD_{i+1}.pkl', 'wb') as file:  
            pickle.dump(model, file)


def load_models(directory):
        
    print('Loading models...')

    models = []
    # List all files in the directory
    files = os.listdir(directory)

    pkl_files = [file for file in files if file.endswith('.pkl')]

    for file in pkl_files:
        with open(os.path.join(directory, file), 'rb') as file:  
            model = pickle.load(file)
        models.append(model)
    
    return models

def get_predictions(models, X_test):

    print('Forecasting test data...')

    preds = []
    for model in models:
        preds.append(post_process(model.predict(X_test)))

    return np.mean(preds, axis=0)

def post_process(predictions):
    
    predictions = predictions.clip(0)
    
    return predictions

def save_results(dataframe, file_name):

    print('Saving results...')

    today_date = datetime.now().strftime("%Y-%m-%d")

    dataframe.to_excel(f'demand_predictions/{file_name}_{today_date}.xlsx', index=False)

def save_parquet(dataframe, path):
    dataframe.to_parquet(path, index=False)

def load_parquet(path):
    return pd.read_parquet(path)

def send_mail(**kwargs):

    mail = Mail(config['MJ_APIKEY_PUBLIC'], config['MJ_APIKEY_PRIVATE'])

    status = mail.send_mail(
        fromName=kwargs.get('name'),
        fromMail=kwargs.get('email'),
        fromSubject=kwargs.get('subject'),
        fromMsg=kwargs.get('message'),
        dataframe=kwargs.get('dataframe'),
        forecast_dates=(kwargs.get('forecast_start_date'),kwargs.get('forecast_end_date')),
        toMail=kwargs.get('toMail')
    )

    if status == 200:
        return True
    
    return False