Skip to content

Commit

Permalink
iit
Browse files Browse the repository at this point in the history
  • Loading branch information
huiwenn committed Feb 24, 2024
1 parent c0a5d96 commit ca19300
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pylinter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
with:
source: "src"
mypy-options: "--ignore-missing-imports --show-error-codes"
black-options: "--exclude **/*"
#pylint-options: "--rcfile=setup.cfg"
isort-options: "-w 100"
django: true
Expand Down
87 changes: 47 additions & 40 deletions src/experiments/covid_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import matplotlib.pyplot as plt
from src.models import rnn, lstm, bjrnn, cfrnn, copulaCPTS, dplstm, vanila_copula


class COVIDDataset(torch.utils.data.Dataset):
def __init__(self, X, Y, sequence_lengths):
super(COVIDDataset, self).__init__()
Expand All @@ -20,64 +21,65 @@ def __getitem__(self, idx):
return self.X[idx], self.Y[idx], self.sequence_lengths[idx]


def experiment(train, valid, test, target_len=50, name="exp"):
rnn_model = rnn.rnn(
input_size=1, embedding_size=128, output_size=1, horizon=target_len
)
encdec_model = lstm.lstm_seq2seq(
input_size=1, output_size=1, embedding_size=128, target_len=target_len
)
models = [rnn_model] # , encdec_model]

def experiment(train, valid, test, target_len = 50,name='exp'):
rnn_model = rnn.rnn(input_size=1, embedding_size=128, output_size=1, horizon=target_len)
encdec_model = lstm.lstm_seq2seq(input_size=1, output_size=1 , embedding_size=128, target_len =target_len)
models = [rnn_model] #, encdec_model]

x_train = train.X
y_train = train.Y

for m in models:
m.train_model(x_train,y_train, n_epochs=200, batch_size=50)
with open('./trained/models_%s.pkl'%name, 'wb') as f:
pickle.dump(models , f)
m.train_model(x_train, y_train, n_epochs=200, batch_size=50)

with open("./trained/models_%s.pkl" % name, "wb") as f:
pickle.dump(models, f)

x_cali = valid.X
y_cali = valid.Y

UQ = {}


dprnn = dplstm.DPRNN(epochs=150, input_size=1, output_size=1, n_steps=target_len, dropout_prob=0.2)

dprnn = dplstm.DPRNN(
epochs=150, input_size=1, output_size=1, n_steps=target_len, dropout_prob=0.2
)
dprnn.fit(x_train, y_train)
UQ['dprnn'] = dprnn
bj_class = bjrnn.bj_rnn(models[0],recursion_depth=15)
UQ["dprnn"] = dprnn

bj_class = bjrnn.bj_rnn(models[0], recursion_depth=15)
bj_class.LOBO_residuals = np.expand_dims(bj_class.LOBO_residuals, axis=-1)
UQ['bjrnn'] = bj_class
UQ["bjrnn"] = bj_class

cf = cfrnn.CFRNN(models[0], x_cali, y_cali)
cf.calibrate()
UQ['cfrnn'] = cf
UQ["cfrnn"] = cf

cf = cfrnn.CFRNN(models[1], x_cali, y_cali)
cf.calibrate()
UQ['cf-EncDec'] = cf

UQ["cf-EncDec"] = cf

copula = copulaCPTS.copulaCPTS(models[0], x_cali, y_cali)
copula.calibrate()
UQ['copula-rnn'] = copula
UQ["copula-rnn"] = copula

copula = copulaCPTS.copulaCPTS(models[1], x_cali, y_cali)
copula.calibrate()
UQ['copula-EncDec'] = copula

UQ["copula-EncDec"] = copula

vanilla = vanila_copula.vanila_copula(models[0], x_cali, y_cali)
vanilla.calibrate()
UQ['vanila-copula'] = vanilla
UQ["vanila-copula"] = vanilla

x_test = test.X
y_test = test.Y
y_test = test.Y

areas = {}
coverages = {}


epsilon_ls = np.linspace(0.05, 0.50, 10)

for k, uqmethodß in UQ.items():
Expand All @@ -91,27 +93,32 @@ def experiment(train, valid, test, target_len = 50,name='exp'):
coverage.append(uqmethod.calc_coverage_1d(box, pred, y_test))
areas[k] = area
coverages[k] = coverage
with open('./trained/uq_%s.pkl'%name, 'wb') as f:
pickle.dump(UQ , f)
with open('./trained/results_%s.pkl'%name, 'wb') as f:
pickle.dump((areas, coverages) , f)

with open("./trained/uq_%s.pkl" % name, "wb") as f:
pickle.dump(UQ, f)
with open("./trained/results_%s.pkl" % name, "wb") as f:
pickle.dump((areas, coverages), f)

return areas, coverages, (models, UQ)


def main():

with open("processed_data/covid_conformal_scaled.pkl", "rb") as f:
train_dataset, calibration_dataset, test_dataset = pickle.load(f)

for i in range(3):
res = experiment(train_dataset, calibration_dataset, test_dataset, target_len=50,
name='covid_daily_vanilla'+str(i)) # target len 6
print('finished run ' + str(i))
res = experiment(
train_dataset,
calibration_dataset,
test_dataset,
target_len=50,
name="covid_daily_vanilla" + str(i),
) # target len 6
print("finished run " + str(i))

del res



if __name__ == '__main__':
if __name__ == "__main__":
main()

0 comments on commit ca19300

Please sign in to comment.