The graph batch that connects the wrong molecules
A graph batch preserves feature slices and ownership pointers but loses cumulative node coordinates, corrupting later graphs and reordered duplicate selections.
What’s going wrong
ML practice. Historical teaching reduction of a repaired DeepChem graph-batching defect, independently written for Bugger. This standard-library exercise does not load DeepChem or train a model. The historical repair is not a currently open issue to resubmit.
Packing several molecular graphs produces plausible feature arrays and pointer tables, yet edges in later graphs refer to the wrong nodes. Selecting, unpacking and repacking graphs makes the error spread across coordinate systems. Trace the existing flow and repair the shared cause while preserving all features, graph boundaries and input data.
Input is {"graphs":[{"node_features":[1,2],"edges":[[0,1]],"edge_features":[5]},...],"select":[2,0,2]}. Each node has one integer feature. Each directed edge is a pair of zero-based local node indices, with its matching integer feature at the same position in edge_features. Self-loops and repeated edges are valid and keep their order. A graph may have isolated nodes, no edges, or no nodes and no edges. Selection entries identify source graph positions, and may repeat or appear in any order.
A packed batch has six fields. node_features concatenates graph node features in graph order. edges contains the original local edge pairs with both endpoints increased by the total number of nodes in all earlier graphs. edge_features concatenates features in the exact same graph and edge order. node_ptr and edge_ptr each begin with 0 and append the cumulative node or edge total after every graph, so each has graph_count+1 entries, including repeated pointer values for empty slices. node_graph contains the zero-based position of the owning graph for every node; empty graphs still occupy a graph position. An empty graph list therefore packs to empty feature, edge and ownership arrays with both pointers [0].
First pack all source graphs. Unpack each selected graph from that batch using its node_ptr and edge_ptr slices, subtracting its source node start from every edge endpoint to recover local indices. Preserve all node features, edge features and edge order. Return those graphs in select order. Then repack the selected list using the same batch contract, assigning each selected occurrence its own new graph position even when the same source graph is selected twice. Do not mutate source graph lists or reuse an earlier selected occurrence’s packed coordinates.
Print {"batch":{...},"selected_graphs":[...],"selected_batch":{...},"input_unchanged":true}. The provided driver checks source graph equality against a deep copy after processing; input_unchanged must remain true. An empty selection produces selected_graphs [] and an empty selected_batch, while batch still describes every source graph. The input has 0 to 8 graphs and 0 to 8 selection entries. Each graph has 0 to 8 nodes and 0 to 12 edges. All feature values are integers from -9 to 9; edge_features has exactly one entry per edge; endpoints are valid local node indices; every selection entry names an existing source graph. If graphs is empty, select is empty. Inputs satisfy these bounds.
Expected behavior
Preserve cumulative node coordinates, independent node and edge pointer totals, feature alignment and graph ownership through packing, local-coordinate unpacking and reordered duplicate repacking, without changing source graphs.
Reproduction
{"graphs":[{"node_features":[1,2],"edges":[[0,1]],"edge_features":[5]},{"node_features":[3],"edges":[[0,0]],"edge_features":[6]},{"node_features":[4,5],"edges":[[1,0]],"edge_features":[7]}],"select":[2,0,2]}{"batch":{"node_features":[1,2,3,4,5],"edges":[[0,1],[2,2],[4,3]],"edge_features":[5,6,7],"node_ptr":[0,2,3,5],"edge_ptr":[0,1,2,3],"node_graph":[0,0,1,2,2]},"selected_graphs":[{"node_features":[4,5],"edges":[[1,0]],"edge_features":[7]},{"node_features":[1,2],"edges":[[0,1]],"edge_features":[5]},{"node_features":[4,5],"edges":[[1,0]],"edge_features":[7]}],"selected_batch":{"node_features":[4,5,1,2,4,5],"edges":[[1,0],[2,3],[5,4]],"edge_features":[7,5,7],"node_ptr":[0,2,4,6],"edge_ptr":[0,1,2,3],"node_graph":[0,0,1,1,2,2]},"input_unchanged":true}{"graphs":[{"node_features":[1,2],"edges":[[0,1]],"edge_features":[5]},{"node_features":[3],"edges":[[0,0]],"edge_features":[6]}],"select":[1,0]}{"batch":{"node_features":[1,2,3],"edges":[[0,1],[2,2]],"edge_features":[5,6],"node_ptr":[0,2,3],"edge_ptr":[0,1,2],"node_graph":[0,0,1]},"selected_graphs":[{"node_features":[3],"edges":[[0,0]],"edge_features":[6]},{"node_features":[1,2],"edges":[[0,1]],"edge_features":[5]}],"selected_batch":{"node_features":[3,1,2],"edges":[[0,0],[1,2]],"edge_features":[6,5],"node_ptr":[0,1,3],"edge_ptr":[0,1,2],"node_graph":[0,1,1]},"input_unchanged":true}{"graphs":[{"node_features":[1,2],"edges":[[0,1]],"edge_features":[5]},{"node_features":[],"edges":[],"edge_features":[]},{"node_features":[3],"edges":[[0,0]],"edge_features":[6]}],"select":[2,1,0]}{"batch":{"node_features":[1,2,3],"edges":[[0,1],[2,2]],"edge_features":[5,6],"node_ptr":[0,2,2,3],"edge_ptr":[0,1,1,2],"node_graph":[0,0,2]},"selected_graphs":[{"node_features":[3],"edges":[[0,0]],"edge_features":[6]},{"node_features":[],"edges":[],"edge_features":[]},{"node_features":[1,2],"edges":[[0,1]],"edge_features":[5]}],"selected_batch":{"node_features":[3,1,2],"edges":[[0,0],[1,2]],"edge_features":[6,5],"node_ptr":[0,1,1,3],"edge_ptr":[0,1,1,2],"node_graph":[0,2,2]},"input_unchanged":true}Before you begin
- Directed graph edge lists
- Cumulative offsets and pointer arrays
- Local and global indices
- Ownership and round-trip invariants
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.