Beispiel #1
0
def test_percent_change():
    x = np.array([[99, 100, 101], [4, 5, 6]])
    p = utils.percent_change(x)

    npt.assert_equal(x.shape, p.shape)
    npt.assert_almost_equal(p[0, 2], 1.0)

    ts = np.arange(4 * 5).reshape(4, 5)
    ax = 0
    npt.assert_almost_equal(
        utils.percent_change(ts, ax),
        np.array(
            [
                [-100.0, -88.23529412, -78.94736842, -71.42857143, -65.2173913],
                [-33.33333333, -29.41176471, -26.31578947, -23.80952381, -21.73913043],
                [33.33333333, 29.41176471, 26.31578947, 23.80952381, 21.73913043],
                [100.0, 88.23529412, 78.94736842, 71.42857143, 65.2173913],
            ]
        ),
    )

    ax = 1
    npt.assert_almost_equal(
        utils.percent_change(ts, ax),
        np.array(
            [
                [-100.0, -50.0, 0.0, 50.0, 100.0],
                [-28.57142857, -14.28571429, 0.0, 14.28571429, 28.57142857],
                [-16.66666667, -8.33333333, 0.0, 8.33333333, 16.66666667],
                [-11.76470588, -5.88235294, 0.0, 5.88235294, 11.76470588],
            ]
        ),
    )
Beispiel #2
0
def test_percent_change():
    x = np.array([[99, 100, 101], [4, 5, 6]])
    p = utils.percent_change(x)

    npt.assert_equal(x.shape, p.shape)
    npt.assert_almost_equal(p[0, 2], 1.0)

    ts = np.arange(4 * 5).reshape(4, 5)
    ax = 0
    npt.assert_almost_equal(
        utils.percent_change(ts, ax),
        np.array(
            [[-100., -88.23529412, -78.94736842, -71.42857143, -65.2173913],
             [
                 -33.33333333, -29.41176471, -26.31578947, -23.80952381,
                 -21.73913043
             ],
             [33.33333333, 29.41176471, 26.31578947, 23.80952381, 21.73913043],
             [100., 88.23529412, 78.94736842, 71.42857143, 65.2173913]]))

    ax = 1
    npt.assert_almost_equal(
        utils.percent_change(ts, ax),
        np.array([[-100., -50., 0., 50., 100.],
                  [-28.57142857, -14.28571429, 0., 14.28571429, 28.57142857],
                  [-16.66666667, -8.33333333, 0., 8.33333333, 16.66666667],
                  [-11.76470588, -5.88235294, 0., 5.88235294, 11.76470588]]))
def compute_coherence(time_windows, TR, f_lb, f_ub, roi_names):
    n_timewindows = time_windows.shape[0]
    n_samples = time_windows.shape[1]
    n_rois = time_windows.shape[2]

    coherence_3Darray = np.zeros((n_timewindows, n_rois, n_rois))

    if n_rois == len(roi_names):

        for time_index in range(n_timewindows):

            ts = time_windows[time_index, :, :]
            data = np.zeros((n_rois, n_samples))

            for n_idx, roi in enumerate(roi_names):
                data[n_idx] = ts[:, n_idx]

            data = percent_change(data)
            T = TimeSeries(data, sampling_interval=TR)
            C = CoherenceAnalyzer(T)
            freq_idx = np.where(
                (C.frequencies > f_lb) * (C.frequencies < f_ub))[0]
            coh = np.mean(C.coherence[:, :, freq_idx], -1)

            coherence_3Darray[time_index] = coh
    else:
        raise Exception(
            "Number of ROIs in 3D Array do not match number of ROI names provided."
        )

    return coherence_3Darray
Beispiel #4
0
"""

roi_names = np.array(data_rec.dtype.names)
nseq = len(roi_names)
n_samples = data_rec.shape[0]
data = np.zeros((nseq, n_samples))

for n_idx, roi in enumerate(roi_names):
    data[n_idx] = data_rec[roi]
"""

We normalize the data in each of the ROIs to be in units of % change:

"""

pdata = utils.percent_change(data)
"""

We start by performing the detailed analysis, but note that a significant
short-cut is presented below, so if you just want to know how to do this
(without needing to understand the details), skip on down.

