def test_set_obsm(adata):
    init_hash = joblib.hash(adata)

    dim0_size = np.random.randint(2, adata.shape[0] - 1)
    dim1_size = np.random.randint(1, 99)
    orig_obsm_val = adata.obsm["o"].copy()
    subset_idx = np.random.choice(adata.obs_names, dim0_size, replace=False)

    subset = adata[subset_idx, :]
    assert subset.is_view
    subset.obsm = dict(o=np.ones((dim0_size, dim1_size)))
    assert not subset.is_view
    assert np.all(orig_obsm_val == adata.obsm["o"])  # Checking for mutation
    assert np.all(subset.obsm["o"] == np.ones((dim0_size, dim1_size)))

    subset = adata[subset_idx, :]
    subset_hash = joblib.hash(subset)
    with pytest.raises(ValueError):
        subset.obsm = dict(o=np.ones((dim0_size + 1, dim1_size)))
    with pytest.raises(ValueError):
        subset.varm = dict(o=np.ones((dim0_size - 1, dim1_size)))
    assert subset_hash == joblib.hash(subset)

    # Only modification have been made to a view
    assert init_hash == joblib.hash(adata)
def __init__(self, root, cwd, kindpats, ctx=None, badfn=None):
        super(patternmatcher, self).__init__(root, cwd, badfn)
        # kindpats are already normalized to be relative to repo-root.
        # Can we use tree matcher?
        rules = _kindpatstoglobs(kindpats, recursive=False)
        fallback = True
        if rules is not None:
            try:
                matcher = treematcher(root, cwd, badfn=badfn, rules=rules)
                # Replace self to 'matcher'.
                self.__dict__ = matcher.__dict__
                self.__class__ = matcher.__class__
                fallback = False
            except ValueError:
                # for example, Regex("Compiled regex exceeds size limit of 10485760 bytes.")
                pass
        if fallback:
            self._prefix = _prefix(kindpats)
            self._pats, self.matchfn = _buildmatch(ctx, kindpats, "$", root)

        self._files = _explicitfiles(kindpats)