Пример #1
0
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
Пример #2
0
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 update_freq_map_ghsom(clickdata, logscale, slider_value, figure,
                          data_portion_option):

    if clickdata is None:
        raise PreventUpdate

    slider_update = dash.no_update
    points = clickdata['points']
    punto_clickeado = points[0]
    cord_horizontal_punto_clickeado = punto_clickeado['x']
    cord_vertical_punto_clickeado = punto_clickeado['y']

    #Actualizar  COLOR DEL punto seleccionado en el grafo
    data_g = []
    data_g.append(figure['data'][0])
    data_g.append(figure['data'][1])

    data_g.append(
        go.Scattergl(x=[cord_horizontal_punto_clickeado],
                     y=[cord_vertical_punto_clickeado],
                     mode='markers',
                     marker=dict(color='blue', size=14)))
    figure['data'] = data_g

    #Mapa Freq  del gsom seleccionado en el grafo
    nodes_dict = session_data.get_ghsom_nodes_by_coord_dict()
    coords_nodes_dict = (cord_vertical_punto_clickeado,
                         cord_horizontal_punto_clickeado)
    gsom = nodes_dict[coords_nodes_dict]
    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) or
         (pre_calc_freq[2] != coords_nodes_dict))):  #recalculate freq map

        g = session_data.get_ghsom_structure_graph()
        neurons_mapped_targets = g.nodes[gsom]['neurons_mapped_targets']
        data_to_plot = np.zeros([tam_eje_vertical, tam_eje_horizontal],
                                dtype=int)

        for i in range(tam_eje_vertical):
            for j in range(tam_eje_horizontal):
                if ((i, j) in neurons_mapped_targets):
                    c = len(neurons_mapped_targets[(i, j)])
                    data_to_plot[i][j] = c
                else:
                    data_to_plot[i][j] = 0

        session_data.set_calculated_freq_map(data_to_plot, data_portion_option,
                                             coords_nodes_dict)

        slider_update = 0

    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

    ctx = dash.callback_context
    trigger_id = ctx.triggered[0]["prop_id"].split(".")[0]

    if (slider_value != 0 and trigger_id
            == 'min_hits_slider_ghsom'):  #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_ghsom', 'Frequency Map',
                                        tam_eje_horizontal, tam_eje_vertical)

    return children, figure, max_freq, marks, slider_update
