Exemple #1
0
 def _array_iter(self, keys_only, method, recurse):
     if self._version == 2:
         for key in sorted(listdir(self._store, self._path)):
             path = self._key_prefix + key
             assert not path.startswith("meta")
             if contains_array(self._store, path):
                 _key = key.rstrip("/")
                 yield _key if keys_only else (_key, self[key])
             elif recurse and contains_group(self._store, path):
                 group = self[key]
                 for i in getattr(group, method)(recurse=recurse):
                     yield i
     else:
         dir_name = meta_root + self._path
         array_sfx = '.array' + self._metadata_key_suffix
         for key in sorted(listdir(self._store, dir_name)):
             if key.endswith(array_sfx):
                 key = key[:-len(array_sfx)]
             path = self._key_prefix + key
             assert not path.startswith("meta")
             if key.endswith('.group' + self._metadata_key_suffix):
                 # skip group metadata keys
                 continue
             if contains_array(self._store, path):
                 _key = key.rstrip("/")
                 yield _key if keys_only else (_key, self[key])
             elif recurse and contains_group(self._store, path):
                 group = self[key]
                 for i in getattr(group, method)(recurse=recurse):
                     yield i
Exemple #2
0
    def group_keys(self):
        """Return an iterator over member names for groups only.

        Examples
        --------
        >>> import zarr
        >>> g1 = zarr.group()
        >>> g2 = g1.create_group('foo')
        >>> g3 = g1.create_group('bar')
        >>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
        >>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
        >>> sorted(g1.group_keys())
        ['bar', 'foo']

        """
        if self._version == 2:
            for key in sorted(listdir(self._store, self._path)):
                path = self._key_prefix + key
                if contains_group(self._store, path):
                    yield key
        else:
            dir_name = meta_root + self._path
            group_sfx = '.group' + self._metadata_key_suffix
            for key in sorted(listdir(self._store, dir_name)):
                if key.endswith(group_sfx):
                    key = key[:-len(group_sfx)]
                path = self._key_prefix + key
                if path.endswith(".array" + self._metadata_key_suffix):
                    # skip array keys
                    continue
                if contains_group(self._store, path, explicit_only=False):
                    yield key
Exemple #3
0
    def arrays(self):
        """Return an iterator over (name, value) pairs for arrays only.

        Examples
        --------
        >>> import zarr
        >>> g1 = zarr.group()
        >>> g2 = g1.create_group('foo')
        >>> g3 = g1.create_group('bar')
        >>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
        >>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
        >>> for n, v in g1.arrays():
        ...     print(n, type(v))
        baz <class 'zarr.core.Array'>
        quux <class 'zarr.core.Array'>

        """
        for key in sorted(listdir(self._store, self._path)):
            path = self._key_prefix + key
            if contains_array(self._store, path):
                yield key, Array(self._store,
                                 path=path,
                                 read_only=self._read_only,
                                 chunk_store=self._chunk_store,
                                 cache_attrs=self.attrs.cache,
                                 synchronizer=self._synchronizer)
Exemple #4
0
    def groups(self):
        """Return an iterator over (name, value) pairs for groups only.

        Examples
        --------
        >>> import zarr
        >>> g1 = zarr.group()
        >>> g2 = g1.create_group('foo')
        >>> g3 = g1.create_group('bar')
        >>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
        >>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
        >>> for n, v in g1.groups():
        ...     print(n, type(v))
        bar <class 'zarr.hierarchy.Group'>
        foo <class 'zarr.hierarchy.Group'>

        """
        if self._version == 2:
            for key in sorted(listdir(self._store, self._path)):
                path = self._key_prefix + key
                if contains_group(self._store, path, explicit_only=False):
                    yield key, Group(self._store,
                                     path=path,
                                     read_only=self._read_only,
                                     chunk_store=self._chunk_store,
                                     cache_attrs=self.attrs.cache,
                                     synchronizer=self._synchronizer,
                                     zarr_version=self._version)

        else:
            dir_name = meta_root + self._path
            group_sfx = '.group' + self._metadata_key_suffix
            for key in sorted(listdir(self._store, dir_name)):
                if key.endswith(group_sfx):
                    key = key[:-len(group_sfx)]
                path = self._key_prefix + key
                if path.endswith(".array" + self._metadata_key_suffix):
                    # skip array keys
                    continue
                if contains_group(self._store, path, explicit_only=False):
                    yield key, Group(self._store,
                                     path=path,
                                     read_only=self._read_only,
                                     chunk_store=self._chunk_store,
                                     cache_attrs=self.attrs.cache,
                                     synchronizer=self._synchronizer,
                                     zarr_version=self._version)
Exemple #5
0
 def _array_iter(self, keys_only, method, recurse):
     for key in sorted(listdir(self._store, self._path)):
         path = self._key_prefix + key
         if contains_array(self._store, path):
             yield key if keys_only else (key, self[key])
         elif recurse and contains_group(self._store, path):
             group = self[key]
             for i in getattr(group, method)(recurse=recurse):
                 yield i
