Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 21, 2024
1 parent d0addd9 commit 176eb0a
Showing 1 changed file with 59 additions and 56 deletions.
115 changes: 59 additions & 56 deletions brainglobe_atlasapi/atlas_generation/atlas_scripts/kim_devccf_mouse.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import json
import multiprocessing as mp
import time
from pathlib import Path
from os import listdir, path
import pooch

import numpy as np
import pandas as pd
import pooch
from brainglobe_utils.IO.image import load_nii
from rich.progress import track

Expand All @@ -16,7 +14,6 @@
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

Expand All @@ -31,84 +28,93 @@
PACKAGER = "Carlo Castoldi <castoldi[at]ipmc.cnrs.fr>"
ATLAS_FILE_URL = "https://doi.org/10.6084/m9.figshare.26377171.v1"

TIMEPOINTS = (
"E11.5",
"E13.5",
"E15.5",
"E18.5",
"P04",
"P14",
"P56"
)
TIMEPOINTS = ("E11.5", "E13.5", "E15.5", "E18.5", "P04", "P14", "P56")
RESOLUTIONS = (20, 50)
MODALITIES = (
"LSFM", # Light Sheet Fluorescence Microscopy
"LSFM", # Light Sheet Fluorescence Microscopy
"MRI-adc", # MRI Apparent Diffusion Coefficient
"MRI-dwi", # MRI Difusion Weighted Imaging
"MRI-fa", # MRI Fractional Anisotropy
"MRI-fa", # MRI Fractional Anisotropy
"MRI-MTR", # MRI Magnetization Transfer Ratio
"MRI-T2" # MRI T2-weighted
"MRI-T2", # MRI T2-weighted
)


def pooch_init(download_dir_path: Path):
utils.check_internet_connection()

hash = None
registry = {a+".zip": hash for a in TIMEPOINTS}
registry = {a + ".zip": hash for a in TIMEPOINTS}
registry["DevCCFv1_OntologyStructure.xlsx"] = hash
return pooch.create(
path=download_dir_path, #/ATLAS_NAME,
path=download_dir_path, # /ATLAS_NAME,
base_url="doi:10.6084/m9.figshare.26377171.v1/",
registry=registry
registry=registry,
)


def fetch_animal(pooch_: pooch.Pooch, age: str):
assert age in TIMEPOINTS, f"Unknown age timepoint: '{age}'"
archive = age+".zip"
archive = age + ".zip"
members = [
f"{age}/{age.replace('.','-')}_DevCCF_Annotations_20um.nii.gz",
f"{age}/{age.replace('.','-')}_LSFM_20um.nii.gz"
f"{age}/{age.replace('.','-')}_LSFM_20um.nii.gz",
]
annotations_path, reference_path = pooch_.fetch(archive,
progressbar=True,
processor=pooch.Unzip(extract_dir=".", members=members)
)
annotations_path, reference_path = pooch_.fetch(
archive,
progressbar=True,
processor=pooch.Unzip(extract_dir=".", members=members),
)
# archive_path: Path = (pooch_.path/archive)
# archive_path.unlink()
annotations = load_nii(annotations_path, as_array=True)
reference = load_nii(reference_path, as_array=True)
return annotations, reference


def fetch_ontology(pooch_: pooch.Pooch):
devccfv1_path = pooch_.fetch("DevCCFv1_OntologyStructure.xlsx", progressbar=True)
devccfv1_path = pooch_.fetch(
"DevCCFv1_OntologyStructure.xlsx", progressbar=True
)
xl = pd.ExcelFile(devccfv1_path)
# xl.sheet_names # it has two excel sheets
# 'DevCCFv1_Ontology', 'README'
# 'DevCCFv1_Ontology', 'README'
df = xl.parse("DevCCFv1_Ontology", header=1)
df = df[["Acronym", "ID16", "Name", "Structure ID Path16", "R", "G", "B"]]
df.rename(columns={
"Acronym": "acronym",
"ID16": "id",
"Name": "name",
"Structure ID Path16": "structure_id_path",
"R": "r",
"G": "g",
"B": "b"
}, inplace=True)
df.rename(
columns={
"Acronym": "acronym",
"ID16": "id",
"Name": "name",
"Structure ID Path16": "structure_id_path",
"R": "r",
"G": "g",
"B": "b",
},
inplace=True,
)
structures = list(df.to_dict(orient="index").values())
for structure in structures:
if structure["acronym"] == "mouse":
structure["acronym"] = "root"
structure_path = structure["structure_id_path"]
structure["structure_id_path"] = [int(id) for id in structure_path.strip("/").split("/")]
structure["rgb_triplet"] = [structure["r"], structure["g"], structure["b"]]
structure["structure_id_path"] = [
int(id) for id in structure_path.strip("/").split("/")
]
structure["rgb_triplet"] = [
structure["r"],
structure["g"],
structure["b"],
]
del structure["r"]
del structure["g"]
del structure["b"]
return structures

