示例#1
0
    def test_spline_detrend_plotting(self):
        """
        Tests the plotting of the spline detrend operation.
        """
        tr = obspy.read()[0].filter("highpass", freq=2)
        tr.data += 6000 + 4 * tr.times() ** 2 - 0.1 * tr.times() ** 3 - \
            0.00001 * tr.times() ** 5

        # Use an first order spline to see a difference to the polynomial
        # picture.
        with ImageComparison(self.path_images,
                             'degree_1_spline_detrend.png') as ic:
            spline(tr.data, order=1, dspline=1500, plot=ic.name)
示例#2
0
    def test_spline_detrend_plotting(self):
        """
        Tests the plotting of the spline detrend operation.
        """
        tr = obspy.read()[0].filter("highpass", freq=2)
        tr.data += 6000 + 4 * tr.times() ** 2 - 0.1 * tr.times() ** 3 - \
            0.00001 * tr.times() ** 5

        # Use an first order spline to see a difference to the polynomial
        # picture.
        with ImageComparison(self.path_images,
                             'degree_1_spline_detrend.png') as ic:
            spline(tr.data, order=1, dspline=1500, plot=ic.name)
示例#3
0
    def test_spline_detrend(self):
        """
        Simple test for the spline detrending.
        """
        coeffs = [(1, 2, 3), (2, -4), (-3, 2, -5, 15), (-10, 20, -1, 2, 15)]
        data = np.linspace(-5, 5, 100)

        for c in coeffs:
            # Create data.
            d = np.polyval(c, data)
            original_ptp = np.ptp(d)
            # Detrend with a spline of the same order as the polynomial.
            # This should be very very similar.
            detrended = spline(d, order=len(c) - 1, dspline=10)
            # Not as good as for the polynomial detrending.
            self.assertLess(np.ptp(detrended) * 1E4, original_ptp)
示例#4
0
    def test_spline_detrend(self):
        """
        Simple test for the spline detrending.
        """
        coeffs = [(1, 2, 3), (2, -4), (-3, 2, -5, 15), (-10, 20, -1, 2, 15)]
        data = np.linspace(-5, 5, 100)

        for c in coeffs:
            # Create data.
            d = np.polyval(c, data)
            original_ptp = np.ptp(d)
            # Detrend with a spline of the same order as the polynomial.
            # This should be very very similar.
            detrended = spline(d, order=len(c) - 1, dspline=10)
            # Not as good as for the polynomial detrending.
            self.assertLess(np.ptp(detrended) * 1E4, original_ptp)
