Пример #1
0
  def test_arrays() -> None:

    x = np.array([1,4,9,16])
    if no.mpi.rank() == 0:
      comm.send(x, dest=1)
    if no.mpi.rank() == 1:
      y = comm.recv(source=0)
      assert np.array_equal(x,y)

    df = pd.read_csv("./test/df2.csv")
    if no.mpi.rank() == 0:
      comm.send(df, dest=1)
    if no.mpi.rank() == 1:
      dfrec = comm.recv(source=0)
      assert dfrec.equals(df)

    i = "rank %d" % no.mpi.rank()
    root = 0
    i = comm.bcast(i, root=root)
    # all procs should now have root process value
    assert i == "rank 0"

    # a0 will be different for each proc
    a0 = np.random.rand(2,2)
    a1 = comm.bcast(a0, root)
    # a1 will equal a0 on rank 0 only
    if no.mpi.rank() == 0:
      assert np.array_equal(a0, a1)
    else:
      assert not np.array_equal(a0, a1)

    # base model for MC engine
    model = no.Model(no.NoTimeline(), no.MonteCarlo.deterministic_identical_stream)

    # # check identical streams (independent=False)
    u = model.mc.ustream(1000)
    v = comm.bcast(u, root=root)
    # u == v on all processes
    assert np.array_equal(u, v)

    # base model for MC engine
    model = no.Model(no.NoTimeline(), no.MonteCarlo.deterministic_independent_stream)

    # # check identical streams (independent=False)
    u = model.mc.ustream(1000)
    v = comm.bcast(u, root=root)
    # u != v on all non-root processes
    if no.mpi.rank() != root:
      assert not np.array_equal(u, v)
    else:
      assert np.array_equal(u, v)
Пример #2
0
def test() -> None:

    df = pd.read_csv("./test/df.csv")

    # base model for MC engine
    model = no.Model(no.NoTimeline(),
                     no.MonteCarlo.deterministic_identical_stream)

    cats = np.array(range(4))
    # identity matrix means no transitions
    trans = np.identity(len(cats))

    no.df.transition(model, cats, trans, df, "DC2101EW_C_ETHPUK11")

    assert len(df["DC2101EW_C_ETHPUK11"].unique()
               ) == 1 and df["DC2101EW_C_ETHPUK11"].unique()[0] == 2

    # NOTE transition matrix interpreted as being COLUMN MAJOR due to pandas DataFrame storing data in column-major order

    # force 2->3
    trans[2, 2] = 0.0
    trans[2, 3] = 1.0
    no.df.transition(model, cats, trans, df, "DC2101EW_C_ETHPUK11")
    no.log(df["DC2101EW_C_ETHPUK11"].unique())
    assert len(df["DC2101EW_C_ETHPUK11"].unique()
               ) == 1 and df["DC2101EW_C_ETHPUK11"].unique()[0] == 3

    # ~half of 3->0
    trans[3, 0] = 0.5
    trans[3, 3] = 0.5
    no.df.transition(model, cats, trans, df, "DC2101EW_C_ETHPUK11")
    assert np.array_equal(np.sort(df["DC2101EW_C_ETHPUK11"].unique()),
                          np.array([0, 3]))
Пример #3
0
def test_errors() -> None:

    df = pd.read_csv("./test/df.csv")

    # base model for MC engine
    model = no.Model(no.NoTimeline(),
                     no.MonteCarlo.deterministic_identical_stream)

    cats = np.array(range(4))
    # identity matrix means no transitions
    trans = np.identity(len(cats))

    # invalid transition matrices
    assert_throws(ValueError, no.df.transition, model, cats, np.ones((1, 2)),
                  df, "DC2101EW_C_ETHPUK11")
    assert_throws(ValueError, no.df.transition, model, cats, np.ones((1, 1)),
                  df, "DC2101EW_C_ETHPUK11")
    assert_throws(ValueError, no.df.transition, model, cats, trans + 0.1, df,
                  "DC2101EW_C_ETHPUK11")

    # category data MUST be 64bit integer. This will almost certainly be the default on linux/OSX (LP64) but maybe not on windows (LLP64)
    df["DC2101EW_C_ETHPUK11"] = df["DC2101EW_C_ETHPUK11"].astype(np.int32)

    assert_throws(TypeError, no.df.transition, model, cats, trans, df,
                  "DC2101EW_C_ETHPUK11")
