Skip to content

Commit

Permalink
list atlases (#40)
Browse files Browse the repository at this point in the history
* list atlases

* added test

* test fix

* auto detect available atlases

* test_get_atlas_from_name

* added rich to requirements.txt
  • Loading branch information
FedeClaudi committed Jun 16, 2020
1 parent becbdcb commit 73b3e06
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 2 deletions.
20 changes: 20 additions & 0 deletions brainatlas_api/__init__.py
@@ -0,0 +1,20 @@
from brainatlas_api import bg_atlas
from brainatlas_api.list_atlases import list_atlases

available_atlases = [
cls for cls in map(bg_atlas.__dict__.get, bg_atlas.__all__)
]


def get_atlas_class_from_name(name):
names = [
f"{atlas.atlas_name}_v{atlas.version}" for atlas in available_atlases
]
atlases = {n: a for n, a in zip(names, available_atlases)}

if name in atlases.keys():
return atlases[name]
else:
print(f"Could not find atlas with name {name}. Available atlases:\n")
list_atlases()
return None
2 changes: 2 additions & 0 deletions brainatlas_api/bg_atlas.py
Expand Up @@ -8,6 +8,8 @@

COMPRESSED_FILENAME = "atlas.tar.gz"

__all__ = ["FishAtlas", "RatAtlas", "AllenBrain25Um", "AllenHumanBrain500Um"]


class BrainGlobeAtlas(core.Atlas):
"""Add download functionalities to Atlas class.
Expand Down
64 changes: 64 additions & 0 deletions brainatlas_api/list_atlases.py
@@ -0,0 +1,64 @@
from pathlib import Path
from rich.table import Table
from rich import print as rprint

from brainatlas_api import config
from brainatlas_api import bg_atlas


"""
Some functionality to list all available and downloaded brainglobe atlases
"""


def list_atlases():
# Parse config
conf = config.read_config()
brainglobe_dir = Path(conf["default_dirs"]["brainglobe_dir"])

# ----------------------------- Get local atlases ---------------------------- #
atlases = {}
for elem in brainglobe_dir.iterdir():
if elem.is_dir():
atlases[elem.name] = dict(
downloaded=True,
local=str(elem),
online=bg_atlas.BrainGlobeAtlas._remote_url_base.format(
elem.name
),
)

# ---------------------- Get atlases not yet downloaded ---------------------- #
available_atlases = [
cls for cls in map(bg_atlas.__dict__.get, bg_atlas.__all__)
]
for atlas in available_atlases:
name = f"{atlas.atlas_name}_v{atlas.version}"
if name not in atlases.keys():
atlases[str(name)] = dict(
downloaded=False,
local="[red]---[/red]",
online=atlas._remote_url_base.format(name),
)

# -------------------------------- print table ------------------------------- #
table = Table(
show_header=True,
header_style="bold green",
title="\n\nBrainglobe Atlases",
)
table.add_column("Name")
table.add_column("Downloaded")
table.add_column("Local path")
table.add_column("Online path", style="dim")

for atlas, info in atlases.items():
if info["downloaded"]:
downloaded = "[green]:heavy_check_mark:[/green]"
else:
downloaded = "[red]---[/red]"
table.add_row(
"[b]" + atlas + "[/b]", downloaded, info["local"], info["online"]
)

rprint(table)
3 changes: 2 additions & 1 deletion requirements.txt
Expand Up @@ -5,4 +5,5 @@ treelib
pandas
requests
meshio
click
click
rich
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -30,7 +30,8 @@
python_requires=">=3.6, <3.8",
entry_points={
"console_scripts": [
"brainatlas_config = brainatlas_api.config:cli_modify_config"
"brainatlas_config = brainatlas_api.config:cli_modify_config",
"brainatlas_list_atlases = brainatlas_api.list_atlases:list_atlases",
]
},
packages=find_namespace_packages(exclude=("atlas_gen", "docs", "tests*")),
Expand Down
15 changes: 15 additions & 0 deletions tests/test_list_atlases.py
@@ -0,0 +1,15 @@
import brainatlas_api


def test_list_atlases():
brainatlas_api.list_atlases()


def test_get_atlas_from_name():
a1 = brainatlas_api.get_atlas_class_from_name("allen_mouse_25um_v0.2")
a2 = brainatlas_api.get_atlas_class_from_name("xxxx")

if a1 is None:
raise ValueError
if a2 is not None:
raise ValueError

0 comments on commit 73b3e06

Please sign in to comment.