Ejemplo n.º 1
0
def create_nice_cx_from_networkx(G):
    """
    Create a NiceCXNetwork based on a networkx graph. The resulting NiceCXNetwork
    contains the nodes edges and their attributes from the networkx graph and also
    preserves the graph 'pos' attribute as a CX cartesian coordinates aspect.
    :param G: networkx graph
    :type G: networkx graph
    :return: NiceCXNetwork
    :rtype: NiceCXNetwork
    """
    if G is None:
        raise Exception('Networkx input is empty')

    my_nicecx = NiceCXNetwork()

    if G.graph.get('name'):
        my_nicecx.set_name(G.graph.get('name'))
    else:
        my_nicecx.set_name('created from networkx')

    my_nicecx.add_metadata_stub('networkAttributes')
    for n, d in G.nodes_iter(data=True):
        # =============
        # ADD NODES
        # =============
        if d and d.get('name'):
            my_nicecx.create_node(id=n,
                                  node_name=d.get('name'),
                                  node_represents=d.get('name'))
        else:
            my_nicecx.create_node(id=n, node_name=n, node_represents=n)

        # ======================
        # ADD NODE ATTRIBUTES
        # ======================
        for k, v in d.items():
            attr_type = None
            if isinstance(v, float):
                attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
            elif isinstance(v, int):
                attr_type = ATTRIBUTE_DATA_TYPE.INTEGER

            my_nicecx.set_node_attribute(n, k, v, type=attr_type)

    index = 0
    for u, v, d in G.edges_iter(data=True):
        # =============
        # ADD EDGES
        # =============
        my_nicecx.create_edge(id=index,
                              edge_source=u,
                              edge_target=v,
                              edge_interaction=d.get('interaction'))

        # ==============================
        # ADD EDGE ATTRIBUTES
        # ==============================
        for k, val in d.items():
            if k != 'interaction':
                attr_type = None
                if isinstance(val, float):
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif isinstance(val, int):
                    attr_type = ATTRIBUTE_DATA_TYPE.INTEGER
                my_nicecx.set_edge_attribute(index, k, val, type=attr_type)

        index += 1

    my_nicecx.add_metadata_stub('nodes')
    my_nicecx.add_metadata_stub('edges')
    if my_nicecx.nodeAttributes:
        my_nicecx.add_metadata_stub('nodeAttributes')
    if my_nicecx.edgeAttributes:
        my_nicecx.add_metadata_stub('edgeAttributes')

    if hasattr(G, 'pos'):
        aspect = _create_cartesian_coordinates_aspect_from_networkx(G)
        my_nicecx.add_opaque_aspect('cartesianLayout', aspect)
        my_nicecx.add_metadata_stub('cartesianLayout')

    return my_nicecx
Ejemplo n.º 2
0
def create_nice_cx_from_networkx(G):
    """
    Constructor that uses a networkx graph to build niceCX
    :param G: networkx graph
    :type G: networkx graph
    :return: none
    :rtype: none
    """
    my_nicecx = NiceCXNetwork()

    if G.graph.get('name'):
        my_nicecx.set_name(G.graph.get('name'))
    else:
        my_nicecx.set_name('created from networkx')

    my_nicecx.add_metadata_stub('networkAttributes')
    for n, d in G.nodes_iter(data=True):
        #=============
        # ADD NODES
        #=============
        if d and d.get('name'):
            my_nicecx.create_node(id=n,
                                  node_name=d.get('name'),
                                  node_represents=d.get('name'))
        else:
            my_nicecx.create_node(id=n, node_name=n, node_represents=n)

        #======================
        # ADD NODE ATTRIBUTES
        #======================
        for k, v in d.items():
            my_nicecx.set_node_attribute(n, k, v)

    index = 0
    for u, v, d in G.edges_iter(data=True):
        #=============
        # ADD EDGES
        #=============
        my_nicecx.create_edge(id=index,
                              edge_source=u,
                              edge_target=v,
                              edge_interaction=d.get('interaction'))

        #==============================
        # ADD EDGE ATTRIBUTES
        #==============================
        for k, v in d.items():
            if k != 'interaction':
                my_nicecx.set_edge_attribute(index, k, v)

        index += 1

    #Cartesian aspect
    cartesian_aspect = []
    #for

    my_nicecx.add_metadata_stub('nodes')
    my_nicecx.add_metadata_stub('edges')
    if my_nicecx.nodeAttributes:
        my_nicecx.add_metadata_stub('nodeAttributes')
    if my_nicecx.edgeAttributes:
        my_nicecx.add_metadata_stub('edgeAttributes')

    if hasattr(G, 'pos'):
        G_pos = create_cartesian_coordinates_aspect_from_networkx(G)

        my_nicecx.set_opaque_aspect('cartesianLayout',
                                    G_pos.get('cartesianLayout'))
        my_nicecx.add_metadata_stub('cartesianLayout')

    return my_nicecx
