|
|
| import os |
| import sys |
| import numpy as np |
| import json |
| import joblib |
| import logging |
| from os.path import join, dirname |
| import pickle |
|
|
| def make_delayed(stim, delays, circpad=False): |
| """Creates non-interpolated concatenated delayed versions of [stim] with the given [delays] |
| (in samples). |
| |
| If [circpad], instead of being padded with zeros, [stim] will be circularly shifted. |
| """ |
| nt,ndim = stim.shape |
| dstims = [] |
| for di,d in enumerate(delays): |
| dstim = np.zeros((nt, ndim)) |
| if d<0: |
| dstim[:d,:] = stim[-d:,:] |
| if circpad: |
| dstim[d:,:] = stim[:-d,:] |
| elif d>0: |
| dstim[d:,:] = stim[:-d,:] |
| if circpad: |
| dstim[:d,:] = stim[-d:,:] |
| else: |
| dstim = stim.copy() |
| dstims.append(dstim) |
| return np.hstack(dstims) |
|
|
| def lanczosfun(cutoff, t, window=3): |
| """Compute the lanczos function with some cutoff frequency [B] at some time [t]. |
| [t] can be a scalar or any shaped numpy array. |
| If given a [window], only the lowest-order [window] lobes of the sinc function |
| will be non-zero. |
| """ |
| t = t * cutoff |
| val = window * np.sin(np.pi*t) * np.sin(np.pi*t/window) / (np.pi**2 * t**2) |
| val[t==0] = 1.0 |
| val[np.abs(t)>window] = 0.0 |
| return val |
|
|
| def lanczosinterp2D(data, oldtime, newtime, window=3, cutoff_mult=1.0, rectify=False): |
| """Interpolates the columns of [data], assuming that the i'th row of data corresponds to |
| oldtime(i). A new matrix with the same number of columns and a number of rows given |
| by the length of [newtime] is returned. |
| |
| The time points in [newtime] are assumed to be evenly spaced, and their frequency will |
| be used to calculate the low-pass cutoff of the interpolation filter. |
| |
| [window] lobes of the sinc function will be used. [window] should be an integer. |
| """ |
| |
| cutoff = 1/np.mean(np.diff(newtime)) * cutoff_mult |
| |
| |
| |
| sincmat = np.zeros((len(newtime), len(oldtime))) |
| for ndi in range(len(newtime)): |
| sincmat[ndi,:] = lanczosfun(cutoff, newtime[ndi]-oldtime, window) |
| |
| if rectify: |
| newdata = np.hstack([np.dot(sincmat, np.clip(data, -np.inf, 0)), |
| np.dot(sincmat, np.clip(data, 0, np.inf))]) |
| else: |
| |
| newdata = np.dot(sincmat, data) |
|
|
| return newdata |
|
|
| def downsample_word_vectors(stories, word_vectors, wordseqs): |
| """Get Lanczos downsampled word_vectors for specified stories. |
| |
| Args: |
| stories: List of stories to obtain vectors for. |
| word_vectors: Dictionary of {story: <float32>[num_story_words, vector_size]} |
| wordseqs: Dictionary of {story: <object>} |
| |
| Returns: |
| Dictionary of {story: downsampled vectors} |
| """ |
| downsampled_semanticseqs = dict() |
| for story in stories: |
| downsampled_semanticseqs[story] = lanczosinterp2D( |
| word_vectors[story], wordseqs[story].data_times, |
| wordseqs[story].tr_times, window=3) |
| return downsampled_semanticseqs |
|
|
|
|
|
|
| if __name__ == "__main__": |
| raw_stories = pickle.load(open('raw_stories.pkl', 'rb')) |
| |