def test_setting_dim_index(dim): index_attr = f"{dim}_names" mapping_attr = f"{dim}m" orig = gen_adata((5, 5)) orig.raw = orig curr = orig.copy() view = orig[:, :] new_idx = pd.Index(list("abcde"), name="letters") setattr(curr, index_attr, new_idx) pd.testing.assert_index_equal(getattr(curr, index_attr), new_idx) pd.testing.assert_index_equal( getattr(curr, mapping_attr)["df"].index, new_idx) pd.testing.assert_index_equal(curr.obs_names, curr.raw.obs_names) # Testing view behaviour setattr(view, index_attr, new_idx) assert not view.is_view pd.testing.assert_index_equal(getattr(view, index_attr), new_idx) pd.testing.assert_index_equal( getattr(view, mapping_attr)["df"].index, new_idx) with pytest.raises(AssertionError): pd.testing.assert_index_equal(getattr(view, index_attr), getattr(orig, index_attr)) assert_equal(view, curr, exact=True)
def test_attr_deletion(): full = gen_adata((30, 30)) # Empty has just X, obs_names, var_names empty = AnnData(full.X, obs=full.obs[[]], var=full.var[[]]) for attr in ["obs", "var", "obsm", "varm", "obsp", "varp", "layers"]: delattr(full, attr) assert_equal(getattr(full, attr), getattr(empty, attr)) assert_equal(full, empty, exact=True)