def get_numpy_drawings_list(reduced_set=None, data_type="train", class_list=None): numpy_drawings_list = [] nl = len(QUICK_DRAW_LABELS) logger.info("Number of QUICK_DRAW_LABELS: {0}".format(nl)) for i in range(nl): l = QUICK_DRAW_LABELS[i] if data_type == "train": class_numpy_drawings_list = glob.glob( "train_data/class_{0}_{1}x{1}*.npy".format( l, REDUCED_DATA_IMAGE_SIZE)) else: class_numpy_drawings_list = glob.glob( "test_data/class_{0}_{1}x{1}*.npy".format( l, REDUCED_DATA_IMAGE_SIZE)) if reduced_set is not None: class_numpy_drawings_list = class_numpy_drawings_list[ 0:reduced_set] numpy_drawings_list = numpy_drawings_list + class_numpy_drawings_list return numpy_drawings_list
def remake_db(): db.session.close_all() db.drop_all() db.create_all() first_admin = Users(username='******', email=os.getenv('ADMIN_EMAIL'), password=(bcrypt.generate_password_hash( os.getenv('ADMIN_PW')).decode('utf-8')), admin=True) mock_booking_1 = Bookings( requester=first_admin, query='jump bikes', human_readable_address=( '2200 Jerrold Ave, San Francisco, CA 94124, USA'), matched_bike_address='2218 Jerrold Ave, San Francisco, CA', latitude=37.7458634, longitude=-122.4021239, status='booked', created_at=datetime.utcnow() - timedelta(days=370, minutes=1300)) mock_booking_2 = Bookings( requester=first_admin, query='google hq', human_readable_address='345 Spear St, San Francisco, CA 94105, USA', latitude=37.79005, longitude=-122.39019, status='error', created_at=datetime.utcnow() - timedelta(minutes=5)) db.session.add(first_admin) db.session.add(mock_booking_1) db.session.add(mock_booking_2) db.session.commit() logger.info('Succesfully recreated the database.')
def main(): logger.info('Start execution') bot.send_message('Start execution') t_image = target_image.TargetImage() t_image.run()
def __init__(self, input_shape, n_classes, n_channels=1): self.n_rows = input_shape[0] self.n_cols = input_shape[1] self.n_channels = n_channels self.n_classes = n_classes logger.info("imput_shape: {0}".format(input_shape)) self.inputs = Input(shape=(self.n_rows, self.n_cols, n_channels)) x = Conv2D(32, kernel_size=(3, 3), activation='relu', data_format="channels_last", name="first_layer")(self.inputs) x = Conv2D(64, (3, 3), activation='relu')(x) x = MaxPooling2D(pool_size=(2, 2))(x) x = Dropout(0.25)(x) x = Flatten()(x) x = Dense(128, activation='relu')(x) x = Dropout(0.5)(x) self.outputs = Dense(n_classes, activation='sigmoid')(x) self.model = Model(inputs=[self.inputs], outputs=[self.outputs]) self.model.compile(loss=keras.losses.binary_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])
def image_cb(self, msg): """Grab the first incoming camera image and saves it Args: msg (Image): image from car-mounted camera """ # unregister the subscriber to throttle the images coming in # if self.sub_raw_camera is not None: # self.sub_raw_camera.unregister() # self.sub_raw_camera = None # fixing convoluted camera encoding... if hasattr(msg, 'encoding'): if msg.encoding == '8UC3': msg.encoding = "rgb8" else: msg.encoding = 'rgb8' self.camera_image = self.bridge.imgmsg_to_cv2(msg, "rgb8") self.camera_img_idx += 1 self.camera_image, traffic_light = recognize_traffic_lights( self.camera_image) logger.info('Traffic Light: %s' % traffic_light) # Save the camera image camera_image = cv2.cvtColor(self.camera_image, cv2.COLOR_BGR2RGB) cv2.imwrite('src/tools/traffic_lights/%s.png' % self.camera_img_idx, camera_image)
def waypoints_cb(self, msg): logger.info('WayPoints Callback called') if self.waypoints is None: self.waypoints = [] for waypoint in msg.waypoints: self.waypoints.append(waypoint) # create the polyline that defines the track x = [] y = [] for i in range(len(self.waypoints)): x.append(self.waypoints[i].pose.pose.position.x) y.append(self.screen.height - (self.waypoints[i].pose.pose.position.y - self.screen.canvas.height)) self.XYPolyline = np.column_stack((x, y)).astype(np.int32) # just need to get it once self.sub_waypoints.unregister() self.sub_waypoints = None # initialize lights to waypoint map self.initialize_light_to_waypoint_map() self.waypoints_initialized = True
def reading_speed(file_extension): file_list = glob.glob("../train_data/*." + file_extension) n = len(file_list) start = time.time() for i in range(n): if file_extension == "npz": d = np.load(file_list[i]) four_channel_img = d["four_channel_img"] label = d["label"] elif file_extension == "h5": h5f = h5py.File(file_list[i], "r") four_channel_img = h5f["four_channel_img"][:, :, :] label = h5f["four_channel_img"].attrs["label"] elif file_extension == "npy": label = train_file_name_to_label(file_list[i]) four_channel_img = np.load(file_list[i]) else: assert False, "Wrong file extension!" stop = time.time() logger.info("Reading time of {0}: {1}".format(file_extension, stop - start))
def get_data_files(file_type="ndjson", data_type="train", color="green"): search_string = "../{0}/*_{1}.{2}".format(data_type, color, file_type) logger.info("Looking for {0}".format(search_string)) data_files = glob.glob(search_string) return data_files
def send_message(self, message): logger.info('Send message: ' + message) chat_id = self._get_last_chat_id() if chat_id: self._bot.send_message(chat_id=self._get_last_chat_id(), text=message) else: logger.warning('chat id is empty')
def save_game(current_map): """saves game to "save.txt" Arguments: current_map {list} -- current game map state """ Pickler(open("save.txt", 'wb')).dump(current_map) log.info("game saved!")
def print_state(self): x = character.position[0] y = character.position[1] for i in range(x - 1, x + 2): for j in range(y - 1, y + 2): if game_terrain.terrain[i][j] == '⭐': log.info("treasure is near") elif game_terrain.terrain[i][j] == '◉': log.info("trap is near")
def autoimport(package_name): try: __import__(package_name) except ModuleNotFoundError as e: logger.error(e) logger.info('Trying to install ' + package_name + ' package') subprocess.check_call( [sys.executable, '-m', 'pip', 'install', package_name]) __import__(package_name) logger.info('Package ' + package_name + ' is installed')
def save_game(current_map): """saves game to "save.txt" Arguments: current_map {list} -- current game map state """ try: file = open("save.txt", 'rb') except IOError: log.info("unable to save game") else: Pickler(file).dump(current_map) log.info("game saved!")
def eval(self, x_test_file_list, y_test_labels_list, batch_size, steps): # score = self.model.evaluate(x_test, y_test, verbose=0) # logger.info("Your model scored: {0}".format(score)) # return score score = self.model.evaluate_generator(self._generate_data_from_files( x_data_file_list=x_test_file_list, y_data_labels_list=y_test_labels_list, batch_size=batch_size), steps=steps) logger.info("Your model scored: {0}".format(score))
def load_game(): """loads game map if file "save.txt" exists Returns: list -- saved game map """ if isfile("save.txt"): save = load(open("save.txt", 'rb')) log.info("game loaded") return save else: log.info("unable to load game")
def load_game(): """loads game map if file "save.txt" exists Returns: list -- saved game map """ try: file = open("save.txt", 'rb') except IOError: log.info("unable to load game") else: save = load(file) log.info("game loaded") return save
def list_closest_bikes(latitude, longitude): """ Fetches the 10 closest bikes of the provided coordinates """ list_url = (f'{BASE_URL}/bikes.json?' 'per_page=10&sort=distance_asc' f'&latitude={latitude}' f'&longitude={longitude}') logger.info(f'GET - {list_url}') r = requests.get(list_url, headers=HEADERS) if r.status_code < 400 and r.status_code >= 200: return r.json().get('items') logger.error(f'Request did not go through (code {r.status_code}):\n' f'{r.text}') return None
def convert_ndjson_simplified_data_into_numpy_arrays(ndjson_csv_file_list, drawings_per_file=100, N_DRAWINGS_PER_ARRAY=10): n_files = len(ndjson_csv_file_list) for i in range(n_files): start = time.time() logger.info("File number: {0}".format(i)) convert_images_from_ndjson_file_into_numpy_arrays_and_save( ndjson_csv_file_list[i], drawings_per_file, N_DRAWINGS_PER_ARRAY) end = time.time() logger.info("Elapsed time: {0} s\n".format(end - start))
def _update_thread(self, dummy): while True: current_time = datetime.datetime.now().time() try: self._last_message = self._bot.get_updates()[-1].message.text except: logger.error('Exception during updating telegram data') pass try: cod = self._last_message.encode('utf8', 'ignore') # logger.info('Last message: ' + self._last_message) logger.info('Last message: ' + cod.decode()) except: logger.error('Coding error') logger.info(self._last_message) pass if m_exit in self._last_message and not self._is_exit: self._is_exit = True GlobalData.telegram_closed = True logger.info('Get exit message') break if m_hello in self._last_message and not self._is_hello: self._is_hello = True self.send_message(m_hello) if m_tam in self._last_message and not self._h: self._h = True self.send_message(m_all_ok) if 'Шо на таргеті?' in self._last_message and self._last_message != self._last_checked_message: ver = TargetVersion() # qq = ver.get_version() # self.send_message(qq) try: qq = ver.get_version() self.send_message(qq) except: logger.error('FTP conenction error') self.send_message('FTP conenction error') if m_run_tests in self._last_message and not self._is_tests_running: logger.info('Start executing test runner') self._is_tests_running = True self.send_message(m_ok) test_thread = Thread(target=self._test_thread, args=(10, )) test_thread.start() self._last_checked_message = self._last_message time.sleep(5)
def book_bike(bike): """ Attempt to book a bike. Return True or False depending on the success. """ try: book_url = f'{BASE_URL}/bikes/{bike["id"]}/book_bike.json' logger.info(f'POST - {book_url}') r = requests.post(book_url, headers=HEADERS) if r.status_code >= 200 and r.status_code < 400: logger.info(f'Succesfully booked bike {bike["name"]}') return True else: logger.error(f'{r.status_code} - {r.text}') return False except Exception as e: logger.exception(e) return False
def schedule_trip(booking_id, email): booking = db.session.query(Bookings).get(booking_id) # Todo: handle auto-booking if booking.status == 'error': return Response(response='Error', status=429) logger.info(f'Searching bikes around {booking.human_readable_address}') candidate_bike = find_best_bike(booking, attempt=1) email_response = send_email(booking, email) if email_response.status_code < 400: logger.info('Email successfully sent') else: logger.warn(f'Email not sent (status {email_response.status_code}') if not candidate_bike: socket.emit('booked', {'status': 'warning'}, namespace=f'/booking_{booking.id}') return Response(response='No bike around', status=404) socket.emit('booked', { 'address': booking.matched_bike_address, 'bike_name': booking.matched_bike_name, 'status': 'success' }, namespace=f'/booking_{booking.id}') if ENV != 'dev': book_bike(candidate_bike) booking.status = 'booked' db.session.add(booking) db.session.commit() return Response(response='Booked', status=200) logger.warn( f'Would have booked bike {candidate_bike["name"]} in production') return Response(response='Gotem', status=200)
def read_npy_drawing_file_lists_and_return_data_array( x_npy_drawing_file_list, y_npy_drawing_labels_list, le, number_of_classes): """ This function is used mainly to prepare the data for the evaulate/predict methods of the model. """ logger.info( "Reading data from *npy files and packing them into one big numpy array." ) n_x = len(x_npy_drawing_file_list) n_y = len(y_npy_drawing_labels_list) assert n_x == n_y, "x and y dimensions do not match!" x_drawings = np.zeros((n_x, REDUCED_DATA_IMAGE_SIZE, REDUCED_DATA_IMAGE_SIZE, NUMBER_IMAGE_OF_CHANNELS)) y_labels = np.zeros((n_y, number_of_classes)) for i in range(n_x): x = np.load(x_npy_drawing_file_list[i]).reshape( (REDUCED_DATA_IMAGE_SIZE, REDUCED_DATA_IMAGE_SIZE, NUMBER_IMAGE_OF_CHANNELS)) rm = NPY_FILE_REXEXP.match(x_npy_drawing_file_list[i]) assert rm, "Regexp not matched!" l = rm.group("drawing_name") label = le.transform([l]) logger.debug("label: {0}, expected label: {1}".format( label, y_npy_drawing_labels_list[i])) assert label == y_npy_drawing_labels_list[i], "Labels do not match!" x_drawings[i, :, :, :] = x y_labels[i, y_npy_drawing_labels_list[i]] = 1.0 return x_drawings, y_labels
def cancel_rental(): """ Cancel the current active rental. """ try: cancel_url = f'{BASE_URL}/rentals/cancel.json' logger.info(f'POST - {cancel_url}') r = requests.delete(cancel_url, headers=HEADERS) if r.status_code >= 200 and r.status_code < 400: logger.info(f'Succesfully cancelled rental.') return { 'message': 'Successfully cancelled rental!', 'category': 'success' } else: logger.warn(f'{r.status_code} - {json.loads(r.text)}') return { 'message': json.loads(r.text).get('error'), 'category': 'info' } except Exception as e: logger.exception(e) return {'message': 'Internal error', 'category': 'error'}
def compare_features_between_test_and_train_data(train_features, test_features): logger.info("Check train vs test features...") for key, val in sorted(train_features.items()): if key not in test_features: logger.info( "{0} is not present in test features but is present in train set" .format(key)) logger.info("Check test vs train features...") for key, val in sorted(test_features.items()): if key not in train_features: logger.info( "{0} is not present in train features but is present in test set" .format(key))
def find_best_bike(booking, attempt): """ Try MAX_ATTEMPTS time (spaced by 30 seconds) to find a close match. Return False if eventually no match. Return the closest bike available otherwise. """ bike_list = list_closest_bikes(booking.latitude, booking.longitude) if bike_list is None: return None valid_bike_list = [bike for bike in bike_list if is_valid(bike)] if not valid_bike_list: if attempt >= MAX_ATTEMPTS: logger.warn(f'No bikes found after {attempt} attempts') booking.status = 'not found' db.session.add(booking) db.session.commit() return False logger.warn('No bikes found nearby yet.') socket.sleep(RETRY_DELAY) return find_best_bike(booking, attempt + 1) logger.info(f'Found {len(valid_bike_list)} bikes matching criteria, ' 'selecting the closest one.') best_bike = min(valid_bike_list, key=lambda bike: bike['distance']) logger.info(f'Closest bike located at {best_bike["address"]}.') booking.matched_bike_address = best_bike['address'] booking.matched_bike_name = best_bike['name'] booking.status = 'match found' db.session.add(booking) db.session.commit() return best_bike
def move_enemy(self): while not self.is_over(): move = choice(enemy.moves) while game_terrain.terrain[enemy.position[0] + move[0]][enemy.position[1] + move[1]] == '⏹': move = choice(enemy.moves) game_terrain.terrain[enemy.position[0]][ enemy.position[1]] = enemy.previous_cell enemy.position[0] += move[0] enemy.position[1] += move[1] enemy.previous_cell = game_terrain.terrain[enemy.position[0]][ enemy.position[1]] if enemy.position[0] == character.position[0] and enemy.position[ 1] == character.position[1]: character.current_hp -= 1 log.info( f"You've found a {'👾'} \n{character.current_hp} / 3 hp") game_terrain.respawn_enemy() if character.current_hp == 0: self.game_over_event.set() log.info("\nyou lost \n press any button") else: game_terrain.terrain[enemy.position[0]][ enemy.position[1]] = '👾' log.info("enemy moved somewhere") x = enemy.position[0] y = enemy.position[1] for i in range(x - 1, x + 2): for j in range(y - 1, y + 2): if game_terrain.terrain[i][j] == '웃': log.info("enemy is near") sleep(3)
def stop_line_position_cb(self, msg): self.stop_line_position = msg.data logger.info("Stop Line position: %s" % msg.data)
def main(options): logger.debug("Storing files into %s", options.output) if not os.path.isdir(options.output): os.mkdir(options.output) aemet_scraper = parser.MainParser( base_output_path=options.output, current_time=time.strftime("%Y%m%d%H00"), file_format=options.format ) aemet_scraper.download_data() if __name__ == '__main__': options = parse_options() logger.configure_logger(options) exitcode = 0 logger.info("Start") try: main(options) except Exception: logger.exception("Unknown error when getting AEMET data.") exitcode = 1 logger.info("Done") sys.exit(exitcode)
def __init__(self, input_shape, n_classes, n_channels=1): self.n_rows = input_shape[0] self.n_cols = input_shape[1] self.n_channels = n_channels self.n_classes = n_classes logger.info("input_shape: {0}".format(input_shape)) self.inputs = Input(shape=(self.n_rows, self.n_cols, n_channels)) # --- Block 1 --- x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name='block1_conv1')(self.inputs) x = BatchNormalization(name='bl1_cn1_bn')(x) x = Activation('relu', name='bl1_cn1_act')(x) logger.info("bl1_cn1 shape: {0}".format(K.int_shape(x))) x = Conv2D(64, (3, 3), use_bias=False, name='bl1_cn2')(x) x = BatchNormalization(name='bl1_cn2_bn')(x) x = Activation('relu', name='bl1_cn2_act')(x) logger.info("bl1_cn2 shape: {0}".format(K.int_shape(x))) residual = Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) # --- Block 2 --- x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='bl2_scn1')(x) x = BatchNormalization(name='bl2_scn1_bn')(x) x = Activation('relu', name='bl2_scn2_act')(x) x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='bl2_scn2')(x) x = BatchNormalization(name='bl2_scn2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='bl2_pool')(x) x = layers.add([x, residual]) residual = Conv2D(256, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) # --- Block 3 --- x = Activation('relu', name='bl3_scn1_act')(x) x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='bl3_scn1')(x) x = BatchNormalization(name='bl3_scn1_bn')(x) x = Activation('relu', name='bl3_scn2_act')(x) x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='bl3_scn2')(x) x = BatchNormalization(name='bl3_scn2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='bl3_pool')(x) x = layers.add([x, residual]) residual = Conv2D(728, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) # --- Block 4 --- x = Activation('relu', name='bl4_scn1_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='bl4_scn1')(x) x = BatchNormalization(name='bl4_scn1_bn')(x) x = Activation('relu', name='bl4_scn2_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='bl4_scn2')(x) x = BatchNormalization(name='bl4_scn2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='bl4_pool')(x) x = layers.add([x, residual]) for i in range(8): residual = x prefix = 'block' + str(i + 5) x = Activation('relu', name=prefix + '_scn1_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv1')(x) x = BatchNormalization(name=prefix + '_scn1_bn')(x) x = Activation('relu', name=prefix + '_scn2_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv2')(x) x = BatchNormalization(name=prefix + '_scn2_bn')(x) x = Activation('relu', name=prefix + '_scn3_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv3')(x) x = BatchNormalization(name=prefix + '_scn3_bn')(x) x = layers.add([x, residual]) residual = Conv2D(1024, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x) residual = BatchNormalization()(residual) # --- Block 13 --- x = Activation('relu', name='bl13_scn1_act')(x) x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='bl13_scn1')(x) x = BatchNormalization(name='bl13_scn1_bn')(x) x = Activation('relu', name='bl13_scn2_act')(x) x = SeparableConv2D(1024, (3, 3), padding='same', use_bias=False, name='bl13_scn2')(x) x = BatchNormalization(name='bl13_scn2_bn')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block13_pool')(x) x = layers.add([x, residual]) # --- Block 14 --- x = SeparableConv2D(1536, (3, 3), padding='same', use_bias=False, name='block14_sepconv1')(x) x = BatchNormalization(name='bl14_scn1_bn')(x) x = Activation('relu', name='bl14_scn1_act')(x) x = SeparableConv2D(2048, (3, 3), padding='same', use_bias=False, name='bl14_scn2')(x) x = BatchNormalization(name='bl14_scn2_bn')(x) x = Activation('relu', name='bl14_scn2_act')(x) x = GlobalAveragePooling2D(name='avg_pool')(x) self.outputs = Dense(n_classes, activation='softmax', name='predictions')(x) # x = Conv2D(64, (3, 3), activation='relu')(x) # x = MaxPooling2D(pool_size=(2, 2))(x) # x = Dropout(0.25)(x) # x = Flatten()(x) # x = Dense(128, activation='relu')(x) # x = Dropout(0.5)(x) # self.outputs = Dense(n_classes, activation='softmax')(x) self.model = Model(inputs=[self.inputs], outputs=[self.outputs]) self.model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])
def stop(self): self.stop_thread = True logger.info('Stopping SystemScanner')
def perform_player_action(self): log.info("enter your action :") log.info(self.player_actions) action = input() new_player_position = list(character.position) result = True try: if action in self.player_actions: if action == 'up': new_player_position[0] -= 1 elif action == 'down': new_player_position[0] += 1 elif action == 'right': new_player_position[1] += 1 elif action == 'left': new_player_position[1] -= 1 elif action == 'save': save_game(game_terrain.terrain) elif action == 'load': game_terrain.terrain = load_game() if game_terrain.terrain[new_player_position[0]][new_player_position[1]] == WALL: log.info("you found the wall. Can't move further") elif game_terrain.terrain[new_player_position[0]][new_player_position[1]] == '⭐': character.current_backpack += 1 log.info( f"You've found a treasure ⭐ \n {character.current_backpack} / 3 total") game_terrain.terrain[character.position[0] ][character.position[1]] = '🟋' character.position = new_player_position game_terrain.terrain[character.position[0] ][character.position[1]] = '웃' elif game_terrain.terrain[new_player_position[0]][new_player_position[1]] == '◉': character.current_hp -= 1 log.info( f"You've found a trap ◉ \n{character.current_hp} / 3 hp") game_terrain.terrain[character.position[0] ][character.position[1]] = '🟋' character.position = new_player_position game_terrain.terrain[character.position[0] ][character.position[1]] = '웃' else: game_terrain.terrain[character.position[0] ][character.position[1]] = '🟋' character.position = new_player_position game_terrain.terrain[character.position[0] ][character.position[1]] = '웃' if character.current_hp == 0: result = False log.info("\nyou lost") elif character.current_backpack == 3: result = False log.info("\nyou won") else: raise PlayerInputError(action) except: log.info("wrong input") finally: return result