def _getTargetPlayers(self, team, num, pos, params, stored=[]): players = [] if type(pos) == builtins.list: for p in pos: players += self._filterUsed( getPlayersFromTeamByPos(self.teamdata, team, p), stored) pos = pos[0] else: players = self._filterUsed(\ getPlayersFromTeamByPos(self.teamdata, team, pos), stored) if len(players) == 0: players = self._getFromAnotherPos(team, pos, num, \ list(map(lambda x:x[LASTNAME], players))) vecparams = self._getParamValues(players, params) matr = np.array(vecparams) if len(matr) > 0: if len(matr) > num: return self._optimalPlayers(matr, np.argmax, num, players) if len(matr) == num: return list(map(lambda x: x[LASTNAME], players)) else: another = self._getFromAnotherPos(team, pos, num, players) return dataToNames(another)
def _clustering(self, targetgame, games): ''' Find similar games with clustering TODO ''' preparegames = list(map(lambda x: [i[1] for i in x.data], games)) preparegame = list(map(lambda x: x[1], targetgame.data)) lables = list(range(len(games))) clf = NearestCentroid() clf.fit(preparegames, lables) print(clf.predict(preparegame))
def _sampleCase(self, startmin, endmin, data, size): """ Return random sample with size in data with """ sto = list(map(lambda x: data[x], data)) sampledata = np.random.choice(sto,size) result = [] for game in sampledata: if len(game) > 0: result += list( map(lambda x: x[1], filter(lambda x: x[0] >= startmin and x[0] <= endmin,\ game[0]))) return Counter(result).most_common()
def _sampleCase(self, startmin, endmin, data, size): """ Return random sample with size in data with """ sto = list(map(lambda x: data[x], data)) sampledata = np.random.choice(sto, size) result = [] for game in sampledata: if len(game) > 0: result += list( map(lambda x: x[1], filter(lambda x: x[0] >= startmin and x[0] <= endmin,\ game[0]))) return Counter(result).most_common()
def _distance(self, targetevent, events): ''' Find similar games(events) with naive distance todo: normalize to similar length return Game object with optimal game ''' SCORE_INIT = 99999 #Global scores bestscore = SCORE_INIT bestscore2 = SCORE_INIT game = Game('', '') for event in events: localresult1 = 0 localresult2 = 0 for targ in targetevent.data: tmin, tdescription = targ[0], targ[1] preres = list(map(lambda x: -1 if x[1] != tdescription else (abs(tmin - x[0])), event.data)) res = abs(sum(preres)) minuscount = len(list(filter(lambda x: x == -1, preres))) localresult1 += minuscount localresult2 += res if localresult1 < bestscore and localresult2 < bestscore2: bestscore = localresult1 bestscore2 = localresult2 game = event return game
def mostFreqEvents(self, startmin, endmin, *args, **kwargs): """ Get Most frequent events from startmin until endmin sample - Get sample from n games """ if( isinstance(startmin, int) != True or isinstance(endmin, int) != True): raise LiveGameAnalysisException("Time need to be in int format") if(startmin > endmin): raise LiveGameAnalysisException("Starttime can't be greather then endtime") sample = kwargs.get('sample') if sample != None: return self._sampleCase(startmin, endmin, self.data, sample) preresult = [] sampledata = kwargs.get('data') data = self.data if sampledata == None else sampledata for game in data: if len(data[game]) > 0: filtering = list( map(lambda x: x[1], filter(lambda x: x[0] >= startmin and x[0] <= endmin,\ self.data[game][0]))) preresult += filtering if len(preresult) > 0: limit = kwargs.get('limit',0) count = Counter(preresult) lim = len(preresult) if limit == 0 else limit return count.most_common()[0:lim]
def getLocalResults(self, players, vec): ''' Получить результат, основываясь только на данных одноклубников vec - Вектор параметров ''' print(list(map(lambda x: (x['TotalClearances'], x['Rating'], x['GameStarted'], \ x['ManOfTheMatch'], x['AerialWon'], ), players)))
def _chooseDefenceCenter(self, team, opteam, num): """ Get optimal Defender to center """ players = list(getPlayersFromTeamByPos(self.teamdata, team, 'D(C)')) params = ['TotalTackles', 'AerialWon', 'Rating','OffsidesWon','GameStarted',\ 'ShotsBlocked', LASTNAME] tomaxvalues = self._getParamValues(players, params) tominvalues = self._getParamValues( players, ['AerialLost', 'Dispossesed', 'Yellow', LASTNAME]) if len(tomaxvalues) > 1: maxv = self._optimalPlayers(np.array(tomaxvalues), np.argmax, num, players) else: maxv = tomaxvalues[0][-1:] if len(tominvalues) > 1: minv = self._optimalPlayers(np.array(tominvalues), np.argmin, num, players) else: minv = tominvalues[0][-1:] #if same players both in maxv and minv append in result list data = list(set(maxv).intersection(set(minv))) if len(data) < num: return data + list( map( lambda x: x[LASTNAME], self._getFromAnotherPos(team, 'D(C)', num - len(data), data))) #self._opteamOptimal(self.fwparams, opteam, players) size = len(data) if size == 2: return data else: return data + list(filter(lambda x: x not in data, maxv))
def _chooseDefenceCenter(self, team, opteam, num): """ Get optimal Defender to center """ players = list(getPlayersFromTeamByPos(self.teamdata, team, 'D(C)')) params = ['TotalTackles', 'AerialWon', 'Rating','OffsidesWon','GameStarted',\ 'ShotsBlocked', LASTNAME] tomaxvalues = self._getParamValues(players, params) tominvalues = self._getParamValues(players,['AerialLost','Dispossesed','Yellow', LASTNAME]) if len(tomaxvalues) > 1: maxv = self._optimalPlayers(np.array(tomaxvalues), np.argmax, num, players) else: maxv = tomaxvalues[0][-1:] if len(tominvalues) > 1: minv = self._optimalPlayers(np.array(tominvalues), np.argmin, num, players) else: minv = tominvalues[0][-1:] #if same players both in maxv and minv append in result list data = list(set(maxv).intersection(set(minv))) if len(data) < num: return data + list(map(lambda x: x[LASTNAME], self._getFromAnotherPos(team, 'D(C)', num-len(data), data))) #self._opteamOptimal(self.fwparams, opteam, players) size = len(data) if size == 2: return data else: return data + list(filter(lambda x: x not in data, maxv))
def _distance(self, targetevent, events): ''' Find similar games(events) with naive distance todo: normalize to similar length return Game object with optimal game ''' SCORE_INIT = 99999 #Global scores bestscore = SCORE_INIT bestscore2 = SCORE_INIT game = Game('', '') for event in events: localresult1 = 0 localresult2 = 0 for targ in targetevent.data: tmin, tdescription = targ[0], targ[1] preres = list( map( lambda x: -1 if x[1] != tdescription else (abs(tmin - x[0])), event.data)) res = abs(sum(preres)) minuscount = len(list(filter(lambda x: x == -1, preres))) localresult1 += minuscount localresult2 += res if localresult1 < bestscore and localresult2 < bestscore2: bestscore = localresult1 bestscore2 = localresult2 game = event return game
def mostFreqEvents(self, startmin, endmin, *args, **kwargs): """ Get Most frequent events from startmin until endmin sample - Get sample from n games """ if (isinstance(startmin, int) != True or isinstance(endmin, int) != True): raise LiveGameAnalysisException("Time need to be in int format") if (startmin > endmin): raise LiveGameAnalysisException( "Starttime can't be greather then endtime") sample = kwargs.get('sample') if sample != None: return self._sampleCase(startmin, endmin, self.data, sample) preresult = [] sampledata = kwargs.get('data') data = self.data if sampledata == None else sampledata for game in data: if len(data[game]) > 0: filtering = list( map(lambda x: x[1], filter(lambda x: x[0] >= startmin and x[0] <= endmin,\ self.data[game][0]))) preresult += filtering if len(preresult) > 0: limit = kwargs.get('limit', 0) count = Counter(preresult) lim = len(preresult) if limit == 0 else limit return count.most_common()[0:lim]
def get_stations_for_journey_time_update(self) -> Iterable[Station]: docs: Iterable[DocumentSnapshot] = self._destinations\ .order_by('journey_times_updated')\ .limit(1)\ .get() return map(lambda doc: Station.from_dict(doc.to_dict()), docs)
def show(self, by=None): """ Output results by - return only target column """ if by == None: return self.resultdata idx = self.queries.index(by) return list(map(lambda x: x[idx], self.resultdata))
def get_stations_for_journey_costs_update(self) -> Iterable[Station]: this_year = date.today().year docs: Iterable[DocumentSnapshot] = self._destinations\ .where('journey_costs_updated', '<', datetime(this_year, 1, 1))\ .order_by('journey_costs_updated')\ .limit(1).get() return map(lambda doc: Station.from_dict(doc.to_dict()), docs)
def _opteamOptimal(self, params, opteam, teamdata): ''' Best players from opposite team ''' gs = 'GameStarted' opplayers = list(filter(lambda x:x[gs] > 0, \ getPlayersFromTeamByPos(self.teamdata, opteam, 'FW'))) #values = list(filter(lambda x: x[-1:][0] > 0, self._getParamValues(opplayers, params))) for v in params.keys(): #print(opplayers) for player in teamdata: #preresult5 = list(F() << (_/player[gs]) << (filter, _ > 0)) preresult = list( map(_/player[gs], filter(_ > 0, \ map(lambda x: player[x], params[v])) )) print(preresult)
def test_fib_infinite_stream(self): from operator import add f = Stream() fib = f << [0, 1] << iters.map(add, f, iters.drop(1, f)) self.assertEqual([0,1,1,2,3,5,8,13,21,34], list(iters.take(10, fib))) self.assertEqual(6765, fib[20]) self.assertEqual([832040,1346269,2178309,3524578,5702887], fib[30:35])
def getPlayersByParams(teamdatas, params): ''' List of params for all players from all teams ''' teams = list(teamdatas.keys()) allparams = list(teamdatas[teams[0]][0].keys()) for team in teams: for player in teamdatas[team]: yield (list(map(lambda y: player[y], \ list(filter(lambda x: x in params, allparams)))))
def test_fib_infinite_stream(self): from operator import add f = Stream() fib = f << [0, 1] << iters.map(add, f, iters.drop(1, f)) self.assertEqual([0,1,1,2,3,5,8,13,21,34], list(iters.take(10, fib))) self.assertEqual(6765, fib[20]) self.assertEqual([832040,1346269,2178309,3524578,5702887], list(fib[30:35])) # 35 elements should be already evaluated self.assertEqual(fib.cursor(), 35)
def _preparegetBestByAllParams(self, params): if 'team' in params and team != None and team in self.teams: return team, self.teams[team] if 'players' in params: """ Return tuple """ result = params['players'] return result[0], \ list(map(lambda x: getPlayer(self.teams[result[0]],x), \ functools.reduce(list.__add__, \ list(result[1].values()),[]) ) )
def compare(self, first, second): ''' Compare some two parameters st = Statistics(teams) st.compare('Height', 'AerialWon') ''' bans = self.teamdata keys = list(self.teamdata.keys()) result = [] for targteam in keys: result.append(list(map(lambda x: [x[first], x[second],x['gamestarted']], bans[targteam]))) return sorted(result, key=lambda x:x[2])
def optimalInner(matr, func, num, players, res=set()): c = Counter(func(matr, axis=0)).most_common(num) result = set(map(lambda x: players[x[0]][LASTNAME], c)) if len(result) == num: res |= result return False, list(res) idxs = list(filter(lambda x: players[x][LASTNAME] in result, \ range(len(players)))) temp = matr.tolist() for i in idxs: del players[i] del temp[i] res |= result return True, (np.array(temp), func, num-len(result), players, res)
def optimalInner(matr, func, num, players, res=set()): c = Counter(func(matr, axis=0)).most_common(num) result = set(map(lambda x: players[x[0]][LASTNAME], c)) if len(result) == num: res |= result return False, list(res) idxs = list(filter(lambda x: players[x][LASTNAME] in result, \ range(len(players)))) temp = matr.tolist() for i in idxs: del players[i] del temp[i] res |= result return True, (np.array(temp), func, num - len(result), players, res)
def compare(self, first, second): ''' Compare some two parameters st = Statistics(teams) st.compare('Height', 'AerialWon') ''' bans = self.teamdata keys = list(self.teamdata.keys()) result = [] for targteam in keys: result.append( list( map(lambda x: [x[first], x[second], x['gamestarted']], bans[targteam]))) return sorted(result, key=lambda x: x[2])
def main_generator(self): objective = self.objective driver = self.driver # test the entire initial simplex self.simplex_points = list(map(driver.get_configuration, self.initial_simplex())) if len(self.simplex_points) <= 1: log.warning("only 1 point in simplex, will not use %s", self.name) return log.debug("initial points") for p in self.simplex_points: self.yield_nonblocking(p) yield None # wait until results are ready self.simplex_points.sort(cmp=objective.compare) while not self.convergence_criterea(): # set limit from worst point self.limit = objective.limit_from_config(self.simplex_points[-1]) if log.isEnabledFor(logging.DEBUG): self.debug_log() reflected = self.reflected_simplex() yield None # wait until results are ready reflected.sort(cmp=objective.compare) # this next condition implies reflected[0] < simplex_points[0] since # reflected is sorted and contains simplex_points[0] (saves a db query) if reflected[0] is not self.simplex_points[0]: expanded = self.expanded_simplex() yield None # wait until results are ready expanded.sort(cmp=objective.compare) if objective.lt(expanded[0], reflected[0]): log.debug("expansion performed") self.simplex_points = expanded else: log.debug("reflection performed") self.simplex_points = reflected else: contracted = self.contracted_simplex() yield None # wait until results are ready contracted.sort(cmp=objective.compare) log.debug("contraction performed") self.simplex_points = contracted
def compareTeams(self, team1, team2): ''' Compare players by total number of params For example: Total number of goals or total number of yellow cards ''' if team1 not in self.teamdata or team2 not in self.teamdata: raise StatisticsException("On of teams not in the base") result = {} poses = set(map(lambda x: x['positionshort'], self.teamdata[team1])) players1 = self.teamdata[team1] players2 = self.teamdata[team2] params = players1[0].keys() print("{0} vs {1}".format(team1, team2)) for param in params: result_team1, result_team2 = self._accumulatePlayerParams(param, players1, players2) if result_team1 != None: print("{0} : {1} vs {2}".format(param, result_team1, result_team2))
def compareTeams(self, team1, team2): ''' Compare players by total number of params For example: Total number of goals or total number of yellow cards ''' if team1 not in self.teamdata or team2 not in self.teamdata: raise StatisticsException("On of teams not in the base") result = {} poses = set(map(lambda x: x['positionshort'], self.teamdata[team1])) players1 = self.teamdata[team1] players2 = self.teamdata[team2] params = players1[0].keys() print("{0} vs {1}".format(team1, team2)) for param in params: result_team1, result_team2 = self._accumulatePlayerParams( param, players1, players2) if result_team1 != None: print("{0} : {1} vs {2}".format(param, result_team1, result_team2))
def getOptimalTeam(self, team, opteam, formation): ''' formation can be 4-4-2 or 3-5-2, but not 4-2-1-3 Если более сильная атака, то выбираем мощную защиту и наоборот opteam-opposite team GK - Goalkeeper D(CR) - Defence Right D(LR) - Defence Left D(L) - Defence Left FW - Forward AM - Attack mid ''' result = {} if team not in self.teamdata: raise OptimalTeamException("This team not in base") form_res = list(map(lambda x: int(x), formation.split('-'))) if len(form_res) != 3: raise OptimalTeamException("Error in formation representation") result['GK'] = self._chooseGK(team) result['DF'] = self._chooseDefence(team, opteam, form_res[0]) result['MF'] = self._chooseMidfielder(team, form_res[1]) result['FW'] = self._chooseForward(team, opteam, form_res[2]) return result
def test_map_withTuple(self): out = tuple(iters.map(_ * 2, (2, ))) self.assertEqual(4, out[0]) self.assertEqual(tuple, type(out))
def dataToNames(data): """ Change list with params to only last name """ return list(map(lambda x: x[LASTNAME], data))
def test_map_withList(self): out = list(iters.map(_ * 2, [2])) self.assertEqual(4, out[0]) self.assertEqual(list, type(out))
def getGames(self, name): """ Get games where contains 'name' (as team)""" teams = self.games.keys() return list( map(lambda m: self.games[m], filter(lambda x: name in x.split(), teams)))
def appendPlayers(team): result.extend(list(map(getPlayer, data[team])))
def getAllEventsName(self): """ Return just all events name Now is dirty solution, return event from only one game """ tempkeys = list(self.data.keys()) return set(map(lambda x: x[1], self.data[tempkeys[0]][0]))
def get_all_stations(self) -> Iterable[Station]: docs: Iterable[DocumentSnapshot] = self._stations.get() return map(lambda doc: Station.from_dict(doc.to_dict()), docs)
def inner(self): return head(filter(bool, map(fn, self.comments.all())))
def getGames(self, name): """ Get games where contains 'name' (as team)""" teams = self.games.keys() return list(map(lambda m: self.games[m], filter(lambda x: name in x.split(), teams)))
def _getParamValues(self, players, values): return list(map(lambda x: [x[p] for p in values], players))
def annotation_view(request, task_key): """Return a specific profile give a user's screen_name. :request: @todo :task: @todo :returns: @todo """ user = get_user(request) if user.task_package is None and not settings.DEBUG: raise Http404 show_rk = request_property(request, 'show_rk', False) task = _k(task_key, 'AnnotationTask').get() rs = [r.get() for r in task.rankings] ts = [r.topic.get() for r in rs] if not show_rk: title = lambda ts, _: '\n'.join( ['Example Inquiry:'] + [q + '?' for q in ts[0].example.split('? ')])[:-1] else: title = lambda _, rs: '\n'.join( [rs[0].candidate.get().screen_name] + ['{0}, {1[profile_type]}: {1[rank]} ({1[score]:.6})'.format(r.rank_method, r.rank_info) for r in rs]) def mk_topic(ts, rs): """ Prepare topics for the view """ return {'topic_id': rs[0].topic_id, 'topic_type': ts[0].level, 'topic': ts[0].name, 'region': rs[0].region, 'title': title(ts, rs)} red = op.foldl(lambda x, y: dict(x.items() + y.items()), {}) topics = red(map( F() >> (lambda item: (item[0], list(item[1]))) >> (lambda item: {item[0]: mk_topic(*zip(*item[1]))}), groupby( sorted( zip(ts, rs), key=L[1].topic_id), key=L[1].topic_id))) # make filters out of topics fsm = FilterSetMaker() for _, g in groupby(ts, key=L.name): fsm.addTopic(g.next()) fs = fsm.getFilterSet() fs_injson = json.dumps([ {'name': f.name, 'level': f.level, 'pid': f.pid} for f in fs ]) # prepare for review review = request_property(request, 'review', None) if review is not None: # judgements = [j # for t in topics.values() # for j in Judgement.query(Judgement.topic_id == t['topic_id'], # Judgement.candidate == rs[0].candidate).fetch(1)] judgements = [_k(j, 'Judgement').get() for j in review.split(',')] topic_judgements = json.dumps({j.topic_id: j.score for j in judgements}) else: topic_judgements = 'null' return render_to_response( 'expert_view.html', { 'user': user.js_encode(), 'topics': topics.values(), 'candidate': task.candidate.urlsafe(), 'task_key': task_key, 'filters': fs, 'filters_json': fs_injson, 'topic_judgement': topic_judgements }, context_instance=RequestContext(request))
def _accumulatePlayerParams(self, param, players1, players2): ''' Sum over all player params ''' if isinstance(players1[0][param], int): compute = lambda player: sum(list(map(lambda x: x[param], player))) return compute(players1), compute(players2) return None, None
def main_generator(self): objective = self.objective driver = self.driver # test the entire initial simplex self.simplex_points = list(map(driver.get_configuration, self.initial_simplex())) if len(self.simplex_points) <= 1: log.warning("only 1 point in simplex, will not use %s", self.name) return log.debug("initial points") for p in self.simplex_points: self.yield_nonblocking(p) yield None # wait until results are ready while not self.convergence_criterea(): # next steps assume this ordering self.simplex_points.sort(cmp=objective.compare) # set limit from worst point self.limit = objective.limit_from_config(self.simplex_points[-1]) self.centroid = self.calculate_centroid() if log.isEnabledFor(logging.DEBUG): self.debug_log() reflection = self.reflection_point() yield reflection if objective.lt(reflection, self.simplex_points[0]): #expansion case expansion = self.expansion_point(reflection) yield expansion if objective.lt(expansion, reflection): log.debug("using expansion point") self.simplex_points[-1] = expansion else: log.debug("using reflection point (considered expansion)") self.simplex_points[-1] = reflection elif objective.lt(reflection, self.simplex_points[1]): #reflection case log.debug("using reflection point") self.simplex_points[-1] = reflection else: # contraction case if objective.lte(reflection, self.simplex_points[-1]): # outside contraction contract_base = reflection else: # inside contraction contract_base = self.simplex_points[-1] contraction = self.contraction_point(contract_base) yield contraction if objective.lte(contraction, contract_base): log.debug("using contraction point") self.simplex_points[-1] = contraction else: #reduction case log.debug("performing shrink reduction") self.perform_shrink_reduction() for p in self.simplex_points: self.yield_nonblocking(p) yield None # wait until results are ready
def cfg_to_str(self, cfg): params = list(filter(Parameter.is_primitive, self.manipulator.parameters(cfg))) params.sort(key=_.name) return str(tuple(map(lambda x: x.get_unit_value(cfg), params)))
def _accumulatePlayerParams(self, param, players1, players2): ''' Sum over all player params ''' if isinstance(players1[0][param], int): compute = lambda player: sum(list(map(lambda x: x[param], player))) return compute(players1), compute(players2) return None,None