def fade(widget, smoothness=3, cnf={}, **kw): """This function will show faded effect on widget's different color options. Args: widget (tk.Widget): Passed by the bind function. smoothness (int): Set the smoothness of the fading (1-10). background (str): Fade background color to. foreground (str): Fade foreground color to.""" kw = tk._cnfmerge((cnf, kw)) if not kw: raise ValueError("No option given, -bg, -fg, etc") if len(kw) > 1: return [fade(widget, smoothness, {k: v}) for k, v in kw.items()][0] if not getattr(widget, '_after_ids', None): widget._after_ids = {} widget.after_cancel(widget._after_ids.get(list(kw)[0], ' ')) c1 = tuple(map(lambda a: a / 65535, widget.winfo_rgb(widget[list(kw)[0]]))) c2 = tuple(map(lambda a: a / 65535, widget.winfo_rgb(list(kw.values())[0]))) colors = tuple( colour.rgb2hex(c, force_long=True) for c in colour.color_scale(c1, c2, max(1, smoothness * 15))) def worker(count=0): if len(colors) - 1 <= count: return widget.config({list(kw)[0]: colors[count]}) widget._after_ids.update({ list(kw)[0]: widget.after(max(1, int(smoothness / 10)), worker, count + 1) }) worker()
def set_color_range(self, color1, color2): hsl_cs = colour.color_scale(color1.get_hsl(), color2.get_hsl(), 100) rgb_cs = [colour.hsl2rgb(a) for a in hsl_cs] self.scaled_cs = [] for a in rgb_cs: self.scaled_cs.append( (round(a[0] * 255), round(a[1] * 255), round(a[2] * 255)))
def dataset_viz(mu_df, targets): """Creates histograms for given dataset, type filter and targets Args: mu_df (pd.DataFrame): A dataframe holding all failures targets (dict(str,list(str))): Column names from mu_df to perform analysis on """ def lengths(x): if isinstance(x, list): yield len(x) for y in x: yield from lengths(y) rows = len(targets.keys()) cols = max(lengths(list(targets.values()))) fig, axes_list = plt.subplots(rows, cols) axes_list[-1, -1].axis('off') fig.set_size_inches(18, 8) meta_c_df = pd.read_csv('datasets/study_classification_info.csv') meta_r_df = pd.read_csv('datasets/study_regression_info.csv') meta_df = pd.concat([meta_c_df, meta_r_df]) row_size = max([len(x) for x in targets.values()]) base_colors = [ hsl2hex(c) for c in color_scale((0., 0.8, 0.6), (0.8, 0.8, 0.6), row_size) ] for j, TYPE in enumerate(targets): for i, BASE in enumerate(targets[TYPE]): all_data = pd.merge(mu_df.loc[mu_df['TYPE'] == TYPE], meta_df, how='left') full_data = pd.merge(mu_df, meta_df, how='left') ax = axes_list[j][i] ax.set_xlabel(BASE.capitalize() + " Count (Log Scale)") ax.set_ylabel("{} Frequency".format(BASE.capitalize())) counts, bins, bars = ax.hist(all_data[BASE], bins=np.logspace( np.log10(np.min(full_data[BASE])), np.log10(np.max(full_data[BASE])), 30), stacked=True, color=base_colors[i], alpha=0.7, edgecolor='black', linewidth=0.6) ax.set_xscale('log') fig.suptitle('Content Analysis of Datasets') fig.subplots_adjust(hspace=0.4, wspace=0.3) if not os.path.exists('figures'): os.makedirs('figures') plt.savefig('figures/DatasetShapes.pdf', dpi=fig.dpi, transparent=True)
def color_fade(widget, **kw): if not getattr(widget, '_after_ids', None): widget._after_ids = {} widget.after_cancel(widget._after_ids.get(list(kw)[0], ' ')) color_a = tuple(c / 65535 for c in widget.winfo_rgb(widget[list(kw)[0]])) color_b = tuple(c / 65535 for c in widget.winfo_rgb(list(kw.values())[0])) colors = tuple( colour.rgb2hex(color, force_long=True) for color in colour.color_scale(color_a, color_b, 70)) def update_widget_after(count=0): if len(colors) - 1 <= count: return else: widget.config({list(kw)[0]: colors[count]}) widget._after_ids.update( {list(kw)[0]: widget.after(1, update_widget_after, count + 1)}) update_widget_after()
def boxplot_viz(clean_df, target): clean_df = clean_df[target] models = pd.unique(clean_df.index.values) data_arr = np.array([clean_df[m].values for m in models]).T base_colors = [ hsl2hex(c) for c in color_scale((0., 0.8, 0.6), (0.8, 0.8, 0.6), len(models)) ] plt.figure(figsize=(7, 3.5)) title_str = "Raw Per Model {} Comparison ({})".format( 'Classification' if target == 'F1_SCORE' else 'Regression', target) plt.title(title_str, size=12) bplot = plt.boxplot(data_arr, vert=False, patch_artist=True, notch=True, labels=" ", positions=list(reversed(range(1, len(models) + 1)))) for p, c in zip(bplot['boxes'], base_colors): p.set_facecolor(c) plt.legend(bplot['boxes'], models, loc='lower left', prop={'size': 8}, fancybox=True, framealpha=0.6) plt.setp(bplot['fliers'], markeredgecolor='grey') plt.setp(bplot['medians'], color='black') # plt.show() plt.savefig('figures/RawDataBoxPlot{}.pdf'.format(target), dpi=plt.gcf().dpi, transparent=True)
def rg_gradient(start, end, point): gradient = colour.color_scale(colour.web2hsl('red'), colour.web2hsl('green'), 20) stepsize = (end - start) / 20 gradient_bin = np.clip(0, 19, int((point - start) / stepsize)) return colour.hsl2web(gradient[gradient_bin])
def rg_gradient(start, end, point): gradient = colour.color_scale(colour.web2hsl( 'red'), colour.web2hsl('green'), 20) stepsize = (end - start) / 20 gradient_bin = np.clip(0, 19, int((point - start) / stepsize)) return colour.hsl2web(gradient[gradient_bin])
def pairwise_comp_viz(mu_df, target): """Creates a pariwise interaction visualization plot comparing each model against the other Args: mu_df (pd.Dataframe): A dataframe of valid runs with type and model as indicies with aggregated means across runs c_df_info (pd.Dataframe): A dataframe with information about each dataset type """ def plot_comp(mu_df, m1, m2, target, vmin, vmax, cmap, ax): m1_values = mu_df.xs(m1, level=1).values m2_values = mu_df.xs(m2, level=1).values # difference from y=x color mapping (not magnitude because independent) colors = np.array( [m_2 - m_1 for m_2, m_1 in zip(m2_values, m1_values)]) sc = ax.scatter(m1_values, m2_values, alpha=0.7, s=15, c=colors, cmap=cmap, zorder=10, norm=MidpointNormalize(vmin=vmin, vmax=vmax, midpoint=0)) ax.set_xlabel(m1) ax.set_ylabel(m2) lims = [ np.min([ax.get_xlim(), ax.get_ylim()]), np.max([ax.get_xlim(), ax.get_ylim()]) ] ax.plot(lims, lims, 'k-', lw=0.7, alpha=0.7, zorder=0) ax.set_aspect('equal') ax.set_xlim(lims) ax.set_ylim(lims) return sc class MidpointNormalize(mpl.colors.Normalize): def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False): self.midpoint = midpoint mpl.colors.Normalize.__init__(self, vmin, vmax, clip) def __call__(self, value, clip=None): x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1] return np.ma.masked_array(np.interp(value, x, y)) def get_color_range(c1, c2, bins): MAX_L = 0.7 c1h, c1s, c1l = c1.hsl c2h, c2s, c2l = c2.hsl c1_bins = [ Color(hsl=(c1h, c1s, var_l)) for var_l in np.linspace(c1l, MAX_L, int(bins / 2)) ] c2_bins = [ Color(hsl=(c2h, c2s, var_l)) for var_l in np.linspace(MAX_L, c2l, int(bins / 2)) ] color_range = [c.hex_l for c in (c1_bins + c2_bins)] return color_range sort_order = {'auto-sklearn': 1, 'tpot': 2, 'h2o': 3, 'auto_ml': 4} mu_df = mu_df[target] models = sorted(pd.unique(mu_df.index.get_level_values('MODEL').values), key=lambda x: sort_order[x]) combos = list(itertools.combinations(models, 2)) sorted_combos = list( sorted(combos, key=lambda x: (sort_order[x[0]], sort_order[x[1]]))) plot_count = len(sorted_combos) rows, cols = square_fac(plot_count) fig, ax_list = plt.subplots(rows, cols) fig.set_size_inches(17, 8) metric_name = target.replace( '_', ' ').title() if target == 'F1_SCORE' else target base_colors = [ hsl2hex(c) for c in color_scale((0, 0.7, 0.4), (1, 0.7, 0.4), plot_count) ] model_colors = {m: c for m, c in zip(models, base_colors)} color_bins = 10 scatters = [] # get min-max of differences vmin = np.inf vmax = -np.inf for m1, m2 in sorted_combos: m1_values = mu_df.xs(m1, level=1).values m2_values = mu_df.xs(m2, level=1).values colors = np.array( [m_2 - m_1 for m_2, m_1 in zip(m2_values, m1_values)]) if np.max(colors) > vmax: vmax = np.max(colors) if np.min(colors) < vmin: vmin = np.min(colors) for combo, ax in zip(sorted_combos, ax_list.ravel()): m1, m2 = combo color_range = get_color_range(Color(model_colors[m1]), Color(model_colors[m2]), color_bins) cmap = mpl.colors.ListedColormap(color_range) scatters.append(plot_comp(mu_df, m1, m2, target, vmin, vmax, cmap, ax)) for sc, ax in zip(scatters, ax_list.ravel()): cbar = fig.colorbar(sc, ax=ax, fraction=0.046, pad=0.08) cbar.ax.tick_params(labelsize=10) ax_str = '{} Difference' if target == 'F1_score' else 'Standardized Inverted {} Difference' cbar.set_label(ax_str.format(metric_name), rotation=90, fontsize=8, labelpad=-57) # if target == 'F1_SCORE' else -65) fig.suptitle('Dataset Mean {} Across Frameworks'.format(metric_name)) if not os.path.exists('figures'): os.makedirs('figures') plt.savefig('figures/DatasetMean{}.pdf'.format(metric_name.replace( ' ', '')), dpi=fig.dpi, transparent=True)
def correlation_viz(mu_df, targets): """Creates scatterplots of correlation betwene dataset stats and model performance Args: mu_df (pd.DataFrame): A dataframe holding all failures targets (dict(str,list(str))): Column names from mu_df to perform analysis on """ def get_true_features(d_id): df = pd.read_csv('datasets/{}.csv'.format(d_id)) df_types = pd.read_csv('datasets/{}_types.csv'.format(d_id)) #Get categorical encoded column count df_types_cat = df_types.loc[df_types['TYPE'] == 'categorical']['NAME'] df_cat = df[df.columns.intersection(df_types_cat.values)] uniques = [len(df_cat[col].unique()) for col in df_cat] df_types_num = df_types.loc[df_types['TYPE'] == 'numerical']['NAME'] df_num = df[df.columns.intersection(df_types_num.values)] count = np.sum(uniques) + len(df_num.columns) return count plt.gcf().set_size_inches(20, 15) meta_c_df = pd.read_csv('datasets/study_classification_info.csv') meta_r_df = pd.read_csv('datasets/study_regression_info.csv') meta_df = pd.concat([meta_c_df, meta_r_df]) meta_df['DIMENSIONALITY'] = meta_df.apply( lambda row: get_true_features(row['DATASET_ID']), axis=1) full_data = pd.merge(mu_df, meta_df, how='left') models = full_data['MODEL'].unique() row_size = max([len(x) for x in targets.values()]) base_colors = [ hsl2hex(c) for c in color_scale((0., 0.8, 0.6), (0.8, 0.8, 0.6), len(models)) ] lines = None for j, TYPE in enumerate(targets): for i, BASE in enumerate(targets[TYPE][1]): all_data = full_data.loc[full_data['TYPE'] == TYPE] all_data = all_data[[ 'MODEL', 'DATASET_ID', BASE, targets[TYPE][0] ]] all_data = all_data.groupby(['MODEL', 'DATASET_ID', BASE], as_index=False).mean() all_data = all_data.sort_values(BASE) plt.subplot(len(targets), row_size, row_size * j + i + 1) ylabel_str = targets[TYPE][0] if ylabel_str.lower() == 'mse': label_str = 'standardized negated mse' plt.xlabel(BASE.replace('_', ' ').capitalize()) plt.ylabel("{} {}".format( TYPE.replace('_', ' ').capitalize(), ylabel_str.replace('_', ' ').capitalize())) local_lines = [] for k, m in enumerate(models): ss = all_data.loc[all_data['MODEL'] == m] ss[BASE] = ss[BASE].rolling(int(len(ss[BASE]) / 2)).median() ss[targets[TYPE][0]] = ss[targets[TYPE][0]].rolling( int(len(ss[BASE]) / 2)).median() x = ss[BASE] y = ss[targets[TYPE][0]] line, = plt.plot(x, y, color=base_colors[k], alpha=0.7, label=m) local_lines.append(line) if lines == None: lines = local_lines plt.figlegend(lines, models, fancybox=True, framealpha=0.0) plt.gcf().suptitle('Dataset Dependent Performance Analysis') if not os.path.exists('figures'): os.makedirs('figures') plt.savefig('figures/DatasetPerformance.pdf', dpi=plt.gcf().dpi, transparent=True)
cyanExtruder = 2 magentaExtruder = 0 yellowExtruder = 1 def cambiacolor(color, v_ext=0): cyan, magenta, yellow = color result = "M163 S%s P%s\n" % (cyanExtruder, cyan) result += "M163 S%s P%s\n" % (magentaExtruder, magenta) result += "M163 S%s P%s\n" % (yellowExtruder, yellow) result += "M164 S%s\n" % v_ext result += "T%s" % v_ext return result while True: try: line = raw_input() except EOFError: break if ";LAYER_COUNT:" in line: layers = int(line[len(";LAYER_COUNT:"):]) scale = c.color_scale((0, 0, 1), (0, 1, 0), int(layers / 2)) + c.color_scale( (0, 1, 0), (1, 0, 0), int(layers / 2)) elif ";LAYER:" in line: print(line) print(cambiacolor(scale.pop(0))) else: print(line)