Load and process data from a specified path. Parameters: data_path (str): Path to the data file. feature_type (str): Type of feature to extract. seq_len (int): Desired sequence length. Returns: tuple: Processed feature tensor and label tensor.
(data_path, feature_type, seq_len, num_tab=1)
| 23 | return X |
| 24 | |
| 25 | def load_data(data_path, feature_type, seq_len, num_tab=1): |
| 26 | """ |
| 27 | Load and process data from a specified path. |
| 28 | |
| 29 | Parameters: |
| 30 | data_path (str): Path to the data file. |
| 31 | feature_type (str): Type of feature to extract. |
| 32 | seq_len (int): Desired sequence length. |
| 33 | |
| 34 | Returns: |
| 35 | tuple: Processed feature tensor and label tensor. |
| 36 | """ |
| 37 | data = np.load(data_path) |
| 38 | X = data["X"] |
| 39 | y = data["y"] |
| 40 | |
| 41 | if feature_type == "DIR": |
| 42 | X = np.sign(X) # Directional feature |
| 43 | X = length_align(X, seq_len) |
| 44 | X = torch.tensor(X[:,np.newaxis], dtype=torch.float32) |
| 45 | elif feature_type == "DT": |
| 46 | X = length_align(X, seq_len) |
| 47 | X = torch.tensor(X[:,np.newaxis], dtype=torch.float32) |
| 48 | elif feature_type == "DT2": |
| 49 | X_dir = np.sign(X) |
| 50 | X_time = np.abs(X) |
| 51 | X_time = np.diff(X_time) |
| 52 | X_time[X_time < 0] = 0 # Ensure no negative values |
| 53 | X_dir = length_align(X_dir, seq_len)[:, np.newaxis] |
| 54 | X_time = length_align(X_time, seq_len)[:, np.newaxis] |
| 55 | X = np.concatenate([X_dir, X_time], axis=1) |
| 56 | X = torch.tensor(X, dtype=torch.float32) |
| 57 | elif feature_type == "TAM": |
| 58 | X = length_align(X, seq_len) |
| 59 | X = torch.tensor(X[:,np.newaxis], dtype=torch.float32) |
| 60 | elif feature_type == "TAF": |
| 61 | X = length_align(X, seq_len) |
| 62 | X = torch.tensor(X, dtype=torch.float32) |
| 63 | elif feature_type == "Origin": |
| 64 | X = length_align(X, seq_len) |
| 65 | return X, y |
| 66 | else: |
| 67 | raise ValueError(f"Feature type {feature_type} is not matched.") |
| 68 | |
| 69 | if num_tab == 1: |
| 70 | y = torch.tensor(y, dtype=torch.int64) |
| 71 | else: |
| 72 | y = torch.tensor(y, dtype=torch.float32) |
| 73 | |
| 74 | return X, y |
| 75 | |
| 76 | def load_iter(X, y, batch_size, is_train=True, num_workers=8, weight_sample=False): |
| 77 | """ |
nothing calls this directly
no test coverage detected