diff --git a/api/common/data_handlers/core/misc.py b/api/common/data_handlers/core/misc.py index 3cb0706..dc6204b 100644 --- a/api/common/data_handlers/core/misc.py +++ b/api/common/data_handlers/core/misc.py @@ -182,7 +182,7 @@ def get_parcellation_labelled_map(parcellation_id: str, space_id: str, region_id # raise e @data_decorator(ROLE) -def get_resampled_map(parcellation_id: str, space_id: str): +def get_resampled_map(parcellation_id: str, space_id: str, name: str=""): """Retrieve and save a labelled map, resampled in space (if necessary), and then return the path of the map. Args: @@ -193,7 +193,7 @@ def get_resampled_map(parcellation_id: str, space_id: str): path to statistical map, if a cached file is returned """ import os - full_filename = get_filename("resampled_map", parcellation_id, space_id, ext=".nii.gz") + full_filename = get_filename("resampled_map", parcellation_id, space_id, name, ext=".nii.gz") if os.path.isfile(full_filename): return full_filename, True @@ -201,7 +201,10 @@ def get_resampled_map(parcellation_id: str, space_id: str): import nibabel as nib parcellation: siibra.core.parcellation.Parcellation = siibra.parcellations[parcellation_id] parcellation_map = parcellation.get_map(siibra.spaces[space_id], siibra.MapType.LABELLED) - nii = parcellation_map.get_resampled_template() + if len(parcellation_map.fragments) > 0: + nii = parcellation_map.get_resampled_template(fragment=name) + else: + nii = parcellation_map.get_resampled_template() assert isinstance(nii, nib.Nifti1Image), f"resample failed... returned not of type nii" diff --git a/api/server/api.py b/api/server/api.py index 10fe9ce..bb8d1f8 100644 --- a/api/server/api.py +++ b/api/server/api.py @@ -57,12 +57,6 @@ add_pagination(siibra_api) -# Versioning for api endpoints -siibra_api = VersionedFastAPI(siibra_api) - -templates = Jinja2Templates(directory="templates/") -siibra_api.mount("/static", StaticFiles(directory="static"), name="static") - # Allow Cors origins = ["*"] @@ -363,6 +357,13 @@ def exception_sapi(request: Request, exc: SapiBaseException): general_logger.warning(f"Error handler: exception_sapi: {str(exc)}") raise HTTPException(400, str(exc)) +@siibra_api.exception_handler(IndexError) +async def handle_index_error(request: Request, exc: IndexError): + return JSONResponse(status_code=404, content={ + "detail": "Cannot find item", + "error": str(exc) + }) + @siibra_api.exception_handler(Exception) async def exception_other(request: Request, exc: Exception): """Catch all exception handler""" @@ -375,6 +376,20 @@ async def exception_other(request: Request, exc: Exception): } ) +# Versioning for api endpoints +exception_handlers = siibra_api.exception_handlers +siibra_api = VersionedFastAPI(siibra_api) + +# add error handlers to versioned fastapi +# see https://github.com/DeanWay/fastapi-versioning/issues/30 +for sub_app in siibra_api.routes: + if hasattr(sub_app.app, "add_exception_handler"): + for klass, handler in exception_handlers.items(): + sub_app.app.add_exception_handler(klass, handler) + +# it seems other mounts must be mounted **after** VersionedFastAPI is called +templates = Jinja2Templates(directory="templates/") +siibra_api.mount("/static", StaticFiles(directory="static"), name="static") # TODO lifespan not working properly. Fix and use lifespan in future @siibra_api.on_event("shutdown") diff --git a/api/server/volumes/parcellationmap.py b/api/server/volumes/parcellationmap.py index 57da6ed..eac6219 100644 --- a/api/server/volumes/parcellationmap.py +++ b/api/server/volumes/parcellationmap.py @@ -46,7 +46,7 @@ def get_siibra_map(parcellation_id: str, space_id: str, map_type: MapType, name """) @version(*FASTAPI_VERSION) @router_decorator(ROLE, func=old_get_resampled_map) -def get_resampled_map(parcellation_id: str, space_id: str, *, func): +def get_resampled_map(parcellation_id: str, space_id: str, name: str="", *, func): """Get resampled map according to specification""" if func is None: raise HTTPException(500, f"func: None passsed") @@ -55,7 +55,7 @@ def get_resampled_map(parcellation_id: str, space_id: str, *, func): "content-disposition": f'attachment; filename="labelled_map.nii.gz"' } - full_filename, cache_flag = func(parcellation_id=parcellation_id, space_id=space_id) + full_filename, cache_flag = func(parcellation_id=parcellation_id, space_id=space_id, name=name) if cache_flag: headers[cache_header] = "hit" assert os.path.isfile(full_filename), f"file saved incorrectly"