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

first draft of juvenile zebrafish atlas #440

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
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,277 @@
import time
from pathlib import Path

Check warning on line 2 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L1-L2

Added lines #L1 - L2 were not covered by tests

import numpy as np
import pandas as pd
from brainglobe_utils.IO.image import load_any, save_any
from rich.progress import track

Check warning on line 7 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L4-L7

Added lines #L4 - L7 were not covered by tests

from brainglobe_atlasapi.atlas_generation.mesh_utils import (

Check warning on line 9 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L9

Added line #L9 was not covered by tests
Region,
create_region_mesh,
)
from brainglobe_atlasapi.atlas_generation.wrapup import wrapup_atlas_from_data
from brainglobe_atlasapi.config import DEFAULT_WORKDIR
from brainglobe_atlasapi.structure_tree_util import get_structures_tree

Check warning on line 15 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L13-L15

Added lines #L13 - L15 were not covered by tests

__version__ = 0

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L17

Added line #L17 was not covered by tests

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

Check warning on line 21 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L21

Added line #L21 was not covered by tests

# DOI of the most relevant citable document
CITATION = "unpublished"

Check warning on line 24 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L24

Added line #L24 was not covered by tests

# The scientific name of the species, ie; Rattus norvegicus
SPECIES = "Danio rerio"

Check warning on line 27 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L27

Added line #L27 was not covered by tests

# The URL for the data files
ATLAS_LINK = None

Check warning on line 30 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L30

Added line #L30 was not covered by tests

# The orientation of the **original** atlas data, in BrainGlobe convention:
ORIENTATION = "sal"

Check warning on line 33 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L33

Added line #L33 was not covered by tests

# The id of the highest level of the atlas.
ROOT_ID = 999

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L36

Added line #L36 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 = 1

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L40

Added line #L40 was not covered by tests


def download_resources():

Check warning on line 43 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L43

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

If possible, please use the Pooch library to retrieve any resources.
"""
pass

Check warning on line 49 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L49

Added line #L49 was not covered by tests


def retrieve_reference_and_annotation():

Check warning on line 52 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L52

Added line #L52 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.
"""
atlas_path = Path(

Check warning on line 60 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L60

Added line #L60 was not covered by tests
"/media/ceph/neuroinformatics/neuroinformatics/atlas-forge/JuvenileZebrafish/"
)
reference_image_path = atlas_path / "DAPI.tif"
reference = load_any(reference_image_path)

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L63 - L64 were not covered by tests

mask_path = atlas_path / "masks"
annotation = np.zeros_like(reference, dtype=np.uint16)
annotation[reference > 0] = ROOT_ID

Check warning on line 68 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L66-L68

Added lines #L66 - L68 were not covered by tests

region_names_path = atlas_path / "juvenile_zebrafish_atlas_regions.csv"
region_names_df = pd.read_csv(region_names_path)

Check warning on line 71 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L70-L71

Added lines #L70 - L71 were not covered by tests

for _, row in region_names_df.iterrows():
id = row["id"]
filename = row["filename"]
mask_file = mask_path / filename
mask = load_any(mask_file)
print(mask_file)
annotation[mask > 0] = id

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L73-L79

Added lines #L73 - L79 were not covered by tests

save_any(annotation, Path.home() / "juvenile_zebrafish_annotation.tif")
return reference, annotation

Check warning on line 82 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L81-L82

Added lines #L81 - L82 were not covered by tests


def retrieve_hemisphere_map():

Check warning on line 85 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L85

Added line #L85 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 99 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L99

Added line #L99 was not covered by tests


def retrieve_structure_information():

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L102

Added line #L102 was not covered by tests
"""
This function should return a pandas DataFrame with information about your
atlas.

The DataFrame should be in the following format:

╭────┬───────────────────┬─────────┬───────────────────┬─────────────────╮
| id | name | acronym | structure_id_path | rgb_triplet |
| | | | | |
├────┼───────────────────┼─────────┼───────────────────┼─────────────────┤
| 997| root | root | [997] | [255, 255, 255] |
├────┼───────────────────┼─────────┼───────────────────┼─────────────────┤
| 8 | Basic cell groups | grey | [997, 8] | [191, 218, 227] |
├────┼───────────────────┼─────────┼───────────────────┼─────────────────┤
| 567| Cerebrum | CH | [997, 8, 567] | [176, 240, 255] |
╰────┴───────────────────┴─────────┴───────────────────┴─────────────────╯

Returns:
pandas.DataFrame: A DataFrame containing the atlas information.
"""
atlas_path = Path(

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L123

Added line #L123 was not covered by tests
"/media/ceph/neuroinformatics/neuroinformatics/atlas-forge/JuvenileZebrafish/"
)
structures = []

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L126

