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 all 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,375 @@
__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 meshio as mio
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 15 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-L15

Added lines #L6 - L15 were not covered by tests

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

Check warning on line 18 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#L17-L18

Added lines #L17 - L18 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 23 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#L22-L23

Added lines #L22 - L23 were not covered by tests


def create_atlas(working_dir, resolution):

Check warning on line 26 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#L26

Added line #L26 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 33 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#L28-L33

Added lines #L28 - L33 were not covered by tests

# Parameters for mesh creation:
ROOT_ID = 997
CLOSING_N_ITERS = 2
DECIMATE_FRACTION = 0.3
SMOOTH = True

Check warning on line 39 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#L36-L39

Added lines #L36 - L39 were not covered by tests

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

Check warning on line 43 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#L42-L43

Added lines #L42 - L43 were not covered by tests

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

Check warning on line 47 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#L47

Added line #L47 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 55 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#L55

Added line #L55 was not covered by tests

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

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#L60-L61

Added lines #L60 - L61 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 66 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-L66

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

Check warning on line 68 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#L68

Added line #L68 was not covered by tests

utils.check_internet_connection()
annotation_file_path = pooch.retrieve(

Check warning on line 71 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#L70-L71

Added lines #L70 - L71 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 79 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#L79

Added line #L79 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 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#L83-L84

Added lines #L83 - L84 were not covered by tests

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

Check warning on line 87 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#L87

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

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
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 98 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#L98

Added line #L98 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 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#L101-L102

Added lines #L101 - L102 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 109 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#L109

Added line #L109 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 112 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#L112

Added line #L112 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 128 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#L127-L128

Added lines #L127 - L128 were not covered by tests

matching_dicts = []

Check warning on line 130 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#L130

Added line #L130 was not covered by tests

def recurse(sub_d):
contains_substring = False

Check warning on line 133 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#L132-L133

Added lines #L132 - L133 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 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#L135-L143

Added lines #L135 - L143 were not covered by tests

if contains_substring:
matching_dicts.append(sub_d)

Check warning on line 146 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-L146

Added lines #L145 - L146 were not covered by tests

recurse(d)

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

return matching_dicts

Check warning on line 150 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#L150

Added line #L150 was not covered by tests

matching_dicts = find_dicts_with_key_containing_substring(

Check warning on line 152 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#L152

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

Check warning on line 155 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#L155

Added line #L155 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 160 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#L160

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

Check warning on line 163 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#L163

Added line #L163 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 171 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#L171

Added line #L171 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 182 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#L181-L182

Added lines #L181 - L182 were not covered by tests

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

Check warning on line 187 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#L185-L187

Added lines #L185 - L187 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 192 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#L189-L192

Added lines #L189 - L192 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 196 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-L196

Added lines #L195 - L196 were not covered by tests

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

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
# Find corresponding parent structure
if d["graph_order"] == 52: # barrel-level -> find SSp-bfd

Check warning on line 202 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#L202

Added line #L202 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 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#L204

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

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
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 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#L210-L211

Added lines #L210 - L211 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 214 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#L214

Added line #L214 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 218 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

Added line #L218 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 228 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#L225-L228

Added lines #L225 - L228 were not covered by tests

# Add list of dicts to structs_with_mesh
structs_with_mesh = structs_with_mesh + dict_to_add

Check warning on line 231 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#L231

Added line #L231 was not covered by tests

# Directory for mesh saving:
meshes_dir = (

Check warning on line 234 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#L234

Added line #L234 was not covered by tests
working_dir
/ descriptors.MESHES_DIRNAME
/ "meshes_{}".format(resolution)
)
# If directory exists, then skip
if not meshes_dir.exists():
meshes_dir.mkdir(exist_ok=False, parents=True)

Check warning on line 241 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#L240-L241

Added lines #L240 - L241 were 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"

Check warning on line 248 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-L248

Added lines #L244 - L248 were not covered by tests

if filename.exists():
meshes_dict[name] = filename
continue

Check warning on line 252 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#L250-L252

Added lines #L250 - L252 were not covered by tests

try:
space.download_structure_mesh(

Check warning on line 255 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#L254-L255

Added lines #L254 - L255 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 262 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#L260-L262

Added lines #L260 - L262 were not covered by tests

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

Check warning on line 266 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-L266

Added lines #L265 - L266 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_)

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

for key, node in tree.nodes.items():
if key in labels:
is_label = True

Check warning on line 276 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#L274-L276

Added lines #L274 - L276 were not covered by tests
else:
is_label = False

Check warning on line 278 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

Added line #L278 was not covered by tests

node.data = Region(is_label)

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#L280

Added line #L280 was not covered by tests

start = time.time()

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

for node in track(

Check warning on line 284 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#L284

Added line #L284 was not covered by tests
tree.nodes.values(),
total=tree.size(),
description="Creating meshes",
):

# Check if mesh already exists
file_name = meshes_dir / f"{node.identifier}.obj"
if file_name.exists():
meshes_dict[node.identifier] = file_name
continue

Check warning on line 294 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#L291-L294

Added lines #L291 - L294 were not covered by tests

else:
create_region_mesh(

Check warning on line 297 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#L297

Added line #L297 was not covered by tests
(
meshes_dir,
node,
tree,
labels,
annotated_volume,
ROOT_ID,
CLOSING_N_ITERS,
DECIMATE_FRACTION,
SMOOTH,
)
)
meshes_dict[node.identifier] = file_name

Check warning on line 310 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#L310

Added line #L310 was not covered by tests

print(

Check warning on line 312 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

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

# Once mesh creation is over, rescale
for mesh_id, meshfile in meshes_dict.items():

Check warning on line 319 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#L319

Added line #L319 was not covered by tests
# Check if mesh is barrel-related
if mesh_id in [s["id"] for s in dict_to_add]:

Check warning on line 321 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#L321

Added line #L321 was not covered by tests

try:
mesh = mio.read(meshfile)
mesh.points *= resolution
mio.write(meshfile, mesh)
except mio._exceptions.ReadError:
print(f"Mesh file {meshfile} not found.")

Check warning on line 328 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#L323-L328

Added lines #L323 - L328 were not covered by tests

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

Check warning on line 332 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#L331-L332

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

# Remove problematic 545 structure
if 545 in meshes_dict.keys():
meshes_dict.pop(545)

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#L338-L339

Added lines #L338 - L339 were not covered by tests

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

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

output_filename = wrapup_atlas_from_data(

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
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 364 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#L364

Added line #L364 was not covered by tests


if __name__ == "__main__":
RES_UM = 25

Check warning on line 368 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#L367-L368

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

Check warning on line 370 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#L370

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

Check warning on line 373 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#L373

Added line #L373 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 375 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#L375

Added line #L375 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