Skip to content

Interface npz

pynapple.io.interface_npz

NPZFile

Bases: object

Class that points to a NPZ file that can be loaded as a pynapple object. Objects have a save function in npz format as well as the Folder class.

Examples:

>>> import pynapple as nap
>>> tsd = nap.load_file("path/to/my_tsd.npz")
>>> tsd
Time (s)
0.0    0
0.1    1
0.2    2
dtype: int64
Source code in pynapple/io/interface_npz.py
class NPZFile(object):
    """Class that points to a NPZ file that can be loaded as a pynapple object.
    Objects have a save function in npz format as well as the Folder class.

    Examples
    --------
    >>> import pynapple as nap
    >>> tsd = nap.load_file("path/to/my_tsd.npz")
    >>> tsd
    Time (s)
    0.0    0
    0.1    1
    0.2    2
    dtype: int64

    """

    def __init__(self, path):
        """Initialization of the NPZ file

        Parameters
        ----------
        path : str
            Valid path to a NPZ file
        """
        path = Path(path)
        self.path = path
        self.name = path.name
        self.file = np.load(self.path, allow_pickle=True)
        type_ = ""

        # First check if type is explicitely defined in the file:
        try:
            type_ = self.file["type"][0]
            assert type_ in EXPECTED_ENTRIES.keys()

        # if not, use heuristics:
        except (KeyError, IndexError, AssertionError):
            file_variables = set(self.file.keys())
            data_ndims = self.file["d"].ndim if "d" in file_variables else None

            type_ = _find_class_from_variables(file_variables, data_ndims)

        self.type = type_

    def load(self):
        """Load the NPZ file

        Returns
        -------
        (Tsd, Ts, TsdFrame, TsdTensor, TsGroup, IntervalSet)
            A pynapple object
        """
        if self.type == "npz":
            return self.file

        return getattr(nap, self.type)._from_npz_reader(self.file)

__init__

__init__(path)

Initialization of the NPZ file

Parameters:

Name Type Description Default
path str

Valid path to a NPZ file

required
Source code in pynapple/io/interface_npz.py
def __init__(self, path):
    """Initialization of the NPZ file

    Parameters
    ----------
    path : str
        Valid path to a NPZ file
    """
    path = Path(path)
    self.path = path
    self.name = path.name
    self.file = np.load(self.path, allow_pickle=True)
    type_ = ""

    # First check if type is explicitely defined in the file:
    try:
        type_ = self.file["type"][0]
        assert type_ in EXPECTED_ENTRIES.keys()

    # if not, use heuristics:
    except (KeyError, IndexError, AssertionError):
        file_variables = set(self.file.keys())
        data_ndims = self.file["d"].ndim if "d" in file_variables else None

        type_ = _find_class_from_variables(file_variables, data_ndims)

    self.type = type_

load

load()

Load the NPZ file

Returns:

Type Description
(Tsd, Ts, TsdFrame, TsdTensor, TsGroup, IntervalSet)

A pynapple object

Source code in pynapple/io/interface_npz.py
def load(self):
    """Load the NPZ file

    Returns
    -------
    (Tsd, Ts, TsdFrame, TsdTensor, TsGroup, IntervalSet)
        A pynapple object
    """
    if self.type == "npz":
        return self.file

    return getattr(nap, self.type)._from_npz_reader(self.file)