Skip to content

Commit

Permalink
Merge pull request #21 from TheJacksonLaboratory/G3-246-geneweaver-co…
Browse files Browse the repository at this point in the history
…re-geneset-score-type-should-validate-usage-of-threshold-low

Adding validators to GenesetScoreType
  • Loading branch information
bergsalex authored May 29, 2024
2 parents 1415135 + 2f892ab commit 4ae1f33
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "geneweaver-core"
version = "0.10.0a0"
version = "0.10.0a1"
description = "The core of the Jax-Geneweaver Python library"
authors = ["Jax Computational Sciences <[email protected]>"]
readme = "README.md"
Expand Down
30 changes: 28 additions & 2 deletions src/geneweaver/core/schema/score.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""Pydantic schema for defining score types."""

# ruff: noqa: N805

from typing import Optional

from geneweaver.core.enum import ScoreType
from pydantic import BaseModel
from pydantic import BaseModel, validator


class GenesetScoreType(BaseModel):
"""Pydantic schema for defining score types."""

score_type: ScoreType
threshold_low: Optional[float] = None
threshold: float = 0.05
threshold_low: Optional[float] = None

def __str__(self: "GenesetScoreType") -> str:
"""Return a string representation of the score type."""
Expand All @@ -26,3 +28,27 @@ def threshold_as_db_string(self) -> str:
return f"{self.threshold_low},{self.threshold}"
else:
return str(self.threshold)

@validator("threshold_low")
def threshold_low_must_be_less_than_threshold(
cls, v: Optional[float], values: dict
) -> Optional[float]:
"""Threshold low must be less than threshold."""
if v is not None and v > values.get("threshold"):
raise ValueError("threshold_low must be less than threshold")
return v

@validator("threshold_low")
def threshold_low_correlation_and_effect_only(
cls, v: Optional[float], values: dict
) -> Optional[float]:
"""Threshold low should only be set for correlation and effect score types."""
if v is not None and values.get("score_type") not in [
ScoreType.CORRELATION,
ScoreType.EFFECT,
]:
raise ValueError(
"threshold_low should only be set for "
"correlation and effect score types"
)
return v

0 comments on commit 4ae1f33

Please sign in to comment.