class AllenBrainReference: def __init__(self, graph: str = "adult") -> None: self._onto = OntologiesApi() if graph == "adult": self._struct_dicts_list = self._onto.get_structures( structure_graph_ids=1, num_rows='all') elif graph == "development": self._struct_dicts_list = self._onto.get_structures( structure_graph_ids=17, num_rows='all') self._structures = { i["id"]: AllenBrainStructure(i, self) for i in self._struct_dicts_list } self._link() def __getitem__(self, key: int) -> Any: return self._structures[key] def __iter__(self) -> Any: for k, v in self._structures.items(): yield v def _link(self) -> None: for name, structure in self._structures.items(): structure.connect()
def oapi(): oa = OntologiesApi() oa.get_structures = mock.MagicMock(return_value=[{'id': 1, 'structure_id_path': '1'}]) oa.get_structure_set_map = mock.MagicMock(return_value={1: [2, 3]}) return oa
def oapi(): oa = OntologiesApi() oa.get_structures = mock.MagicMock(return_value=[{'id': 1, 'structure_id_path': '1'}]) oa.get_structure_set_map = mock.MagicMock(return_value={1: [2, 3]}) return oa
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)
qform = np.array([[0, 0, allen_resolution*pow(10, -3), 0], [-allen_resolution*pow(10, -3), 0, 0, 0], [0, -allen_resolution*pow(10, -3), 0, 0], [0, 0, 0, 1]]) img_annotation = nib.Nifti1Image(annot, np.eye(4)) img_average_template = nib.Nifti1Image(template, np.eye(4)) img_annotation.set_qform(qform, code=1) img_average_template.set_qform(qform, code=1) img_annotation.set_sform(np.eye(4), code=0) img_average_template.set_sform(np.eye(4), code=0) # img_average_template.set_qform(img_average_template_wrongread.get_qform()) nib.save(img_annotation, allen_annotation_path) nib.save(img_average_template, allen_average_template_path) # Get structure graph oapi = OntologiesApi() allen_structure_graph_dict = oapi.get_structures([1]) # Get structure graph with structure graph id = 1, which is the Mouse Brain Atlas structure graph # This removes some unused fields returned by the query allen_structure_graph_dict = StructureTree.clean_structures(allen_structure_graph_dict) # Get tree allen_structure_graph_tree = StructureTree(allen_structure_graph_dict) # now let's take a look at a structure allen_structure_graph_tree.get_structures_by_name(['Dorsal auditory area']) # Look at children or parent of structure, important for later (volume calculations) # Define path of structure graph table allen_average_template_csv_path=os.path.join(allen_dir, 'structure_graph.csv') allen_average_template_csv_remapped_path = os.path.join(allen_dir, 'structure_graph_remapped.csv')