From 5a7c575974212556be9bfe7128c428808ef69460 Mon Sep 17 00:00:00 2001 From: Anita Caron Date: Fri, 24 Mar 2023 17:17:42 +0000 Subject: [PATCH 1/4] create FacetInfo and add to Page model --- src/models.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/models.py b/src/models.py index fb93fb0..80e8eb8 100644 --- a/src/models.py +++ b/src/models.py @@ -7,11 +7,6 @@ from sssom_schema import Mapping, MappingSet, MappingRegistry, MappingSetReference T = TypeVar("T") -# class ResponseMapping(BaseModel): -# subject_id: Mapping.subject_id -# predicate_id: Mapping.predicate_id -# object_id: Mapping.object_id -# mapping_justification: Mapping.mapping_justification class PaginationParams(BaseModel): request: Request @@ -30,9 +25,15 @@ class PaginationInfo(BaseModel): total_pages: int +class FacetInfo(BaseModel): + mapping_justification: dict + predicate: dict + + class Page(GenericModel, Generic[T]): data: List[T] pagination: PaginationInfo + facets: FacetInfo class SearchEntity(BaseModel): From 5fbd0c3aa0e4252c498e957c69eab255d17f9e17 Mon Sep 17 00:00:00 2001 From: Anita Caron Date: Fri, 24 Mar 2023 17:20:12 +0000 Subject: [PATCH 2/4] add facets in the page result --- src/utils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/utils.py b/src/utils.py index ff3a802..3ffcd6c 100644 --- a/src/utils.py +++ b/src/utils.py @@ -3,10 +3,11 @@ import itertools import functools import math +from collections import Counter from fastapi import Request -from .models import Page, PaginationInfo +from .models import Page, PaginationInfo, FacetInfo def _replace_page_param(request: Request, new_page: Union[int, None]) -> Union[str, None]: @@ -24,7 +25,7 @@ def paginate(iterable: Iterable[T], page: int, limit: int, request: Request) -> prev_page = None next_page = None data = [] - iter_data, iter_total = itertools.tee(iterable) + iter_data, iter_total, iter_facets = itertools.tee(iterable, 3) total_items = functools.reduce(lambda prev, curr: prev + 1, iter_total, 0) total_pages = math.ceil(total_items / limit) for idx, item in enumerate(iter_data): @@ -44,6 +45,15 @@ def paginate(iterable: Iterable[T], page: int, limit: int, request: Request) -> total_items=total_items, total_pages=total_pages ), + facets=_create_facets(iter_facets) + ) + +def _create_facets(data: Iterable[T]) -> FacetInfo: + iter_mj, iter_pred = itertools.tee(data) + + return FacetInfo( + mapping_justification=dict(Counter(list(map(lambda d: d["mapping_justification"], iter_mj)))), + predicate=dict(Counter(list(map(lambda d: d["predicate_id"], iter_pred)))), ) def parser_filter(datamodel: T, filter: Union[List[str], None] = None) -> Union[List[dict], None]: From 6925dd5033cc2ff78707b9ba18c1559bc874c694 Mon Sep 17 00:00:00 2001 From: Anita Caron Date: Mon, 27 Mar 2023 21:25:17 +0100 Subject: [PATCH 3/4] change facet name to `predicate_id` --- src/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models.py b/src/models.py index 80e8eb8..0d731bc 100644 --- a/src/models.py +++ b/src/models.py @@ -27,7 +27,7 @@ class PaginationInfo(BaseModel): class FacetInfo(BaseModel): mapping_justification: dict - predicate: dict + predicate_id: dict class Page(GenericModel, Generic[T]): From 913342c53bab2f808c51197ece81761cacc2b3d7 Mon Sep 17 00:00:00 2001 From: Anita Caron Date: Tue, 28 Mar 2023 08:42:51 +0100 Subject: [PATCH 4/4] use countby in facets --- requirements.txt | 3 ++- src/utils.py | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 1f82926..98952d9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ sssom-schema sssom oaklib==v0.1.71 pydantic[dotenv] -curies \ No newline at end of file +curies +toolz \ No newline at end of file diff --git a/src/utils.py b/src/utils.py index 3ffcd6c..bad591b 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,9 +1,9 @@ from typing import Iterable, TypeVar, Union, List import itertools -import functools import math -from collections import Counter +from toolz.recipes import countby +from toolz.itertoolz import count from fastapi import Request @@ -26,7 +26,7 @@ def paginate(iterable: Iterable[T], page: int, limit: int, request: Request) -> next_page = None data = [] iter_data, iter_total, iter_facets = itertools.tee(iterable, 3) - total_items = functools.reduce(lambda prev, curr: prev + 1, iter_total, 0) + total_items = count(iter_total) total_pages = math.ceil(total_items / limit) for idx, item in enumerate(iter_data): if idx == start - 1: @@ -52,8 +52,8 @@ def _create_facets(data: Iterable[T]) -> FacetInfo: iter_mj, iter_pred = itertools.tee(data) return FacetInfo( - mapping_justification=dict(Counter(list(map(lambda d: d["mapping_justification"], iter_mj)))), - predicate=dict(Counter(list(map(lambda d: d["predicate_id"], iter_pred)))), + mapping_justification=countby(lambda d: d["mapping_justification"], iter_mj), + predicate_id=countby(lambda d: d["predicate_id"], iter_pred), ) def parser_filter(datamodel: T, filter: Union[List[str], None] = None) -> Union[List[dict], None]: