File size: 3,200 Bytes
ee4dd77 40e07d5 ee4dd77 3eef9a0 05690d2 ee4dd77 a788d0f ee4dd77 a788d0f ee4dd77 6cf4e3f 3eef9a0 1847ddd 40e07d5 a788d0f e3d5a14 a788d0f 40e07d5 1847ddd e3d5a14 a788d0f 1847ddd e11d622 3eef9a0 e11d622 6cf4e3f a788d0f 05690d2 a788d0f 05690d2 e3d5a14 6cf4e3f |
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
from darts import TimeSeries
from darts.datasets import ILINetDataset
from darts.metrics import mape
from darts.models import ExponentialSmoothing
from darts.utils.missing_values import fill_missing_values
from darts.dataprocessing.transformers import Scaler
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
def load_ILINetDataset():
"""
Dataset's Components Descriptions:
* % WEIGHTED ILI: Combined state-specific data of patients visit to healthcare providers for ILI reported each week weighted by state population
* % UNWEIGHTED ILI: Combined state-specific data of patients visit to healthcare providers for ILI reported each week unweighted by state population
* AGE 0-4: Number of patients between 0 and 4 years of age
* AGE 25-49: Number of patients between 25 and 49 years of age
* AGE 25-64: Number of patients between 25 and 64 years of age
* AGE 5-24: Number of patients between 5 and 24 years of age
* AGE 50-64: Number of patients between 50 and 64 years of age
* AGE 65: Number of patients above (>=65) 65 years of age
* ILITOTAL: Total number of ILI patients. For this system, ILI is defined as fever (temperature of 100°F [37.8°C] or greater) and a cough and/or a sore throat
* NUM. OF PROVIDERS: Number of outpatient healthcare providers
* TOTAL PATIENTS: Total number of patients
"""
ilidata = ILINetDataset().load().astype(np.float32)
return ilidata
def preprocess_data(ilidata):
ilitotal = ilidata['ILITOTAL']
covariates = ilidata.drop_columns(col_names='ILITOTAL') # TODO perform predictions with covariates time series
# Apply logaritmic transformation
log_ili = ilitotal.map(np.log)
# Remove -inf values
pd_log_ili = log_ili.pd_dataframe()
pd_log_ili['ILITOTAL'].replace(to_replace=-np.inf, value=np.nan, inplace=True)
log_ili = TimeSeries.from_dataframe(pd_log_ili)
log_ili = fill_missing_values(log_ili)
return log_ili
def train_val_split(ilidata):
train_ili, val_ili = ilidata.split_before(0.75)
return train_ili, val_ili
def scale_train(train_ili):
scaler = Scaler()
train_ili_scaled = scaler.fit_transform(train_ili)
return train_ili_scaled, scaler
def train(train_ili_scaled):
model = ExponentialSmoothing()
model.fit(train_ili_scaled)
return model
def save_model(model, path):
model_name = str(model).split('(')[0]
if not os.path.exists(path=path):
os.makedirs(path)
model.save(os.path.join(path, model_name))
#else:
# model = ExponentialSmoothing.load(path)
def load_model(path):
model = ExponentialSmoothing.load(path)
return model
def predict(model, val_ili):
preds = model.predict(len(val_ili))
return preds
def inverse_scale_predictions(scaled_preds, scaler):
preds = scaler.inverse_transform(scaled_preds)
return preds
def plot_results(train_ili, val_ili, preds):
fig, _ = plt.subplots()
train_ili.plot(label='train')
val_ili.plot(label='val')
preds.plot(label='preds')
return fig
def compute_mape(preds, val):
metric = mape(val, preds)
return metric
|