def test_design_matrix():
    """
    Testing that our assumptions about the design matrix are correct
    """
    data = (np.random.rand(10 * 10 * 10).reshape(10 * 10 * 10, 1) +
            np.zeros((10 * 10 * 10, 160))).reshape(10, 10, 10,160)

    mask_array = np.zeros(ni.load(data_path+'small_dwi.nii.gz').shape[:3])
    # Only two voxels:
    mask_array[1:3, 1:3, 1:3] = 1

    SSD = SparseDeconvolutionModel(data,
                                   data_path + 'dwi.bvecs',
                                   data_path + 'dwi.bvals',
                                   mask=mask_array,
        params_file=tempfile.NamedTemporaryFile().name)

    all_bvecs = SSD.bvecs[:, SSD.b_idx]
    all_bvals = SSD.bvals[SSD.b_idx]
    axial_diffusivity=AD
    radial_diffusivity=RD

    for i, this_bvec in enumerate(all_bvecs.T):
        my_tensor = ozt.rotate_to_vector(this_bvec, [axial_diffusivity,
                                                 radial_diffusivity,
                                                 radial_diffusivity],
            np.eye(3), all_bvecs, all_bvals)

        pred_sig = my_tensor.predicted_signal(1)
        pred_sig = pred_sig - np.mean(pred_sig)

        # The ith column of the matrix should be the demeaned response function
        # of a tensor pointing in this direction:
        npt.assert_array_equal(pred_sig, SSD.design_matrix[:, i])
Esempio n. 2
0
    def _calc_rotations(self, vertices, mode=None):
        """
        Given the rot_vecs of the object and a set of vertices (for the fitting
        these are the b-vectors of the measurement), calculate the rotations to
        be used as a design matrix

        """
        # unless we ask to change it, just use the mode of the object
        if mode is None:
            mode = self.mode

        out = np.empty((self.rot_vecs.shape[-1], vertices.shape[-1]))
        
        # We will use the eigen-value/vectors from the response function
        # and rotate them around to each one of these vectors, calculating
        # the predicted signal in the bvecs of the actual measurement (even
        # when over-sampling):

        # If we have as many vertices as b-vectors, we can take the
        # b-values from the measurement
        if vertices.shape[0] == len(self.b_idx): 
            bvals = self.bvals[self.b_idx]
        # Otherwise, we have to assume a constant b-value
        else:
            bvals = np.ones(vertices.shape[-1]) * self.bvals[self.b_idx][0]
            
        evals, evecs = self.response_function.decompose
        for idx, bvec in enumerate(self.rot_vecs.T):
            this_rot = ozt.rotate_to_vector(bvec, evals, evecs,
                                            vertices, bvals)
            pred_sig = this_rot.predicted_signal(1) 

            if mode == 'distance':
                # This is the special case where we use the diffusion distance
                # calculation, instead of the predicted signal:
                out[idx] = this_rot.diffusion_distance
            elif mode == 'ADC':
                # This is another special case, calculating the ADC instead of
                # using the predicted signal: 
                out[idx] = this_rot.ADC
            # Otherwise, we do one of these with the predicted signal: 
            elif mode == 'signal_attenuation':
                # Fit to 1 - S/S0 
                out[idx] = 1 - pred_sig
            elif mode == 'relative_signal':
                # Fit to S/S0 using the predicted diffusion attenuated signal:
                out[idx] = pred_sig
            elif mode == 'normalize':
                # Normalize your regressors to have a maximum of 1:
                out[idx] = pred_sig / np.max(pred_sig)
            elif mode == 'log':
                # Take the log and divide out the b value:
                out[idx] = np.log(pred_sig)
            
        return out
Esempio n. 3
0
    def _calc_rotations(self, vertices, mode=None):
        """
        Given the rot_vecs of the object and a set of vertices (for the fitting
        these are the b-vectors of the measurement), calculate the rotations to
        be used as a design matrix

        """
        # unless we ask to change it, just use the mode of the object
        if mode is None:
            mode = self.mode

        out = np.empty((self.rot_vecs.shape[-1], vertices.shape[-1]))

        # We will use the eigen-value/vectors from the response function
        # and rotate them around to each one of these vectors, calculating
        # the predicted signal in the bvecs of the actual measurement (even
        # when over-sampling):

        # If we have as many vertices as b-vectors, we can take the
        # b-values from the measurement
        if vertices.shape[0] == len(self.b_idx):
            bvals = self.bvals[self.b_idx]
        # Otherwise, we have to assume a constant b-value
        else:
            bvals = np.ones(vertices.shape[-1]) * self.bvals[self.b_idx][0]

        evals, evecs = self.response_function.decompose
        for idx, bvec in enumerate(self.rot_vecs.T):
            this_rot = ozt.rotate_to_vector(bvec, evals, evecs, vertices,
                                            bvals)
            pred_sig = this_rot.predicted_signal(1)

            if mode == 'distance':
                # This is the special case where we use the diffusion distance
                # calculation, instead of the predicted signal:
                out[idx] = this_rot.diffusion_distance
            elif mode == 'ADC':
                # This is another special case, calculating the ADC instead of
                # using the predicted signal:
                out[idx] = this_rot.ADC
            # Otherwise, we do one of these with the predicted signal:
            elif mode == 'signal_attenuation':
                # Fit to 1 - S/S0
                out[idx] = 1 - pred_sig
            elif mode == 'relative_signal':
                # Fit to S/S0 using the predicted diffusion attenuated signal:
                out[idx] = pred_sig
            elif mode == 'normalize':
                # Normalize your regressors to have a maximum of 1:
                out[idx] = pred_sig / np.max(pred_sig)
            elif mode == 'log':
                # Take the log and divide out the b value:
                out[idx] = np.log(pred_sig)

        return out
def test_rotations():
    evals_t, evecs_t = test_response_function()
    out_t = np.empty((5, 1))

    for idx, bvec in enumerate(rot_vecs_t[:,3:8].T):
        this_rot_t = ozt.rotate_to_vector(bvec, evals_t, evecs_t,
                                        np.reshape(bvecs_t[:,4], (3,1)),
                                        np.array([bvals_scaled_t[4]/1000]))
        
        out_t[idx] = this_rot_t.predicted_signal(1)
        
    npt.assert_equal(out_t, mb._calc_rotations(np.reshape(bvecs_t[:,4], (3,1)),
                                               bvals_scaled_t[4]/1000))
    
    return out_t
Esempio n. 5
0
def test_rotations():
    evals_t, evecs_t = test_response_function()
    out_t = np.empty((5, 1))

    for idx, bvec in enumerate(rot_vecs_t[:, 3:8].T):
        this_rot_t = ozt.rotate_to_vector(bvec, evals_t, evecs_t,
                                          np.reshape(bvecs_t[:, 4], (3, 1)),
                                          np.array([bvals_scaled_t[4] / 1000]))

        out_t[idx] = this_rot_t.predicted_signal(1)

    npt.assert_equal(
        out_t,
        mb._calc_rotations(np.reshape(bvecs_t[:, 4], (3, 1)),
                           bvals_scaled_t[4] / 1000))

    return out_t