class _MaybeBackToDefault(SideEffectOnly): """ This is an internal operation of story, that transits members back to default with probability as declared in back_to_default_probability """ def __init__(self, story): self.judge = NumpyRandomGenerator(method="uniform", seed=1234) self.story = story def side_effect(self, story_data): # only transiting members that have ran during this clock tick active_timer = self.story.timer.loc[story_data.index] non_default_ids = active_timer[ active_timer["state"] != "default"].index if non_default_ids.shape[0] == 0: return back_prob = self.story.get_param("back_to_default_probability", non_default_ids) if np.all(back_prob == 0): cond = [False] * non_default_ids.shape[0] elif np.all(back_prob == 1): cond = [True] * non_default_ids.shape[0] else: baseline = self.judge.generate(back_prob.shape[0]) cond = back_prob > baseline member_ids = back_prob[cond].index states = ["default"] * member_ids.shape[0] self.story.transit_to_state(ids=member_ids, states=states)
def load_empirical_discrete_generator(namespace, gen_id, seed): root_folder = _empirical_discrete_gen_folder(namespace) gen_file_path = os.path.join(root_folder, "%s.csv" % gen_id) df = pd.read_csv(gen_file_path) gen = NumpyRandomGenerator(method="choice", a=df["x"].tolist(), p=df["px"].tolist(), seed=seed) return gen
def _add_message_story(self): story_timer_gen = DefaultDailyTimerGenerator( clock=self.clock, seed=next(self.seeder)) low_activity = story_timer_gen.activity(n=3, per=pd.Timedelta("1 day")) med_activity = story_timer_gen.activity(n=10, per=pd.Timedelta("1 day")) high_activity = story_timer_gen.activity(n=20, per=pd.Timedelta("1 day")) activity_gen = NumpyRandomGenerator( method="choice", a=[low_activity, med_activity, high_activity], p=[.2, .7, .1], seed=next(self.seeder)) hello_world = self.create_story( name="hello_world", initiating_population=self.populations["person"], member_id_field="PERSON_ID", timer_gen=story_timer_gen, activity_gen=activity_gen ) hello_world.set_operations( self.clock.ops.timestamp(named_as="TIME"), self.populations["person"].get_relationship("quotes") .ops.select_one(from_field="PERSON_ID",named_as="MESSAGE"), self.populations["person"] .get_relationship("friends") .ops.select_one(from_field="PERSON_ID", named_as="OTHER_PERSON"), self.populations["person"] .ops.lookup(id_field="PERSON_ID", select={"NAME": "EMITTER_NAME"}), self.populations["person"] .ops.lookup(id_field="OTHER_PERSON", select={"NAME": "RECEIVER_NAME"}), operations.FieldLogger(log_id="hello_6") )
def _add_person_population(self): id_gen = SequencialGenerator(prefix="PERSON_") age_gen = NumpyRandomGenerator(method="normal", loc=3, scale=5, seed=next(self.seeder)) name_gen = FakerGenerator(method="name", seed=next(self.seeder)) person = self.create_population(name="person", size=1000, ids_gen=id_gen) person.create_attribute("NAME", init_gen=name_gen) person.create_attribute("AGE", init_gen=age_gen) quote_generator = FakerGenerator(method="sentence", nb_words=6, variable_nb_words=True, seed=next(self.seeder)) quotes_rel = self.populations["person"].create_relationship("quotes") for w in range(4): quotes_rel.add_relations( from_ids=person.ids, to_ids=quote_generator.generate(size=person.size), weights=w )
def __init__(self, story): self.judge = NumpyRandomGenerator(method="uniform", seed=1234) self.story = story
from tabulate import tabulate from core import container, operations from core.random_generators import SequencialGenerator, FakerGenerator, NumpyRandomGenerator, ConstantDependentGenerator, ConstantGenerator import core.util_functions as util_functions util_functions.setup_logging() example_circus = container.Container(name="example", master_seed=12345, start=pd.Timestamp("1 Jan 2017 00:00"), step_duration=pd.Timedelta("1h")) id_gen = SequencialGenerator(prefix="PERSON_") age_gen = NumpyRandomGenerator(method="normal", loc=3, scale=5, seed=next(example_circus.seeder)) name_gen = FakerGenerator(method="name", seed=next(example_circus.seeder)) person = example_circus.create_population(name="person", size=1000, ids_gen=id_gen) person.create_attribute("NAME", init_gen=name_gen) person.create_attribute("AGE", init_gen=age_gen) hello_world = example_circus.create_story( name="hello_world", initiating_population=example_circus.populations["person"], member_id_field="PERSON_ID", timer_gen=ConstantDependentGenerator(value=1))
from core.random_generators import SequencialGenerator, FakerGenerator, NumpyRandomGenerator, ConstantDependentGenerator, ConstantGenerator import core.util_functions as util_functions util_functions.setup_logging() example_container = container.Container(name="example1", master_seed=1234, start=pd.Timestamp("1 Jan 2017 00:00"), step_duration=pd.Timedelta("1h")) id_gen = SequencialGenerator(prefix="PERSON_") name_gen = FakerGenerator(method="name", seed=next(example_container.seeder)) age_gen = NumpyRandomGenerator(method="choice", a=[20, 30, 40, 60], p=[0.2, 0.3, 0.2, 0.3], seed=next(example_container.seeder)) gender_gen = NumpyRandomGenerator(method="choice", a=["male", "female"], p=[0.6, 0.4], seed=next(example_container.seeder)) job_type_gen = NumpyRandomGenerator( method="choice", a=["salaried", "non_salaried", "job_type3", "job_type4", "job_type5"], p=[0.2, 0.2, 0.2, 0.2, 0.2], seed=next(example_container.seeder)) person = example_container.create_population(name="person", size=1000,
def build_healthy_level_gen(seed): return NumpyRandomGenerator(method="beta", a=1, b=999, seed=seed)