Exemple #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})
         })
     )
Exemple #2
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))
Exemple #3
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")
Exemple #4
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))
Exemple #5
0
 def setUp(self):
     # fixme mm 02-03-2020:
     # assemble this set from a file of class definitions
     self.mvars = {
         A,
         B,
         C,
         D,
         E,
         F,
         G,
         H,
         I,
     }
     # fixme mm 02-03-2020:
     # assemble this set from a file of annotated functions in
     # special file
     self.computers = computers
     self.spsg = sparse_powerset_graph(self.computers)
     self.cf = computer_color_func(self.computers)
Exemple #6
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})
         })
     )
Exemple #7
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))
Exemple #8
0
    def test_Markus_graph_creation(self):
        # Now we build the directed Graph we can use to compute connectivity
        # the Nodes are sets of Mvars (elemenst of the powerset of all Mvars)
        # and a connection between two sets indicates computability of the target set from
        # the source set.
        # The complete graph would contain all elements of the powerset of allMvars and all
        # possible connections, which is prohibitively expensive.
        # Instead we will compute a subgraph where we start with one element sets as targets
        # and infer the predecessors of those sets and then the predecessors of the predecessors and so on until we do not find new nodes=start_sets.
        # spsg=sparse_powerset_graph(self.mvars,self.computers)

        ################# linear A1->A2->A3
        spsg = sparse_powerset_graph(frozenset({a2_from_a1, a3_from_a2}))
        self.assertSetEqual(
            set(spsg.nodes()), {frozenset({A1}), frozenset({A2}), frozenset({A3})}
        )
        self.assertSetEqual(
            set(spsg.edges()),
            {(frozenset({A1}), frozenset({A2})), (frozenset({A2}), frozenset({A3}))},
        )
        ################## cross
        #       B-2->B-1->B0->B1->B2
        #                  ||
        #                  \/
        #       A-2->A-1->A0->A1->A2
        spsg = sparse_powerset_graph(
            frozenset(
                {
                    b_minus_1_from_b_minus_2,
                    b0_from_b_minus_1,
                    b1_from_b0,
                    b2_from_b1,
                    b3_from_b2,
                    #
                    a0_from_b0,
                    #
                    a_minus_1_from_a_minus_2,
                    a0_from_a_minus_1,
                    a1_from_a0,
                    a2_from_a1,
                    a3_from_a2,
                }
            )
        )
        self.assertSetEqual(
            set(spsg.nodes()),
            {
                frozenset({B_minus_1}),
                frozenset({B_minus_2}),
                frozenset({B0}),
                frozenset({B1}),
                frozenset({B2}),
                frozenset({B3}),
                frozenset({A_minus_1}),
                frozenset({A_minus_2}),
                frozenset({A0}),
                frozenset({A1}),
                frozenset({A2}),
                frozenset({A3}),
            },
        )
        self.assertSetEqual(
            set(spsg.edges()),
            {
                (frozenset({B_minus_2}), frozenset({B_minus_1})),
                (frozenset({B_minus_1}), frozenset({B0})),
                (frozenset({B0}), frozenset({B1})),
                (frozenset({B1}), frozenset({B2})),
                (frozenset({B2}), frozenset({B3})),
                #
                (frozenset({B0}), frozenset({A0})),
                #
                (frozenset({A_minus_2}), frozenset({A_minus_1})),
                (frozenset({A_minus_1}), frozenset({A0})),
                (frozenset({A0}), frozenset({A1})),
                (frozenset({A1}), frozenset({A2})),
                (frozenset({A2}), frozenset({A3})),
            },
        )