Ejemplo n.º 1
0
Archivo: data.py Proyecto: iiasa/ixmp
def add_test_data(scen: Scenario):
    # New sets
    t_foo = ["foo{}".format(i) for i in (1, 2, 3)]
    t_bar = ["bar{}".format(i) for i in (4, 5, 6)]
    t = t_foo + t_bar
    y = list(map(str, range(2000, 2051, 10)))

    # Add to scenario
    scen.init_set("t")
    scen.add_set("t", t)
    scen.init_set("y")
    scen.add_set("y", y)

    # Data
    ureg = pint.get_application_registry()
    x = Quantity(
        xr.DataArray(np.random.rand(len(t), len(y)),
                     coords=[("t", t), ("y", y)]),
        units=ureg.kg,
    )

    # As a pd.DataFrame with units
    x_df = x.to_series().rename("value").reset_index()
    x_df["unit"] = "kg"

    scen.init_par("x", ["t", "y"])
    scen.add_par("x", x_df)

    return t, t_foo, t_bar, x
Ejemplo n.º 2
0
    def scen_with_big_data(self, test_mp, num_params=10):
        from itertools import zip_longest

        # test_mp.add_unit('kg')
        scen = Scenario(test_mp, 'TestQuantity', 'big data', version='new')

        # Dimensions and their lengths (Fibonacci numbers)
        N_dims = 6
        dims = 'abcdefgh'[:N_dims + 1]
        sizes = [1, 5, 21, 21, 89, 377, 1597, 6765][:N_dims + 1]

        # commented: "377 / 73984365 elements = 0.00051% full"
        # from functools import reduce
        # from operator import mul
        # size = reduce(mul, sizes)
        # print('{} / {} elements = {:.5f}% full'
        #       .format(max(sizes), size, 100 * max(sizes) / size))

        # Names like f_0000 ... f_1596 along each dimension
        coords = []
        for d, N in zip(dims, sizes):
            coords.append([f'{d}_{i:04d}' for i in range(N)])
            # Add to Scenario
            scen.init_set(d)
            scen.add_set(d, coords[-1])

        def _make_values():
            """Make a DataFrame containing each label in *coords* ≥ 1 time."""
            values = list(zip_longest(*coords, np.random.rand(max(sizes))))
            result = pd.DataFrame(values, columns=list(dims) + ['value']) \
                       .ffill()
            result['unit'] = 'kg'
            return result

        # Fill the Scenario with quantities named q_01 ... q_09
        names = []
        for i in range(num_params):
            name = f'q_{i:02d}'
            scen.init_par(name, list(dims))
            scen.add_par(name, _make_values())
            names.append(name)

        yield scen