Skip to content

Improve error if no matplotlib #463

@lauramurgatroyd

Description

@lauramurgatroyd

If we don't have matplotlib we get an error like


To use colormaps other than:  ['viridis', 'plasma', 'inferno', 'magma'], please install matplotlib.
Traceback (most recent call last):
  File "C:\Users\lhe97136\Work\GitHub\CCPi-Users-Antikythera\Visualization\resample-ui.py", line 635, in <module>
    window = AntikitheraGUI()
  File "C:\Users\lhe97136\Work\GitHub\CCPi-Users-Antikythera\Visualization\resample-ui.py", line 91, in __init__
    self._plane_clipping()
  File "C:\Users\lhe97136\Work\GitHub\CCPi-Users-Antikythera\Visualization\resample-ui.py", line 199, in _plane_clipping
    observePlaneClipping(viewer1, viewer1.getInteractor(), "KeyPress")
  File "C:\Users\lhe97136\Work\GitHub\CCPi-Users-Antikythera\Visualization\plane_clip_slice.py", line 44, in observePlaneClipping
    transfer_function = CILColorMaps.get_color_transfer_function('gray',
  File "C:\Users\lhe97136\AppData\Local\miniconda3\envs\cilviewer\lib\site-packages\ccpi\viewer\utils\colormaps.py", line 460, in get_color_transfer_function
    colors = cls.get_color_map(cmap)
  File "C:\Users\lhe97136\AppData\Local\miniconda3\envs\cilviewer\lib\site-packages\ccpi\viewer\utils\colormaps.py", line 453, in get_color_map
    return colors
UnboundLocalError: local variable 'colors' referenced before assignment

The first line of the error (actually its just a print) is the useful part - the rest is not useful.

We should either make sure it returns nothing if matplotlib not found, or make it raise an exceptio rather than just doing a print.

class CILColorMaps(object):
@staticmethod
def get_color_map(cmap):
if not cmap in _color_map_dict.keys():
try:
from matplotlib import cm
colors = []
for x in range(0, 255):
color = cm.get_cmap(cmap)(x)
colors.append([color[0], color[1], color[2]])
except ImportError:
print("To use colormaps other than: ",
"{}, please install matplotlib.".format(str(list(_color_map_dict.keys()))))
else:
colors = _color_map_dict[cmap]
return colors

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions