Aquí hay un ejemplo de trabajo mínimo para el problema al que me enfrento:
Estoy construyendo un gráfico simple con Networkx y luego lo estoy mostrando con Bokeh, agregando un control deslizante para mostrar solo los bordes cuyo peso es mayor que el valor del control deslizante. Desafortunadamente, esto funciona perfectamente cuando el valor aumenta, es decir, el control deslizante se mueve hacia la derecha, mientras que deja de funcionar (reaparecen algunos bordes, pero luego, al hacer clic en el gráfico, todo explota) cuando el valor del control deslizante disminuye. En la función de devolución de llamada customJS , estoy modificando los datos del borde, y también cuando imprimo en la consola cada parte, funcionan como se esperaba, pero en la consola del navegador aparece un error de discrepancia de forma, incluso si no se especifica qué dos formas son siendo comparado.
import pandas as pd import networkx as nx from bokeh.io import show from bokeh.plotting import figure, from_networkx from bokeh.models import CustomJS, Slider from bokeh.layouts import row, column import copy df = pd.DataFrame(data={'Source': {0: 'A', 1: 'A', 2: 'A', 3: 'B', 4: 'B', 5: 'C'}, 'Target': {0: 'B', 1: 'C', 2: 'D', 3: 'C', 4: 'D', 5: 'D'}, 'Weight': {0: 0, 1: 1, 2: 1.5, 3: 0.6, 4: 3, 5: 4}}) G = nx.from_pandas_edgelist(df, 'Source', 'Target', 'Weight') plot = figure(title='Attempt') network_graph = from_networkx(G, nx.circular_layout, scale=1, center=(0, 0)) plot.renderers.append(network_graph) # save edge data to select only a subset of the edges backup_edge_data = copy.deepcopy(network_graph.edge_renderer.data_source.data) slider = Slider(start=0, end=4, value=0, step=.2) # the last line of this object (the one with change.emit()) is probably unnecessary code = """ const old_Weight = edata["Weight"]; const old_start = edata["start"]; const old_end = edata["end"]; let acceptableIndexes = old_Weight.reduce(function(acc, curr, index) { if (curr >= cb_obj.value) { acc.push(index); } return acc; }, []); const new_Weight = acceptableIndexes.map(i => old_Weight[i]); const new_start = acceptableIndexes.map(i => old_start[i]); const new_end = acceptableIndexes.map(i => old_end[i]); const new_data_edge = {'Weight': new_Weight, 'start': new_start, 'end': new_end}; graph_setup.edge_renderer.data_source.data = new_data_edge; graph_setup.edge_renderer.data_source.change.emit(); """ callback = CustomJS(args = dict(graph_setup = network_graph, edata = backup_edge_data), code = code) slider.js_on_change('value', callback) layout = row( plot, column(slider), ) show(layout)Descubrí que esto en realidad se debe a un error : https://discourse.bokeh.org/t/dynamic-layout-behavior-changes- between-bokeh-2-2-3-and-bokeh-2-3- 0/7594
Para resolver esto hasta que la biblioteca bokeh no se actualice (estoy usando la versión 2.4.0), modifiqué el código customJS para agregar datos de marcador de posición para que coincidan con la dimensión inicial de la fuente de datos:
code = """ const old_Weight = edata["Weight"]; const old_start = edata["start"]; const old_end = edata["end"]; let acceptableIndexes = old_Weight.reduce(function(acc, curr, index) { if (curr >= cb_obj.value) { acc.push(index); } return acc; }, []); \\ compute how many fake edges have to be added const num_ph = old_Weight.length - acceptableIndexes.length \\ create an array of that dimension with fake value '9999' const placeholder = Array(num_ph).fill('9999') \\ for each new value, concatenate the new array with the placeholder array const new_Weight = acceptableIndexes.map(i => old_Weight[i]).concat(placeholder); const new_start = acceptableIndexes.map(i => old_start[i]).concat(placeholder); const new_end = acceptableIndexes.map(i => old_end[i]).concat(placeholder); const new_data_edge = {'Weight': new_Weight, 'start': new_start, 'end': new_end}; graph_setup.edge_renderer.data_source.data = new_data_edge; graph_setup.edge_renderer.data_source.change.emit(); """