def formatNetwork2(filePath): network = linkpred.read_network(filePath) G = nx.convert_node_labels_to_integers(network, 1, "default", "label") jsonNodeArray = [] jsonEdgeArray = [] for node in json.loads(nx.jit_data(G)): authorName = node['data']['label'] authorId = node['data']['id'] jsonNodeArray.append({ "id": int(authorId), "label": authorName }) if 'adjacencies' in node: edges = node['adjacencies'] for edge in edges: jsonEdgeArray.append({ "from": int(authorId), "to": int(edge['nodeTo']), "value": int(edge['data']['weight']), "edgeID": str(authorId)+'to'+str(edge['nodeTo']) }) jsonNetwork = { "nodes": jsonNodeArray, "edges": jsonEdgeArray, } return jsonNetwork
def processSDF(cur, sdfile, max_depth, with_properties, limit, verbosity, store_graphs, removeH, storeMolblock): nmol = 0 suppl = Chem.SDMolSupplier(sdfile, removeHs=removeH) for mol in suppl: if limit and nmol >= limit: break #print (json.dumps(mol, cls=MolAtomEncoder)) (molblock, sep, moldata) = suppl.GetItemText(nmol).partition('M END\n') nmol += 1 if storeMolblock: molid = addMol(cur, mol, molblock+sep, nmol, verbosity) else: molid = addMol(cur, mol, None, nmol, verbosity) for p in mol.GetPropNames(): addProp(cur, molid, p, mol.GetProp(p)) # MW provides a nice test of prediction at depth = 0 if not mol.HasProp("MW"): addProp(cur, molid, "MW", Descriptors.MolWt(mol)) previous_fragments = None mol_graph = mol_to_graph(mol, attr_fn=atom_type, smarts=True) if store_graphs else None #print (mol_graph.graph, mol_graph.nodes()) for depth in range(0, max_depth+1): (fragments, visited, graphs) = fragment_mol(mol, depth, with_properties, mol_graph) if verbosity > 1: if store_graphs: bfs_fragments = fragment_bfs(mol_graph, depth) # for f in bfs_fragments: # print (f.graph, f.nodes()) print ("compare graphs vs nx bfs fragemented graphs") print (mol_graph.graph, depth) for (a,b) in compare (graphs, bfs_fragments): if a.graph["atom"] != b.graph["atom"]: print (a.graph["atom"], b.graph["atom"]) for i in range(len(fragments)): if previous_fragments and fragments[i] == previous_fragments[i]: # atom has no neighbors past previous depth, e.g. water at depth > 0, CO at depth > 1, et. al. ff = None jit_graph = None cansmi = None nodes = None else: ff = fragments[i] cansmi = make_cansmarts(ff) nodes = str(visited[i]) if store_graphs: nodes = str(graphs[i].nodes()) #cansmi = Chem.MolToSmiles(Chem.MolFromSmarts(ff)) jit_graph = nx.jit_data(graphs[i]) cur.execute ("Insert Into atoms (molid, iteration, atomid, atom_index, cansmiles, nodes) Values (?,?,?,?,?,?)", (molid, depth, ff, i+1, cansmi, nodes)) if store_graphs: cur.execute ("Insert or Ignore Into graphs (iteration, atomid, jit_graph, cansmiles) Values (?,?,?,?)", (depth, ff, jit_graph, cansmi)) previous_fragments = fragments return nmol
def test_jit_round_trip(self): G = nx.Graph() d = nx.jit_data(G) H = jit_graph(json.loads(d)) K = jit_graph(d) assert nx.is_isomorphic(H, K)
See the JIT documentation and examples at https://philogb.github.io/jit/ """ import json import matplotlib.pyplot as plt import networkx as nx # add some nodes to a graph G = nx.Graph() G.add_node("one", type="normal") G.add_node("two", type="special") G.add_node("solo") # add edges G.add_edge("one", "two") G.add_edge("two", 3, type="extra special") # convert to JIT JSON jit_json = nx.jit_data(G, indent=4) print(jit_json) X = nx.jit_graph(json.loads(jit_json)) print(f"Nodes: {list(X.nodes(data=True))}") print(f"Edges: {list(X.edges(data=True))}") nx.draw(G, pos=nx.planar_layout(G), with_labels=True) plt.show()
def toJsonStr(graph): return nx.jit_data(removeConvenienceAttributes(graph.copy()))
def sauvJson(Graphe, fichier): fic = fichier[:-4] + "tab" L = nx.jit_data(Graphe) sortie = codecs.open(fic, 'w') sortie.write(L) sortie.close()
def serveLayout(): session_id = str(uuid.uuid4()) graph = addMinRequirements(generateDefaultGraph()) tracer = [ dropdown_model[dropdown_model_default]['id'], dropdown_model[dropdown_model_default]['visual_default']] return html.Div([ html.Div([ dbc.Modal( [ dbc.ModalHeader('Generate'), dbc.ModalBody([ dcc.Dropdown( id='modal-gen-dropdown', options=[{'label': y['name'], 'value': x} for x, y in graph_gens.items()], value=graph_gens_default, style={'left': '0px', 'right': '0px'} ), html.Div([], id='modal-gen-comp', style=designs.col) ]), dbc.ModalFooter([ dbc.Button('Close', id='modal-gen-close', className='ml-auto', style={'width': '10em'}), dbc.Button('Generate', id='modal-gen-generate', className='ml-auto', style={'width': '10em'}) ], style={'margin-left': 'auto', 'margin-right': '0'}), ], id='modal', ), dbc.Modal( [ dbc.ModalHeader('Information | Legal Notice'), dbc.ModalBody(info), dbc.ModalFooter([ dbc.Button('Close', id='modal-info-close', className='ml-auto', style={'width': '10em'}), ], style={'margin-left': 'auto', 'margin-right': '0'}), ], id='modal-info', size='xl' ), dbc.Modal( [ dbc.ModalHeader('Input'), dbc.ModalBody([ dcc.Textarea( id='model-input-textarea', value='', style={'width': '100%', 'height': 300}, ), ]), dbc.ModalFooter([ dbc.Button('Close', id='modal-input-close', className='ml-auto', style={'width': '10em'}), dbc.Button('Generate', id='modal-input-generate', className='ml-auto', style={'width': '10em'}) ], style={'margin-left': 'auto', 'margin-right': '0'}), ], id='modal-input' ), ]), dcc.Store(data=session_id, id='session-id'), dcc.Store(data=[], id='session-actions'), dcc.Store(data=tracer, id='session-tracer'), dcc.Store(data=json.loads(nx.jit_data(graph)), id='session-graph'), html.Div([dcc.Store(data=[], id=model['session-tracer']) for model in dropdown_model.values()]), html.Div([dcc.Store(data=[], id=model['session-actions']) for model in dropdown_model.values()]), html.Div(dcc.Store(data=[], id='session-graph-actions')), html.Div([ html.Div( dbc.DropdownMenu( [ dbc.DropdownMenuItem( 'Save', id='action-menu-save', className='m-1', style={'width': '200px'} ), dbc.DropdownMenuItem( 'Load', id='action-menu-load', className='m-1', style={'width': '200px'} ), dbc.DropdownMenuItem(divider=True), dbc.DropdownMenuItem( 'Sources', href='https://github.com/KonstantinRr', className='m-1', style={'width': '200px'} ), dbc.DropdownMenuItem( 'Author', href='https://github.com/KonstantinRr', className='m-1', style={'width': '200px'} ), dbc.DropdownMenuItem(divider=True), dbc.DropdownMenuItem( 'Generate', id='modal-gen-open', className='m-1', style={'width': '200px'} ), dbc.DropdownMenuItem( 'Custom', id='modal-input-open', className='m-1', style={'width': '200px'} ), dbc.DropdownMenuItem(divider=True), dbc.DropdownMenuItem( 'Info', id='modal-info-open', className='m-1', style={'width': '200px'} ), ], label='Menu', bs_size='lg', className='mb-3', ), style=designs.col ), html.Div([ dcc.Loading( id='loading-1', type='default', children=html.Div('', id='loader', style={'width': '80px', 'padding-top': '20px'}), ), ], style=designs.col), html.Div([html.Button('Add', id='action-add', style=designs.but)], style=designs.col), html.Div([html.Button('Delete', id='action-delete', style=designs.but)], style=designs.col), html.Div([html.Button('Connect', id='action-connect', style=designs.but)], style=designs.col), html.Div([html.Button('Deconnect', id='action-deconnect', style=designs.but)], style=designs.col), html.Div([ 'Layout', html.Div([dcc.Dropdown( id='dropdown-layout', options=[{'label': val['name'], 'value': key} for key, val in layouts.items()], value=layouts_default, style={'width': '300px'} )], style=designs.col), 'Model', html.Div([dcc.Dropdown( id='dropdown-model', options=[{'label': value['name'], 'value': key} for key, value in dropdown_model.items()], value=dropdown_model_default, style={'width': '300px'} )], style=designs.col), ], style=designs.row), ], style=designs.row), html.Div([model['ui']() for model in dropdown_model.values()], id='tab-specific'), dcc.Graph( id='basic-graph', figure=generateFigure(graph, dropdown_model_default, dropdown_model, tracer), style={'height' : '90vh', 'width' : '90vw', 'background-color': 'white'} ) ])
def main(): parser = parse_args() if len(sys.argv) == 1: parser.print_help() sys.exit() parsed = parser.parse_args() # input sdf sdfile = parsed.sdf # remove H removeH = not parsed.keepH # store molblocks storeMolblock = parsed.molblock # limit number of input molecules limit = parsed.limit # output sqlite file db = parsed.db #maximum depth of neighbor search max_depth = parsed.depth # which atom to include atom_properties in output smarts string. # n for none; r for root, t for tail, a for all, rt for root and tail with_properties = parsed.properties # verbose verbosity = parsed.verbosity # store graphs store_graphs = parsed.graphs # single smiles to process smi = parsed.test if smi and sdfile: print ("using single smiles in place of sdfile") if sdfile: con = sqlite3.connect(db) cur = con.cursor() makeTables(cur) nmols = processSDF(cur, sdfile, max_depth, with_properties, limit, verbosity, store_graphs, removeH, storeMolblock) if verbosity > 0: print ("%d molecules processed" % nmols) con.commit() elif smi: # test single smiles, prodide lots of output print (smi) mol = Chem.MolFromSmiles(smi) mol.SetProp("_Name", "atest") mol_graph = mol_to_graph(mol) #print (nx.jit_data(mol_graph, 2)) (fragments, visited, graphs) = fragment_mol(mol, max_depth, with_properties, mol_graph) # pick your poison show_results(fragments) if verbosity > 2: print ("\ncanonicalize") for ff in fragments: cansmi = make_cansmarts(ff) print (ff, cansmi) if verbosity > 3: show_results(list(set(fragments))) show_results(["[$(%s)]" % recursive_smarts for recursive_smarts in set(fragments)]) if verbosity > 1: # some tests print ("graphs") for g in graphs: print (max_depth, g.graph, g.nodes()) print (nx.jit_data(g)) # if not nx.is_tree(g): # print (max_depth, g.graph, g.nodes(), nx.cycle_basis(g)) print ("nx bfs fragmented graphs") bfs_fragments = fragment_bfs(mol_graph, max_depth) for f in bfs_fragments: print (f.graph, f.nodes()) print ("compare graphs vs nx bfs fragemented graphs") for (a,b) in compare (graphs, bfs_fragments): #print (a.graph["atom"], nx.is_tree(a), b.graph["atom"], nx.is_tree(b)) print (a.graph["atom"], b.graph["atom"]) if verbosity > 0: print () print ("identical framents") for f in set(fragments): same = [i for i, x in enumerate(fragments) if x == f] if len(same) > 1: print (same) if max_depth > 0: # doesn't handle depth 0, which has no graph edges print ("isomorphs") isomorphs = compare(graphs, graphs) for (a,b) in isomorphs: atoma = a.graph["atom"] atomb = b.graph["atom"] if (atoma != atomb): print (atoma, atomb, a.nodes[atoma]["attr"], b.nodes[atomb]["attr"], fragments[atoma], fragments[atomb]) else: parser.print_help()
def save(g: nx.DiGraph): with open(GRAPH_FILE, 'w') as f: f.write(nx.jit_data(g))
# plt.subplot(1,3,1) # plt.bar(range(NUM_NODES), srcs) # plt.subplot(1,3,2) # plt.bar(range(NUM_NODES), dsts) # plt.subplot(1,3,3) # plt.bar(range(NUM_NODES), np.array(srcs)+np.array(dsts)) # plt.show() # print np.sum((np.array(srcs) + np.array(dsts)) > 3) for seed in range(1000, 1020): myalgo = SimulatedAnnealing(env=env, reqs=rqs, num_extra_edges=NUM_EDGES, num_steps=100, temperature0=1000, cooling_factor=0.99, seed=seed, subsample=45, rnd_routing_order=False, outpath="data/") res_num_routed, res_env, res_sequence = myalgo.run() myalgo.visualize_topo(res_env, res_sequence, filename=PLT_OUT_PATH + "topo_%s" % seed, close=True) with open("data/topo_%s.json" % seed, "w") as fp: json.dump(nx.jit_data(res_env.topology), fp=fp) print seed, res_num_routed