Beispiel #1
0
 def test_minimal_startnodes_for_node(self):
     spsg = sparse_powerset_graph(self.computers)
     targetVars = frozenset({A, B})
     fig1 = plt.figure(figsize=(15, 30))
     axs = fig1.subplots(2, 1)
     draw_ComputerSetMultiDiGraph_matplotlib(
         axs[1],
         spsg,
         targetNode=frozenset({B})
     )
     draw_ComputerSetMultiDiGraph_matplotlib(
         axs[0],
         spsg,
         targetNode=frozenset({A})
     )
     fig1.savefig("spsg.pdf")
     plt.close(fig1)
     res = minimal_startnodes_for_node(spsg, targetVars)
     # print('###############################') print('miminal startsets
     # for: ', node_2_string(targetVars),nodes_2_string(res))
     # print('###############################')
     self.assertSetEqual(
         res,
         frozenset({
             frozenset({I, E, F}),
             frozenset({I, C, D}),
             frozenset({I, H, C, G}),
             frozenset({I, H, C, D, G}),
             frozenset({I, E, H, F, G})
         })
     )
Beispiel #2
0
    def test_arg_set_graph(self):
        asg = arg_set_graph(D, self.computers)
        # For compatibility arg_set_graph returns a multigraph
        # although we do not have more than one edge between a pair
        # of nodes.

        ref = nx.MultiDiGraph()
        ref.add_edge(
            frozenset({B}),
            frozenset({D}),
            computers=frozenset({d_from_b})
        )
        ref.add_edge(
            frozenset({G, H}),
            frozenset({D}),
            computers=frozenset({d_from_g_h})
        )

        # picture for manual check
        fig = plt.figure(figsize=(20, 20))
        ax1 = fig.add_subplot(1, 2, 1)
        ax2 = fig.add_subplot(1, 2, 2)
        draw_ComputerSetMultiDiGraph_matplotlib(ax1, ref)
        draw_ComputerSetMultiDiGraph_matplotlib(ax2, asg)
        fig.savefig("arg_set_graph.pdf")

        self.assertTrue(equivalent_multigraphs(asg, ref))
Beispiel #3
0
 def test_computable_mvars(self):
     # for debugging we draw the sparse_powerset_graph of the actually present computers and mvars
     spsg = sparse_powerset_graph(bgc_md2_computers())
     f = plt.figure()
     ax = f.add_subplot(1, 1, 1)
     draw_ComputerSetMultiDiGraph_matplotlib(ax,
                                             spsg,
                                             bgc_md2_mvar_aliases(),
                                             bgc_md2_computer_aliases(),
                                             targetNode=frozenset(
                                                 {SmoothModelRun}))
     f.savefig("spgs.pdf")
     fig = plt.figure()
     draw_update_sequence(bgc_md2_computers(), max_it=8, fig=fig)
     fig.savefig("c1.pdf")
     # https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests
     for mn in [
             "Potter1993GlobalBiogeochemicalCycles",
             "testVectorFree",
             "Williams2005GCB",  # just uncomment the one model you are working on and comment the others
     ]:
         with self.subTest(mn=mn):
             mvs = MVarSet.from_model_name(mn)
             mvars = mvs.computable_mvar_types()
             list_str = "\n".join(
                 ["<li> " + str(var.__name__) + " </li>" for var in mvars])
             print(list_str)
             for var in mvars:
                 print("########################################")
                 print(str(var.__name__))
                 print(mvs._get_single_mvar_value(var))
Beispiel #4
0
 def test_ComputerSetMultiGraph_matplotlib(self):
     # The direct visualization of networkx (using matplotlib)
     # is very rudimentary.
     fig = plt.figure(figsize=(20, 20))
     ax = fig.add_subplot(1, 1, 1)
     draw_ComputerSetMultiDiGraph_matplotlib(ax, self.spsg)
     fig.savefig("SetMultiGraph.pdf")
Beispiel #5
0
 def test_ComputerSetMultiGraph_matplotlib_bgc_md2(self):
     # The direct visualization of networkx (using matplotlib)
     # is very rudimentary.
     spsg = sparse_powerset_graph(bgc_md2_computers())
     fig = plt.figure(figsize=(20, 20))
     ax = fig.add_subplot(1, 1, 1)
     draw_ComputerSetMultiDiGraph_matplotlib(
         ax,
         spsg,
         bgc_md2_mvar_aliases(), 
         bgc_md2_computer_aliases(),
     )
     fig.savefig("SetMultiGraph.pdf")
