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

Perens stereotaxic mri #370

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a5073e7
add perens mri
PolarBean Aug 23, 2024
426e033
update requirement for perens_mri
PolarBean Aug 23, 2024
5866489
remove hardcoded path
PolarBean Aug 23, 2024
7f1d9eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 23, 2024
d479291
fix formatting to pass checks
PolarBean Aug 23, 2024
2128e25
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 23, 2024
d8215ed
fix linting issue
PolarBean Aug 23, 2024
5c5f886
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 23, 2024
af31484
Update brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_ster…
PolarBean Aug 27, 2024
592e255
Merge branch 'brainglobe:main' into perens_stereotaxic_mri
PolarBean Aug 28, 2024
c284faf
Merge branch 'brainglobe:main' into perens_stereotaxic_mri
PolarBean Aug 28, 2024
705b4f7
remove sitk in favour of brainglobe utils
PolarBean Aug 28, 2024
fac995d
move download and extraction logic into a seperate function
PolarBean Aug 28, 2024
71b9f51
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
dff50a7
replace functions that could instead use bg atlas functionality
PolarBean Aug 28, 2024
112a4b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
f129f23
Add stereotaxic atlas to readme
PolarBean Aug 29, 2024
fead940
Update brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_ster…
PolarBean Aug 29, 2024
0efb276
Update brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_ster…
PolarBean Aug 29, 2024
b05021f
update to follow new template
PolarBean Sep 11, 2024
dbe44f1
removed weird nested function
PolarBean Sep 11, 2024
4b8e085
change template to reference for consistency
PolarBean Sep 11, 2024
f568d65
update URL to new version, gubra updated this to fix label issue in lsfm
PolarBean Sep 12, 2024
1fd2290
add progress bar
PolarBean Sep 12, 2024
178d92e
add more print statements so progress is easier to observe
PolarBean Sep 12, 2024
239af9a
added correct orientation
PolarBean Sep 15, 2024
ecc4f40
Merge branch 'brainglobe:main' into perens_stereotaxic_mri
PolarBean Sep 27, 2024
1250a59
added print statement to structure information function
PolarBean Sep 27, 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ A number of atlases are in development, but those available currently are:
| [Blind Mexican Cavefish Brain Atlas](https://doi.org/10.7554/eLife.80777) | 2 micron | 1 year | IHC
| [BlueBrain Barrel Cortex Atlas](https://doi.org/10.1162/imag_a_00209) | 10 and 25 micron | P56 | STPT
| [UNAM Axolotl Brain Atlas](https://doi.org/10.1038/s41598-021-89357-3) | 40 micron | ~ 3 months post hatching | MRI
| [Gubra's stereotaxic MRI mouse brain atlas](https://doi.org/10.1007/s12021-023-09623-9) | 25 micron | 10-week-old | MRI


## Installation

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,344 @@
import multiprocessing as mp
import os
import time
import urllib.request
from pathlib import Path

Check warning on line 5 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L1-L5

Added lines #L1 - L5 were not covered by tests

import numpy as np
import py7zr
from brainglobe_utils.IO.image import load_any
from rich.progress import Progress, track

Check warning on line 10 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L7-L10

Added lines #L7 - L10 were not covered by tests

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

Check warning on line 13 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L12-L13

Added lines #L12 - L13 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 18 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L17-L18

Added lines #L17 - L18 were not covered by tests

# The Perens atlas re-uses information from the Allen atlas, so it's useful to
# have an instance of the Allen atlas around
allen_atlas = BrainGlobeAtlas("allen_mouse_25um")
PARALLEL = True # disable parallel mesh extraction for easier debugging

Check warning on line 23 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L22-L23

Added lines #L22 - L23 were not covered by tests

HEADERS = {

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L25

Added line #L25 was not covered by tests
"User-Agent": (
"Mozilla/5.0 "
"(Windows NT 10.0; "
"Win64; x64; "
"rv:129.0) "
"Gecko/20100101 "
"Firefox/129.0"
),
"Accept": (
"text/html,"
"application/xhtml+xml,"
"application/xml;q=0.9,"
"image/avif,"
"image/webp,"
"image/png,"
"image/svg+xml,"
"*/*;q=0.8"
),
"Accept-Language": "en-GB,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br, zstd",
"DNT": "1",
"Sec-GPC": "1",
"Host": "www.neuropedia.dk",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"TE": "trailers",
"Priority": "u=0, i",
}


# 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

Check warning on line 69 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L69

Added line #L69 was not covered by tests

# The expected format is FirstAuthor_SpeciesCommonName, e.g. kleven_rat, or
# Institution_SpeciesCommonName, e.g. allen_mouse.
ATLAS_NAME = "perens_stereotaxic_mri_mouse"

Check warning on line 73 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L73

Added line #L73 was not covered by tests

# DOI of the most relevant citable document
CITATION = "Perens et al. 2023, https://doi.org/10.1007/s12021-023-09623-9"

Check warning on line 76 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L76

Added line #L76 was not covered by tests

# The scientific name of the species, ie; Rattus norvegicus
SPECIES = "Mus musculus"

Check warning on line 79 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L79

Added line #L79 was not covered by tests

# The URL for the data files
ATLAS_LINK = "https://www.neuropedia.dk/resource/multimodal-3d-mouse-brain-atlas-framework-with-the-skull-derived-coordinate-system/"
ATLAS_FILE_URL = "https://www.neuropedia.dk/wp-content/uploads/Multimodal_mouse_brain_atlas_files_v2.7z"

Check warning on line 83 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L82-L83

Added lines #L82 - L83 were not covered by tests
# The orientation of the **original** atlas data, in BrainGlobe convention:
# https://brainglobe.info/documentation/setting-up/image-definition.html#orientation
ORIENTATION = "als"

Check warning on line 86 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L86

Added line #L86 was not covered by tests

# 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 = 997

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L91

Added line #L91 was not covered by tests

# The resolution of your volume in microns. Details on how to format this
# parameter for non isotropic datasets or datasets with multiple resolutions.
RESOLUTION = 25

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L95

Added line #L95 was not covered by tests

BG_ROOT_DIR = Path.home() / "brainglobe_workingdir" / ATLAS_NAME

Check warning on line 97 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L97

Added line #L97 was not covered by tests


def download_resources():

Check warning on line 100 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L100

Added line #L100 was not covered by tests
"""
Download the necessary resources for the atlas.

If possible, please use the Pooch library to retrieve any resources.
"""
download_dir_path = BG_ROOT_DIR / "downloads"
download_dir_path.mkdir(exist_ok=True)
atlas_files_dir = download_dir_path / "atlas_files"

Check warning on line 108 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L106-L108

Added lines #L106 - L108 were not covered by tests

## Download atlas_file
utils.check_internet_connection()

Check warning on line 111 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L111

Added line #L111 was not covered by tests

destination_path = download_dir_path / "atlas_download.7z"
if not os.path.isdir(

Check warning on line 114 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L113-L114

Added lines #L113 - L114 were not covered by tests
atlas_files_dir / "Multimodal_mouse_brain_atlas_files"
):
req = urllib.request.Request(ATLAS_FILE_URL, headers=HEADERS)
with (

Check warning on line 118 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L117-L118

Added lines #L117 - L118 were not covered by tests
urllib.request.urlopen(req) as response,
open(destination_path, "wb") as out_file,
):
total = int(response.headers.get("content-length", 0))
with Progress() as progress:
task = progress.add_task("[cyan]Downloading...", total=total)
while not progress.finished:
chunk = response.read(1024)
if not chunk:
break
out_file.write(chunk)
progress.update(task, advance=len(chunk))
with py7zr.SevenZipFile(destination_path, mode="r") as z:
adamltyson marked this conversation as resolved.
Show resolved Hide resolved
z.extractall(path=atlas_files_dir)
destination_path.unlink()

Check warning on line 133 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L122-L133

Added lines #L122 - L133 were not covered by tests


def retrieve_reference_and_annotation():

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L136

Added line #L136 was not covered by tests
"""
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.
"""
print("loading reference and annotation volume")
download_dir_path = BG_ROOT_DIR / "downloads"
atlas_files_dir = download_dir_path / "atlas_files"

Check warning on line 146 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L144-L146

Added lines #L144 - L146 were not covered by tests

annotations_file = (

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L148

Added line #L148 was not covered by tests
atlas_files_dir
/ "Multimodal_mouse_brain_atlas_files"
/ "MRI_space_oriented"
/ "mri_ano.nii.gz"
)
reference_file = (

Check warning on line 154 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L154

Added line #L154 was not covered by tests
atlas_files_dir
/ "Multimodal_mouse_brain_atlas_files"
/ "MRI_space_oriented"
/ "mri_temp.nii.gz"
)

annotated_volume = load_any(annotations_file)
reference_volume = load_any(reference_file)

Check warning on line 162 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L161-L162

Added lines #L161 - L162 were not covered by tests

return reference_volume, annotated_volume

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L164

Added line #L164 was not covered by tests


def retrieve_hemisphere_map():

Check warning on line 167 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L167

Added line #L167 was not covered by tests
"""
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

Check warning on line 181 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L181

Added line #L181 was not covered by tests


def retrieve_structure_information():

Check warning on line 184 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L184

Added line #L184 was not covered by tests
"""
Retrieve the structures tree and meshes for the Allen mouse brain atlas.

Returns:
pandas.DataFrame: A DataFrame containing the atlas information.
"""
# Since this atlas inherits from the allen can we not simply get the data
# from the bgapi?
print("determining structures")
allen_atlas = BrainGlobeAtlas("allen_mouse_25um")
allen_structures = allen_atlas.structures_list
allen_structures = [

Check warning on line 196 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L193-L196

Added lines #L193 - L196 were not covered by tests
{
"id": i["id"],
"name": i["name"],
"acronym": i["acronym"],
"structure_id_path": i["structure_id_path"],
"rgb_triplet": i["rgb_triplet"],
}
for i in allen_structures
]
return allen_structures

Check warning on line 206 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L206

Added line #L206 was not covered by tests


def retrieve_or_construct_meshes():

Check warning on line 209 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L209

Added line #L209 was not covered by tests
"""
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.
"""
print("constructing meshes")

Check warning on line 217 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L217

Added line #L217 was not covered by tests

download_dir_path = BG_ROOT_DIR / "downloads"
meshes_dir_path = download_dir_path / "meshes"
meshes_dir_path.mkdir(exist_ok=True)

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L219-L221

Added lines #L219 - L221 were not covered by tests

tree = get_structures_tree(structures)

Check warning on line 223 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L223

Added line #L223 was not covered by tests

labels = np.unique(annotated_volume).astype(np.int32)
for key, node in tree.nodes.items():
if key in labels:
is_label = True

Check warning on line 228 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L225-L228

Added lines #L225 - L228 were not covered by tests
else:
is_label = False

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L230

Added line #L230 was not covered by tests

node.data = Region(is_label)

Check warning on line 232 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L232

Added line #L232 was not covered by tests

# Mesh creation
closing_n_iters = 2 # not used for this atlas
decimate_fraction = 0.2 # not used for this atlas

Check warning on line 236 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L235-L236

Added lines #L235 - L236 were not covered by tests

smooth = False
start = time.time()
if PARALLEL:
pool = mp.Pool(mp.cpu_count() - 2)

Check warning on line 241 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L238-L241

Added lines #L238 - L241 were not covered by tests

try:
pool.map(

Check warning on line 244 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L243-L244

Added lines #L243 - L244 were not covered by tests
create_region_mesh,
[
(
meshes_dir_path,
node,
tree,
labels,
annotated_volume,
ROOT_ID,
closing_n_iters,
decimate_fraction,
smooth,
)
for node in tree.nodes.values()
],
)
except mp.pool.MaybeEncodingError:

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L261

Added line #L261 was not covered by tests
# error with returning results from pool.map but we don't care
pass

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L263

Added line #L263 was not covered by tests
else:
for node in track(

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L265

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

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L270

Added line #L270 was not covered by tests
(
meshes_dir_path,
node,
tree,
labels,
annotated_volume,
ROOT_ID,
closing_n_iters,
decimate_fraction,
smooth,
)
)

print(

Check warning on line 284 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L284

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

# Create meshes dict
meshes_dict = dict()
structures_with_mesh = []
for s in structures:

Check warning on line 293 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L291-L293

Added lines #L291 - L293 were not covered by tests
# Check if a mesh was created
mesh_path = meshes_dir_path / f'{s["id"]}.obj'
if not mesh_path.exists():
print(f"No mesh file exists for: {s}, ignoring it")
continue

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L295-L298

Added lines #L295 - L298 were not covered by tests
else:
# Check that the mesh actually exists (i.e. not empty)
if mesh_path.stat().st_size < 512:
print(f"obj file for {s} is too small, ignoring it.")
continue

Check warning on line 303 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L301-L303

Added lines #L301 - L303 were not covered by tests

structures_with_mesh.append(s)
meshes_dict[s["id"]] = mesh_path

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L305 - L306 were not covered by tests

print(

Check warning on line 308 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L308

Added line #L308 was not covered by tests
f"In the end, {len(structures_with_mesh)} "
"structures with mesh are kept"
)

return meshes_dict

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L313

Added line #L313 was not covered by tests


### 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.mkdir(exist_ok=True)
download_resources()
reference_volume, annotated_volume = retrieve_reference_and_annotation()
hemispheres_stack = retrieve_hemisphere_map()
structures = retrieve_structure_information()
meshes_dict = retrieve_or_construct_meshes()
print("wrapping up atlas")
output_filename = wrapup_atlas_from_data(

Check warning on line 326 in brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/perens_stereotaxic_mri_mouse.py#L318-L326

Added lines #L318 - L326 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=ROOT_ID,
reference_stack=reference_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,
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ allenmouse = ["allensdk"]
allenmouse_barrels = [
"allensdk",
"voxcell"]
perens_stereotaxic_mri_mouse = ["py7zr"]
PolarBean marked this conversation as resolved.
Show resolved Hide resolved

atlasgen = [
"loguru",
Expand Down
Loading