Пример #4
0
def test_base() -> None:
    base = no.Model(no.NoTimeline(),
                    no.MonteCarlo.deterministic_identical_stream)

    assert_throws(
        RuntimeError, no.run, base
    )  # RuntimeError: Tried to call pure virtual function "Model::step"
Пример #5
0
    def __init__(self, mortality_hazard: float, n: int) -> None:

        # initialise base model with a nondeterministic seed results will vary (slightly)
        super().__init__(neworder.NoTimeline(),
                         neworder.MonteCarlo.nondeterministic_stream)

        # initialise population
        self.population = [Person(mortality_hazard) for _ in range(n)]
        neworder.log("created %d individuals" % n)
Пример #6
0
def test_null_timeline() -> None:
    t0 = no.NoTimeline()
    assert t0.nsteps() == 1
    assert t0.dt() == 0.0
    assert not t0.at_end()
    assert t0.index() == 0
    assert no.time.isnever(t0.time())
    assert no.time.isnever(t0.end())

    m = _TestModel2(0, 1, 1)
    no.run(m)
    assert m.timeline.at_end()
    assert m.timeline.index() == 1
    assert m.timeline.time() == 1.0
Пример #7
0
    def __init__(self, mortality_hazard_file: str, n: int, dt: float) -> None:
        # Direct sampling doesnt require a timeline
        super().__init__(neworder.NoTimeline(),
                         neworder.MonteCarlo.deterministic_identical_stream)
        # initialise cohort
        self.mortality_hazard = pd.read_csv(mortality_hazard_file)

        # store the largest age we have a rate for
        self.max_rate_age = max(self.mortality_hazard.DC1117EW_C_AGE) - 1

        # neworder.log(self.mortality_hazard.head())
        self.population = pd.DataFrame(
            index=neworder.df.unique_index(n),
            data={"age_at_death": neworder.time.far_future()})

        # the time interval of the mortality data values
        self.dt = dt
Пример #8
0
    def __init__(self, n: int, p: float) -> None:
        """
    We create a null timeline, corresponding to a single instantaneous
    transition, and initialise the base class with this plus a
    randomly-seeded Monte-Carlo engine

    NB it is *essential* to initialise the base class.
    Failure to do so will result in UNDEFINED BEHAVIOUR
    """
        super().__init__(neworder.NoTimeline(),
                         neworder.MonteCarlo.nondeterministic_stream)

        # create a silent population of size n
        self.population = pd.DataFrame(index=neworder.df.unique_index(n),
                                       data={"talkative": False})
        self.population.index.name = "id"

        # set the transition probability
        self.p_talk = p
Пример #9
0
    def __init__(self, n: int):

        super().__init__(neworder.NoTimeline(),
                         neworder.MonteCarlo.deterministic_identical_stream)

        # initialise population - time of death only
        self.population = pd.DataFrame(index=neworder.df.unique_index(n),
                                       data={
                                           "TimeOfDeath":
                                           self.mc.first_arrival(
                                               data.mortality_rate,
                                               data.mortality_delta_t, n, 0.0),
                                           "TimeOfPregnancy":
                                           neworder.time.never(),
                                           "Parity":
                                           Parity.CHILDLESS,
                                           "Unions":
                                           0,
                                       })
