How to use the spektral.utils.nx_to_numpy function in spektral

To help you get started, we’ve selected a few spektral examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dzambon / cdg / cdg / graph / conversion.py View on Github external
ef_preprocessing = kwargs.pop('ef_preprocessing')
    nf_postprocessing = kwargs.pop('nf_postprocessing')
    ef_postprocessing = kwargs.pop('ef_postprocessing')
    

    for g_nx in graphs:
        for v in g_nx.nodes:
            for i in range(len(nf_keys)):
                g_nx.node[v][nf_keys[i]] = nf_preprocessing[i](g_nx.node[v][nf_keys[i]])
        
        for e in g_nx.edges:
            for i in range(len(nf_keys)):
                g_nx.get_edge_data(e[0], e[1])[0][ef_keys[i]] = ef_preprocessing[i] \
                    (g_nx.get_edge_data(e[0], e[1])[0][ef_keys[i]])
    
    A, X, E = spektral.utils.nx_to_numpy(graphs, nf_keys=nf_keys, ef_keys=ef_keys, auto_pad=True,
                                      nf_postprocessing=nf_postprocessing, ef_postprocessing=ef_postprocessing)
    return A, X, E
github danielegrattarola / spektral / spektral / datasets / tud.py View on Github external
if clean:
        dataset_name += '_clean'
    if not os.path.exists(DATA_PATH + dataset_name):
        _download_data(dataset_name)

    # Read data
    nx_graphs, y = _read_graphs(dataset_name)

    # Preprocessing
    y = np.array(y)[..., None]
    y = OneHotEncoder(sparse=False, categories='auto').fit_transform(y)

    # Get node attributes
    try:
        A, X_attr, _ = nx_to_numpy(nx_graphs, nf_keys=['attributes'], auto_pad=False)
        X_attr = _normalize_node_features(X_attr, normalize_features)
    except KeyError:
        print('Featureless nodes')
        A, X_attr, _ = nx_to_numpy(nx_graphs, auto_pad=False)

    # Get clustering coefficients (always zscore norm)
    clustering_coefficients = [np.array(list(nx.clustering(g).values()))[..., None] for g in nx_graphs]
    clustering_coefficients = _normalize_node_features(clustering_coefficients, 'zscore')

    # Get node degrees
    node_degrees = np.array([np.sum(_, axis=-1, keepdims=True) for _ in A])
    node_degrees = _normalize_node_features(node_degrees, 'zscore')

    # Get node labels
    try:
        _, X_labs, _ = nx_to_numpy(nx_graphs, nf_keys=['label'], auto_pad=False)
github danielegrattarola / spektral / spektral / datasets / tud.py View on Github external
_download_data(dataset_name)

    # Read data
    nx_graphs, y = _read_graphs(dataset_name)

    # Preprocessing
    y = np.array(y)[..., None]
    y = OneHotEncoder(sparse=False, categories='auto').fit_transform(y)

    # Get node attributes
    try:
        A, X_attr, _ = nx_to_numpy(nx_graphs, nf_keys=['attributes'], auto_pad=False)
        X_attr = _normalize_node_features(X_attr, normalize_features)
    except KeyError:
        print('Featureless nodes')
        A, X_attr, _ = nx_to_numpy(nx_graphs, auto_pad=False)

    # Get clustering coefficients (always zscore norm)
    clustering_coefficients = [np.array(list(nx.clustering(g).values()))[..., None] for g in nx_graphs]
    clustering_coefficients = _normalize_node_features(clustering_coefficients, 'zscore')

    # Get node degrees
    node_degrees = np.array([np.sum(_, axis=-1, keepdims=True) for _ in A])
    node_degrees = _normalize_node_features(node_degrees, 'zscore')

    # Get node labels
    try:
        _, X_labs, _ = nx_to_numpy(nx_graphs, nf_keys=['label'], auto_pad=False)
        X_labs = _normalize_node_features(X_labs, 'ohe')
    except KeyError:
        print('Label-less nodes')
        X_labs = None
github danielegrattarola / spektral / spektral / datasets / qm9.py View on Github external
# Convert to Networkx
        data = [sdf_to_nx(_) for _ in data]

    if return_type is 'numpy':
        if nf_keys is not None:
            if isinstance(nf_keys, str):
                nf_keys = [nf_keys]
        else:
            nf_keys = NODE_FEATURES
        if ef_keys is not None:
            if isinstance(ef_keys, str):
                ef_keys = [ef_keys]
        else:
            ef_keys = EDGE_FEATURES

        adj, nf, ef = nx_to_numpy(data,
                                  auto_pad=auto_pad, self_loops=self_loops,
                                  nf_keys=nf_keys, ef_keys=ef_keys)
        return adj, nf, ef, labels
    elif return_type is 'networkx':
        return data, labels
    else:
        # Should not get here
        raise RuntimeError()