Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 7 additions & 8 deletions dicomtrolley/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
TypeVar,
)

from pydantic import Field, ValidationError, field_validator
from pydantic import ConfigDict, Field, ValidationError, field_validator
from pydantic.main import BaseModel
from pydicom.datadict import tag_for_keyword
from pydicom.dataset import Dataset
Expand Down Expand Up @@ -187,8 +187,8 @@ class DICOMObject(BaseModel, DICOMDownloadable):
a DICOM dataset
"""

class Config:
arbitrary_types_allowed = True # allows the use of Dataset type below
# allows the use of Dataset type below
model_config = ConfigDict(arbitrary_types_allowed=True)

uid: str
data: Dataset
Expand Down Expand Up @@ -614,9 +614,8 @@ class Query(BaseModel):
max_study_date: Optional[datetime] = None
min_study_date: Optional[datetime] = None
include_fields: Optional[List[str]] = Field(default_factory=list)

class Config:
extra = "forbid" # raise ValueError when passing an unknown keyword to init
# raise ValueError when passing an unknown keyword to init
model_config = ConfigDict(extra="forbid")

@classmethod
def init_from_query(
Expand All @@ -643,7 +642,7 @@ def init_from_query(
this class
"""
# remove empty, None and 0 values
params = {key: val for key, val in query.dict().items() if val}
params = {key: val for key, val in query.model_dump().items() if val}
try:
return cls(**params)
except ValidationError as e:
Expand Down Expand Up @@ -672,7 +671,7 @@ def include_fields_check(cls, include_fields, values): # noqa: B902, N805

def to_short_string(self):
"""A more information-dense str repr. For human reading"""
filled_fields = {key: val for key, val in self.dict().items() if val}
filled_fields = {key: val for key, val in self.model_dump().items() if val}
filled_fields["query_level"] = filled_fields["query_level"].value
return f"{type(self).__name__}: {filled_fields}"

Expand Down
8 changes: 4 additions & 4 deletions dicomtrolley/dicom_qr.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)
from dicomtrolley.exceptions import DICOMTrolleyError
from dicomtrolley.parsing import DICOMParseTree
from pydantic import ConfigDict


class QueryRetrieveLevels:
Expand Down Expand Up @@ -59,9 +60,8 @@ class DICOMQuery(ExtendedQuery):
Modality: str = ""
ProtocolName: str = ""
StudyID: str = ""

class Config:
extra = "forbid" # raise ValueError when passing an unknown keyword to init
# raise ValueError when passing an unknown keyword to init
model_config = ConfigDict(extra="forbid")

@staticmethod
def get_default_include_fields(query_level):
Expand Down Expand Up @@ -95,7 +95,7 @@ def as_parameters(self) -> Dict[str, str]:

# remove non-DICOM parameters and replace with DICOM tags based on them
parameters = {
x: y for x, y in self.dict().items()
x: y for x, y in self.model_dump().items()
} # all params for query
parameters["StudyDate"] = self.get_study_date(
parameters.pop("min_study_date"), parameters.pop("max_study_date")
Expand Down
16 changes: 8 additions & 8 deletions dicomtrolley/mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ class MintQuery(ExtendedQuery):
limit: int = 0 # how many results to return. 0 = all

@model_validator(mode="after")
def min_max_study_date_xor(cls, values): # noqa: B902, N805
def min_max_study_date_xor(self): # noqa: B902, N805
"""Min and max should both be given or both be empty"""
min_date = values.min_study_date
max_date = values.max_study_date
min_date = self.min_study_date
max_date = self.max_study_date
if min_date and not max_date:
raise ValueError(
f"min_study_date parameter was passed"
Expand All @@ -175,7 +175,7 @@ def min_max_study_date_xor(cls, values): # noqa: B902, N805
f"max_study_date parameter was passed ({max_date}), "
f"but min_study_date was not. Both need to be given"
)
return values
return self

@model_validator(mode="after")
def include_fields_check(self):
Expand Down Expand Up @@ -204,7 +204,7 @@ def __str__(self):

def as_parameters(self):
"""All non-empty query parameters. For use as url parameters"""
parameters = {x: y for x, y in self.dict().items() if y}
parameters = {x: y for x, y in self.model_dump().items() if y}

if "min_study_date" in parameters:
parameters["min_study_date"] = parameters[
Expand Down Expand Up @@ -332,6 +332,6 @@ def parse_attribs(element):
return dataset


MintInstance.update_forward_refs() # enables pydantic validation
MintSeries.update_forward_refs()
MintStudy.update_forward_refs()
MintInstance.model_rebuild() # enables pydantic validation
MintSeries.model_rebuild()
MintStudy.model_rebuild()
6 changes: 3 additions & 3 deletions dicomtrolley/qido_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def uri_search_params(self) -> Dict[str, Union[str, List[str]]]:
}
other_search_params = {
key: val
for key, val in self.dict().items()
for key, val in self.model_dump().items()
if key not in exclude_fields
}

Expand Down Expand Up @@ -182,7 +182,7 @@ def assert_parents_filled(a_hierarchy, value_dict):
else:
return assert_parents_filled(a_hierarchy, value_dict)

assert_parents_filled(order, self.dict())
assert_parents_filled(order, self.model_dump())
return self

@model_validator(mode="after")
Expand All @@ -198,7 +198,7 @@ def assert_key_exists(values_in, query_level_in, missing_key_in):
f"a QIDO-RS relational query"
)

values = self.dict()
values = self.model_dump()
if query_level == QueryLevels.STUDY:
pass # Fine. you can always look for some studies
elif query_level == QueryLevels.SERIES:
Expand Down
Loading