Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Vital Algorithm Registration in Python #1123

Open
Erotemic opened this issue Oct 23, 2020 · 2 comments
Open

Simplify Vital Algorithm Registration in Python #1123

Erotemic opened this issue Oct 23, 2020 · 2 comments

Comments

@Erotemic
Copy link
Member

The current way of registering vital algorithms is a bit unwieldy and verbose. It seems like a perfect job for a decorator. I made a small proof-of-concept that demos a possible implementation for said decorator.

We may be able to refactor something like this

# In vital.py

def _register_algorithm(cls, name=None, desc=''):
    if name is None:
        name = cls.__name__
    from vital.algo import algorithm_factory
    if not algorithm_factory.has_algorithm_impl_name(cls.static_type_name(), name):
        algorithm_factory.add_algorithm(name, desc, cls)
        algorithm_factory.mark_algorithm_as_loaded(name)

def register_algorithm(name=None, desc=''):
    """
    POC refactor of __vital_algorithm_register__ into a decorator
    """
    def _wrapper(cls):
        _register_algorithm(cls, name, desc)
        return cls
    return _wrapper

def lazy_register(cls, name=None, desc=''):
    ''' Alternate Proof-of-Concept '''
    def __vital_algorithm_register__():
        return _register_algorithm(cls, name, desc)
    return __vital_algorithm_register__

# Then in your class
import vital
@vial.register_algorithm(desc="PyTorch Netharn classification routine")
class MyAlgorithm(BaseAlgo):
    ...

# OR if the current lazy structure is important
import vital
class MyAlgorithm(BaseAlgo):
    ...

__vital_algorithm_register__ = vital.lazy_register(MyAlgorithm, desc="PyTorch Netharn classification routine")

# We could also play with adding class member variables for the lazy
# initialization. There is lots of room to make this better / easier.
@Erotemic Erotemic changed the title Simply Algorithm Registration Simplify Algorithm Registration Oct 23, 2020
@Erotemic Erotemic changed the title Simplify Algorithm Registration Simplify Vital Algorithm Registration in Python Oct 23, 2020
@Erotemic
Copy link
Member Author

@johnwparent @tao558 @mleotta @as6520 @linus-sherrill @dstoup Thoughts?

@as6520
Copy link
Contributor

as6520 commented Oct 23, 2020

I think using decorators to register the algorithm is an improvement over the current approach. As for the lazy registration, can it be used anywhere or should it present in the file with the algorithm?

If we decide to change the registration, I would recommend moving it out of vital into a module of its own since we would be replacing sprokit_register too with a decorator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants