Icon

kn_​forum_​python_​color_​manager

Transform KNIME Color Manager to Python Color Dictionary

Inspired by a forum question I constructed a Python script that would transform KNIME Color manager colors into a Python dictionary that could be used to colourize graphics.

There is also a Jupyter notebook in the subfolder /script/ to play with.

# Convert RGB to HEX values and store color dictionary as pickle fileimport pickleimport osimport pandas as pddf_color_dict = input_table.copy()# https://stackoverflow.com/questions/3380726/converting-a-rgb-color-tuple-to-a-six-digit-code-in-python# define function to convert RGB to HEXdef rgb2hex(r,g,b): return "#{:02x}{:02x}{:02x}".format(r,g,b)df_color_dict['HEX'] = df_color_dict.apply(lambda row: rgb2hex(row['R'], row['G'], row['B']),axis=1)df_dictionary = df_color_dict[['value','HEX']]df_dictionary.set_index("value", drop=True, inplace=True)# pure dictionary to picklecolour_dictionary = df_dictionary.T.to_dict('records')[0]# list to be exported to KNIME via Pandascolour_dictionary2 = df_dictionary.T.to_dict('records')pkl_file = flow_variables['context.workflow.absolute-path'] + "/data/" +'my_colour_dictionary.pickle' with open(pkl_file, 'wb') as handle: pickle.dump(colour_dictionary, handle, protocol=pickle.HIGHEST_PROTOCOL)# c = pd.DataFrame.from_dict(b, orient='index')output_table = pd.DataFrame.from_dict(colour_dictionary2, orient='columns') import numpy as npimport pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltimport picklefrom sklearn import preprocessingfrom io import BytesIOiris = input_table.copy()# https://www.kaggle.com/xuhewen/iris-dataset-visualization-and-machine-learningiris.rename(columns={'Sepal.Length':'SepalLengthCm' ,'Sepal.Width':'SepalWidthCm' }, inplace=True)# import the colour dictionary from pickle filepkl_file = flow_variables['context.workflow.absolute-path'] + "/data/" + 'my_colour_dictionary.pickle' with open(pkl_file, 'rb') as handle: colour_dictionary = pickle.load(handle)# Create buffer to write intobuffer = BytesIO()fig_dims = (10, 6)fig, ax = plt.subplots(figsize=fig_dims)ax = sns.scatterplot( x="SepalLengthCm", y="SepalWidthCm", data=iris, ax=ax, hue='Species', legend='full', palette=colour_dictionary)ax.set_title('Scatterplot with Color Dictionary derived from KNIME Color Manager') plt.legend(loc='lower right')ax.figure.savefig(buffer, format='svg')# The output is the content of the bufferoutput_image = buffer.getvalue() Inspired by a forum question I constructed a Python script that would transform KNIME Color manager colors into a Pythondictionary that could be used to colourize graphics.https://forum.knime.com/t/python-view-and-color-manager/21110/3?u=mlauber71There is also a Jupyter notebook in the subfolder /script/ to play with. pickle color dictionaryset the colours you wantfor speciesextract the colours selectediris.csviris_plot_without_color_dictionary.pngscatteriris_plot_with_color_dictionary.pngscatterwith dictionary Python Script (1⇒1) Extract ContextProperties Color Manager Extract Color CSV Reader Image Writer (Port) Renderer to Image Table To Image Image To Table Python View Image To Table Table To Image Renderer to Image Image Writer (Port) Python View # Convert RGB to HEX values and store color dictionary as pickle fileimport pickleimport osimport pandas as pddf_color_dict = input_table.copy()# https://stackoverflow.com/questions/3380726/converting-a-rgb-color-tuple-to-a-six-digit-code-in-python# define function to convert RGB to HEXdef rgb2hex(r,g,b): return "#{:02x}{:02x}{:02x}".format(r,g,b)df_color_dict['HEX'] = df_color_dict.apply(lambda row: rgb2hex(row['R'], row['G'], row['B']),axis=1)df_dictionary = df_color_dict[['value','HEX']]df_dictionary.set_index("value", drop=True, inplace=True)# pure dictionary to picklecolour_dictionary = df_dictionary.T.to_dict('records')[0]# list to be exported to KNIME via Pandascolour_dictionary2 = df_dictionary.T.to_dict('records')pkl_file = flow_variables['context.workflow.absolute-path'] + "/data/" +'my_colour_dictionary.pickle' with open(pkl_file, 'wb') as handle: pickle.dump(colour_dictionary, handle, protocol=pickle.HIGHEST_PROTOCOL)# c = pd.DataFrame.from_dict(b, orient='index')output_table = pd.DataFrame.from_dict(colour_dictionary2, orient='columns') import numpy as npimport pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltimport picklefrom sklearn import preprocessingfrom io import BytesIOiris = input_table.copy()# https://www.kaggle.com/xuhewen/iris-dataset-visualization-and-machine-learningiris.rename(columns={'Sepal.Length':'SepalLengthCm' ,'Sepal.Width':'SepalWidthCm' }, inplace=True)# import the colour dictionary from pickle filepkl_file = flow_variables['context.workflow.absolute-path'] + "/data/" + 'my_colour_dictionary.pickle' with open(pkl_file, 'rb') as handle: colour_dictionary = pickle.load(handle)# Create buffer to write intobuffer = BytesIO()fig_dims = (10, 6)fig, ax = plt.subplots(figsize=fig_dims)ax = sns.scatterplot( x="SepalLengthCm", y="SepalWidthCm", data=iris, ax=ax, hue='Species', legend='full', palette=colour_dictionary)ax.set_title('Scatterplot with Color Dictionary derived from KNIME Color Manager') plt.legend(loc='lower right')ax.figure.savefig(buffer, format='svg')# The output is the content of the bufferoutput_image = buffer.getvalue() Inspired by a forum question I constructed a Python script that would transform KNIME Color manager colors into a Pythondictionary that could be used to colourize graphics.https://forum.knime.com/t/python-view-and-color-manager/21110/3?u=mlauber71There is also a Jupyter notebook in the subfolder /script/ to play with. pickle color dictionaryset the colours you wantfor speciesextract the colours selectediris.csviris_plot_without_color_dictionary.pngscatteriris_plot_with_color_dictionary.pngscatterwith dictionaryPython Script (1⇒1) Extract ContextProperties Color Manager Extract Color CSV Reader Image Writer (Port) Renderer to Image Table To Image Image To Table Python View Image To Table Table To Image Renderer to Image Image Writer (Port) Python View

Nodes

Extensions

Links