Ejemplo n.º 3
0
def create_nice_cx_from_pandas(df,
                               source_field=None,
                               target_field=None,
                               source_node_attr=[],
                               target_node_attr=[],
                               edge_attr=[],
                               edge_interaction=None):
    """
    Create a NiceCXNetwork from a pandas dataframe in which each row
    specifies one edge in the network.

    If only the df argument is provided the dataframe is treated as 'SIF' format,
    where the first two columns specify the source and target node ids of the edge
    and all other columns are ignored. The edge interaction is defaulted to "interacts-with"

    If both the source_field and target_field arguments are provided, the those and any other
    arguments refer to headers in the dataframe, controlling the mapping of columns to
    the attributes of nodes, and edges in the resulting NiceCXNetwork. If a header is not
    mapped the corresponding column is ignored. If the edge_interaction is not specified it
    defaults to "interacts-with"
    :param df: pandas dataframe to process
    :param source_field: header name specifying the name of the source node.
    :param target_field: header name specifying the name of the target node.
    :param source_node_attr: list of header names specifying attributes of the source node.
    :param target_node_attr: list of header names specifying attributes of the target node.
    :param edge_attr: list of header names specifying attributes of the edge.
    :param edge_interaction: the relationship between the source node and the target node, defaulting to "interacts-with"
    :return: NiceCXNetwork
    """

    my_nicecx = NiceCXNetwork()

    # ====================================================
    # IF NODE FIELD NAME (SOURCE AND TARGET) IS PROVIDED
    # THEN USE THOSE FIELDS OTHERWISE USE INDEX 0 & 1
    # ====================================================
    my_nicecx.set_name('Pandas Upload')
    my_nicecx.add_metadata_stub('networkAttributes')
    count = 0
    if source_field and target_field:
        for index, row in df.iterrows():
            if count % 10000 == 0:
                print(count)
            count += 1
            # =============
            # ADD NODES
            # =============
            my_nicecx.create_node(id=row[source_field],
                                  node_name=row[source_field],
                                  node_represents=row[source_field])
            my_nicecx.create_node(id=row[target_field],
                                  node_name=row[target_field],
                                  node_represents=row[target_field])

            # =============
            # ADD EDGES
            # =============
            if edge_interaction:
                if row.get(edge_interaction):
                    my_nicecx.create_edge(
                        id=index,
                        edge_source=row[source_field],
                        edge_target=row[target_field],
                        edge_interaction=row[edge_interaction])
                else:
                    my_nicecx.create_edge(id=index,
                                          edge_source=row[source_field],
                                          edge_target=row[target_field],
                                          edge_interaction=edge_interaction)
            else:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[source_field],
                                      edge_target=row[target_field],
                                      edge_interaction='interacts-with')

            # ==============================
            # ADD SOURCE NODE ATTRIBUTES
            # ==============================
            for sp in source_node_attr:
                attr_type = None
                if type(row[sp]) is float and math.isnan(row[sp]):
                    row[sp] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[sp]) is float and math.isinf(row[sp]):
                    row[sp] = 'Inf'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[sp]) is float:
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif isinstance(row[sp], int):
                    attr_type = ATTRIBUTE_DATA_TYPE.INTEGER
                my_nicecx.set_node_attribute(row[source_field],
                                             sp,
                                             row[sp],
                                             type=attr_type)

            # ==============================
            # ADD TARGET NODE ATTRIBUTES
            # ==============================
            for tp in target_node_attr:
                attr_type = None
                if type(row[tp]) is float and math.isnan(row[tp]):
                    row[tp] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[tp]) is float and math.isinf(row[tp]):
                    row[tp] = 'Inf'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[tp]) is float:
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif isinstance(row[tp], int):
                    attr_type = ATTRIBUTE_DATA_TYPE.INTEGER
                my_nicecx.set_node_attribute(row[target_field],
                                             tp,
                                             row[tp],
                                             type=attr_type)

            # ==============================
            # ADD EDGE ATTRIBUTES
            # ==============================
            for ep in edge_attr:
                attr_type = None
                if type(row[ep]) is float and math.isnan(row[ep]):
                    row[ep] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[ep]) is float and math.isinf(row[ep]):
                    row[ep] = 'INFINITY'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT

                my_nicecx.set_edge_attribute(index,
                                             ep,
                                             row[ep],
                                             type=attr_type)

    else:
        for index, row in df.iterrows():
            # =============
            # ADD NODES
            # =============
            my_nicecx.create_node(id=row[0],
                                  node_name=row[0],
                                  node_represents=row[0])
            my_nicecx.create_node(id=row[1],
                                  node_name=row[1],
                                  node_represents=row[1])

            # =============
            # ADD EDGES
            # =============
            if len(row) > 2:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[0],
                                      edge_target=row[1],
                                      edge_interaction=row[2])
            else:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[0],
                                      edge_target=row[1],
                                      edge_interaction='interacts-with')

    my_nicecx.add_metadata_stub('nodes')
    my_nicecx.add_metadata_stub('edges')
    if source_node_attr or target_node_attr:
        my_nicecx.add_metadata_stub('nodeAttributes')
    if edge_attr:
        my_nicecx.add_metadata_stub('edgeAttributes')

    return my_nicecx
