Skip to content

Commit f742d7e

Browse files
committed
methods matplotlib invoked raises correctly if not installed
Matplotlib is not a hard requirement to use siibra-python. It is required for a few utility functions only.
1 parent 63ea031 commit f742d7e

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

siibra/features/tabular/receptor_density_fingerprint.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
from .. import anchor as _anchor
1717
from . import tabular
1818

19-
from ... import commons, vocabularies
19+
from ...commons import logger
20+
from ...vocabularies import RECEPTOR_SYMBOLS
2021
from ...retrieval import requests
2122

2223
import pandas as pd
@@ -75,9 +76,9 @@ def neurotransmitters(self) -> List[str]:
7576
# Likely ill-formed tsv's
7677
return [
7778
"{} ({})".format(
78-
vocabularies.RECEPTOR_SYMBOLS[t]['neurotransmitter']['label'],
79-
vocabularies.RECEPTOR_SYMBOLS[t]['neurotransmitter']['name'],
80-
) if t in vocabularies.RECEPTOR_SYMBOLS else
79+
RECEPTOR_SYMBOLS[t]['neurotransmitter']['label'],
80+
RECEPTOR_SYMBOLS[t]['neurotransmitter']['name'],
81+
) if t in RECEPTOR_SYMBOLS else
8182
f"{t} (undeciphered)"
8283
for t in self.receptors
8384
]
@@ -107,7 +108,7 @@ def parse_tsv_data(cls, data: dict):
107108
std = [data[_]["density (sd)"] for _ in labels]
108109
except KeyError as e:
109110
print(str(e))
110-
commons.logger.error("Could not parse fingerprint from this dictionary")
111+
logger.error("Could not parse fingerprint from this dictionary")
111112
return {
112113
'unit': next(iter(units)),
113114
'labels': labels,
@@ -124,9 +125,11 @@ def polar_plot(self, *args, backend='matplotlib', **kwargs):
124125
if backend == "matplotlib":
125126
try:
126127
import matplotlib.pyplot as plt
127-
except ImportError:
128-
commons.logger.error("matplotlib not available. Plotting of fingerprints disabled.")
129-
return None
128+
except ImportError as e:
129+
logger.error(
130+
"matplotlib not available. Please install matplotlib or use or another backend such as plotly."
131+
)
132+
raise e
130133
from collections import deque
131134

132135
# default args

siibra/features/tabular/tabular.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from .. import anchor as _anchor
2121

22-
from ... import commons
22+
from ...commons import logger
2323

2424
import pandas as pd
2525
from textwrap import wrap
@@ -91,9 +91,11 @@ def plot(self, *args, backend="matplotlib", **kwargs):
9191
if backend == "matplotlib":
9292
try:
9393
import matplotlib.pyplot as plt
94-
except ImportError:
95-
commons.logger.error("matplotlib not available. Plotting of fingerprints disabled.")
96-
return None
94+
except ImportError as e:
95+
logger.error(
96+
"matplotlib not available. Please install matplotlib or use or another backend such as plotly."
97+
)
98+
raise e
9799
# default kwargs
98100
if kwargs.get("error_y") is None:
99101
kwargs["yerr"] = kwargs.get("yerr", 'std' if 'std' in self.data.columns else None)

siibra/volumes/parcellationmap.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,13 @@ def get_colormap(self, region_specs: Iterable = None):
703703
-------
704704
ListedColormap
705705
"""
706-
from matplotlib.colors import ListedColormap
707-
import numpy as np
706+
try:
707+
from matplotlib.colors import ListedColormap
708+
except ImportError as e:
709+
logger.error(
710+
"matplotlib not available. Please install matplotlib to create a matplotlib colormap."
711+
)
712+
raise e
708713

709714
colors = {}
710715
if region_specs is not None:

0 commit comments

Comments
 (0)