-
Notifications
You must be signed in to change notification settings - Fork 55
/
ts.py
66 lines (50 loc) · 2.17 KB
/
ts.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
"""Time series datasets, especially for medical time series"""
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from src.dataloaders.base import default_data_path, SequenceDataset, deprecated
class BIDMC(SequenceDataset):
"""BIDMC datasets for Respiratory Rate / Heart Rate / Oxygen Saturation regression"""
_name_ = "bidmc"
d_input = 2
@property
def d_output(self):
return 2 if self.prediction else 1
@property
def l_output(self):
return 4000 if self.prediction else 0
@property
def init_defaults(self):
return {
"target": "RR", # 'RR' | 'HR' | 'SpO2'
"prediction": False,
"reshuffle": True,
}
def setup(self):
self.data_dir = self.data_dir or default_data_path / self._name_
split = "reshuffle" if self.reshuffle else "original"
# X: (dataset_size, length, d_input)
# y: (dataset_size)
X_train = np.load(self.data_dir / self.target / split / "trainx.npy")
y_train = np.load(self.data_dir / self.target / split / "trainy.npy")
X_val = np.load(self.data_dir / self.target / split / "validx.npy")
y_val = np.load(self.data_dir / self.target / split / "validy.npy")
X_test = np.load(self.data_dir / self.target / split / "testx.npy")
y_test = np.load(self.data_dir / self.target / split / "testy.npy")
if self.prediction:
y_train = np.pad(X_train[:, 1:, :], ((0, 0), (0, 1), (0, 0)))
y_val = np.pad(X_val[:, 1:, :], ((0, 0), (0, 1), (0, 0)))
y_test = np.pad(X_test[:, 1:, :], ((0, 0), (0, 1), (0, 0)))
self.dataset_train = torch.utils.data.TensorDataset(
torch.FloatTensor(X_train), torch.FloatTensor(y_train)
)
self.dataset_val = torch.utils.data.TensorDataset(
torch.FloatTensor(X_val), torch.FloatTensor(y_val)
)
self.dataset_test = torch.utils.data.TensorDataset(
torch.FloatTensor(X_test), torch.FloatTensor(y_test)
)
def __str__(self):
split = "reshuffle" if self.reshuffle else "original"
return f"BIDMC{self.target}_{split}"