-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
128 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,17 @@ | |
# Created by Linglong Qian, Joseph Arul Raj <[email protected], [email protected]> | ||
# License: BSD-3-Clause | ||
|
||
import copy | ||
from typing import Iterable | ||
from ...data.dataset import BaseDataset | ||
from typing import Union | ||
|
||
import numpy as np | ||
import torch | ||
from typing import Union | ||
import copy | ||
from ...data.utils import parse_delta | ||
from sklearn.preprocessing import StandardScaler | ||
|
||
from ...data.dataset import BaseDataset | ||
from ...data.utils import parse_delta | ||
|
||
|
||
def normalize_csai( | ||
data, | ||
|
@@ -22,7 +24,8 @@ def normalize_csai( | |
compute_intervals: bool = False, | ||
): | ||
""" | ||
Normalize the data based on the given mean and standard deviation, and optionally compute time intervals between observations. | ||
Normalize the data based on the given mean and standard deviation, | ||
and optionally compute time intervals between observations. | ||
Parameters | ||
---------- | ||
|
@@ -33,7 +36,8 @@ def normalize_csai( | |
The mean values for each variable, used for normalization. If empty, means will be computed from the data. | ||
std : list of float, optional | ||
The standard deviation values for each variable, used for normalization. If empty, std values will be computed from the data. | ||
The standard deviation values for each variable, used for normalization. | ||
If empty, std values will be computed from the data. | ||
compute_intervals : bool, optional, default=False | ||
Whether to compute the time intervals between observations for each variable. | ||
|
@@ -47,10 +51,12 @@ def normalize_csai( | |
The mean values for each variable after normalization, either computed from the data or passed as input. | ||
std_set : np.ndarray | ||
The standard deviation values for each variable after normalization, either computed from the data or passed as input. | ||
The standard deviation values for each variable after normalization, | ||
either computed from the data or passed as input. | ||
intervals_list : dict of int to float, optional | ||
If `compute_intervals` is True, this will return the median time intervals between observations for each variable. | ||
If `compute_intervals` is True, this will return the median time intervals between observations | ||
for each variable. | ||
""" | ||
|
||
# Convert data to numpy array if it is a torch tensor | ||
|
@@ -296,13 +302,23 @@ class DatasetForCSAI(BaseDataset): | |
Parameters | ||
---------- | ||
data : | ||
The dataset for model input, which can be either a dictionary or a path string to a data file. If it's a dictionary, `X` should be an array-like structure with shape [n_samples, sequence length (n_steps), n_features], containing the time-series data, and it can have missing values. Optionally, the dictionary can include `y`, an array-like structure with shape [n_samples], representing the labels of `X`. If `data` is a path string, it should point to a data file (e.g., h5 file) that contains key-value pairs like a dictionary, including keys for `X` and possibly `y`. | ||
The dataset for model input, which can be either a dictionary or a path string to a data file. | ||
If it's a dictionary, `X` should be an array-like structure | ||
with shape [n_samples, sequence length (n_steps), n_features], containing the time-series data, | ||
and it can have missing values. Optionally, the dictionary can include `y`, | ||
an array-like structure with shape [n_samples], representing the labels of `X`. | ||
If `data` is a path string, it should point to a data file (e.g., h5 file) that contains key-value pairs like | ||
a dictionary, including keys for `X` and possibly `y`. | ||
return_X_ori : | ||
Whether to return the original time-series data (`X_ori`) when fetching data samples, useful for evaluation purposes. | ||
Whether to return the original time-series data (`X_ori`) when fetching data samples, | ||
useful for evaluation purposes. | ||
return_y : | ||
Whether to return classification labels in the `__getitem__()` method if they exist in the dataset. If `True`, labels will be included in the returned data samples, which is useful for training classification models. If `False`, the labels won't be returned, suitable for testing or validation stages. | ||
Whether to return classification labels in the `__getitem__()` method if they exist in the dataset. | ||
If `True`, labels will be included in the returned data samples, | ||
which is useful for training classification models. | ||
If `False`, the labels won't be returned, suitable for testing or validation stages. | ||
file_type : | ||
The type of the data file if `data` is a path string, such as "hdf5". | ||
|
@@ -317,20 +333,29 @@ class DatasetForCSAI(BaseDataset): | |
Whether to compute time intervals between observations for handling irregular time-series data. | ||
replacement_probabilities : | ||
Optional precomputed probabilities for sampling missing values. If not provided, they will be calculated during the initialization of the dataset. | ||
Optional precomputed probabilities for sampling missing values. | ||
If not provided, they will be calculated during the initialization of the dataset. | ||
normalise_mean : | ||
A list of mean values for normalizing the input features. If not provided, they will be computed during initialization. | ||
A list of mean values for normalizing the input features. | ||
If not provided, they will be computed during initialization. | ||
normalise_std : | ||
A list of standard deviation values for normalizing the input features. If not provided, they will be computed during initialization. | ||
A list of standard deviation values for normalizing the input features. | ||
If not provided, they will be computed during initialization. | ||
training : | ||
Whether the dataset is used for training. If `False`, it will adjust how data is processed, particularly for evaluation and testing phases. | ||
Whether the dataset is used for training. | ||
If `False`, it will adjust how data is processed, particularly for evaluation and testing phases. | ||
Notes | ||
----- | ||
The DatasetForCSAI class is designed for bidirectional imputation of time-series data, handling both forward and backward directions to improve imputation accuracy. It supports on-the-fly data normalization and missing value simulation, making it suitable for training and evaluating deep learning models like CSAI. The class can work with large datasets stored on disk, leveraging lazy-loading to minimize memory usage, and supports both training and testing scenarios, adjusting data handling as needed. | ||
The DatasetForCSAI class is designed for bidirectional imputation of time-series data, | ||
handling both forward and backward directions to improve imputation accuracy. | ||
It supports on-the-fly data normalization and missing value simulation, | ||
making it suitable for training and evaluating deep learning models like CSAI. | ||
The class can work with large datasets stored on disk, leveraging lazy-loading to minimize memory usage, | ||
and supports both training and testing scenarios, adjusting data handling as needed. | ||
""" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,11 @@ | |
# Created by Linglong Qian, Joseph Arul Raj <[email protected], [email protected]> | ||
# License: BSD-3-Clause | ||
|
||
import math | ||
|
||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
import math | ||
|
||
from .layers import FeatureRegression, Decay, Decay_obs, PositionalEncoding, Conv1dWithInit, TorchTransformerEncoder | ||
from ....utils.metrics import calc_mae | ||
|
||
|
@@ -91,7 +92,7 @@ class BackboneCSAI(nn.Module): | |
""" | ||
|
||
def __init__(self, n_steps, n_features, rnn_hidden_size, step_channels, medians_df=None): | ||
super(BackboneCSAI, self).__init__() | ||
super().__init__() | ||
|
||
if medians_df is not None: | ||
self.medians_tensor = torch.tensor(list(medians_df.values())).float() | ||
|
@@ -134,7 +135,7 @@ def forward(self, x, mask, deltas, last_obs, h=None): | |
|
||
decay_factor = self.weighted_obs(deltas - medians.unsqueeze(1)) | ||
|
||
if h == None: | ||
if h is None: | ||
data_last_obs = self.input_projection(last_obs.permute(0, 2, 1)).permute(0, 2, 1) | ||
data_decay_factor = self.input_projection(decay_factor.permute(0, 2, 1)).permute(0, 2, 1) | ||
|
||
|
@@ -191,7 +192,7 @@ def forward(self, x, mask, deltas, last_obs, h=None): | |
|
||
class BackboneBCSAI(nn.Module): | ||
def __init__(self, n_steps, n_features, rnn_hidden_size, step_channels, medians_df=None): | ||
super(BackboneBCSAI, self).__init__() | ||
super().__init__() | ||
|
||
self.model_f = BackboneCSAI(n_steps, n_features, rnn_hidden_size, step_channels, medians_df) | ||
self.model_b = BackboneCSAI(n_steps, n_features, rnn_hidden_size, step_channels, medians_df) | ||
|
Oops, something went wrong.