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

Add support for predictions app #364

Merged
merged 4 commits into from
Nov 8, 2024
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
10 changes: 10 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ Added
and propagate this change in ``Data`` resource


==========
Unreleased
==========

Added
-----
- Add support for predictions
- Add version to annotation field


===================
21.2.1 - 2024-10-08
===================
Expand Down
45 changes: 43 additions & 2 deletions src/resdk/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import tqdm

from resdk.resources import AnnotationField, DescriptorSchema, Process
from resdk.resources import AnnotationField, DescriptorSchema, PredictionField, Process
from resdk.resources.base import BaseResource


Expand Down Expand Up @@ -419,7 +419,48 @@ def _fetch(self):
if missing:
# Get corresponding annotation field details in a single query and attach it to
# the values.
for field in self.resolwe.annotation_field.filter(id__in=missing.keys()):
for field in self.resolwe.annotation_field.filter(
id__in=missing.keys()
).iterate():
for value in missing[field.id]:
value._field = field
value._original_values["field"] = field._original_values


class PredictionFieldQuery(ResolweQuery):
"""Add additional method to the prediction field query."""

def from_path(self, full_path: str) -> "PredictionField":
"""Get the PredictionField from full path.

:raises LookupError: when field at the specified path does not exist.
"""
group_name, field_name = full_path.split(".", maxsplit=1)
return self.get(name=field_name, group__name=group_name)


class PredictionValueQuery(ResolweQuery):
"""Populate prediction fields with a single query."""

def _fetch(self):
"""Make request to the server and populate cache.

Fetch all values and their fields with 2 queries.
"""
# Execute the query in a single request.
super()._fetch()

missing = collections.defaultdict(list)
for value in self._cache:
if value._field is None:
missing[value.field_id].append(value)

if missing:
# Get corresponding annotation field details in a single query and attach it to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get corresponding prediction field details...

# the values.
for field in self.resolwe.prediction_field.filter(
id__in=missing.keys()
).iterate():
for value in missing[field.id]:
value._field = field
value._original_values["field"] = field._original_values
30 changes: 21 additions & 9 deletions src/resdk/resolwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@

from .constants import CHUNK_SIZE
from .exceptions import ValidationError, handle_http_exception
from .query import AnnotationFieldQuery, AnnotationValueQuery, ResolweQuery
from .query import (
AnnotationFieldQuery,
AnnotationValueQuery,
PredictionFieldQuery,
PredictionValueQuery,
ResolweQuery,
)
from .resources import (
AnnotationField,
AnnotationValue,
Expand All @@ -43,6 +49,8 @@
Geneset,
Group,
Metadata,
PredictionField,
PredictionValue,
Process,
Relation,
Sample,
Expand Down Expand Up @@ -106,26 +114,30 @@ class Resolwe:
# Map between resource and Query map. Default in ResorweQuery, only overrides must
# be listed here.
resource_query_class = {
AnnotationValue: AnnotationValueQuery,
AnnotationField: AnnotationFieldQuery,
AnnotationValue: AnnotationValueQuery,
PredictionField: PredictionFieldQuery,
PredictionValue: PredictionValueQuery,
}

# Map resource class to ResolweQuery name
resource_query_mapping = {
AnnotationField: "annotation_field",
AnnotationValue: "annotation_value",
Data: "data",
Collection: "collection",
Sample: "sample",
Relation: "relation",
Process: "process",
Data: "data",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reordering should go into a separate commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

DescriptorSchema: "descriptor_schema",
User: "user",
Group: "group",
Feature: "feature",
Mapping: "mapping",
Geneset: "geneset",
Group: "group",
Mapping: "mapping",
Metadata: "metadata",
PredictionField: "prediction_field",
PredictionValue: "prediction_value",
Process: "process",
Relation: "relation",
Sample: "sample",
User: "user",
}
# Map ResolweQuery name to it's slug_field
slug_field_mapping = {
Expand Down
16 changes: 16 additions & 0 deletions src/resdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@
:members:
:inherited-members:

.. autoclass:: resdk.resources.PredictionValue
:members:
:inherited-members:

.. autoclass:: resdk.resources.PredictionGroup
:members:
:inherited-members:

.. autoclass:: resdk.resources.PredictionField
:members:
:inherited-members:

.. autoclass:: resdk.resources.User
:members:
:inherited-members:
Expand Down Expand Up @@ -98,6 +110,7 @@
from .descriptor import DescriptorSchema
from .geneset import Geneset
from .metadata import Metadata
from .predictions import PredictionField, PredictionGroup, PredictionValue
from .process import Process
from .relation import Relation
from .sample import Sample
Expand All @@ -113,6 +126,9 @@
"Geneset",
"Group",
"Metadata",
"PredictionField",
"PredictionGroup",
"PredictionValue",
"Sample",
"Process",
"Relation",
Expand Down
1 change: 1 addition & 0 deletions src/resdk/resources/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class AnnotationField(BaseResource):
"validator_regex",
"vocabulary",
"required",
"version",
)

def __init__(self, resolwe: "Resolwe", **model_data):
Expand Down
Loading