def test_step_function(): # test step function # step function is a function of t s = step_function([0,4,5],[2,4,6]) tval = np.array([-0.1,0,3.9,4,4.1,5.1]) lam = lambdify(t, s) yield assert_array_equal(lam(tval), [0, 2, 2, 4, 4, 6]) s = step_function([0,4,5],[4,2,1]) lam = lambdify(t, s) yield assert_array_equal(lam(tval), [0, 4, 4, 2, 2, 1])
def convolve_regressors(paradigm, hrf_model, names=None, fir_delays=[0], fir_duration = 1.): """ Creation of a formula that represents the convolution of the conditions onset witha certain hrf model Parameters ---------- paradigm array of shape (nevents,2) if the type is event-related design or (nenvets,3) for a block design that contains (condition id, onset) or (condition id, onset, duration) hrf_model, string that can be 'Canonical', 'Canonical With Derivative' or 'FIR' that specifies the hemodynamic reponse function names=None, list of strings corresponding to the condition names if names==None, these are create as 'c1',..,'cn' meaning 'condition 1'.. 'condition n' fir_delays=[0], optional, array of shape(nb_onsets) or list in case of FIR design, yields the array of delays used in the FIR model fir_duration=1., float, duration of the FIR block; in general it should eb equal to the tr Returns ------- f a formula object that contains the convolved regressors as functions of time names list of strings corresponding to the condition names the output names depend on teh hrf model used if 'Canonical' then this is identical to the input names if 'Canonical With Derivative', then two names are produced for input name 'name': 'name' and 'name_derivative' fixme: normalization of the columns of the design matrix ? """ paradigm = np.asarray(paradigm) if paradigm.ndim !=2: raise ValueError('Paradigm should have 2 dimensions') ncond = int(paradigm[:,0].max()+1) if names==None: names=["c%d" % k for k in range(ncond)] else: if len(names)<ncond: raise ValueError, 'the number of names is less than the \ number of conditions' else: ncond = len(names) listc = [] hnames = [] if paradigm.shape[1]>2: typep = 'block' else: typep='event' for nc in range(ncond): onsets = paradigm[paradigm[:,0]==nc,1] nos = np.size(onsets) if nos>0: if typep=='event': if hrf_model=="Canonical": c = formula.define(names[nc], utils.events(onsets, f=hrf.glover)) listc.append(c) hnames.append(names[nc]) elif hrf_model=="Canonical With Derivative": c1 = formula.define(names[nc], utils.events(onsets, f=hrf.glover)) c2 = formula.define(names[nc]+"_derivative", utils.events(onsets, f=hrf.dglover)) listc.append(c1) listc.append(c2) hnames.append(names[nc]) hnames.append(names[nc]+"_derivative") elif hrf_model=="FIR": for i,ft in enumerate(fir_delays): lnames = names[nc]+"_delay_%d"%i changes = np.hstack((onsets+ft,onsets+ft+fir_duration)) ochanges = np.argsort(changes) values = np.hstack((np.ones(nos), np.zeros(nos))) changes = changes[ochanges] values = values[ochanges] c = formula.define(lnames, utils.step_function(changes,values)) listc.append(c) hnames.append(lnames) else: raise NotImplementedError,'unknown hrf model' elif typep=='block': offsets = onsets+paradigm[paradigm[:,0]==nc,2] changes = np.hstack((onsets,offsets)) values = np.hstack((np.ones(nos), -np.ones(nos))) if hrf_model=="Canonical": c = utils.events(changes,values, f=hrf.iglover) listc.append(c) hnames.append(names[nc]) elif hrf_model=="Canonical With Derivative": c1 = utils.events(changes,values, f=hrf.iglover) c2 = utils.events(changes,values, f=hrf.glover) listc.append(c1) listc.append(c2) hnames.append(names[nc]) hnames.append(names[nc]+"_derivative") elif hrf_model=="FIR": raise NotImplementedError,\ 'block design are not compatible with FIR at the moment' else: raise NotImplementedError,'unknown hrf model' else: raise NotImplementedError,'unknown type of paradigm' # create the formula p = formula.Formula(listc) return p, hnames
def convolve_regressors(paradigm, hrf_model, names=None, fir_delays=[0], fir_duration=1.0): """ Creation of a formula that represents the convolution of the conditions onset witha certain hrf model Parameters ---------- paradigm: paradigm instance hrf_model, string that can be 'Canonical', 'Canonical With Derivative' or 'FIR' that specifies the hemodynamic reponse function names=None, list of strings corresponding to the condition names if names==None, these are create as 'c1',..,'cn' meaning 'condition 1'.. 'condition n' fir_delays=[0], optional, array of shape(nb_onsets) or list in case of FIR design, yields the array of delays used in the FIR model fir_duration=1., float, duration of the FIR block; in general it should eb equal to the tr Returns ------- f a formula object that contains the convolved regressors as functions of time names list of strings corresponding to the condition names the output names depend on teh hrf model used if 'Canonical' then this is identical to the input names if 'Canonical With Derivative', then two names are produced for input name 'name': 'name' and 'name_derivative' fixme: normalization of the columns of the design matrix ? """ ncond = int(paradigm.index.max() + 1) if names == None: names = ["c%d" % k for k in range(ncond)] else: if len(names) < ncond: raise ValueError, "the number of names is less than the \ number of conditions" else: ncond = len(names) listc = [] hnames = [] typep = paradigm.type for nc in range(ncond): onsets = paradigm.onset[paradigm.index == nc] nos = np.size(onsets) if paradigm.amplitude is not None: values = paradigm.amplitude[paradigm.index == nc] else: values = np.ones(nos) if nos > 0: if typep == "event": if hrf_model == "Canonical": c = utils.define(names[nc], utils.events(onsets, values, f=hrf.glover)) listc.append(c) hnames.append(names[nc]) elif hrf_model == "Canonical With Derivative": c1 = utils.define(names[nc], utils.events(onsets, values, f=hrf.glover)) c2 = utils.define(names[nc] + "_derivative", utils.events(onsets, values, f=hrf.dglover)) listc.append(c1) listc.append(c2) hnames.append(names[nc]) hnames.append(names[nc] + "_derivative") elif hrf_model == "FIR": for i, ft in enumerate(fir_delays): lnames = names[nc] + "_delay_%d" % i changes = np.hstack((onsets + ft, onsets + ft + fir_duration)) ochanges = np.argsort(changes) lvalues = np.hstack((values, np.zeros(nos))) changes = changes[ochanges] lvalues = lvalues[ochanges] c = utils.define(lnames, utils.step_function(changes, lvalues)) listc.append(c) hnames.append(lnames) else: raise NotImplementedError, "unknown hrf model" elif typep == "block": offsets = onsets + paradigm.duration[paradigm.type == nc] changes = np.hstack((onsets, offsets)) values = np.hstack((values, -values)) if hrf_model == "Canonical": c = utils.events(changes, values, f=hrf.iglover) listc.append(c) hnames.append(names[nc]) elif hrf_model == "Canonical With Derivative": c1 = utils.events(changes, values, f=hrf.iglover) c2 = utils.events(changes, values, f=hrf.glover) listc.append(c1) listc.append(c2) hnames.append(names[nc]) hnames.append(names[nc] + "_derivative") elif hrf_model == "FIR": raise NotImplementedError, "block design are not compatible with FIR at the moment" else: raise NotImplementedError, "unknown hrf model" # create the formula p = formula.Formula(listc) return p, hnames
def convolve_regressors(paradigm, hrf_model, end_time, fir_delays=[0], fir_duration=1.): """ Creation of a formula that represents the convolution of the conditions onset with a certain hrf model Parameters ---------- paradigm: paradigm instance hrf_model: string that can be 'Canonical', 'Canonical With Derivative' or 'FIR' that specifies the hemodynamic reponse function end_time: float, end time of the paradigm (needed only for block designs) fir_delays=[0], optional, array of shape(nb_onsets) or list in case of FIR design, yields the array of delays used in the FIR model fir_duration=1., float, duration of the FIR block in general it should eb equal to the tr Returns ------- f: formula instance, contains the convolved regressors as functions of time names: list of strings, the condition names, that depend on the hrf model used if 'Canonical' then this is identical to the input names if 'Canonical With Derivative', then two names are produced for input name 'name': 'name' and 'name_derivative' fixme ----- normalization of the columns of the design matrix ? """ listc = [] hnames = [] typep = paradigm.type for nc in np.unique(paradigm.con_id): onsets = paradigm.onset[paradigm.con_id == nc] nos = np.size(onsets) if paradigm.amplitude is not None: values = paradigm.amplitude[paradigm.con_id == nc] else: values = np.ones(nos) if nos < 1: continue if typep == 'event': if hrf_model == "Canonical": c = utils.define( nc, utils.events(onsets, values, f=hrf.glover)) listc.append(c) hnames.append(nc) elif hrf_model == "Canonical With Derivative": c1 = utils.define( nc, utils.events(onsets, values, f=hrf.glover)) c2 = utils.define(nc + "_derivative", utils.events(onsets, values, f=hrf.dglover)) listc.append(c1) listc.append(c2) hnames.append(nc) hnames.append(nc + "_derivative") elif hrf_model == "FIR": for i, ft in enumerate(fir_delays): lnames = nc + "_delay_%d" % i changes = np.hstack((onsets + ft, onsets + ft + fir_duration)) ochanges = np.argsort(changes) lvalues = np.hstack((values, np.zeros(nos))) changes = changes[ochanges] lvalues = lvalues[ochanges] c = utils.define(lnames, utils.step_function(changes, lvalues)) listc.append(c) hnames.append(lnames) else: raise NotImplementedError('unknown hrf model') elif typep == 'block': offsets = onsets + paradigm.duration[paradigm.con_id == nc] intervals = [[on, off] for (on, off) in zip(onsets, offsets)] blks = utils.blocks(intervals, values) changes = np.hstack((onsets, offsets)) cvalues = np.hstack((values, - values)) if hrf_model == "Canonical": c = utils.convolve_functions(blks, hrf.glover(hrf.T), [0, end_time], 0.001) listc.append(c) hnames.append(nc) elif hrf_model == "Canonical With Derivative": c1 = utils.convolve_functions(blks, hrf.glover(hrf.T), [0, end_time], 0.001) c2 = utils.events(changes, cvalues, f=hrf.glover) listc.append(c1) listc.append(c2) hnames.append(nc) hnames.append(nc + "_derivative") elif hrf_model == "FIR": raise NotImplementedError( 'block design are not compatible with FIR') else: raise NotImplementedError('unknown hrf model') # create the formula p = formula.Formula(listc) return p, hnames