class ImageDownload(SvgApi, ImageDownloadApi): # useful tutorial: https://allensdk.readthedocs.io/en/latest/_static/examples/nb/image_download.html def __init__(self): SvgApi.__init__( self ) # https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/svg_api.py ImageDownloadApi.__init__( self ) # https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/image_download_api.py self.annsetsapi = AnnotatedSectionDataSetsApi( ) # https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/annotated_section_data_sets_api.py self.oapi = OntologiesApi( ) # https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/ontologies_api.py # Get metadata about atlases self.atlases = pd.DataFrame(self.oapi.get_atlases_table()) self.atlases_names = sorted(list(self.atlases['name'].values)) self.mouse_coronal_atlas_id = int(self.atlases.loc[ self.atlases['name'] == "Mouse, P56, Coronal"].id.values[0]) self.mouse_sagittal_atlas_id = int(self.atlases.loc[ self.atlases['name'] == "Mouse, P56, Sagittal"].id.values[0]) self.mouse_3D_atlas_id = int(self.atlases.loc[ self.atlases['name'] == "Mouse, Adult, 3D Coronal"].id.values[0]) # Get metadata about products if connected_to_internet(): self.products = pd.DataFrame( send_query( "http://api.brain-map.org/api/v2/data/query.json?criteria=model::Product" )) self.mouse_brain_reference_product_id = 12 self.mouse_brain_ish_data_product_id = 1 self.products_names = sorted(list(self.products["name"].values)) self.mouse_products_names = sorted( list(self.products.loc[self.products.species == "Mouse"] ["name"].values)) else: raise ConnectionError( "It seems that you are not connected to the internet, you won't be able to download stuff." ) # UTILS def get_atlas_by_name(self, atlas_name): if not atlas_name in self.atlases_names: raise ValueError("Available atlases: {}".format( self.atlases_names)) return self.atlases.loc[self.atlases['name'] == atlas_name].id.values[0] def get_products_by_species(self, species): return self.products.loc[self.products.species == species] def get_experimentsid_by_productid(self, productid, **kwargs): # for more details: https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/image_download_api.py return pd.DataFrame( self.get_section_data_sets_by_product([productid], **kwargs)) def get_experimentimages_by_expid(self, expid): # expid should be a section dataset id return pd.DataFrame(self.section_image_query(expid)) def get_atlasimages_by_atlasid(self, atlasid): if not isinstance(atlasid, int): raise ValueError( "Atlas id should be an integer not: {}".format(atlasid)) return pd.DataFrame(self.atlas_image_query(atlasid)) def download_images_by_imagesid(self, savedir, imagesids, downsample=0, annotated=True, snames=None, atlas_svg=True): if not os.path.isdir(savedir): os.mkdir(savedir) curdir = os.getcwd() os.chdir(savedir) for i, imgid in tqdm(enumerate(imagesids)): if not atlas_svg and not annotated: savename = str(imgid) + ".jpg" elif not atlas_svg and annotated: savename = str(imgid) + "_annotated.jpg" else: savename = str(imgid) + ".svg" if snames is not None: sname, ext = savename.split(".") savename = sname + "_sect{}_img{}.".format(snames[i], i + 1) + ext if os.path.isfile(savename): continue if not atlas_svg and not annotated: self.download_section_image(imgid, file_path=savename, downsample=downsample) elif not atlas_svg and annotated: self.download_atlas_image(imgid, file_path=savename, annotation=True, downsample=downsample) else: self.download_svg(imgid, file_path=savename) file_names = os.listdir(savedir) print("Downloaded {} images".format(len(file_names))) os.chdir(curdir) def download_images_by_atlasid(self, savedir, atlasid, **kwargs): imgsids = self.get_atlasimages_by_atlasid(atlasid)['id'] imgs_secs_n = self.get_atlasimages_by_atlasid( atlasid)['section_number'] self.download_images_by_imagesid(savedir, imgsids, snames=imgs_secs_n, **kwargs)
class ImageDownload(SvgApi, ImageDownloadApi): """ Handles query to the Allen ImageDownloadApi and saves the data """ # useful tutorial: https://allensdk.readthedocs.io/en/latest/_static/examples/nb/image_download.html def __init__(self): SvgApi.__init__( self ) # https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/svg_api.py ImageDownloadApi.__init__( self ) # https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/image_download_api.py self.annsetsapi = AnnotatedSectionDataSetsApi( ) # https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/annotated_section_data_sets_api.py self.oapi = OntologiesApi( ) # https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/ontologies_api.py # Get metadata about atlases self.atlases = pd.DataFrame(self.oapi.get_atlases_table()) self.atlases_names = sorted(list(self.atlases['name'].values)) self.mouse_coronal_atlas_id = int(self.atlases.loc[ self.atlases['name'] == "Mouse, P56, Coronal"].id.values[0]) self.mouse_sagittal_atlas_id = int(self.atlases.loc[ self.atlases['name'] == "Mouse, P56, Sagittal"].id.values[0]) self.mouse_3D_atlas_id = int(self.atlases.loc[ self.atlases['name'] == "Mouse, Adult, 3D Coronal"].id.values[0]) # Get metadata about products if connected_to_internet(): self.products = pd.DataFrame( send_query( "http://api.brain-map.org/api/v2/data/query.json?criteria=model::Product" )) self.mouse_brain_reference_product_id = 12 self.mouse_brain_ish_data_product_id = 1 self.products_names = sorted(list(self.products["name"].values)) self.mouse_products_names = sorted( list(self.products.loc[self.products.species == "Mouse"] ["name"].values)) else: raise ConnectionError( "It seems that you are not connected to the internet, you won't be able to download stuff." ) # UTILS def get_atlas_by_name(self, atlas_name): """ Get a brain atlas in the Allen's database given it's name :param atlas_name: str with atlas name """ if not atlas_name in self.atlases_names: raise ValueError("Available atlases: {}".format( self.atlases_names)) return self.atlases.loc[self.atlases['name'] == atlas_name].id.values[0] def get_products_by_species(self, species): """ Get all 'products' in the Allen Database for a given species :param species: str """ return self.products.loc[self.products.species == species] def get_experimentsid_by_productid(self, productid, **kwargs): """ Get the experiment's ID that belong to the same project (product). :param productid: int with product ID number :param **kwargs: """ # for more details: https://github.com/AllenInstitute/AllenSDK/blob/master/allensdk/api/queries/image_download_api.py return pd.DataFrame( self.get_section_data_sets_by_product([productid], **kwargs)) def get_experimentimages_by_expid(self, expid): """ Get's images that belong to an experiment :param expid: int with experiment name. """ # expid should be a section dataset id return pd.DataFrame(self.section_image_query(expid)) def get_atlasimages_by_atlasid(self, atlasid): """ Get the metadata of images that belong to an atlas. :param atlasid: int with atlas number """ if not isinstance(atlasid, int): raise ValueError( "Atlas id should be an integer not: {}".format(atlasid)) return pd.DataFrame(self.atlas_image_query(atlasid)) def download_images_by_imagesid(self, savedir, imagesids, downsample=0, annotated=True, snames=None, atlas_svg=True): """ Downloads and saves images given a list of images IDs. :param savedir: str, folder in which to save the image :param imagesids: list of int with images IDs :param downsample: downsample factor, to reduce the image size and resolution (Default value = 0) :param annotated: if True the images are overlayed with annotations (Default value = True) :param snames: if True the images are overlayed with the structures names (Default value = None) :param atlas_svg: if True fetches the images as SVG, otherwise as PNG (Default value = True) """ if not os.path.isdir(savedir): os.mkdir(savedir) curdir = os.getcwd() os.chdir(savedir) for i, imgid in tqdm(enumerate(imagesids)): if not atlas_svg and not annotated: savename = str(imgid) + ".jpg" elif not atlas_svg and annotated: savename = str(imgid) + "_annotated.jpg" else: savename = str(imgid) + ".svg" if snames is not None: sname, ext = savename.split(".") savename = sname + "_sect{}_img{}.".format(snames[i], i + 1) + ext if os.path.isfile(savename): continue if not atlas_svg and not annotated: self.download_section_image(imgid, file_path=savename, downsample=downsample) elif not atlas_svg and annotated: self.download_atlas_image(imgid, file_path=savename, annotation=True, downsample=downsample) else: self.download_svg(imgid, file_path=savename) file_names = os.listdir(savedir) print("Downloaded {} images".format(len(file_names))) os.chdir(curdir) def download_images_by_atlasid(self, savedir, atlasid, **kwargs): """ Downloads all the images that belong to an altlas :param savedir: str, folder in which to save the images :param atlasid: int, ID of the atlas to use :param **kwargs: keyword arguments for self.download_images_by_imagesid """ imgsids = self.get_atlasimages_by_atlasid(atlasid)['id'] imgs_secs_n = self.get_atlasimages_by_atlasid( atlasid)['section_number'] self.download_images_by_imagesid(savedir, imgsids, snames=imgs_secs_n, **kwargs)
class OntologiesApiTests(unittest.TestCase): def __init__(self, *args, **kwargs): super(OntologiesApiTests, self).__init__(*args, **kwargs) def setUp(self): self.oa = OntologiesApi() def tearDown(self): self.oa = None def test_get_structure_graph(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,[graph_id$in1],rma::options[num_rows$eq'all'][order$eqstructures.graph_order][count$eqfalse]" structure_graph_id = 1 self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structures(structure_graph_id) self.oa.json_msg_query.assert_called_once_with(expected) def test_list_structure_graphs(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::StructureGraph,rma::options[num_rows$eq'all'][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structure_graphs() self.oa.json_msg_query.assert_called_once_with(expected) def test_list_structure_sets(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::StructureSet,rma::options[num_rows$eq'all'][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structure_sets() self.oa.json_msg_query.assert_called_once_with(expected) def test_list_atlases(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Atlas,rma::options[num_rows$eq'all'][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_atlases() self.oa.json_msg_query.assert_called_once_with(expected) def test_structure_graph_by_name(self): expected = u"http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,graph[structure_graphs.name$in'Mouse Brain Atlas'],rma::options[num_rows$eq'all'][order$eqstructures.graph_order][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structures(structure_graph_names="'Mouse Brain Atlas'") self.oa.json_msg_query.assert_called_once_with(expected) def test_structure_graphs_by_names(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,graph[structure_graphs.name$in'Mouse Brain Atlas','Human Brain Atlas'],rma::options[num_rows$eq'all'][order$eqstructures.graph_order][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structures(structure_graph_names=["'Mouse Brain Atlas'", "'Human Brain Atlas'"]) self.oa.json_msg_query.assert_called_once_with(expected) def test_structure_set_by_id(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,[structure_set_id$in8],rma::options[num_rows$eq'all'][order$eqstructures.graph_order][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structures(structure_set_ids=8) self.oa.json_msg_query.assert_called_once_with(expected) def test_structure_sets_by_ids(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,[structure_set_id$in7,8],rma::options[num_rows$eq'all'][order$eqstructures.graph_order][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structures(structure_set_ids=[7,8]) self.oa.json_msg_query.assert_called_once_with(expected) def test_structure_set_by_name(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,structure_sets[name$in'Mouse Connectivity - Summary'],rma::options[num_rows$eq'all'][order$eqstructures.graph_order][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structures(structure_set_names="'Mouse Connectivity - Summary'") self.oa.json_msg_query.assert_called_once_with(expected) def test_structure_set_by_names(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,structure_sets[name$in'NHP - Coarse','Mouse Connectivity - Summary'],rma::options[num_rows$eq'all'][order$eqstructures.graph_order][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structures(structure_set_names=["'NHP - Coarse'", "'Mouse Connectivity - Summary'"]) self.oa.json_msg_query.assert_called_once_with(expected) def test_structure_set_no_order(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,[graph_id$in1],rma::options[num_rows$eq'all'][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_structures(1, order=None) self.oa.json_msg_query.assert_called_once_with(expected) def test_atlas_1(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Atlas,rma::criteria,[id$in1],structure_graph(ontology),graphic_group_labels,rma::include,structure_graph(ontology),graphic_group_labels,rma::options[only$eq'atlases.id,atlases.name,atlases.image_type,ontologies.id,ontologies.name,structure_graphs.id,structure_graphs.name,graphic_group_labels.id,graphic_group_labels.name'][num_rows$eq'all'][count$eqfalse]" atlas_id = 1 self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_atlases_table(atlas_id) self.oa.json_msg_query.assert_called_once_with(expected) def test_atlas_verbose(self): expected = "http://api.brain-map.org/api/v2/data/query.json?q=model::Atlas,rma::criteria,structure_graph(ontology),graphic_group_labels,rma::include,structure_graph(ontology),graphic_group_labels,rma::options[num_rows$eq'all'][count$eqfalse]" self.oa.json_msg_query = \ MagicMock(name='json_msg_query') self.oa.get_atlases_table(brief=False) self.oa.json_msg_query.assert_called_once_with(expected)