Пример #10
0
def test_consistency() -> None:

    # need to wrap timeline in a model to do the stepping, which isnt directly accessible from python
    class ConsistencyTest(no.Model):
        def __init__(self, timeline: no.Timeline) -> None:
            super().__init__(timeline,
                             no.MonteCarlo.deterministic_identical_stream)

        def step(self) -> None:
            pass

    m = ConsistencyTest(no.NoTimeline())
    assert m.timeline.nsteps() == 1
    no.run(m)
    assert m.timeline.index() == 1

    m = ConsistencyTest(no.LinearTimeline(2020, 2021, 12))

    assert m.timeline.nsteps() == 12
    no.run(m)
    assert m.timeline.index() == 12
    assert m.timeline.time() == 2021

    m = ConsistencyTest(no.NumericTimeline([2020 + i / 12 for i in range(13)]))
    assert m.timeline.nsteps() == 12
    no.run(m)
    assert m.timeline.index() == 12
    assert m.timeline.time() == 2021

    s = date(2019, 10, 31)
    e = date(2020, 10, 31)

    m = ConsistencyTest(no.CalendarTimeline(s, e, 1, "m"))
    assert m.timeline.time().date() == s
    assert m.timeline.nsteps() == 12
    no.run(m)
    assert m.timeline.time().date() == e
    assert m.timeline.index() == 12
Пример #11
0
# t = np.ones((3,3)) / 3
# no.df.transition(m, c, t, df, "n")
# no.log(df.n.value_counts())
# for i in c:
#   no.log(df.n.value_counts()[i] > n/3 - sqrt(n) and df.n.value_counts()[i] < n/3 + sqrt(n))

# t = np.array([
#   [1.0, 1.0, 1.0],
#   [0.0, 0.0, 0.0],
#   [0.0, 0.0, 0.0],
# ])
# no.df.transition(m, c, t, df, "n")
# no.log(df.n.value_counts())

if __name__ == "__main__":
    m = no.Model(no.NoTimeline(), no.MonteCarlo.deterministic_identical_stream)

    rows, tc, colcpp = cpp_impl(m, get_data())
    no.log("C++ %d: %f" % (rows, tc))

    m.mc.reset()
    rows, tp, colpy = python_impl(m, get_data())
    no.log("py  %d: %f" % (rows, tp))

    #no.log(colcpp-colpy)

    assert np.array_equal(colcpp, colpy)

    no.log("speedup factor = %f" % (tp / tc))

#  f(m)
Пример #12
0
 def __init__(self) -> None:
     super().__init__(no.NoTimeline(),
                      no.MonteCarlo.deterministic_identical_stream)
Пример #13
0
def test_basic() -> None:

    # test unique index generation
    idx = no.df.unique_index(100)
    assert np.array_equal(
        idx, np.arange(no.mpi.rank(), 100 * no.mpi.size(), step=no.mpi.size()))

    idx = no.df.unique_index(100)
    assert np.array_equal(
        idx,
        np.arange(100 * no.mpi.size() + no.mpi.rank(),
                  200 * no.mpi.size(),
                  step=no.mpi.size()))

    N = 100000
    # base model for MC engine
    model = no.Model(no.NoTimeline(),
                     no.MonteCarlo.deterministic_identical_stream)

    c = [1, 2, 3]
    df = pd.DataFrame({"category": [1] * N})

    # no transitions, check no changes
    t = np.identity(3)
    no.df.transition(model, c, t, df, "category")
    assert df.category.value_counts()[1] == N

    # all 1 -> 2
    t[0, 0] = 0.0
    t[0, 1] = 1.0
    no.df.transition(model, c, t, df, "category")
    assert 1 not in df.category.value_counts()
    assert df.category.value_counts()[2] == N

    # 2 -> 1 or 3
    t = np.array([
        [1.0, 0.0, 0.0],
        [0.5, 0.0, 0.5],
        [0.0, 0.0, 1.0],
    ])

    no.df.transition(model, c, t, df, "category")
    assert 2 not in df.category.value_counts()
    for i in [1, 3]:
        assert df.category.value_counts()[i] > N / 2 - sqrt(
            N) and df.category.value_counts()[i] < N / 2 + sqrt(N)

    # spread evenly
    t = np.ones((3, 3)) / 3
    no.df.transition(model, c, t, df, "category")
    for i in c:
        assert df.category.value_counts()[i] > N / 3 - sqrt(
            N) and df.category.value_counts()[i] < N / 3 + sqrt(N)

    # all -> 1
    t = np.array([
        [1.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
    ])
    no.df.transition(model, c, t, df, "category")
    assert df.category.value_counts()[1] == N