def view_winner_map_by_selected_point(clickdata, check_annotations, logscale,
                                      figure, data_portion_option):

    if clickdata is None:
        raise PreventUpdate

    output_alert_too_categorical_targets_ghsom = False
    log_scale = False

    #print('clikedpoint:',clickdata)
    #{'points': [{'curveNumber': 0, 'x': 0, 'y': 0, 'z': 0}]}
    points = clickdata['points']
    punto_clickeado = points[0]
    cord_horizontal_punto_clickeado = punto_clickeado['x']
    cord_vertical_punto_clickeado = punto_clickeado['y']

    #Actualizar  COLOR DEL punto seleccionado en el grafo
    data_g = []
    data_g.append(figure['data'][0])
    data_g.append(figure['data'][1])

    data_g.append(
        go.Scattergl(x=[cord_horizontal_punto_clickeado],
                     y=[cord_vertical_punto_clickeado],
                     mode='markers',
                     marker=dict(color='blue', size=14)))
    figure['data'] = data_g

    #Mapa de neuronas ganadoras del gsom seleccionado en el grafo
    nodes_dict = session_data.get_ghsom_nodes_by_coord_dict()
    ghsom = nodes_dict[(cord_vertical_punto_clickeado,
                        cord_horizontal_punto_clickeado)]
    tam_eje_vertical, tam_eje_horizontal = ghsom.map_shape()

    g = session_data.get_ghsom_structure_graph()
    neurons_mapped_targets = g.nodes[ghsom]['neurons_mapped_targets']
    level = g.nodes[ghsom]['nivel']
    data_to_plot = np.empty([tam_eje_vertical, tam_eje_horizontal],
                            dtype=object)
    neurona_padre_string = None
    if ('neurona_padre_pos' in g.nodes[ghsom]):
        cord_ver, cord_hor = g.nodes[ghsom]['neurona_padre_pos']
        neurona_padre_string = '(' + str(cord_hor) + ',' + str(cord_ver) + ')'

    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 neurons_mapped_targets):
                    data_to_plot[i][j] = np.mean(neurons_mapped_targets[(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_ghsom = 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 neurons_mapped_targets):
                    c = Counter(neurons_mapped_targets[(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,
                                                 title=None,
                                                 log_scale=log_scale)

    if (table_legend is not None):
        children = pu.get_fig_div_with_info(fig,
                                            'winnersmap_fig_ghsom',
                                            'Winners Target per Neuron Map',
                                            tam_eje_horizontal,
                                            tam_eje_vertical,
                                            level,
                                            neurona_padre_string,
                                            table_legend=table_legend)
    else:
        children = pu.get_fig_div_with_info(fig, 'winnersmap_fig_ghsom',
                                            'Winners Target per Neuron Map',
                                            tam_eje_horizontal,
                                            tam_eje_vertical, level,
                                            neurona_padre_string)

    print('\nGHSOM Winners Map Render finished\n')
    return children, figure, output_alert_too_categorical_targets_ghsom
def ver_umatrix_ghsom_fig(clickdata, check_annotations, log_scale, fig_grafo):

    if (clickdata is None):
        raise PreventUpdate

    points = clickdata['points']
    punto_clickeado = points[0]
    cord_horizontal_punto_clickeado = punto_clickeado['x']
    cord_vertical_punto_clickeado = punto_clickeado['y']

    #Actualizar  COLOR DEL punto seleccionado en el grafo
    data_g = []
    data_g.append(fig_grafo['data'][0])
    data_g.append(fig_grafo['data'][1])

    data_g.append(
        go.Scattergl(x=[cord_horizontal_punto_clickeado],
                     y=[cord_vertical_punto_clickeado],
                     mode='markers',
                     marker=dict(color='blue', size=14)))
    fig_grafo['data'] = data_g

    #umatrix del gsom seleccionado en el grafo
    nodes_dict = session_data.get_ghsom_nodes_by_coord_dict()
    gsom = nodes_dict[(cord_vertical_punto_clickeado,
                       cord_horizontal_punto_clickeado)]
    tam_eje_vertical, tam_eje_horizontal = gsom.map_shape()
    weights_map = gsom.get_weights_map()
    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

    #TODO borrar:
    '''
    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)

    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_ghsom', 'U-Matrix',
                                        tam_eje_horizontal, tam_eje_vertical)
    print('\n GHSOM UMatrix: Plotling complete! \n')

    return children, fig_grafo
def update_mapa_componentes_ghsom_fig(input_freq_map, check_annotations,
                                      log_scale, slider_value, names,
                                      fig_grafo, clickdata):

    if (clickdata is None or names is None or len(names) == 0):
        raise PreventUpdate

    #{'points': [{'curveNumber': 0, 'x': 0, 'y': 0, 'z': 0}]}
    points = clickdata['points']
    punto_clickeado = points[0]
    cord_horizontal_punto_clickeado = punto_clickeado['x']
    cord_vertical_punto_clickeado = punto_clickeado['y']

    #Actualizar  COLOR DEL punto seleccionado en el grafo
    data_g = []
    data_g.append(fig_grafo['data'][0])
    data_g.append(fig_grafo['data'][1])

    data_g.append(
        go.Scattergl(x=[cord_horizontal_punto_clickeado],
                     y=[cord_vertical_punto_clickeado],
                     mode='markers',
                     marker=dict(color='blue', size=14)))
    fig_grafo['data'] = data_g

    #Mapa de componentes del gsom seleccionado en el grafo
    nodes_dict = session_data.get_ghsom_nodes_by_coord_dict()
    gsom = nodes_dict[(cord_vertical_punto_clickeado,
                       cord_horizontal_punto_clickeado)]
    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)

        figure, _ = pu.create_heatmap_figure(data_to_plot,
                                             tam_eje_horizontal,
                                             tam_eje_vertical,
                                             check_annotations,
                                             title=nombres_atributos[k],
                                             log_scale=log_scale)
        id = 'graph-{}'.format(k)
        traces.append(html.Div(children=dcc.Graph(id=id, figure=figure)))

    #return traces, fig_grafo
    return traces
Пример #7
0
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
Пример #8
0
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