Пример #1
0
def compute_model_ts(center_freq, sigma, spectrogram, freqs, target_times):

    # generate stimulus time-series
    rf = gaussian_1D(freqs, center_freq, sigma)
    # make sure sigma isn't too big
    # if np.any(np.round(rf[0],3) > 0):
    #     return np.inf
    # if np.any(np.round(rf[-1],3) > 0):
    #     return np.inf

    # create mask for speed
    distance = freqs - center_freq
    mask = np.zeros_like(distance, dtype='uint8')
    mask[distance < (5 * sigma)] = 1

    # extract the response
    stim = generate_rf_timeseries_1D(spectrogram, rf, mask)

    # recast the stimulus into a time-series that i can
    source_times = np.linspace(0, target_times[-1], len(stim), endpoint=True)
    f = interp1d(source_times, stim, kind='linear')
    new_stim = f(target_times)

    # hard-set the hrf_delay
    hrf_delay = 0

    # convolve it with the HRF
    hrf = utils.double_gamma_hrf(hrf_delay, 1.0, 10)
    stim_pad = np.tile(new_stim, 3)
    model = fftconvolve(stim_pad, hrf, 'same')[len(new_stim):len(new_stim) * 2]

    # normalize it
    model = utils.zscore(model)

    return model
Пример #2
0
 def estimate_scaling(self, center_freq, sigma):
     
     # de-log the center and spread
     center_freq = np.e ** center_freq
     sigma = np.e ** sigma
     
     # generate stimulus time-series
     rf = np.exp(-((self.stimulus.freqs-center_freq)**2)/(2*sigma**2))
     rf /= (sigma*np.sqrt(2*np.pi))
     
     mask = np.ones_like(rf).astype('uint8')
     
     # extract the response
     response = generate_rf_timeseries_1D(self.stimulus.spectrogram, rf, mask)
     
     # convolve it with the stimulus
     model = fftconvolve(response, self.hrf())[0:len(response)]
     
     # units
     model = (model - np.mean(model)) / np.mean(model)
     
     # regress to find beta and baseline
     p = linregress(model, self.data)
     
     return p
Пример #3
0
    def generate_ballpark_prediction(self, center_freq, sigma, hrf_delay):
        r"""
        Generate a prediction for the 1D Gaussian model.
        
        This function generates a prediction of the 1D Gaussian model, 
        given a stimulus and the stimulus-referred model parameters.  This
        function operates on the native stimulus.  Usually, the function
        `generate_ballpark_prediction` would operate on the downsampled
        stimulus.
        
        Paramaters
        ----------
        
        center_freq : float
            The center frequency of the 1D Gaussian, units are in Hz.
            
        sigma : float
            The dispersion of the 1D Gaussian, units are in Hz.
        
        beta : float
            The scaling factor to account for arbitrary units of the BOLD signal.
        
        hrf_delay : float
            The delay of the HRF, units are in seconds.
        
        """

        # receptive field
        rf = np.exp(-((10**self.stimulus.freqs - 10**center_freq)**2) /
                    (2 * (10**sigma)**2))
        rf /= (10**sigma * np.sqrt(2 * np.pi))

        # # create mask for speed
        # distance = self.stimulus.freqs - center_freq
        # mask = np.zeros_like(distance, dtype='uint8')
        # mask[distance < (self.mask_size*sigma)] = 1
        mask = np.ones_like(rf).astype('uint8')

        # extract the response
        response = generate_rf_timeseries_1D(self.stimulus.spectrogram, rf,
                                             mask)

        # convolve it with the stimulus
        hrf = self.hrf_model(hrf_delay, self.stimulus.tr_length)
        model = fftconvolve(response, hrf)[0:len(response)]

        # units
        model = (model - np.mean(model)) / np.mean(model)

        # regress to find beta and baseline
        p = linregress(model, self.data)

        # offset
        model += p[1]

        # scale it
        model *= np.abs(p[0])

        return model
Пример #4
0
 def generate_ballpark_prediction(self, center_freq, sigma, hrf_delay):
     
     r"""
     Generate a prediction for the 1D Gaussian model.
     
     This function generates a prediction of the 1D Gaussian model, 
     given a stimulus and the stimulus-referred model parameters.  This
     function operates on the native stimulus.  Usually, the function
     `generate_ballpark_prediction` would operate on the downsampled
     stimulus.
     
     Paramaters
     ----------
     
     center_freq : float
         The center frequency of the 1D Gaussian, units are in Hz.
         
     sigma : float
         The dispersion of the 1D Gaussian, units are in Hz.
         
     beta : float
         The scaling factor to account for arbitrary units of the BOLD signal.
         
     hrf_delay : float
         The delay of the HRF, units are in seconds.
         
     """
     
     # receptive field
     rf = np.exp(-((10**self.stimulus.freqs-10**center_freq)**2)/(2*(10**sigma)**2))
     rf /= (10**sigma*np.sqrt(2*np.pi))
     
     # # create mask for speed
     # distance = self.stimulus.freqs - center_freq
     # mask = np.zeros_like(distance, dtype='uint8')
     # mask[distance < (self.mask_size*sigma)] = 1
     mask = np.ones_like(rf).astype('uint8')
     
     # extract the response
     response = generate_rf_timeseries_1D(self.stimulus.spectrogram, rf, mask)
     
     # convolve it with the stimulus
     hrf = self.hrf_model(hrf_delay, self.stimulus.tr_length)
     model = fftconvolve(response, hrf)[0:len(response)]
     
     # units
     model = (model - np.mean(model)) / np.mean(model)
     
     # regress out mean and linear
     p = linregress(model, self.data)
     
     # offset
     model += p[1]
     
     # scale
     model *= p[0]
     
     return model
