temdb 3.0.0__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.
- temdb/alembic.ini +29 -0
- temdb/migrations/env.py +66 -0
- temdb/migrations/script.py.mako +28 -0
- temdb/migrations/versions/.gitkeep +0 -0
- temdb/migrations/versions/20260612_0001_initial_schema.py +542 -0
- temdb/migrations/versions/20260717_ec4c96d0bf77_add_description_to_block_table.py +32 -0
- temdb/migrations/versions/20260721_44f75ed44d66_updated_at_nullable_no_server_default.py +165 -0
- temdb/migrations/versions/20260721_bec991e0d0eb_dataset_status_server_default.py +38 -0
- temdb/migrations/versions/20260801_a1d2f3e4b5c6_add_sections_run_parameters.py +67 -0
- temdb/server/__init__.py +3 -0
- temdb/server/api/__init__.py +0 -0
- temdb/server/api/v2/__init__.py +0 -0
- temdb/server/api/v2/acquisition.py +1094 -0
- temdb/server/api/v2/block.py +228 -0
- temdb/server/api/v2/cutting_session.py +229 -0
- temdb/server/api/v2/dataset.py +139 -0
- temdb/server/api/v2/lens_correction.py +121 -0
- temdb/server/api/v2/microscope.py +93 -0
- temdb/server/api/v2/quality_control.py +185 -0
- temdb/server/api/v2/roi.py +372 -0
- temdb/server/api/v2/section.py +457 -0
- temdb/server/api/v2/specimen.py +246 -0
- temdb/server/api/v2/substrate.py +180 -0
- temdb/server/api/v2/tasks.py +508 -0
- temdb/server/cli.py +48 -0
- temdb/server/config.py +46 -0
- temdb/server/database.py +75 -0
- temdb/server/dependencies.py +24 -0
- temdb/server/exception_handlers.py +109 -0
- temdb/server/ids.py +12 -0
- temdb/server/main.py +172 -0
- temdb/server/sqlmodels/__init__.py +29 -0
- temdb/server/sqlmodels/acquisition.py +101 -0
- temdb/server/sqlmodels/base.py +41 -0
- temdb/server/sqlmodels/block.py +18 -0
- temdb/server/sqlmodels/cutting_session.py +27 -0
- temdb/server/sqlmodels/dataset.py +34 -0
- temdb/server/sqlmodels/lens_correction.py +36 -0
- temdb/server/sqlmodels/microscope.py +22 -0
- temdb/server/sqlmodels/roi.py +55 -0
- temdb/server/sqlmodels/section.py +55 -0
- temdb/server/sqlmodels/specimen.py +17 -0
- temdb/server/sqlmodels/substrate.py +23 -0
- temdb/server/sqlmodels/task.py +60 -0
- temdb/server/sqlmodels/tile.py +53 -0
- temdb/server/sqlmodels/tile_partition.py +147 -0
- temdb-3.0.0.dist-info/METADATA +16 -0
- temdb-3.0.0.dist-info/RECORD +51 -0
- temdb-3.0.0.dist-info/WHEEL +4 -0
- temdb-3.0.0.dist-info/entry_points.txt +3 -0
- temdb-3.0.0.dist-info/licenses/LICENSE +11 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from collections.abc import Iterable
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
|
|
5
|
+
from fastapi import APIRouter, Body, Depends, HTTPException, Query, status
|
|
6
|
+
from sqlalchemy import and_, select
|
|
7
|
+
from sqlalchemy.exc import IntegrityError
|
|
8
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
9
|
+
|
|
10
|
+
from temdb.models import APIErrorResponse, ROICreate, ROIResponse, ROIUpdate
|
|
11
|
+
from temdb.server.dependencies import get_async_session
|
|
12
|
+
from temdb.server.sqlmodels import (
|
|
13
|
+
AcquisitionSQLModel,
|
|
14
|
+
AcquisitionTaskSQLModel,
|
|
15
|
+
ROISQLModel,
|
|
16
|
+
SectionSQLModel,
|
|
17
|
+
SubstrateSQLModel,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
roi_api = APIRouter(
|
|
21
|
+
tags=["ROIs"],
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(__name__)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
async def _has_children(session: AsyncSession, roi_id: str) -> bool:
|
|
28
|
+
"""Whether any ROI references `roi_id` as its parent."""
|
|
29
|
+
result = await session.scalars(select(ROISQLModel.id).where(ROISQLModel.parent_roi_id == roi_id).limit(1))
|
|
30
|
+
return result.first() is not None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
async def _parents_with_children(session: AsyncSession, roi_ids: Iterable[str]) -> set[str]:
|
|
34
|
+
"""Batch version of `_has_children`: which of `roi_ids` have at least one child."""
|
|
35
|
+
candidate_ids = [roi_id for roi_id in roi_ids if roi_id]
|
|
36
|
+
if not candidate_ids:
|
|
37
|
+
return set()
|
|
38
|
+
result = await session.scalars(
|
|
39
|
+
select(ROISQLModel.parent_roi_id).where(ROISQLModel.parent_roi_id.in_(candidate_ids)).distinct()
|
|
40
|
+
)
|
|
41
|
+
return {parent_id for parent_id in result if parent_id is not None}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _to_response(roi: ROISQLModel, is_parent: bool = False) -> ROIResponse:
|
|
45
|
+
response = ROIResponse.model_validate(roi)
|
|
46
|
+
response.is_parent = is_parent
|
|
47
|
+
return response
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@roi_api.get("/rois", response_model=list[ROIResponse])
|
|
51
|
+
async def list_rois(
|
|
52
|
+
specimen_id: str | None = Query(None, description="Filter by human-readable Specimen ID"),
|
|
53
|
+
block_id: str | None = Query(None, description="Filter by human-readable Block ID"),
|
|
54
|
+
cutting_session_id: str | None = Query(None, description="Filter by human-readable Cutting Session ID"),
|
|
55
|
+
section_id: str | None = Query(None, description="Filter by human-readable Section ID"),
|
|
56
|
+
is_parent_roi: bool | None = Query(None, description="Filter ROIs that are parents (have children)"),
|
|
57
|
+
skip: int = Query(0, ge=0),
|
|
58
|
+
limit: int = Query(10, ge=1, le=100),
|
|
59
|
+
session: AsyncSession = Depends(get_async_session),
|
|
60
|
+
):
|
|
61
|
+
"""Retrieve a list of ROIs with optional filters and pagination."""
|
|
62
|
+
statement = select(ROISQLModel)
|
|
63
|
+
conditions = []
|
|
64
|
+
if specimen_id:
|
|
65
|
+
conditions.append(ROISQLModel.specimen_id == specimen_id)
|
|
66
|
+
if block_id:
|
|
67
|
+
conditions.append(ROISQLModel.block_id == block_id)
|
|
68
|
+
if cutting_session_id:
|
|
69
|
+
sections = await session.scalars(
|
|
70
|
+
select(SectionSQLModel.section_id).where(SectionSQLModel.cutting_session_id == cutting_session_id)
|
|
71
|
+
)
|
|
72
|
+
section_ids = [item for item in sections]
|
|
73
|
+
if not section_ids:
|
|
74
|
+
return []
|
|
75
|
+
conditions.append(ROISQLModel.section_id.in_(section_ids))
|
|
76
|
+
if section_id:
|
|
77
|
+
conditions.append(ROISQLModel.section_id == section_id)
|
|
78
|
+
if conditions:
|
|
79
|
+
statement = statement.where(and_(*conditions))
|
|
80
|
+
rois = (await session.scalars(statement.offset(skip).limit(limit))).all()
|
|
81
|
+
parents_with_children = await _parents_with_children(session, (roi.roi_id for roi in rois))
|
|
82
|
+
return [_to_response(roi, roi.roi_id in parents_with_children) for roi in rois]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@roi_api.post("/rois", response_model=ROIResponse, status_code=status.HTTP_201_CREATED)
|
|
86
|
+
async def create_roi(roi_data: ROICreate, session: AsyncSession = Depends(get_async_session)):
|
|
87
|
+
"""Create a new ROI with hierarchical ID generation."""
|
|
88
|
+
section = await session.scalars(select(SectionSQLModel).where(SectionSQLModel.section_id == roi_data.section_id))
|
|
89
|
+
section_obj = section.one_or_none()
|
|
90
|
+
if section_obj is None:
|
|
91
|
+
raise HTTPException(
|
|
92
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
93
|
+
detail=f"Section with ID '{roi_data.section_id}' not found",
|
|
94
|
+
)
|
|
95
|
+
substrate = await session.scalars(
|
|
96
|
+
select(SubstrateSQLModel).where(SubstrateSQLModel.media_id == roi_data.substrate_media_id)
|
|
97
|
+
)
|
|
98
|
+
if substrate.one_or_none() is None:
|
|
99
|
+
raise HTTPException(
|
|
100
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
101
|
+
detail=f"Substrate with media_id '{roi_data.substrate_media_id}' not found",
|
|
102
|
+
)
|
|
103
|
+
parent_roi = None
|
|
104
|
+
hierarchy_level = 1
|
|
105
|
+
if roi_data.parent_roi_id:
|
|
106
|
+
parent_result = await session.scalars(select(ROISQLModel).where(ROISQLModel.roi_id == roi_data.parent_roi_id))
|
|
107
|
+
parent_roi = parent_result.one_or_none()
|
|
108
|
+
if parent_roi is None:
|
|
109
|
+
raise HTTPException(
|
|
110
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
111
|
+
detail=f"Parent ROI with ID '{roi_data.parent_roi_id}' not found",
|
|
112
|
+
)
|
|
113
|
+
hierarchy_level = parent_roi.hierarchy_level + 1
|
|
114
|
+
roi_id = ROISQLModel.generate_roi_id(
|
|
115
|
+
specimen_id=roi_data.specimen_id,
|
|
116
|
+
block_id=roi_data.block_id,
|
|
117
|
+
section_id=roi_data.section_id,
|
|
118
|
+
substrate_media_id=roi_data.substrate_media_id,
|
|
119
|
+
roi_number=roi_data.roi_number,
|
|
120
|
+
parent_roi_id=roi_data.parent_roi_id,
|
|
121
|
+
)
|
|
122
|
+
existing_roi = await session.scalars(select(ROISQLModel).where(ROISQLModel.roi_id == roi_id))
|
|
123
|
+
if existing_roi.one_or_none() is not None:
|
|
124
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"ROI with ID '{roi_id}' already exists")
|
|
125
|
+
|
|
126
|
+
new_roi = ROISQLModel(
|
|
127
|
+
roi_id=roi_id,
|
|
128
|
+
roi_number=roi_data.roi_number,
|
|
129
|
+
section_id=roi_data.section_id,
|
|
130
|
+
block_id=roi_data.block_id,
|
|
131
|
+
specimen_id=roi_data.specimen_id,
|
|
132
|
+
substrate_media_id=roi_data.substrate_media_id,
|
|
133
|
+
hierarchy_level=hierarchy_level,
|
|
134
|
+
parent_roi_id=roi_data.parent_roi_id,
|
|
135
|
+
section_number=roi_data.section_number,
|
|
136
|
+
roi_payload=roi_data.payload.model_dump(mode="json"),
|
|
137
|
+
created_at=roi_data.created_at or datetime.now(timezone.utc),
|
|
138
|
+
updated_at=datetime.now(timezone.utc),
|
|
139
|
+
)
|
|
140
|
+
session.add(new_roi)
|
|
141
|
+
await session.commit()
|
|
142
|
+
await session.refresh(new_roi)
|
|
143
|
+
return _to_response(new_roi, is_parent=False)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@roi_api.post(
|
|
147
|
+
"/rois/batch",
|
|
148
|
+
response_model=list[ROIResponse],
|
|
149
|
+
status_code=status.HTTP_201_CREATED,
|
|
150
|
+
summary="Create multiple ROIs in bulk",
|
|
151
|
+
responses={
|
|
152
|
+
status.HTTP_400_BAD_REQUEST: {"model": APIErrorResponse, "description": "Invalid input data"},
|
|
153
|
+
status.HTTP_404_NOT_FOUND: {"model": APIErrorResponse, "description": "Parent section not found"},
|
|
154
|
+
status.HTTP_409_CONFLICT: {"model": APIErrorResponse, "description": "Duplicate ROI ID"},
|
|
155
|
+
status.HTTP_500_INTERNAL_SERVER_ERROR: {"model": APIErrorResponse, "description": "Internal server error"},
|
|
156
|
+
},
|
|
157
|
+
)
|
|
158
|
+
async def create_rois_batch(
|
|
159
|
+
rois_data: list[ROICreate],
|
|
160
|
+
session: AsyncSession = Depends(get_async_session),
|
|
161
|
+
):
|
|
162
|
+
"""Creates multiple ROI documents from a list with hierarchical ID generation."""
|
|
163
|
+
if not rois_data:
|
|
164
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="ROI data list cannot be empty.")
|
|
165
|
+
|
|
166
|
+
section_cache: dict[str, SectionSQLModel] = {}
|
|
167
|
+
seen_roi_ids: set[str] = set()
|
|
168
|
+
rois_to_insert: list[ROISQLModel] = []
|
|
169
|
+
|
|
170
|
+
for index, roi_create in enumerate(rois_data):
|
|
171
|
+
section_id = roi_create.section_id
|
|
172
|
+
if section_id not in section_cache:
|
|
173
|
+
section = await session.scalars(select(SectionSQLModel).where(SectionSQLModel.section_id == section_id))
|
|
174
|
+
section_obj = section.one_or_none()
|
|
175
|
+
if section_obj is None:
|
|
176
|
+
raise HTTPException(
|
|
177
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
178
|
+
detail=f"Section '{section_id}' not found for ROI item {index}.",
|
|
179
|
+
)
|
|
180
|
+
section_cache[section_id] = section_obj
|
|
181
|
+
section_obj = section_cache[section_id]
|
|
182
|
+
|
|
183
|
+
substrate = await session.scalars(
|
|
184
|
+
select(SubstrateSQLModel).where(SubstrateSQLModel.media_id == roi_create.substrate_media_id)
|
|
185
|
+
)
|
|
186
|
+
if substrate.one_or_none() is None:
|
|
187
|
+
raise HTTPException(
|
|
188
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
189
|
+
detail=f"Substrate with media_id '{roi_create.substrate_media_id}' not found for ROI item {index}.",
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
roi_id = ROISQLModel.generate_roi_id(
|
|
193
|
+
specimen_id=roi_create.specimen_id,
|
|
194
|
+
block_id=roi_create.block_id,
|
|
195
|
+
section_id=roi_create.section_id,
|
|
196
|
+
substrate_media_id=roi_create.substrate_media_id,
|
|
197
|
+
roi_number=roi_create.roi_number,
|
|
198
|
+
parent_roi_id=roi_create.parent_roi_id,
|
|
199
|
+
)
|
|
200
|
+
if roi_id in seen_roi_ids:
|
|
201
|
+
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="duplicate ROI IDs in batch")
|
|
202
|
+
seen_roi_ids.add(roi_id)
|
|
203
|
+
hierarchy_level = ROISQLModel.parse_hierarchy_level(roi_id)
|
|
204
|
+
rois_to_insert.append(
|
|
205
|
+
ROISQLModel(
|
|
206
|
+
roi_id=roi_id,
|
|
207
|
+
roi_number=roi_create.roi_number,
|
|
208
|
+
section_id=section_obj.section_id,
|
|
209
|
+
block_id=section_obj.block_id,
|
|
210
|
+
specimen_id=section_obj.specimen_id,
|
|
211
|
+
substrate_media_id=roi_create.substrate_media_id,
|
|
212
|
+
hierarchy_level=hierarchy_level,
|
|
213
|
+
parent_roi_id=roi_create.parent_roi_id,
|
|
214
|
+
section_number=roi_create.section_number,
|
|
215
|
+
roi_payload=roi_create.payload.model_dump(mode="json"),
|
|
216
|
+
created_at=roi_create.created_at or datetime.now(timezone.utc),
|
|
217
|
+
updated_at=datetime.now(timezone.utc),
|
|
218
|
+
)
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
session.add_all(rois_to_insert)
|
|
222
|
+
try:
|
|
223
|
+
await session.commit()
|
|
224
|
+
except IntegrityError as error:
|
|
225
|
+
await session.rollback()
|
|
226
|
+
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=f"duplicate ROI IDs: {error.orig}") from error
|
|
227
|
+
for roi in rois_to_insert:
|
|
228
|
+
await session.refresh(roi)
|
|
229
|
+
# Newly inserted ROIs may be parents of each other (child created before parent in the
|
|
230
|
+
# same batch is impossible given FK ordering, but a batch may contain a parent whose
|
|
231
|
+
# child is also in the batch), so derive is_parent from the batch's own parent links.
|
|
232
|
+
parent_ids_in_batch = {roi.parent_roi_id for roi in rois_to_insert if roi.parent_roi_id}
|
|
233
|
+
return [_to_response(roi, roi.roi_id in parent_ids_in_batch) for roi in rois_to_insert]
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
@roi_api.get("/rois/{roi_id}", response_model=ROIResponse)
|
|
237
|
+
async def get_roi(roi_id: str, session: AsyncSession = Depends(get_async_session)):
|
|
238
|
+
"""Retrieve a specific ROI by its human-readable integer ID."""
|
|
239
|
+
roi = await session.scalars(select(ROISQLModel).where(ROISQLModel.roi_id == roi_id))
|
|
240
|
+
roi_obj = roi.one_or_none()
|
|
241
|
+
if roi_obj is None:
|
|
242
|
+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"ROI with ID '{roi_id}' not found")
|
|
243
|
+
is_parent = await _has_children(session, roi_obj.roi_id)
|
|
244
|
+
return _to_response(roi_obj, is_parent)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
@roi_api.get("/rois/{roi_id}/hierarchy", response_model=dict)
|
|
248
|
+
async def get_roi_hierarchy(roi_id: str, session: AsyncSession = Depends(get_async_session)):
|
|
249
|
+
"""Get the full hierarchy path for an ROI."""
|
|
250
|
+
roi_result = await session.scalars(select(ROISQLModel).where(ROISQLModel.roi_id == roi_id))
|
|
251
|
+
current = roi_result.one_or_none()
|
|
252
|
+
if current is None:
|
|
253
|
+
raise HTTPException(status_code=404, detail=f"ROI with ID '{roi_id}' not found")
|
|
254
|
+
hierarchy_path = []
|
|
255
|
+
while current:
|
|
256
|
+
hierarchy_path.insert(
|
|
257
|
+
0,
|
|
258
|
+
{"roi_id": current.roi_id, "hierarchy_level": current.hierarchy_level, "section_id": current.section_id},
|
|
259
|
+
)
|
|
260
|
+
if current.parent_roi_id:
|
|
261
|
+
parent_result = await session.scalars(
|
|
262
|
+
select(ROISQLModel).where(ROISQLModel.roi_id == current.parent_roi_id)
|
|
263
|
+
)
|
|
264
|
+
current = parent_result.one_or_none()
|
|
265
|
+
else:
|
|
266
|
+
current = None
|
|
267
|
+
return {"roi_id": roi_id, "hierarchy_path": hierarchy_path, "total_levels": len(hierarchy_path)}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
@roi_api.patch("/rois/{roi_id}", response_model=ROIResponse)
|
|
271
|
+
async def update_roi(
|
|
272
|
+
roi_id: str,
|
|
273
|
+
updated_fields: ROIUpdate = Body(...),
|
|
274
|
+
session: AsyncSession = Depends(get_async_session),
|
|
275
|
+
):
|
|
276
|
+
"""Update details (attributes from ROIPayload) of a specific ROI."""
|
|
277
|
+
roi_result = await session.scalars(select(ROISQLModel).where(ROISQLModel.roi_id == roi_id))
|
|
278
|
+
roi_obj = roi_result.one_or_none()
|
|
279
|
+
if roi_obj is None:
|
|
280
|
+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"ROI with ID '{roi_id}' not found")
|
|
281
|
+
update_data = updated_fields.payload.model_dump(mode="json", exclude_unset=True)
|
|
282
|
+
if not update_data:
|
|
283
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="No update data provided")
|
|
284
|
+
payload = dict(roi_obj.roi_payload or {})
|
|
285
|
+
payload.update(update_data)
|
|
286
|
+
roi_obj.roi_payload = payload
|
|
287
|
+
roi_obj.updated_at = datetime.now(timezone.utc)
|
|
288
|
+
session.add(roi_obj)
|
|
289
|
+
await session.commit()
|
|
290
|
+
await session.refresh(roi_obj)
|
|
291
|
+
is_parent = await _has_children(session, roi_obj.roi_id)
|
|
292
|
+
return _to_response(roi_obj, is_parent)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
@roi_api.delete("/rois/{roi_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
296
|
+
async def delete_roi(roi_id: str, session: AsyncSession = Depends(get_async_session)):
|
|
297
|
+
"""Delete a specific ROI."""
|
|
298
|
+
roi_result = await session.scalars(select(ROISQLModel).where(ROISQLModel.roi_id == roi_id))
|
|
299
|
+
roi_obj = roi_result.one_or_none()
|
|
300
|
+
if roi_obj is None:
|
|
301
|
+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"ROI with ID '{roi_id}' not found")
|
|
302
|
+
child_rois_count = await session.scalars(select(ROISQLModel).where(ROISQLModel.parent_roi_id == roi_id))
|
|
303
|
+
if child_rois_count.first() is not None:
|
|
304
|
+
raise HTTPException(
|
|
305
|
+
status_code=status.HTTP_400_BAD_REQUEST,
|
|
306
|
+
detail=f"Cannot delete ROI '{roi_id}' as it has child ROIs",
|
|
307
|
+
)
|
|
308
|
+
task_count = await session.scalars(select(AcquisitionTaskSQLModel).where(AcquisitionTaskSQLModel.roi_id == roi_id))
|
|
309
|
+
if task_count.first() is not None:
|
|
310
|
+
raise HTTPException(
|
|
311
|
+
status_code=400,
|
|
312
|
+
detail=f"Cannot delete ROI '{roi_id}' as it has associated Acquisition Tasks.",
|
|
313
|
+
)
|
|
314
|
+
acq_count = await session.scalars(select(AcquisitionSQLModel).where(AcquisitionSQLModel.roi_id == roi_id))
|
|
315
|
+
if acq_count.first() is not None:
|
|
316
|
+
raise HTTPException(status_code=400, detail=f"Cannot delete ROI '{roi_id}' as it has associated Acquisitions.")
|
|
317
|
+
await session.delete(roi_obj)
|
|
318
|
+
await session.commit()
|
|
319
|
+
return None
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
@roi_api.get("/sections/{section_id}/rois", response_model=list[ROIResponse])
|
|
323
|
+
async def list_section_rois(
|
|
324
|
+
section_id: str,
|
|
325
|
+
skip: int = Query(0, ge=0),
|
|
326
|
+
limit: int = Query(10, ge=1, le=100),
|
|
327
|
+
session: AsyncSession = Depends(get_async_session),
|
|
328
|
+
):
|
|
329
|
+
"""Retrieve ROIs associated with a specific section using its human-readable ID."""
|
|
330
|
+
rois = await session.scalars(
|
|
331
|
+
select(ROISQLModel)
|
|
332
|
+
.where(ROISQLModel.section_id == section_id)
|
|
333
|
+
.order_by(ROISQLModel.roi_id)
|
|
334
|
+
.offset(skip)
|
|
335
|
+
.limit(limit)
|
|
336
|
+
)
|
|
337
|
+
roi_items = rois.all()
|
|
338
|
+
if not roi_items:
|
|
339
|
+
section = await session.scalars(select(SectionSQLModel).where(SectionSQLModel.section_id == section_id))
|
|
340
|
+
if section.one_or_none() is None:
|
|
341
|
+
raise HTTPException(status_code=404, detail=f"Section '{section_id}' not found")
|
|
342
|
+
parents_with_children = await _parents_with_children(session, (roi.roi_id for roi in roi_items))
|
|
343
|
+
return [_to_response(roi, roi.roi_id in parents_with_children) for roi in roi_items]
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
@roi_api.get("/rois/{roi_id}/children", response_model=dict)
|
|
347
|
+
async def get_child_rois(
|
|
348
|
+
roi_id: str,
|
|
349
|
+
skip: int = Query(0, ge=0),
|
|
350
|
+
limit: int = Query(10, ge=1, le=100),
|
|
351
|
+
session: AsyncSession = Depends(get_async_session),
|
|
352
|
+
):
|
|
353
|
+
"""Retrieve child ROIs for a given parent ROI using the parent's hierarchical ID."""
|
|
354
|
+
parent_roi = await session.scalars(select(ROISQLModel).where(ROISQLModel.roi_id == roi_id))
|
|
355
|
+
parent_obj = parent_roi.one_or_none()
|
|
356
|
+
if parent_obj is None:
|
|
357
|
+
raise HTTPException(status_code=404, detail=f"Parent ROI with ID '{roi_id}' not found")
|
|
358
|
+
children = await session.scalars(
|
|
359
|
+
select(ROISQLModel)
|
|
360
|
+
.where(ROISQLModel.parent_roi_id == roi_id)
|
|
361
|
+
.order_by(ROISQLModel.roi_id)
|
|
362
|
+
.offset(skip)
|
|
363
|
+
.limit(limit)
|
|
364
|
+
)
|
|
365
|
+
children_list = children.all()
|
|
366
|
+
total_children = len((await session.scalars(select(ROISQLModel).where(ROISQLModel.parent_roi_id == roi_id))).all())
|
|
367
|
+
more_results = skip + limit < total_children
|
|
368
|
+
parents_with_children = await _parents_with_children(session, (child.roi_id for child in children_list))
|
|
369
|
+
return {
|
|
370
|
+
"children": [_to_response(child, child.roi_id in parents_with_children) for child in children_list],
|
|
371
|
+
"metadata": {"skip": skip, "limit": limit, "total_children": total_children, "has_more": more_results},
|
|
372
|
+
}
|