Exemplo n.º 1
0
    def test_absolute_and_relative_error(self):
        v_right = Variable(data=np.arange(10).reshape(2, 5), unit="m")
        v_wrong = Variable(data=np.arange(10).reshape(2, 5) + 1, unit="m")
        abs_err = v_wrong.absolute_error(v_right)
        self.assertTrue(np.all(abs_err.data == np.ones(10).reshape(2, 5)))
        self.assertEqual(abs_err.unit, "m")
        rel_err = v_wrong.relative_error(v_right)
        res = 100 / np.ma.arange(10).reshape(2, 5)
        self.assertTrue(np.all(rel_err.data == res.data))
        self.assertEqual(rel_err.unit, "%")

        ## convertible units
        v_right = Variable(data=np.arange(10).reshape(2, 5) * 1000, unit="m")
        v_wrong = Variable(data=np.arange(10).reshape(2, 5) + 1, unit="km")
        abs_err = v_wrong.absolute_error(v_right)
        self.assertTrue(np.all(abs_err.data == np.ones(10).reshape(2, 5) * 1000))
        self.assertEqual(abs_err.unit, "m")
        rel_err = v_wrong.relative_error(v_right)
        res = 100 / np.ma.arange(10).reshape(2, 5)
        self.assertTrue(np.all(rel_err.data == res.data))
        self.assertEqual(rel_err.unit, "%")

        ## unconvertible units
        v_right = Variable(data=np.arange(10).reshape(2, 5) * 1000, unit="m")
        v_wrong = Variable(data=np.arange(10).reshape(2, 5) + 1, unit="km/s")
        with self.assertRaises(ValueError):
            abs_err = v_wrong.absolute_error(v_right)
        with self.assertRaises(ValueError):
            rel_err = v_wrong.relative_error(v_right)

        ## incompatible shapes
        v_right = Variable(data=np.arange(10) * 1000, unit="m")
        v_wrong = Variable(data=np.arange(10).reshape(2, 5) + 1, unit="km/s")
        with self.assertRaises(AssertionError):
            abs_err = v_wrong.absolute_error(v_right)
        with self.assertRaises(AssertionError):
            rel_err = v_wrong.relative_error(v_right)
Exemplo n.º 2
0
    def create_discrete_model_run(self, errors=False):
        out = self.load_xs_Us_Fs_Rs()
        xs, Us, Fs, Rs = out
        start_values = xs.data[0, :]

        if (xs.data.mask.sum() + Fs.data.mask.sum() + Us.data.mask.sum() +
                Rs.data.mask.sum() == 0):

            # fixme hm 2020-04-21:
            # which reconstruction version is the right choice?
            #            dmr = DMR.reconstruct_from_data(
            #                self.time_agg.data.filled(),
            #                start_values.filled(),
            #                xs.data.filled(),
            #                Fs.data.filled(),
            #                Rs.data.filled(),
            #                Us.data.filled()
            #            )

            #            dmr = DMR.reconstruct_from_fluxes_and_solution(
            dmr = DMRWGF.reconstruct_from_fluxes_and_solution(
                self.time_agg.data.filled(),
                xs.data.filled(),
                Fs.data.filled(),
                Rs.data.filled(),
                Us.data.filled(),
                Fs.data.filled(),
                Rs.data.filled(),
            )
        else:
            dmr = None

        if errors:
            soln_dmr = Variable(data=dmr.solve(), unit=self.stock_unit)
            abs_err = soln_dmr.absolute_error(xs)
            rel_err = soln_dmr.relative_error(xs)

            return dmr, abs_err, rel_err
        else:
            return dmr