Beispiel #6
0
    def test_arg_set_graph(self):
        asg_I = arg_set_graph(I, self.computers)
        asg_B = arg_set_graph(B, self.computers)
        # For compatibility arg_set_graph returns a multigraph
        # although we do not have more than one edge between a pair
        # of nodes.

        ref_I = nx.MultiDiGraph()
        ref_I.add_edge(
            frozenset({A, B, K, J}),
            frozenset({I}),
            computers=frozenset({i_from_a_b_k_j}),
        )

        ref_B = nx.MultiDiGraph()
        ref_B.add_node(frozenset({B}))
        # picture for manual check
        fig = plt.figure(figsize=(20, 20))
        ax1 = fig.add_subplot(2, 2, 1)
        ax2 = fig.add_subplot(2, 2, 2)
        ax3 = fig.add_subplot(2, 2, 3)
        ax4 = fig.add_subplot(2, 2, 4)
        draw_ComputerSetMultiDiGraph_matplotlib(ax1, ref_I)
        draw_ComputerSetMultiDiGraph_matplotlib(ax2, asg_I)
        draw_ComputerSetMultiDiGraph_matplotlib(ax3, ref_B)
        draw_ComputerSetMultiDiGraph_matplotlib(ax4, asg_B)
        fig.savefig("arg_set_graph.pdf")

        self.assertTrue(equivalent_multigraphs(asg_I, ref_I))
        self.assertTrue(equivalent_multigraphs(asg_B, ref_B))
Beispiel #7
0
    def test_ComputerSetMultiGraph_matplotlib(self):
        # The direct visualization of networkx (using matplotlib)
        # is very rudimentary.
        fig = plt.figure(figsize=(20, 20))
        ax = fig.add_subplot(1, 1, 1)
        draw_ComputerSetMultiDiGraph_matplotlib(ax, self.spsg)
        fig.savefig("SetMultiGraph.pdf")

        fig = plt.figure(figsize=(20, 20))
        ax = fig.add_subplot(1, 1, 1)
        print(list(self.spsg.nodes))
        draw_ComputerSetMultiDiGraph_matplotlib(ax,
                                                self.spsg,
                                                targetNode=frozenset({C, D}))
        fig.savefig("SetMultiGraphWithTargetNode.pdf")
Beispiel #8
0
    def test_minimal_startnodes_for_single_var(self):
        spsg = sparse_powerset_graph(self.computers)
        ## After the graph has been computed we can use it
        ## to infer computability of all Mvars
        fig1 = plt.figure(figsize=(20, 20))
        axs = fig1.subplots(1, 1)
        draw_ComputerSetMultiDiGraph_matplotlib(axs, spsg)
        fig1.savefig("spsg.pdf")
        plt.close(fig1)

        fig2 = plt.figure(figsize=(20, 100))
        draw_update_sequence(self.computers, max_it=8, fig=fig2)
        fig2.savefig("c1.pdf")
        plt.close(fig2)
        res = minimal_startnodes_for_single_var(spsg, H)
        print(nodes_2_string(res))
        path = nx.single_source_shortest_path(spsg,
                                              source=frozenset(
                                                  {A, B, C, E, G, J, K}))
        print("path", nodes_2_string(path))
Beispiel #9
0
 def test_minimal_startnodes_for_single_var(self):
     spsg = sparse_powerset_graph(self.computers)
     fig1 = plt.figure(figsize=(15, 15))
     axs = fig1.subplots(1, 1)
     draw_ComputerSetMultiDiGraph_matplotlib(
         axs,
         spsg,
         targetNode=frozenset({B})
     )
     fig1.savefig("spsg_B.pdf")
     plt.close(fig1)
     # After the graph has been computed we can use it
     # to infer computability of all Mvars
     res_B = minimal_startnodes_for_single_var(spsg, B)
     self.assertSetEqual(
         res_B,
         frozenset({
             frozenset({E, F}),
             frozenset({C, D}),
             frozenset({H, C, G}),
             frozenset({H, C, D, G}),
             frozenset({E, H, F, G})
         })
     )
     fig1 = plt.figure(figsize=(15, 15))
     axs = fig1.subplots(1, 1)
     draw_ComputerSetMultiDiGraph_matplotlib(
         axs,
         spsg,
         targetNode=frozenset({A})
     )
     fig1.savefig("spsg_A.pdf")
     plt.close(fig1)
     res_A = minimal_startnodes_for_single_var(spsg, A)
     self.assertSetEqual(
         res_A,
         frozenset({
             frozenset({I})
         })
     )
Beispiel #10
0
 def test_computable_mvars(self):
     spsg=sparse_powerset_graph(bgc_md2_computers())
     f = plt.figure()
     ax = f.add_subplot(1,1,1)
     draw_ComputerSetMultiDiGraph_matplotlib(
             ax,
             spsg, 
             bgc_md2_mvar_aliases(), 
             bgc_md2_computer_aliases(),
             targetNode=frozenset({SmoothModelRun})
     )
     f.savefig("spgs.pdf")
     fig = plt.figure()
     draw_update_sequence(bgc_md2_computers(), max_it=8, fig=fig)
     fig.savefig("c1.pdf")
     mvs=self.mvs
     mvars = mvs.computable_mvar_types()
     list_str = "\n".join(["<li> " + str(var.__name__) + " </li>" for var in mvars])
     print(list_str)
     for var in mvars:
         print("########################################")
         print(str(var.__name__))
         print(mvs._get_single_mvar_value(var))
