def test_gtab_and_shape_init(self):
     handler = DataHandler(self.gtab, spatial_shape=(2, 2, 1))
     dummy = 47
     assert (handler.gtab is self.gtab)
     assert (handler.data is None)
     npt.assert_allclose(handler.originalShape, self.data.shape)
     npt.assert_almost_equal(handler.qMagnitudeTransform(dummy), dummy)
     assert (handler.box_cox_lambda is None)
 def test_gtab_and_data_init(self):
     handler = DataHandler(self.gtab, data=self.data)
     dummy = 47
     assert (handler.gtab == self.gtab)
     npt.assert_allclose(handler.data, self.data)
     npt.assert_allclose(handler.originalShape, self.data.shape)
     npt.assert_almost_equal(handler.qMagnitudeTransform(dummy), dummy)
     assert (handler.box_cox_lambda is None)
    def test_inverse_box_cox(self):
        lmbda = 0
        handler = DataHandler(self.gtab, data=self.data, box_cox_lambda=lmbda)
        dataAfterInverse = inverseBoxCox(handler.y, lmbda)
        npt.assert_allclose(dataAfterInverse, handler.data.reshape(-1, 1))

        lmbda = 2
        handler = DataHandler(self.gtab, data=self.data, box_cox_lambda=lmbda)
        dataAfterInverse = inverseBoxCox(handler.y, lmbda)
        npt.assert_allclose(dataAfterInverse, handler.data.reshape(-1, 1))
    def test_qFeatures(self):
        handler = DataHandler(self.gtab, data=self.data)
        expected = np.column_stack((self.gtab.qvals, self.gtab.bvecs))
        npt.assert_allclose(handler.X_q, expected)

        def f(q):
            return q**2

        handler = DataHandler(self.gtab, data=self.data, qMagnitudeTransform=f)
        expected = np.column_stack((self.gtab.qvals**2, self.gtab.bvecs))
        npt.assert_allclose(handler.X_q, expected)
    def test_box_cox(self):
        lmbda = 2
        handler = DataHandler(self.gtab, data=self.data, box_cox_lambda=lmbda)
        expected = ((self.data**lmbda - 1) / lmbda).reshape(-1, 1)
        npt.assert_allclose(handler.y, expected)

        handler = DataHandler(self.gtab,
                              data=self.data,
                              spatialIdx=self.maskIdx,
                              box_cox_lambda=lmbda)

        expected = (self.data[self.maskIdx[0], self.maskIdx[1],
                              self.maskIdx[2], :]**lmbda - 1) / lmbda
        npt.assert_allclose(handler.y, expected)
Exemple #6
0
    def __init__(self,
                 model,
                 data,
                 mean=None,
                 voxel_size=None,
                 image_origin=None,
                 spatial_idx=None,
                 retrain=True,
                 restarts=False,
                 noise_variance=1.):
        self.model = model
        self.voxel_size = voxel_size
        self.spatial_shape = data.shape[:-1]
        self.data_handler = DataHandler(
            self.model.gtab,
            data=data,
            mean=mean,
            voxelSize=self.voxel_size,
            image_origin=image_origin,
            spatialIdx=spatial_idx,
            box_cox_lambda=self.model.box_cox_lambda,
            qMagnitudeTransform=self.model.q_magnitude_transform)

        self.GP_model = GPy.models.GPRegressionGrid(
            self.data_handler.X,
            self.data_handler.y,
            self.model.kernel,
            grid_dims=self.model.grid_dims)
        self.GP_model.Gaussian_noise.variance = noise_variance

        if retrain:
            self.train(restarts=restarts)
    def test_X(self):
        handler = DataHandler(self.gtab, data=self.data, voxelSize=(3, 2, 1))
        x = np.array([[0], [3]])
        y = np.array([[0], [2]])
        z = np.array([[0]])
        expected = [x, y, z, handler.X_q]

        npt.assert_array_almost_equal(handler.X[0], expected[0])
        npt.assert_array_almost_equal(handler.X[1], expected[1])
        npt.assert_array_almost_equal(handler.X[2], expected[2])
        npt.assert_array_almost_equal(handler.X[3], expected[3])
Exemple #8
0
    def predict(self,
                gtab_pred,
                mean=None,
                voxel_size=None,
                image_origin=None,
                spatial_idx=None,
                spatial_shape=None,
                compute_var=False):

        if voxel_size is None:
            voxel_size = self.voxel_size

        if spatial_shape is None:
            spatial_shape = self.spatial_shape

        data_handler_pred = DataHandler(
            gtab_pred,
            data=None,
            mean=mean,
            voxelSize=voxel_size,
            image_origin=image_origin,
            spatialIdx=spatial_idx,
            spatial_shape=spatial_shape,
            box_cox_lambda=self.model.box_cox_lambda,
            qMagnitudeTransform=self.model.q_magnitude_transform)

        if self.model.verbose:
            print("Prediction started.")

        if compute_var:
            mu, var = self.GP_model.predict_noiseless(data_handler_pred.X,
                                                      compute_var=compute_var)
            return [
                data_handler_pred.untransform(mu),
                data_handler_pred.untransform(var)
            ]
        else:
            mu = self.GP_model.predict_noiseless(data_handler_pred.X,
                                                 compute_var=compute_var)
            return data_handler_pred.untransform(mu)
    def test_X_with_2D_coordinates_and_offset(self):
        data_2D = self.data[:, :, 0, :]
        handler = DataHandler(self.gtab,
                              data=data_2D,
                              voxelSize=(3, 2),
                              image_origin=(1, 2))
        x = np.array([[1], [4]])
        y = np.array([[2], [4]])
        expected = [x, y, handler.X_q]

        npt.assert_array_almost_equal(handler.X[0], expected[0])
        npt.assert_array_almost_equal(handler.X[1], expected[1])
        npt.assert_array_almost_equal(handler.X[2], expected[2])
 def test_y_with_mean(self):
     mean = np.ones_like(self.data)
     handler = DataHandler(self.gtab, data=self.data, mean=mean)
     npt.assert_array_equal(handler.y, (self.data - mean).reshape(-1, 1))
 def test_y(self):
     handler = DataHandler(self.gtab, data=self.data)
     npt.assert_array_equal(handler.y, self.data.reshape(-1, 1))
 def test_spatialIdx(self):
     handler = DataHandler(self.gtab,
                           data=self.data,
                           spatialIdx=self.maskIdx)
     npt.assert_allclose(handler.spatialIdx, self.maskIdx)
     npt.assert_allclose(handler.y.shape, (2, 3))
    def test_untransform_mean(self):
        mean = np.ones_like(self.data)
        handler = DataHandler(self.gtab, data=self.data, mean=mean)

        npt.assert_allclose(handler.untransform(handler.y), self.data)
    def test_untransform_box_cox(self):
        lmbda = 2
        handler = DataHandler(self.gtab, data=self.data, box_cox_lambda=lmbda)

        npt.assert_allclose(handler.untransform(handler.y), self.data)
    def test_X_coordinates_ordering_matches_y(self):
        expected = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0]])

        handler = DataHandler(self.gtab, data=self.data)
        npt.assert_allclose(handler.X_coordinates, expected)