def test_tile_bad_parameters(client): ''' As a blackmagic user, when I don't send tx, ty, acquired, date & chips via HTTP POST the HTTP status is 400 and the response body tells me the required parameters so that I can send a good request. ''' tx = "not-an-integer" ty = test.ty acquired = test.acquired chips = test.chips date = test.training_date delete_tile(test.tx, test.ty) response = client.post('/tile', json={ 'tx': tx, 'ty': ty, 'acquired': acquired, 'chips': chips, 'date': date }) tiles = _ceph.select_tile(tx=test.tx, ty=test.ty) assert response.status == '400 BAD REQUEST' assert get('tx', response.get_json()) == tx assert get('ty', response.get_json()) == ty assert get('acquired', response.get_json()) == acquired assert get('date', response.get_json()) == date assert get('chips', response.get_json()) == count(chips) assert type(get('exception', response.get_json())) is str assert len(get('exception', response.get_json())) > 0 assert len(list(map(lambda x: x, tiles))) == 0
async def command_leave(self, message: discord.Message): lobby = await self.get_active_lobby() if lobby: user = await self.register_user(message.author) if lobby.creator_id == user.id: lobby.closed = datetime.utcnow() lobby.updated = datetime.utcnow() await self.database.update(lobby) await message.channel.send(f"<@{user.id}> closed the lobby") else: match_format = await self.get_active_lobby_format(lobby) match_players = await self.get_active_lobby_players(lobby) if toolz.count( filter(lambda x: x.player_id == user.id, match_players)): match_player = toolz.first( filter(lambda x: x.player_id == user.id, match_players)) await self.database.delete(match_player) await message.channel.send( f"<@{user.id}> has left the lobby ({len(match_players)-1}/{match_format.max_player})" ) else: await message.channel.send(f"<@{user.id}> isn't in a lobby" ) else: await message.channel.send( f"no lobby active, use `!create [MatchFormat]`")
def build_data() -> Tuple[np.ndarray, np.ndarray]: """Creates a set of scikit-learn-formatted training data, from processed images.""" num_samples = int(count(training_processed_path.iterdir())) data = np.zeros([num_samples, HEIGHT, WIDTH]) target = np.zeros(num_samples) for i, file in enumerate(training_processed_path.iterdir()): im = scipy.misc.imread(file) # todo temporary workaroudn for some images ending up 74 wide; add # todo extra column if im.shape[0] == HEIGHT - 1: extra_col = np.array([np.zeros(WIDTH)]) im = np.concatenate([im, extra_col]) data[i] = im if 'sct' in file.name: cat = 0 elif 'ovc' in file.name: cat = 1 else: raise AttributeError target[i] = cat # Flatten each image to a 1d array. data = data.reshape((num_samples, -1)) return data, target
def __curse_count(self): """ Count the number of items in sequence. Like the builtin ``len``. """ return cytoolz.count(self)
def unique_mentions_per_word(mentions, field): """Count of unique mentions per previous/next-word Parameters: mentions, list: a list of Mention objects field, string : can be one of `('previous_word', 'next_word')` Returns: a dictionary with words as keys and counts as values """ d = defaultdict(int) groups = cytoolz.groupby(lambda x: x[field], mentions) for k, g in groups.iteritems(): d[k] = count(unique(g, lambda x: x.text)) return d
def wrapper(*args, **kwargs): start = datetime.now() ctx = fn(*args, **kwargs) d = {"tx":get("tx", ctx, None), "ty":get("ty", ctx, None), "date":get("date", ctx, None), "acquired":get("acquired", ctx, None), "chips":count(get("chips", ctx, []))} logger.info(json.dumps(assoc(d, "{name}_elapsed_seconds".format(name=fn.__name__), (datetime.now() - start).total_seconds()))) return ctx
def exception_handler(ctx, http_status, name, fn): try: return fn(ctx) except Exception as e: d = {'tx': get('tx', ctx, None), 'ty': get('ty', ctx, None), 'acquired': get('acquired', ctx, None), 'date': get('date', ctx, None), 'chips': get('chips', ctx, None), 'exception': '{name} exception: {ex}'.format(name=name, ex=e), 'http_status': http_status} logger.exception(json.dumps(assoc(d, 'chips', count(get('chips', ctx, []))))) return d
def test_tile_runs_as_expected(client): ''' As a blackmagic user, when I send tx, ty, date & chips via HTTP POST, an xgboost model is trained and saved to Cassandra so that change segments may be classified. ''' tx = test.tx ty = test.ty cx = test.cx cy = test.cy acquired = test.acquired chips = test.chips date = test.training_date # train a model based on those segments and # the aux data # prepopulate a chip of segments assert client.post('/segment', json={ 'cx': test.cx, 'cy': test.cy, 'acquired': test.acquired }).status == '200 OK' response = client.post('/tile', json={ 'tx': tx, 'ty': ty, 'acquired': acquired, 'chips': chips, 'date': date }) tiles = db.execute_statement(cfg=app.cfg, stmt=db.select_tile(cfg=app.cfg, tx=test.tx, ty=test.ty)) assert response.status == '200 OK' assert get('tx', response.get_json()) == tx assert get('ty', response.get_json()) == ty assert get('acquired', response.get_json()) == acquired assert get('date', response.get_json()) == date assert get('chips', response.get_json()) == count(chips) assert get('exception', response.get_json(), None) == None assert len(list(map(lambda x: x, tiles))) == 1
def respond(ctx): '''Send the HTTP response''' body = {'tx': get('tx', ctx, None), 'ty': get('ty', ctx, None), 'acquired': get('acquired', ctx, None), 'date': get('date', ctx, None), 'chips': count(get('chips', ctx, []))} e = get('exception', ctx, None) if e: response = jsonify(assoc(body, 'exception', e)) else: response = jsonify(body) response.status_code = get('http_status', ctx, 200) return response
def test_tile_cassandra_exception(client): ''' As a blackmagic user, when an exception occurs saving models to Cassandra, an HTTP 500 is issued with a descriptive message so that the issue may be investigated, corrected & retried. ''' tx = test.tx ty = test.ty acquired = test.acquired chips = test.chips date = test.training_date delete_tile(test.tx, test.ty) response = client.post('/tile', json={ 'tx': tx, 'ty': ty, 'acquired': acquired, 'chips': chips, 'date': date, 'test_cassandra_exception': True }) tiles = db.execute_statement(cfg=app.cfg, stmt=db.select_tile(cfg=app.cfg, tx=test.tx, ty=test.ty)) assert response.status == '500 INTERNAL SERVER ERROR' assert get('tx', response.get_json()) == tx assert get('ty', response.get_json()) == ty assert get('acquired', response.get_json()) == acquired assert get('date', response.get_json()) == date assert get('chips', response.get_json()) == count(chips) assert type(get('exception', response.get_json())) is str assert len(get('exception', response.get_json())) > 0 assert len(list(map(lambda x: x, tiles))) == 0
async def command_join(self, message: discord.Message): if await self.send_invalid_match(message): return lobby = await self.get_active_lobby() if lobby: match_format = await self.get_active_lobby_format(lobby) match_players = await self.get_active_lobby_players(lobby) user = await self.register_user(message.author) if len(match_players) >= match_format.max_player: await message.channel.send( f"lobby is currently full, <@{lobby.creator_id}> `!start`") elif not user.disabled: if toolz.count( filter(lambda x: x.player_id == user.id, match_players)): await message.channel.send( f"you are already signed up for the current lobby") else: lobby.updated = datetime.utcnow() async with self.database.atomic(): score = await self.get_score(user, match_format) await self.database.create(MatchLobbyPlayer, lobby=lobby, player=user, mu=score.mu, sigma=score.sigma, games=score.games) lobby.updated = datetime.utcnow() await self.database.update(lobby) if len(match_players) + 1 >= match_format.min_player: await message.channel.send( f"<@{user.id}> has joined the lobby ({len(match_players) + 1}/{match_format.max_player}), " f"<@{lobby.creator_id}> can `!start`") else: await message.channel.send( f"<@{user.id}> has joined the lobby ({len(match_players) + 1}/{match_format.max_player})" ) else: await message.channel.send( f"no lobby active, use `!create [MatchFormat]`")
def test_tile_training_exception(client): ''' As a blackmagic user, when an exception occurs training a model, an HTTP 500 is issued with a message describing the failure so that the issue may be investigated & resolved. ''' tx = test.tx ty = test.ty acquired = test.acquired chips = test.chips date = test.training_date delete_tile(test.tx, test.ty) response = client.post('/tile', json={ 'tx': tx, 'ty': ty, 'acquired': acquired, 'chips': chips, 'date': date, 'test_training_exception': True }) tiles = _ceph.select_tile(tx=test.tx, ty=test.ty) assert response.status == '500 INTERNAL SERVER ERROR' assert get('tx', response.get_json()) == tx assert get('ty', response.get_json()) == ty assert get('acquired', response.get_json()) == acquired assert get('date', response.get_json()) == date assert get('chips', response.get_json()) == count(chips) assert type(get('exception', response.get_json())) is str assert len(get('exception', response.get_json())) > 0 assert len(list(map(lambda x: x, tiles))) == 0
def nsamples(self): """Amount of samples in stream. .. warning:: This consumes the stream. """ return cytoolz.count(self.samples())
def support_size(self, *args): return toolz.count(self.support(*args))
def compute_one(t, seq, **kwargs): return cytoolz.count(seq)
def is_source_moving(self): return count(unique(self.source, key=tuple)) != 1
def compute_up_1d(expr, seq, **kwargs): try: return len(seq) except TypeError: return cytoolz.count(seq)
def compute(t, seq): parent = compute(t.parent, seq) return cytoolz.count(unique(parent))
def is_receiver_moving(self): return count(unique(self.receiver, key=tuple)) != 1
def compute_up(t, seq, **kwargs): return cytoolz.count(filter(None, seq))
def compute_up_1d(t, seq, **kwargs): return cytoolz.count(filter(None, seq))