def __init__(self, init_db=False): """ Initializes the Restaurant Chooser Database object, which allows a user to interact with the database associated with the application. :param init_db: If true, the database will be deleted and then recreated with the default values set. Set to False by default. """ self._db = DatabaseAccessor() if (init_db): clear_data( ) # Clears all data from the app data directory, including the database file self._db.create_connection( ) # Creates a new database file if it doesn't exist) if (init_db): self._generate_initial_database_structure()
def train(args): tr_data = pickle.load(open(args.train_file)) vd_data = pickle.load(open(args.validation_file)) [theta, bias] = pickle.load(open(args.init_wt_file)) perf_db = DatabaseAccessor(Perf, args.model_perf_db) perf_db.create_table() debug_db = DatabaseAccessor(Distribution, args.debug_db) debug_db.create_table() nnet = FFNeuralNetwork() nnet.set_activation_func(args.actv) nnet.set_output_func('softmax') nnet.initialize(theta, bias) if args.train_layers: nnet.set_train_layers(args.train_layers) nnet.set_perf_writer(perf_db) nnet.set_debug_writer(debug_db) btheta, bbias = nnet.train(tr_data['X'], tr_data['Y'], vd_data['X'], vd_data['Y'], args.mini_batch_size, args.epochs, args.validation_freq) pickle.dump([btheta,bbias], open(args.model_file, 'wb'))
BS_4 = np.array([ 00.550, 01.083, 01.466, 01.983, 02.499, 03.083, 03.599, 04.199, 04.799, 05.499, 06.199, 06.950, 07.750, 08.633, 09.583, 10.850, 12.483, 14.266, 16.316 ]) * 1000 WS_4 = np.array([02.566, 07.933, 13.366]) * 1000 # Final number is 18. phase at cutoff is 22. # Time left is 18.183 - 16.316 = 1.86 seconds. # Number of revolutions left is 7/8 = 0.875. try: os.remove(Constants.DATABASE_NAME) except OSError: pass da = DatabaseAccessor() session_id = da.increment_and_get_session_id() for bs in BS: da.insert_ball_lap_times(session_id, bs) for ws in WS: da.insert_wheel_lap_times(session_id, ws) session_id = da.increment_and_get_session_id() for bs in BS_2: da.insert_ball_lap_times(session_id, bs) for ws in WS_2: da.insert_wheel_lap_times(session_id, ws)
def graph(db_file, filename_prefix): db = DatabaseAccessor(Distribution, db_file) data = [item for item in db.read()] save_fig(data, filename_prefix)
def graph(rand_db, adaboost_db, filename, ttl, x_lim, y_lim): db = DatabaseAccessor(Perf, rand_db) rand_data = db.read() db = DatabaseAccessor(Perf, adaboost_db) adaboost_data = db.read() save_fig(rand_data, adaboost_data, filename, ttl, x_lim, y_lim)
def graph(db_file, filename, ttl): db = DatabaseAccessor(Perf, db_file) perf_data = db.read() save_fig(perf_data, filename, ttl)
class RestaurantChooserDatabase(object): def __init__(self, init_db=False): """ Initializes the Restaurant Chooser Database object, which allows a user to interact with the database associated with the application. :param init_db: If true, the database will be deleted and then recreated with the default values set. Set to False by default. """ self._db = DatabaseAccessor() if (init_db): clear_data( ) # Clears all data from the app data directory, including the database file self._db.create_connection( ) # Creates a new database file if it doesn't exist) if (init_db): self._generate_initial_database_structure() def _generate_initial_database_structure(self): """ Creates the database tables and inserts the initial default values into them. """ # Generates tables for database if they do not already exist for create_table_sql in CREATE_TABLES: self._db.execute(create_table_sql) # Inserts the default table values for insert_sql in INITIAL_VALUE_INSERTS: self._db._write_query(insert_sql) def get_all(self, table_name): """ Gets all items from the given table. :param table_name: Name of the table where data is being retrieved from. :return: List of dictionaries for every queried item. """ if (table_name == "cuisine_types"): return [ CuisineTypeModel(*param).parameters for param in self._db.select_all_query(table_name) ] elif (table_name == "item_types"): return [ ItemTypeModel(*param).parameters for param in self._db.select_all_query(table_name) ] elif (table_name == "list_items"): return [ ListItemModel(*param).parameters for param in self._db.select_all_query(table_name) ] elif (table_name == "restaurants"): return [ RestaurantModel(*param).parameters for param in self._db.select_all_query(table_name) ] elif (table_name == "schedules"): return [ ScheduleModel(*param).parameters for param in self._db.select_all_query(table_name) ] elif (table_name == "users"): return [ UserModel(*param).parameters for param in self._db.select_all_query(table_name) ] else: raise NotImplementedError( "No implementation for given table name.") def get_by_id(self, table_name, id): """ Gets an item with the given id from the given table. :param table_name: Name of the table where data is being retrieved from. :param id: Id of the item being queried. :return: Dictionary containing parameters for the queried item. """ try: items = self._db.select_all_query(table_name, {"id": id}) if (table_name == "cuisine_types"): return CuisineTypeModel( *items[0]).parameters if (len(items) > 0) else None elif (table_name == "item_types"): return ItemTypeModel( *items[0]).parameters if (len(items) > 0) else None elif (table_name == "list_items"): return ListItemModel( *items[0]).parameters if (len(items) > 0) else None elif (table_name == "restaurants"): return RestaurantModel( *items[0]).parameters if (len(items) > 0) else None elif (table_name == "schedules"): return ScheduleModel( *items[0]).parameters if (len(items) > 0) else None elif (table_name == "users"): return UserModel( *items[0]).parameters if (len(items) > 0) else None else: raise NotImplementedError( "No implementation for given table name.") # Catches error where fatchall() is attempted on None - happens when invalid table name is passed except AttributeError: raise ValueError("Given table name doesn't exist within database.") def insert_item(self, table_name, parameters): """ Inserts item with given parameters into the given table within the database. :param table_name: Name of the table where data is being inserted into. :param parameters: Dictionary containing the values to be inserted into the given table. :return: Dictionary of parameters with database id added. """ id = self._db.insert_query(table_name, parameters) parameters["id"] = id return parameters def update_item(self, table_name, id, parameters): """ Updates item within given table with new parameters. :param table_name: Name of the table where data is being updated. :param id: Identifier used to determine which item to update within the database. :param parameters: Dictionary containing the column name->value mappings for data to be updated. :return: Dictionary containing parameters for the updated item after update. """ self._db.update_query(table_name, parameters, {"id": id}) return self.get_by_id(table_name, id) def delete_item(self, table_name, id): """ Deletes item with given id from the table. :param table_name: Name of the table where data is being deleted. :param id: Identifier used to determine which item to delete within the given table. """ if (self.get_by_id(table_name, id)): self._db.delete_query(table_name, {"id": id}) else: raise Exception( "Illegal deletion attempted: record with id of {} cannot be found in the database." .format(id)) # Item List functions def _get_item_list(self, item_type): """ Gets the list of items of the given type. :param item_type: Name of the item type. :return: List of items of the given type. """ sql = "SELECT * FROM list_items WHERE item_type_id IN (SELECT id FROM item_types WHERE item_type='{}')".format( item_type) return self._db._read_query(sql) def get_states(self): """ Gets the lists of states. :return: List of dictionaries containing data for each state. """ return [ ListItemModel(*param).parameters for param in self._get_item_list(STATE_ITEM_TYPE) ] def get_weekdays(self): """ Gets the lists of weekdays. :return: List of dictionaries containing data for each weekday. """ return [ ListItemModel(*param).parameters for param in self._get_item_list(WEEKDAY_ITEM_TYPE) ] # Other functions def close_connection(self): """ Closes the connection to the SQLite database in order to free up resources. """ self._db.conn.close()
def add_all_folders_to_python_path(): sys.path.append("./database") sys.path.append("./computations") sys.path.append("./computations/comp_utils") sys.path.append("./comp_utils") add_all_folders_to_python_path() from database.DatabaseAccessor import * def current_time_millis(): return int(round(time.time() * 1000)) Constants.DATABASE_NAME = 'keyboard-recorder.db' da = DatabaseAccessor() if __name__ == '__main__': session_id = da.increment_and_get_session_id() print(session_id) while True: raw_input('') millis = current_time_millis() print(millis), da.insert_ball_lap_times(session_id, millis) da.close()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.model, self.tokenizer = load_model_and_tokenizer(config["eval"]["model_path"]) self.database_accessor = DatabaseAccessor()
class Bot(discord.Client): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.model, self.tokenizer = load_model_and_tokenizer(config["eval"]["model_path"]) self.database_accessor = DatabaseAccessor() async def on_ready(self): print("Ready, logged in as {}".format(self.user)) self.update_status.start() @tasks.loop(minutes=config["bot"]["update_status_interval_mins"]) async def update_status(self): print("update") await self.change_presence(activity=discord.Game(name=",help | {}".format(random.choice(status_messages)))) async def on_message(self, message: discord.Message): print("Received message from {}: {}".format(message.author.name, message.content)) channel = message.channel # check for prefix/eagerness/interactivity/since_last_reply/last_forget if channel.type == discord.ChannelType.text: # create guild if doesn't exist if not self.database_accessor.guild_exists(channel.guild.id): print("no guild, creating") self.database_accessor.add_guild(channel.guild.id, channel.guild.name) # create chat if doesn't exist if not self.database_accessor.chat_exists(channel.id): print("no chat, creating") self.database_accessor.add_chat(channel.id, True, "text", channel.guild.id, channel.name) guild_obj = self.database_accessor.get_guild_by_id(channel.guild.id) chat_obj = self.database_accessor.get_chat_by_id(channel.id) if chat_obj.override_chat_settings: eagerness, interactivity = chat_obj.eagerness, chat_obj.interactivity else: eagerness, interactivity = guild_obj.eagerness, guild_obj.interactivity prefix = guild_obj.prefix elif channel.type in (discord.ChannelType.private, discord.ChannelType.group): # create chat if doesn't exist if not self.database_accessor.chat_exists(channel.id): self.database_accessor.add_chat(channel.id, False, "private" if channel.type == discord.ChannelType.private else "group", None, channel.recipient.name if channel.type == discord.ChannelType.private else channel.name) chat_obj = self.database_accessor.get_chat_by_id(channel.id) eagerness, interactivity = chat_obj.eagerness, chat_obj.interactivity prefix = chat_obj.prefix else: raise NotImplementedError("Channel type not implemented: {}".format(str(channel.type))) since_last_reply = chat_obj.since_last_reply last_forget = chat_obj.last_forget print("prefix: {} eagerness: {} interactivity: {} since_last_reply: {} last_forget: {}" .format(prefix, eagerness, interactivity, since_last_reply, last_forget)) # Commands force_reply = False if message.content.startswith(prefix): command = message.content[len(prefix):].strip() split_command = command.split(" ") keyword = split_command[0] clean_prefix = prefix + " " if prefix[-1] in string.ascii_letters else prefix if keyword == "forcereply": force_reply = True elif keyword == "help": embed = discord.Embed(title="Help", description=help_text.format(clean_prefix)) await channel.send(embed=embed) return elif keyword == "forget": await channel.send("^^I have forgotten everything before this message!") self.database_accessor.reset_last_forget_chat(channel.id) return elif keyword == "options": if len(split_command) <= 1: await channel.send("^^Command not recognized. Please type `{0}help` for more info.".format(clean_prefix)) if split_command[1] == "prefix": new_prefix = " ".join(split_command[2:]) if len(new_prefix) > 8: await channel.send("^^Prefix is too long. Max length is 8 characters.") elif len(new_prefix) == 0: await channel.send("^^Prefix cannot be empty!") else: if channel.type == discord.ChannelType.text: self.database_accessor.set_guild_prefix(channel.guild.id, new_prefix) else: self.database_accessor.set_chat_prefix(channel.id, new_prefix) await channel.send("^^Success! Prefix changed to `{0}`".format(new_prefix)) elif split_command[1] in ("mode", "smode"): if split_command[1] == "smode" and channel.type != discord.ChannelType.text: await channel.send("^^This command is only available on servers.") return if len(split_command) == 2: await channel.send("^^Command options not recognized. Please type `{0}help` for more info.".format(clean_prefix)) elif len(split_command) == 3: if split_command[2] == "default" and split_command[1] == "mode": if channel.type == discord.ChannelType.text: self.database_accessor.set_chat_override(channel.id, False) await channel.send("^^Success! Options reset to server default.") else: await channel.send("^^This command is only available on servers.") elif split_command[2] == "get": if split_command[1] == "mode": e, i = eagerness, interactivity else: e, i = guild_obj.eagerness, guild_obj.interactivity for preset, values in conversation_presets.items(): if (e, i) == values: await channel.send("^^This {}'s mode is currently: {} (eagerness {}, interactivity {})" .format("channel" if split_command[1] == "mode" else "server", preset, e, i)) break else: await channel.send("^^This {}'s mode is currently: eagerness {}, interactivity {}" .format("channel" if split_command[1] == "mode" else "server", e, i)) else: for preset, values in conversation_presets.items(): if split_command[2] == preset: if split_command[1] == "mode": self.database_accessor.set_chat_eagerness_interactivity(channel.id, *values) if channel.type == discord.ChannelType.text: self.database_accessor.set_chat_override(channel.id, True) await channel.send("^^Success! Channel options set to {}.".format(preset)) else: self.database_accessor.set_guild_eagerness_interactivity(channel.guild.id, *values) await channel.send("^^Success! Server options set to {}.".format(preset)) break else: await channel.send("^^Command options not recognized. Please type `{0}help` for more info.".format(clean_prefix)) else: try: eagerness = float(split_command[2]) interactivity = float(split_command[3]) if not (0 <= eagerness <= 1000 and 0 <= interactivity <= 1000): await channel.send("^^Eagerness and interactivity values must be between 0 and 1000 inclusive.") else: if split_command[1] == "mode": self.database_accessor.set_chat_eagerness_interactivity(channel.id, eagerness, interactivity) if channel.type == discord.ChannelType.text: self.database_accessor.set_chat_override(channel.id, True) await channel.send("^^Success! Channel eagerness set to {} and interactivity set to {}.".format(eagerness, interactivity)) else: self.database_accessor.set_guild_eagerness_interactivity(channel.guild.id, eagerness, interactivity) await channel.send("^^Success! Server eagerness set to {} and interactivity set to {}.".format(eagerness, interactivity)) except ValueError: await channel.send("^^Command options not recognized. Please type `{0}help` for more info.".format(clean_prefix)) else: await channel.send("^^Command options not recognized. Please type `{0}help` for more info.".format(clean_prefix)) return else: await channel.send("^^Command options not recognized. Please type `{0}help` for more info.".format(clean_prefix)) return else: if message.author.id == self.user.id or message.author.bot or re.match(likely_command_regex, message.content): print("Skipping") return history = await channel.history(limit=config["bot"]["history_limit"]).flatten() history.reverse() # filter bots except itself, and likely commands history = filter(lambda x: (not x.author.bot or x.author.id == self.user.id) and not re.match(likely_command_regex, x.content) and (last_forget is None or x.created_at > last_forget), history) history = list(map(lambda x: (x.author.id == self.user.id, x.content), history)) print("History length: {}".format(len(history))) reply_chance = chance_reply(history, self.tokenizer, self.model, torch.device(config["bot"]["device"])) # probability = 1 - np.exp(-interactivity * since_last_reply) * ((1 - reply_chance) ** eagerness) probability = 1 - (1 - reply_chance) ** (eagerness + since_last_reply * interactivity) print("Chance: {:.03f} Probability: {:.03f}".format(reply_chance, probability)) if force_reply or np.random.binomial(1, probability): async with channel.typing(): print("Replying") reply = generate_from_history(history, self.tokenizer, self.model, torch.device(config["bot"]["device"]), token_blacklist=[photo, call, video, voice, sticker]) for x in reply: await channel.send(x) self.database_accessor.set_since_last_reply(channel.id, 0) else: print("Not replying") self.database_accessor.increment_last_reply(channel.id)