def test_masked_array_events(): # make sure masked arrays passed in stay as masked arrays masked = np.ma.masked_invalid([0,np.nan,2]) e = ts.Events([1,2,3], d=masked) npt.assert_equal(e.data['d'].mask, [False, True, False]) # make sure regular arrays passed don't become masked notmasked = np.array([0,np.nan,2]) e2 = ts.Events([1,2,3], d=notmasked) npt.assert_raises(AttributeError, e2.data['d'].__getattribute__,'mask')
def analyze_average(data, onsets, sampling_interval, len_et=12, offset=-2, y_label="Bold signal", time_unit='s'): # Times series initialization ts = timeseries.TimeSeries(data, sampling_interval=sampling_interval, time_unit=time_unit) # Events initialization events = timeseries.Events(onsets, time_unit=time_unit) # Timeseries analysis #len_et = numbre of TR what you want to see #offset = number of offset including in len_et analyzer = analysis.EventRelatedAnalyzer(ts, events, len_et=len_et, offset=offset) return analyzer
def test_Events_scalar(): t = ts.TimeArray(1, time_unit='ms') i, j = 4, 5 ev = ts.Events(t, i=i, j=j) # The semantics of scalar indexing into events are such that the returned # value is always a new Events object (the mental model is that of python # strings, where slicing OR scalar indexing still return the same thing, a # string again -- there are no 'string scalars', and there are no 'Event # scalars' either). npt.assert_equal(ev.data['i'][0], i) npt.assert_equal(ev.data['j'][0], j)
def test_Events(): # time has to be one-dimensional nt.assert_raises(ValueError, ts.Events, np.zeros((2, 2))) t = ts.TimeArray([1, 2, 3], time_unit='ms') x = [1, 2, 3] y = [2, 4, 6] z = [10., 20., 30.] i0 = [0, 0, 1] i1 = [0, 1, 2] for unit in [None, 's', 'ns', 'D']: # events with data ev1 = ts.Events(t, time_unit=unit, i=x, j=y, k=z) # events with indices ev2 = ts.Events(t, time_unit=unit, indices=[i0, i1]) # events with indices and labels ev3 = ts.Events(t, time_unit=unit, labels=['trial', 'other'], indices=[i0, i1]) # Note that the length of indices and labels has to be identical: nt.assert_raises(ValueError, ts.Events, t, time_unit=unit, labels=['trial', 'other'], indices=[i0])# Only # one of # the # indices! # make sure the time is retained npt.assert_equal(ev1.time, t) npt.assert_equal(ev2.time, t) # make sure time unit is correct if unit is not None: npt.assert_equal(ev1.time_unit, unit) npt.assert_equal(ev2.time_unit, unit) else: npt.assert_equal(ev1.time_unit, t.time_unit) npt.assert_equal(ev2.time_unit, t.time_unit) # make sure we can extract data npt.assert_equal(ev1.data['i'], x) npt.assert_equal(ev1.data['j'], y) npt.assert_equal(ev1.data['k'], z) # make sure we can get the indices by label npt.assert_equal(ev3.index.trial, i0) npt.assert_equal(ev3.index.other, i1) # make sure we can get the indices by position npt.assert_equal(ev2.index.i0, i0) npt.assert_equal(ev2.index.i1, i1) #make sure slicing works #one_event = ts.Events(t[[0]],time_unit=unit,i=[x[0]],j=[y[0]],k=[z[0]]) #regular indexing npt.assert_equal(ev1[0].data['i'], x[0]) npt.assert_equal(ev1[0:2].data['i'], x[0:2]) # indexing w/ time npt.assert_equal(ev1[0.].data['i'], x[0]) # indexing w/ epoch ep = ts.Epochs(start=0, stop=1.5, time_unit='ms') npt.assert_equal(ev1[ep].data['i'], x[0]) # fancy indexing (w/ boolean mask) npt.assert_equal(ev1[ev3.index.trial == 0].data['j'], y[0:2]) # len() function is implemented and working assert len(t) == len(ev1) == len(ev2) == len(ev3)
""" Two data files are used in this example. The first contains the times of action potentials ('spikes'), recorded intra-cellularly from primary auditory receptors in the grasshopper *Locusta Migratoria*. We read in these times and initialize an Events object from them. The spike-times are given in micro-seconds: """ data_path = os.path.join(nitime.__path__[0], 'data') spike_times = np.loadtxt(os.path.join(data_path, 'grasshopper_spike_times1.txt')) spike_ev = ts.Events(spike_times, time_unit='us') """ The first data file contains the stimulus that was played during the recording. Briefly, the stimulus played was a pure-tone in the cell's preferred frequency amplitude modulated by Gaussian white-noise, up to a cut-off frequency (200 Hz in this case, for details on the experimental procedures and the stimulus see [Rokem2006]_). """ stim = np.loadtxt(os.path.join(data_path, 'grasshopper_stimulus1.txt'))
def test_EventRelatedAnalyzer(): cycles = 10 l = 1024 unit = 2 * np.pi / l t = np.arange(0, 2 * np.pi + unit, unit) signal = np.sin(cycles * t) events = np.zeros(t.shape) # Zero crossings: idx = np.where(np.abs(signal) < 0.03)[0] # An event occurs at the beginning of every cycle: events[idx[:-2:2]] = 1 # and another kind of event at the end of each cycle: events[idx[1:-1:2]] = 2 T_signal = ts.TimeSeries(signal, sampling_rate=1) T_events = ts.TimeSeries(events, sampling_rate=1) for correct_baseline in [True, False]: ETA = nta.EventRelatedAnalyzer(T_signal, T_events, l / (cycles * 2), correct_baseline=correct_baseline).eta # This should hold npt.assert_almost_equal(ETA.data[0], signal[:ETA.data.shape[-1]], 3) npt.assert_almost_equal(ETA.data[1], -1 * signal[:ETA.data.shape[-1]], 3) # Same should be true for the FIR analysis: FIR = nta.EventRelatedAnalyzer(T_signal, T_events, l / (cycles * 2)).FIR npt.assert_almost_equal(FIR.data[0], signal[:FIR.data.shape[-1]], 3) npt.assert_almost_equal(FIR.data[1], -1 * signal[:FIR.data.shape[-1]], 3) # Same should be true for XCORR = nta.EventRelatedAnalyzer(T_signal, T_events, l / (cycles * 2)).xcorr_eta npt.assert_almost_equal(XCORR.data[0], signal[:XCORR.data.shape[-1]], 3) npt.assert_almost_equal(XCORR.data[1], -1 * signal[:XCORR.data.shape[-1]], 3) # More dimensions: T_signal = ts.TimeSeries(np.vstack([signal, signal]), sampling_rate=1) T_events = ts.TimeSeries(np.vstack([events, events]), sampling_rate=1) ETA = nta.EventRelatedAnalyzer(T_signal, T_events, l / (cycles * 2)).eta # The events input and the time-series input have different dimensions: T_events = ts.TimeSeries(events, sampling_rate=1) ETA = nta.EventRelatedAnalyzer(T_signal, T_events, l / (cycles * 2)).eta npt.assert_almost_equal(ETA.data[0][0], signal[:ETA.data.shape[-1]], 3) npt.assert_almost_equal(ETA.data[1][1], -1 * signal[:ETA.data.shape[-1]], 3) # Input is an Events object, instead of a time-series: ts1 = ts.TimeSeries(np.arange(100), sampling_rate=1) ev = ts.Events([10, 20, 30]) et = nta.EventRelatedAnalyzer(ts1, ev, 5) # The five points comprising the average of the three sequences: npt.assert_equal(et.eta.data, [20., 21., 22., 23., 24.]) ts2 = ts.TimeSeries(np.arange(200).reshape(2, 100), sampling_rate=1) ev = ts.Events([10, 20, 30]) et = nta.EventRelatedAnalyzer(ts2, ev, 5) npt.assert_equal( et.eta.data, [[20., 21., 22., 23., 24.], [120., 121., 122., 123., 124.]]) # The event-triggered SEM should be approximately zero: for correct_baseline in [True, False]: EA = nta.EventRelatedAnalyzer(T_signal, T_events, l / (cycles * 2), correct_baseline=correct_baseline) npt.assert_almost_equal(EA.ets.data[0], np.zeros_like(EA.ets.data[0]), decimal=2) # Test the et_data method: npt.assert_almost_equal(EA.et_data[0][0].data[0], signal[:ETA.data.shape[-1]]) # Test that providing the analyzer with an array, instead of an Events or a # TimeSeries object throws an error: with pytest.raises(ValueError) as e_info: nta.EventRelatedAnalyzer(ts2, events, 10) # This is not yet implemented, so this should simply throw an error, for # now: with pytest.raises(NotImplementedError) as e_info: nta.EventRelatedAnalyzer.FIR_estimate(EA)