We start by defining how many tapers will be used and calculate the values of
the tapers and the associated eigenvalues of each taper:

"""

NW = 4
K = 2 * NW - 1

tapers, eigs = alg.dpss_windows(n_samples, NW, K)
Beispiel #5
0
roi_names = np.array(data_rec.dtype.names)
nseq = len(roi_names)
n_samples = data_rec.shape[0]
data = np.zeros((nseq, n_samples))

for n_idx, roi in enumerate(roi_names):
    data[n_idx] = data_rec[roi]


"""

We normalize the data in each of the ROIs to be in units of % change:

"""

pdata = utils.percent_change(data)

"""

We start by performing the detailed analysis, but note that a significant
short-cut is presented below, so if you just want to know how to do this
(without needing to understand the details), skip on down.

We start by defining how many tapers will be used and calculate the values of
the tapers and the associated eigenvalues of each taper:

"""

NW = 4
K = 2 * NW - 1
Beispiel #6
0
from nitime.utils import percent_change
import nitime.viz
reload(nitime.viz)
from nitime.viz import plot_xcorr

#This part is the same as before
TR=1.89
data_rec = csv2rec('data/fmri_timeseries.csv')
roi_names= np.array(data_rec.dtype.names)
n_samples = data_rec.shape[0]
data = np.zeros((len(roi_names),n_samples))

for n_idx, roi in enumerate(roi_names):
   data[n_idx] = data_rec[roi]

data = percent_change(data)
T = UniformTimeSeries(data,sampling_interval=TR)
T.metadata['roi'] = roi_names 
C = CorrelationAnalyzer(T)

#Extract the cross-correlation:
xc = C.xcorr_norm

idx_lcau = np.where(roi_names=='lcau')[0]
idx_rcau = np.where(roi_names=='rcau')[0]
idx_lput = np.where(roi_names=='lput')[0]

plot_xcorr(xc,((idx_lcau,idx_rcau),(idx_lcau,idx_lput)),
               line_labels = ['rcau','lput'])

Beispiel #7
0
data_rec = mlab.csv2rec('data/fmri_timeseries.csv')

#Extract information:
roi_names= data_rec.dtype.names
n_samples = data_rec.shape[0]

#This information has to be known in advance:
TR=1.89

#Make an empty container for the data
data = np.zeros((len(roi_names),n_samples))

for n_idx in range(len(roi_names)):
   data[n_idx] = data_rec[roi_names[n_idx]]

#Normalize the data:
data = tsu.percent_change(data)

#Initialize the time-series from the normalized data:
T = ts.UniformTimeSeries(data,sampling_interval=TR)
T.metadata['roi'] = roi_names 

C = tsa.CorrelationAnalyzer(T)
xc = C.xcorr_norm

N = len(roi_names)
ind = np.arange(N)  # the evenly spaced plot indices



Beispiel #8
0
 def percent_change(self):
     return ts.TimeSeries(tsu.percent_change(self.input.data),
                          sampling_rate=self.input.sampling_rate,
                          time_unit = self.input.time_unit)
Beispiel #9
0
percent signal change, using the utils.percent_change function.

"""

#Extract information:
roi_names = np.array(data_rec.dtype.names)
n_samples = data_rec.shape[0]

#Make an empty container for the data
data = np.zeros((len(roi_names), n_samples))

for n_idx, roi in enumerate(roi_names):
    data[n_idx] = data_rec[roi]

#Normalize the data:
data = percent_change(data)
"""

We initialize a TimeSeries object from the normalized data:

"""

T = TimeSeries(data, sampling_interval=TR)
T.metadata['roi'] = roi_names
"""

First, we examine the correlations between the time-series extracted from
different parts of the brain. The following script extracts the data (using the
drawmatrix_channels function, displaying the correlation matrix with the ROIs
labeled.
Beispiel #10
0
roi_names = np.array(data_rec.dtype.names)
nseq = len(roi_names)
n_samples = data_rec.shape[0]
data = np.zeros((nseq, n_samples))

for n_idx, roi in enumerate(roi_names):
    data[n_idx] = data_rec[roi]
"""

We normalize the data in each of the ROIs to be in units of % change and
initialize the TimeSeries object:

"""

pdata = tsu.percent_change(data)
time_series = ts.TimeSeries(pdata, sampling_interval=TR)
"""

