Skip to content

Commit

Permalink
Shared models v2 (#114)
Browse files Browse the repository at this point in the history
* Add tests to create all models defined in bia_data_model

* Change type of size_t in ImageRepresentation to int

* Fix minor issues before merging PR

* Use conlist for Specimen, make description mandatory for Study
  • Loading branch information
kbab authored Jun 28, 2024
1 parent 3c6e535 commit 89bd4b7
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 7 deletions.
5 changes: 4 additions & 1 deletion bia-shared-datamodels/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ license = "Apache Software License 2.0"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.9"
python = "^3.10"
pydantic = {version = "^2", extras = ["email"]}
pydantic-2-mermaid = "^0.7"
pytest = "^7"

[tool.poetry.group.dev.dependencies]
ipython = "^8.25.0"

[tool.poetry-dynamic-versioning]
enable = true
vcs = "git"
Expand Down
8 changes: 5 additions & 3 deletions bia-shared-datamodels/src/bia_models/bia_data_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import semantic_models
from pydantic import BaseModel, Field, AnyUrl
from pydantic import BaseModel, Field, AnyUrl, conlist
from typing import List, Optional, Union
from uuid import UUID

Expand All @@ -26,6 +26,8 @@ class Study(
):
experimental_imaging_component: List[UUID] = Field()
annotation_component: List[UUID] = Field()
author: conlist(item_type=semantic_models.Contributor, min_length=1) = Field()
description: str = Field()


class FileReference(
Expand Down Expand Up @@ -57,8 +59,8 @@ class ExperimentalImagingDataset(


class Specimen(semantic_models.Specimen):
preparation_method: List[UUID] = Field()
sample_of: List[UUID] = Field()
preparation_method: conlist(item_type=UUID, min_length=1) = Field()
sample_of: conlist(item_type=UUID, min_length=1) = Field()


class ExperimentallyCapturedImage(
Expand Down
10 changes: 7 additions & 3 deletions bia-shared-datamodels/src/bia_models/semantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ class Study(DocumentMixin):
attribute: dict = Field(
description="""Freeform key-value pairs from user provided metadata (e.g. filelist data) and experimental fields."""
)
# Override optional description in DocumentMixin
description: str = Field(
None, description="""Brief description of the scientific document."""
)


class Publication(DocumentMixin):
Expand Down Expand Up @@ -296,9 +300,9 @@ class ImageRepresentation(BaseModel):
None,
description="""Number of channels of the image.""",
)
size_t: Optional[float] = Field(
size_t: Optional[int] = Field(
None,
description="""temporal dimension of the data array of the image (in seconds???).""",
description="""Size of temporal dimension of the data array of the image).""",
)
image_viewer_setting: Optional[List[RenderedView]] = Field(
None,
Expand Down Expand Up @@ -404,7 +408,7 @@ class ImageAcquisition(BaseModel):
imaging_instrument_description: str = Field(
description="""Names, types, or description of how the instruments used to create the image."""
)
image_acquistion_parameters: str = Field(
image_acquisition_parameters: str = Field(
description="""Parameters relevant to how the image was taken, such as instrument settings."""
)
fbbi_id: List[str] = Field(
Expand Down
Empty file.
59 changes: 59 additions & 0 deletions bia-shared-datamodels/test/test_shared_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from uuid import uuid4
import pytest
from pydantic import ValidationError
from . import utils
from .utils import (
bia_data_model,
semantic_models,
)


@pytest.mark.parametrize(
("expected_model_type", "model_creation_func",),
(
(bia_data_model.Study, utils.get_template_study,),
(bia_data_model.FileReference, utils.get_template_file_reference,),
(bia_data_model.ImageRepresentation, utils.get_template_image_representation,),
(
bia_data_model.ExperimentalImagingDataset,
utils.get_template_experimental_imaging_dataset,
),
(bia_data_model.Specimen, utils.get_template_specimen,),
(
bia_data_model.ExperimentallyCapturedImage,
utils.get_template_experimentally_captured_image,
),
(bia_data_model.ImageAcquisition, utils.get_template_image_acquisition,),
(
bia_data_model.SpecimenPrepartionProtocol,
utils.get_template_specimen_preparation_protocol,
),
(bia_data_model.BioSample, utils.get_template_biosample,),
(
bia_data_model.ImageAnnotationDataset,
utils.get_template_image_annotation_dataset,
),
(
bia_data_model.AnnotationFileReference,
utils.get_template_annotation_file_reference,
),
(bia_data_model.DerivedImage, utils.get_template_derived_image,),
(bia_data_model.AnnotationMethod, utils.get_template_annotation_method,),
),
)
def test_create_models(expected_model_type, model_creation_func):
expected_model = model_creation_func()
assert type(expected_model) is expected_model_type


def test_create_specimen_with_empty_lists_fails():
with pytest.raises(ValidationError):
specimen = bia_data_model.Specimen.model_validate(
{"sample_of": [], "preparation_method": [],}
)
specimen = bia_data_model.Specimen.model_validate(
{"sample_of": [uuid4()], "preparation_method": [],}
)
specimen = bia_data_model.Specimen.model_validate(
{"sample_of": [], "preparation_method": [uuid4()],}
)
Loading

0 comments on commit 89bd4b7

Please sign in to comment.