def test_example(): start_time = time() fig, ax = plt.subplots(2, figsize=(10, 4.5)) obs1 = simulator(sim_params, true_params) sim_params['I_C'] = 100.0 / 1000.0 sim_params['I_Str'] = 8.0 / 1000.0 obs2 = simulator(sim_params, true_params) display_time(time()-start_time) plot_data(obs1, ax[0]) plot_data(obs2, ax[1]) plt.savefig("data/test.png") plt.close()
def run_single_round(samples_filename="data/samples.pt"): start_time = time() torch.set_num_threads(num_threads) prior = utils.torchutils.BoxUniform(low=torch.as_tensor(prior_min), high=torch.as_tensor(prior_max)) posterior = infer(simulation_wrapper, prior, method=method, num_simulations=num_simulations, num_workers=num_workers) observation_trace = simulator(sim_params, true_params) obs_stats = calculate_summary_statistics(observation_trace) samples = posterior.sample((num_samples,), x=obs_stats) torch.save(samples, f'{samples_filename}') display_time(time() - start_time)
async def sync_error(self, ctx, error): if isinstance(error, commands.CommandOnCooldown): await ctx.send( cooldown.format( ctx.author.mention, botutils.BotEmoji.x_emoji, display_time(int( ctx.command.get_cooldown_retry_after(ctx))))) else: try: raise error except Exception: await ctx.send(error_str) await botutils.log(botutils.Level.error, traceback.format_exc())
async def playerstats_error(self, ctx, error): if isinstance(error, commands.CommandOnCooldown): await ctx.send( cooldown.format( ctx.author.mention, botutils.BotEmoji.x_emoji, display_time(int( ctx.command.get_cooldown_retry_after(ctx))))) elif isinstance(error, commands.CheckFailure): return elif isinstance(error, commands.errors.BadUnionArgument): await ctx.send(user_not_found_str.format(ctx.author.mention)) else: try: raise error except Exception: await ctx.send(error_str) await botutils.log(botutils.Level.error, traceback.format_exc())
async def time(self, ctx): """Time command usage: time can be used by all players or in DM """ import globvars # Day phase if globvars.master_state.game.current_phase == Phase.day: # Day phase: pre-nomination (base day phase) if base_day_loop.is_running(): start_time = base_day_loop.next_iteration total_duration = calculate_base_day_duration( globvars.master_state.game) __time_elapsed = ( datetime.datetime.now(datetime.timezone.utc) - start_time).seconds time_left = total_duration - __time_elapsed msg = time_day_base.format( display_time(total_duration), "is" if time_left == 1 or (time_left >= 60 and time_left < 120) else "are", display_time(time_left)) await ctx.send(msg) # Day phase: nomination loop is running elif nomination_loop.is_running(): # We are in the debate phase if debate_timer.is_running(): end_time = debate_timer.next_iteration total_duration = DEBATE_TIME time_left = ( end_time - datetime.datetime.now(datetime.timezone.utc)).seconds msg = time_debate.format(display_time(total_duration), display_time(time_left)) await ctx.send(msg) # We are in the active voting phase else: msg = time_voting await ctx.send(msg) # Day phase: waiting for a nomination else: start_time = globvars.master_state.game.nomination_iteration_date[ 0] duration = globvars.master_state.game.nomination_iteration_date[ 1] time_left = duration - (datetime.datetime.now() - start_time).seconds msg = time_nomination.format(display_time(duration), display_time(time_left)) await ctx.send(msg) # Night phase elif globvars.master_state.game.current_phase == Phase.night: min_night_duration = BASE_NIGHT max_night_duration = BASE_NIGHT + NIGHT_MULTIPLER * INCREMENT __time_elapsed = ( datetime.datetime.now() - globvars.master_state.game.night_start_time).seconds time_left = max_night_duration - __time_elapsed msg = time_night.format( display_time(min_night_duration), display_time(max_night_duration), "is" if time_left == 1 or (time_left >= 60 and time_left < 120) else "are", display_time(time_left)) await ctx.send(msg) # Dawn phase elif globvars.master_state.game.current_phase == Phase.dawn: min_dawn_duration = BASE_DAWN max_dawn_duration = BASE_DAWN + DAWN_MULTIPLIER * INCREMENT __time_elapsed = ( datetime.datetime.now() - globvars.master_state.game.dawn_start_time).seconds time_left = max_dawn_duration - __time_elapsed msg = time_dawn.format( display_time(min_dawn_duration), display_time(max_dawn_duration), "is" if time_left == 1 or (time_left >= 60 and time_left < 120) else "are", display_time(time_left)) await ctx.send(msg)
async def notify(self, ctx): """Notify command""" if ctx.invoked_subcommand is None: import globvars if cooldown_map.get(len(globvars.master_state.pregame), 0) > time.time(): min_next_no_cd_players = 20 while cooldown_map.get( min_next_no_cd_players, None) is None or cooldown_map[ min_next_no_cd_players] < time.time(): min_next_no_cd_players -= 1 return await ctx.send( notify_cooldown.format( ctx.author.mention, BotEmoji.x_emoji, display_time( int(cooldown_map[len( globvars.master_state.pregame)] - time.time())), min_next_no_cd_players)) # Set cooldowns if botutils.check_if_lobby(ctx): for i in range(len(globvars.master_state.pregame), -1, -1): if cooldown_map.get( i, None) is None or cooldown_map[i] < time.time(): cooldown_map[i] = time.time() + NOTIFY_COOLDOWN users_to_ping = [] with sqlite3.connect("data.sqlite3") as conn: c = conn.cursor() c.execute(""" SELECT user_id, min_playercount, cooldown, always_notify FROM notify """) for user_id, min_playercount, cooldown, always_notify in c.fetchall( ): member = globvars.client.get_guild(SERVER_ID).get_member( user_id) # Do not ping the person that ran the command notify_map[ctx.author.id] = time.time() if (member and member.id not in globvars.master_state.pregame and len(globvars.master_state.pregame) >= min_playercount and time.time() - notify_map.get(member.id, 0) > cooldown and (always_notify == 1 or member.status != Status.offline) and member.id != ctx.author.id): notify_map[member.id] = time.time() users_to_ping.append(member.id) c.execute("SELECT user_id FROM ignore") for user_id, in c.fetchall(): if user_id in users_to_ping: users_to_ping.remove(user_id) if len(users_to_ping) > 0: pings = [] for user_id in users_to_ping: pings.append(botutils.make_ping(user_id)) pings = ' '.join(pings) msg = f"{ctx.author.mention} {BotEmoji.mention} {pings}" else: msg = no_users_to_notify.format(ctx.author.mention, BotEmoji.x_emoji) await ctx.send(msg)