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

Fix train state and modification time for unfinished project training #722

Merged
merged 2 commits into from
Aug 8, 2023
Merged
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
15 changes: 13 additions & 2 deletions annif/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,26 @@ def params(self) -> dict[str, Any]:
params.update(self.config_params)
return params

@property
def _model_file_paths(self) -> list:
all_paths = glob(os.path.join(self.datadir, "*"))
ignore_patterns = ("*-train*", "tmp-*", "vectorizer")
ignore_paths = [
path
for igp in ignore_patterns
for path in glob(os.path.join(self.datadir, igp))
]
return list(set(all_paths) - set(ignore_paths))

@property
def is_trained(self) -> bool:
return bool(glob(os.path.join(self.datadir, "*")))
return bool(self._model_file_paths)

@property
def modification_time(self) -> datetime | None:
mtimes = [
datetime.utcfromtimestamp(os.path.getmtime(p))
for p in glob(os.path.join(self.datadir, "*"))
for p in self._model_file_paths
]
most_recent = max(mtimes, default=None)
if most_recent is None:
Expand Down
1 change: 1 addition & 0 deletions annif/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def atomic_save(
final name."""

prefix, suffix = os.path.splitext(filename)
prefix = "tmp-" + prefix
tempfd, tempfilename = tempfile.mkstemp(prefix=prefix, suffix=suffix, dir=dirname)
os.close(tempfd)
logger.debug("saving %s to temporary file %s", str(obj)[:90], tempfilename)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ def test_project_tfidf_is_not_trained(registry):
assert not project.is_trained


def test_project_tfidf_is_not_trained_prepared_only(registry, testdatadir):
testdatadir.join("projects/tfidf-fi").ensure("vectorizer")
testdatadir.join("projects/tfidf-fi").ensure("dummy-tfidf-train.txt")
project = registry.get_project("tfidf-fi")
assert not project.is_trained


def test_project_tfidf_modification_time_prepared_only(registry, testdatadir):
testdatadir.join("projects/tfidf-fi").ensure("vectorizer")
testdatadir.join("projects/tfidf-fi").ensure("dummy-tfidf-train.txt")
project = registry.get_project("tfidf-fi")
assert project.modification_time is None


def test_project_train_tfidf(registry, document_corpus, testdatadir):
project = registry.get_project("tfidf-fi")
project.train(document_corpus)
Expand Down
Loading