<jupyter_start><jupyter_text>Load libraries.  In this case we need the ndex library, which you can get with "pip install ndex"<jupyter_code>import ndex2
import requests
import json
import base64<jupyter_output><empty_output><jupyter_text>Connect to NDEx in order to retrieve a network.Via `ndex2.create_nice_cx_from_server()`, we grab the [NCI Pathway Interaction Database - Final Revision - Extended Binary SIF](http://ndexbio.org//network/c0e70804-d848-11e6-86b1-0ac135e8bacf) network from NDEx, using its UUID.  The network is downloaded to create a [NiceCX](https://ndex2.readthedocs.io/en/latest/ndex2.htmlnicecxnetwork) object `nice_cx_net`.<jupyter_code>nice_cx_net = ndex2.create_nice_cx_from_server(server='http://public.ndexbio.org',
                                               uuid='c0e70804-d848-11e6-86b1-0ac135e8bacf')<jupyter_output><empty_output><jupyter_text>Select seed nodes for diffusionTo set seed nodes, add the node attribute `diffusion_input` with a value of `1.0`The code below grabs the first node, setting the `diffusion_input` node attribute.<jupyter_code>for nodeid, nodeobj in nice_cx_net.get_nodes():
    nice_cx_net.remove_node_attribute(node=nodeid, attribute_name='diffusion_input')
    nice_cx_net.set_node_attribute(type='double', node=nodeid, attribute_name='diffusion_input', values='1.0')
    print(nice_cx_net.get_node_attribute(nodeid,'diffusion_input'))
    # break out of loop so we only set the attribute on the first node
    break<jupyter_output>{'po': 3431, 'n': 'diffusion_input', 'v': '1.0', 'd': 'double'}<jupyter_text>Call the Diffusion serviceHere the diffusion service is called. This can take a few minutes to run.  NOTE: Diffusion uses the CXmate service, which requires all attributes to be passed as strings. So we convert the network to CX and stringify attributes<jupyter_code>url = 'http://v3.heat-diffusion.cytoscape.io'
payload = nice_cx_net.to_cx()
for p in payload:
    k = list(p.keys())[0]
    if 'Attributes' in k:
        for i in range(len(p[k])):
            p[k][i]['v'] = str(p[k][i]['v'])
response = requests.post(url, json=payload)<jupyter_output>Generating CX<jupyter_text>Add results from diffusion to our networkThe `data` dictionary in the json response `response.json()['data']` is a dictionary that contains [CX](https://home.ndexbio.org/data-model/) formatted data with results of diffussion. This next fragment of code grabs the results of diffusion<jupyter_code>for aspect in response.json()['data']:
    if 'nodeAttributes' not in aspect:
        continue
    for n_attr in aspect['nodeAttributes']:
        if n_attr['n'] == 'diffusion_output_rank' or n_attr['n'] == 'diffusion_output_heat':
            if n_attr['d'] == 'float':
                n_type = 'double'
            else:
                n_type = n_attr['d']
            nice_cx_net.add_node_attribute(property_of=int(n_attr['po']),
                                 name=n_attr['n'],
                                 values=n_attr['v'],
                                 type=n_type)<jupyter_output><empty_output><jupyter_text>Output best results from diffusionHere the code iterates through the nodes of the network looking for the `10` best hits (by examining `diffusion_output_rank`).<jupyter_code>nos = []
for node_id, node in nice_cx_net.get_nodes():
    rank = nice_cx_net.get_node_attribute_value(node_id, 'diffusion_output_rank')
    if int(rank) < 10:
        node['diffusion_output_rank'] = rank
        heat = nice_cx_net.get_node_attribute_value(node_id, 'diffusion_output_heat')
        node['diffusion_output_heat'] = float(heat)
        nos.append(node)

# sort the result
nos = sorted(nos, key=lambda k: k['diffusion_output_rank'])       
nos<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10867237687366167, 'fraction_numerical': 0.016862955032119913, 'mean_word_length': 4.0843537414965985, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '433897', 'license': 'MIT', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1200, 'n_words': 305, 'path': 'demo.ipynb', 'repo_name': 'idekerlab/heat-diffusion', 'size': '3736'}