Beispiel #1
0
def run_test():
    ######################################################
    #
    # Runs a couple sanity checks on the network:
    #
    #    0      0
    #    |\    /|
    #    | 0--0 |
    #    |/    \|
    #    0      0
    #
    # with the obvious correct grouping followed
    # by a slightly non-optimal grouping.
    #
    # Results should be Q1 ~ .357, Q2 ~ .122
    #
    ######################################################

    network = create_test_network()

    print "A = ", network

    group1 = Grouping.create_from_vector([1, 1, 1, 0, 0, 0], network)
    print "S1 = ", group1

    group2 = Grouping.create_from_vector([1, 1, 1, 1, 0, 0], network)
    print "S2 = ", group2

    group3 = Grouping.create_from_vector([1, 1, 0, 1, 0, 0], network)
    print "S3 = ", group3

    Q1 = group1.get_modularity()
    Q2 = group2.get_modularity()
    Q3 = group3.get_modularity()

    print "Modularities:"

    print "Q_1 = ", Q1
    print "Q_2 = ", Q2
    print "Q_3 = ", Q3

    if ((Q1 - 0.357) < 0.001) and ((Q2 - 0.122) < 0.001):
        print "Test passed!"
    else:
        print "*** Test Failed! ***"
        return False

    print "Trying point patch method..."

    pointpatch = PointPatch(3, 0, 1)

    Q4 = pointpatch.get_modularity(group1, network)

    print "Q_4 = ", Q4

    print group1.get_nodes()
    print group1.get_groups()

    pointpatch.modify(group1)

    print group1.get_nodes()
    print group1.get_groups()

    Q5 = group1.calculate_modularity(network)

    print "Q_5 = ", Q5

    if Q5 - Q2 < 0.00001:
        print "Test passed!"
    else:
        print "*** Test Failed! ***"
        return False

    return True
Beispiel #2
0
def run_test():
    ######################################################
    #
    # Runs a couple sanity checks on the network:
    #
    #    0      0
    #    |\    /|
    #    | 0--0 |
    #    |/    \|
    #    0      0
    #
    # with the obvious correct grouping followed
    # by a slightly non-optimal grouping.
    #
    # Results should be Q1 ~ .357, Q2 ~ .122
    #
    ######################################################

    network = create_test_network()

    print "A = ", network

    group1 = Grouping.create_from_vector([1, 1, 1, 0, 0, 0], network)
    print "S1 = ", group1

    group2 = Grouping.create_from_vector([1, 1, 1, 1, 0, 0], network)
    print "S2 = ", group2

    group3 = Grouping.create_from_vector([1, 1, 0, 1, 0, 0], network)
    print "S3 = ", group3

    Q1 = group1.get_modularity()
    Q2 = group2.get_modularity()
    Q3 = group3.get_modularity()

    print "Modularities:"

    print "Q_1 = ", Q1
    print "Q_2 = ", Q2
    print "Q_3 = ", Q3

    if ((Q1 - .357) < .001) and ((Q2 - .122) < .001):
        print "Test passed!"
    else:
        print "*** Test Failed! ***"
        return False

    print "Trying point patch method..."

    pointpatch = PointPatch(3, 0, 1)

    Q4 = pointpatch.get_modularity(group1, network)

    print "Q_4 = ", Q4

    print group1.get_nodes()
    print group1.get_groups()

    pointpatch.modify(group1)

    print group1.get_nodes()
    print group1.get_groups()

    Q5 = group1.calculate_modularity(network)

    print "Q_5 = ", Q5

    if Q5 - Q2 < .00001:
        print "Test passed!"
    else:
        print "*** Test Failed! ***"
        return False

    return True
