generated from ghga-de/microservice-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
template files for count references transformation
- Loading branch information
Showing
16 changed files
with
463 additions
and
5 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/metldata/builtin_transformations/count_references/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
63 changes: 63 additions & 0 deletions
63
src/metldata/builtin_transformations/count_references/assumptions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/metldata/builtin_transformations/count_references/config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=[], | ||
) |
37 changes: 37 additions & 0 deletions
37
src/metldata/builtin_transformations/count_references/data_transform.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
src/metldata/builtin_transformations/count_references/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
38 changes: 38 additions & 0 deletions
38
src/metldata/builtin_transformations/count_references/model_transform.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
tests/fixtures/example_transformations/count_references/multiple/config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Oops, something went wrong.