Registry¶
registry ¶
Classes¶
Functions¶
register_loader ¶
register_loader(name: str, loader_class: type[LoaderPlugin]) -> None
Register a loader plugin.
Adds a loader plugin to the global registry, making it available for use throughout the application. The loader can then be retrieved by name using get_loader().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | Unique identifier for the loader (e.g., 'coco', 'yolo'). Should be lowercase and descriptive of the format. | required |
loader_class | type[LoaderPlugin] | Loader class to register. Must be a subclass of LoaderPlugin. | required |
Raises:
| Type | Description |
|---|---|
ValueError | If a loader with the same name is already registered. |
Example
Register a custom loader:
from boxlab.dataset.plugins import LoaderPlugin
from boxlab.dataset.registry import register_loader
class CustomLoader(LoaderPlugin):
@property
def name(self) -> str:
return "custom"
@property
def description(self) -> str:
return "Custom format loader"
def load(self, path, **kwargs):
# Implementation
pass
# Register the loader
register_loader("custom", CustomLoader)
# Now it can be used
loader = get_loader("custom")
dataset = loader.load("data.custom")
Example
Register built-in loaders at application startup:
Source code in boxlab/dataset/plugins/registry.py
register_exporter ¶
register_exporter(name: str, exporter_class: type[ExporterPlugin]) -> None
Register an exporter plugin.
Adds an exporter plugin to the global registry, making it available for use throughout the application. The exporter can then be retrieved by name using get_exporter().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | Unique identifier for the exporter (e.g., 'coco', 'yolo'). Should be lowercase and descriptive of the format. | required |
exporter_class | type[ExporterPlugin] | Exporter class to register. Must be a subclass of ExporterPlugin. | required |
Raises:
| Type | Description |
|---|---|
ValueError | If an exporter with the same name is already registered. |
Example
Register a custom exporter:
from boxlab.dataset.plugins import ExporterPlugin
from boxlab.dataset.registry import register_exporter
class CustomExporter(ExporterPlugin):
@property
def name(self) -> str:
return "custom"
@property
def description(self) -> str:
return "Custom format exporter"
def export(self, dataset, output_dir, **kwargs):
# Implementation
pass
# Register the exporter
register_exporter("custom", CustomExporter)
# Now it can be used
exporter = get_exporter("custom")
exporter.export(dataset, "output/")
Example
Register multiple exporters:
from boxlab.dataset.exporters import (
COCOExporter,
YOLOExporter,
PascalVOCExporter,
)
from boxlab.dataset.registry import register_exporter
def setup_exporters():
register_exporter("coco", COCOExporter)
register_exporter("yolo", YOLOExporter)
register_exporter("pascal_voc", PascalVOCExporter)
setup_exporters()
Source code in boxlab/dataset/plugins/registry.py
get_loader ¶
get_loader(name: str) -> LoaderPlugin
Get a registered loader instance.
Retrieves and instantiates a loader plugin from the registry by name. Each call creates a new instance of the loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | Loader name. Must match a previously registered loader. | required |
Returns:
| Type | Description |
|---|---|
LoaderPlugin | A new instance of the requested LoaderPlugin. |
Raises:
| Type | Description |
|---|---|
KeyError | If no loader with the given name is registered. The error message includes a list of available loaders. |
Example
Basic usage:
Example
Error handling with available loaders:
Example
Dynamic loader selection:
from pathlib import Path
from boxlab.dataset.registry import get_loader
def load_dataset(file_path: str):
path = Path(file_path)
# Determine format from extension
if path.suffix == ".json":
loader = get_loader("coco")
elif path.suffix in [".yaml", ".yml"]:
loader = get_loader("yolo")
else:
raise ValueError(
f"Unsupported format: {path.suffix}"
)
return loader.load(file_path)
dataset = load_dataset("data/annotations.json")
Source code in boxlab/dataset/plugins/registry.py
get_exporter ¶
get_exporter(name: str) -> ExporterPlugin
Get a registered exporter instance.
Retrieves and instantiates an exporter plugin from the registry by name. Each call creates a new instance of the exporter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | Exporter name. Must match a previously registered exporter. | required |
Returns:
| Type | Description |
|---|---|
ExporterPlugin | A new instance of the requested ExporterPlugin. |
Raises:
| Type | Description |
|---|---|
KeyError | If no exporter with the given name is registered. The error message includes a list of available exporters. |
Example
Basic usage:
Example
Export to multiple formats:
from boxlab.dataset.registry import get_exporter
from boxlab.dataset.types import SplitRatio
dataset = my_dataset # Your dataset
split_ratio = SplitRatio(train=0.7, val=0.2, test=0.1)
# Export to COCO format
coco_exporter = get_exporter("coco")
coco_exporter.export(
dataset, "output/coco", split_ratio=split_ratio, seed=42
)
# Export to YOLO format
yolo_exporter = get_exporter("yolo")
yolo_exporter.export(
dataset, "output/yolo", split_ratio=split_ratio, seed=42
)
Example
Error handling:
from boxlab.dataset.registry import (
get_exporter,
list_exporters,
)
def export_dataset(
dataset, format_name: str, output_dir: str
):
try:
exporter = get_exporter(format_name)
exporter.export(dataset, output_dir)
print(
f"Exported to {format_name} format successfully"
)
except KeyError:
available = list_exporters()
print(f"Unknown format '{format_name}'")
print(f"Available formats: {', '.join(available)}")
Source code in boxlab/dataset/plugins/registry.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
list_loaders ¶
List all registered loaders.
Returns the names of all loader plugins currently registered in the system. Useful for displaying available formats or implementing format auto-detection.
Returns:
| Type | Description |
|---|---|
list[str] | List of loader names as strings, in no particular order. |
Example
Display available loaders:
Example
Check if a specific loader is available:
Example
Build a CLI help message:
from boxlab.dataset.registry import list_loaders
def print_help():
loaders = list_loaders()
print("Usage: dataset-tool load --format FORMAT file")
print(f"Supported formats: {', '.join(loaders)}")
print_help()
# Output: Usage: dataset-tool load --format FORMAT file
# Supported formats: coco, yolo, pascal_voc
Source code in boxlab/dataset/plugins/registry.py
list_exporters ¶
List all registered exporters.
Returns the names of all exporter plugins currently registered in the system. Useful for displaying available export formats or building conversion tools.
Returns:
| Type | Description |
|---|---|
list[str] | List of exporter names as strings, in no particular order. |
Example
Display available export formats:
Example
Validate user input:
from boxlab.dataset.registry import list_exporters
def select_export_format(user_format: str) -> str:
available = list_exporters()
if user_format not in available:
raise ValueError(
f"Invalid format '{user_format}'. "
f"Choose from: {', '.join(available)}"
)
return user_format
# Usage
try:
format_choice = select_export_format("coco")
print(f"Selected: {format_choice}")
except ValueError as e:
print(e)
Example
Build a format converter:
from boxlab.dataset.registry import (
list_exporters,
get_loader,
get_exporter,
)
def convert_dataset(
input_file: str,
input_format: str,
output_formats: list[str],
output_dir: str,
):
# Load dataset
loader = get_loader(input_format)
dataset = loader.load(input_file)
# Export to all requested formats
available_exporters = list_exporters()
for fmt in output_formats:
if fmt not in available_exporters:
print(
f"Warning: Skipping unknown format '{fmt}'"
)
continue
exporter = get_exporter(fmt)
exporter.export(dataset, f"{output_dir}/{fmt}")
print(f"Exported to {fmt}")
# Convert COCO to YOLO and Pascal VOC
convert_dataset(
"data.json", "coco", ["yolo", "pascal_voc"], "output"
)
Source code in boxlab/dataset/plugins/registry.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
get_loader_info ¶
Get information about all registered loaders.
Retrieves detailed information about all registered loader plugins, including
their names, descriptions, and supported file extensions. This is useful for
building user interfaces, documentation, or diagnostic tools.
Returns:
Dictionary mapping loader names to their information dictionaries.
Each info dictionary contains:
- name (str): The loader's identifier
- description (str): Human-readable description
- supported_extensions (list[str]): List of file extensions
Example:
Display loader information:
```python
from boxlab.dataset.registry import get_loader_info
info = get_loader_info()
for name, details in info.items():
print(f"
Loader: {name}") print(f" Description: {details['description']}") print( f" Extensions: {', '.join(details['supported_extensions'])}" )
# Output:
# Loader: coco
# Description: Load COCO JSON format datasets
# Extensions: .json
#
# Loader: yolo
# Description: Load YOLO format datasets
# Extensions: .yaml, .yml, .txt
```
Example:
Auto-detect format from file extension:
```python
from pathlib import Path
from boxlab.dataset.registry import (
get_loader_info,
get_loader,
)
def auto_load_dataset(file_path: str):
file_ext = Path(file_path).suffix.lower()
# Find compatible loader
loader_info = get_loader_info()
for name, info in loader_info.items():
if file_ext in info["supported_extensions"]:
print(f"Detected format: {name}")
loader = get_loader(name)
return loader.load(file_path)
raise ValueError(
f"No loader found for extension '{file_ext}'"
)
# Usage
dataset = auto_load_dataset(
"annotations.json"
) # Auto-detects COCO
```
Example:
Generate documentation:
```python
from boxlab.dataset.registry import get_loader_info
def generate_loader_docs():
info = get_loader_info()
docs = "# Supported Input Formats
" for name, details in sorted(info.items()): docs += f"## {name.upper()}
" docs += f"{details['description']}
"
if details["supported_extensions"]:
exts = ", ".join(
details["supported_extensions"]
)
docs += f"**File Extensions:** {exts}
"
return docs
print(generate_loader_docs())
```
Example:
Build a CLI with format hints:
```python
import argparse
from boxlab.dataset.registry import get_loader_info
def create_parser():
parser = argparse.ArgumentParser()
# Get available formats
loader_info = get_loader_info()
format_choices = list(loader_info.keys())
parser.add_argument(
"--format",
choices=format_choices,
help="Input format",
)
# Add format descriptions to help
help_text = "
Supported formats: " for name, info in loader_info.items(): help_text += f" {name}: {info['description']} "
parser.epilog = help_text
return parser
parser = create_parser()
args = parser.parse_args()
```
Source code in boxlab/dataset/plugins/registry.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
get_exporter_info ¶
Get information about all registered exporters.
Retrieves detailed information about all registered exporter plugins, including
their names, descriptions, and default configurations. This is useful for
building user interfaces, configuration tools, or documentation.
Returns:
Dictionary mapping exporter names to their information dictionaries.
Each info dictionary contains:
- name (str): The exporter's identifier
- description (str): Human-readable description
- default_config (dict): Default configuration options
Example:
Display exporter information:
```python
from boxlab.dataset.registry import get_exporter_info
info = get_exporter_info()
for name, details in info.items():
print(f"
Exporter: {name}") print(f" Description: {details['description']}") print(f" Default Config:") for key, value in details["default_config"].items(): print(f" {key}: {value}")
# Output:
# Exporter: coco
# Description: Export to COCO JSON format
# Default Config:
# copy_images: True
# naming_strategy: original
# indent: 2
```
Example:
Build a configuration UI:
```python
from boxlab.dataset.registry import get_exporter_info
def get_export_options(format_name: str) -> dict:
info = get_exporter_info()
if format_name not in info:
raise ValueError(f"Unknown format: {format_name}")
# Get default config and allow overrides
config = info[format_name]["default_config"].copy()
return config
# Get YOLO export options
yolo_config = get_export_options("yolo")
print(f"YOLO defaults: {yolo_config}")
# Customize
yolo_config["copy_images"] = False
yolo_config["normalize_coords"] = True
```
Example:
Generate format comparison table:
```python
from boxlab.dataset.registry import get_exporter_info
def compare_exporters():
info = get_exporter_info()
print("Format Comparison:
") print( f"{'Format':<15} {'Image Copy':<12} {'Description'}" ) print("-" * 60)
for name, details in sorted(info.items()):
copy_images = details["default_config"].get(
"copy_images", "N/A"
)
desc = details["description"][:30]
print(f"{name:<15} {str(copy_images):<12} {desc}")
compare_exporters()
# Output:
# Format Comparison:
#
# Format Image Copy Description
# ------------------------------------------------------------
# coco True Export to COCO JSON format
# yolo True Export to YOLO format
```
Example:
Validate export configuration:
```python
from boxlab.dataset.registry import get_exporter_info
def validate_export_config(
format_name: str, config: dict
) -> dict:
info = get_exporter_info()
if format_name not in info:
raise ValueError(f"Unknown format: {format_name}")
# Start with defaults
default_config = info[format_name]["default_config"]
validated_config = default_config.copy()
# Override with user config
for key, value in config.items():
if key in default_config:
validated_config[key] = value
else:
print(
f"Warning: Unknown option '{key}' for {format_name}"
)
return validated_config
# Usage
user_config = {
"copy_images": False,
"invalid_option": "test", # Will trigger warning
}
final_config = validate_export_config("coco", user_config)
print(final_config)
```
Example:
Create export presets:
```python
from boxlab.dataset.registry import (
get_exporter_info,
get_exporter,
)
def create_export_preset(preset_name: str) -> dict:
presets = {
"quick": {
"copy_images": False,
"naming_strategy": "original",
},
"production": {
"copy_images": True,
"naming_strategy": "sequential",
},
}
return presets.get(preset_name, {})
def export_with_preset(
dataset, format_name: str, preset_name: str
):
# Get base config
info = get_exporter_info()
config = info[format_name]["default_config"].copy()
# Apply preset
preset = create_export_preset(preset_name)
config.update(preset)
# Export
exporter = get_exporter(format_name)
exporter.export(
dataset, f"output/{preset_name}", **config
)
# Usage
export_with_preset(my_dataset, "coco", "quick")
```
Source code in boxlab/dataset/plugins/registry.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | |
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true
Overview¶
The registry module provides centralized management for dataset loader and exporter plugins. It allows registration, retrieval, and discovery of available plugins at runtime.
Key Features¶
- Plugin Registration: Register custom loaders and exporters
- Plugin Retrieval: Get plugin instances by name
- Plugin Discovery: List and inspect available plugins
- Plugin Information: Access plugin metadata and capabilities
Registration¶
Register custom plugins to make them available throughout the application:
from boxlab.dataset.plugins.registry import register_loader, register_exporter
from boxlab.dataset.loaders import COCOLoader
from boxlab.dataset.exporters import COCOExporter
# Register loader
register_loader("coco", COCOLoader)
# Register exporter
register_exporter("coco", COCOExporter)
Retrieval¶
Get plugin instances by name:
from boxlab.dataset.plugins.registry import get_loader, get_exporter
# Get loader instance
loader = get_loader("coco")
dataset = loader.load("annotations.json")
# Get exporter instance
exporter = get_exporter("yolo")
exporter.export(dataset, "output/yolo_format")
Discovery¶
List and inspect available plugins:
from boxlab.dataset.plugins.registry import (
list_loaders,
list_exporters,
get_loader_info,
get_exporter_info,
)
# List available loaders
loaders = list_loaders()
print(f"Available loaders: {loaders}") # ['coco', 'yolo']
# List available exporters
exporters = list_exporters()
print(f"Available exporters: {exporters}") # ['coco', 'yolo']
# Get detailed loader information
loader_info = get_loader_info()
for name, info in loader_info.items():
print(f"\nLoader: {name}")
print(f" Description: {info['description']}")
print(f" Extensions: {info['supported_extensions']}")
# Get detailed exporter information
exporter_info = get_exporter_info()
for name, info in exporter_info.items():
print(f"\nExporter: {name}")
print(f" Description: {info['description']}")
print(f" Default Config: {info['default_config']}")
Auto-Detection¶
Use registry information for format auto-detection:
from pathlib import Path
from boxlab.dataset.plugins.registry import get_loader_info, get_loader
def auto_load_dataset(file_path: str):
file_ext = Path(file_path).suffix.lower()
# Find compatible loader
loader_info = get_loader_info()
for name, info in loader_info.items():
if file_ext in info['supported_extensions']:
print(f"Detected format: {name}")
loader = get_loader(name)
return loader.load(file_path)
raise ValueError(f"No loader found for extension '{file_ext}'")
# Usage
dataset = auto_load_dataset("annotations.json") # Auto-detects COCO
Error Handling¶
Handle missing plugins gracefully:
from boxlab.dataset.plugins.registry import get_loader, list_loaders
try:
loader = get_loader("unknown_format")
dataset = loader.load("data.txt")
except KeyError as e:
print(f"Error: {e}")
print(f"Available loaders: {list_loaders()}")
API Reference¶
Registration Functions¶
register_loader(name, loader_class): Register a loader pluginregister_exporter(name, exporter_class): Register an exporter plugin
Retrieval Functions¶
get_loader(name): Get a loader instanceget_exporter(name): Get an exporter instance
Discovery Functions¶
list_loaders(): List all registered loader nameslist_exporters(): List all registered exporter namesget_loader_info(): Get detailed information about all loadersget_exporter_info(): Get detailed information about all exporters
See Also¶
- Plugin System: Plugin architecture overview
- COCO Plugin: COCO format plugin
- YOLO Plugin: YOLO format plugin
- Dataset: Core dataset management