def ver_estadisticas_gsom(n_clicks, data_portion_option): zero_unit = session_data.get_modelo() gsom = zero_unit.child_map data = session_data.get_data(data_portion_option) gsom.replace_parent_dataset(data) qe, mqe = gsom.get_map_qe_and_mqe() qe = round(qe, 4) mqe = round(mqe, 4) #Table table_header = [ html.Thead(html.Tr([html.Th("Magnitude"), html.Th("Value")])) ] row0 = html.Tr([html.Td("Quantization Error (Total)"), html.Td(qe)]) row1 = html.Tr([html.Td("Mean Quantization Error"), html.Td(mqe)]) table_body = [html.Tbody([row0, row1])] table = dbc.Table(table_header + table_body, bordered=True, dark=False, hover=True, responsive=True, striped=True) children = [table] return children
def update_mapa_componentes_gsom_fig(n_cliks, slider_value, names, check_annotations, log_scale): ctx = dash.callback_context trigger_id = ctx.triggered[0]["prop_id"].split(".")[0] if (n_cliks == 0 and trigger_id == 'min_hits_slider_gsom'): raise PreventUpdate elif (trigger_id == 'min_hits_slider_gsom' and (names is None or len(names) == 0)): return dash.no_update #params = session_data.get_gsom_model_info_dict() zero_unit = session_data.get_modelo() gsom = zero_unit.child_map tam_eje_vertical, tam_eje_horizontal = gsom.map_shape() weights_map = gsom.get_weights_map() # weights_map[(row,col)] = np vector whith shape=n_feauters, dtype=np.float32 nombres_atributos = session_data.get_features_names() lista_de_indices = [] for n in names: lista_de_indices.append(nombres_atributos.index(n)) traces = [] for k in lista_de_indices: data_to_plot = np.empty([tam_eje_vertical, tam_eje_horizontal], dtype=np.float64) for i in range(tam_eje_vertical): for j in range(tam_eje_horizontal): data_to_plot[i][j] = weights_map[(i, j)][k] if (slider_value != 0 and session_data.get_freq_hitrate_mask() is not None): data_to_plot = ma.masked_array( data_to_plot, mask=session_data.get_freq_hitrate_mask()).filled(np.nan) id = 'graph-{}'.format(k) figure, _ = pu.create_heatmap_figure(data_to_plot, tam_eje_horizontal, tam_eje_vertical, check_annotations, title=nombres_atributos[k], log_scale=log_scale) children = pu.get_fig_div_with_info(figure, id, '', None, None, gsom_level=None, neurona_padre=None) traces.append(html.Div(children=children)) return traces
def update_freq_map_gsom(click, logscale, slider_value, data_portion_option): data = session_data.get_data(data_portion_option) zero_unit = session_data.get_modelo() gsom = zero_unit.child_map tam_eje_vertical, tam_eje_horizontal = gsom.map_shape() pre_calc_freq = session_data.get_calculated_freq_map() if (pre_calc_freq is None or (pre_calc_freq is not None and pre_calc_freq[1] != data_portion_option)): #recalculate freq map data_to_plot = np.zeros([tam_eje_vertical, tam_eje_horizontal], dtype=int) # Getting winnig neurons for each data element for i, d in enumerate(data): winner_neuron = gsom.winner_neuron(d)[0][0] r, c = winner_neuron.position data_to_plot[r][c] = data_to_plot[r][c] + 1 session_data.set_calculated_freq_map(data_to_plot, data_portion_option) else: #load last calculated map data_to_plot, _, _ = pre_calc_freq max_freq = np.nanmax(data_to_plot) if (max_freq > 0): marks = {0: '0 hits', int(max_freq): '{} hits'.format(int(max_freq))} else: marks = dash.no_update if (slider_value != 0): #filter minimum hit rate per neuron #frequencies = np.where(frequencies< slider_value,np.nan,frequencies) data_to_plot = ma.masked_less(data_to_plot, slider_value) session_data.set_freq_hitrate_mask(ma.getmask(data_to_plot)) else: session_data.set_freq_hitrate_mask(None) fig, _ = pu.create_heatmap_figure(data_to_plot, tam_eje_horizontal, tam_eje_vertical, True, title=None, log_scale=logscale) children = pu.get_fig_div_with_info(fig, 'freq_map_gsom', 'Frequency Map', tam_eje_horizontal, tam_eje_vertical) return children, max_freq, marks
def save_ghsom_model(n_clicks, name, isvalid): if (not isvalid): return '' data = [] params = session_data.get_ghsom_model_info_dict() columns_dtypes = session_data.get_features_dtypes() data.append('ghsom') data.append(columns_dtypes) data.append(params) data.append(session_data.get_modelo()) filename = name + '_ghsom.pickle' with open(DIR_SAVED_MODELS + filename, 'wb') as handle: pickle.dump(data, handle, protocol=pickle.HIGHEST_PROTOCOL) return 'Model saved! Filename: ' + filename
def get_ghsom_fig(data, target_list): zero_unit = session_data.get_modelo() grafo = nx.Graph() g = zero_unit.child_map.get_structure_graph(grafo, data, target_list, level=0) session_data.set_ghsom_structure_graph(g) edge_x = [] edge_y = [] nodes_dict = {} counter = 0 for node in g.nodes: nodes_dict[node] = counter counter += 1 for edge in g.edges: nodo1, nodo2 = edge a = nodes_dict[nodo1] b = nodes_dict[nodo2] edge_x.append(a) edge_y.append(-g.nodes[nodo1]['nivel']) edge_x.append(b) edge_y.append(-g.nodes[nodo2]['nivel']) #Fix to plot segments edge_x.append(None) edge_y.append(None) #TODO quicker scattergl #edge_trace = go.Scattergl( edge_trace = go.Scatter( x=edge_x, y=edge_y, #z=edge_z, line=dict(width=0.5, color='#888'), hoverinfo='none', mode='lines') node_x = [] node_y = [] hover_text = [] nodes_by_cords_dict = {} for node in g.nodes: x_cord = nodes_dict[node] y_cord = -g.nodes[node]['nivel'] node_x.append(x_cord) node_y.append(y_cord) nodes_by_cords_dict[(y_cord, x_cord)] = node #Coordenadas de la neurona padre if ('neurona_padre_pos' in g.nodes[node]): cord_ver, cord_hor = g.nodes[node]['neurona_padre_pos'] string = '(' + str(cord_hor) + ',' + str(cord_ver) + ')' hover_text.append(string) else: hover_text.append('') session_data.set_ghsom_nodes_by_coord_dict(nodes_by_cords_dict) node_trace = go.Scatter( #TODO quicker scattergl #TODO Scattergl no muestra el hovertext #node_trace = go.Scattergl( x=node_x, y=node_y, mode='markers', text=hover_text, hovertemplate='Neuron Parent Coordinates: <b>%{text}</b><br>' + 'Level: %{y}<br>' + "<extra></extra>", marker=dict( #color=['blue',], #set color equal to a variable color='slategray', size=14)) data1 = [edge_trace, node_trace] layout = go.Layout( title="Complete GHSOM Structure", titlefont_size=16, showlegend=False, hovermode='closest', margin=dict(b=20, l=5, r=5, t=40), xaxis=dict(showgrid=False, zeroline=False, showticklabels=False), yaxis=dict(showgrid=False, zeroline=False, showticklabels=False), ) fig = dict(data=data1, layout=layout) return fig
def ver_umatrix_gsom_fig(click, check_annotations, log_scale): #print('Button clicked, calculating umatrix') #params = session_data.get_gsom_model_info_dict() zero_unit = session_data.get_modelo() gsom = zero_unit.child_map tam_eje_vertical, tam_eje_horizontal = gsom.map_shape() weights_map = gsom.get_weights_map() # weights_map[(row,col)] = np vector whith shape=n_feauters, dtype=np.float32 data_to_plot = np.empty([tam_eje_vertical, tam_eje_horizontal], dtype=np.float64) saved_distances = {} #for saving distances # saved_distances[i,j,a,b] with (i,j) and (a,b) neuron cords ''' debugg for i in range(tam_eje_vertical): for j in range(tam_eje_horizontal): print('pesos[][]: ',i, j, '---: ',weights_map[(i,j)]) print('eje x e y: ',tam_eje_vertical,tam_eje_horizontal) ''' for i in range(tam_eje_vertical): for j in range(tam_eje_horizontal): neuron_neighbords = [] if (j - 1 >= 0): #bottom neighbor neuron_neighbords.append( get_distances(weights_map, saved_distances, i, j, i, j - 1)) if (j + 1 < tam_eje_horizontal): #top neighbor neuron_neighbords.append( get_distances(weights_map, saved_distances, i, j, i, j + 1)) if (i - 1 >= 0): # #left neighbor neuron_neighbords.append( get_distances(weights_map, saved_distances, i, j, i - 1, j)) if (i + 1 < tam_eje_vertical): #right neighbor neuron_neighbords.append( get_distances(weights_map, saved_distances, i, j, i + 1, j)) if (any(neuron_neighbords)): data_to_plot[i][j] = sum(neuron_neighbords) / len( neuron_neighbords) ''' debug print('distancias' ) for item in saved_distances.items(): print(item) ''' fig, _ = pu.create_heatmap_figure(data_to_plot, tam_eje_horizontal, tam_eje_vertical, check_annotations, title=None, colorscale=UMATRIX_HEATMAP_COLORSCALE, reversescale=True, log_scale=log_scale) children = pu.get_fig_div_with_info(fig, 'umatrix_fig_gsom', 'U-Matrix', tam_eje_horizontal, tam_eje_vertical) return children
def update_winner_map_gsom(click, check_annotations, logscale, data_portion_option): output_alert_too_categorical_targets_gsom = False log_scale = False data = session_data.get_data(data_portion_option) targets_list = session_data.get_targets_list(data_portion_option) zero_unit = session_data.get_modelo() gsom = zero_unit.child_map tam_eje_vertical, tam_eje_horizontal = gsom.map_shape() #visualizacion data_to_plot = np.empty([tam_eje_vertical, tam_eje_horizontal], dtype=object) positions = {} # Getting winnig neurons for each data element for i, d in enumerate(data): winner_neuron = gsom.winner_neuron(d)[0][0] r, c = winner_neuron.position if ((r, c) in positions): positions[(r, c)].append(targets_list[i]) else: positions[(r, c)] = [] positions[(r, c)].append(targets_list[i]) target_type, unique_targets = session_data.get_selected_target_type( data_portion_option) values = None text = None if (target_type == 'numerical' ): #numerical data: mean of the mapped values in each neuron data_to_plot = np.empty([tam_eje_vertical, tam_eje_horizontal], dtype=np.float64) #labeled heatmap does not support nonetypes data_to_plot[:] = np.nan log_scale = logscale for i in range(tam_eje_vertical): for j in range(tam_eje_horizontal): if ((i, j) in positions): data_to_plot[i][j] = np.mean(positions[(i, j)]) else: data_to_plot[i][j] = np.nan elif (target_type == 'string'): data_to_plot = np.empty([tam_eje_vertical, tam_eje_horizontal], dtype=np.float64) #labeled heatmap does not support nonetypes data_to_plot[:] = np.nan text = np.empty([tam_eje_vertical, tam_eje_horizontal], dtype=object) #labeled heatmap does not support nonetypes text[:] = np.nan values = np.linspace(0, 1, len(unique_targets), endpoint=False).tolist() targets_codification = dict(zip(unique_targets, values)) if (len(unique_targets) >= 270): output_alert_too_categorical_targets = True #showing the class more represented in each neuron for i in range(tam_eje_vertical): for j in range(tam_eje_horizontal): if ((i, j) in positions): c = Counter(positions[(i, j)]) #data_to_plot[i][j] = c.most_common(1)[0][0] max_target = c.most_common(1)[0][0] data_to_plot[i][j] = targets_codification[max_target] text[i][j] = max_target else: data_to_plot[i][j] = np.nan else: #error raiseExceptions('Unexpected error') data_to_plot = np.empty([1, 1], dtype=np.bool_) #labeled heatmap does not support nonetypes data_to_plot[:] = np.nan fig, table_legend = pu.create_heatmap_figure(data_to_plot, tam_eje_horizontal, tam_eje_vertical, check_annotations, text=text, discrete_values_range=values, unique_targets=unique_targets, log_scale=log_scale) if (table_legend is not None): children = pu.get_fig_div_with_info(fig, 'winners_map_gsom', 'Winning Neuron Map', tam_eje_horizontal, tam_eje_vertical, gsom_level=None, neurona_padre=None, table_legend=table_legend) else: children = pu.get_fig_div_with_info(fig, 'winners_map_gsom', 'Winning Neuron Map', tam_eje_horizontal, tam_eje_vertical, gsom_level=None, neurona_padre=None) print('\n GSOM Winning Neuron Map: Plotling complete! \n') return children, output_alert_too_categorical_targets_gsom