Skip to content

Commit

Permalink
study.custom_heuristic changed to be non-nullable
Browse files Browse the repository at this point in the history
Heuristic column should not be nullable. Refactored slightly to set
default to be whatever is stored under study.custom_heuristic, and
making the column non-nullable.
  • Loading branch information
kaitj committed Feb 29, 2024
1 parent 7c87e11 commit 07f8280
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
23 changes: 2 additions & 21 deletions autobidsportal/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions autobidsportal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -22,6 +24,22 @@

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",
Expand Down Expand Up @@ -393,7 +411,12 @@ 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."""
Expand Down Expand Up @@ -478,7 +501,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."""
Expand Down

0 comments on commit 07f8280

Please sign in to comment.