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)
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]))
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")
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"
plt.show() # %% counts = crime.get_crime_counts() print(counts) outcomes = crime.get_crime_outcomes() print(outcomes) cats = crime.get_category_breakdown() print(cats) subcats = cats.loc["violence and sexual offences"] print(subcats) print(subcats.proportion.sum()) d = subcats.index.values p = subcats.proportion.values m = no.Model(no.Timeline.null(), no.MonteCarlo.deterministic_identical_stream) print("%g" % (subcats.proportion.sum() - 1.0)) s = m.mc().sample(100, subcats.proportion.values / subcats.proportion.sum()) print(subcats.iloc[s].index) print(counts.index.levels[1].unique()) print(cats.index.levels[0].unique()) # %% # import importlib # importlib.reload(crime.Crime) # %%
# 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)
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
no.log(str(t)) no.log(t.__str__()) no.log(no.time.isnever(1.234)) no.log(no.time.isnever(no.time.distant_past())) no.log(no.time.isnever(no.time.far_future())) no.log(no.time.isnever(no.time.never())) while not t.at_end(): no.log((t.index(), t.at_checkpoint(), t.at_end())) t.next() no.log((t.index(), t.at_checkpoint(), t.at_end())) # for _ in t: # no.log() m = no.Model(t, no.MonteCarlo.deterministic_identical_seed) m.modify(no.mpi.rank()) try: m.step() except NotImplementedError as e: no.log(e) else: assert False, "expected error, didnt get one" assert m.check() try: m.checkpoint() except NotImplementedError as e: