Skip to content

Commit

Permalink
Simpler & more controllable parameter defaulting
Browse files Browse the repository at this point in the history
  • Loading branch information
juhoinkinen committed Sep 4, 2019
1 parent f8825ed commit bf58247
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
21 changes: 9 additions & 12 deletions annif/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class AnnifBackend(metaclass=abc.ABCMeta):
needs_subject_index = False
needs_subject_vectorizer = False

DEFAULT_PARAMS = {
'limit': 100,
}
DEFAULT_PARAMS = {'limit': 100}

def __init__(self, backend_id, params, datadir):
"""Initialize backend with specific parameters. The
Expand All @@ -25,18 +23,17 @@ def __init__(self, backend_id, params, datadir):
self.datadir = datadir
self.fill_params_with_defaults()

def default_params(self):
return self.DEFAULT_PARAMS

def fill_params_with_defaults(self):
"""Set the parameters that are not provided in the projects config file
with default values defined in the backend class and its parents."""
for source_cls in type(self).mro()[:-1]: # omit the object class
for default_param, default_value in \
source_cls.DEFAULT_PARAMS.items():
if default_param not in self.params:
self.debug(
"parameter {} not set, using default value {} from {}"
.format(default_param, default_value,
source_cls.__name__))
self.params[default_param] = str(default_value)
for default_param, default_value in self.default_params().items():
if default_param not in self.params:
self.debug('parameter "{}" not set, using default value "{}"'
.format(default_param, default_value))
self.params[default_param] = str(default_value)

def train(self, corpus, project):
"""train the model on the given document or subject corpus"""
Expand Down
10 changes: 7 additions & 3 deletions annif/backend/fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ class FastTextBackend(mixins.ChunkingBackend, backend.AnnifBackend):
't': float
}

DEFAULT_PARAMS = {
# 'chunksize': 2, # EXAMPLE Could override chunksize in ChunkingBackend
}
DEFAULT_PARAMS = {'loss': 'hs'} # TODO: and others

MODEL_FILE = 'fasttext-model'
TRAIN_FILE = 'fasttext-train.txt'

# defaults for uninitialized instances
_model = None

def default_params(self):
params = backend.AnnifBackend.DEFAULT_PARAMS.copy()
params.update(mixins.ChunkingBackend.DEFAULT_PARAMS)
params.update(self.DEFAULT_PARAMS)
return params

def initialize(self):
if self._model is None:
path = os.path.join(self.datadir, self.MODEL_FILE)
Expand Down
7 changes: 4 additions & 3 deletions annif/backend/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
class ChunkingBackend(metaclass=abc.ABCMeta):
"""Annif backend mixin that implements chunking of input"""

DEFAULT_PARAMS = {
'chunksize': 1,
}
DEFAULT_PARAMS = {'chunksize': 1}

def default_params(self):
return self.DEFAULT_PARAMS

@abc.abstractmethod
def _suggest_chunks(self, chunktexts, project):
Expand Down

0 comments on commit bf58247

Please sign in to comment.