示例#5
0
def xcorr2(tr1,
           tr2,
           window_seconds=3600,
           window_overlap=0,
           interval_seconds=86400,
           flo=0.9,
           fhi=1.1,
           clip_to_2std=False,
           one_bit_normalize=False,
           verbose=1,
           logger=None):
    sr1 = tr1.stats.sampling_rate
    sr2 = tr2.stats.sampling_rate
    tr1_d_all = tr1.data  # refstn
    tr2_d_all = tr2.data
    lentr1_all = tr1_d_all.shape[0]
    lentr2_all = tr2_d_all.shape[0]
    window_samples_1 = window_seconds * sr1
    window_samples_2 = window_seconds * sr2
    interval_samples_1 = interval_seconds * sr1
    interval_samples_2 = interval_seconds * sr2
    itr1s = 0
    itr2s = 0
    resll = []

    intervalCount = 0
    windowsPerInterval = [
    ]  # Stores the number of windows processed per interval
    intervalStartSeconds = []
    intervalEndSeconds = []
    while itr1s < lentr1_all and itr2s < lentr2_all:
        itr1e = min(lentr1_all, itr1s + interval_samples_1)
        itr2e = min(lentr2_all, itr2s + interval_samples_2)
        sr = max(sr1, sr2)
        xcorlen = int(2 * window_seconds * sr - 1)
        fftlen = 2**(int(np.log2(xcorlen)) + 1)

        windowCount = 0
        wtr1s = int(itr1s)
        wtr2s = int(itr2s)
        resl = []

        while wtr1s < itr1e and wtr2s < itr2e:
            wtr1e = int(min(itr1e, wtr1s + window_samples_1))
            wtr2e = int(min(itr2e, wtr2s + window_samples_2))

            # Discard small windows
            if wtr1e - wtr1s < window_samples_1 or wtr2e - wtr2s < window_samples_2:
                wtr1s = wtr1e
                wtr2s = wtr2e
                continue
            # end if

            # Discard windows with masked regions, i.e. with gaps
            if (not (np.ma.is_masked(tr1_d_all[wtr1s:wtr1e])
                     or np.ma.is_masked(tr2_d_all[wtr2s:wtr2e]))):

                tr1_d = np.array(tr1_d_all[wtr1s:wtr1e], dtype=np.float32)
                tr2_d = np.array(tr2_d_all[wtr2s:wtr2e], dtype=np.float32)

                # detrend
                tr1_d = spline(tr1_d, 2, 1000)
                tr2_d = spline(tr2_d, 2, 1000)

                # zero-mean
                tr1_d -= np.mean(tr1_d)
                tr2_d -= np.mean(tr2_d)

                # clip to +/- 2*std
                if (clip_to_2std):
                    std_tr1 = np.std(tr1_d)
                    std_tr2 = np.std(tr2_d)
                    clip_indices_tr1 = np.fabs(tr1_d) > 2 * std_tr1
                    clip_indices_tr2 = np.fabs(tr2_d) > 2 * std_tr2

                    tr1_d[clip_indices_tr1] = 2 * std_tr1 * np.sign(
                        tr1_d[clip_indices_tr1])
                    tr2_d[clip_indices_tr2] = 2 * std_tr2 * np.sign(
                        tr2_d[clip_indices_tr2])
                # end if

                # apply zero-phase band-pass
                tr1_d = bandpass(tr1_d, flo, fhi, sr1, zerophase=True)
                tr2_d = bandpass(tr2_d, flo, fhi, sr2, zerophase=True)

                # taper
                #tr1_d = taper(tr1_d, int(sr1 / flo))
                #tr2_d = taper(tr2_d, int(sr2 / flo))

                # 1-bit normalization
                if (one_bit_normalize):
                    tr1_d = np.sign(tr1_d)
                    tr2_d = np.sign(tr2_d)
                # end if

                if (sr1 < sr2):
                    fftlen2 = fftlen
                    fftlen1 = int((fftlen2 * 1.0 * sr1) / sr)
                    outdims2 = np.array([fftlen2])
                    outdims1 = np.array([fftlen1])
                    rf = zeropad_ba(
                        fftn(zeropad(tr1_d, fftlen1), shape=outdims1),
                        fftlen2) * fftn(zeropad(ndflip(tr2_d), fftlen2),
                                        shape=outdims2)
                elif (sr1 > sr2):
                    fftlen1 = fftlen
                    fftlen2 = int((fftlen1 * 1.0 * sr2) / sr)
                    outdims2 = np.array([fftlen2])
                    outdims1 = np.array([fftlen1])
                    rf = fftn(zeropad(tr1_d, fftlen1),
                              shape=outdims1) * zeropad_ba(
                                  fftn(zeropad(ndflip(tr2_d), fftlen2),
                                       shape=outdims2), fftlen1)
                else:
                    fftlen = 2**(int(np.log2(2 * window_samples_1 - 1)) + 1)
                    outdims = np.array([fftlen])
                    rf = fftn(zeropad(tr1_d, fftlen), shape=outdims) * fftn(
                        zeropad(ndflip(tr2_d), fftlen), shape=outdims)
                # end if

                resl.append(rf)
                windowCount += 1
            # end if

            wtr1s += int(window_samples_1 - window_samples_1 * window_overlap)
            wtr2s += int(window_samples_2 - window_samples_2 * window_overlap)
        # end while (windows within interval)

        if (verbose > 1):
            if (logger):
                logger.info('\tProcessed %d windows in interval %d' %
                            (windowCount, intervalCount))
        # end fi

        if (np.fabs(itr1e - itr1s) < sr1):
            itr1s = itr1e
            itr2s = itr2e
            continue
        # end if

        intervalStartSeconds.append(itr1s / sr1)
        intervalEndSeconds.append(itr1e / sr1)
        itr1s = itr1e
        itr2s = itr2e
        intervalCount += 1

        # Append an array of zeros if no windows were processed for the current interval
        if (windowCount == 0):
            resl.append(np.zeros(fftlen))
            if (verbose == 1):
                if (logger):
                    logger.info(
                        '\tWarning: No windows processed due to gaps in data in current interval'
                    )
            # end if
        # end if

        windowsPerInterval.append(windowCount)

        step = np.sign(np.fft.fftfreq(fftlen, 1.0 / sr))
        mean = reduce((lambda tx, ty: tx + ty), resl) / len(resl)
        mean = mean + step * mean  # compute analytic
        mean = ifftn(mean)

        # Compute magnitude of mean
        mean = np.abs(mean)
        normFactor = np.max(mean)

        # mean can be 0 for a null result
        if (normFactor > 0):
            mean /= normFactor
        #end if

        resll.append(mean[:xcorlen])
    # end while (iteration over intervals)

    if (len(resll)):
        return np.array(resll), np.array(windowsPerInterval), \
               np.array(intervalStartSeconds, dtype='i8'), \
               np.array(intervalEndSeconds, dtype='i8')
    else:
        return None, None, None, None
