espnet2.fileio.npy_scp.NpyScpReader
espnet2.fileio.npy_scp.NpyScpReader
class espnet2.fileio.npy_scp.NpyScpReader(fname: Path | str)
Bases: Mapping
Reader class for a scp file of numpy files.
This class allows for reading numpy arrays from a specified scp (script) file, which contains key-path pairs, where each key maps to a numpy file. The keys can be used to retrieve the corresponding numpy arrays efficiently.
fname
The path to the scp file.
- Type: Path
data
A dictionary mapping keys to numpy file paths.
Type: dict
Parameters:fname (Union *[*Path , str ]) – The path to the scp file.
Returns: None
######### Examples
The scp file might look like this: : key1 /some/path/a.npy key2 /some/path/b.npy key3 /some/path/c.npy key4 /some/path/d.npy …
Usage: : ```python
reader = NpyScpReader('npy.scp') array = reader['key1']
You can check if a key exists and get the number of keys:
: ```python
>>> 'key1' in reader
True
>>> len(reader)
4
- Yields: None
- Raises:KeyError – If the specified key does not exist in the scp file.
NOTE
Ensure that the numpy files referenced in the scp file exist at the specified paths.
get_path(key)
Retrieve the file path associated with the given key.
This method returns the path of the numpy file that corresponds to the provided key in the SCP file. It allows users to access the storage location of the numpy array without loading the array into memory.
- Parameters:key (str) – The key associated with the desired numpy file.
- Returns: The file path of the numpy file corresponding to the key.
- Return type: str
- Raises:KeyError – If the key does not exist in the data.
######### Examples
>>> reader = NpyScpReader('npy.scp')
>>> path = reader.get_path('key1')
>>> print(path)
/some/path/a.npy
NOTE
Ensure that the key exists in the data before calling this method to avoid a KeyError.
keys()
Reader class for a SCP (script) file of numpy files.
This class allows users to read numpy arrays from a specified SCP file. The SCP file should contain lines with keys and corresponding paths to numpy files.
fname
The path to the SCP file.
- Type: Path
data
A dictionary mapping keys to file paths read from the SCP file.
Type: dict
Parameters:fname (Union *[*Path , str ]) – The path to the SCP file.
Returns: None
######### Examples
Suppose the SCP file npy.scp contains: : key1 /some/path/a.npy key2 /some/path/b.npy key3 /some/path/c.npy key4 /some/path/d.npy
You can read the numpy array for key1 as follows:
>>> reader = NpyScpReader('npy.scp')
>>> array = reader['key1']
- Raises:
- KeyError – If the specified key is not found in the SCP file.
- FileNotFoundError – If the numpy file corresponding to the key does not exist.