Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to generate Allen mouse brain atlas with barrel annotations #313

Merged
merged 26 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cf2a8fb
script to generate allen atlas with barrel annotations + pyproject.to…
abisi Jun 21, 2024
e0cc4a0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 21, 2024
b617d3b
voxcell library + update atlas script with 10um res only, meshes, cre…
abisi Jul 5, 2024
5fa35c4
fixed conflicts
abisi Jul 5, 2024
a20fb46
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2024
831e9e2
using gin-hosted pregenerated files
abisi Jul 14, 2024
31ca81a
fixed conflicts
abisi Jul 14, 2024
83fac20
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 14, 2024
c8e1feb
added comments
abisi Jul 15, 2024
1cbee61
mesh creation update and cleanup
abisi Jul 15, 2024
8afd634
removed comments
abisi Jul 15, 2024
10441e1
fixed conflicts
abisi Jul 15, 2024
308a975
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 15, 2024
44aecac
Make pre-commit happy
adamltyson Jul 16, 2024
100f05a
Update brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse…
abisi Jul 16, 2024
bc3e472
mesh params at file top
abisi Jul 16, 2024
533fd8f
post-creation rescaling of new meshes
abisi Jul 18, 2024
d98b34a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 18, 2024
9e1b1f7
Ensure parent directory exists
adamltyson Jul 21, 2024
84ce3b8
Update version
adamltyson Jul 21, 2024
eb431ea
Improve exception handling
adamltyson Jul 21, 2024
a2f6920
Add all meshes to meshes_dict
adamltyson Jul 25, 2024
b951f20
test
abisi Jul 30, 2024
ff1b1c1
Merge branch 'main' of https://github.com/abisi/brainglobe-atlasapi
abisi Jul 30, 2024
be74f37
test with new file + handling strct 545
abisi Jul 30, 2024
1d6f684
Update filenames in meshes_dict
adamltyson Aug 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,344 @@
__version__ = "0"
import json
import time
from pathlib import Path

Check warning on line 4 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L1-L4

Added lines #L1 - L4 were not covered by tests

import nrrd
import numpy as np
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
from requests import exceptions
from rich.progress import track
from tqdm import tqdm

Check warning on line 14 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L6-L14

Added lines #L6 - L14 were not covered by tests

from brainglobe_atlasapi import descriptors, utils
from brainglobe_atlasapi.atlas_generation.mesh_utils import (

Check warning on line 17 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L16-L17

Added lines #L16 - L17 were not covered by tests
Region,
create_region_mesh,
)
from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data
from brainglobe_atlasapi.structure_tree_util import get_structures_tree

Check warning on line 22 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L21-L22

Added lines #L21 - L22 were not covered by tests


def create_atlas(working_dir, resolution):

Check warning on line 25 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L25

Added line #L25 was not covered by tests
# Specify information about the atlas:
ATLAS_NAME = "allen_mouse_bluebrain_barrels"
SPECIES = "Mus musculus"
ATLAS_LINK = "http://www.brain-map.org"
CITATION = "Bolaños-Puchet S., Teska A., et al. (2024). https://doi.org/10.1162/imag_a_00209"
ATLAS_PACKAGER = "Axel Bisi"
ORIENTATION = "asr"

Check warning on line 32 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L27-L32

Added lines #L27 - L32 were not covered by tests

# Temporary folder for nrrd files download:
download_dir_path = working_dir / "downloading_path"
download_dir_path.mkdir(exist_ok=True)

Check warning on line 36 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L35-L36

Added lines #L35 - L36 were not covered by tests

# Download original Allen template volume:
#########################################
spacecache = ReferenceSpaceCache(

Check warning on line 40 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L40

Added line #L40 was not covered by tests
manifest=download_dir_path / "manifest.json",
# downloaded files are stored relative to here
resolution=resolution,
reference_space_key="annotation/ccf_2017",
# use the latest version of the CCF
)

template_volume, _ = spacecache.get_template_volume()

Check warning on line 48 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L48

Added line #L48 was not covered by tests

# Download enhanced barrel-containing Allen annotation files by BlueBrain,
# and hierarchy:
#########################################
annotation_dir_path = working_dir / "downloading_path/annotation_enhanced"
annotation_dir_path.mkdir(exist_ok=True)

