Exemplo n.º 1
0
def test_var_returns_a_float():
    var = Var([50, 17], [1990, 2000, 2010])
    var[:, :] = 34.7
    assert isinstance(var[50, 2000], float)

    var[17, 1990] = 204.3
    assert var[17, 1990] == 204.3
Exemplo n.º 2
0
def test_mulstd_read_failure():
    onet = Var([50, 60], [2000, 2010])
    with pytest.raises(ValueError):
        onet.get_mulstd("else")

    # Here the key is good, but there is nothing there.
    assert isnan(onet.get_mulstd('dage'))
Exemplo n.º 3
0
def test_mulstd(name, value):
    onet = Var([50, 60], [2000, 2010])
    onet.set_mulstd(name, value)
    assert onet.get_mulstd(name) == value

    onet.set_mulstd("dage", 2.4)
    onet.set_mulstd("dtime", -7.3)
    assert onet.get_mulstd("dage") == 2.4
    assert onet.get_mulstd("dtime") == -7.3
Exemplo n.º 4
0
    def var_from_mean(self):
        """Given a prior grid, create a Var from the mean of the value priors.

        Returns:
            Var: A new Var object with the same ages and times and value
            equal to the mean.
        """
        var = Var(self.ages, self.times)
        for age, time in self.age_time():
            var[age, time] = self.value[age, time].mean
        return var
Exemplo n.º 5
0
    def _ensure_weights(self):
        """If weights weren't made, then make them constant. Must be done after
        there is data in the Model."""
        # Find an age and time already in the model because adding an
        # age and time outside the model can change the integration ranges.
        arbitrary_grid = next(iter(self.rate.values()))
        one_age_time = (arbitrary_grid.ages[0:1], arbitrary_grid.times[0:1])

        for kind in (weight.name for weight in WeightEnum):
            if kind not in self.weights:
                self.weights[kind] = Var(*one_age_time)
                self.weights[kind].grid.loc[:, "mean"] = 1.0
Exemplo n.º 6
0
    def get_weights(self):
        """
        Gets the weights to be written for the model.
        """
        weights = self.weights.copy()
        arbitrary_grid = next(iter(self.rate.values()))
        one_age_time = (arbitrary_grid.ages[0:1], arbitrary_grid.times[0:1])

        for kind in (weight.name for weight in WeightEnum):
            if kind not in self.weights:
                weights[kind] = Var(*one_age_time)
                weights[kind].grid.loc[:, "mean"] = 1.0
        return weights
Exemplo n.º 7
0
def rectangular_data_to_var(gridded_data):
    """Using this very regular data, where every age and time is present,
    construct an initial guess as a Var object. Very regular means that there
    is a complete set of ages-cross-times."""
    gridded_data = gridded_data.copy()
    try:
        gridded_data['age'] = gridded_data[['age_lower', 'age_upper']].mean(axis=1)
        gridded_data['time'] = gridded_data[['time_lower', 'time_upper']].mean(axis=1)
    except AttributeError:
        LOG.error(f"Data to make a var has columns {gridded_data.columns}")
        raise RuntimeError(
            f"Wrong columns in rectangular_data_to_var {gridded_data.columns}")
    gridded_data = gridded_data.sort_values(by=['age', 'time'])
    guess = Var(ages=sorted(gridded_data['age'].unique()), times=sorted(gridded_data['time'].unique()))
    assert guess.variable_count() == len(gridded_data), \
        "Number of age/time points exceed number of unique age/time points"
    guess[:, :] = gridded_data['mean'].values.reshape((-1, 1))
    return guess
Exemplo n.º 8
0
def test_time_dimension(a, t, v):
    times = [1990, 1995, 2000, 2005]
    onet = Var([50], times)
    for i in range(4):
        onet[50, times[i]] = 0.3 * i
    assert isclose(onet(a, t), v)
Exemplo n.º 9
0
def test_age_dimension(a, t, v):
    onea = Var([0, 1, 2, 3], [2000])
    for i in range(4):
        onea[i, :] = 0.2 * i
    assert isclose(onea(a, t), v)
Exemplo n.º 10
0
def test_warp(a, t, v):
    warp = Var([0, 1], [5, 10, 15])
    warp[:, :] = 0.5
    warp[1, 10] = 6
    assert isclose(warp(a, t), v)
    assert warp(0.9, 10) > 5
Exemplo n.º 11
0
def test_const_square(a, t, v):
    two = Var([0, 1], [5, 10])
    two[:, :] = 36.9
    assert isclose(two(a, t), v)
Exemplo n.º 12
0
def test_works_as_a_function():
    const = Var([0], [0])
    const[0, 0] = 21.2
    assert const(0, 0) == 21.2
    assert const(365, -29) == 21.2
Exemplo n.º 13
0
def test_mulstd_failure(name, value):
    onet = Var([50, 60], [2000, 2010])
    with pytest.raises(ValueError):
        onet.set_mulstd(name, value)