NERFSTUDIO_METHOD_CONFIGS callable MethodSpecification (#2385)

* NERFSTUDIO_METHOD_CONFIGS allow method specification as callabale (function or class)

* docs - NERFSTUDIO_METHOD_CONFIGS accepts function or derived class
This commit is contained in:
drewgilliam authored and GitHub committed 2023-09-05 13:55:08 -07:00
1 parent a121d76fd0
commit 4d0033343c
3 files changed
+64 -10

No files matched your search

+33 -9
View File
@@ -3,6 +3,7 @@ Tests for the nerfstudio.plugins.registry module.
"""
import os
import sys
from dataclasses import dataclass
from nerfstudio.engine.trainer import TrainerConfig
from nerfstudio.pipelines.base_pipeline import VanillaPipelineConfig
@@ -15,14 +16,21 @@ else:
from importlib import metadata as importlib_metadata
TestConfig = MethodSpecification(
config=TrainerConfig(
@dataclass
class TestConfigClass(MethodSpecification):
config: TrainerConfig = TrainerConfig(
method_name="test-method",
pipeline=VanillaPipelineConfig(),
optimizers={},
),
description="Test description",
)
)
description: str = "Test description"
def TestConfigFunc():
return TestConfigClass()
TestConfig = TestConfigClass()
def test_discover_methods():
@@ -52,17 +60,18 @@ def test_discover_methods():
registry.entry_points = entry_points_backup
def test_discover_methods_from_environment_variable():
def _test_discover_methods_from_environment_variable(method):
"""Tests if a custom method from env variable gets properly registered using the discover_methods method"""
old_env = None
try:
old_env = os.environ.get("NERFSTUDIO_METHOD_CONFIGS", None)
os.environ["NERFSTUDIO_METHOD_CONFIGS"] = "test-method-env=test_registry:TestConfig"
method_key = f"{method}-env"
os.environ["NERFSTUDIO_METHOD_CONFIGS"] = f"{method_key}=test_registry:{method}"
# Discover plugins
methods, _ = registry.discover_methods()
assert "test-method-env" in methods
config = methods["test-method-env"]
assert method_key in methods
config = methods[method_key]
assert isinstance(config, TrainerConfig)
finally:
# Revert mock
@@ -70,3 +79,18 @@ def test_discover_methods_from_environment_variable():
os.environ["NERFSTUDIO_METHOD_CONFIGS"] = old_env
else:
del os.environ["NERFSTUDIO_METHOD_CONFIGS"]
def test_discover_methods_from_environment_variable_class():
"""Tests if a custom method class from env variable gets properly registered using the discover_methods method"""
_test_discover_methods_from_environment_variable("TestConfigClass")
def test_discover_methods_from_environment_variable_function():
"""Tests if a custom method function from env variable gets properly registered using the discover_methods method"""
_test_discover_methods_from_environment_variable("TestConfigFunc")
def test_discover_methods_from_environment_variable_instance():
"""Tests if a custom method instance from env variable gets properly registered using the discover_methods method"""
_test_discover_methods_from_environment_variable("TestConfig")