multimeditron.dataset.loader package¶
Subpackages¶
Module contents¶
- class multimeditron.dataset.loader.AutoModalityLoader¶
Bases:
objectLoader registry to automatically manage and instantiate modality loaders.
The
AutoModalityLoaderacts as a central registry for different modality loader classes, enabling easy registration, instantiation, and retrieval of loaders. Each loader must inherit fromBaseModalityLoaderto be compatible.- _registry¶
Internal registry of modality loader classes keyed by name.
- Type:
Dict[str, Type[BaseModalityLoader]]
- classmethod from_name(name: str, *args, **kwargs) BaseModalityLoader¶
Retrieve and instantiate a registered loader class by name.
- Parameters:
name (str) – The registered name of the loader class.
*args – Positional arguments to pass to the loader’s initializer.
**kwargs – Keyword arguments to pass to the loader’s initializer.
- Returns:
The instantiated loader.
- Return type:
- Raises:
ValueError – If the name is not registered.
- classmethod register(name: str)¶
Register a modality loader class under a specific name.
- Parameters:
name (str) – The name under which the loader class will be registered.
- Returns:
A decorator to register the loader class.
- Return type:
Callable
- Raises:
ValueError – If the class is not a subclass of
BaseModalityLoaderor if the name is already registered.
- class multimeditron.dataset.loader.BaseModalityLoader¶
Bases:
ABCAbstract base class for modality loaders.
- abstractmethod load(*args, **kwargs) Any¶
Abstract method to load modality data. This method must be implemented by subclasses.
- Parameters:
*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
- Returns:
The loaded modality data.
- Return type:
Any
- static load_modalities(sample: Dict[str, Any], loaders: Dict[str, BaseModalityLoader])¶
Load modalities in a sample using the provided modality loaders. This function loads the modality if needed using the loader defined in
loadersFor instance: Ifloadersis a dictionary containing an element"image" : FileSystemImageLoader, then this function assumes that every samples store images as path and load the image using theFileSystemImageLoaderNote that each sample should contain a key
MODALITY_TYPE_KEYdefining what kind of modality this is (for instance “image”). And a keyMODALITY_VALUE_KEYdefining the value of the modality, this could be a path or the bytes of the images for instance.- Parameters:
sample (Dict[str, Any]) – The input sample containing modalities.
loaders (Dict[str, BaseModalityLoader]) – A dictionary mapping modality types to their corresponding loaders.
- Returns:
The processed sample with merged modalities.
- Return type:
Dict[str, Any]
- Raises:
ValueError – If a modality type is not found in the loaders.
- name: str¶
- class multimeditron.dataset.loader.FileSystemImageLoader(base_path: str | Path)¶
Bases:
BaseModalityLoaderLoader for image files from the filesystem. Expects the sample dictionary to have a “value” key containing the relative path to the image file. The base_path parameter specifies the root directory where images are stored. Example:
loader = FileSystemImageLoader(base_path="/path/to/images") sample = {"value": "image1.jpg", "type": "image"} image = loader.load(sample) # image is a PIL Image object
- load(sample: Dict[str, Any]) Image¶
Load an image from the filesystem. :param sample: A dictionary containing at least the “value” key with the relative path to the image file. :type sample: Dict[str, Any]
- Returns:
The loaded image as a PIL Image object.
- Return type:
PIL.Image.Image
- name: str = 'fs-image'¶
- class multimeditron.dataset.loader.RawImageLoader¶
Bases:
BaseModalityLoaderLoader for raw image bytes. Expects the sample dictionary to have a “value” key containing a dictionary with a “bytes” key holding the raw image bytes. Example:
loader = RawImageLoader() sample = {"value": {"bytes": b'...'}, "type": "image"} image = loader.load(sample) # image is a PIL Image object
- load(sample: Dict[str, Any]) Image¶
Load an image from raw bytes.
- Parameters:
sample (Dict[str, Any]) – A dictionary containing at least the “value” key with a dictionary that has a “bytes” key holding the raw image bytes.
- Returns:
The loaded image as a PIL Image object.
- Return type:
PIL.Image.Image
- name: str = 'raw-image'¶