diff --git a/autobidsportal/forms.py b/autobidsportal/forms.py index 6140a37..72de398 100644 --- a/autobidsportal/forms.py +++ b/autobidsportal/forms.py @@ -53,21 +53,6 @@ def get_default_bidsignore() -> str: return bidsignore_file.read() -@lru_cache -def get_default_heuristic() -> str: - """Read default heuristic file. - - Returns - ------- - str - Contents of heuristic - """ - with (Path(__file__).parent / "resources" / "heuristics.py.default").open( - encoding="utf-8", - ) as heuristics_file: - return heuristics_file.read() - - def _gen_familiarity_field(label: str) -> SelectField: """Generate familiarty selections. @@ -388,11 +373,7 @@ def defaults_from_study( if study.retrospective_data: self.retrospective_start.default = study.retrospective_start self.retrospective_end.default = study.retrospective_end - self.heuristic.default = ( - get_default_heuristic() - if study.custom_heuristic is None - else study.custom_heuristic - ) + self.heuristic.default = study.heuristic if study.subj_expr is None: self.subj_expr.default = "*_{subject}" else: @@ -528,7 +509,7 @@ def update_study( study.principal = self.pi_name.data study.project_name = self.project_name.data study.dataset_name = self.dataset_name.data - study.custom_heuristic = self.heuristic.data + study.heuristic = self.heuristic.data study.subj_expr = self.subj_expr.data study.deface = self.deface.data study.patient_str = self.patient_str.data diff --git a/autobidsportal/models.py b/autobidsportal/models.py index fc5fca4..3822c53 100644 --- a/autobidsportal/models.py +++ b/autobidsportal/models.py @@ -8,6 +8,8 @@ from collections.abc import Sequence from datetime import datetime from enum import Enum +from functools import lru_cache +from pathlib import Path from time import time from typing import Any @@ -22,6 +24,21 @@ from autobidsportal.dateutils import TIME_ZONE + +@lru_cache +def get_default_heuristic() -> str: + """Read default heuristic file. + + Returns + ------- + str + Contents of heuristic + """ + with (Path(__file__).parent / "resources" / "heuristics.py.default").open( + encoding="utf-8", + ) as heuristics_file: + return heuristics_file.read() + login = LoginManager() convention = { "ix": "ix_%(column_0_label)s", @@ -393,7 +410,10 @@ class Study(db.Model): custom_bidsignore = db.Column(db.Text, nullable=True) - custom_heuristic = db.Column(db.Text, nullable=True) + # There should be a default here + heuristic = db.Column( + db.Text, nullable=False, default=get_default_heuristic(), + ) def __repr__(self) -> str: """Generate a str representation of this study.""" @@ -478,7 +498,7 @@ class Tar2bidsOutput(db.Model): id = db.Column(db.Integer, primary_key=True) study_id = db.Column(db.Integer, db.ForeignKey("study.id"), nullable=False) bids_dir = db.Column(db.String(200), index=True, nullable=True) - heuristic = db.Column(db.Text, index=True, nullable=True) + heuristic = db.Column(db.Text, index=True, nullable=False) def __repr__(self) -> str: """Generate a str representation of this output."""