espnet2.fileio.npy_scp.NpyScpWriter
espnet2.fileio.npy_scp.NpyScpWriter
class espnet2.fileio.npy_scp.NpyScpWriter(outdir: Path | str, scpfile: Path | str)
Bases: object
Writer class for creating a SCP file of numpy arrays.
This class allows users to write numpy arrays to a specified directory and generate a corresponding SCP file that maps keys to numpy file paths.
dir
The directory where numpy files will be saved.
- Type: Path
fscp
The file object for writing the SCP file.
- Type: TextIOWrapper
data
A dictionary to store the mapping of keys to numpy file paths.
Type: dict
Parameters:
- outdir (Union *[*Path , str ]) – The output directory for numpy files.
- scpfile (Union *[*Path , str ]) – The path for the SCP file to be created.
####### Examples
The SCP file will contain lines in the format: : key1 /some/path/a.npy key2 /some/path/b.npy key3 /some/path/c.npy key4 /some/path/d.npy …
>>> writer = NpyScpWriter('./data/', './data/feat.scp')
>>> writer['aa'] = numpy_array # Save 'numpy_array' to './data/aa.npy'
>>> writer['bb'] = numpy_array # Save 'numpy_array' to './data/bb.npy'
- Raises:AssertionError – If the value assigned is not a numpy ndarray.
NOTE
Ensure that the output directory and SCP file path are valid and writable.
close()
Closes the SCP file associated with the NpyScpWriter instance.
This method should be called to ensure that all data is properly flushed and the file is closed when writing operations are complete. It is automatically invoked when exiting the context manager.
Note: : It is important to call this method to prevent any data loss or corruption.
Examples: : ```python
writer = NpyScpWriter('./data/', './data/feat.scp') writer['aa'] = numpy_array writer['bb'] = numpy_array writer.close() # Ensure the SCP file is closed properly
Raises: : ValueError: If the SCP file is already closed when attempting to close it again.
get_path(key)
Retrieve the file path associated with the given key.
This method looks up the provided key in the internal data structure and returns the corresponding file path where the numpy array is stored.
- Parameters:key (str) – The key for which the file path needs to be retrieved.
- Returns: The file path corresponding to the given key.
- Return type: str
- Raises:KeyError – If the key is not found in the internal data structure.
####### Examples
>>> writer = NpyScpWriter('./data/', './data/feat.scp')
>>> writer['example_key'] = np.array([1, 2, 3])
>>> path = writer.get_path('example_key')
>>> print(path) # Output: './data/example_key.npy'
NOTE
Ensure that the key exists in the internal data structure before calling this method to avoid a KeyError.