def get_regional_map_info(atlas_id: str, parcellation_id: str, region_id: str, space_id: Optional[str] = None): """ Returns information about a regional map for given region name. """ validate_and_return_atlas(atlas_id) parcellation = validate_and_return_parcellation(parcellation_id) region = validate_and_return_region(region_id, parcellation) region.get_regional_map("mni152", siibra.MapType.CONTINUOUS) roi, space_of_interest = parse_region_selection(atlas_id, parcellation_id, region_id, space_id) query_id = f'{atlas_id}{parcellation_id}{roi.name}{space_id}' cached_fullpath = get_path_to_regional_map(query_id, roi, space_of_interest) import nibabel as nib import numpy as np nii = nib.load(cached_fullpath) data = nii.get_fdata() return { 'min': np.min(data), 'max': np.max(data), }
def get_parcellation_by_id(atlas_id: str, parcellation_id: str, request: Request): """ Returns one parcellation for given id. """ atlas = validate_and_return_atlas(atlas_id) parcellation = validate_and_return_parcellation(parcellation_id, atlas=atlas) result = __parcellation_result_info(parcellation, atlas_id, request) return jsonable_encoder(result, custom_encoder=siibra_custom_json_encoder)
def get_global_features_rest(atlas_id: str, parcellation_id: str, request: Request): """ Returns all global features for a parcellation. """ validate_and_return_atlas(atlas_id) validate_and_return_parcellation(parcellation_id) result = { 'features': [ { m.modality(): '{}atlases/{}/parcellations/{}/features/{}'.format( get_base_url_from_request(request), atlas_id.replace('/', '%2F'), parcellation_id.replace('/', '%2F'), m.modality()) } for m in siibra.features. modalities # TODO siibra.get_features(parcellation, 'all') - too slow at the moment ] } return jsonable_encoder(result)
def get_all_regions_for_parcellation_id(atlas_id: str, parcellation_id: str, space_id: Optional[str] = None): """ Returns all regions for a given parcellation id. """ parcellation = validate_and_return_parcellation(parcellation_id) if not space_id is None: space = validate_and_return_space(space_id) else: space = None return [ region_encoder(region, space=space) for region in parcellation.regiontree.children ]
def parse_region_selection(atlas_id: str, parcellation_id: str, region_id: str, space_id: str): if space_id is None: raise HTTPException( status_code=400, detail='space_id is required for this functionality') space_of_interest = validate_and_return_space(space_id) validate_and_return_atlas(atlas_id) parcellation = validate_and_return_parcellation(parcellation_id) region = validate_and_return_region(region_id, parcellation) if region is None: raise HTTPException(status_code=404, detail=f'cannot find region with spec {region_id}') # if len(region) > 1: # raise HTTPException( # status_code=400, # detail=f'found multiple region withs pec {region_id}') return region, space_of_interest
def get_all_features_for_region(request: Request, atlas_id: str, parcellation_id: str, region_id: str): """ Returns all regional features for a region. """ validate_and_return_atlas(atlas_id) parcellation = validate_and_return_parcellation(parcellation_id) region = validate_and_return_region(region_id, parcellation) result = { 'features': [{ m: '{}atlases/{}/parcellations/{}/regions/{}/features/{}'.format( get_base_url_from_request(request), atlas_id.replace('/', '%2F'), parcellation_id.replace('/', '%2F'), region_id.replace('/', '%2F'), m) } for m in siibra.get_features(region, 'all')] } return jsonable_encoder(result)
def get_spatial_features(atlas_id, space_id, modality_id, feature_id=None, detail=False, parc_id=None, region_id=None): if modality_id not in siibra.features.modalities: raise HTTPException(status_code=400, detail=f'{modality_id} is not a valid modality') # TODO: Why only spatial features if not issubclass( # feature_classes[modality_id], # feature_export.SpatialFeature): siibra.features.modalities[modality_id]._FEATURETYPE, siibra.features.feature.SpatialFeature): # modality_id is not a global feature, return empty array return [] atlas = validate_and_return_atlas(atlas_id) space_of_interest = validate_and_return_space(space_id, atlas) # TODO not working at the moment. Space_of_interest is never in atlas.spaces # if space_of_interest not in atlas.spaces: # raise HTTPException(404, detail=f'space {space_id} not in atlas {atlas}') # TODO: No Selection of parcellation and region is needed - TODO: How to provide parcellation/region if parc_id is None: raise HTTPException(status_code=400, detail='Parc id is needed') parc = validate_and_return_parcellation(parc_id, atlas) roi = validate_and_return_region(region_id, parc) logger.debug( f'get_spatial_features: {str(atlas)}, {str(space_of_interest)}, {str(parc)}, {str(roi)}' ) try: # spatial_features=atlas.get_features(modality_id) spatial_features = siibra.get_features(roi, modality_id, space=space_of_interest) except Exception as e: logger.warn(e, str(space_of_interest), space_id) raise HTTPException(404, detail=f'Could not get spatial features.') shaped_features = None if siibra.features.modalities[ modality_id] == siibra.features.ieeg.IEEG_SessionQuery: shaped_features = [{ 'summary': { '@id': hashlib.md5(str(feat).encode("utf-8")).hexdigest(), 'name': f'{feat.dataset.name} sub:{feat.sub_id}', 'description': str(feat.dataset.description), 'origin_datainfos': [{ 'urls': feat.dataset.urls }] }, 'get_detail': lambda ft: get_ieeg_session_detail(ft, region=roi, parcellation=parc, space=space_of_interest, atlas=atlas), 'instance': feat } for feat in spatial_features] if shaped_features is None: raise HTTPException(501, detail=f'{modality_id} not yet implemented') if feature_id is not None: shaped_features = [ f for f in shaped_features if f['summary']['@id'] == feature_id ] return [{ **f['summary'], **(f['get_detail'](f['instance']) if detail else {}), } for f in shaped_features]
def get_regional_feature(atlas_id, parcellation_id, region_id, modality_id, detail=True, feature_id=None, gene=None): if not siibra.modalities.provides(modality_id): #feature_classes: # modality_id not found in feature_classes raise HTTPException(status_code=400, detail=f'{modality_id} is not a valid modality') # TODO - check for subclass # if not issubclass( # feature_classes[modality_id], # feature_export.RegionalFeature): # # modality_id is not a global feature, return empty array # return [] atlas = validate_and_return_atlas(atlas_id) parcellation = validate_and_return_parcellation(parcellation_id) # region = atlas.get_region(region_id, parcellation) region = validate_and_return_region(region_id, parcellation) # TODO: validate region # raise HTTPException(status_code=404, # detail=f'Region with id {region_id} not found!') try: got_features = siibra.get_features(region, modality_id) except: raise HTTPException( status_code=500, detail=f'Could not get features for region with id {region_id}') shaped_features = None if siibra.features.modalities[ modality_id] == siibra.features.ebrains.EbrainsRegionalFeatureQuery: shaped_features = [{ 'summary': { '@id': kg_rf_f.id, 'src_name': kg_rf_f.name, }, 'get_detail': lambda kg_rf_f: { '__detail': kg_rf_f.detail }, 'instance': kg_rf_f, } for kg_rf_f in got_features] if siibra.features.modalities[ modality_id] == siibra.features.genes.AllenBrainAtlasQuery: shaped_features = [{ 'summary': { '@id': hashlib.md5(str(gene_feat).encode("utf-8")).hexdigest(), }, 'get_detail': lambda gene_feat: { '__donor_info': gene_feat.donor_info, '__gene': gene_feat.gene, '__probe_ids': gene_feat.probe_ids, '__mri_coord': gene_feat.mri_coord, '__z_scores': gene_feat.z_scores, '__expression_levels': gene_feat.expression_levels }, 'instance': gene_feat, } for gene_feat in got_features] if siibra.features.modalities[ modality_id] == siibra.features.connectivity.ConnectivityProfileQuery: shaped_features = [ { 'summary': { "@id": conn_pr._matrix.id, "src_name": conn_pr.name, "src_info": conn_pr.description, "kgSchema": conn_pr._matrix.type_id, "kgId": conn_pr._matrix.id, }, 'get_detail': lambda conn_pr: { "__column_names": list(conn_pr.regionnames ), #TODO: where to get the names? "__profile": conn_pr.profile.tolist(), }, 'instance': conn_pr, } for conn_pr in got_features ] if siibra.features.modalities[ modality_id] == siibra.features.receptors.ReceptorQuery: shaped_features = [ { 'summary': { "@id": receptor_pr.name, "name": receptor_pr.name, "info": receptor_pr.info, "origin_datainfos": [{ 'name': receptor_pr.name, 'description': receptor_pr.info, 'urls': [{ 'doi': receptor_pr.url }] }] }, 'get_detail': lambda receptor_pr: { "__receptor_symbols": siibra.features.receptors. RECEPTOR_SYMBOLS, "__files": [ ], #receptor_pr.files, # TODO where to get files "__data": { "__profiles": receptor_pr.profiles, "__autoradiographs": { key: { "content_type": "application/octet-stream", "content_encoding": "gzip; base64", "x-width": np.transpose(value, axes=[1, 0, 2]).shape[0], "x-height": np.transpose(value, axes=[1, 0, 2]).shape[1], "x-channel": np.transpose(value, axes=[1, 0, 2]).shape[2], "content": base64.b64encode( zlib.compress( np.transpose(value, axes=[1, 0, 2]). tobytes(order="F"))), } for key, value in receptor_pr.autoradiographs. items() }, "__fingerprint": receptor_pr.fingerprint, # "__profile_unit": receptor_pr.profile_unit, TODO check where to get units }, }, 'instance': receptor_pr, } for receptor_pr in got_features ] if siibra.features.modalities[ modality_id] == siibra.features.cells.RegionalCellDensityExtractor: shaped_features = [{ 'summary': { "@id": '...', 'regionspec': conn_pr.regionspec, 'species': conn_pr.species, 'cells': conn_pr.cells.to_dict(), 'section': conn_pr.section, 'patch': conn_pr.patch }, 'get_detail': lambda conn_pr: { 'regionspec': conn_pr.regionspec, 'species': conn_pr.species, 'cells': conn_pr.cells.to_dict(), 'section': conn_pr.section, 'patch': conn_pr.patch }, 'instance': conn_pr, } for conn_pr in got_features] if shaped_features is None: raise HTTPException( status_code=501, detail=f'feature {modality_id} has not yet been implmented') if feature_id is not None: shaped_features = [ f for f in shaped_features if f['summary']['@id'] == feature_id ] return [{ **f['summary'], **(f['get_detail'](f['instance']) if detail else {}) } for f in shaped_features]