def test_add_lambda_override(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") r.add(lambda x: x, name="bar") assert "bar" in r._factories
def test_add_function(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") r.add(foo) assert "foo" in r._factories
def test_add_function_name_override(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") r.add(foo, name="bar") assert "bar" in r._factories
def _datasets_loader(r: registry.Registry): from catalyst.data import dataset as m # noqa: WPS347 r.add_from_module(m) from catalyst.contrib import datasets as m_contrib # noqa: WPS347 r.add_from_module(m_contrib)
def _engines_loader(r: registry.Registry): from catalyst.core.engine import IEngine r.add(IEngine) from catalyst import engines as m # noqa: WPS347 r.add_from_module(m)
def _runners_loader(r: registry.Registry): from catalyst.core.runner import IRunner r.add(IRunner) r.add(IRunner) from catalyst import runners as m # noqa: WPS347 r.add_from_module(m)
def _optimizers_loader(r: registry.Registry): from catalyst.contrib.nn import optimizers as m r.add_from_module(m) if SETTINGS.fairscale_required: from fairscale import optim as m2 r.add_from_module(m2, prefix=["fairscale."])
def test_decorator(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") @r.add def bar(): pass r.get("bar")
def _samplers_loader(r: Registry): from torch.utils.data import sampler as s factories = { k: v for k, v in s.__dict__.items() if "Sampler" in k and k != "Sampler" } r.add(**factories) from catalyst.data import sampler r.add_from_module(sampler)
def _model_loader(r: Registry): from catalyst.contrib import models as m r.add_from_module(m) try: import segmentation_models_pytorch as smp r.add_from_module(smp, prefix="smp.") except ImportError as ex: if settings.segmentation_models_required: logger.warning("segmentation_models_pytorch not available," " to install segmentation_models_pytorch," " run `pip install segmentation-models-pytorch`.") raise ex
def test_add_module(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") r.add_from_module(module) r.get("foo") with pytest.raises(RegistryException): r.get_instance("bar")
def test_kwargs(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") r.add(bar=foo) r.get("bar")
def test_double_add_same_nofail(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") r.add(foo) # It's ok to add same twice, forced by python relative import # implementation # https://github.com/catalyst-team/catalyst/issues/135 r.add(foo)
def _callbacks_loader(r: registry.Registry): from catalyst.core.callback import Callback, CallbackWrapper r.add(Callback) r.add(CallbackWrapper) from catalyst import callbacks as m # noqa: WPS347 r.add_from_module(m)
def test_instantiations(): """@TODO: Docs. Contribution is welcome.""" r = Registry() r.add(foo) res = r.get_instance("foo", 1, 2)() assert res == {"a": 1, "b": 2} res = r.get_instance("foo", 1, b=2)() assert res == {"a": 1, "b": 2} res = r.get_instance("foo", a=1, b=2)() assert res == {"a": 1, "b": 2} res = r.get_instance("tests.catalyst.tools.registery_foo.foo", a=1, b=2)() assert res == {"a": 1, "b": 2}
def test_fail_instantiation(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") r.add(foo) with pytest.raises(RegistryException) as e_ifo: r.get_instance("foo", c=1) assert hasattr(e_ifo.value, "__cause__")
def test_instantiations(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") r.add(foo) res = r.get_instance("foo", 1, 2) assert res == {"a": 1, "b": 2} res = r.get_instance("foo", 1, b=2) assert res == {"a": 1, "b": 2} res = r.get_instance("foo", a=1, b=2) assert res == {"a": 1, "b": 2}
def test_from_config(): """@TODO: Docs. Contribution is welcome.""" r = Registry("obj") r.add(foo) res = r.get_from_params(**{"obj": "foo", "a": 1, "b": 2}) assert res == {"a": 1, "b": 2} res = r.get_from_params(**{}) assert res is None
def test_name_key(): """@TODO: Docs. Contribution is welcome.""" r = Registry(name_key="_key_") r.add(foo) res = r.get_from_params(**{"_key_": "foo", "a": 1, "b": 2})() assert res == {"a": 1, "b": 2} res = r.get_from_params(**{"_target_": "foo", "a": 1, "b": 2}) assert res == {"_target_": "foo", "a": 1, "b": 2}
def test_fail_double_add_different(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") r.add(foo) with pytest.raises(RegistryException): def bar(): pass r.add(foo=bar)
def _transforms_loader(r: registry.Registry): from catalyst.data import transforms as t # add `'transform.'` prefix to avoid nameing conflicts with other catalyst modules r.add_from_module(t, prefix=["transform."]) if SETTINGS.albu_required: import albumentations as m r.add_from_module(m, prefix=["A.", "albu.", "albumentations."]) from albumentations import pytorch as p r.add_from_module(p, prefix=["A.", "albu.", "albumentations."])
def _transforms_loader(r: Registry): from torch.jit.frontend import UnsupportedNodeError try: import albumentations as m r.add_from_module(m, prefix=["A.", "albu.", "albumentations."]) from albumentations import pytorch as p r.add_from_module(p, prefix=["A.", "albu.", "albumentations."]) from catalyst.contrib.data.cv import transforms as t r.add_from_module(t, prefix=["catalyst.", "C."]) except ImportError as ex: if settings.albumentations_required: logger.warning( "albumentations not available, to install albumentations, " "run `pip install albumentations`.") raise ex try: from kornia import augmentation as k r.add_from_module(k, prefix=["kornia."]) except ImportError as ex: if settings.kornia_required: logger.warning("kornia not available, to install kornia, " "run `pip install kornia`.") raise ex except UnsupportedNodeError as ex: logger.warning( "kornia has requirement torch>=1.5.0, probably you have" " an old version of torch which is incompatible.\n" "To update pytorch, run `pip install -U 'torch>=1.5.0'`.") if settings.kornia_required: raise ex
def test_meta_factory(): """@TODO: Docs. Contribution is welcome.""" # noqa: D202 def meta_1(fn, args, kwargs): return fn def meta_2(fn, args, kwargs): return 1 r = Registry("obj", meta_1) r.add(foo) res = r.get_from_params(**{"obj": "foo"}) assert res == foo res = r.get_from_params(**{"obj": "foo"}, meta_factory=meta_2) assert res == 1
def _transforms_loader(r: Registry): try: import albumentations as m r.add_from_module(m, prefix=["A.", "albu.", "albumentations."]) from albumentations import pytorch as p r.add_from_module(p, prefix=["A.", "albu.", "albumentations."]) from catalyst.contrib.data.cv import transforms as t r.add_from_module(t, prefix=["catalyst.", "C."]) except ImportError as ex: if settings.albumentations_required: logger.warning( "albumentations not available, to install albumentations, " "run `pip install albumentations`.") raise ex
def _callbacks_loader(r: Registry): from catalyst.dl import callbacks as m # noqa: WPS347 r.add_from_module(m)
def _schedulers_loader(r: Registry): from catalyst.contrib.nn import schedulers as m r.add_from_module(m)
def _grad_clip_loader(r: Registry): from torch.nn.utils import clip_grad as m r.add_from_module(m)
def _modules_loader(r: Registry): from catalyst.contrib.nn import modules as m r.add_from_module(m)
def test_add_lambda_fail(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") with pytest.raises(RegistryException): r.add(lambda x: x)
def test_fail_multiple_with_name(): """@TODO: Docs. Contribution is welcome.""" r = Registry("") with pytest.raises(RegistryException): r.add(foo, foo, name="bar")