| | import os |
| | import glob |
| | import h5py |
| | import numpy as np |
| |
|
| | def load_stimulus_features(root_data_dir, |
| | modality='all', |
| | last_season=False): |
| | """ |
| | Load the stimulus features from the .h5 files stored for each modality |
| | (visual, audio, language). If last_season is True, only load data for |
| | Friends Season 7 (i.e., groups whose name starts with 's07'). |
| | |
| | Parameters |
| | ---------- |
| | root_data_dir : str |
| | Root data directory. |
| | modality : str, optional |
| | One of ['visual', 'audio', 'language', 'all']. Decides which |
| | modality's features to load. Default is 'all'. |
| | last_season : bool, optional |
| | If True, only load data from Friends Season 7 (i.e., group names |
| | that start with 's07'). Default is False. |
| | |
| | Returns |
| | ------- |
| | features : dict |
| | Dictionary containing the stimulus features. The keys are |
| | 'visual', 'audio', 'language'. Each key maps to another dict, |
| | keyed by group name (e.g. 's07e01a'), whose value is the loaded |
| | numpy array. |
| | |
| | E.g.: |
| | { |
| | 'visual': { 's01e01a': np.ndarray, 's07e05b': np.ndarray, ... }, |
| | 'audio': { 's01e01a': np.ndarray, 's07e05b': np.ndarray, ... }, |
| | 'language': { 's01e01a': np.ndarray, 's07e05b': np.ndarray, ... } |
| | } |
| | """ |
| |
|
| | features = { |
| | "visual": {}, |
| | "audio": {}, |
| | "language": {} |
| | } |
| | |
| | |
| | |
| | |
| | def should_load(group_name): |
| | if last_season: |
| | return group_name.startswith('s07') |
| | return True |
| |
|
| | |
| | |
| | |
| | if modality == 'visual' or modality == 'all': |
| | visual_dir = os.path.join( |
| | root_data_dir, |
| | 'stimulus_features', |
| | 'raw', |
| | 'visual' |
| | ) |
| | h5_files = sorted(glob.glob(os.path.join(visual_dir, '*.h5'))) |
| | for h5_file in h5_files: |
| | with h5py.File(h5_file, 'r') as f: |
| | for group_name in f.keys(): |
| | if should_load(group_name): |
| | data = f[group_name]['visual'][...] |
| | features['visual'][group_name] = data |
| |
|
| | |
| | |
| | |
| | if modality == 'audio' or modality == 'all': |
| | audio_dir = os.path.join( |
| | root_data_dir, |
| | 'stimulus_features', |
| | 'raw', |
| | 'audio' |
| | ) |
| | h5_files = sorted(glob.glob(os.path.join(audio_dir, '*.h5'))) |
| | for h5_file in h5_files: |
| | with h5py.File(h5_file, 'r') as f: |
| | for group_name in f.keys(): |
| | if should_load(group_name): |
| | data = f[group_name]['audio'][...] |
| | features['audio'][group_name] = data |
| |
|
| | |
| | |
| | |
| | if modality == 'language' or modality == 'all': |
| | language_dir = os.path.join( |
| | root_data_dir, |
| | 'stimulus_features', |
| | 'raw', |
| | 'language' |
| | ) |
| | h5_files = sorted(glob.glob(os.path.join(language_dir, '*.h5'))) |
| | for h5_file in h5_files: |
| | with h5py.File(h5_file, 'r') as f: |
| | for group_name in f.keys(): |
| | if should_load(group_name): |
| | pooler = f[group_name]['language_pooler_output'][...] |
| | last_hidden = f[group_name]['language_last_hidden_state'][...] |
| | data = np.concatenate([ |
| | pooler.reshape(pooler.shape[0], -1), |
| | last_hidden.reshape(last_hidden.shape[0], -1) |
| | ], axis=1) |
| | features['language'][group_name] = data |
| |
|
| | return features |