Beispiel #11
0
    def test_product_graph(self):
        computers = frozenset({a_from_y, a_from_z, b_from_y, b_from_z})

        asg_A = arg_set_graph(A, computers)
        asg_B = arg_set_graph(B, computers)
        pg_A_B = product_graph(asg_A, asg_B)

        fig1 = plt.figure(figsize=(20, 100))

        ax1 = fig1.add_subplot(411, frame_on=True, title="arg_set_graph(A)")
        ax2 = fig1.add_subplot(412, frame_on=True, title="arg_set_graph(B)")
        ax3 = fig1.add_subplot(413, frame_on=True, title="product_graph(A,B)")
        # ax4=fig1.add_subplot(414,frame_on=True,title="ref")
        draw_ComputerSetMultiDiGraph_matplotlib(ax1, asg_A)
        draw_ComputerSetMultiDiGraph_matplotlib(ax2, asg_B)
        draw_ComputerSetMultiDiGraph_matplotlib(ax3, pg_A_B)
        fig1.savefig("AB_Z.pdf")

        computers = frozenset({a_from_y, b_from_y, b_from_z})
        asg_A = arg_set_graph(A, computers)
        asg_B = arg_set_graph(B, computers)
        pg_A_B = product_graph(asg_A, asg_B)

        fig1 = plt.figure(figsize=(10, 30))
        ax1 = fig1.add_subplot(411, frame_on=True, title="arg_set_graph(A)")
        ax2 = fig1.add_subplot(412, frame_on=True, title="arg_set_graph(B)")
        ax3 = fig1.add_subplot(413, frame_on=True, title="product_graph(A,B)")
        # ax4=fig1.add_subplot(414,frame_on=True,title="ref")
        draw_ComputerSetMultiDiGraph_matplotlib(ax1, asg_A)
        draw_ComputerSetMultiDiGraph_matplotlib(ax2, asg_B)
        draw_ComputerSetMultiDiGraph_matplotlib(ax3, pg_A_B)
        fig1.savefig("A_y_B_Y_B_Z.pdf")

        #    {a(z)}          {b(z)}          {a(z),b(z)}

        # {z}  ->  {a} x {z}  ->   {b}   = {z} -> {a,b}

        computers = frozenset({a_from_z, b_from_z})
        asg_A = arg_set_graph(A, computers)
        asg_B = arg_set_graph(B, computers)
        pg_A_B = product_graph(asg_A, asg_B)

        fig1 = plt.figure(figsize=(10, 30))
        ax1 = fig1.add_subplot(411, frame_on=True, title="arg_set_graph(A)")
        ax2 = fig1.add_subplot(412, frame_on=True, title="arg_set_graph(B)")
        ax3 = fig1.add_subplot(413, frame_on=True, title="product_graph(A,B)")
        # ax4=fig1.add_subplot(414,frame_on=True,title="ref")
        draw_ComputerSetMultiDiGraph_matplotlib(ax1, asg_A)
        draw_ComputerSetMultiDiGraph_matplotlib(ax2, asg_B)
        draw_ComputerSetMultiDiGraph_matplotlib(ax3, pg_A_B)
        fig1.savefig("A_Z_B_Y.pdf")

        computers = frozenset({a_from_z, b_from_z, c_from_b})
        asg_A = arg_set_graph(A, computers)
        asg_B = arg_set_graph(B, computers)
        asg_C = arg_set_graph(C, computers)
        asg_C = arg_set_graph(C, computers)
        prod = product_graph(*[asg_A, asg_B, asg_C])

        fig1 = plt.figure(figsize=(10, 30))
        ax1 = fig1.add_subplot(511, frame_on=True, title="arg_set_graph(A)")
        ax2 = fig1.add_subplot(512, frame_on=True, title="arg_set_graph(B)")
        ax3 = fig1.add_subplot(513, frame_on=True, title="arg_set_graph(C)")
        ax4 = fig1.add_subplot(514, frame_on=True, title="product_graph(A,B,C)")
        # ax4=fig1.add_subplot(414,frame_on=True,title="ref")
        draw_ComputerSetMultiDiGraph_matplotlib(ax1, asg_A)
        draw_ComputerSetMultiDiGraph_matplotlib(ax2, asg_B)
        draw_ComputerSetMultiDiGraph_matplotlib(ax3, asg_C)
        draw_ComputerSetMultiDiGraph_matplotlib(ax4, prod)
        fig1.savefig("ABC.pdf")