File size: 4,340 Bytes
7c7d73c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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": {}
    }
    
    # Helper function to determine if we should load a given group.
    # If last_season=True, only groups starting with 's07' are kept.
    # Otherwise, load all.
    def should_load(group_name):
        if last_season:
            return group_name.startswith('s07')
        return True

    # -------------------------------------------------------------------------
    # 1) VISUAL FEATURES
    # -------------------------------------------------------------------------
    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

    # -------------------------------------------------------------------------
    # 2) AUDIO FEATURES
    # -------------------------------------------------------------------------
    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

    # -------------------------------------------------------------------------
    # 3) LANGUAGE FEATURES
    # -------------------------------------------------------------------------
    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