dmf.io.load#
- dmf.io.load(file_path: str | Path, loader: str | None = None, **kwargs)[source]#
Load data from a file using the appropriate loader.
This function loads data from various file formats by automatically determining the appropriate loader based on the file extension. You can also specify the loader explicitly if desired.
Supported Loaders#
“pickle”: For .pkl files.
“joblib”: For .joblib files.
“pandas”: For .csv, .parquet, .xlsx, .xls, .feather files.
“json”: For .json files.
“str”: For .txt, .html, .log, .md, .rst files.
“hdf5”: For .h5, .hdf5, .hdf files.
“numpy”: For .npz, .npy files.
“pillow”: For image files (.jpg, .jpeg, .png, .bmp, .gif, .tiff, .tif, .webp).
“pytorch”: For PyTorch model files (.pt, .pth).
“yaml”: For .yaml, .yml files.
“ini”: For .ini, .cfg files.
“matlab”: For .mat files.
“audio”: For audio files (.wav, .mp3, .flac, .ogg).
“video”: For video files (.mp4, .avi, .mov, .mkv).
- param file_path:
The path to the file to load. The file extension will be used to determine the appropriate loader if not specified.
- type file_path:
Union[str, Path]
- param loader:
The loader type to use. If not provided, it will be inferred from the file extension.
- type loader:
Optional[str], default=None
- param kwargs:
Additional keyword arguments to pass to the loader function.
- type kwargs:
dict
- returns:
The data loaded from the file. The return type depends on the loader used, such as: - pd.DataFrame for Pandas files (.csv, .parquet, etc.) - dict for JSON or YAML files - np.ndarray for NumPy files (.npy, .npz) - torch.Tensor for PyTorch model files (.pt, .pth) - PIL.Image for image files (.jpg, .png, etc.) - str for text files (.txt, .log, etc.) - configparser.ConfigParser for INI files - dict for MATLAB files (.mat) - tuple[np.ndarray, int] for audio files - list[np.ndarray] for video files - Any for pickle and joblib files (the specific type depends on the serialized object)
- rtype:
Any
- raises ValueError:
If the file extension or specified loader is not supported.
- raises ImportError:
If the required library for the specified loader is not installed.
Examples
Loading a DataFrame from a CSV file:
import pandas as pd df = load('data.csv') type(df) # <class 'pandas.core.frame.DataFrame'>
Loading an image using Pillow:
img = load('image.png') type(img) # <class 'PIL.PngImagePlugin.PngImageFile'>
Loading a PyTorch model:
tensor = load('model.pth') type(tensor) # <class 'torch.Tensor'>