Added line #L126 was not covered by tests

region_names_path = atlas_path / "juvenile_zebrafish_atlas_regions.csv"
region_names_df = pd.read_csv(region_names_path)

Check warning on line 129 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L128-L129

Added lines #L128 - L129 were not covered by tests

for _, row in region_names_df.iterrows():
id = row["id"]
structure = {

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L131-L133

Added lines #L131 - L133 were not covered by tests
"id": id,
"acronym": row["acronym"],
"name": row["region_name"],
"structure_id_path": [999, id],
"rgb_triplet": [0, 125, 125],
}
structures.append(structure)

Check warning on line 140 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L140

Added line #L140 was not covered by tests

structures.append(

Check warning on line 142 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L142

Added line #L142 was not covered by tests
{
"id": ROOT_ID,
"acronym": "root",
"name": "root",
"structure_id_path": [999],
"rgb_triplet": [255, 255, 255],
}
)
return structures

Check warning on line 151 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L151

Added line #L151 was not covered by tests


def retrieve_or_construct_meshes(hierarchy, annotated_volume):

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L154

Added line #L154 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.
"""
tree = get_structures_tree(hierarchy)
print(

Check warning on line 163 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L162-L163

Added lines #L162 - L163 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 172 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L169-L172

Added lines #L169 - L172 were not covered by tests
else:
is_label = False

Check warning on line 174 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L174

Added line #L174 was not covered by tests

node.data = Region(is_label)

Check warning on line 176 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L176

Added line #L176 was not covered by tests

meshes_dir_path = DEFAULT_WORKDIR / "tmp_meshes"
meshes_dir_path.mkdir(exist_ok=True)

Check warning on line 179 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L178-L179

Added lines #L178 - L179 were not covered by tests

# mesh creation
closing_n_iters = 2
decimate_fraction = 0.3
smooth = True

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L182-L184

Added lines #L182 - L184 were not covered by tests

start = time.time()

Check warning on line 186 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L186

Added line #L186 was not covered by tests

for node in track(

Check warning on line 188 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L188

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

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L193

Added line #L193 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 207 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L207

Added line #L207 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 hierarchy:

Check warning on line 216 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L214-L216

Added lines #L214 - L216 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 221 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L218 - L221 were not covered by tests
else:
# check that the mesh actually exists and isn't empty
if mesh_path.stat().st_size < 512:
print(f"obj file for {s} is too small, ignoring it.")
continue
structures_with_mesh.append(s)
meshes_dict[s["id"]] = mesh_path

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L224-L228

Added lines #L224 - L228 were not covered by tests

print(

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

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L230

Added line #L230 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 234 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L234

Added line #L234 was not covered by tests


def retrieve_additional_references():

Check warning on line 237 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L237

Added line #L237 was not covered by tests
"""This function only needs editing if the atlas has additional reference
images. It should return a dictionary that maps the name of each
additional reference image to an image stack containing its data.
"""
additional_references = {}
return additional_references

Check warning on line 243 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L242-L243

Added lines #L242 - L243 were 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 = Path.home() / "brainglobe_workingdir" / ATLAS_NAME
bg_root_dir.mkdir(exist_ok=True)
download_resources()
reference_volume, annotated_volume = retrieve_reference_and_annotation()
additional_references = retrieve_additional_references()
hemispheres_stack = retrieve_hemisphere_map()
structures = retrieve_structure_information()
meshes_dict = retrieve_or_construct_meshes(structures, annotated_volume)

Check warning on line 256 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L248-L256

Added lines #L248 - L256 were not covered by tests

output_filename = wrapup_atlas_from_data(

Check warning on line 258 in brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_atlasapi/atlas_generation/atlas_scripts/juvenile_zebrafish.py#L258

Added line #L258 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=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,
additional_references=additional_references,
)
Loading