Skip to content

Commit

Permalink
Atlas metadata print out (#91)
Browse files Browse the repository at this point in the history
* printing metadata

* merged latest PR

* black

* requirement update

* moved to utils + core
  • Loading branch information
FedeClaudi authored Oct 22, 2020
1 parent 739cc0d commit 454c22f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
12 changes: 1 addition & 11 deletions bg_atlasapi/bg_atlas.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from pathlib import Path
import tarfile
import requests

from rich import print as rprint


from bg_atlasapi import utils, config, core, descriptors


Expand Down Expand Up @@ -179,13 +179,3 @@ def check_latest_version(self):
)
return False
return True

def __repr__(self):
"""Fancy print for the atlas providing authors information."""
meta = self.metadata
name_split = self.atlas_name.split("_")
pretty_name = "{} {} atlas (res. {})".format(*name_split)
pretty_string = (
f"{pretty_name}\nFrom: {meta['atlas_link']} ({meta['citation']} )"
)
return pretty_string
33 changes: 32 additions & 1 deletion bg_atlasapi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import numpy as np
from collections import UserDict
import warnings
from rich.console import Console
from io import StringIO

from bg_space import AnatomicalSpace

from bg_atlasapi.utils import read_json, read_tiff
from bg_atlasapi.utils import read_json, read_tiff, _rich_atlas_metadata
from bg_atlasapi.structure_class import StructuresDict
from bg_atlasapi.descriptors import (
METADATA_FILENAME,
Expand Down Expand Up @@ -70,6 +72,35 @@ def __init__(self, path):
self._hemispheres = None
self._lookup = None

def __repr__(self):
"""Fancy print for the atlas providing authors information."""
name_split = self.atlas_name.split("_")
pretty_name = "{} {} atlas (res. {})".format(*name_split)
return pretty_name

def __str__(self):
"""
If the atlas metadat are to be printed
with the built in print function instead of rich's, then
print the rich panel as a string.
It will miss the colors.
"""
buf = StringIO()
_console = Console(file=buf, force_jupyter=False)
_console.print(self)

return buf.getvalue()

def __rich_console__(self, *args):
"""
Method for rich API's console protocol.
Prints the atlas metadata as a table nested in a panel
"""
panel = _rich_atlas_metadata(self.atlas_name, self.metadata)
yield panel

@property
def resolution(self):
"""Make resolution more accessible from class."""
Expand Down
52 changes: 52 additions & 0 deletions bg_atlasapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,62 @@
TimeRemainingColumn,
Progress,
)
from rich.table import Table
from rich.panel import Panel
from rich.pretty import Pretty
from rich.text import Text

logging.getLogger("urllib3").setLevel(logging.WARNING)


def _rich_atlas_metadata(atlas_name, metadata):
orange = "#f59e42"
dimorange = "#b56510"
gray = "#A9A9A9"
mocassin = "#FFE4B5"
cit_name, cit_link = metadata["citation"].split(", ")

# Create a rich table
tb = Table(
box=None,
show_lines=False,
title=atlas_name.replace("_", " ").capitalize(),
title_style=f"bold {orange}",
)

# Add entries to table
tb.add_column(
style=f"bold {mocassin}",
justify="right",
min_width=8,
max_width=40,
)
tb.add_column(min_width=20, max_width=48)

tb.add_row(
"name:",
Text.from_markup(
metadata["name"] + f' [{gray}](v{metadata["version"]})'
),
)
tb.add_row("species:", Text.from_markup(f'[i]{metadata["species"]}'))
tb.add_row("citation:", Text.from_markup(f"{cit_name} [{gray}]{cit_link}"))
tb.add_row("link:", Text.from_markup(metadata["atlas_link"]))

tb.add_row("")
tb.add_row(
"orientation:",
Text.from_markup(f"[bold]{metadata['orientation']}"),
)
tb.add_row("symmetric:", Pretty(metadata["symmetric"]))
tb.add_row("resolution:", Pretty(metadata["resolution"]))
tb.add_row("shape:", Pretty(metadata["shape"]))

# Fit into panel and yield
panel = Panel.fit(tb, border_style=dimorange)
return panel


def atlas_repr_from_name(name):
"""Generate dictionary with atlas description given the name."""
parts = name.split("_")
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pandas
requests
meshio
click
rich>=8.0.0
rich>=9.0.0
bg-space>=0.5.0

0 comments on commit 454c22f

Please sign in to comment.