Source code for node_colormap

# This file is part of Sympathy for Data.
# Copyright (c) 2018, Combine Control Systems AB
#
# SYMPATHY FOR DATA COMMERCIAL LICENSE
# You should have received a link to the License with Sympathy for Data.
import numpy as np
import matplotlib as mpl

from sympathy.api import node as synode
from sympathy.platform.exceptions import SyError
from sympathy.api.nodeconfig import Port, Ports, Tag, Tags
from sylib.figure import colors


[docs] class ColormapLookup(synode.Node): name = 'Colormap lookup' icon = 'colourmap.svg' description = 'Maps input values into colours based on a colourmap.' nodeid = 'org.sysess.sympathy.visualize.colormap_lookup' tags = Tags(Tag.Visual.Figure) inputs = Ports([Port.Table('Input data', name='input')]) outputs = Ports([Port.Table('Output data', name='output')]) parameters = synode.parameters() parameters.set_string( 'colormap', value='viridis', label='Colormap', description=( 'Colormap used for converting the input values'), editor=synode.editors.combo_editor( options=list(colors.COLORMAPS.keys()))) parameters.set_float( 'vmin', value=0.0, label='vmin', description=( 'First (lowest) value that should be mapped to colors')) parameters.set_float( 'vmax', value=1.0, label='vmax', description=( 'Last (highest) value that should be mapped to colors')) parameters.set_string( 'suffix', value='', label='Suffix', description=('Suffix added to each column name when ' 'generating the output names')) def execute(self, node_context): in_tbl = node_context.input['input'] out_tbl = node_context.output['output'] suffix = node_context.parameters['suffix'].value vmin = node_context.parameters['vmin'].value vmax = node_context.parameters['vmax'].value colormap_name = node_context.parameters['colormap'].value colormap = mpl.colormaps[colors.COLORMAPS[colormap_name]] norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax) for col in in_tbl.cols(): try: data = (colormap(norm(col.data)) * 255)[:, :3].astype(int) except TypeError as exc: raise SyError( "Colormap node requires all input " "columns to be numerical." ) from exc as_strings = ['#{:0>2X}{:0>2X}{:0>2X}' .format(*list(c)) for c in data] out_tbl.set_column_from_array(col.name+suffix, np.array(as_strings))