示例#6
0
    def _run_interface(self, runtime):
        print("Linear detrending")
        print("=================")

        # Output from previous preprocessing step
        ref_path = self.inputs.in_file

        # Load data
        dataimg = nib.load(ref_path)
        data = dataimg.get_data()
        tp = data.shape[3]

        # GLM: regress out nuisance covariates
        new_data_det = data.copy()
        gm = nib.load(self.inputs.gm_file[0]).get_data().astype(np.uint32)

        from scipy import signal

        for index, value in np.ndenumerate(gm):
            if value == 0:
                continue

            Ydet = signal.detrend(data[index[0], index[1],
                                       index[2], :].reshape(tp, 1),
                                  axis=0)
            new_data_det[index[0], index[1], index[2], :] = Ydet[:, 0]

        img = nib.Nifti1Image(new_data_det, dataimg.get_affine(),
                              dataimg.get_header())
        nib.save(img, os.path.abspath("fMRI_detrending.nii.gz"))

        if self.inputs.mode == "quadratic":
            print("Quadratic detrending")
            print("=================")
            from obspy.signal.detrend import polynomial

            # GLM: regress out nuisance covariates
            new_data_det2 = new_data_det.copy()
            for index, value in np.ndenumerate(gm):
                if value == 0:
                    continue
                Ydet = polynomial(new_data_det2[index[0], index[1],
                                                index[2], :],
                                  order=2)

            img = nib.Nifti1Image(new_data_det2, dataimg.get_affine(),
                                  dataimg.get_header())
            nib.save(img, os.path.abspath("fMRI_detrending.nii.gz"))

        if self.inputs.mode == "cubic":
            print("Cubic-spline detrending")
            print("=================")
            from obspy.signal.detrend import spline

            # GLM: regress out nuisance covariates
            new_data_det2 = new_data_det.copy()
            for index, value in np.ndenumerate(gm):
                if value == 0:
                    continue
                Ydet = spline(new_data_det2[index[0], index[1], index[2], :],
                              order=3)

            img = nib.Nifti1Image(new_data_det2, dataimg.get_affine(),
                                  dataimg.get_header())
            nib.save(img, os.path.abspath("fMRI_detrending.nii.gz"))

        print("[ DONE ]")
        return runtime