lightly-studio 0.3.2__py3-none-any.whl → 0.3.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of lightly-studio might be problematic. Click here for more details.

Files changed (163) hide show
  1. lightly_studio/__init__.py +1 -1
  2. lightly_studio/api/app.py +8 -4
  3. lightly_studio/api/db_tables.py +0 -3
  4. lightly_studio/api/routes/api/annotation.py +26 -0
  5. lightly_studio/api/routes/api/annotations/__init__.py +7 -0
  6. lightly_studio/api/routes/api/annotations/create_annotation.py +52 -0
  7. lightly_studio/api/routes/api/caption.py +30 -0
  8. lightly_studio/api/routes/api/dataset.py +3 -5
  9. lightly_studio/api/routes/api/embeddings2d.py +136 -0
  10. lightly_studio/api/routes/api/export.py +73 -0
  11. lightly_studio/api/routes/api/metadata.py +57 -1
  12. lightly_studio/api/routes/api/selection.py +87 -0
  13. lightly_studio/core/add_samples.py +138 -9
  14. lightly_studio/core/dataset.py +174 -63
  15. lightly_studio/core/dataset_query/dataset_query.py +5 -0
  16. lightly_studio/dataset/env.py +4 -0
  17. lightly_studio/dataset/file_utils.py +13 -2
  18. lightly_studio/dataset/loader.py +2 -62
  19. lightly_studio/dataset/mobileclip_embedding_generator.py +3 -2
  20. lightly_studio/db_manager.py +10 -4
  21. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/0.B3oFNb6O.css +1 -0
  22. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/2.CkOblLn7.css +1 -0
  23. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/Samples.CIbricz7.css +1 -0
  24. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/_layout.7Ma7YdVg.css +1 -0
  25. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/{useFeatureFlags.CV-KWLNP.css → _layout.CefECEWA.css} +1 -1
  26. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/transform.2jKMtOWG.css +1 -0
  27. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/-DXuGN29.js +1 -0
  28. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/{Ccq4ZD0B.js → B7302SU7.js} +1 -1
  29. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/BeWf8-vJ.js +1 -0
  30. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Bqz7dyEC.js +1 -0
  31. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/C1FmrZbK.js +1 -0
  32. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/{DRZO-E-T.js → CSCQddQS.js} +1 -1
  33. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/CZGpyrcA.js +1 -0
  34. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/CfQ4mGwl.js +1 -0
  35. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/CiaNZCBa.js +1 -0
  36. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Cqo0Vpvt.js +417 -0
  37. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Cy4fgWTG.js +1 -0
  38. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/D5w4xp5l.js +1 -0
  39. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/DD63uD-T.js +1 -0
  40. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/DQ8aZ1o-.js +3 -0
  41. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/{Df3aMO5B.js → DSxvnAMh.js} +1 -1
  42. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/D_JuJOO3.js +20 -0
  43. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/D_ynJAfY.js +2 -0
  44. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Dafy4oEQ.js +1 -0
  45. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/{BqBqV92V.js → Dj4O-5se.js} +1 -1
  46. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/DmjAI-UV.js +1 -0
  47. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Dug7Bq1S.js +1 -0
  48. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Dv5BSBQG.js +1 -0
  49. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/DzBTnFhV.js +1 -0
  50. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/DzX_yyqb.js +1 -0
  51. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Frwd2CjB.js +1 -0
  52. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/H4l0JFh9.js +1 -0
  53. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/H60ATh8g.js +2 -0
  54. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/qIv1kPyv.js +1 -0
  55. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/sLqs1uaK.js +20 -0
  56. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/u-it74zV.js +96 -0
  57. lightly_studio/dist_lightly_studio_view_app/_app/immutable/entry/app.BPc0HQPq.js +2 -0
  58. lightly_studio/dist_lightly_studio_view_app/_app/immutable/entry/start.SNvc2nrm.js +1 -0
  59. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/0.5jT7P06o.js +1 -0
  60. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/1.Cdy-7S5q.js +1 -0
  61. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/10.C_uoESTX.js +1 -0
  62. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/12.DcO8wIAc.js +1 -0
  63. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/2.BIldfkxL.js +1012 -0
  64. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/{3.w9g4AcAx.js → 3.BC9z_TWM.js} +1 -1
  65. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/{4.BBI8KwnD.js → 4.D8X_Ch5n.js} +1 -1
  66. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/5.CAXhxJu6.js +39 -0
  67. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/{6.CrbkRPam.js → 6.DRA5Ru_2.js} +1 -1
  68. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/7.WVBsruHQ.js +1 -0
  69. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/8.BuKUrCEN.js +20 -0
  70. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/9.CUIn1yCR.js +1 -0
  71. lightly_studio/dist_lightly_studio_view_app/_app/immutable/workers/clustering.worker-DKqeLtG0.js +2 -0
  72. lightly_studio/dist_lightly_studio_view_app/_app/immutable/workers/search.worker-vNSty3B0.js +1 -0
  73. lightly_studio/dist_lightly_studio_view_app/_app/version.json +1 -1
  74. lightly_studio/dist_lightly_studio_view_app/index.html +15 -14
  75. lightly_studio/examples/example.py +4 -0
  76. lightly_studio/examples/example_coco.py +4 -0
  77. lightly_studio/examples/example_coco_caption.py +24 -0
  78. lightly_studio/examples/example_metadata.py +4 -1
  79. lightly_studio/examples/example_selection.py +4 -0
  80. lightly_studio/examples/example_split_work.py +4 -0
  81. lightly_studio/examples/example_yolo.py +4 -0
  82. lightly_studio/export/export_dataset.py +73 -0
  83. lightly_studio/export/lightly_studio_label_input.py +120 -0
  84. lightly_studio/few_shot_classifier/classifier_manager.py +5 -26
  85. lightly_studio/metadata/compute_typicality.py +67 -0
  86. lightly_studio/models/annotation/annotation_base.py +11 -12
  87. lightly_studio/models/caption.py +73 -0
  88. lightly_studio/models/dataset.py +1 -2
  89. lightly_studio/models/metadata.py +1 -1
  90. lightly_studio/models/sample.py +2 -2
  91. lightly_studio/resolvers/annotation_label_resolver/__init__.py +2 -1
  92. lightly_studio/resolvers/annotation_label_resolver/get_all.py +15 -0
  93. lightly_studio/resolvers/annotation_resolver/__init__.py +2 -3
  94. lightly_studio/resolvers/annotation_resolver/create_many.py +3 -3
  95. lightly_studio/resolvers/annotation_resolver/delete_annotation.py +1 -1
  96. lightly_studio/resolvers/annotation_resolver/delete_annotations.py +7 -3
  97. lightly_studio/resolvers/annotation_resolver/get_by_id.py +19 -1
  98. lightly_studio/resolvers/annotation_resolver/update_annotation_label.py +0 -1
  99. lightly_studio/resolvers/annotations/annotations_filter.py +1 -11
  100. lightly_studio/resolvers/caption_resolver.py +80 -0
  101. lightly_studio/resolvers/dataset_resolver.py +4 -7
  102. lightly_studio/resolvers/metadata_resolver/__init__.py +2 -2
  103. lightly_studio/resolvers/metadata_resolver/sample/__init__.py +3 -3
  104. lightly_studio/resolvers/metadata_resolver/sample/bulk_update_metadata.py +46 -0
  105. lightly_studio/resolvers/samples_filter.py +18 -10
  106. lightly_studio/selection/mundig.py +7 -10
  107. lightly_studio/selection/selection_config.py +4 -1
  108. lightly_studio/services/annotations_service/__init__.py +8 -0
  109. lightly_studio/services/annotations_service/create_annotation.py +63 -0
  110. lightly_studio/services/annotations_service/delete_annotation.py +22 -0
  111. lightly_studio/type_definitions.py +2 -0
  112. {lightly_studio-0.3.2.dist-info → lightly_studio-0.3.4.dist-info}/METADATA +231 -41
  113. {lightly_studio-0.3.2.dist-info → lightly_studio-0.3.4.dist-info}/RECORD +114 -104
  114. lightly_studio/api/routes/api/annotation_task.py +0 -37
  115. lightly_studio/api/routes/api/metrics.py +0 -76
  116. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/0.DenzbfeK.css +0 -1
  117. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/SelectableSvgGroup.BBm0IWdq.css +0 -1
  118. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/SelectableSvgGroup.BNTuXSAe.css +0 -1
  119. lightly_studio/dist_lightly_studio_view_app/_app/immutable/assets/_layout.T-zjSUd3.css +0 -1
  120. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/2O287xak.js +0 -3
  121. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/7YNGEs1C.js +0 -1
  122. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/BBoGk9hq.js +0 -1
  123. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/BRnH9v23.js +0 -92
  124. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Bg1Y5eUZ.js +0 -1
  125. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/C0JiMuYn.js +0 -1
  126. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/C98Hk3r5.js +0 -1
  127. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/CG0dMCJi.js +0 -1
  128. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Cpy-nab_.js +0 -1
  129. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Crk-jcvV.js +0 -1
  130. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Cs31G8Qn.js +0 -1
  131. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/CsKrY2zA.js +0 -1
  132. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/Cur71c3O.js +0 -1
  133. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/CzgC3GFB.js +0 -1
  134. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/D8GZDMNN.js +0 -1
  135. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/DFRh-Spp.js +0 -1
  136. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/DcGCxgpH.js +0 -1
  137. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/DkR_EZ_B.js +0 -1
  138. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/DqUGznj_.js +0 -1
  139. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/H7C68rOM.js +0 -1
  140. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/KpAtIldw.js +0 -1
  141. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/M1Q1F7bw.js +0 -4
  142. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/OH7-C_mc.js +0 -1
  143. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/gLNdjSzu.js +0 -1
  144. lightly_studio/dist_lightly_studio_view_app/_app/immutable/chunks/i0ZZ4z06.js +0 -1
  145. lightly_studio/dist_lightly_studio_view_app/_app/immutable/entry/app.BI-EA5gL.js +0 -2
  146. lightly_studio/dist_lightly_studio_view_app/_app/immutable/entry/start.CcsRl3cZ.js +0 -1
  147. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/0.BbO4Zc3r.js +0 -1
  148. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/1._I9GR805.js +0 -1
  149. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/10.J2RBFrSr.js +0 -1
  150. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/12.Cmqj25a-.js +0 -1
  151. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/2.C45iKJHA.js +0 -6
  152. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/5.huHuxdiF.js +0 -1
  153. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/7.FomEdhD6.js +0 -1
  154. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/8.Cb_ADSLk.js +0 -1
  155. lightly_studio/dist_lightly_studio_view_app/_app/immutable/nodes/9.CajIG5ce.js +0 -1
  156. lightly_studio/metrics/__init__.py +0 -0
  157. lightly_studio/metrics/detection/__init__.py +0 -0
  158. lightly_studio/metrics/detection/map.py +0 -268
  159. lightly_studio/models/annotation_task.py +0 -28
  160. lightly_studio/resolvers/annotation_resolver/create.py +0 -19
  161. lightly_studio/resolvers/annotation_task_resolver.py +0 -31
  162. lightly_studio/resolvers/metadata_resolver/sample/bulk_set_metadata.py +0 -48
  163. {lightly_studio-0.3.2.dist-info → lightly_studio-0.3.4.dist-info}/WHEEL +0 -0