Ejemplo n.º 4
0
def create_nice_cx_from_pandas(df,
                               source_field=None,
                               target_field=None,
                               source_node_attr=[],
                               target_node_attr=[],
                               edge_attr=[],
                               edge_interaction=None):
    """
    Constructor that uses a pandas dataframe to build niceCX
    :param df: dataframe
    :type df: Pandas Dataframe
    :param headers:
    :type headers:
    :return: none
    :rtype: n/a
    """

    my_nicecx = NiceCXNetwork()

    #====================================================
    # IF NODE FIELD NAME (SOURCE AND TARGET) IS PROVIDED
    # THEN USE THOSE FIELDS OTHERWISE USE INDEX 0 & 1
    #====================================================
    my_nicecx.set_name('Pandas Upload')
    my_nicecx.add_metadata_stub('networkAttributes')
    count = 0
    if source_field and target_field:
        for index, row in df.iterrows():
            if count % 10000 == 0:
                print(count)
            count += 1
            #=============
            # ADD NODES
            #=============
            my_nicecx.create_node(id=row[source_field],
                                  node_name=row[source_field],
                                  node_represents=row[source_field])
            my_nicecx.create_node(id=row[target_field],
                                  node_name=row[target_field],
                                  node_represents=row[target_field])

            #=============
            # ADD EDGES
            #=============
            if edge_interaction:
                if row.get(edge_interaction):
                    my_nicecx.create_edge(
                        id=index,
                        edge_source=row[source_field],
                        edge_target=row[target_field],
                        edge_interaction=row[edge_interaction])
                else:
                    my_nicecx.create_edge(id=index,
                                          edge_source=row[source_field],
                                          edge_target=row[target_field],
                                          edge_interaction=edge_interaction)
            else:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[source_field],
                                      edge_target=row[target_field],
                                      edge_interaction='neighbor-of')

            #==============================
            # ADD SOURCE NODE ATTRIBUTES
            #==============================
            for sp in source_node_attr:
                attr_type = None
                if type(row[sp]) is float and math.isnan(row[sp]):
                    row[sp] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[sp]) is float and math.isinf(row[sp]):
                    row[sp] = 'Inf'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                my_nicecx.set_node_attribute(row[source_field],
                                             sp,
                                             row[sp],
                                             type=attr_type)

            #==============================
            # ADD TARGET NODE ATTRIBUTES
            #==============================
            for tp in target_node_attr:
                attr_type = None
                if type(row[tp]) is float and math.isnan(row[tp]):
                    row[tp] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[tp]) is float and math.isinf(row[tp]):
                    row[tp] = 'Inf'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                my_nicecx.set_node_attribute(row[target_field],
                                             tp,
                                             row[tp],
                                             type=attr_type)

            #==============================
            # ADD EDGE ATTRIBUTES
            #==============================
            for ep in edge_attr:
                attr_type = None
                if type(row[ep]) is float and math.isnan(row[ep]):
                    row[ep] = ''
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT
                elif type(row[ep]) is float and math.isinf(row[ep]):
                    row[ep] = 'INFINITY'
                    attr_type = ATTRIBUTE_DATA_TYPE.FLOAT

                my_nicecx.set_edge_attribute(index,
                                             ep,
                                             row[ep],
                                             type=attr_type)

    else:
        for index, row in df.iterrows():
            #=============
            # ADD NODES
            #=============
            my_nicecx.create_node(id=row[0],
                                  node_name=row[0],
                                  node_represents=row[0])
            my_nicecx.create_node(id=row[1],
                                  node_name=row[1],
                                  node_represents=row[1])

            #=============
            # ADD EDGES
            #=============
            if len(row) > 2:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[0],
                                      edge_target=row[1],
                                      edge_interaction=row[2])
            else:
                my_nicecx.create_edge(id=index,
                                      edge_source=row[0],
                                      edge_target=row[1],
                                      edge_interaction='interacts-with')

    my_nicecx.add_metadata_stub('nodes')
    my_nicecx.add_metadata_stub('edges')
    if source_node_attr or target_node_attr:
        my_nicecx.add_metadata_stub('nodeAttributes')
    if edge_attr:
        my_nicecx.add_metadata_stub('edgeAttributes')

    return my_nicecx