We initialize the GrangerAnalyzer object, while specifying the order of the
autoregressive model to be 1 (predict the current behavior of the time-series
based on one time-point back).

"""

G = nta.GrangerAnalyzer(time_series, order=1)
"""

For comparison, we also initialize a CoherenceAnalyzer and a
CorrelationAnalyzer, with the same TimeSeries object:
Beispiel #11
0
 def percent_change(self):
     return ts.TimeSeries(tsu.percent_change(self.input.data),
                          sampling_rate=self.input.sampling_rate,
                          time_unit=self.input.time_unit)
Beispiel #12
0
def get_time_series_inplane(coords,scan_file,
                                  f_c=0.01,up_sample_factor=[1,1,1],
                                  detrend=True,normalize=True,average=True,
                                  TR=None):
    
    """vista_get_time_series: Acquire a time series for a particular scan/ROI.
    
    Parameters 
    ---------- 
    coords: a list of arrays
        each array holds the X,Y,Z locations of an ROI
        (as represented in the Inplane)

    scan_file: string, full path to the analyze file of the scan

    TR: float the repetition time in the experiment
    
    up_sample_factor: float
       the ratio between the size of the inplane and the size of the gray
       (taking into account FOV and number of voxels in each
       dimension). Defaults to [1,1,1] - no difference 
      
    detrend: bool, optional
      whether to detrend the signal. Default to 'True'
      
    normalize: bool, optional
      whether to transform the signal into % signal change. Default to 'True'

    average: bool, optional
      whether to average the resulting signal

    Returns
    -------
    time_series: array, the resulting time_series
    Depending on the averaging flag, can have the dimensions 1*time-points or
    number-voxels*time-points.
    
    Notes
    -----

    The order of the operations on the time-series is:
    detrend(on a voxel-by-voxel basis) => normalize (on a voxel-by-voxel basis)
    => average (across voxels, on a time-point-by-time-point basis)

    """
    from nibabel import load
    
    #Get the nifti image object

    print 'Reading data from %s' %scan_file 
    data = load(scan_file).get_data() #if using nipy.io.imageformats.load

    #Adjusted the coordinates according to the ratio between the
    #sampling in the gray and the sampling in the inplane, move the
    #slice dimension to be the first one and change the indexing from
    #1-based to 0-based. The coord order is as it is in the input, so need to
    #make sure that it is correct on the input side. 
    
    this_data = data[np.round(coords[0]/up_sample_factor[0]).astype(int)-1,
                         np.round(coords[1]/up_sample_factor[1]).astype(int)-1,
                         np.round(coords[2]/up_sample_factor[2]).astype(int)-1]

    if normalize:
        this_data = tsu.percent_change(this_data)

    if average:
        this_data = np.mean(this_data,0)
        
    time_series = ts.TimeSeries(data=this_data,sampling_interval=TR)

    if detrend:
        F = ta.FilterAnalyzer(this_bold,lb=f_c)
        time_series = F.filtered_boxcar
        
    return time_series