Пример #5
0
 def generate_prediction(self, center_freq, sigma, beta, baseline, hrf_delay):
     
     r"""
     Generate a prediction for the 1D Gaussian model.
     
     This function generates a prediction of the 1D Gaussian model, 
     given a stimulus and the stimulus-referred model parameters.  This
     function operates on the native stimulus.
     
     Paramaters
     ----------
     
     center_freq : float
         The center frequency of the 1D Gaussian, units are in Hz.
         
     sigma : float
         The dispersion of the 1D Gaussian, units are in Hz.
     
     beta : float
         The scaling factor to account for arbitrary units of the BOLD signal.
     
     hrf_delay : float
         The delay of the HRF, units are in seconds.
     
     """
     
     # de-log the center and spread
     center_freq = np.e ** center_freq
     sigma = np.e ** sigma
     
     # generate stimulus time-series
     rf = np.exp(-((self.stimulus.freqs-center_freq)**2)/(2*sigma**2))
     rf /= (sigma*np.sqrt(2*np.pi))
     
     # # create mask for speed
     # distance = self.stimulus.freqs - center_freq
     # mask = np.zeros_like(distance, dtype='uint8')
     # mask[distance < (self.mask_size*sigma)] = 1
     mask = np.ones_like(rf).astype('uint8')
     
     # extract the response
     response = generate_rf_timeseries_1D(self.stimulus.spectrogram, rf, mask)
     
     # convolve it with the stimulus
     self.hrf_delay = hrf_delay
     model = fftconvolve(response, self.hrf())[0:len(response)]
     
     # units
     model = (model - np.mean(model)) / np.mean(model)
     
     # offset
     model += baseline
     
     # scale it
     model *= beta
     
     return model
Пример #6
0
 def generate_prediction(self, center_freq, sigma, beta, baseline, unscaled=False):
     
     r"""
     Generate a prediction for the 1D Gaussian auditory pRF model.
     
     This function generates a prediction of the 1D Gaussian model, 
     given a stimulus and the stimulus-referred model parameters.  
     This function operates on a spectrogram representation of an 
     auditory stimulus.
     
     Paramaters
     ----------
     
     center_freq : float
         The center frequency of the 1D Gaussian, units are in Hz.
         
     sigma : float
         The dispersion of the 1D Gaussian, units are in Hz.
     
     baseline : float
         The y-intercept aka offset in amplitude to account for baseline.
     
     beta : float
         The scaling factor to account for arbitrary units of the BOLD signal.
     
     """ 
     
     # receptive field
     rf = np.exp(-((10**self.stimulus.freqs-10**center_freq)**2)/(2*(10**sigma)**2))
     rf /= (10**sigma*np.sqrt(2*np.pi))
     
     # evaluate entire RF
     mask = np.ones_like(rf).astype('uint8')
     
     # extract the response
     response = generate_rf_timeseries_1D(self.stimulus.spectrogram, rf, mask)
     
     # convolve it with the stimulus
     model = fftconvolve(response, self.hrf())[0:len(response)]
     
     # units
     model = self.normalizer(model)
     
     if unscaled:
         return model
     else:
         
         # offset
         model += baseline
         
         # scale it by beta
         model *= beta
         
         return model
Пример #7
0
 def generate_prediction(self, center_freq, sigma, beta, baseline, unscaled=False):
     
     r"""
     Generate a prediction for the 1D Gaussian auditory pRF model.
     
     This function generates a prediction of the 1D Gaussian model, 
     given a stimulus and the stimulus-referred model parameters.  
     This function operates on a spectrogram representation of an 
     auditory stimulus.
     
     Paramaters
     ----------
     
     center_freq : float
         The center frequency of the 1D Gaussian, units are in Hz.
         
     sigma : float
         The dispersion of the 1D Gaussian, units are in Hz.
     
     baseline : float
         The y-intercept aka offset in amplitude to account for baseline.
     
     beta : float
         The scaling factor to account for arbitrary units of the BOLD signal.
     
     """ 
     
     # receptive field
     rf = np.exp(-((10**self.stimulus.freqs-10**center_freq)**2)/(2*(10**sigma)**2))
     rf /= (10**sigma*np.sqrt(2*np.pi))
     
     # evaluate entire RF
     mask = np.ones_like(rf).astype('uint8')
     
     # extract the response
     response = generate_rf_timeseries_1D(self.stimulus.spectrogram, rf, mask)
     
     # convolve it with the stimulus
     model = fftconvolve(response, self.hrf())[0:len(response)]
     
     # units
     model = self.normalizer(model)
     
     if unscaled:
         return model
     else:
         
         # scale it by beta
         model *= beta
         
         # offset
         model += baseline
         
         return model
