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

Add code #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added sirara/ext/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions sirara/ext/spams_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import spams as _spams

from sirara import wrapper

trainDL = wrapper.factorizer(data_kw="X", num_atoms_kw="K", init_kw="D")(_spams.trainDL)
59 changes: 59 additions & 0 deletions sirara/wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
The module ``sirara`` exists to provide a wrapper for factorization functions.

Overview
===============================================================================

This provides a wrapper to generate a standard factorization API. This allows
for usage in other functions expecting a standard API.

"""


def factorizer(data_kw, num_atoms_kw, init_kw=None):
"""
A wrapper that standardizes various factorization functions.

Args:
data_kw(str): Keyword to use for the data.

num_atoms_kw(str): Keyword to use for the number of atoms.

init_kw(str): Keyword to use for the initial dictionary
(if possible to set).

Returns:
callable: A decorator to use to wrap a function.
"""

def factorizer_wrapper(a_callable):
"""
The wrapper of the callable that creates a new standard function.

Args:
a_callable(callable): The callable to wrap.

Returns:
callable: A decorator to wrap a function.
"""

def wrapped(data, num_atoms, init=None, *args, **kwargs):
"""
The wrapped function.
"""

if init_kw is None:
assert init is None, \
"This function doesn't take an initial argument."
else:
if init is not None:
kwargs[init_kw] = init

kwargs[data_kw] = data
kwargs[num_atoms_kw] = num_atoms

return a_callable(*args, **kwargs)

return wrapped

return factorizer_wrapper
Empty file added tests/test_ext/__init__.py
Empty file.
Empty file.
Empty file added tests/test_wrapper.py
Empty file.