From fcb1bf99aa42f43e7d9fd7898a1f4ebfd7fa5ff4 Mon Sep 17 00:00:00 2001 From: Harry Carey <38996929+PolarBean@users.noreply.github.com> Date: Wed, 28 Aug 2024 18:50:59 +0200 Subject: [PATCH 01/38] First version of the template script --- .../atlas_scripts/template_script.py | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py new file mode 100644 index 00000000..9d292f98 --- /dev/null +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -0,0 +1,103 @@ +###Metadata +__version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 +ATLAS_NAME = None # The expected format is FirstAuthor_SpeciesCommonName, ie; kleven_rat +CITATION = None # DOI of the most relevant citable document +SPECIES = None # The scientific name of the species, ie; Rattus norvegicus +ATLAS_LINK = None #The URL for the data files +ORIENTATION = None #The orientation of the atlas, for more information on how to determine this click here: ........ +ROOT_ID = None # The id of the highest level of the atlas. This is commonly called root or brain. Include some information on what to do if your atlas is not hierarchical +RESOLUTION = None # the resolution of your volume in microns. details on how to format this parameter for non isotropic datasets or datasets with multiple resolutions. + +def download_resources(): + """ + Download the necessary resources for the atlas. + + If possible, please use the Pooch library to retrieve any resources. + """ + pass + +def retrieve_template_and_reference(): + """ + Retrieve the desired template and reference as two numpy arrays. + + Returns: + tuple: A tuple containing two numpy arrays. The first array is the template volume, and the second array is the reference volume. + """ + template = None + reference = None + return template, reference + +def retrieve_hemisphere_map(): + """ + Retrieve a hemisphere map for the atlas. + + If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, + with 0's marking the left hemisphere, and 1's marking the right. + + If your atlas is symmetrical, ignore this function. + + Returns: + numpy.array or None: A numpy array representing the hemisphere map, or None if the atlas is symmetrical. + """ + return None + +def retrieve_structure_information(): + """ + This function should return a pandas DataFrame with information about your atlas. + + The DataFrame should be in the following format: + + ╭─────────────┬───────────────────────────────────┬─────────┬───────────────────────┬───────────────────╮ + | id | name | acronym | structure_id_path | rgb_triplet | + | | | | | | + ├─────────────┼───────────────────────────────────┼─────────┼───────────────────────┼───────────────────┤ + | 997 | root | root | [] | [255, 255, 255] | + ├─────────────┼───────────────────────────────────┼─────────┼───────────────────────┼───────────────────┤ + | 8 | Basic cell groups and regions | grey | [997] | [191, 218, 227] | + ├─────────────┼───────────────────────────────────┼─────────┼───────────────────────┼───────────────────┤ + | 567 | Cerebrum | CH | [997, 8] | [176, 240, 255] | + ╰─────────────┴───────────────────────────────────┴─────────┴───────────────────────┴───────────────────╯ + + Returns: + pandas.DataFrame: A DataFrame containing the atlas information. + """ + return None + +def retrieve_or_construct_meshes(): + """ + This function should return a dictionary of ids and corresponding paths to mesh files. + Some atlases are packaged with mesh files, in these cases we should use these files. + Then this function should download those meshes. In other cases we need to construct + the meshes ourselves. For this we have helper functions to achieve this. + """ + meshes_dict = {} + return meshes_dict + +### If the code above this line has been filled correctly, nothing needs to be edited below. +bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME +bg_root_dir.mkdir(exist_ok=True) +download_resources() +template_volume, reference_volume = retrieve_template_and_reference() +hemispheres_stack = retrieve_hemisphere_map() +structures = retrieve_structure_information() +meshes_dict = retrieve_or_construct_meshes() + +output_filename = wrapup_atlas_from_data( + atlas_name=ATLAS_NAME, + atlas_minor_version=__version__, + citation=CITATION, + atlas_link=ATLAS_LINK, + species=SPECIES, + resolution=(RESOLUTION,) * 3, + orientation=ORIENTATION, + root_id=ROOT_ID, + reference_stack=template_volume, + annotation_stack=annotated_volume, + structures_list=structures, + meshes_dict=meshes_dict, + working_dir=working_dir, + hemispheres_stack=None, + cleanup_files=False, + compress=True, + scale_meshes=True, + ) From 6dc6052592626c0f0b7f52d0c49b9f631c0ad607 Mon Sep 17 00:00:00 2001 From: Harry Carey <38996929+PolarBean@users.noreply.github.com> Date: Wed, 28 Aug 2024 18:51:28 +0200 Subject: [PATCH 02/38] make example_mouse follow the new template --- .../atlas_scripts/example_mouse.py | 184 ++++++++++-------- 1 file changed, 106 insertions(+), 78 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 447e08a4..2f72a488 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -1,67 +1,103 @@ -__version__ = "2" +__version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 +ATLAS_NAME = "example_mouse" # The expected format is FirstAuthor_SpeciesCommonName, i.e., kleven_rat +CITATION = "Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007" # DOI of the most relevant citable document +SPECIES = "Mus musculus" # The scientific name of the species, i.e., Rattus norvegicus +ATLAS_LINK = "http://www.brain-map.org" # The URL for the data files +ORIENTATION = "asr" # The orientation of the atlas +ROOT_ID = 997 # The id of the highest level of the atlas. This is commonly called root or brain. +RESOLUTION = 100 # The resolution of your volume in microns. from pathlib import Path - from allensdk.api.queries.ontologies_api import OntologiesApi from allensdk.api.queries.reference_space_api import ReferenceSpaceApi from allensdk.core.reference_space_cache import ReferenceSpaceCache from requests import exceptions from tqdm import tqdm - from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data - -def create_atlas(working_dir, resolution): - # Specify information about the atlas: - RES_UM = resolution # 100 - ATLAS_NAME = "example_mouse" - SPECIES = "Mus musculus" - ATLAS_LINK = "http://www.brain-map.org" - CITATION = "Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007" - ORIENTATION = "asr" - - # Temporary folder for nrrd files download: - download_dir_path = working_dir / "downloading_path" +def download_resources(): + """ + Download the necessary resources for the atlas. + """ + continue + +def retrieve_template_and_reference(): + """ + Retrieve the desired template and reference as two numpy arrays. + + Returns: + tuple: A tuple containing two numpy arrays. The first array is the reference volume, and the second array is the annotated volume. + """ + # Create temporary download directory + download_dir_path = Path.cwd() / "downloading_path" download_dir_path.mkdir(exist_ok=True) - - # Download annotated and template volume: - ######################################### + + # Setup the reference space cache spacecache = ReferenceSpaceCache( manifest=download_dir_path / "manifest.json", - # downloaded files are stored relative to here - resolution=RES_UM, + resolution=RESOLUTION, reference_space_key="annotation/ccf_2017", - # use the latest version of the CCF ) - # Download - annotated_volume, _ = spacecache.get_annotation_volume() + # Download annotated and template volumes + reference_volume, _ = spacecache.get_annotation_volume() template_volume, _ = spacecache.get_template_volume() - print("Download completed...") + return reference_volume, annotated_volume - # Download structures tree and meshes: - ###################################### - oapi = OntologiesApi() # ontologies - struct_tree = spacecache.get_structure_tree() # structures tree +def retrieve_hemisphere_map(): + """ + Retrieve a hemisphere map for the atlas. - # Find id of set of regions with mesh: - select_set = ( - "Structures whose surfaces are represented by a precomputed mesh" - ) + If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, + with 0's marking the left hemisphere, and 1's marking the right. - mesh_set_ids = [ - s["id"] - for s in oapi.get_structure_sets() - if s["description"] == select_set - ] + If your atlas is symmetrical, ignore this function. - structs_with_mesh = struct_tree.get_structures_by_set_id(mesh_set_ids)[:3] + Returns: + numpy.array or None: A numpy array representing the hemisphere map, or None if the atlas is symmetrical. + """ + return None + +def retrieve_structure_information(): + """ + Retrieve the structures tree and meshes. + + Returns: + pandas.DataFrame: A DataFrame containing the atlas information. + """ + download_dir_path = Path.cwd() / "downloading_path" + oapi = OntologiesApi() + spacecache = ReferenceSpaceCache( + manifest=download_dir_path / "manifest.json", + resolution=RESOLUTION, + reference_space_key="annotation/ccf_2017", + ) + struct_tree = spacecache.get_structure_tree() # Download structures tree - # Directory for mesh saving: - meshes_dir = working_dir / "mesh_temp_download" + select_set = "Structures whose surfaces are represented by a precomputed mesh" + mesh_set_ids = [s["id"] for s in oapi.get_structure_sets() if s["description"] == select_set] + structs_with_mesh = struct_tree.get_structures_by_set_id(mesh_set_ids)[:3] + # Loop over structures, remove entries not used + for struct in structs_with_mesh: + [struct.pop(k) for k in ["graph_id", "structure_set_ids", "graph_order"]] + return structs_with_mesh + +def retrieve_or_construct_meshes(): + """ + This function should return a dictionary of ids and corresponding paths to mesh files. + Some atlases are packaged with mesh files, in these cases we should use these files. + Then this function should download those meshes. In other cases we need to construct + the meshes ourselves. For this we have helper functions to achieve this. + """ + oapi = OntologiesApi() space = ReferenceSpaceApi() + meshes_dir = Path.cwd() / "mesh_temp_download" + meshes_dir.mkdir(exist_ok=True) + meshes_dict = dict() + structs_with_mesh = retrieve_structure_information() + for s in tqdm(structs_with_mesh): name = s["id"] filename = meshes_dir / f"{name}.obj" @@ -74,41 +110,33 @@ def create_atlas(working_dir, resolution): meshes_dict[name] = filename except (exceptions.HTTPError, ConnectionError): print(s) - - # Loop over structures, remove entries not used: - for struct in structs_with_mesh: - [ - struct.pop(k) - for k in ["graph_id", "structure_set_ids", "graph_order"] - ] - - # Wrap up, compress, and remove file: - print("Finalising atlas") - output_filename = wrapup_atlas_from_data( - atlas_name=ATLAS_NAME, - atlas_minor_version=__version__, - citation=CITATION, - atlas_link=ATLAS_LINK, - species=SPECIES, - resolution=(RES_UM,) * 3, - orientation=ORIENTATION, - root_id=997, - reference_stack=template_volume, - annotation_stack=annotated_volume, - structures_list=structs_with_mesh, - meshes_dict=meshes_dict, - working_dir=working_dir, - hemispheres_stack=None, - cleanup_files=False, - compress=True, - ) - - return output_filename - - -if __name__ == "__main__": - # Generated atlas path: - bg_root_dir = Path.home() / "brainglobe_workingdir" / "example" - bg_root_dir.mkdir(exist_ok=True) - - # create_atlas(working_dir, 100) + return meshes_dict + +### If the code above this line has been filled correctly, nothing needs to be edited below. +bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME +bg_root_dir.mkdir(exist_ok=True) +download_resources() +template_volume, reference_volume = retrieve_template_and_reference() +hemispheres_stack = retrieve_hemisphere_map() +structures = retrieve_structure_information() +meshes_dict = retrieve_or_construct_meshes() + +output_filename = wrapup_atlas_from_data( + atlas_name=ATLAS_NAME, + atlas_minor_version=__version__, + citation=CITATION, + atlas_link=ATLAS_LINK, + species=SPECIES, + resolution=(RESOLUTION,) * 3, + orientation=ORIENTATION, + root_id=ROOT_ID, + reference_stack=template_volume, + annotation_stack=annotated_volume, + structures_list=structures, + meshes_dict=meshes_dict, + working_dir=bg_root_dir, + hemispheres_stack=hemispheres_stack, + cleanup_files=False, + compress=True, + scale_meshes=True, +) From 37a75fb5dece0a31069d19aa1bbf76906bfa9f13 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 28 Aug 2024 16:53:49 +0000 Subject: [PATCH 03/38] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../atlas_scripts/example_mouse.py | 31 ++++++-- .../atlas_scripts/template_script.py | 70 ++++++++++--------- 2 files changed, 62 insertions(+), 39 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 2f72a488..5c0be8e5 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -8,19 +8,23 @@ RESOLUTION = 100 # The resolution of your volume in microns. from pathlib import Path + from allensdk.api.queries.ontologies_api import OntologiesApi from allensdk.api.queries.reference_space_api import ReferenceSpaceApi from allensdk.core.reference_space_cache import ReferenceSpaceCache from requests import exceptions from tqdm import tqdm + from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data + def download_resources(): """ Download the necessary resources for the atlas. - """ + """ continue + def retrieve_template_and_reference(): """ Retrieve the desired template and reference as two numpy arrays. @@ -31,7 +35,7 @@ def retrieve_template_and_reference(): # Create temporary download directory download_dir_path = Path.cwd() / "downloading_path" download_dir_path.mkdir(exist_ok=True) - + # Setup the reference space cache spacecache = ReferenceSpaceCache( manifest=download_dir_path / "manifest.json", @@ -44,11 +48,12 @@ def retrieve_template_and_reference(): template_volume, _ = spacecache.get_template_volume() return reference_volume, annotated_volume + def retrieve_hemisphere_map(): """ Retrieve a hemisphere map for the atlas. - If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, + If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, with 0's marking the left hemisphere, and 1's marking the right. If your atlas is symmetrical, ignore this function. @@ -58,6 +63,7 @@ def retrieve_hemisphere_map(): """ return None + def retrieve_structure_information(): """ Retrieve the structures tree and meshes. @@ -74,19 +80,29 @@ def retrieve_structure_information(): ) struct_tree = spacecache.get_structure_tree() # Download structures tree - select_set = "Structures whose surfaces are represented by a precomputed mesh" - mesh_set_ids = [s["id"] for s in oapi.get_structure_sets() if s["description"] == select_set] + select_set = ( + "Structures whose surfaces are represented by a precomputed mesh" + ) + mesh_set_ids = [ + s["id"] + for s in oapi.get_structure_sets() + if s["description"] == select_set + ] structs_with_mesh = struct_tree.get_structures_by_set_id(mesh_set_ids)[:3] # Loop over structures, remove entries not used for struct in structs_with_mesh: - [struct.pop(k) for k in ["graph_id", "structure_set_ids", "graph_order"]] + [ + struct.pop(k) + for k in ["graph_id", "structure_set_ids", "graph_order"] + ] return structs_with_mesh + def retrieve_or_construct_meshes(): """ This function should return a dictionary of ids and corresponding paths to mesh files. - Some atlases are packaged with mesh files, in these cases we should use these files. + Some atlases are packaged with mesh files, in these cases we should use these files. Then this function should download those meshes. In other cases we need to construct the meshes ourselves. For this we have helper functions to achieve this. """ @@ -112,6 +128,7 @@ def retrieve_or_construct_meshes(): print(s) return meshes_dict + ### If the code above this line has been filled correctly, nothing needs to be edited below. bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME bg_root_dir.mkdir(exist_ok=True) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 9d292f98..3f004905 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -1,21 +1,23 @@ ###Metadata -__version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 -ATLAS_NAME = None # The expected format is FirstAuthor_SpeciesCommonName, ie; kleven_rat -CITATION = None # DOI of the most relevant citable document -SPECIES = None # The scientific name of the species, ie; Rattus norvegicus -ATLAS_LINK = None #The URL for the data files -ORIENTATION = None #The orientation of the atlas, for more information on how to determine this click here: ........ -ROOT_ID = None # The id of the highest level of the atlas. This is commonly called root or brain. Include some information on what to do if your atlas is not hierarchical -RESOLUTION = None # the resolution of your volume in microns. details on how to format this parameter for non isotropic datasets or datasets with multiple resolutions. +__version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 +ATLAS_NAME = None # The expected format is FirstAuthor_SpeciesCommonName, ie; kleven_rat +CITATION = None # DOI of the most relevant citable document +SPECIES = None # The scientific name of the species, ie; Rattus norvegicus +ATLAS_LINK = None # The URL for the data files +ORIENTATION = None # The orientation of the atlas, for more information on how to determine this click here: ........ +ROOT_ID = None # The id of the highest level of the atlas. This is commonly called root or brain. Include some information on what to do if your atlas is not hierarchical +RESOLUTION = None # the resolution of your volume in microns. details on how to format this parameter for non isotropic datasets or datasets with multiple resolutions. + def download_resources(): """ Download the necessary resources for the atlas. - + If possible, please use the Pooch library to retrieve any resources. """ pass + def retrieve_template_and_reference(): """ Retrieve the desired template and reference as two numpy arrays. @@ -27,11 +29,12 @@ def retrieve_template_and_reference(): reference = None return template, reference + def retrieve_hemisphere_map(): """ Retrieve a hemisphere map for the atlas. - If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, + If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, with 0's marking the left hemisphere, and 1's marking the right. If your atlas is symmetrical, ignore this function. @@ -41,6 +44,7 @@ def retrieve_hemisphere_map(): """ return None + def retrieve_structure_information(): """ This function should return a pandas DataFrame with information about your atlas. @@ -62,18 +66,20 @@ def retrieve_structure_information(): pandas.DataFrame: A DataFrame containing the atlas information. """ return None - + + def retrieve_or_construct_meshes(): """ This function should return a dictionary of ids and corresponding paths to mesh files. - Some atlases are packaged with mesh files, in these cases we should use these files. + Some atlases are packaged with mesh files, in these cases we should use these files. Then this function should download those meshes. In other cases we need to construct - the meshes ourselves. For this we have helper functions to achieve this. + the meshes ourselves. For this we have helper functions to achieve this. """ meshes_dict = {} return meshes_dict -### If the code above this line has been filled correctly, nothing needs to be edited below. + +### If the code above this line has been filled correctly, nothing needs to be edited below. bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME bg_root_dir.mkdir(exist_ok=True) download_resources() @@ -83,21 +89,21 @@ def retrieve_or_construct_meshes(): meshes_dict = retrieve_or_construct_meshes() output_filename = wrapup_atlas_from_data( - atlas_name=ATLAS_NAME, - atlas_minor_version=__version__, - citation=CITATION, - atlas_link=ATLAS_LINK, - species=SPECIES, - resolution=(RESOLUTION,) * 3, - orientation=ORIENTATION, - root_id=ROOT_ID, - reference_stack=template_volume, - annotation_stack=annotated_volume, - structures_list=structures, - meshes_dict=meshes_dict, - working_dir=working_dir, - hemispheres_stack=None, - cleanup_files=False, - compress=True, - scale_meshes=True, - ) + atlas_name=ATLAS_NAME, + atlas_minor_version=__version__, + citation=CITATION, + atlas_link=ATLAS_LINK, + species=SPECIES, + resolution=(RESOLUTION,) * 3, + orientation=ORIENTATION, + root_id=ROOT_ID, + reference_stack=template_volume, + annotation_stack=annotated_volume, + structures_list=structures, + meshes_dict=meshes_dict, + working_dir=working_dir, + hemispheres_stack=None, + cleanup_files=False, + compress=True, + scale_meshes=True, +) From 193a78b6a1afadb79837b4ec2a2a541657f7d0ca Mon Sep 17 00:00:00 2001 From: Harry Carey <38996929+PolarBean@users.noreply.github.com> Date: Wed, 28 Aug 2024 18:57:08 +0200 Subject: [PATCH 04/38] Fix error in example mouse --- .../atlas_scripts/example_mouse.py | 39 ++++++------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 5c0be8e5..c1ffb970 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -8,22 +8,18 @@ RESOLUTION = 100 # The resolution of your volume in microns. from pathlib import Path - from allensdk.api.queries.ontologies_api import OntologiesApi from allensdk.api.queries.reference_space_api import ReferenceSpaceApi from allensdk.core.reference_space_cache import ReferenceSpaceCache from requests import exceptions from tqdm import tqdm - from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data - def download_resources(): """ Download the necessary resources for the atlas. - """ - continue - + """ + pass def retrieve_template_and_reference(): """ @@ -35,7 +31,7 @@ def retrieve_template_and_reference(): # Create temporary download directory download_dir_path = Path.cwd() / "downloading_path" download_dir_path.mkdir(exist_ok=True) - + # Setup the reference space cache spacecache = ReferenceSpaceCache( manifest=download_dir_path / "manifest.json", @@ -45,15 +41,14 @@ def retrieve_template_and_reference(): # Download annotated and template volumes reference_volume, _ = spacecache.get_annotation_volume() - template_volume, _ = spacecache.get_template_volume() + annotated_volume, _ = spacecache.get_template_volume() return reference_volume, annotated_volume - def retrieve_hemisphere_map(): """ Retrieve a hemisphere map for the atlas. - If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, + If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, with 0's marking the left hemisphere, and 1's marking the right. If your atlas is symmetrical, ignore this function. @@ -63,7 +58,6 @@ def retrieve_hemisphere_map(): """ return None - def retrieve_structure_information(): """ Retrieve the structures tree and meshes. @@ -80,29 +74,19 @@ def retrieve_structure_information(): ) struct_tree = spacecache.get_structure_tree() # Download structures tree - select_set = ( - "Structures whose surfaces are represented by a precomputed mesh" - ) - mesh_set_ids = [ - s["id"] - for s in oapi.get_structure_sets() - if s["description"] == select_set - ] + select_set = "Structures whose surfaces are represented by a precomputed mesh" + mesh_set_ids = [s["id"] for s in oapi.get_structure_sets() if s["description"] == select_set] structs_with_mesh = struct_tree.get_structures_by_set_id(mesh_set_ids)[:3] # Loop over structures, remove entries not used for struct in structs_with_mesh: - [ - struct.pop(k) - for k in ["graph_id", "structure_set_ids", "graph_order"] - ] + [struct.pop(k) for k in ["graph_id", "structure_set_ids", "graph_order"]] return structs_with_mesh - def retrieve_or_construct_meshes(): """ This function should return a dictionary of ids and corresponding paths to mesh files. - Some atlases are packaged with mesh files, in these cases we should use these files. + Some atlases are packaged with mesh files, in these cases we should use these files. Then this function should download those meshes. In other cases we need to construct the meshes ourselves. For this we have helper functions to achieve this. """ @@ -128,12 +112,11 @@ def retrieve_or_construct_meshes(): print(s) return meshes_dict - ### If the code above this line has been filled correctly, nothing needs to be edited below. bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME bg_root_dir.mkdir(exist_ok=True) download_resources() -template_volume, reference_volume = retrieve_template_and_reference() +reference_volume, annotated_volume = retrieve_template_and_reference() hemispheres_stack = retrieve_hemisphere_map() structures = retrieve_structure_information() meshes_dict = retrieve_or_construct_meshes() @@ -147,7 +130,7 @@ def retrieve_or_construct_meshes(): resolution=(RESOLUTION,) * 3, orientation=ORIENTATION, root_id=ROOT_ID, - reference_stack=template_volume, + reference_stack=reference_volume, annotation_stack=annotated_volume, structures_list=structures, meshes_dict=meshes_dict, From cd9bafe7e195ea91bc5abf1f7ba8abac9a0cc51b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 28 Aug 2024 16:58:43 +0000 Subject: [PATCH 05/38] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../atlas_scripts/example_mouse.py | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index c1ffb970..64013e81 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -8,19 +8,23 @@ RESOLUTION = 100 # The resolution of your volume in microns. from pathlib import Path + from allensdk.api.queries.ontologies_api import OntologiesApi from allensdk.api.queries.reference_space_api import ReferenceSpaceApi from allensdk.core.reference_space_cache import ReferenceSpaceCache from requests import exceptions from tqdm import tqdm + from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data + def download_resources(): """ Download the necessary resources for the atlas. - """ + """ pass + def retrieve_template_and_reference(): """ Retrieve the desired template and reference as two numpy arrays. @@ -31,7 +35,7 @@ def retrieve_template_and_reference(): # Create temporary download directory download_dir_path = Path.cwd() / "downloading_path" download_dir_path.mkdir(exist_ok=True) - + # Setup the reference space cache spacecache = ReferenceSpaceCache( manifest=download_dir_path / "manifest.json", @@ -44,11 +48,12 @@ def retrieve_template_and_reference(): annotated_volume, _ = spacecache.get_template_volume() return reference_volume, annotated_volume + def retrieve_hemisphere_map(): """ Retrieve a hemisphere map for the atlas. - If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, + If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, with 0's marking the left hemisphere, and 1's marking the right. If your atlas is symmetrical, ignore this function. @@ -58,6 +63,7 @@ def retrieve_hemisphere_map(): """ return None + def retrieve_structure_information(): """ Retrieve the structures tree and meshes. @@ -74,19 +80,29 @@ def retrieve_structure_information(): ) struct_tree = spacecache.get_structure_tree() # Download structures tree - select_set = "Structures whose surfaces are represented by a precomputed mesh" - mesh_set_ids = [s["id"] for s in oapi.get_structure_sets() if s["description"] == select_set] + select_set = ( + "Structures whose surfaces are represented by a precomputed mesh" + ) + mesh_set_ids = [ + s["id"] + for s in oapi.get_structure_sets() + if s["description"] == select_set + ] structs_with_mesh = struct_tree.get_structures_by_set_id(mesh_set_ids)[:3] # Loop over structures, remove entries not used for struct in structs_with_mesh: - [struct.pop(k) for k in ["graph_id", "structure_set_ids", "graph_order"]] + [ + struct.pop(k) + for k in ["graph_id", "structure_set_ids", "graph_order"] + ] return structs_with_mesh + def retrieve_or_construct_meshes(): """ This function should return a dictionary of ids and corresponding paths to mesh files. - Some atlases are packaged with mesh files, in these cases we should use these files. + Some atlases are packaged with mesh files, in these cases we should use these files. Then this function should download those meshes. In other cases we need to construct the meshes ourselves. For this we have helper functions to achieve this. """ @@ -112,6 +128,7 @@ def retrieve_or_construct_meshes(): print(s) return meshes_dict + ### If the code above this line has been filled correctly, nothing needs to be edited below. bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME bg_root_dir.mkdir(exist_ok=True) From d96ed9d2cf2e06ce3148720a81c8f5d0f96368a3 Mon Sep 17 00:00:00 2001 From: Harry Carey <38996929+PolarBean@users.noreply.github.com> Date: Wed, 28 Aug 2024 19:10:41 +0200 Subject: [PATCH 06/38] mixed up reference and annotation --- .../atlas_generation/atlas_scripts/example_mouse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 64013e81..7b7edff0 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -44,8 +44,8 @@ def retrieve_template_and_reference(): ) # Download annotated and template volumes - reference_volume, _ = spacecache.get_annotation_volume() - annotated_volume, _ = spacecache.get_template_volume() + reference_volume, _ = spacecache.get_template_volume() + annotation_volume, _ = spacecache.get_annotation_volume() return reference_volume, annotated_volume From 22548868dc54050329ef8605fa03ddd820cdaf42 Mon Sep 17 00:00:00 2001 From: Harry Carey <38996929+PolarBean@users.noreply.github.com> Date: Wed, 28 Aug 2024 19:54:26 +0200 Subject: [PATCH 07/38] added mandatory import to template --- .../atlas_generation/atlas_scripts/example_mouse.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 7b7edff0..6248a9e4 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -1,3 +1,5 @@ +from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data + __version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 ATLAS_NAME = "example_mouse" # The expected format is FirstAuthor_SpeciesCommonName, i.e., kleven_rat CITATION = "Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007" # DOI of the most relevant citable document From 813b25109c38e123265e9b5d887ddced55f40998 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 28 Aug 2024 17:54:33 +0000 Subject: [PATCH 08/38] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../atlas_generation/atlas_scripts/example_mouse.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 6248a9e4..84e9278a 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -17,8 +17,6 @@ from requests import exceptions from tqdm import tqdm -from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data - def download_resources(): """ From c521a2f25c0650ccdd59f7344d3b4381e81a7786 Mon Sep 17 00:00:00 2001 From: Harry Carey <38996929+PolarBean@users.noreply.github.com> Date: Wed, 28 Aug 2024 19:54:58 +0200 Subject: [PATCH 09/38] remove import from example --- .../atlas_generation/atlas_scripts/example_mouse.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 84e9278a..fcdbc04e 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -1,5 +1,3 @@ -from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data - __version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 ATLAS_NAME = "example_mouse" # The expected format is FirstAuthor_SpeciesCommonName, i.e., kleven_rat CITATION = "Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007" # DOI of the most relevant citable document From 602dbefc2079766420ff500aa4a71ee13c570a59 Mon Sep 17 00:00:00 2001 From: Harry Carey <38996929+PolarBean@users.noreply.github.com> Date: Wed, 28 Aug 2024 19:55:20 +0200 Subject: [PATCH 10/38] added mandatory import to template --- .../atlas_generation/atlas_scripts/template_script.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 3f004905..1aafc0f5 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -1,3 +1,6 @@ +from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data + + ###Metadata __version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 ATLAS_NAME = None # The expected format is FirstAuthor_SpeciesCommonName, ie; kleven_rat From f4b04522422ec0dc63418c5a4c8353b4bb4bd7cd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 28 Aug 2024 17:55:27 +0000 Subject: [PATCH 11/38] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../atlas_generation/atlas_scripts/template_script.py | 1 - 1 file changed, 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 1aafc0f5..5503585e 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -1,6 +1,5 @@ from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data - ###Metadata __version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 ATLAS_NAME = None # The expected format is FirstAuthor_SpeciesCommonName, ie; kleven_rat From 9009dc2478ae3c1587fcadf43094786085f0abb6 Mon Sep 17 00:00:00 2001 From: Harry Carey <38996929+PolarBean@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:37:41 +0200 Subject: [PATCH 12/38] include case where variables should be passed between functions --- .../atlas_generation/atlas_scripts/template_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 5503585e..04bdb183 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -81,7 +81,7 @@ def retrieve_or_construct_meshes(): return meshes_dict -### If the code above this line has been filled correctly, nothing needs to be edited below. +### If the code above this line has been filled correctly, nothing needs to be edited below (unless variables need to be passed between the functions). bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME bg_root_dir.mkdir(exist_ok=True) download_resources() From 1383b14661a180aa0b2c149da2502b5c6b52064b Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:03:49 +0200 Subject: [PATCH 13/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/example_mouse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index fcdbc04e..b305fb3e 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -23,9 +23,9 @@ def download_resources(): pass -def retrieve_template_and_reference(): +def retrieve_reference_and_annotation(): """ - Retrieve the desired template and reference as two numpy arrays. + Retrieve the Allen Mouse atlas reference and annotation as two numpy arrays using the allen_sdk. Returns: tuple: A tuple containing two numpy arrays. The first array is the reference volume, and the second array is the annotated volume. From 2b0d42e0cd51940d51bd4d8b58ad0d3c6d815dbc Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:07:03 +0200 Subject: [PATCH 14/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/template_script.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 04bdb183..bccd732e 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -22,10 +22,10 @@ def download_resources(): def retrieve_template_and_reference(): """ - Retrieve the desired template and reference as two numpy arrays. + Retrieve the desired reference and annotation as two numpy arrays. Returns: - tuple: A tuple containing two numpy arrays. The first array is the template volume, and the second array is the reference volume. + tuple: A tuple containing two numpy arrays. The first array is the reference volume, and the second array is the annotation volume. """ template = None reference = None From c47ca80910c10e12a22b5e6b15ffb3af09b644ac Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:10:39 +0200 Subject: [PATCH 15/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/template_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index bccd732e..a8790c33 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -1,7 +1,7 @@ from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data ###Metadata -__version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 +__version__ = 0 # The minor version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 0 (minor version is the first number after the decimal point, ie the minor version of 1.2 is 2) ATLAS_NAME = None # The expected format is FirstAuthor_SpeciesCommonName, ie; kleven_rat CITATION = None # DOI of the most relevant citable document SPECIES = None # The scientific name of the species, ie; Rattus norvegicus From 262a07cb8027e29ffd70f0c0047916e155c0cc73 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:10:46 +0200 Subject: [PATCH 16/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/template_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index a8790c33..c149cacd 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -2,7 +2,7 @@ ###Metadata __version__ = 0 # The minor version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 0 (minor version is the first number after the decimal point, ie the minor version of 1.2 is 2) -ATLAS_NAME = None # The expected format is FirstAuthor_SpeciesCommonName, ie; kleven_rat +ATLAS_NAME = "example_mouse" # The expected format is FirstAuthor_SpeciesCommonName, e.g. kleven_rat, or Institution_SpeciesCommonName, e.g. allen_mouse. CITATION = None # DOI of the most relevant citable document SPECIES = None # The scientific name of the species, ie; Rattus norvegicus ATLAS_LINK = None # The URL for the data files From c51231c0b24ad8273f91ae195ec51164698179b1 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:11:04 +0200 Subject: [PATCH 17/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/example_mouse.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index b305fb3e..904fc881 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -1,7 +1,8 @@ -__version__ = 1 # The version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 1 -ATLAS_NAME = "example_mouse" # The expected format is FirstAuthor_SpeciesCommonName, i.e., kleven_rat +# a working example atlas-packaging script, which makes a simplified version of the Allen Mouse Brain Atlas, at 100um resolution. See `template_script.py` for a starting point to package your own atlas. +__version__ = 0 # This will make the example mouse version 1.0 (zero is the minor version) +ATLAS_NAME = "example_mouse" CITATION = "Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007" # DOI of the most relevant citable document -SPECIES = "Mus musculus" # The scientific name of the species, i.e., Rattus norvegicus +SPECIES = "Mus musculus" # The scientific name of the species, ATLAS_LINK = "http://www.brain-map.org" # The URL for the data files ORIENTATION = "asr" # The orientation of the atlas ROOT_ID = 997 # The id of the highest level of the atlas. This is commonly called root or brain. From fca9c7b81430369bc70667043821b73f019b993d Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:11:25 +0200 Subject: [PATCH 18/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/example_mouse.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 904fc881..9fffa9c9 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -50,12 +50,7 @@ def retrieve_reference_and_annotation(): def retrieve_hemisphere_map(): """ - Retrieve a hemisphere map for the atlas. - - If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, - with 0's marking the left hemisphere, and 1's marking the right. - - If your atlas is symmetrical, ignore this function. +The Allen atlas is symmetrical, so we can just return `None` in this function. Returns: numpy.array or None: A numpy array representing the hemisphere map, or None if the atlas is symmetrical. From 8052298ceed49a07446982061f49e3dc5fb27830 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:12:47 +0000 Subject: [PATCH 19/38] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../atlas_generation/atlas_scripts/example_mouse.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 9fffa9c9..1cf095af 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -1,6 +1,6 @@ # a working example atlas-packaging script, which makes a simplified version of the Allen Mouse Brain Atlas, at 100um resolution. See `template_script.py` for a starting point to package your own atlas. __version__ = 0 # This will make the example mouse version 1.0 (zero is the minor version) -ATLAS_NAME = "example_mouse" +ATLAS_NAME = "example_mouse" CITATION = "Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007" # DOI of the most relevant citable document SPECIES = "Mus musculus" # The scientific name of the species, ATLAS_LINK = "http://www.brain-map.org" # The URL for the data files @@ -50,10 +50,10 @@ def retrieve_reference_and_annotation(): def retrieve_hemisphere_map(): """ -The Allen atlas is symmetrical, so we can just return `None` in this function. + The Allen atlas is symmetrical, so we can just return `None` in this function. - Returns: - numpy.array or None: A numpy array representing the hemisphere map, or None if the atlas is symmetrical. + Returns: + numpy.array or None: A numpy array representing the hemisphere map, or None if the atlas is symmetrical. """ return None From 91c09f75792ca410e429966f90d7d8564606503f Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:13:14 +0200 Subject: [PATCH 20/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/template_script.py | 1 + 1 file changed, 1 insertion(+) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index c149cacd..0008d7bb 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -1,4 +1,5 @@ from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data +from pathlib import Path ###Metadata __version__ = 0 # The minor version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 0 (minor version is the first number after the decimal point, ie the minor version of 1.2 is 2) From 3ebdbd42da7299e1014ee4b43863989e68b9b965 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:13:21 +0200 Subject: [PATCH 21/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/template_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 0008d7bb..f4574395 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -21,7 +21,7 @@ def download_resources(): pass -def retrieve_template_and_reference(): +def retrieve_reference_and_annotation(): """ Retrieve the desired reference and annotation as two numpy arrays. From 6cc32227363b7a772dde5a39d308c85b51c6362b Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:13:35 +0200 Subject: [PATCH 22/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/template_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index f4574395..894472f4 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -86,7 +86,7 @@ def retrieve_or_construct_meshes(): bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME bg_root_dir.mkdir(exist_ok=True) download_resources() -template_volume, reference_volume = retrieve_template_and_reference() +template_volume, annotated_volume = retrieve_reference_and_annotation() hemispheres_stack = retrieve_hemisphere_map() structures = retrieve_structure_information() meshes_dict = retrieve_or_construct_meshes() From 73387c03c41bc376bc2d8ecdabca1097b508ced2 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:13:46 +0200 Subject: [PATCH 23/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/template_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 894472f4..7b386067 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -1,6 +1,6 @@ from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data from pathlib import Path - +# Copy-paste this script into a new file and fill in the functions to package your own atlas. ###Metadata __version__ = 0 # The minor version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 0 (minor version is the first number after the decimal point, ie the minor version of 1.2 is 2) ATLAS_NAME = "example_mouse" # The expected format is FirstAuthor_SpeciesCommonName, e.g. kleven_rat, or Institution_SpeciesCommonName, e.g. allen_mouse. From 23b3a16e10cdc701c420815b6ed4851eb2d92700 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:13:58 +0000 Subject: [PATCH 24/38] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../atlas_generation/atlas_scripts/template_script.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 7b386067..c9d1d466 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -1,5 +1,7 @@ -from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data from pathlib import Path + +from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data + # Copy-paste this script into a new file and fill in the functions to package your own atlas. ###Metadata __version__ = 0 # The minor version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 0 (minor version is the first number after the decimal point, ie the minor version of 1.2 is 2) From 4740c0696663c76897f96e8bb488a6c22f54c362 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:14:19 +0200 Subject: [PATCH 25/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/example_mouse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 1cf095af..8d7ce2fe 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -123,7 +123,7 @@ def retrieve_or_construct_meshes(): return meshes_dict -### If the code above this line has been filled correctly, nothing needs to be edited below. +### Set up for the example mouse done: use default code to wrap up the atlas from here bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME bg_root_dir.mkdir(exist_ok=True) download_resources() From f6253e0ee3e1a8c7eb23061ebe21ddaea9ce69ca Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:14:30 +0200 Subject: [PATCH 26/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/example_mouse.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 8d7ce2fe..46701e3a 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -96,9 +96,7 @@ def retrieve_structure_information(): def retrieve_or_construct_meshes(): """ This function should return a dictionary of ids and corresponding paths to mesh files. - Some atlases are packaged with mesh files, in these cases we should use these files. - Then this function should download those meshes. In other cases we need to construct - the meshes ourselves. For this we have helper functions to achieve this. + This atlas comes packaged with mesh files, so we don't need to use our helper functions to create them ourselves in this case. """ oapi = OntologiesApi() space = ReferenceSpaceApi() From af0f4506bfd5e9cfc569eae1f1a9a8e53a1adeb1 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:14:43 +0200 Subject: [PATCH 27/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/example_mouse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 46701e3a..794f919c 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -60,7 +60,7 @@ def retrieve_hemisphere_map(): def retrieve_structure_information(): """ - Retrieve the structures tree and meshes. + Retrieve the structures tree and meshes for the Allen mouse brain atlas. Returns: pandas.DataFrame: A DataFrame containing the atlas information. From 91b97c8a6b6bfc9a9c4e70749daea8b762360f16 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 15:15:02 +0200 Subject: [PATCH 28/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/template_script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index c9d1d466..d827c81a 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -9,7 +9,7 @@ CITATION = None # DOI of the most relevant citable document SPECIES = None # The scientific name of the species, ie; Rattus norvegicus ATLAS_LINK = None # The URL for the data files -ORIENTATION = None # The orientation of the atlas, for more information on how to determine this click here: ........ +ORIENTATION = "asr" # The orientation of the **original** atlas data, in BrainGlobe convention: https://brainglobe.info/documentation/setting-up/image-definition.html#orientation ROOT_ID = None # The id of the highest level of the atlas. This is commonly called root or brain. Include some information on what to do if your atlas is not hierarchical RESOLUTION = None # the resolution of your volume in microns. details on how to format this parameter for non isotropic datasets or datasets with multiple resolutions. From d9bc6e3d77ce91a6db7d414a14187d800f4747d9 Mon Sep 17 00:00:00 2001 From: polarbean Date: Wed, 11 Sep 2024 15:40:37 +0200 Subject: [PATCH 29/38] keep lines shorter than limit and only run code in __main__ --- .../atlas_scripts/template_script.py | 145 +++++++++++------- 1 file changed, 89 insertions(+), 56 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index d827c81a..70b12a91 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -2,16 +2,42 @@ from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data -# Copy-paste this script into a new file and fill in the functions to package your own atlas. -###Metadata -__version__ = 0 # The minor version of the atlas in the brainglobe_atlasapi, this is internal, if this is the first time this atlas has been added the value should be 0 (minor version is the first number after the decimal point, ie the minor version of 1.2 is 2) -ATLAS_NAME = "example_mouse" # The expected format is FirstAuthor_SpeciesCommonName, e.g. kleven_rat, or Institution_SpeciesCommonName, e.g. allen_mouse. -CITATION = None # DOI of the most relevant citable document -SPECIES = None # The scientific name of the species, ie; Rattus norvegicus -ATLAS_LINK = None # The URL for the data files -ORIENTATION = "asr" # The orientation of the **original** atlas data, in BrainGlobe convention: https://brainglobe.info/documentation/setting-up/image-definition.html#orientation -ROOT_ID = None # The id of the highest level of the atlas. This is commonly called root or brain. Include some information on what to do if your atlas is not hierarchical -RESOLUTION = None # the resolution of your volume in microns. details on how to format this parameter for non isotropic datasets or datasets with multiple resolutions. +# Copy-paste this script into a new file and fill in the functions to package +# your own atlas. + +### Metadata ### + +# The minor version of the atlas in the brainglobe_atlasapi, this is internal, +# if this is the first time this atlas has been added the value should be 0 +# (minor version is the first number after the decimal point, ie the minor +# version of 1.2 is 2) +__version__ = 0 + +# The expected format is FirstAuthor_SpeciesCommonName, e.g. kleven_rat, or +# Institution_SpeciesCommonName, e.g. allen_mouse. +ATLAS_NAME = "example_mouse" + +# DOI of the most relevant citable document +CITATION = None + +# The scientific name of the species, ie; Rattus norvegicus +SPECIES = None + +# The URL for the data files +ATLAS_LINK = None + +# The orientation of the **original** atlas data, in BrainGlobe convention: +# https://brainglobe.info/documentation/setting-up/image-definition.html#orientation +ORIENTATION = "asr" + +# The id of the highest level of the atlas. This is commonly called root or +# brain. Include some information on what to do if your atlas is not +# hierarchical +ROOT_ID = None + +# The resolution of your volume in microns. Details on how to format this +# parameter for non isotropic datasets or datasets with multiple resolutions. +RESOLUTION = None def download_resources(): @@ -28,7 +54,8 @@ def retrieve_reference_and_annotation(): Retrieve the desired reference and annotation as two numpy arrays. Returns: - tuple: A tuple containing two numpy arrays. The first array is the reference volume, and the second array is the annotation volume. + tuple: A tuple containing two numpy arrays. The first array is the + reference volume, and the second array is the annotation volume. """ template = None reference = None @@ -39,33 +66,36 @@ def retrieve_hemisphere_map(): """ Retrieve a hemisphere map for the atlas. - If your atlas is asymmetrical, you may want to use a hemisphere map. This is an array in the same shape as your template, + If your atlas is asymmetrical, you may want to use a hemisphere map. + This is an array in the same shape as your template, with 0's marking the left hemisphere, and 1's marking the right. If your atlas is symmetrical, ignore this function. Returns: - numpy.array or None: A numpy array representing the hemisphere map, or None if the atlas is symmetrical. + numpy.array or None: A numpy array representing the hemisphere map, + or None if the atlas is symmetrical. """ return None def retrieve_structure_information(): """ - This function should return a pandas DataFrame with information about your atlas. + This function should return a pandas DataFrame with information about your + atlas. The DataFrame should be in the following format: - ╭─────────────┬───────────────────────────────────┬─────────┬───────────────────────┬───────────────────╮ - | id | name | acronym | structure_id_path | rgb_triplet | - | | | | | | - ├─────────────┼───────────────────────────────────┼─────────┼───────────────────────┼───────────────────┤ - | 997 | root | root | [] | [255, 255, 255] | - ├─────────────┼───────────────────────────────────┼─────────┼───────────────────────┼───────────────────┤ - | 8 | Basic cell groups and regions | grey | [997] | [191, 218, 227] | - ├─────────────┼───────────────────────────────────┼─────────┼───────────────────────┼───────────────────┤ - | 567 | Cerebrum | CH | [997, 8] | [176, 240, 255] | - ╰─────────────┴───────────────────────────────────┴─────────┴───────────────────────┴───────────────────╯ + ╭────┬───────────────────┬─────────┬───────────────────┬─────────────────╮ + | id | name | acronym | structure_id_path | rgb_triplet | + | | | | | | + ├────┼───────────────────┼─────────┼───────────────────┼─────────────────┤ + | 997| root | root | [] | [255, 255, 255] | + ├────┼───────────────────┼─────────┼───────────────────┼─────────────────┤ + | 8 | Basic cell groups | grey | [997] | [191, 218, 227] | + ├────┼───────────────────┼─────────┼───────────────────┼─────────────────┤ + | 567| Cerebrum | CH | [997, 8] | [176, 240, 255] | + ╰────┴───────────────────┴─────────┴───────────────────┴─────────────────╯ Returns: pandas.DataFrame: A DataFrame containing the atlas information. @@ -75,40 +105,43 @@ def retrieve_structure_information(): def retrieve_or_construct_meshes(): """ - This function should return a dictionary of ids and corresponding paths to mesh files. - Some atlases are packaged with mesh files, in these cases we should use these files. - Then this function should download those meshes. In other cases we need to construct - the meshes ourselves. For this we have helper functions to achieve this. + This function should return a dictionary of ids and corresponding paths to + mesh files. Some atlases are packaged with mesh files, in these cases we + should use these files. Then this function should download those meshes. + In other cases we need to construct the meshes ourselves. For this we have + helper functions to achieve this. """ meshes_dict = {} return meshes_dict -### If the code above this line has been filled correctly, nothing needs to be edited below (unless variables need to be passed between the functions). -bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME -bg_root_dir.mkdir(exist_ok=True) -download_resources() -template_volume, annotated_volume = retrieve_reference_and_annotation() -hemispheres_stack = retrieve_hemisphere_map() -structures = retrieve_structure_information() -meshes_dict = retrieve_or_construct_meshes() - -output_filename = wrapup_atlas_from_data( - atlas_name=ATLAS_NAME, - atlas_minor_version=__version__, - citation=CITATION, - atlas_link=ATLAS_LINK, - species=SPECIES, - resolution=(RESOLUTION,) * 3, - orientation=ORIENTATION, - root_id=ROOT_ID, - reference_stack=template_volume, - annotation_stack=annotated_volume, - structures_list=structures, - meshes_dict=meshes_dict, - working_dir=working_dir, - hemispheres_stack=None, - cleanup_files=False, - compress=True, - scale_meshes=True, -) +### If the code above this line has been filled correctly, nothing needs to be +### edited below (unless variables need to be passed between the functions). +if __name__ == "__main__": + bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME + bg_root_dir.mkdir(exist_ok=True) + download_resources() + template_volume, annotated_volume = retrieve_reference_and_annotation() + hemispheres_stack = retrieve_hemisphere_map() + structures = retrieve_structure_information() + meshes_dict = retrieve_or_construct_meshes() + + output_filename = wrapup_atlas_from_data( + atlas_name=ATLAS_NAME, + atlas_minor_version=__version__, + citation=CITATION, + atlas_link=ATLAS_LINK, + species=SPECIES, + resolution=(RESOLUTION,) * 3, + orientation=ORIENTATION, + root_id=ROOT_ID, + reference_stack=template_volume, + annotation_stack=annotated_volume, + structures_list=structures, + meshes_dict=meshes_dict, + working_dir=bg_root_dir, + hemispheres_stack=None, + cleanup_files=False, + compress=True, + scale_meshes=True, + ) From 2ee5c7b26001fd765cf4db62d326194ff0fac4b5 Mon Sep 17 00:00:00 2001 From: polarbean Date: Wed, 11 Sep 2024 15:52:53 +0200 Subject: [PATCH 30/38] reformat example mouse --- .../atlas_scripts/example_mouse.py | 90 ++++++++++--------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 794f919c..b7f3a985 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -1,13 +1,3 @@ -# a working example atlas-packaging script, which makes a simplified version of the Allen Mouse Brain Atlas, at 100um resolution. See `template_script.py` for a starting point to package your own atlas. -__version__ = 0 # This will make the example mouse version 1.0 (zero is the minor version) -ATLAS_NAME = "example_mouse" -CITATION = "Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007" # DOI of the most relevant citable document -SPECIES = "Mus musculus" # The scientific name of the species, -ATLAS_LINK = "http://www.brain-map.org" # The URL for the data files -ORIENTATION = "asr" # The orientation of the atlas -ROOT_ID = 997 # The id of the highest level of the atlas. This is commonly called root or brain. -RESOLUTION = 100 # The resolution of your volume in microns. - from pathlib import Path from allensdk.api.queries.ontologies_api import OntologiesApi @@ -16,6 +6,20 @@ from requests import exceptions from tqdm import tqdm +from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data + +# a working example atlas-packaging script, which makes a simplified version +# of the Allen Mouse Brain Atlas, at 100um resolution. See `template_script.py` +# for a starting point to package your own atlas. +__version__ = 0 # This will make the example mouse version 1.0 +ATLAS_NAME = "example_mouse" +CITATION = "Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007" +SPECIES = "Mus musculus" # The scientific name of the species, +ATLAS_LINK = "http://www.brain-map.org" # The URL for the data files +ORIENTATION = "asr" # The orientation of the atlas +ROOT_ID = 997 # The id of the highest level of the atlas. +RESOLUTION = 100 # The resolution of your volume in microns. + def download_resources(): """ @@ -26,10 +30,12 @@ def download_resources(): def retrieve_reference_and_annotation(): """ - Retrieve the Allen Mouse atlas reference and annotation as two numpy arrays using the allen_sdk. + Retrieve the Allen Mouse atlas reference and annotation as two numpy arrays + using the allen_sdk. Returns: - tuple: A tuple containing two numpy arrays. The first array is the reference volume, and the second array is the annotated volume. + tuple: A tuple containing two numpy arrays. The first array is the + reference volume, and the second array is the annotated volume. """ # Create temporary download directory download_dir_path = Path.cwd() / "downloading_path" @@ -45,15 +51,17 @@ def retrieve_reference_and_annotation(): # Download annotated and template volumes reference_volume, _ = spacecache.get_template_volume() annotation_volume, _ = spacecache.get_annotation_volume() - return reference_volume, annotated_volume + return reference_volume, annotation_volume def retrieve_hemisphere_map(): """ - The Allen atlas is symmetrical, so we can just return `None` in this function. + The Allen atlas is symmetrical, so we can just return `None` in this + function. Returns: - numpy.array or None: A numpy array representing the hemisphere map, or None if the atlas is symmetrical. + numpy.array or None: A numpy array representing the hemisphere map, + or None if the atlas is symmetrical. """ return None @@ -67,6 +75,7 @@ def retrieve_structure_information(): """ download_dir_path = Path.cwd() / "downloading_path" oapi = OntologiesApi() + spacecache = ReferenceSpaceCache( manifest=download_dir_path / "manifest.json", resolution=RESOLUTION, @@ -95,10 +104,10 @@ def retrieve_structure_information(): def retrieve_or_construct_meshes(): """ - This function should return a dictionary of ids and corresponding paths to mesh files. - This atlas comes packaged with mesh files, so we don't need to use our helper functions to create them ourselves in this case. + This function should return a dictionary of ids and corresponding paths to + mesh files. This atlas comes packaged with mesh files, so we don't need to + use our helper functions to create them ourselves in this case. """ - oapi = OntologiesApi() space = ReferenceSpaceApi() meshes_dir = Path.cwd() / "mesh_temp_download" meshes_dir.mkdir(exist_ok=True) @@ -121,31 +130,32 @@ def retrieve_or_construct_meshes(): return meshes_dict -### Set up for the example mouse done: use default code to wrap up the atlas from here +# Set up for the example mouse done: use default code to wrap up the atlas bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME bg_root_dir.mkdir(exist_ok=True) download_resources() -reference_volume, annotated_volume = retrieve_template_and_reference() +reference_volume, annotated_volume = retrieve_reference_and_annotation() hemispheres_stack = retrieve_hemisphere_map() structures = retrieve_structure_information() meshes_dict = retrieve_or_construct_meshes() - -output_filename = wrapup_atlas_from_data( - atlas_name=ATLAS_NAME, - atlas_minor_version=__version__, - citation=CITATION, - atlas_link=ATLAS_LINK, - species=SPECIES, - resolution=(RESOLUTION,) * 3, - orientation=ORIENTATION, - root_id=ROOT_ID, - reference_stack=reference_volume, - annotation_stack=annotated_volume, - structures_list=structures, - meshes_dict=meshes_dict, - working_dir=bg_root_dir, - hemispheres_stack=hemispheres_stack, - cleanup_files=False, - compress=True, - scale_meshes=True, -) +if __name__ == "__main__": + + output_filename = wrapup_atlas_from_data( + atlas_name=ATLAS_NAME, + atlas_minor_version=__version__, + citation=CITATION, + atlas_link=ATLAS_LINK, + species=SPECIES, + resolution=(RESOLUTION,) * 3, + orientation=ORIENTATION, + root_id=ROOT_ID, + reference_stack=reference_volume, + annotation_stack=annotated_volume, + structures_list=structures, + meshes_dict=meshes_dict, + working_dir=bg_root_dir, + hemispheres_stack=hemispheres_stack, + cleanup_files=False, + compress=True, + scale_meshes=True, + ) From 1e68bed2dc1558c8d718b081a1a4717c75229edb Mon Sep 17 00:00:00 2001 From: polarbean Date: Wed, 11 Sep 2024 16:30:02 +0200 Subject: [PATCH 31/38] use pooch to validate hash --- .../atlas_scripts/example_mouse.py | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index b7f3a985..b4d70f0b 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -1,5 +1,6 @@ from pathlib import Path +import pooch from allensdk.api.queries.ontologies_api import OntologiesApi from allensdk.api.queries.reference_space_api import ReferenceSpaceApi from allensdk.core.reference_space_cache import ReferenceSpaceCache @@ -39,6 +40,7 @@ def retrieve_reference_and_annotation(): """ # Create temporary download directory download_dir_path = Path.cwd() / "downloading_path" + print(download_dir_path) download_dir_path.mkdir(exist_ok=True) # Setup the reference space cache @@ -48,9 +50,28 @@ def retrieve_reference_and_annotation(): reference_space_key="annotation/ccf_2017", ) - # Download annotated and template volumes reference_volume, _ = spacecache.get_template_volume() annotation_volume, _ = spacecache.get_annotation_volume() + expected_reference_hash = ( + "6c24cae773a5cf256586b0384af0ac93ad68564d211c9bdcff4bee9acf07786a" + ) + expected_annotation_hash = ( + "451e6a82f531d3db4b58056d024d3e2311703c2adc15cefa75e0268e7f0e69a4" + ) + reference_hash = pooch.file_hash( + download_dir_path / "average_template_100.nrrd" + ) + annotation_hash = pooch.file_hash( + download_dir_path / "annotation" / "ccf_2017" / "annotation_100.nrrd" + ) + assert ( + reference_hash == expected_reference_hash + ), "The hash of the reference volume does not match the expected hash." + + assert ( + annotation_hash == expected_annotation_hash + ), "The hash of the annotation volume does not match the expected hash." + # Download annotated and template volumes return reference_volume, annotation_volume From 924e48455a1374ebed59c81480a57788b470e809 Mon Sep 17 00:00:00 2001 From: polarbean Date: Wed, 11 Sep 2024 16:47:55 +0200 Subject: [PATCH 32/38] add bg_root_dir global --- .../atlas_generation/atlas_scripts/example_mouse.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index b4d70f0b..2f5bb4ec 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -21,6 +21,8 @@ ROOT_ID = 997 # The id of the highest level of the atlas. RESOLUTION = 100 # The resolution of your volume in microns. +BG_ROOT_DIR = Path.home() / "brainglobe_workingdir" / ATLAS_NAME + def download_resources(): """ @@ -39,7 +41,7 @@ def retrieve_reference_and_annotation(): reference volume, and the second array is the annotated volume. """ # Create temporary download directory - download_dir_path = Path.cwd() / "downloading_path" + download_dir_path = BG_ROOT_DIR / "downloading_path" print(download_dir_path) download_dir_path.mkdir(exist_ok=True) @@ -94,7 +96,7 @@ def retrieve_structure_information(): Returns: pandas.DataFrame: A DataFrame containing the atlas information. """ - download_dir_path = Path.cwd() / "downloading_path" + download_dir_path = BG_ROOT_DIR / "downloading_path" oapi = OntologiesApi() spacecache = ReferenceSpaceCache( @@ -130,7 +132,7 @@ def retrieve_or_construct_meshes(): use our helper functions to create them ourselves in this case. """ space = ReferenceSpaceApi() - meshes_dir = Path.cwd() / "mesh_temp_download" + meshes_dir = BG_ROOT_DIR / "mesh_temp_download" meshes_dir.mkdir(exist_ok=True) meshes_dict = dict() @@ -152,8 +154,7 @@ def retrieve_or_construct_meshes(): # Set up for the example mouse done: use default code to wrap up the atlas -bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME -bg_root_dir.mkdir(exist_ok=True) +BG_ROOT_DIR.mkdir(exist_ok=True) download_resources() reference_volume, annotated_volume = retrieve_reference_and_annotation() hemispheres_stack = retrieve_hemisphere_map() @@ -174,7 +175,7 @@ def retrieve_or_construct_meshes(): annotation_stack=annotated_volume, structures_list=structures, meshes_dict=meshes_dict, - working_dir=bg_root_dir, + working_dir=BG_ROOT_DIR, hemispheres_stack=hemispheres_stack, cleanup_files=False, compress=True, From d643653860af71a40406c007f604999e2198f4d2 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 11 Sep 2024 19:24:01 +0200 Subject: [PATCH 33/38] fix references to annotation and reference --- .../atlas_generation/atlas_scripts/template_script.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 70b12a91..2ed1542f 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -57,9 +57,9 @@ def retrieve_reference_and_annotation(): tuple: A tuple containing two numpy arrays. The first array is the reference volume, and the second array is the annotation volume. """ - template = None reference = None - return template, reference + annoatation = None + return reference, annoatation def retrieve_hemisphere_map(): @@ -121,7 +121,7 @@ def retrieve_or_construct_meshes(): bg_root_dir = Path.home() / "brainglobe_workingdir" / ATLAS_NAME bg_root_dir.mkdir(exist_ok=True) download_resources() - template_volume, annotated_volume = retrieve_reference_and_annotation() + reference_volume, annotated_volume = retrieve_reference_and_annotation() hemispheres_stack = retrieve_hemisphere_map() structures = retrieve_structure_information() meshes_dict = retrieve_or_construct_meshes() @@ -135,7 +135,7 @@ def retrieve_or_construct_meshes(): resolution=(RESOLUTION,) * 3, orientation=ORIENTATION, root_id=ROOT_ID, - reference_stack=template_volume, + reference_stack=reference_volume, annotation_stack=annotated_volume, structures_list=structures, meshes_dict=meshes_dict, From 78fe6d5249223540a0955189029095854d673147 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Thu, 12 Sep 2024 14:59:45 +0200 Subject: [PATCH 34/38] fix error in the way structure id paths were shown in the example --- .../atlas_generation/atlas_scripts/template_script.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 2ed1542f..4d03b045 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -90,11 +90,11 @@ def retrieve_structure_information(): | id | name | acronym | structure_id_path | rgb_triplet | | | | | | | ├────┼───────────────────┼─────────┼───────────────────┼─────────────────┤ - | 997| root | root | [] | [255, 255, 255] | + | 997| root | root | [997] | [255, 255, 255] | ├────┼───────────────────┼─────────┼───────────────────┼─────────────────┤ - | 8 | Basic cell groups | grey | [997] | [191, 218, 227] | + | 8 | Basic cell groups | grey | [997, 8] | [191, 218, 227] | ├────┼───────────────────┼─────────┼───────────────────┼─────────────────┤ - | 567| Cerebrum | CH | [997, 8] | [176, 240, 255] | + | 567| Cerebrum | CH | [997, 8, 567] | [176, 240, 255] | ╰────┴───────────────────┴─────────┴───────────────────┴─────────────────╯ Returns: From 642e0bb45175496ccf93462bff3e1b4c01c6417d Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Thu, 12 Sep 2024 18:14:53 +0200 Subject: [PATCH 35/38] fix typo --- .../atlas_generation/atlas_scripts/template_script.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index 4d03b045..fdc596f1 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -58,8 +58,8 @@ def retrieve_reference_and_annotation(): reference volume, and the second array is the annotation volume. """ reference = None - annoatation = None - return reference, annoatation + annotation = None + return reference, annotation def retrieve_hemisphere_map(): From 536281f02a5381cdfec2cc8efb280057491ce79e Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 18 Sep 2024 16:27:45 +0200 Subject: [PATCH 36/38] Update brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py Co-authored-by: Alessandro Felder --- .../atlas_generation/atlas_scripts/example_mouse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 2f5bb4ec..06aaf2e7 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -26,7 +26,7 @@ def download_resources(): """ - Download the necessary resources for the atlas. + Download the necessary resources for the atlas. Here we don't, because we can out-source this to the Allen SDK in later functions. """ pass From df352c39d14f137d87a96079d547101e66162911 Mon Sep 17 00:00:00 2001 From: Harry Carey Date: Wed, 18 Sep 2024 17:54:25 +0200 Subject: [PATCH 37/38] adjust comment suggestion to be compliant with the 79 character line limit --- .../atlas_generation/atlas_scripts/example_mouse.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py index 06aaf2e7..04c17962 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/example_mouse.py @@ -26,7 +26,8 @@ def download_resources(): """ - Download the necessary resources for the atlas. Here we don't, because we can out-source this to the Allen SDK in later functions. + Download the necessary resources for the atlas. Here we don't, because we + can out-source this to the Allen SDK in later functions. """ pass From c15a4493bc39756354f02bd8b16043473e3359bf Mon Sep 17 00:00:00 2001 From: alessandrofelder Date: Mon, 28 Oct 2024 16:14:11 +0000 Subject: [PATCH 38/38] add additional reference to packaging template script --- .../atlas_generation/atlas_scripts/template_script.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py index fdc596f1..f2e92807 100644 --- a/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py +++ b/brainglobe_atlasapi/atlas_generation/atlas_scripts/template_script.py @@ -115,6 +115,15 @@ def retrieve_or_construct_meshes(): return meshes_dict +def retrieve_additional_references(): + """This function only needs editing if the atlas has additional reference + images. It should return a dictionary that maps the name of each + additional reference image to an image stack containing its data. + """ + additional_references = {} + return additional_references + + ### If the code above this line has been filled correctly, nothing needs to be ### edited below (unless variables need to be passed between the functions). if __name__ == "__main__": @@ -122,6 +131,7 @@ def retrieve_or_construct_meshes(): bg_root_dir.mkdir(exist_ok=True) download_resources() reference_volume, annotated_volume = retrieve_reference_and_annotation() + additional_references = retrieve_additional_references() hemispheres_stack = retrieve_hemisphere_map() structures = retrieve_structure_information() meshes_dict = retrieve_or_construct_meshes() @@ -144,4 +154,5 @@ def retrieve_or_construct_meshes(): cleanup_files=False, compress=True, scale_meshes=True, + additional_references=additional_references, )