Exemplo n.º 3
0
    def create_model_run(self, errors=False):
        out = self.load_xs_Us_Fs_Rs()
        xs, Us, Fs, Rs = out
        # print(self.time_agg.data)
        # print(xs.data)
        # print(Fs.data)
        # print(Rs.data)
        # print(Us.data)
        # input()

        times = self.time_agg.data.filled()
        # times = np.arange(len(self.time_agg.data))
        if (xs.data.mask.sum() + Fs.data.mask.sum() + Us.data.mask.sum() +
                Rs.data.mask.sum() == 0):
            pwc_mr_fd = PWCMRFD.from_gross_fluxes(
                symbols("t"),
                times,
                xs.data.filled()[0],
                # xs.data.filled(),
                Us.data.filled(),
                Fs.data.filled(),
                Rs.data.filled(),
                #                xs.data.filled()
            )
        else:
            pwc_mr_fd = None

        if errors:
            err_dict = {}

            soln = pwc_mr_fd.solve()
            soln_pwc_mr_fd = Variable(data=soln, unit=self.stock_unit)
            abs_err = soln_pwc_mr_fd.absolute_error(xs)
            rel_err = soln_pwc_mr_fd.relative_error(xs)
            err_dict["stocks"] = {"abs_err": abs_err, "rel_err": rel_err}

            Us_pwc_mr_fd = Variable(
                name="acc_gross_external_input_vector",
                data=pwc_mr_fd.acc_gross_external_input_vector(),
                unit=self.stock_unit,
            )
            abs_err = Us_pwc_mr_fd.absolute_error(Us)
            rel_err = Us_pwc_mr_fd.relative_error(Us)
            err_dict["acc_gross_external_inputs"] = {
                "abs_err": abs_err,
                "rel_err": rel_err,
            }

            Rs_pwc_mr_fd = Variable(
                name="acc_gross_external_output_vector",
                data=pwc_mr_fd.acc_gross_external_output_vector(),
                unit=self.stock_unit,
            )
            abs_err = Rs_pwc_mr_fd.absolute_error(Rs)
            rel_err = Rs_pwc_mr_fd.relative_error(Rs)
            err_dict["acc_gross_external_outputs"] = {
                "abs_err": abs_err,
                "rel_err": rel_err,
            }

            Fs_pwc_mr_fd = Variable(
                name="acc_gross_internal_flux_matrix",
                data=pwc_mr_fd.acc_gross_internal_flux_matrix(),
                unit=self.stock_unit,
            )
            abs_err = Fs_pwc_mr_fd.absolute_error(Fs)
            rel_err = Fs_pwc_mr_fd.relative_error(Fs)
            err_dict["acc_gross_internal_fluxes"] = {
                "abs_err": abs_err,
                "rel_err": rel_err,
            }

            return pwc_mr_fd, err_dict
        else:
            return pwc_mr_fd
Exemplo n.º 4
0
    def create_model_run(self,
                         integration_method='solve_ivp',
                         nr_nodes=None,
                         errors=False,
                         check_success=True):
        out = self.load_xs_Us_Fs_Rs()
        xs, Us, Fs, Rs = out
        # print(self.time_agg.data)
        # print(xs.data)
        # print(Fs.data)
        # print(Rs.data)
        # print(Us.data)
        # input()

        times = self.time_agg.data.filled()
        # times = np.arange(len(self.time_agg.data))
        if (xs.data.mask.sum() + Fs.data.mask.sum() + Us.data.mask.sum() +
                Rs.data.mask.sum() == 0):
            pwc_mr_fd = PWCMRFD.from_gross_fluxes(symbols("t"), times,
                                                  xs.data.filled()[0],
                                                  Us.data.filled(),
                                                  Fs.data.filled(),
                                                  Rs.data.filled(),
                                                  xs.data.filled(),
                                                  integration_method, nr_nodes,
                                                  check_success)
        else:
            pwc_mr_fd = None

        if errors:
            #            print('Computing reconstruction errors')
            err_dict = {}

            #            print('  solution error')
            soln = pwc_mr_fd.solve()
            soln_pwc_mr_fd = Variable(name="stocks",
                                      data=soln,
                                      unit=self.stock_unit)
            abs_err = soln_pwc_mr_fd.absolute_error(xs)
            rel_err = soln_pwc_mr_fd.relative_error(xs)
            err_dict["stocks"] = {
                "abs_err": abs_err.max(),
                "rel_err": rel_err.max()
            }

            #            print('  input fluxes error')
            Us_pwc_mr_fd = Variable(
                name="acc_gross_external_input_vector",
                data=pwc_mr_fd.acc_gross_external_input_vector(),
                unit=self.stock_unit,
            )
            abs_err = Us_pwc_mr_fd.absolute_error(Us)
            rel_err = Us_pwc_mr_fd.relative_error(Us)
            err_dict["acc_gross_external_inputs"] = {
                "abs_err": abs_err.max(),
                "rel_err": rel_err.max(),
            }

            #            print('  output fluxes error')
            Rs_pwc_mr_fd = Variable(
                name="acc_gross_external_output_vector",
                data=pwc_mr_fd.acc_gross_external_output_vector(),
                unit=self.stock_unit,
            )
            abs_err = Rs_pwc_mr_fd.absolute_error(Rs)
            rel_err = Rs_pwc_mr_fd.relative_error(Rs)
            err_dict["acc_gross_external_outputs"] = {
                "abs_err": abs_err.max(),
                "rel_err": rel_err.max(),
            }

            #            print('  internal fluxes error')
            Fs_pwc_mr_fd = Variable(
                name="acc_gross_internal_flux_matrix",
                data=pwc_mr_fd.acc_gross_internal_flux_matrix(),
                unit=self.stock_unit,
            )
            abs_err = Fs_pwc_mr_fd.absolute_error(Fs)
            rel_err = Fs_pwc_mr_fd.relative_error(Fs)
            err_dict["acc_gross_internal_fluxes"] = {
                "abs_err": abs_err.max(),
                "rel_err": rel_err.max(),
            }
            abs_err.argmax()
            rel_err.argmax()

            #            print('done')
            return pwc_mr_fd, err_dict
        else:
            return pwc_mr_fd, dict()