def fn(x): summed = np.expand_dims(np.sum(x, axis=0), axis=0) if not np.all(offsets == 0): summed = _offset_coefficients(summed, offsets=offsets, fs=fs, pad_bins=False) return summed
def _get_fir_coefficients(modelspec, idx=0, fs=None): i = 0 for m in modelspec: if 'fir' in m['fn']: if 'fn_coefficients' in m.keys(): fn = ms._lookup_fn_at(m['fn_coefficients']) kwargs = {**m['fn_kwargs'], **m['phi']} # Merges dicts return fn(**kwargs) #elif 'pole_zero' in m['fn']: # c = pz_coefficients(poles=m['phi']['poles'], # zeros=m['phi']['zeros'], # delays=m['phi']['delays'], # gains=m['phi']['gains'], # n_coefs=m['fn_kwargs']['n_coefs'], fs=100) # return c elif 'dexp' in m['fn']: c = fir_dexp_coefficients(phi=m['phi']['phi'], n_coefs=m['fn_kwargs']['n_coefs']) return c elif 'exp' in m['fn']: tau = m['phi']['tau'] if 'a' in m['phi']: a = m['phi']['a'] else: a = m['fn_kwargs']['a'] if 'b' in m['phi']: b = m['phi']['b'] else: b = m['fn_kwargs']['b'] c = fir_exp_coefficients(tau, a=a, b=b, n_coefs=m['fn_kwargs']['n_coefs']) return c elif i == idx: coefficients = m['phi']['coefficients'] if 'offsets' in m['phi']: if fs is None: log.warning("couldn't compute offset coefficients for " "STRF heatmap, no fs provided.") else: coefficients = _offset_coefficients( coefficients, m['phi']['offsets'], fs=fs) return coefficients else: i += 1 return None
def contrast(rec, tau, a, b, s, mean, sd, i='stim', o='ctpred', c='contrast', offsets=0.0, n_channels=18, n_coefs=15, compute_contrast=False): if compute_contrast: wc_coeffs = gaussian_coefficients(mean, sd, n_channels) fir_coeffs = fir_exp_coefficients(tau, a, b, s, n_coefs=15) if not np.all(offsets == 0): fs = rec[i].fs fir_coeffs = _offset_coefficients(fir_coeffs, offsets, fs, pad_bins=True) wc_coeffs = np.abs(wc_coeffs) fir_coeffs = np.abs(fir_coeffs) def fn(x): weighted = wc_coeffs.T * x weighted[np.isnan(weighted)] = 0 width = wc_coeffs.shape[0] history = fir_coeffs.shape[-1] zero_pad = np.zeros([width, history - 1]) filt = np.concatenate((zero_pad, fir_coeffs), axis=1) filt /= np.sum(fir_coeffs) mn = convolve2d(weighted, filt, mode='same', fillvalue=0) var = convolve2d(weighted**2, filt, mode='same', fillvalue=0) - mn**2 ct = np.sqrt(var) / (mn * .99 + np.nanmax(mn) * 0.01) ctpred = np.sum(ct, axis=0) ctpred = np.expand_dims(ctpred, axis=0) return ctpred else: # pass through zeros until ready to fit GC portion of the model fn = lambda x: np.zeros((1, x.shape[-1])) return [rec[i].transform(fn, o)]
def contrast_kernel_heatmap2(rec, modelspec, ax=None, title=None, idx=0, channels=0, xlabel='Lag (s)', ylabel='Channel In', **options): ct_idx = nu.find_module('contrast', modelspec) phi = copy.deepcopy(modelspec[ct_idx]['phi']) fn_kwargs = copy.deepcopy(modelspec[ct_idx]['fn_kwargs']) fs = rec['stim'].fs wc_kwargs = {k: phi[k] for k in ['mean', 'sd']} wc_kwargs['n_chan_in'] = fn_kwargs['n_channels'] fir_kwargs = {k: phi[k] for k in ['tau', 'a', 'b', 's']} fir_kwargs['n_coefs'] = fn_kwargs['n_coefs'] wc_coefs = gaussian_coefficients(**wc_kwargs) fir_coefs = fir_exp_coefficients(**fir_kwargs) if 'offsets' in phi: offsets = phi['offsets'] elif 'offsets' in fn_kwargs: offsets = fn_kwargs['offsets'] else: offsets = None if offsets is not None: fir_coefs = _offset_coefficients(fir_coefs, offsets, fs, pad_bins=True) wc_coefs = np.abs(wc_coefs).T fir_coefs = np.abs(fir_coefs) strf = wc_coefs @ fir_coefs # TODO: This isn't really doing the same operation as an STRF anymore # so it may be better not to plot it this way in the future. _strf_heatmap(strf, wc_coefs, fir_coefs, xlabel=xlabel, ylabel=ylabel, ax=ax, title=title) return ax
def _get_ctk_coefficients(wc_coefficients=None, fir_coefficients=None, mean=None, sd=None, coefficients=None, f1s=None, taus=None, delays=None, gains=None, use_phi=False, n_coefs=18, offsets=None, offset=None, fs=None, **kwargs): if use_phi: wc_coeffs = gaussian_coefficients(mean, sd, n_coefs) if coefficients is None: fir_coeffs = do_coefficients(f1s=f1s, taus=taus, delays=delays, gains=gains, n_coefs=n_coefs) else: fir_coeffs = coefficients else: if (wc_coefficients is None) or (fir_coefficients is None): raise ValueError("contrast_kernel module was called without " "wc or fir coefficients set.") wc_coeffs = wc_coefficients fir_coeffs = fir_coefficients if (offsets is not None) and (fs is not None): fir_coeffs = _offset_coefficients(fir_coeffs, offsets, fs) elif (offsets is None) and (offset is not None): # old model offset = int(offset) fir_coeffs = np.concatenate((np.zeros( (fir_coeffs.shape[0], offset)), fir_coeffs[:, :-1 * offset]), axis=1) coeffs = np.abs(wc_coeffs.T @ fir_coeffs) return coeffs, wc_coeffs.T, fir_coeffs
def offset_coefficients(self, coefficients, offsets, fs, pad_bins): return _offset_coefficients(coefficients, offsets, fs, pad_bins=pad_bins)