JustAGeek commited on
Commit
7536b30
·
verified ·
1 Parent(s): e839f3b

Upload prepare_cross_session_data.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. prepare_cross_session_data.py +160 -0
prepare_cross_session_data.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ prepare_cross_session_data.py
3
+ Prepare Aug16 session data for cross-session evaluation (Phase 2, Step 1).
4
+
5
+ The WILD dataset has 5 Jacobs Hall sessions. Our models trained on sessions 1+2
6
+ (July28). To test cross-session generalization, we need sessions 3-5 (Aug16).
7
+
8
+ STEP 1 — Download raw feature files from WILD (password-protected):
9
+ https://wcsng.ucsd.edu/wild/
10
+ Place these in ./data/:
11
+ features_jacobs_Aug16_1.mat
12
+ features_jacobs_Aug16_3.mat
13
+ features_jacobs_Aug16_4_ref.mat
14
+
15
+ STEP 2 — Run this script:
16
+ python prepare_cross_session_data.py
17
+
18
+ OUTPUT — Creates in ./data/:
19
+ dataset_train_jacobs_Aug16_1.mat (all train samples from session 3)
20
+ dataset_train_jacobs_Aug16_3.mat (all train samples from session 4)
21
+ dataset_train_jacobs_Aug16_4_ref.mat (all train samples from session 5)
22
+
23
+ These are the files that eval_cross_session.py and train_and_test.py expect.
24
+ """
25
+ import os
26
+ import sys
27
+ import h5py
28
+ import numpy as np
29
+
30
+ # Paths
31
+ DATA_DIR = './data'
32
+ SPLIT_DIR = '../data_split_idx'
33
+ CHUNK = 300
34
+
35
+
36
+ def load_indices(split_file, key):
37
+ """Load MATLAB 1-based index array, return 0-based numpy int array."""
38
+ import scipy.io
39
+ if not os.path.exists(split_file):
40
+ raise FileNotFoundError(f'Split file not found: {split_file}')
41
+ try:
42
+ with h5py.File(split_file, 'r') as f:
43
+ idx = np.array(f[key]).flatten().astype(np.int64) - 1
44
+ except Exception:
45
+ mat = scipy.io.loadmat(split_file)
46
+ idx = mat[key].flatten().astype(np.int64) - 1
47
+ return idx
48
+
49
+
50
+ def get_var_names(h5_file):
51
+ """Handle both naming conventions."""
52
+ keys = list(h5_file.keys())
53
+ w_key = 'features_w_offset' if 'features_w_offset' in keys else 'features_with_offset'
54
+ wo_key = 'features_wo_offset' if 'features_wo_offset' in keys else 'features_without_offset'
55
+ return w_key, wo_key
56
+
57
+
58
+ def extract_and_save(src_path, dst_path, indices):
59
+ """Read samples at indices from source HDF5, write to dest in dataset_* format."""
60
+ sorted_idx = np.sort(indices)
61
+ n = len(sorted_idx)
62
+ print(f' Writing {n} samples -> {os.path.basename(dst_path)}')
63
+
64
+ with h5py.File(src_path, 'r') as src, h5py.File(dst_path, 'w') as dst:
65
+ w_key, wo_key = get_var_names(src)
66
+
67
+ src_shape_w = src[w_key].shape
68
+ src_shape_lbl = src['labels_gaussian_2d'].shape
69
+
70
+ out_shape_w = src_shape_w[:-1] + (n,)
71
+ out_shape_lbl = src_shape_lbl[:-1] + (n,)
72
+
73
+ # Always write with standard names (features_w_offset, etc.)
74
+ ds_w = dst.create_dataset('features_w_offset', shape=out_shape_w, dtype='float32',
75
+ compression='gzip', chunks=True)
76
+ ds_wo = dst.create_dataset('features_wo_offset', shape=out_shape_w, dtype='float32',
77
+ compression='gzip', chunks=True)
78
+ ds_lbl = dst.create_dataset('labels_gaussian_2d', shape=out_shape_lbl, dtype='float32',
79
+ compression='gzip', chunks=True)
80
+
81
+ written = 0
82
+ for start in range(0, n, CHUNK):
83
+ end = min(start + CHUNK, n)
84
+ chunk_idx = sorted_idx[start:end].tolist()
85
+
86
+ ds_w [..., written:written + (end - start)] = np.array(src[w_key ][..., chunk_idx], dtype=np.float32)
87
+ ds_wo [..., written:written + (end - start)] = np.array(src[wo_key][..., chunk_idx], dtype=np.float32)
88
+ ds_lbl[..., written:written + (end - start)] = np.array(src['labels_gaussian_2d'][..., chunk_idx], dtype=np.float32)
89
+
90
+ written += (end - start)
91
+ print(f' {written}/{n}', end='\r', flush=True)
92
+ print()
93
+ print(f' Saved: {dst_path}')
94
+
95
+
96
+ def prepare_aug16_session(session_name, features_filename, split_filename):
97
+ """Prepare one Aug16 session's data."""
98
+ src = os.path.join(DATA_DIR, features_filename)
99
+ split_file = os.path.join(SPLIT_DIR, split_filename)
100
+
101
+ if not os.path.exists(src):
102
+ print(f' MISSING: {src}')
103
+ print(f' -> Download {features_filename} from WILD website')
104
+ return False
105
+
106
+ if not os.path.exists(split_file):
107
+ print(f' MISSING: {split_file}')
108
+ return False
109
+
110
+ print(f'\n Processing {session_name}...')
111
+ print(f' Source: {src}')
112
+ print(f' Split: {split_file}')
113
+
114
+ # Combine fov_train + non_fov_train for all training samples
115
+ # (For cross-session eval, these are used as test data)
116
+ fov_train = load_indices(split_file, 'fov_train_idx')
117
+ non_fov_train = load_indices(split_file, 'non_fov_train_idx')
118
+ all_train = np.concatenate([fov_train, non_fov_train])
119
+ print(f' Samples: {len(fov_train)} fov_train + {len(non_fov_train)} non_fov_train = {len(all_train)} total')
120
+
121
+ dst = os.path.join(DATA_DIR, f'dataset_train_jacobs_{session_name}.mat')
122
+ extract_and_save(src, dst, all_train)
123
+ return True
124
+
125
+
126
+ def main():
127
+ os.makedirs(DATA_DIR, exist_ok=True)
128
+
129
+ print('='*60)
130
+ print('Preparing Aug16 cross-session data')
131
+ print('='*60)
132
+
133
+ sessions = [
134
+ ('Aug16_1', 'features_jacobs_Aug16_1.mat', 'data_split_ids_jacobs_Aug16_1.mat'),
135
+ ('Aug16_3', 'features_jacobs_Aug16_3.mat', 'data_split_ids_jacobs_Aug16_3.mat'),
136
+ ('Aug16_4_ref', 'features_jacobs_Aug16_4_ref.mat', 'data_split_ids_jacobs_Aug16_4_ref.mat'),
137
+ ]
138
+
139
+ results = {}
140
+ for session_name, feat_file, split_file in sessions:
141
+ results[session_name] = prepare_aug16_session(session_name, feat_file, split_file)
142
+
143
+ print('\n' + '='*60)
144
+ print('SUMMARY')
145
+ print('='*60)
146
+ for name, ok in results.items():
147
+ status = 'READY' if ok else 'MISSING (download features file from WILD)'
148
+ print(f' {name}: {status}')
149
+
150
+ all_ok = all(results.values())
151
+ if all_ok:
152
+ print('\nAll Aug16 data prepared. Run:')
153
+ print(' python eval_cross_session.py')
154
+ else:
155
+ print('\nSome files missing. Download from: https://wcsng.ucsd.edu/wild/')
156
+ print('Place features_jacobs_Aug16_*.mat files in ./data/')
157
+
158
+
159
+ if __name__ == '__main__':
160
+ main()