@@ -6,6 +6,6 @@ from lightly_studio.core.start_gui import start_gui
6
6
 
7
7
  # TODO (Jonas 08/25): This will be removed as soon as the new interface is used in the examples
8
8
  from lightly_studio.dataset.loader import DatasetLoader
9
- from lightly_studio.models.annotation_task import AnnotationType
9
+ from lightly_studio.models.annotation.annotation_base import AnnotationType
10
10
 
11
11
  __all__ = ["AnnotationType", "Dataset", "DatasetLoader", "start_gui"]
lightly_studio/api/app.py CHANGED
@@ -16,14 +16,16 @@ from lightly_studio.api.routes import healthz, images, webapp
16
16
  from lightly_studio.api.routes.api import (
17
17
  annotation,
18
18
  annotation_label,
19
- annotation_task,
19
+ caption,
20
20
  classifier,
21
21
  dataset,
22
22
  dataset_tag,
23
+ embeddings2d,
24
+ export,
23
25
  features,
24
26
  metadata,
25
- metrics,
26
27
  sample,
28
+ selection,
27
29
  settings,
28
30
  text_embedding,
29
31
  )
@@ -84,16 +86,18 @@ api_router = APIRouter(prefix="/api", tags=["api"])
84
86
 
85
87
  api_router.include_router(dataset.dataset_router)
