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

Update code to be compatible with Pydantic2 #359

Closed
wants to merge 20 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
pip install "tox<4.0.0"
- name: Test with pytest
run: |
export MIRA_REST_URL=http://34.230.33.149:8771
export MIRA_REST_URL=http://mira-epi-dkg-lb-c7b58edea41524e6.elb.us-east-1.amazonaws.com:8771
tox -e py
# - name: Upload coverage report to codecov
# uses: codecov/codecov-action@v1
Expand Down
22 changes: 11 additions & 11 deletions mira/dkg/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
class RelationQuery(BaseModel):
"""A query for relations in the domain knowledge graph."""

source_type: Optional[str] = Field(description="The source type (i.e., prefix)", example="vo")
source_type: Optional[str] = Field(None, description="The source type (i.e., prefix)", examples=["vo"])
source_curie: Optional[str] = Field(
description="The source compact URI (CURIE)", example="doid:946"
None, description="The source compact URI (CURIE)", examples=["doid:946"]
)
target_type: Optional[str] = Field(
description="The target type (i.e., prefix)", example="ncbitaxon"
None, description="The target type (i.e., prefix)", examples=["ncbitaxon"]
)
target_curie: Optional[str] = Field(
description="The target compact URI (CURIE)", example="ncbitaxon:10090"
None, description="The target compact URI (CURIE)", examples=["ncbitaxon:10090"]
)
relations: Union[None, str, List[str]] = Field(
description="A relation string or list of relation strings", example="vo:0001243"
None, description="A relation string or list of relation strings", examples=["vo:0001243"]
)
relation_direction: Literal["right", "left", "both"] = Field(
"right", description="The direction of the relationship"
Expand All @@ -50,7 +50,7 @@ class RelationQuery(BaseModel):
ge=0,
)
limit: Optional[int] = Field(
description="A limit on the number of records returned", example=50, ge=0
None, description="A limit on the number of records returned", examples=[50], ge=0
)
full: bool = Field(
False,
Expand Down Expand Up @@ -178,12 +178,12 @@ def get_transitive_closure(
class RelationResponse(BaseModel):
"""A triple (or multi-predicate triple) with abbreviated data."""

subject: str = Field(description="The CURIE of the subject of the triple", example="doid:96")
subject: str = Field(description="The CURIE of the subject of the triple", examples=["doid:96"])
predicate: Union[str, List[str]] = Field(
description="A predicate or list of predicates as CURIEs",
example="ro:0002452",
examples=["ro:0002452"],
)
object: str = Field(description="The CURIE of the object of the triple", example="symp:0000001")
object: str = Field(description="The CURIE of the object of the triple", examples=["symp:0000001"])


class FullRelationResponse(BaseModel):
Expand Down Expand Up @@ -396,10 +396,10 @@ class IsOntChildResult(BaseModel):
"""Result of a query to /is_ontological_child"""

child_curie: str = Field(...,
example="vo:0001113",
examples=["vo:0001113"],
description="The child CURIE")
parent_curie: str = Field(...,
example="obi:0000047",
examples=["obi:0000047"],
description="The parent CURIE")
is_child: bool = Field(
...,
Expand Down
58 changes: 29 additions & 29 deletions mira/dkg/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import pystow
import requests
from neo4j import GraphDatabase, Transaction, unit_of_work
from pydantic import BaseModel, Field, validator
from pydantic import BaseModel, Field, field_validator
from tqdm import tqdm
from typing_extensions import Literal, TypeAlias

Expand Down Expand Up @@ -44,82 +44,82 @@
class Relation(BaseModel):
"""A relationship between two entities in the DKG"""
source_curie: str = Field(
description="The curie of the source node", example="probonto:k0000000"
description="The curie of the source node", examples=["probonto:k0000000"]
)
target_curie: str = Field(
description="The curie of the target node", example="probonto:k0000007"
description="The curie of the target node", examples=["probonto:k0000007"]
)
type: str = Field(
description="The type of the relation", example="has_parameter"
description="The type of the relation", examples=["has_parameter"]
)
pred: str = Field(
description="The curie of the relation type",
example="probonto:c0000062"
examples=["probonto:c0000062"]
)
source: str = Field(
description="The prefix of the relation curie", example="probonto"
description="The prefix of the relation curie", examples=["probonto"]
)
graph: str = Field(
description="The URI of the relation",
example="https://raw.githubusercontent.com/probonto"
"/ontology/master/probonto4ols.owl"
examples=["https://raw.githubusercontent.com/probonto"
"/ontology/master/probonto4ols.owl"]
)
version: str = Field(
description="The version number", example="2.5"
description="The version number", examples=["2.5"]
)


class Entity(BaseModel):
"""An entity in the domain knowledge graph."""

id: str = Field(
..., title="Compact URI", description="The CURIE of the entity", example="ido:0000511"
..., title="Compact URI", description="The CURIE of the entity", examples=["ido:0000511"]
)
name: Optional[str] = Field(description="The name of the entity", example="infected population")
type: EntityType = Field(..., description="The type of the entity", example="class")
obsolete: bool = Field(..., description="Is the entity marked obsolete?", example=False)
name: Optional[str] = Field(None, description="The name of the entity", examples=["infected population"])
type: EntityType = Field(..., description="The type of the entity", examples=["class"])
obsolete: bool = Field(..., description="Is the entity marked obsolete?", examples=[False])
description: Optional[str] = Field(
description="The description of the entity.",
example="An organism population whose members have an infection.",
None, description="The description of the entity.",
examples=["An organism population whose members have an infection."],
)
synonyms: List[Synonym] = Field(
default_factory=list, description="A list of string synonyms", example=[]
default_factory=list, description="A list of string synonyms", examples=[[]]
)
alts: List[str] = Field(
title="Alternative Identifiers",
default_factory=list,
example=[],
examples=[[]],
description="A list of alternative identifiers, given as CURIE strings.",
)
xrefs: List[Xref] = Field(
title="Database Cross-references",
default_factory=list,
example=[],
examples=[[]],
description="A list of database cross-references, given as CURIE strings.",
)
labels: List[str] = Field(
default_factory=list,
example=["ido"],
examples=[["ido"]],
description="A list of Neo4j labels assigned to the entity.",
)
properties: Dict[str, List[str]] = Field(
default_factory=dict,
description="A mapping of properties to their values",
example={},
examples=[{}],
)
# Gets auto-populated
link: Optional[str] = None

@validator("link")
def set_link(cls, value, values):
@field_validator("link")
def set_link(cls, value, validation_info):
"""
Set the value of the ``link`` field based on the value of the ``id``
field. This gets run as a post-init hook by Pydantic

See also:
https://stackoverflow.com/questions/54023782/pydantic-make-field-none-in-validator-based-on-other-fields-value
"""
curie = values["id"]
curie = validation_info.data["id"]
return f"{METAREGISTRY_BASE}/{curie}"

@property
Expand Down Expand Up @@ -235,12 +235,12 @@ class AskemEntity(Entity):
"""An extended entity with more ASKEM stuff loaded in."""

# TODO @ben please write descriptions for these
physical_min: Optional[float] = Field(description="")
physical_max: Optional[float] = Field(description="")
suggested_data_type: Optional[str] = Field(description="")
suggested_unit: Optional[str] = Field(description="")
typical_min: Optional[float] = Field(description="")
typical_max: Optional[float] = Field(description="")
physical_min: Optional[float] = Field(None, description="")
physical_max: Optional[float] = Field(None, description="")
suggested_data_type: Optional[str] = Field(None, description="")
suggested_unit: Optional[str] = Field(None, description="")
typical_min: Optional[float] = Field(None, description="")
typical_max: Optional[float] = Field(None, description="")


class Neo4jClient:
Expand Down
20 changes: 10 additions & 10 deletions mira/dkg/grounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
class GroundRequest(BaseModel):
"""A model representing the parameters to be passed to :func:`gilda.ground` for grounding."""

text: str = Field(..., description="The text to be grounded", example="Infected Population")
text: str = Field(..., description="The text to be grounded", examples=["Infected Population"])
context: Optional[str] = Field(
None,
description="Context around the text to be grounded",
example="The infected population increased over the past month",
examples=["The infected population increased over the past month"],
)
namespaces: Optional[List[str]] = Field(
None,
description="A list of namespaces to filter groundings to.",
example=["do", "mondo", "ido"],
examples=[["do", "mondo", "ido"]],
)


Expand All @@ -37,30 +37,30 @@ class GroundResult(BaseModel):
url: str = Field(
...,
description="A URL that resolves the term to an external web service",
example=f"{BR_BASE}/ido:0000511",
examples=[f"{BR_BASE}/ido:0000511"],
)
score: float = Field(
..., description="The matching score calculated by Gilda", ge=0.0, le=1.0, example=0.78
..., description="The matching score calculated by Gilda", ge=0.0, le=1.0, examples=[0.78]
)
prefix: str = Field(
...,
description="The prefix corresponding to the ontology/database from which the term comes",
example="ido",
examples=["ido"],
)
identifier: str = Field(
...,
description="The local unique identifier for the term in the ontology/database denoted by the prefix",
example="0000511",
examples=["0000511"],
)
curie: str = Field(
...,
description="The compact URI that combines the prefix and local identifier.",
example="ido:0000511",
examples=["ido:0000511"],
)
name: str = Field(
..., description="The standard entity name for the term", example="infected population"
..., description="The standard entity name for the term", examples=["infected population"]
)
status: str = Field(..., description="The match status, e.g., name, synonym", example="name")
status: str = Field(..., description="The match status, e.g., name, synonym", examples=["name"])

@classmethod
def from_scored_match(cls, scored_match: ScoredMatch) -> "GroundResult":
Expand Down
Loading
Loading