Beispiel #3
0
    def create_graphml_file(self, filename, grouping=None):
        """Creates a graphml file that can be used to generate a visualization
           of the network using the optional partition to color the nodes."""

        file = open(filename + ".graphml", "w")

        if grouping == None:
            grouping = Grouping.create_from_vector([0] * self.number_of_nodes)

        n = self.number_of_nodes
        m = self.number_of_edges

        nodes = grouping.get_nodes()
        non_empty_groups = grouping.get_non_empty_groups()
        c = len(non_empty_groups)
        A = self.adjacency_matrix

        print n, m, c, len(nodes), len(colors)

        # set palettes...
        if c <= len(colors):
            if c == 1:
                palette = ["#FFFFFF"]
            else:
                palette = graphml_colors
        else:
            palette = map(lambda x: "gray" + str(x), range(0, 100))

        # Preamble stuff...
        file.write(
            '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<graphml xmlns="http://graphml.graphdrawing.org/xmlns/graphml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/graphml http://www.yworks.com/xml/schema/graphml/1.0/ygraphml.xsd">\n<key for="node" id="d0" yfiles.type="nodegraphics"/>\n<key attr.name="description" attr.type="string" for="node" id="d1"/>\n<key for="edge" id="d2" yfiles.type="edgegraphics"/>\n<key attr.name="description" attr.type="string" for="edge" id="d3"/>\n<key for="graphml" id="d4" yfiles.type="resources"/>\n'
        )

        # Graph header

        file.write(
            """<graph edgedefault="directed" id="G" parse.edges="%d" parse.nodes="%d" parse.order="free">\n""" % (m, n)
        )

        # Now we write the nodes...
        for i in xrange(0, n):
            file.write(
                """<node id="n%d">
      <data key="d0">
        <y:ShapeNode>
          <y:Geometry height="30.0" width="30.0" x="80.56138057743618" y="0.0"/>
          <y:Fill color="%s" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="internal" modelPosition="c" textColor="#000000" visible="true" width="33.0" x="-1.5" y="5.6494140625">%d</y:NodeLabel>
          <y:Shape type="ellipse"/>
        </y:ShapeNode>
      </data>
      <data key="d1"/>
    </node>\n"""
                % (i, palette[non_empty_groups.index(nodes[i])], i)
            )

        current_edge = 0
        # Now we do the edges...
        for i in range(0, n):
            for j in range(0, i + 1):
                if A[i, j] == 1:
                    file.write(
                        """<edge id="e%d" source="n%d" target="n%d">
      <data key="d2">
        <y:PolyLineEdge>
          <y:Path sx="0.0" sy="0.0" tx="0.0" ty="0.0"/>
          <y:LineStyle color="#000000" type="line" width="1.0"/>
          <y:Arrows source="none" target="none"/>
          <y:BendStyle smoothed="false"/>
        </y:PolyLineEdge>
      </data>
      <data key="d3"/>
    </edge>\n"""
                        % (current_edge, i, j)
                    )
                    current_edge += 1

        # close off tags...
        file.write(
            """ </graph>
  <data key="d4">
    <y:Resources/>
  </data>
</graphml>"""
        )
        file.close()
Beispiel #4
0
    def create_graphml_file(self, filename, grouping=None):
        """Creates a graphml file that can be used to generate a visualization
           of the network using the optional partition to color the nodes."""

        file = open(filename + ".graphml", "w")

        if grouping == None:
            grouping = Grouping.create_from_vector([0] * self.number_of_nodes)

        n = self.number_of_nodes
        m = self.number_of_edges

        nodes = grouping.get_nodes()
        non_empty_groups = grouping.get_non_empty_groups()
        c = len(non_empty_groups)
        A = self.adjacency_matrix

        print n, m, c, len(nodes), len(colors)

        # set palettes...
        if c <= len(colors):
            if c == 1:
                palette = ["#FFFFFF"]
            else:
                palette = graphml_colors
        else:
            palette = map(lambda x: "gray" + str(x), range(0, 100))

        # Preamble stuff...
        file.write(
            '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<graphml xmlns="http://graphml.graphdrawing.org/xmlns/graphml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/graphml http://www.yworks.com/xml/schema/graphml/1.0/ygraphml.xsd">\n<key for="node" id="d0" yfiles.type="nodegraphics"/>\n<key attr.name="description" attr.type="string" for="node" id="d1"/>\n<key for="edge" id="d2" yfiles.type="edgegraphics"/>\n<key attr.name="description" attr.type="string" for="edge" id="d3"/>\n<key for="graphml" id="d4" yfiles.type="resources"/>\n'
        )

        # Graph header

        file.write(
            """<graph edgedefault="directed" id="G" parse.edges="%d" parse.nodes="%d" parse.order="free">\n"""
            % (m, n))

        # Now we write the nodes...
        for i in xrange(0, n):
            file.write("""<node id="n%d">
      <data key="d0">
        <y:ShapeNode>
          <y:Geometry height="30.0" width="30.0" x="80.56138057743618" y="0.0"/>
          <y:Fill color="%s" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="internal" modelPosition="c" textColor="#000000" visible="true" width="33.0" x="-1.5" y="5.6494140625">%d</y:NodeLabel>
          <y:Shape type="ellipse"/>
        </y:ShapeNode>
      </data>
      <data key="d1"/>
    </node>\n""" % (i, palette[non_empty_groups.index(nodes[i])], i))

        current_edge = 0
        # Now we do the edges...
        for i in range(0, n):
            for j in range(0, i + 1):
                if A[i, j] == 1:
                    file.write("""<edge id="e%d" source="n%d" target="n%d">
      <data key="d2">
        <y:PolyLineEdge>
          <y:Path sx="0.0" sy="0.0" tx="0.0" ty="0.0"/>
          <y:LineStyle color="#000000" type="line" width="1.0"/>
          <y:Arrows source="none" target="none"/>
          <y:BendStyle smoothed="false"/>
        </y:PolyLineEdge>
      </data>
      <data key="d3"/>
    </edge>\n""" % (current_edge, i, j))
                    current_edge += 1

        # close off tags...
        file.write(""" </graph>
  <data key="d4">
    <y:Resources/>
  </data>
</graphml>""")
        file.close()