Example #1
0
    def fit(self, ys, paradigm, initial_beta=None):

        rng = check_random_state(self.random_state)

        ys = np.atleast_1d(ys)
        if self.normalize_y:
            self.y_train_mean = np.mean(ys, axis=0)
            ys = ys - self.y_train_mean
        else:
            self.y_train_mean = np.zeros(1)

        # Removing the drifts
        if self.remove_difts:
            names, onsets, durations, modulation = check_paradigm(paradigm)

            frame_times = np.arange(0, onsets.max() + self.time_offset, self.t_r)
            drifts, dnames = _make_drift(self.drift_model, frame_times,
                                         self.order, self.period_cut)
            ys -= drifts.dot(np.linalg.pinv(drifts).dot(ys))

        if self.zeros_extremes:
            if ys.ndim==2:
                ys = np.append(ys, np.zeros((2, ys.shape[1])), axis=0)
            else:
                ys = np.append(ys, np.zeros((2,)), axis=0)

        self.y_train = ys

        # Get paradigm data
        hrf_measurement_points, visible_events, etas, beta_indices, unique_events = \
            _get_hrf_measurements(paradigm, hrf_length=self.hrf_length,
                                  t_r=self.t_r, time_offset=self.time_offset,
                                  zeros_extremes=self.zeros_extremes,
                                  frame_times=frame_times)
        if initial_beta is None:
            initial_beta = np.ones(len(unique_events))

        # Just to be able to use Kernels class
        hrf_measurement_points = np.concatenate(hrf_measurement_points)
        self.hrf_measurement_points = hrf_measurement_points[:, np.newaxis]
        self.evaluation_points = None
        etas = np.concatenate(etas)

        # Initialize the kernel
        self.hrf_kernel = HRFKernel(kernel=self.kernel, gamma=self.gamma,
                                    return_eval_cov=self.return_var)
        self.hrf_kernel.set_params(**dict(
            beta_values=initial_beta, beta_indices=beta_indices, etas=etas))

        self.visible_events_ = visible_events
        self.unique_events_ = unique_events
        self.etas_ = etas
        self.beta_indices_ = beta_indices
        self.initial_beta_ = initial_beta

        # Maximizing the log-likelihood (gradient based optimization)
        self.f_mean_ = self.f_mean
        self.f_mean = None
        if self.optimize:

            def obj_func(theta):
                print theta
                return -self._fit(theta)[0]

            optima = [(self._constrained_optimization(
                obj_func, self.hrf_kernel.theta, self.hrf_kernel.bounds))]

            # Additional runs are performed from log-uniform chosen initial
            # theta
            if self.n_restarts_optimizer > 0:
                bounds = self.hrf_kernel.bounds
                for i in range(self.n_restarts_optimizer):
                    theta_initial = rng.uniform(bounds[:, 0], bounds[:, 1])
                    optima.append(self._constrained_optimization(
                        obj_func, theta_initial, bounds))
            # Select the best result
            # add logic to deal with nan and -inf
            lm_values = list(map(itemgetter(1), optima))
            self.theta_ = optima[np.argmin(lm_values)][0]
            self.hrf_kernel.theta = self.theta_
            self.log_marginal_likelihood_value_ = -np.min(lm_values)
            # Refit the model
            self.f_mean = self.f_mean_
            loglikelihood, output = self._fit(self.theta_)
        else:
            loglikelihood, output = self._fit(self.hrf_kernel.theta)

        hrf_measurement_points = np.concatenate(output[1][0])
        order = np.argsort(hrf_measurement_points)

        hrf_var = output[1][2][order]
        hx, hy = hrf_measurement_points[order], output[1][1][order]

        residual_norm_squared = output[2][0]
        sigma_squared_resid = output[2][1]

        self.beta = output[0]
        self.hx_ = hx
        self.hrf_ = hy
        self.hrf_var_ = hrf_var
        return (hx, hy, hrf_var, residual_norm_squared, sigma_squared_resid)
