Exemple #1
0
def connect_db():
    """ Returns an interface to the database
        made using setings in app.config
    """
    return db_interface.db_interface(host=app.config['DB_HOST'],
                                     dbname=app.config['DB_NAME'],
                                     user=app.config['DB_USER'],
                                     password=app.config['DB_PASSWORD'])
Exemple #2
0
def connect_db():
    """ Returns an interface to the database
        made using setings in app.config
    """
    return db_interface.db_interface(host=app.config['DB_HOST'],
                                     dbname=app.config['DB_NAME'],
                                     user=app.config['DB_USER'],
                                     password=app.config['DB_PASSWORD'])
Exemple #3
0
def train(args):
    s = time.time()
    tr_data = pickle.load(open(args.train_file))
    vd_data = pickle.load(open(args.validation_file))
    init_wts = None
    hidden_units = None
    if args.init_wt_file: init_wts = pickle.load(open(args.init_wt_file))
    if args.hidden_units: hidden_units = args.hidden_units
    
    nnet = network(args.actv)
    db = db_interface(args.model_perf_db)
    db.create_table()
    theta = nnet.train(db, tr_data['X'],
            tr_data['Y'], vd_data['X'], vd_data['Y'], args.hidden_units,
            init_wts, args.mini_batch_size, args.epochs, args.validation_freq)
    print 'Training time:', time.time() - s, 'seconds'
    pickle.dump(theta, open(args.model_file, 'wb'))
Exemple #4
0
    def OnRefreshMessages(self, widget):
        db_api = db_interface()
        now = datetime.datetime.now()
        new_msgs = db_api.get_new_columns(self.last_update_time, now)
        self.messages_list.clear()
        for msg in new_msgs:
            """messages schema: 
                                msg_id INT PRIMARY KEY,
                                time_sent TEXT NOT NULL,
                                msg TEXT, 
                                sender_uid TEXT NOT NULL, 
                                tag TEXT 

                interface schema: To, From, Message, Time

            """
            # KEVIN
            msg_row = ["me", str(msg[3]), str(msg[2]), str(msg[1])]
            print msg_row
            self.messages_list.append(msg_row)
Exemple #5
0
    def OnRefreshMessages(self, widget):
        db_api = db_interface()
        now = datetime.datetime.now()
        new_msgs = db_api.get_new_columns(self.last_update_time, now)
        self.messages_list.clear()
        for msg in new_msgs:
            """messages schema: 
                                msg_id INT PRIMARY KEY,
                                time_sent TEXT NOT NULL,
                                msg TEXT, 
                                sender_uid TEXT NOT NULL, 
                                tag TEXT 

                interface schema: To, From, Message, Time

            """
            # KEVIN
            msg_row = ["me", str(msg[3]), str(msg[2]), str(msg[1])]
            print msg_row
            self.messages_list.append(msg_row)
Exemple #6
0
    def OnSendChat(self, widget):
        # void gtk_entry_set_text (GtkEntry *entry, const gchar *text);
        sendto = self.sendto_entry.get_text()
        message = self.message_entry.get_text()
        # self.sendto_entry.set_text("");
        self.message_entry.set_text("")
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        db_api = db_interface()
        # self.client_thread.communicate(str(sendto))
        # self.client_thread.communicate(str(message))

        # MY SCHEMA
        # def insert_new_message(self, msgid, message_text, to_user_id, from_user_id, time):
        # db_api.insert_new_message(str(randint(0,999999)), str(message), str(sendto), "MYSELF", str(now))

        # KEVIN SCHEMA
        db_api.insert_new_message(str(randint(0, 999999)), str(message),
                                  str(sendto), "me", str(now))
        # new_messages = db_api.insert_new_message(self.last_update_time, now)
        entry_list = [sendto, "me", message, now]
        self.messages_list.append(entry_list)
Exemple #7
0
    def OnSendChat(self, widget):
        # void gtk_entry_set_text (GtkEntry *entry, const gchar *text);
        sendto = self.sendto_entry.get_text()
        message = self.message_entry.get_text()
        # self.sendto_entry.set_text("");
        self.message_entry.set_text("");
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")



        db_api = db_interface()
        # self.client_thread.communicate(str(sendto))
        # self.client_thread.communicate(str(message))

        # MY SCHEMA
        # def insert_new_message(self, msgid, message_text, to_user_id, from_user_id, time):
        # db_api.insert_new_message(str(randint(0,999999)), str(message), str(sendto), "MYSELF", str(now))

        # KEVIN SCHEMA
        db_api.insert_new_message(str(randint(0,999999)), str(message), str(sendto), "me", str(now))
        # new_messages = db_api.insert_new_message(self.last_update_time, now)
        entry_list = [sendto, "me", message, now]
        self.messages_list.append(entry_list)
    cur.execute("""SELECT COUNT(*)
                   FROM crimes
                   WHERE crimes.vehicle='bicycle'
                   AND EXTRACT(YEAR FROM t) < 2013
                   AND ST_DWithin(""" + location + """, location, %s)""",
                (max_r, ))
    try:
        n_crimes = int(cur.fetchone()[0])
    except TypeError:
        n_crimes = 0

    return n_crimes/n_nearby

db = db_interface.db_interface(host=config.DB_HOST,
                               user=config.DB_USER,
                               dbname=config.DB_NAME,
                               password=config.DB_PASSWORD)
cur = db.cursor

# Pick half of the bike crimes this year to test on
cur.execute("""SELECT incident_num,
                      ST_X(ST_Transform(location, 4326)) AS lon,
                      ST_Y(ST_Transform(location, 4326)) AS lat,
                      CAST(EXTRACT(DOY FROM t) AS int) %2
               FROM crimes
               WHERE vehicle='bicycle'
               AND NOT at_police_station
               AND EXTRACT(YEAR FROM t) >= 2013 """)

max_d = float(402.336)
        """SELECT COUNT(*)
                   FROM crimes
                   WHERE crimes.vehicle='bicycle'
                   AND EXTRACT(YEAR FROM t) < 2013
                   AND ST_DWithin(""" + location + """, location, %s)""",
        (max_r, ))
    try:
        n_crimes = int(cur.fetchone()[0])
    except TypeError:
        n_crimes = 0

    return n_crimes / n_nearby


db = db_interface.db_interface(host=config.DB_HOST,
                               user=config.DB_USER,
                               dbname=config.DB_NAME,
                               password=config.DB_PASSWORD)
cur = db.cursor

# Pick half of the bike crimes this year to test on
cur.execute("""SELECT incident_num,
                      ST_X(ST_Transform(location, 4326)) AS lon,
                      ST_Y(ST_Transform(location, 4326)) AS lat,
                      CAST(EXTRACT(DOY FROM t) AS int) %2
               FROM crimes
               WHERE vehicle='bicycle'
               AND NOT at_police_station
               AND EXTRACT(YEAR FROM t) >= 2013 """)

max_d = float(402.336)