Check warning on line 54 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L53-L54

Added lines #L53 - L54 were not covered by tests

if resolution == 10:
gin_url = "https://gin.g-node.org/BrainGlobe/bluebrain_barrel_materials/raw/master/annotation_barrels_10.nrrd"
elif resolution == 25:
gin_url = "https://gin.g-node.org/BrainGlobe/bluebrain_barrel_materials/raw/master/annotation_barrels_25.nrrd"

Check warning on line 59 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L56-L59

Added lines #L56 - L59 were not covered by tests
else:
raise ValueError("Resolution {}um not supported.".format(resolution))

Check warning on line 61 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L61

Added line #L61 was not covered by tests

utils.check_internet_connection()
annotation_file_path = pooch.retrieve(

Check warning on line 64 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L63-L64

Added lines #L63 - L64 were not covered by tests
gin_url,
known_hash=None,
path=annotation_dir_path,
progressbar=True,
)

# Load enhanced annotation volume:
annotated_volume = nrrd.read(annotation_file_path)[0]

Check warning on line 72 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L72

Added line #L72 was not covered by tests

# Download structures tree and meshes:
######################################
oapi = OntologiesApi() # ontologies
struct_tree = spacecache.get_structure_tree() # structures tree

Check warning on line 77 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L76-L77

Added lines #L76 - L77 were not covered by tests

# Find id of set of regions with mesh:
select_set = (

Check warning on line 80 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L80

Added line #L80 was not covered by tests
"Structures whose surfaces are represented by a precomputed mesh"
)

mesh_set_ids = [

Check warning on line 84 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L84

Added line #L84 was not covered by tests
s["id"]
for s in oapi.get_structure_sets()
if s["description"] == select_set
]

# Get structures with mesh for both versions
structs_with_mesh = struct_tree.get_structures_by_set_id(mesh_set_ids)

Check warning on line 91 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L91

Added line #L91 was not covered by tests

# Download hierarchy:
gin_url = "https://gin.g-node.org/BrainGlobe/bluebrain_barrel_materials/raw/master/hierarchy.json"
hierarchy_path = pooch.retrieve(

Check warning on line 95 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L94-L95

Added lines #L94 - L95 were not covered by tests
gin_url,
known_hash=None,
path=annotation_dir_path,
fname="hierarchy.json",
progressbar=True,
)
structs_with_barrels = json.load(open(hierarchy_path))

Check warning on line 102 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L102

Added line #L102 was not covered by tests

# Add barrels structures to Allen structures
def find_dicts_with_key_containing_substring(d, key, substring):

Check warning on line 105 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L105

Added line #L105 was not covered by tests
"""
Recursively find all dictionaries within a nested dictionary that
contain a specific substring in the value associated with a given key.

Args:
d (dict): The input dictionary.
key (str): The key to search for.
substring (str): The substring to search for in the value associated
with the key.

Returns:
list: A list of dictionaries that contain the key with a value
containing the substring.
"""
if not isinstance(d, dict):
raise ValueError("Input should be a dictionary")

Check warning on line 121 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L120-L121

Added lines #L120 - L121 were not covered by tests

matching_dicts = []

Check warning on line 123 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L123

Added line #L123 was not covered by tests

def recurse(sub_d):
contains_substring = False

Check warning on line 126 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L125-L126

Added lines #L125 - L126 were not covered by tests

for k, v in sub_d.items():
if isinstance(v, dict):
recurse(v)
elif isinstance(v, list):
for item in v:
if isinstance(item, dict):
recurse(item)
if k == key and substring in str(v):
contains_substring = True

Check warning on line 136 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L128-L136

Added lines #L128 - L136 were not covered by tests

if contains_substring:
matching_dicts.append(sub_d)

Check warning on line 139 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L138-L139

Added lines #L138 - L139 were not covered by tests

recurse(d)

Check warning on line 141 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L141

Added line #L141 was not covered by tests

return matching_dicts

Check warning on line 143 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L143

Added line #L143 was not covered by tests