def create_meshes(output_path: str|Path,
structures, annotation_volume, root_id):

def create_meshes(
output_path: str | Path, structures, annotation_volume, root_id
):
if not isinstance(output_path, Path):
output_path = Path(output_path)
output_path.mkdir(exist_ok=True)
Expand All @@ -126,7 +132,7 @@ def create_meshes(output_path: str|Path,

# Mesh creation
closing_n_iters = 2
decimate_fraction = 0.2 # 0.04
decimate_fraction = 0.2 # 0.04
# What fraction of the original number of vertices is to be kept.
smooth = False # smooth meshes after creation
start = time.time()
Expand All @@ -137,7 +143,7 @@ def create_meshes(output_path: str|Path,
# total=5,
description="Creating meshes",
):
output_file = output_path/f"{node.identifier}.obj"
output_file = output_path / f"{node.identifier}.obj"
if output_file.exists():
# print(f"mesh already existing: {output_file.exists()} - {output_file}")
continue
Expand All @@ -164,6 +170,7 @@ def create_meshes(output_path: str|Path,
)
return output_path


def create_mesh_dict(structures, meshes_dir_path):
meshes_dict = dict()
structures_with_mesh = []
Expand All @@ -186,30 +193,26 @@ def create_mesh_dict(structures, meshes_dir_path):
)
return meshes_dict, structures_with_mesh


if __name__ == "__main__":
bg_root_dir = DEFAULT_WORKDIR/ATLAS_NAME
download_dir_path = bg_root_dir/"downloads"
bg_root_dir = DEFAULT_WORKDIR / ATLAS_NAME
download_dir_path = bg_root_dir / "downloads"
download_dir_path.mkdir(exist_ok=True, parents=True)
pooch_ = pooch_init(download_dir_path)
structures = fetch_ontology(pooch_)
# save regions list json:
with open(bg_root_dir/"structures.json", "w") as f:
with open(bg_root_dir / "structures.json", "w") as f:
json.dump(structures, f)

for age in TIMEPOINTS:
atlas_name = f"{ATLAS_NAME}_{age.replace('.', '-')}"
annotation_volume, reference_volume = fetch_animal(pooch_, age)
atlas_dir = bg_root_dir/atlas_name
atlas_dir = bg_root_dir / atlas_name
atlas_dir.mkdir(exist_ok=True)
print(f"Saving atlas data at {atlas_dir}")
# Create meshes:
meshes_dir_path = atlas_dir/"meshes"
create_meshes(
meshes_dir_path,
structures,
annotation_volume,
ROOT_ID
)
meshes_dir_path = atlas_dir / "meshes"
create_meshes(meshes_dir_path, structures, annotation_volume, ROOT_ID)
meshes_dict, structures_with_mesh = create_mesh_dict(
structures, meshes_dir_path
)
Expand All @@ -221,7 +224,7 @@ def create_mesh_dict(structures, meshes_dir_path):
citation=CITATION,
atlas_link=ATLAS_LINK,
species=SPECIES,
resolution=(RESOLUTION_UM,)*3,
resolution=(RESOLUTION_UM,) * 3,
orientation=ORIENTATION,
root_id=ROOT_ID,
reference_stack=reference_volume,
Expand All @@ -230,10 +233,10 @@ def create_mesh_dict(structures, meshes_dir_path):
meshes_dict=meshes_dict,
working_dir=atlas_dir,
atlas_packager=PACKAGER,
hemispheres_stack=None, # it is symmetric
hemispheres_stack=None, # it is symmetric
cleanup_files=False,
compress=True,
scale_meshes=True,
# resolution_mapping=[2, 1, 0],
)
print("Done. Atlas generated at: ", output_filename)
print("Done. Atlas generated at: ", output_filename)

0 comments on commit 176eb0a

Please sign in to comment.