Пример #8
0
    def generate_ballpark_prediction(self, center_freq, sigma):
        r"""
        Generate a prediction for the 1D Gaussian model.

        This function generates a prediction of the 1D Gaussian model, 
        given a stimulus and the stimulus-referred model parameters.  
        This function operates on a spectrogram representation of an 
        auditory stimulus. This method does not estimate the scaling
        paramter `beta` or the offset parameter `baseline`, since this
        method will be used for a grid-fit and these values can simply
        be calculated for a particular `center_freq` and `sigma` pair.
        
        Paramaters
        ----------
        
        center_freq : float
            The center frequency of the 1D Gaussian, units are in Hz.
            
        sigma : float
            The dispersion of the 1D Gaussian, units are in Hz.
            
        """

        # receptive field
        rf = np.exp(-((10**self.stimulus.freqs - 10**center_freq)**2) /
                    (2 * (10**sigma)**2))
        rf /= (10**sigma * np.sqrt(2 * np.pi))

        # evaluate entire RF
        mask = np.ones_like(rf).astype('uint8')

        # extract the response
        response = generate_rf_timeseries_1D(self.stimulus.spectrogram, rf,
                                             mask)

        # convolve it with the stimulus
        model = fftconvolve(response, self.hrf())[0:len(response)]

        # units
        model = (model - np.mean(model)) / np.mean(model)

        # regress to find beta and baseline
        p = linregress(model, self.data)

        # offset
        model += p[1]

        # scale it
        model *= np.abs(p[0])

        return model
Пример #9
0
    def generate_ballpark_prediction(self, center_freq, sigma):

        r"""
        Generate a prediction for the 1D Gaussian model.

        This function generates a prediction of the 1D Gaussian model, 
        given a stimulus and the stimulus-referred model parameters.  
        This function operates on a spectrogram representation of an 
        auditory stimulus. This method does not estimate the scaling
        paramter `beta` or the offset parameter `baseline`, since this
        method will be used for a grid-fit and these values can simply
        be calculated for a particular `center_freq` and `sigma` pair.
        
        Paramaters
        ----------
        
        center_freq : float
            The center frequency of the 1D Gaussian, units are in Hz.
            
        sigma : float
            The dispersion of the 1D Gaussian, units are in Hz.
            
        """
        
        # receptive field
        rf = np.exp(-((10**self.stimulus.freqs-10**center_freq)**2)/(2*(10**sigma)**2))
        rf /= (10**sigma*np.sqrt(2*np.pi))
        
        # evaluate entire RF
        mask = np.ones_like(rf).astype('uint8')
        
        # extract the response
        response = generate_rf_timeseries_1D(self.stimulus.spectrogram, rf, mask)
        
        # convolve it with the stimulus
        model = fftconvolve(response, self.hrf())[0:len(response)]
        
        # units
        model = self.normalizer(model)
        
        # regress out mean and amplitude
        beta, baseline = self.regress(model, self.data)
        
        # offset
        model += baseline
        
        # scale
        model *= beta
        
        return model
Пример #10
0
def compute_model_ts(center_freq, sigma,
                     spectrogram, freqs, target_times):
    
    # generate stimulus time-series
    rf = gaussian_1D(freqs, center_freq, sigma)
    # make sure sigma isn't too big
    # if np.any(np.round(rf[0],3) > 0):
    #     return np.inf
    # if np.any(np.round(rf[-1],3) > 0):
    #     return np.inf
    
    # create mask for speed
    distance = freqs - center_freq
    mask = np.zeros_like(distance, dtype='uint8')
    mask[distance < (5*sigma)] = 1
        
    # extract the response
    stim = generate_rf_timeseries_1D(spectrogram,rf,mask)
    
    # recast the stimulus into a time-series that i can 
    source_times = np.linspace(0,target_times[-1],len(stim),endpoint=True)
    f = interp1d(source_times,stim,kind='linear')
    new_stim = f(target_times)
    
    # hard-set the hrf_delay
    hrf_delay = 0
    
    # convolve it with the HRF
    hrf = utils.double_gamma_hrf(hrf_delay, 1.0, 10)
    stim_pad = np.tile(new_stim,3)
    model = fftconvolve(stim_pad, hrf,'same')[len(new_stim):len(new_stim)*2]
    
    # normalize it
    model = utils.zscore(model)
    
    return model