Skip to content

Commit

Permalink
template files for count references transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
sbilge committed Jul 16, 2024
1 parent c8ce093 commit 01db23a
Show file tree
Hide file tree
Showing 16 changed files with 463 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from pydantic import GetJsonSchemaHandler, ValidationInfo

from metldata.builtin_transformations.infer_relations.path.path_str import (
from metldata.builtin_transformations.common.path.path_str import (
PATH_PATTERN,
ValidationError,
clean_path_str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import re

from metldata.builtin_transformations.infer_relations.path.path_elements import (
from metldata.builtin_transformations.common.path.path_elements import (
RelationPathElement,
RelationPathElementType,
)
Expand Down
20 changes: 20 additions & 0 deletions src/metldata/builtin_transformations/count_references/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2021 - 2024 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln
# for the German Human Genome-Phenome Archive (GHGA)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A transformation to count the references."""

# shortcuts:
from metldata.builtin_transformations.count_references.main import ( # noqa: F401
COUNT_REFERENCES_TRANSFORMATION,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2021 - 2024 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln
# for the German Human Genome-Phenome Archive (GHGA)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"Assumptions for count references transformation"

from typing import Any

from schemapack.spec.schemapack import SchemaPack

from metldata.builtin_transformations.common.path.path import RelationPath
from metldata.builtin_transformations.common.path.path_elements import (
RelationPathElementType,
)
from metldata.transform.base import ModelAssumptionError


def assert_path_classes_and_relations_exist(model: SchemaPack, path: RelationPath):
"""Make sure that all classes and relations defined in the provided path exist in
the provided model.
Raises:
ModelAssumptionError:
if the model does not fulfill the assumptions.
"""
for path_element in path.elements:
if path_element.source not in model.classes:
raise ModelAssumptionError(
f"Class {path_element.source} not found in model."
)

if path_element.target not in model.classes:
raise ModelAssumptionError(
f"Class {path_element.target} not found in model."
)

if path_element.type_ == RelationPathElementType.ACTIVE:
if (
path_element.property
not in model.classes[path_element.source].relations
):
raise ModelAssumptionError(
f"Relation property {path_element.property} not found in class"
f" {path_element.source}."
)

return


def check_model_assumptions(schema: SchemaPack, instructions_by_class: Any) -> None:
"""Check the model assumptions for the count references transformation."""
return None
32 changes: 32 additions & 0 deletions src/metldata/builtin_transformations/count_references/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2021 - 2024 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln
# for the German Human Genome-Phenome Archive (GHGA)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Models used to describe count content properties that shall be calculated and added."""

from typing import Any

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict


class CountReferencesConfig(BaseSettings):
"""Config containing content properties to be deleted from models and data."""

model_config = SettingsConfigDict(extra="forbid")

count_references: Any = Field(
...,
description=("description"),
examples=[],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2021 - 2024 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln
# for the German Human Genome-Phenome Archive (GHGA)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Logic for transforming data."""

from schemapack.spec.datapack import DataPack

# from metldata.transform.base import EvitableTransformationError


def count_references(*, data: DataPack) -> DataPack:
"""Count
Args:
data:
Returns:
The data with
"""
modified_data = data.model_copy(deep=True)

# TODO modifications

return modified_data
77 changes: 77 additions & 0 deletions src/metldata/builtin_transformations/count_references/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2021 - 2024 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln
# for the German Human Genome-Phenome Archive (GHGA)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A transformation to count references."""

from schemapack.spec.datapack import DataPack
from schemapack.spec.schemapack import SchemaPack

from metldata.builtin_transformations.count_references.assumptions import (
check_model_assumptions,
)
from metldata.builtin_transformations.count_references.config import (
CountReferencesConfig,
)
from metldata.builtin_transformations.count_references.data_transform import (
count_references,
)
from metldata.builtin_transformations.count_references.model_transform import (
add_count_references,
)
from metldata.transform.base import DataTransformer, TransformationDefinition


class CountReferencesTransformer(DataTransformer[CountReferencesConfig]):
"""A transformer that counts the references and adds them to content properties."""

def transform(self, data: DataPack) -> DataPack:
"""Transforms data.
Args:
data: The data as DataPack to be transformed.
"""
return count_references(data=data)


def check_model_assumptions_wrapper(
model: SchemaPack, config: CountReferencesConfig
) -> None:
"""Check the assumptions of the model.
Raises:
ModelAssumptionError:
if the model does not fulfill the assumptions.
"""
check_model_assumptions(schema=model, instructions_by_class=config.count_references)


def transform_model(model: SchemaPack, config: CountReferencesConfig) -> SchemaPack:
"""Transform the data model.
Raises:
DataModelTransformationError:
if the transformation fails.
"""
return add_count_references(
model=model, instructions_by_class=config.count_references
)


COUNT_REFERENCES_TRANSFORMATION = TransformationDefinition[CountReferencesConfig](
config_cls=CountReferencesConfig,
check_model_assumptions=check_model_assumptions_wrapper,
transform_model=transform_model,
data_transformer_factory=CountReferencesTransformer,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2021 - 2024 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln
# for the German Human Genome-Phenome Archive (GHGA)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Model transformation logic for the 'count references' transformation"""

from schemapack.spec.schemapack import (
# ClassDefinition,
SchemaPack,
)

# from metldata.transform.base import EvitableTransformationError


def add_count_references(
*, model: SchemaPack, instructions_by_class: dict[str, list[str]]
) -> SchemaPack:
"""Delete content properties from a model.
Args:
model:
The model based on SchemaPack to
Returns:
The model with the
"""
# TODO model transform logic for count references

return model
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

from schemapack.spec.schemapack import SchemaPack

from metldata.builtin_transformations.infer_relations.path.path import (
from metldata.builtin_transformations.common.path.path import (
RelationPath,
)
from metldata.builtin_transformations.infer_relations.path.path_elements import (
from metldata.builtin_transformations.common.path.path_elements import (
RelationPathElementType,
)
from metldata.builtin_transformations.infer_relations.relations import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from pydantic import BaseModel, ConfigDict, Field, model_validator

from metldata.builtin_transformations.infer_relations.path.path import (
from metldata.builtin_transformations.common.path.path import (
RelationPath,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 - 2024 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln
# for the German Human Genome-Phenome Archive (GHGA)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

count_references:
- class_name: Dataset
target_content:
object_path: "file_summary"
property_name: "count"
source_relation_path: "Dataset(files)>File"
- class_name: Sample
target_content:
object_path: "file_summary"
property_name: "count"
source_relation_path: "Sample(files)>File"
- class_name: Experiment
target_content:
object_path: "sample_summary"
property_name: "count"
source_relation_path: "Experiment(samples)>Sample"
Loading

0 comments on commit 01db23a

Please sign in to comment.