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

feat: extend models #164

Merged
merged 3 commits into from
Mar 10, 2022
Merged
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 siibra/core/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Url(ConfigBaseModel):

class DatasetJsonModel(ConfigBaseModel):
id: str = Field(..., alias="@id")
type: str = Field("siibra/base-dataset", const=True)
type: str = Field("siibra/core/dataset", const=True)
metadata: DatasetVersionModel
urls: List[Url]

Expand Down
33 changes: 30 additions & 3 deletions siibra/core/parcellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Model as BrainAtlasVersionModel,
HasTerminologyVersion,
)
from ..openminds.base import ConfigBaseModel
from ..openminds.base import ConfigBaseModel, SiibraAtIdModel

from datetime import date
from typing import List, Optional, Set, Union
Expand All @@ -43,13 +43,21 @@ class AtlasType:
PROBABILISTIC_ATLAS="https://openminds.ebrains.eu/instances/atlasType/probabilisticAtlas"


class SiibraParcellationVersionModel(ConfigBaseModel):
name: str
deprecated: Optional[bool]
prev: Optional[SiibraAtIdModel]
next: Optional[SiibraAtIdModel]


class SiibraParcellationModel(ConfigBaseModel):
id: str = Field(..., alias="@id")
type: str = Field(SIIBRA_PARCELLATION_MODEL_TYPE, const=True, alias="@type")
name: str
modality: Optional[str]
datasets: List[DatasetJsonModel]
brain_atlas_versions: List[BrainAtlasVersionModel] = Field(..., alias="brainAtlasVersions")
version: Optional[SiibraParcellationVersionModel]


# NOTE : such code could be used to automatically resolve
Expand All @@ -62,7 +70,7 @@ class SiibraParcellationModel(ConfigBaseModel):
# pass


class ParcellationVersion:
class ParcellationVersion(JSONSerializable):
def __init__(
self, name=None, collection=None, prev_id=None, next_id=None, deprecated=False
):
Expand Down Expand Up @@ -133,6 +141,24 @@ def _from_json(cls, obj):
next_id=obj.get("@next", None),
deprecated=obj.get("deprecated", False),
)

@property
def model_id(self):
return self.name

def to_model(self, **kwargs) -> SiibraParcellationVersionModel:
assert self.prev is None or isinstance(self.prev, Parcellation), f"parcellationVersion to_model failed. expected .prev, if defined, to be instance of Parcellation, but is {self.prev.__class__} instead"
assert self.next is None or isinstance(self.next, Parcellation), f"parcellationVersion to_model failed. expected .next, if defined, to be instance of Parcellation, but is {self.next.__class__} instead"
return SiibraParcellationVersionModel(
name=self.name,
deprecated=self.deprecated,
prev=SiibraAtIdModel(
id=self.prev.model_id
) if self.prev is not None else None,
next=SiibraAtIdModel(
id=self.next.model_id
) if self.next is not None else None,
)


@provide_registry
Expand Down Expand Up @@ -575,5 +601,6 @@ def to_model(self, **kwargs) -> SiibraParcellationModel:
short_name=self.name[:30],
version_identifier=f"{self.version} in {spc.to_model().full_name}",
version_innovation="",
) for spc in self.supported_spaces]
) for spc in self.supported_spaces],
version=self.version.to_model(**kwargs) if self.version is not None else None
)
2 changes: 1 addition & 1 deletion siibra/features/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

class ConnectivityMatrixDataModel(ConfigBaseModel):
id: str = Field(..., alias="@id")
type: str = Field("siibra/connectivity", const=True)
type: str = Field("siibra/features/connectivity", const=True)
name: str
parcellations: List[Dict[str, str]]
matrix: Optional[NpArrayDataModel]
Expand Down
1 change: 1 addition & 0 deletions siibra/features/ieeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class IEEGElectrodeModel(ConfigBaseModel):

class IEEGSessionModel(ConfigBaseModel):
id: str = Field(..., alias="@id")
type: str = Field('siibra/features/ieegSession', const=True)
dataset: DatasetJsonModel
sub_id: str
electrodes: Dict[str, IEEGElectrodeModel]
Expand Down
4 changes: 2 additions & 2 deletions siibra/features/receptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class ReceptorDataModel(ConfigBaseModel):

class ReceptorDatasetModel(DatasetJsonModel):
data: Optional[ReceptorDataModel]
type: str = Field("siibra/receptor", const=True)
type: str = Field("siibra/features/receptor", const=True)


class DensityFingerprint:
Expand Down Expand Up @@ -464,7 +464,7 @@ def model_id(self):

def to_model(self, detail=False, **kwargs) -> ReceptorDatasetModel:
base_dict = dict(super().to_model(detail=detail, **kwargs).dict())
base_dict["type"] = "siibra/receptor"
base_dict["type"] = "siibra/features/receptor"
if not detail:
return ReceptorDatasetModel(
**base_dict,
Expand Down
6 changes: 5 additions & 1 deletion siibra/features/voi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pydantic import Field
from .feature import SpatialFeature
from .query import FeatureQuery

Expand All @@ -28,6 +29,7 @@


class VOIDataModel(DatasetJsonModel):
type: str = Field('siibra/features/voi', const=True)
volumes: List[VolumeModel]
location: BoundingBoxModel

Expand Down Expand Up @@ -73,10 +75,12 @@ def model_id(self):

def to_model(self, **kwargs) -> VOIDataModel:
super_model = super().to_model(**kwargs)
super_model_dict = super_model.dict()
super_model_dict["type"] = "siibra/features/voi"
return VOIDataModel(
location=self.location.to_model(**kwargs),
volumes=[vol.to_model(**kwargs) for vol in self.volumes],
**super_model.dict()
**super_model_dict,
)


Expand Down