def test_view_mixin_copies_data(adata, array_type: type, attr):
    N = 100
    adata = ad.AnnData(
        obs=pd.DataFrame(index=np.arange(N)), var=pd.DataFrame(index=np.arange(N))
    )

    X = array_type(sparse.eye(N, N).multiply(np.arange(1, N + 1)))
    if attr == "X":
        adata.X = X
    else:
        getattr(adata, attr)["arr"] = X

    view = adata[:50]

    if attr == "X":
        arr_view = view.X
    else:
        arr_view = getattr(view, attr)["arr"]

    arr_view_copy = arr_view.copy()

    if sparse.issparse(X):
        assert not np.shares_memory(arr_view.indices, arr_view_copy.indices)
        assert not np.shares_memory(arr_view.indptr, arr_view_copy.indptr)
        assert not np.shares_memory(arr_view.data, arr_view_copy.data)

        arr_view_copy.data[0] = -5
        assert not np.array_equal(arr_view_copy.data, arr_view.data)
    else:
        assert not np.shares_memory(arr_view, arr_view_copy)

        arr_view_copy[0, 0] = -5
        assert not np.array_equal(arr_view_copy, arr_view)
def test_sgpr_mean_abs_error_cuda(self):
        # Suppress numerical warnings
        warnings.simplefilter("ignore", NumericalWarning)

        if not torch.cuda.is_available():
            return
        with least_used_cuda_device():
            train_x, train_y, test_x, test_y = make_data(cuda=True)
            likelihood = GaussianLikelihood().cuda()
            gp_model = GPRegressionModel(train_x, train_y, likelihood).cuda()
            mll = gpytorch.mlls.ExactMarginalLogLikelihood(likelihood, gp_model)

            # Optimize the model
            gp_model.train()
            likelihood.train()

            optimizer = optim.Adam(gp_model.parameters(), lr=0.1)
            optimizer.n_iter = 0
            for _ in range(25):
                optimizer.zero_grad()
                output = gp_model(train_x)
                loss = -mll(output, train_y)
                loss.backward()
                optimizer.n_iter += 1
                optimizer.step()

            for param in gp_model.parameters():
                self.assertTrue(param.grad is not None)
                self.assertGreater(param.grad.norm().item(), 0)

            # Test the model
            gp_model.eval()
            likelihood.eval()
            test_preds = likelihood(gp_model(test_x)).mean
            mean_abs_error = torch.mean(torch.abs(test_y - test_preds))

            self.assertLess(mean_abs_error.squeeze().item(), 0.02)