matching_dicts = find_dicts_with_key_containing_substring(

Check warning on line 145 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L145

Added line #L145 was not covered by tests
structs_with_barrels, key="name", substring="barrel"
)
matching_dicts = [

Check warning on line 148 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L148

Added line #L148 was not covered by tests
d
for d in matching_dicts
if d["graph_order"] in [52, 53, 54, 55, 56, 57]
]
main_barrel_parent_struct = [

Check warning on line 153 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L153

Added line #L153 was not covered by tests
s for s in structs_with_mesh if s["acronym"] == "SSp-bfd"
][0]
structures_present = [

Check warning on line 156 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L156

Added line #L156 was not covered by tests
"SSp-bfd1",
"SSp-bfd2/3",
"SSp-bfd4",
"SSp-bfd5",
"SSp-bfd6a",
"SSp-bfd6b",
] # keep laminar structures
keys_to_keep = [

Check warning on line 164 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L164

Added line #L164 was not covered by tests
"acronym",
"graph_id",
"graph_order",
"id",
"name",
"rgb_triplet",
"structure_set_ids",
"structure_id_path",
]
dict_to_add = []
for d in matching_dicts:

Check warning on line 175 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L174-L175

Added lines #L174 - L175 were not covered by tests

# Ignore parent-level SSp-bfd layers
if d["acronym"] in structures_present:
print("Skipping", d, "already present.")
continue

Check warning on line 180 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L178-L180

Added lines #L178 - L180 were not covered by tests
# Ignore sub-structures layer 2 and 3 to keep layer 2/3 structure
if d["graph_order"] == 53 and d["acronym"] in ["SSp-bfd2", "SSp-bfd3"]:
print("Excluding", d, "to keep layer 2/3 structure only.")
continue
elif d["graph_order"] == 54 and (

Check warning on line 185 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L182-L185

Added lines #L182 - L185 were not covered by tests
"layer 2" in d["name"] or "layer 3" in d["name"]
):
print("Excluding", d, "to keep layer 2/3 structure only.")
continue

Check warning on line 189 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L188-L189

Added lines #L188 - L189 were not covered by tests

# Add desired barrel-related structures, with corresponding fields
else:
current_id = d["id"]

Check warning on line 193 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L193

Added line #L193 was not covered by tests
# Find corresponding parent structure
if d["graph_order"] == 52: # barrel-level -> find SSp-bfd

Check warning on line 195 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L195

Added line #L195 was not covered by tests
# Create new structure_id_path for barrel structure
d["structure_id_path"] = main_barrel_parent_struct[

Check warning on line 197 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L197

Added line #L197 was not covered by tests
"structure_id_path"
] + [current_id]
elif (

Check warning on line 200 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L200

Added line #L200 was not covered by tests
d["graph_order"] == 53
): # barrel layer-level -> find SSp-bfd-barrel also
parent_struct_id = d["parent_structure_id"]
parent_struct = [

Check warning on line 204 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L203-L204

Added lines #L203 - L204 were not covered by tests
s for s in matching_dicts if s["id"] == parent_struct_id
][0]
parent_struct["structure_id_path"] = main_barrel_parent_struct[

Check warning on line 207 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L207

Added line #L207 was not covered by tests
"structure_id_path"
] + [parent_struct_id]
# Create new structure_id_path for barrel-layer structure
d["structure_id_path"] = (

Check warning on line 211 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L211

Added line #L211 was not covered by tests
main_barrel_parent_struct["structure_id_path"]
+ [d["parent_structure_id"]]
+ [current_id]
)

# Complete with other keys
d["rgb_triplet"] = main_barrel_parent_struct["rgb_triplet"]
d["graph_id"] = 1
d["structure_set_ids"] = None
dict_to_add.append({k: d[k] for k in keys_to_keep})

Check warning on line 221 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L218-L221

Added lines #L218 - L221 were not covered by tests

# Add list of dicts to structs_with_mesh
structs_with_mesh = structs_with_mesh + dict_to_add
print("Added the following structures to the atlas:")
for d in dict_to_add:
print(d["name"])

Check warning on line 227 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L224-L227

Added lines #L224 - L227 were not covered by tests

# Directory for mesh saving:
meshes_dir = working_dir / descriptors.MESHES_DIRNAME

Check warning on line 230 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L230

Added line #L230 was not covered by tests

