From 0b38e42599098b3a2255c76238250546822974ba Mon Sep 17 00:00:00 2001 From: FedeClaudi Date: Wed, 21 Oct 2020 18:27:32 +0100 Subject: [PATCH 1/5] printing metadata --- bg_atlasapi/bg_atlas.py | 83 +++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 12 deletions(-) diff --git a/bg_atlasapi/bg_atlas.py b/bg_atlasapi/bg_atlas.py index f7d90a1f..cc7a1b6f 100644 --- a/bg_atlasapi/bg_atlas.py +++ b/bg_atlasapi/bg_atlas.py @@ -2,7 +2,13 @@ import tarfile import requests -from rich import print as rprint +# from rich import print +from rich.table import Table +from rich.panel import Panel +from rich.pretty import Pretty +from rich.text import Text +from rich.console import Console +from io import StringIO from bg_atlasapi import utils, config, core, descriptors @@ -74,7 +80,7 @@ def __init__( if self.remote_version is None: raise ValueError(f"{atlas_name} is not a valid atlas name!") - rprint( + print( f"[magenta2]Bgatlas_api: {self.atlas_name} not found locally. Downloading...[magenta2]" ) self.download_extract_file() @@ -175,7 +181,7 @@ def check_latest_version(self): online = _version_str_from_tuple(self.remote_version) if local != online: - rprint( + print( f"[b][magenta2]Bg_atlasapi[/b]: [b]{self.atlas_name}[/b] version [b]{local}[/b] is not the latest available ([b]{online}[/b]). " + "To update the atlas run in the terminal:[/magenta2]\n" + f" [gold1]brainglobe update -a {self.atlas_name}[/gold1]" @@ -183,12 +189,65 @@ 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 + # 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 + + + 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 + """ + orange = "#f59e42" + dimorange = "#b56510" + gray = "#A9A9A9" + mocassin = "#FFE4B5" + cit_name, cit_link = self.metadata['citation'].split(', ') + + # Create a rich table + tb = Table(box=None, show_lines=False, + title=self.atlas_name.replace('_', ' ').capitalize(), + title_style=f'bold {orange} u') + + # 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(self.metadata['name'] + f' [{gray}](v{self.metadata["version"]})')) + tb.add_row('species:', Text.from_markup(f'[i]{self.metadata["species"]}')) + tb.add_row('citation:', Text.from_markup(f'{cit_name} [{gray}]{cit_link}')) + tb.add_row('link:', Text.from_markup(self.metadata['atlas_link'])) + + tb.add_row('') + tb.add_row('orientation:', Text.from_markup(f"[bold]{self.metadata['orientation']}")) + tb.add_row('symmetric:', Pretty(self.metadata['symmetric'])) + tb.add_row('resolution:', Pretty(self.metadata['resolution'])) + tb.add_row('shape:', Pretty(self.metadata['shape'])) + + # Fit into panel and yield + panel = Panel.fit(tb, border_style=dimorange) + + yield panel \ No newline at end of file From 156f134709dec1dc86941aa610c948181f3eb944 Mon Sep 17 00:00:00 2001 From: FedeClaudi Date: Wed, 21 Oct 2020 18:29:48 +0100 Subject: [PATCH 2/5] merged latest PR --- bg_atlasapi/bg_atlas.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/bg_atlasapi/bg_atlas.py b/bg_atlasapi/bg_atlas.py index 788d1098..e00522e3 100644 --- a/bg_atlasapi/bg_atlas.py +++ b/bg_atlasapi/bg_atlas.py @@ -186,15 +186,12 @@ 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 + 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) + return pretty_name def __str__(self): From b31dcf3439136b445f7471d67f78923c5a3a6198 Mon Sep 17 00:00:00 2001 From: FedeClaudi Date: Wed, 21 Oct 2020 18:30:32 +0100 Subject: [PATCH 3/5] black --- bg_atlasapi/bg_atlas.py | 70 ++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/bg_atlasapi/bg_atlas.py b/bg_atlasapi/bg_atlas.py index e00522e3..d0132e8d 100644 --- a/bg_atlasapi/bg_atlas.py +++ b/bg_atlasapi/bg_atlas.py @@ -188,20 +188,18 @@ def check_latest_version(self): 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) 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. + 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. - It will miss the colors. - """ buf = StringIO() _console = Console(file=buf, force_jupyter=False) @@ -209,39 +207,59 @@ def __str__(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 + Method for rich API's console protocol. + Prints the atlas metadata as a table nested in a panel """ orange = "#f59e42" dimorange = "#b56510" gray = "#A9A9A9" mocassin = "#FFE4B5" - cit_name, cit_link = self.metadata['citation'].split(', ') + cit_name, cit_link = self.metadata["citation"].split(", ") # Create a rich table - tb = Table(box=None, show_lines=False, - title=self.atlas_name.replace('_', ' ').capitalize(), - title_style=f'bold {orange} u') + tb = Table( + box=None, + show_lines=False, + title=self.atlas_name.replace("_", " ").capitalize(), + title_style=f"bold {orange} u", + ) # Add entries to table - tb.add_column(style=f'bold {mocassin}', justify='right', min_width=8, max_width=40) + 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(self.metadata['name'] + f' [{gray}](v{self.metadata["version"]})')) - tb.add_row('species:', Text.from_markup(f'[i]{self.metadata["species"]}')) - tb.add_row('citation:', Text.from_markup(f'{cit_name} [{gray}]{cit_link}')) - tb.add_row('link:', Text.from_markup(self.metadata['atlas_link'])) - - tb.add_row('') - tb.add_row('orientation:', Text.from_markup(f"[bold]{self.metadata['orientation']}")) - tb.add_row('symmetric:', Pretty(self.metadata['symmetric'])) - tb.add_row('resolution:', Pretty(self.metadata['resolution'])) - tb.add_row('shape:', Pretty(self.metadata['shape'])) + tb.add_row( + "name:", + Text.from_markup( + self.metadata["name"] + + f' [{gray}](v{self.metadata["version"]})' + ), + ) + tb.add_row( + "species:", Text.from_markup(f'[i]{self.metadata["species"]}') + ) + tb.add_row( + "citation:", Text.from_markup(f"{cit_name} [{gray}]{cit_link}") + ) + tb.add_row("link:", Text.from_markup(self.metadata["atlas_link"])) + + tb.add_row("") + tb.add_row( + "orientation:", + Text.from_markup(f"[bold]{self.metadata['orientation']}"), + ) + tb.add_row("symmetric:", Pretty(self.metadata["symmetric"])) + tb.add_row("resolution:", Pretty(self.metadata["resolution"])) + tb.add_row("shape:", Pretty(self.metadata["shape"])) # Fit into panel and yield panel = Panel.fit(tb, border_style=dimorange) - yield panel \ No newline at end of file + yield panel From d62c50ac58c4bf6aadc349e6ac788127b8d7c69a Mon Sep 17 00:00:00 2001 From: FedeClaudi Date: Wed, 21 Oct 2020 18:35:21 +0100 Subject: [PATCH 4/5] requirement update --- bg_atlasapi/bg_atlas.py | 8 ++++---- requirements.txt | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bg_atlasapi/bg_atlas.py b/bg_atlasapi/bg_atlas.py index d0132e8d..210f5cf0 100644 --- a/bg_atlasapi/bg_atlas.py +++ b/bg_atlasapi/bg_atlas.py @@ -2,7 +2,7 @@ import tarfile import requests -# from rich import print +from rich import print as rprint from rich.table import Table from rich.panel import Panel from rich.pretty import Pretty @@ -79,7 +79,7 @@ def __init__( if self.remote_version is None: raise ValueError(f"{atlas_name} is not a valid atlas name!") - print( + rprint( f"[magenta2]Bgatlas_api: {self.atlas_name} not found locally. Downloading...[magenta2]" ) self.download_extract_file() @@ -178,7 +178,7 @@ def check_latest_version(self): online = _version_str_from_tuple(self.remote_version) if local != online: - print( + rprint( f"[b][magenta2]Bg_atlasapi[/b]: [b]{self.atlas_name}[/b] version [b]{local}[/b] is not the latest available ([b]{online}[/b]). " + "To update the atlas run in the terminal:[/magenta2]\n" + f" [gold1]brainglobe update -a {self.atlas_name}[/gold1]" @@ -223,7 +223,7 @@ def __rich_console__(self, *args): box=None, show_lines=False, title=self.atlas_name.replace("_", " ").capitalize(), - title_style=f"bold {orange} u", + title_style=f"bold {orange}", ) # Add entries to table diff --git a/requirements.txt b/requirements.txt index f2c6c883..cb63fa93 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,5 +5,5 @@ pandas requests meshio click -rich>=8.0.0 +rich>=9.0.0 bg-space>=0.5.0 From 35bbe176179a15007e9747d46d88450321690b31 Mon Sep 17 00:00:00 2001 From: FedeClaudi Date: Thu, 22 Oct 2020 08:44:48 +0100 Subject: [PATCH 5/5] moved to utils + core --- bg_atlasapi/bg_atlas.py | 86 +---------------------------------------- bg_atlasapi/core.py | 33 +++++++++++++++- bg_atlasapi/utils.py | 52 +++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 86 deletions(-) diff --git a/bg_atlasapi/bg_atlas.py b/bg_atlasapi/bg_atlas.py index 210f5cf0..2208391b 100644 --- a/bg_atlasapi/bg_atlas.py +++ b/bg_atlasapi/bg_atlas.py @@ -1,14 +1,8 @@ from pathlib import Path import tarfile import requests - from rich import print as rprint -from rich.table import Table -from rich.panel import Panel -from rich.pretty import Pretty -from rich.text import Text -from rich.console import Console -from io import StringIO + from bg_atlasapi import utils, config, core, descriptors @@ -185,81 +179,3 @@ def check_latest_version(self): ) return False return True - - 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 - """ - orange = "#f59e42" - dimorange = "#b56510" - gray = "#A9A9A9" - mocassin = "#FFE4B5" - cit_name, cit_link = self.metadata["citation"].split(", ") - - # Create a rich table - tb = Table( - box=None, - show_lines=False, - title=self.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( - self.metadata["name"] - + f' [{gray}](v{self.metadata["version"]})' - ), - ) - tb.add_row( - "species:", Text.from_markup(f'[i]{self.metadata["species"]}') - ) - tb.add_row( - "citation:", Text.from_markup(f"{cit_name} [{gray}]{cit_link}") - ) - tb.add_row("link:", Text.from_markup(self.metadata["atlas_link"])) - - tb.add_row("") - tb.add_row( - "orientation:", - Text.from_markup(f"[bold]{self.metadata['orientation']}"), - ) - tb.add_row("symmetric:", Pretty(self.metadata["symmetric"])) - tb.add_row("resolution:", Pretty(self.metadata["resolution"])) - tb.add_row("shape:", Pretty(self.metadata["shape"])) - - # Fit into panel and yield - panel = Panel.fit(tb, border_style=dimorange) - - yield panel diff --git a/bg_atlasapi/core.py b/bg_atlasapi/core.py index b2d49150..5f819b89 100644 --- a/bg_atlasapi/core.py +++ b/bg_atlasapi/core.py @@ -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, @@ -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.""" diff --git a/bg_atlasapi/utils.py b/bg_atlasapi/utils.py index ef4960f5..8e7b819e 100644 --- a/bg_atlasapi/utils.py +++ b/bg_atlasapi/utils.py @@ -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("_")