86
88
  api_router.include_router(dataset_tag.tag_router)
89
+ api_router.include_router(export.export_router)
87
90
  api_router.include_router(sample.samples_router)
88
91
  api_router.include_router(annotation_label.annotations_label_router)
89
92
  api_router.include_router(annotation.annotations_router)
93
+ api_router.include_router(caption.captions_router)
90
94
  api_router.include_router(text_embedding.text_embedding_router)
91
- api_router.include_router(annotation_task.router)
92
95
  api_router.include_router(settings.settings_router)
93
96
  api_router.include_router(classifier.classifier_router)
97
+ api_router.include_router(embeddings2d.embeddings2d_router)
94
98
  api_router.include_router(features.features_router)
95
99
  api_router.include_router(metadata.metadata_router)
96
- api_router.include_router(metrics.metrics_router)
100
+ api_router.include_router(selection.selection_router)
97
101
 
98
102
 
99
103
  app.include_router(api_router)
@@ -6,9 +6,6 @@ from lightly_studio.models.annotation.annotation_base import (
6
6
  from lightly_studio.models.annotation_label import (
7
7
  AnnotationLabelTable, # noqa: F401, required for SQLModel to work properly
8
8
  )
9
- from lightly_studio.models.annotation_task import (
10
- AnnotationTaskTable, # noqa: F401, required for SQLModel to work properly
11
- )
12
9
  from lightly_studio.models.dataset import (
13
10
  DatasetTable, # noqa: F401, required for SQLModel to work properly
14
11
  )
@@ -9,6 +9,7 @@ from fastapi.params import Query
9
9
  from pydantic import BaseModel
10
10
  from typing_extensions import Annotated
11
11
 
12
+ from lightly_studio.api.routes.api import annotations as annotations_module
12
13
  from lightly_studio.api.routes.api.dataset import get_and_validate_dataset_id
13
14
  from lightly_studio.api.routes.api.status import (
14
15
  HTTP_STATUS_CREATED,
@@ -36,6 +37,7 @@ from lightly_studio.services.annotations_service.update_annotation import (
36
37
  )
37
38
 
38
39
  annotations_router = APIRouter(prefix="/datasets/{dataset_id}", tags=["annotations"])
40
+ annotations_router.include_router(annotations_module.create_annotation_router)
39
41
 
40
42
 
41
43
  @annotations_router.get("/annotations/count")
@@ -221,3 +223,27 @@ def remove_tag_from_annotation(
221
223
  raise HTTPException(status_code=HTTP_STATUS_NOT_FOUND, detail=f"Tag {tag_id} not found")
222
224
 
223
225
  return True
226
+
227
+
228
+ @annotations_router.delete("/annotations/{annotation_id}")
229
+ def delete_annotation(
230
+ session: SessionDep,
231
+ # We need dataset_id because generator doesn't include it
232
+ # actuall path for this route is /datasets/{dataset_id}/annotations/{annotation_id}
233
+ dataset_id: Annotated[ # noqa: ARG001
234
+ UUID,
235
+ Path(title="Dataset Id", description="The ID of the dataset"),
236
+ ],
237
+ annotation_id: Annotated[
238
+ UUID, Path(title="Annotation ID", description="ID of the annotation to delete")
239
+ ],
240
+ ) -> dict[str, str]:
241
+ """Delete an annotation from the database."""
242
+ try:
243
+ annotations_service.delete_annotation(session=session, annotation_id=annotation_id)
244
+ return {"status": "deleted"}
245
+ except ValueError as e:
246
+ raise HTTPException(
247
+ status_code=HTTP_STATUS_NOT_FOUND,
248
+ detail="Annotation not found",
249
+ ) from e
@@ -0,0 +1,7 @@
1
+ from .create_annotation import (
2
+ create_annotation_router,
3
+ )
4
+
5
+ __all__ = [
6
+ "create_annotation_router",
7
+ ]
@@ -0,0 +1,52 @@
1
+ """Create annotation route."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from uuid import UUID
6
+
7
+ from fastapi import APIRouter, Path
8
+ from fastapi.params import Body
9
+ from pydantic import BaseModel
10
+ from typing_extensions import Annotated
11
+
12
+ from lightly_studio.db_manager import SessionDep
13
+ from lightly_studio.models.annotation.annotation_base import (
14
+ AnnotationBaseTable,
15
+ AnnotationType,
16
+ AnnotationView,
17
+ )
18
+ from lightly_studio.services import annotations_service
19
+ from lightly_studio.services.annotations_service.create_annotation import AnnotationCreateParams
20
+
21
+ create_annotation_router = APIRouter()
22
+
23
+
24
+ class AnnotationCreateInput(BaseModel):
25
+ """API interface to create annotation."""
26
+
27
+ annotation_label_id: UUID
28
+ annotation_type: AnnotationType
29
+ sample_id: UUID
30
+ x: int | None = None
31
+ y: int | None = None
32
+ width: int | None = None
33
+ height: int | None = None
34
+ segmentation_mask: list[int] | None = None
35
+
36
+
37
+ @create_annotation_router.post(
38
+ "/annotations",
39
+ response_model=AnnotationView,
40
+ )
41
+ def create_annotation(
42
+ dataset_id: Annotated[UUID, Path(title="Dataset Id", description="The ID of the dataset")],
43
+ session: SessionDep,
44
+ create_annotation_input: Annotated[AnnotationCreateInput, Body()],
45
+ ) -> AnnotationBaseTable:
46
+ """Create a new annotation."""
47
+ return annotations_service.create_annotation(
48
+ session=session,
49
+ annotation=AnnotationCreateParams(
50
+ dataset_id=dataset_id, **create_annotation_input.model_dump()
51
+ ),
52
+ )
@@ -0,0 +1,30 @@
1
+ """API routes for dataset captions."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from uuid import UUID
6
+
7
+ from fastapi import APIRouter, Depends, Path
8
+ from typing_extensions import Annotated
9
+
10
+ from lightly_studio.api.routes.api.validators import Paginated, PaginatedWithCursor
11
+ from lightly_studio.db_manager import SessionDep
12
+ from lightly_studio.models.caption import CaptionsListView
13
+ from lightly_studio.resolvers import caption_resolver
14
+ from lightly_studio.resolvers.caption_resolver import GetAllCaptionsResult
15
+
16
+ captions_router = APIRouter(prefix="/datasets/{dataset_id}", tags=["captions"])
17
+
18
+
19
+ @captions_router.get("/captions", response_model=CaptionsListView)
20
+ def read_captions(
21
+ dataset_id: Annotated[UUID, Path(title="Dataset Id")],
22
+ session: SessionDep,
23
+ pagination: Annotated[PaginatedWithCursor, Depends()],
24
+ ) -> GetAllCaptionsResult:
25
+ """Retrieve captions for a dataset."""
26
+ return caption_resolver.get_all(
27
+ session=session,
28
+ dataset_id=dataset_id,
29
+ pagination=Paginated(offset=pagination.offset, limit=pagination.limit),
30
+ )
@@ -108,6 +108,7 @@ def delete_dataset(
108
108
  return {"status": "deleted"}
109
109
 
110
110
 
111
+ # TODO(Michal, 09/2025): Move to export.py
111
112
  class ExportBody(BaseModel):
112
113
  """body parameters for including or excluding tag_ids or sample_ids."""
113
114
 
@@ -123,6 +124,7 @@ class ExportBody(BaseModel):
123
124
  # of sample_ids, it is a POST request to avoid URL length limitations.
124
125
  # A body with a GET request is supported by fastAPI however it has undefined
125
126
  # behavior: https://fastapi.tiangolo.com/tutorial/body/
127
+ # TODO(Michal, 09/2025): Move to export.py
126
128
  @dataset_router.post(
127
129
  "/datasets/{dataset_id}/export",
128
130
  )
@@ -155,11 +157,7 @@ def export_dataset_to_absolute_paths(
155
157
  return response
156
158
 
157
159
 
158
- """
159
- Endpoint to export samples from a dataset.
160
- """
161
-
162
-
160
+ # TODO(Michal, 09/2025): Move to export.py
163
161
  @dataset_router.post(
164
162
  "/datasets/{dataset_id}/export/stats",
165
163
  )
@@ -0,0 +1,136 @@
1
+ """Routes delivering 2D embeddings for visualization."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import io
6
+ from uuid import UUID
7
+
8
+ import numpy as np
9
+ import pyarrow as pa
10
+ from fastapi import APIRouter, HTTPException, Response
11
+ from numpy.typing import NDArray
12
+ from pyarrow import ipc
13
+ from pydantic import BaseModel, Field
14
+ from sklearn.manifold import TSNE
15
+ from sqlmodel import select
16
+
17
+ from lightly_studio.db_manager import SessionDep
18
+ from lightly_studio.models.dataset import DatasetTable
19
+ from lightly_studio.models.embedding_model import EmbeddingModelTable
20
+ from lightly_studio.resolvers import sample_embedding_resolver, sample_resolver
21
+ from lightly_studio.resolvers.samples_filter import SampleFilter
22
+
23
+ embeddings2d_router = APIRouter()
24
+
25
+
26
+ class GetEmbeddings2DRequest(BaseModel):
27
+ """Request body for retrieving 2D embeddings."""
28
+
29
+ filters: SampleFilter | None = Field(
30
+ None,
31
+ description="Filter parameters identifying matching samples",
32
+ )
33
+
34
+
35
+ @embeddings2d_router.post("/embeddings2d/tsne")
36
+ def get_embeddings2d__tsne(
37
+ session: SessionDep,
38
+ body: GetEmbeddings2DRequest | None = None,
39
+ ) -> Response:
40
+ """Return 2D embeddings serialized as an Arrow stream."""
41
+ # TODO(Malte, 09/2025): Support choosing the dataset via API parameter.
42
+ dataset = session.exec(select(DatasetTable).limit(1)).first()
43
+ if dataset is None:
44
+ raise HTTPException(status_code=404, detail="No dataset configured.")
45
+
46
+ # TODO(Malte, 09/2025): Support choosing the embedding model via API parameter.
47
+ embedding_model = session.exec(
48
+ select(EmbeddingModelTable)
49
+ .where(EmbeddingModelTable.dataset_id == dataset.dataset_id)
50
+ .limit(1)
51
+ ).first()
52
+ if embedding_model is None:
53
+ raise HTTPException(status_code=404, detail="No embedding model configured.")
54
+
55
+ embeddings = sample_embedding_resolver.get_all_by_dataset_id(
56
+ session=session,
57
+ dataset_id=dataset.dataset_id,
58
+ embedding_model_id=embedding_model.embedding_model_id,
59
+ )
60
+
61
+ embedding_values = np.asarray([e.embedding for e in embeddings], dtype=np.float32)
62
+ embedding_values_tsne = _calculate_tsne_embeddings(embedding_values)
63
+ x = embedding_values_tsne[:, 0]
64
+ y = embedding_values_tsne[:, 1]
65
+
66
+ matching_sample_ids: set[UUID] | None = None
67
+ filters = body.filters if body else None
68
+ if filters:
69
+ matching_samples_result = sample_resolver.get_all_by_dataset_id(
70
+ session=session,
71
+ dataset_id=dataset.dataset_id,
72
+ filters=filters,
73
+ )
74
+ matching_sample_ids = {sample.sample_id for sample in matching_samples_result.samples}
75
+
76
+ sample_ids = [embedding.sample_id for embedding in embeddings]
77
+ if matching_sample_ids is None:
78
+ fulfils_filter = [1] * len(sample_ids)
79
+ else:
80
+ fulfils_filter = [1 if sample_id in matching_sample_ids else 0 for sample_id in sample_ids]
81
+
82
+ # TODO(Malte, 09/2025): Save the 2D-embeddings in the database to avoid recomputing
83
+ # them on every request.
84
+
85
+ # TODO(Malte, 09/2025): Include a sample identifier in the returned payload.
86
+ table = pa.table(
87
+ {
88
+ "x": pa.array(x, type=pa.float32()),
89
+ "y": pa.array(y, type=pa.float32()),
90
+ "fulfils_filter": pa.array(fulfils_filter, type=pa.uint8()),
91
+ "sample_id": pa.array([str(sample_id) for sample_id in sample_ids], type=pa.string()),
92
+ }
93
+ )
94
+
95
+ buffer = io.BytesIO()
96
+ with ipc.new_stream(buffer, table.schema) as writer:
97
+ writer.write_table(table)
98
+ buffer.seek(0)
99
+
100
+ return Response(
101
+ content=buffer.getvalue(),
102
+ media_type="application/vnd.apache.arrow.stream",
103
+ headers={
104
+ "Content-Disposition": "inline; filename=embeddings2d.arrow",
105
+ "Content-Type": "application/vnd.apache.arrow.stream",
106
+ "X-Content-Type-Options": "nosniff",
107
+ },
108
+ )
109
+
110
+
111
+ def _calculate_tsne_embeddings(embedding_values: NDArray[np.float32]) -> NDArray[np.float32]:
112
+ # TODO(Malte, 10/2025): Switch to a better and faster projection method than
113
+ # scikit-learn's TSNE.
114
+ # See https://linear.app/lightly/issue/LIG-7678/embedding-plot-investigate-fasterandbetter-2d-computation-options
115
+ n_samples = embedding_values.shape[0]
116
+ # For 0, 1 or 2 samples we hard-code deterministic coordinates.
117
+ if n_samples == 0:
118
+ return np.zeros((0, 2), dtype=np.float32)
119
+ if n_samples == 1:
120
+ return np.asarray([[0.0, 0.0]], dtype=np.float32)
121
+ if n_samples == 2: # noqa: PLR2004
122
+ return np.asarray([[0.0, 0.0], [1.0, 1.0]], dtype=np.float32)
123
+
124
+ # Copied from lightly-core:
125
+ # https://github.com/lightly-ai/lightly-core/blob/b738952516e916eba42fdd28498491ff18df5c1e/appv2/packages/queueworker/src/jobs/embeddings2d/function-source/main.py#L179-L186
126
+ embeddings_2d: NDArray[np.float32] = TSNE(
127
+ init="pca", # changed in https://github.com/scikit-learn/scikit-learn/issues/18018
128
+ learning_rate="auto", # changed in https://github.com/scikit-learn/scikit-learn/issues/18018
129
+ n_components=2,
130
+ # Perplexity must be _less_ than the number of entries. 30 is the default value.
131
+ # https://scikit-learn.org/stable/modules/generated/sklearn.manifold.TSNE.html
132
+ perplexity=min(30.0, float(n_samples - 1)),
133
+ # Make the computation deterministic.
134
+ random_state=0,
135
+ ).fit_transform(embedding_values)
136
+ return embeddings_2d
@@ -0,0 +1,73 @@
1
+ """API routes for exporting dataset annotation tasks."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Generator
6
+ from pathlib import Path as PathlibPath
7
+ from tempfile import TemporaryDirectory
8
+
9
+ from fastapi import APIRouter, Depends, Path
10
+ from fastapi.responses import StreamingResponse
11
+ from typing_extensions import Annotated
12
+
13
+ from lightly_studio.api.routes.api import dataset as dataset_api
14
+ from lightly_studio.core.dataset_query.dataset_query import DatasetQuery
15
+ from lightly_studio.db_manager import SessionDep
16
+ from lightly_studio.export import export_dataset
17
+ from lightly_studio.models.dataset import DatasetTable
18
+
19
+ export_router = APIRouter(prefix="/datasets/{dataset_id}", tags=["export"])
20
+
21
+
22
+ @export_router.get("/export/annotations")
23
+ def export_dataset_annotations(
24
+ dataset: Annotated[
25
+ DatasetTable,
26
+ Path(title="Dataset Id"),
27
+ Depends(dataset_api.get_and_validate_dataset_id),
28
+ ],
29
+ session: SessionDep,
30
+ ) -> StreamingResponse:
31
+ """Export dataset annotations for an object detection task in COCO format."""
32
+ # Query to export - all samples in the dataset.
33
+ dataset_query = DatasetQuery(dataset=dataset, session=session)
34
+
35
+ # Create the export in a temporary directory. We cannot use a context manager
36
+ # because the directory should be deleted only after the file has finished streaming.
37
+ temp_dir = TemporaryDirectory()
38
+ output_path = PathlibPath(temp_dir.name) / "coco_export.json"
39
+
40
+ try:
41
+ export_dataset.to_coco_object_detections(
42
+ session=session,
43
+ samples=dataset_query,
44
+ output_json=output_path,
45
+ )
46
+ except Exception:
47
+ temp_dir.cleanup()
48
+ # Reraise.
49
+ raise
50
+
51
+ return StreamingResponse(
52
+ content=_stream_export_file(
53
+ temp_dir=temp_dir,
54
+ file_path=output_path,
55
+ ),
56
+ media_type="application/json",
57
+ headers={
58
+ "Access-Control-Expose-Headers": "Content-Disposition",
59
+ "Content-Disposition": f"attachment; filename={output_path.name}",
60
+ },
61
+ )
62
+
63
+
64
+ def _stream_export_file(
65
+ temp_dir: TemporaryDirectory[str],
66
+ file_path: PathlibPath,
67
+ ) -> Generator[bytes, None, None]:
68
+ """Stream the export file and clean up the temporary directory afterwards."""
69
+ try:
70
+ with file_path.open("rb") as file:
71
+ yield from file
72
+ finally:
73
+ temp_dir.cleanup()
@@ -5,11 +5,16 @@ from __future__ import annotations
5
5
  from typing import List
6
6
  from uuid import UUID
7
7
 
8
- from fastapi import APIRouter, Path
8
+ from fastapi import APIRouter, Depends, Path
9
+ from pydantic import BaseModel, Field
9
10
  from typing_extensions import Annotated
10
11
 
12
+ from lightly_studio.api.routes.api.dataset import get_and_validate_dataset_id
11
13
  from lightly_studio.db_manager import SessionDep
14
+ from lightly_studio.metadata import compute_typicality
15
+ from lightly_studio.models.dataset import DatasetTable
12
16
  from lightly_studio.models.metadata import MetadataInfoView
17
+ from lightly_studio.resolvers import embedding_model_resolver
13
18
  from lightly_studio.resolvers.metadata_resolver.sample.get_metadata_info import (
14
19
  get_all_metadata_keys_and_schema,
15
20
  )
@@ -33,3 +38,54 @@ def get_metadata_info(
33
38
  for numerical metadata types.
34
39
  """
35
40
  return get_all_metadata_keys_and_schema(session=session, dataset_id=dataset_id)
41
+
42
+
43
+ class ComputeTypicalityRequest(BaseModel):
44
+ """Request model for computing typicality metadata."""
45
+
46
+ embedding_model_name: str | None = Field(
47
+ default=None,
48
+ description="Embedding model name (uses default if not specified)",
49
+ )
50
+ metadata_name: str = Field(
51
+ default="typicality",
52
+ description="Metadata field name (defaults to 'typicality')",
53
+ )
54
+
55
+
56
+ @metadata_router.post(
57
+ "/metadata/typicality",
58
+ status_code=204,
59
+ response_model=None,
60
+ )
61
+ def compute_typicality_metadata(
62
+ session: SessionDep,
63
+ dataset: Annotated[
64
+ DatasetTable,
65
+ Depends(get_and_validate_dataset_id),
66
+ ],
67
+ request: ComputeTypicalityRequest,
68
+ ) -> None:
69
+ """Compute typicality metadata for a dataset.
70
+
71
+ Args:
72
+ session: The database session.
73
+ dataset: The dataset to compute typicality for.
74
+ request: Request parameters including optional embedding model name
75
+ and metadata field name.
76
+
77
+ Returns:
78
+ None (204 No Content on success).
79
+ """
80
+ embedding_model = embedding_model_resolver.get_by_name(
81
+ session=session,
82
+ dataset_id=dataset.dataset_id,
83
+ embedding_model_name=request.embedding_model_name,
84
+ )
85
+
86
+ compute_typicality.compute_typicality_metadata(
87
+ session=session,
88
+ dataset_id=dataset.dataset_id,
89
+ embedding_model_id=embedding_model.embedding_model_id,
90
+ metadata_name=request.metadata_name,
91
+ )
@@ -0,0 +1,87 @@
1
+ """This module contains the API routes for managing selections."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Union
6
+
7
+ from fastapi import APIRouter, Depends, HTTPException
8
+ from pydantic import BaseModel, Field
9
+ from typing_extensions import Annotated
10
+
11
+ from lightly_studio.api.routes.api.dataset import get_and_validate_dataset_id
12
+ from lightly_studio.db_manager import SessionDep
13
+ from lightly_studio.models.dataset import DatasetTable
14
+ from lightly_studio.resolvers import sample_resolver
15
+ from lightly_studio.selection.select_via_db import select_via_database
16
+ from lightly_studio.selection.selection_config import (
17
+ EmbeddingDiversityStrategy,
18
+ MetadataWeightingStrategy,
19
+ SelectionConfig,
20
+ )
21
+
22
+ selection_router = APIRouter()
23
+
24
+ Strategy = Annotated[
25
+ Union[EmbeddingDiversityStrategy, MetadataWeightingStrategy],
26
+ Field(discriminator="strategy_name"),
27
+ ]
28
+
29
+
30
+ class SelectionRequest(BaseModel):
31
+ """Request model for selection."""
32
+
33
+ n_samples_to_select: int = Field(gt=0, description="Number of samples to select")
34
+ selection_result_tag_name: str = Field(min_length=1, description="Name for the result tag")
35
+ strategies: list[Strategy]
36
+
37
+
38
+ @selection_router.post(
39
+ "/datasets/{dataset_id}/selection",
40
+ status_code=204,
41
+ response_model=None,
42
+ )
43
+ def create_combination_selection(
44
+ session: SessionDep,
45
+ dataset: Annotated[
46
+ DatasetTable,
47
+ Depends(get_and_validate_dataset_id),
48
+ ],
49
+ request: SelectionRequest,
50
+ ) -> None:
51
+ """Create a combination selection on the dataset.
52
+
53
+ This endpoint performs combination selection using embeddings and metadata.
54
+ The selected samples are tagged with the specified tag name.
55
+
56
+ Args:
57
+ session: Database session dependency.
58
+ dataset: Dataset to perform selection on.
59
+ request: Selection parameters including sample count and tag name.
60
+
61
+ Returns:
62
+ None (204 No Content on success).
63
+
64
+ Raises:
65
+ HTTPException: 400 if selection fails due to invalid parameters or other errors.
66
+ """
67
+ # Get all samples in dataset as input for selection.
68
+ all_samples_result = sample_resolver.get_all_by_dataset_id(
69
+ session=session, dataset_id=dataset.dataset_id
70
+ )
71
+ input_sample_ids = [sample.sample_id for sample in all_samples_result.samples]
72
+ # Validate we have enough samples to select from.
73
+ if len(input_sample_ids) < request.n_samples_to_select:
74
+ raise HTTPException(
75
+ status_code=400,
76
+ detail=f"Dataset has only {len(input_sample_ids)} samples, "
77
+ f"cannot select {request.n_samples_to_select}",
78
+ )
79
+ # Create SelectionConfig with diversity strategy.
80
+ config = SelectionConfig(
81
+ dataset_id=dataset.dataset_id,
82
+ n_samples_to_select=request.n_samples_to_select,
83
+ selection_result_tag_name=request.selection_result_tag_name,
84
+ strategies=request.strategies,
85
+ )
86
+ # Perform selection via database.
87
+ select_via_database(session=session, config=config, input_sample_ids=input_sample_ids)