Beispiel #13
0
def get_time_series_inplane(coords,
                            scan_file,
                            f_c=0.01,
                            up_sample_factor=[1, 1, 1],
                            detrend=True,
                            normalize=True,
                            average=True,
                            TR=None):
    """vista_get_time_series: Acquire a time series for a particular scan/ROI.
    
    Parameters 
    ---------- 
    coords: a list of arrays
        each array holds the X,Y,Z locations of an ROI
        (as represented in the Inplane)

    scan_file: string, full path to the analyze file of the scan

    TR: float the repetition time in the experiment
    
    up_sample_factor: float
       the ratio between the size of the inplane and the size of the gray
       (taking into account FOV and number of voxels in each
       dimension). Defaults to [1,1,1] - no difference 
      
    detrend: bool, optional
      whether to detrend the signal. Default to 'True'
      
    normalize: bool, optional
      whether to transform the signal into % signal change. Default to 'True'

    average: bool, optional
      whether to average the resulting signal

    Returns
    -------
    time_series: array, the resulting time_series
    Depending on the averaging flag, can have the dimensions 1*time-points or
    number-voxels*time-points.
    
    Notes
    -----

    The order of the operations on the time-series is:
    detrend(on a voxel-by-voxel basis) => normalize (on a voxel-by-voxel basis)
    => average (across voxels, on a time-point-by-time-point basis)

    """
    from nibabel import load

    #Get the nifti image object

    print 'Reading data from %s' % scan_file
    data = load(scan_file).get_data()  #if using nipy.io.imageformats.load

    #Adjusted the coordinates according to the ratio between the
    #sampling in the gray and the sampling in the inplane, move the
    #slice dimension to be the first one and change the indexing from
    #1-based to 0-based. The coord order is as it is in the input, so need to
    #make sure that it is correct on the input side.

    this_data = data[np.round(coords[0] / up_sample_factor[0]).astype(int) - 1,
                     np.round(coords[1] / up_sample_factor[1]).astype(int) - 1,
                     np.round(coords[2] / up_sample_factor[2]).astype(int) - 1]

    if normalize:
        this_data = tsu.percent_change(this_data)

    if average:
        this_data = np.mean(this_data, 0)

    time_series = ts.TimeSeries(data=this_data, sampling_interval=TR)

    if detrend:
        F = ta.FilterAnalyzer(this_bold, lb=f_c)
        time_series = F.filtered_boxcar

    return time_series
Beispiel #14
0
def time_series_from_nifti(nifti_im,coords,normalize=False,detrend=False,
                           average=False,f_c=0.01,TR=None):
    """ Make a time series from a Nifti file, provided coordinates into the
            Nifti file 

    Parameters
    ----------

    analyze_im: nifti.image.NiftiImage object
           an object derived from the Nifti file containing the time-series data

    coords: ndarray or list of ndarrays
           x,y,z (slice,inplane,inplane) coordinates of the ROI from which the
           time-series is to be derived. If the list has more than one such
           array, the t-series will have more than one row in the data, as many
           as there are coordinates in the total list. Averaging is done on
           each item in the list separately, such that if several ROIs are
           entered, averaging will be done on each one separately and the
           result will be a time-series with as many rows of data as different
           ROIs in the input 

    detrend: bool, optional
           whether to detrend the time-series . For now, we do box-car
           detrending, but in the future we will do real high-pass filtering

    normalize: bool, optional
           whether to convert the time-series values into % signal change (on a
           voxel-by-voxel level)

    average: bool, optional
           whether to average the time-series across the voxels in the ROI. In
           which case, self.data will be 1-d

    f_c: float, optional
        cut-off frequency for detrending

    TR: float, optional
        TR, if different from the one which can be extracted from the nifti
        file header

    Returns
    -------

    time-series object

        """
    #extract the data from the file: 
    nifti_data = nifti_im.asarray()
    
    #Per default read TR from file:
    if TR is None:
        TR = nifti_im.getRepetitionTime()/1000.0 #in msec - convert to seconds


    #If we got a list of coord arrays, we're happy. Otherwise, we want to force
    #our input to be a list:
    try:
        coords.shape #If it is an array, it has a shape, otherwise, we 
        #assume it's a list. If it's an array, we want to
        #make it into a list:
        coords = [coords]
    except: #If it's a list already, we don't need to do anything:
        pass

    #Make a list the size of the coords-list, with place-holder 0's
    data_out = list([0]) * len(coords)
    
    for c in xrange(len(coords)): 
        data_out[c] = nifti_data[:,coords[c][0],coords[c][1],coords[c][2]].T
        #Take the transpose in order to make time the last dimension
        
        if normalize:
            data_out[c] = tsu.percent_change(data_out[c])

        #Currently uses mrVista style box-car detrending, will eventually be
        #replaced by a filter:
    
        if detrend:
            data_out[c] = tsu.vista_detrend_tseries(data_out[c],TR,f_c)
            
        if average:
            data_out[c] = np.mean(data_out[c],0)

    #Convert this into the array with which the time-series object is
    #initialized:
    data_out = np.array(data_out)
        
    tseries = UniformTimeSeries(data_out,sampling_interval=TR)

    return tseries
Beispiel #15
0
def test_percent_change():
    x = np.array([[99,100,101],[4,5,6]])
    p = utils.percent_change(x)

    npt.assert_equal(x.shape,p.shape)
    npt.assert_almost_equal(p[0,2],1.0)