forked from Nixtla/neuralforecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdysts_wrapper.py
71 lines (60 loc) · 2.12 KB
/
dysts_wrapper.py
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
import os
import numpy as np
import pandas as pd
import sys
sys.path.append("../dysts")
from dysts.base import make_trajectory_ensemble
import dysts.flows as flows
import json
def make_randic(name, nseq, seqlen, ic_perturb):
fn = f"{name}_{nseq}x{seqlen}_ic-{ic_perturb}"
if name == "Lorenz":
model = flows.Lorenz()
else:
raise Exception
perturb = 1 + (ic_perturb / 2) - ic_perturb * np.random.random((nseq, 3))
model.ic = model.ic[None, :] * perturb
tpts, sol = model.make_trajectory(n=seqlen, return_times=True)
df = pd.DataFrame(columns=["unique_id", "ds", "y"]).astype(
{"unique_id": str, "ds": np.int64, "y": np.float64}
)
md = {"ic": model.ic.tolist(), "dt": tpts[1]}
with open(f"{fn}.json", "w") as f:
json.dump(md, f, sort_keys=True, indent=4)
for i in range(nseq):
y = sol[i].reshape(-1)
ds = np.arange(len(y))
uid = [f"{name}-{i}"] * len(y)
df = pd.concat(
[df, pd.DataFrame.from_dict({"unique_id": uid, "ds": ds, "y": y})]
)
df.to_csv(f"{fn}.csv", index=False)
def make(seqlen=100, pts_per_period=75, resample=True):
csvfn = f"dysts_ensemble_3d-{seqlen}.csv"
if os.path.exists(csvfn):
print(f"reading from {csvfn}")
df = pd.read_csv(csvfn)
else:
print(f"generating {csvfn}...", end=" ", flush=True)
ens = make_trajectory_ensemble(
seqlen, resample=resample, pts_per_period=pts_per_period
)
df = pd.DataFrame(columns=["unique_id", "ds", "y"]).astype(
{"unique_id": str, "ds": np.int64, "y": np.float64}
)
for k in ens:
if ens[k].shape[1] == 3:
y = ens[k].reshape(-1)
ds = np.arange(len(y))
uid = [k] * len(y)
df = pd.concat(
[df, pd.DataFrame.from_dict({"unique_id": uid, "ds": ds, "y": y})]
)
df.to_csv(csvfn, index=False)
print("done")
return df
if __name__ == "__main__":
import sys
# seqlen = int(sys.argv[1])
# make(seqlen)
make_randic("Lorenz", 25, 10000, 0.02)