def make_design_matrix_hrf(
    frame_times, paradigm=None, hrf_length=32., t_r=2., time_offset=10,
    drift_model='cosine', period_cut=128, drift_order=1, fir_delays=[0],
    add_regs=None, add_reg_names=None, min_onset=-24, f_hrf=None):
    """Generate a design matrix from the input parameters

    Parameters
    ----------
    frame_times : array of shape (n_frames,)
        The timing of the scans in seconds.

    paradigm : DataFrame instance, optional
        Description of the experimental paradigm.

    drift_model : string, optional
        Specifies the desired drift model,
        It can be 'polynomial', 'cosine' or 'blank'.

    period_cut : float, optional
        Cut period of the low-pass filter in seconds.

    drift_order : int, optional
        Order of the drift model (in case it is polynomial).

    fir_delays : array of shape(n_onsets) or list, optional,
        In case of FIR design, yields the array of delays used in the FIR
        model.

    add_regs : array of shape(n_frames, n_add_reg), optional
        additional user-supplied regressors

    add_reg_names : list of (n_add_reg,) strings, optional
        If None, while n_add_reg > 0, these will be termed
        'reg_%i', i = 0..n_add_reg - 1

    min_onset : float, optional
        Minimal onset relative to frame_times[0] (in seconds)
        events that start before frame_times[0] + min_onset are not considered.

    Returns
    -------
    design_matrix : DataFrame instance,
        holding the computed design matrix
    """
    # check arguments
    # check that additional regressor specification is correct
    n_add_regs = 0
    if add_regs is not None:
        if add_regs.shape[0] == np.size(add_regs):
            add_regs = np.reshape(add_regs, (np.size(add_regs), 1))
        n_add_regs = add_regs.shape[1]
        assert add_regs.shape[0] == np.size(frame_times), ValueError(
            'incorrect specification of additional regressors: '
            'length of regressors provided: %s, number of ' +
            'time-frames: %s' % (add_regs.shape[0], np.size(frame_times)))

    # check that additional regressor names are well specified
    if add_reg_names is None:
        add_reg_names = ['reg%d' % k for k in range(n_add_regs)]
    elif len(add_reg_names) != n_add_regs:
        raise ValueError(
            'Incorrect number of additional regressor names was provided'
            '(%s provided, %s expected) % (len(add_reg_names),'
            'n_add_regs)')

    # computation of the matrix
    names = []
    matrix = None

    # step 1: paradigm-related regressors
    if paradigm is not None:
        # create the condition-related regressors
        names0, _, _, _ = check_paradigm(paradigm)
        names = np.append(names, np.unique(names0))
        hrf_measurement_points, _, _, beta_indices, _ = \
                    _get_hrf_measurements(paradigm, hrf_length=hrf_length,
                                          t_r=t_r, time_offset=time_offset, frame_times=frame_times)
        hrf_measurement_points = np.concatenate(hrf_measurement_points)
        hrf_measures = f_hrf(hrf_measurement_points)
        matrix = _get_design_from_hrf_measures(hrf_measures, beta_indices)
        #matrix, names = _convolve_regressors(
        #    paradigm, hrf_model.lower(), frame_times, fir_delays, min_onset)

    # step 2: additional regressors
    if add_regs is not None:
        # add user-supplied regressors and corresponding names
        if matrix is not None:
            matrix = np.hstack((matrix, add_regs))
        else:
            matrix = add_regs
        names = np.append(names, add_reg_names)

    # step 3: drifts
    drift, dnames = _make_drift(drift_model.lower(), frame_times, drift_order,
                                period_cut)

    if matrix is not None:
        matrix = np.hstack((matrix, drift))
    else:
        matrix = drift

    names = np.append(names, dnames)

    # step 4: Force the design matrix to be full rank at working precision
    matrix, _ = full_rank(matrix)

    design_matrix = pd.DataFrame(
        matrix, columns=list(names), index=frame_times)
    return design_matrix
        # Paradigm file
        paradigm_fn = op.join(folder0, 'onsets.csv')
        ########################################################################
        # Load data and parameters
        t_r = 3.
        ys_ = np.load(voxel_fn)
        ysr2_ = np.load(voxel_r2_fn)
        n_scans = ys.shape[0]

        # Create design matrix
        frametimes = np.arange(0, n_scans * t_r, t_r)
        paradigm = experimental_paradigm.paradigm_from_csv(paradigm_fn)
        dm = design_matrix.make_design_matrix(frametimes, paradigm=paradigm)
        modulation = np.array(paradigm)[:, 4]

        drifts = _make_drift('cosine', frame_times)
        ys = ys_ - drifts.dot(np.linalg.pinv(drifts).dot(ys_))
        ysr2 = ysr2_ - drifts.dot(np.linalg.pinv(drifts).dot(ysr2_))

        # GP parameters
        time_offset = 6
        gamma = 4
        fmin_max_iter = 50
        n_restarts_optimizer = 10
        n_iter = 10
        normalize_y = False
        optimize = False
        zeros_extremes = True

        # Estimation HRF on 1 run
        gp = SuperDuperGP(hrf_length=hrf_length, t_r=t_r, oversampling=1./dt,