-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(annotations): move logic from images.py
- Loading branch information
Showing
4 changed files
with
111 additions
and
30 deletions.
There are no files selected for viewing
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,53 @@ | ||
from functools import lru_cache, partial | ||
from typing import Sequence | ||
|
||
|
||
class DeleteCallbackRef: | ||
def __init__(self, del_callback, value): | ||
self.del_callback = del_callback | ||
self.value = value | ||
|
||
def __del__(self): | ||
self.del_callback() | ||
|
||
|
||
ANNOTATION_CACHE_SIZE = 20 | ||
|
||
|
||
def get_annotations_from_dataset( | ||
context, add_to_cache_callback, delete_from_cache_callback, dataset_id: str | ||
): | ||
dataset = context.dataset | ||
annotations = [ | ||
annotation | ||
for annotation in dataset.anns.values() | ||
if str(annotation["image_id"]) == dataset_id | ||
] | ||
add_to_cache_callback(dataset_id, annotations) | ||
with_id = partial(delete_from_cache_callback, dataset_id) | ||
return DeleteCallbackRef(with_id, annotations) | ||
|
||
|
||
class GroundTruthAnnotations: | ||
def __init__( | ||
self, | ||
context, # for dataset | ||
add_to_cache_callback, | ||
delete_from_cache_callback, | ||
): | ||
with_callbacks = partial( | ||
get_annotations_from_dataset, | ||
context, | ||
add_to_cache_callback, | ||
delete_from_cache_callback, | ||
) | ||
self.get_annotations_for_image = lru_cache(maxsize=ANNOTATION_CACHE_SIZE)(with_callbacks) | ||
|
||
def get_annotations(self, dataset_ids: Sequence[str]): | ||
return { | ||
dataset_id: self.get_annotations_for_image(dataset_id).value | ||
for dataset_id in dataset_ids | ||
} | ||
|
||
def cache_clear(self): | ||
self.get_annotations_for_image.cache_clear() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from functools import partial | ||
from typing import Any, Type | ||
from .annotations import GroundTruthAnnotations | ||
from trame.app import get_server | ||
from trame.decorators import TrameApp, change | ||
from nrtk_explorer.app.images.image_ids import ( | ||
image_id_to_result_id, | ||
) | ||
from nrtk_explorer.app.trame_utils import delete_state | ||
|
||
server = get_server() | ||
state, context, ctrl = server.state, server.context, server.controller | ||
|
||
|
||
def add_annotation_to_state(state: Any, image_id: str, annotations: Any): | ||
state[image_id_to_result_id(image_id)] = annotations | ||
|
||
|
||
def remove_annotation_from_state(state: Any, image_id: str): | ||
delete_state(state, image_id_to_result_id(image_id)) | ||
|
||
|
||
@TrameApp() | ||
class StatefulAnnotations: | ||
def __init__( | ||
self, | ||
annotations_factory_class: Type[GroundTruthAnnotations], | ||
context=context, # for dataset | ||
add_to_cache_callback=partial(add_annotation_to_state, state), | ||
delete_from_cache_callback=partial(remove_annotation_from_state, state), | ||
server=server, | ||
): | ||
self.annotations_factory = annotations_factory_class( | ||
context, add_to_cache_callback, delete_from_cache_callback | ||
) | ||
self.server = server | ||
|
||
@change("current_dataset") | ||
def _on_dataset(self, **kwargs): | ||
self.annotations_factory.cache_clear() |
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