Exemple #6
0
    def __iter__(self):
        """Return an iterator over group member names.

        Examples
        --------
        >>> import zarr
        >>> g1 = zarr.group()
        >>> g2 = g1.create_group('foo')
        >>> g3 = g1.create_group('bar')
        >>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
        >>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
        >>> for name in g1:
        ...     print(name)
        bar
        baz
        foo
        quux

        """
        if getattr(self._store, '_store_version', 2) == 2:
            for key in sorted(listdir(self._store, self._path)):
                path = self._key_prefix + key
                if (contains_array(self._store, path)
                        or contains_group(self._store, path)):
                    yield key
        else:
            # TODO: Should this iterate over data folders and/or metadata
            #       folders and/or metadata files

            dir_path = meta_root + self._key_prefix
            name_start = len(dir_path)
            keys, prefixes = self._store.list_dir(dir_path)

            # yield any groups or arrays
            sfx = self._metadata_key_suffix
            for key in keys:
                len_suffix = len('.group') + len(sfx)  # same for .array
                if key.endswith(('.group' + sfx, '.array' + sfx)):
                    yield key[name_start:-len_suffix]

            # also yield any implicit groups
            for prefix in prefixes:
                prefix = prefix.rstrip('/')
                # only implicit if there is no .group.sfx file
                if not prefix + '.group' + sfx in self._store:
                    yield prefix[name_start:]
Exemple #7
0
    def array_keys(self):
        """Return an iterator over member names for arrays only.

        Examples
        --------
        >>> import zarr
        >>> g1 = zarr.group()
        >>> g2 = g1.create_group('foo')
        >>> g3 = g1.create_group('bar')
        >>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
        >>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
        >>> sorted(g1.array_keys())
        ['baz', 'quux']

        """
        for key in sorted(listdir(self._store, self._path)):
            path = self._key_prefix + key
            if contains_array(self._store, path):
                yield key
Exemple #8
0
    def array_keys(self):
        """Return an iterator over member names for arrays only.

        Examples
        --------
        >>> import zarr
        >>> g1 = zarr.group()
        >>> g2 = g1.create_group('foo')
        >>> g3 = g1.create_group('bar')
        >>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
        >>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
        >>> sorted(g1.array_keys())
        ['baz', 'quux']

        """
        for key in sorted(listdir(self._store, self._path)):
            path = self._key_prefix + key
            if contains_array(self._store, path):
                yield key
Exemple #9
0
    def arrays(self):
        """Return an iterator over (name, value) pairs for arrays only.

        Examples
        --------
        >>> import zarr
        >>> g1 = zarr.group()
        >>> g2 = g1.create_group('foo')
        >>> g3 = g1.create_group('bar')
        >>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
        >>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
        >>> for n, v in g1.arrays():
        ...     print(n, type(v))
        baz <class 'zarr.core.Array'>
        quux <class 'zarr.core.Array'>

        """
        for key in sorted(listdir(self._store, self._path)):
            path = self._key_prefix + key
            if contains_array(self._store, path):
                yield key, Array(self._store, path=path, read_only=self._read_only,
                                 chunk_store=self._chunk_store,
                                 synchronizer=self._synchronizer)
Exemple #10
0
    def __iter__(self):
        """Return an iterator over group member names.

        Examples
        --------
        >>> import zarr
        >>> g1 = zarr.group()
        >>> g2 = g1.create_group('foo')
        >>> g3 = g1.create_group('bar')
        >>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
        >>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
        >>> for name in g1:
        ...     print(name)
        bar
        baz
        foo
        quux

        """
        for key in sorted(listdir(self._store, self._path)):
            path = self._key_prefix + key
            if (contains_array(self._store, path)
                    or contains_group(self._store, path)):
                yield key
Exemple #11
0
    def __iter__(self):
        """Return an iterator over group member names.

        Examples
        --------
        >>> import zarr
        >>> g1 = zarr.group()
        >>> g2 = g1.create_group('foo')
        >>> g3 = g1.create_group('bar')
        >>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
        >>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
        >>> for name in g1:
        ...     print(name)
        bar
        baz
        foo
        quux

        """
        for key in sorted(listdir(self._store, self._path)):
            path = self._key_prefix + key
            if (contains_array(self._store, path) or
                    contains_group(self._store, path)):
                yield key
Exemple #12
0
 def nchunks_initialized(self):
     """The number of chunks that have been initialized with some data."""
     return sum(1 for k in listdir(self._chunk_store, self._path)
                if k not in [array_meta_key, attrs_key])
Exemple #13
0
 def nchunks_initialized(self):
     """The number of chunks that have been initialized with some data."""
     return sum(1 for k in listdir(self._chunk_store, self._path)
                if k not in [array_meta_key, attrs_key])