# Download existing Allen meshes:
space = ReferenceSpaceApi()
meshes_dict = dict()
for s in tqdm(structs_with_mesh):
name = s["id"]
filename = meshes_dir / f"{name}.obj"
try:
space.download_structure_mesh(

Check warning on line 239 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L233-L239

Added lines #L233 - L239 were not covered by tests
structure_id=s["id"],
ccf_version="annotation/ccf_2017",
file_name=filename,
)
meshes_dict[name] = filename
except (exceptions.HTTPError, ConnectionError):
print(f"Failed to download mesh for {s['name']} ({s['id']})")

Check warning on line 246 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L244-L246

Added lines #L244 - L246 were not covered by tests

# Create missing meshes
tree = get_structures_tree(structs_with_mesh)
print(

Check warning on line 250 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L249-L250

Added lines #L249 - L250 were not covered by tests
f"Number of brain regions: {tree.size()}, "
f"max tree depth: {tree.depth()}"
)

# generate binary mask for mesh creation
labels = np.unique(annotated_volume).astype(np.int_)
for key, node in tree.nodes.items():
if key in labels:
is_label = True

Check warning on line 259 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L256-L259

Added lines #L256 - L259 were not covered by tests
else:
is_label = False

Check warning on line 261 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L261

Added line #L261 was not covered by tests

node.data = Region(is_label)

Check warning on line 263 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L263

Added line #L263 was not covered by tests

start = time.time()

Check warning on line 265 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L265

Added line #L265 was not covered by tests

root_id = 997
closing_n_iters = 2
decimate_fraction = 0.3
smooth = True

Check warning on line 270 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L267-L270

Added lines #L267 - L270 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these be defined at the top of the file?


for node in track(

Check warning on line 272 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L272

Added line #L272 was not covered by tests
tree.nodes.values(),
total=tree.size(),
description="Creating meshes",
):
# Check if mesh already exists
filename = meshes_dir / f"{node.identifier}.obj"
if filename.exists():
continue

Check warning on line 280 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L278-L280

Added lines #L278 - L280 were not covered by tests

create_region_mesh(

Check warning on line 282 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L282

Added line #L282 was not covered by tests
(
meshes_dir,
node,
tree,
labels,
annotated_volume,
root_id,
closing_n_iters,
decimate_fraction,
smooth,
)
)

meshes_dict[node.identifier] = filename

Check warning on line 296 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L296

Added line #L296 was not covered by tests

print(

Check warning on line 298 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L298

Added line #L298 was not covered by tests
"Finished mesh extraction in : ",
round((time.time() - start) / 60, 2),
" minutes",
)

# Loop over structures, remove entries not used:
for struct in structs_with_mesh:
[

Check warning on line 306 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L305-L306

Added lines #L305 - L306 were not covered by tests
struct.pop(k)
for k in ["graph_id", "structure_set_ids", "graph_order"]
]

# Wrap up, compress, and remove file:0
print("Finalising atlas")
output_filename = wrapup_atlas_from_data(

Check warning on line 313 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L312-L313

Added lines #L312 - L313 were not covered by tests
atlas_name=ATLAS_NAME,
atlas_minor_version=__version__,
citation=CITATION,
atlas_link=ATLAS_LINK,
species=SPECIES,
resolution=(resolution,) * 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,
additional_metadata={"atlas_packager": ATLAS_PACKAGER},
)

return output_filename

Check warning on line 333 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L333

Added line #L333 was not covered by tests


if __name__ == "__main__":
RES_UM = 10

Check warning on line 337 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L336-L337

Added lines #L336 - L337 were not covered by tests
# Generated atlas path:
bg_root_dir = (

Check warning on line 339 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L339

Added line #L339 was not covered by tests
Path.home() / "brainglobe_workingdir" / "allen_mouse_bluebrain_barrels"
)
bg_root_dir.mkdir(exist_ok=True)

Check warning on line 342 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L342

Added line #L342 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bg_root_dir.mkdir(exist_ok=True)
bg_root_dir.mkdir(parents=True, exist_ok=True)

Needed to work on machines where ~/brainglobe_workingdir doesn't exist yet.


create_atlas(bg_root_dir, RES_UM)

Check warning on line 344 in brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/allen_mouse_bluebrain_barrels.py#L344

Added line #L344 was not covered by tests
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ dev = [
"tox",
]
allenmouse = ["allensdk"]
allenmouse_barrels = [
"allensdk",
"voxcell"]

atlasgen = [
"loguru",
"PyMCubes",
Expand Down
Loading