IO
Different IO operations used to load and save data through the exputils package.
They are usually not needed to log or load data which is done with the functions under the exputils.data
module.
See the Logging and Loading sections for more information.
makedirs
Creates a directory and all intermediate directories if they do not exist in a file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The directory path to create. |
required |
makedirs_for_file
Creates the necessary directories for a given file path if they do not already exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
str
|
The complete file path for which the directories are to be created. |
required |
Source code in exputils/io/general.py
load_numpy_files
Loads numpy files from a specified directory into an AttrDict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
str
|
The path to the directory containing the numpy files. |
required |
allowed_data_filter
|
list
|
A list of allowed file names to be loaded. If specified, only files with names in this list will be loaded. |
None
|
denied_data_filter
|
list
|
A list of denied file names to be excluded from loading. If specified, files with names in this list will not be loaded. |
None
|
allow_pickle
|
bool
|
Whether to allow loading pickled (serialized) objects.
Default is True. |
True
|
Raises:
Type | Description |
---|---|
ValueError
|
If both allowed_data_filter and denied_data_filter are specified. |
FileNotFoundError
|
If the specified directory does not exist. |
Exception
|
If an error occurs during loading of a file. |
Returns:
Name | Type | Description |
---|---|---|
data |
AttrDict
|
Dictionary with loaded data where the keys are file names without extensions and the values are the respective numpy arrays. |
Source code in exputils/io/numpy.py
save_dict_to_numpy_files
Saves a dictionary with numpy arrays to numpy files (either .npy, .npz, or .npz compressed formats).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict
|
Dictionary containing the data to be saved, with keys as filenames and values as data to be saved. |
required |
path
|
str
|
Directory or file path where the numpy files will be saved. Default is the current directory. |
'.'
|
mode
|
str
|
Mode in which to save the data. Can be 'npy', 'npz', or 'cnpz'. Default is 'npy'. |
'npy'
|
Raises:
Type | Description |
---|---|
ValueError
|
If an invalid mode is provided. |
Source code in exputils/io/numpy.py
load_dill
Loads a serialized object from a file using the dill library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
The path to the file from which to load the object. The file extension is optionally added if not already present. |
required |
Returns:
Name | Type | Description |
---|---|---|
obj |
Any
|
The object that was deserialized from the file. |
Notes:
- If the specified file does not exist, the function attempts to append the expected file extension (.dill) before throwing an error.
- This could allow arbitrary code execution. Only load files you trust!
Source code in exputils/io/dill.py
load_dill_files
Loads all serialized objects from a directory using the dill library and returns them in a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
str
|
The path to the directory containing dill-serialized files. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified directory does not exist. |
Returns:
Name | Type | Description |
---|---|---|
data |
AttrDict
|
An attribute dictionary where keys are the file names (without extensions) and values are the deserialized objects. |
Notes:
- If the specified file does not exist, the function attempts to append the expected file extension (.dill) before throwing an error.
- This could allow arbitrary code execution. Only load files you trust!
Source code in exputils/io/dill.py
save_dill
Serializes a Python object and saves it to a file using the dill serialization library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The Python object to be serialized. |
required |
file_path
|
str
|
The file path where the serialized object will be saved. |
required |
Notes:
- If the provided file path does not have the correct file extension for Dill files, the extension will be added automatically.
- The necessary directories for the file path will be created if they do not exist.