Beispiel #1
0
 def run_sample(self):
     sample = self.data[random.randint(0, len(self.data) - 1)]
     winner = self.predict_winner(sample)
     neighbours = self.get_neighbours(winner[0], winner[1], self.R)
     from_winner_f = euclidean(np.array(winner))
     for n in neighbours:
         self.update_neuron(n, sample, from_winner_f)
Beispiel #2
0
    def xGs(self, **kwargs):
        """
        Get goal attempts info for xG modelling.

        Returns: generator of (bq, seq, feats)
        --------
        bg: dict, background match info (see self._background_info)
        seq: list, sequence of timed and located events leading to the attempt
        feats: dict, extracted features from the attempt
        """
        bg = self._background_info()
        for attempt in self.get_attempts(**kwargs):
            (*attempt, (_, ga)), seq = list(attempt), []
            mins, secs, team_id = ga['mins'], ga['secs'], utils.get_team_id(ga)
            injurytime = ga.get("injurytime_play", None)
            # find if assist, by whom, from where, length, angle, etc.
            a_id, a_x, a_y, a_dist, a_angle = None, 0., 0., 0., 0.
            if len(attempt) > 0:
                last_type, last_e = attempt[-1]
                if last_type == 'all_passes' and last_e['type'] == 'completed':
                    a_id = last_e['player_id']
                    a_x, a_y, a_dist, a_angle = _get_assist(last_e)
            # add current score
            home, away = self.result(ga['mins'], ga['secs'])
            if ga['team_id'] == self.team_home['id']:
                attack, defend = home, away
            else:
                attack, defend = away, home
            # feats
            feats = {'team_id': ga['team_id'],
                     'player_id': ga['player_id'],
                     'is_home': ga['team_id'] == self.team_home['id'],
                     'headed': ga.get('headed', False),
                     'is_goal': ga['type'] == 'goal',
                     'distance': utils.euclidean(
                         ga['end']['x'], ga['end']['y'], 100, 50),
                     'possession': self.possession(
                         mins, secs, team_id, injurytime=injurytime),
                     'angle': utils.angle((ga['end']['x'], ga['end']['y'])),
                     'x': ga['end']['x'], 'y': ga['end']['y'],
                     'mins': ga['mins'], 'secs': ga['secs'],
                     'assist_x': a_x, 'assist_y': a_y, 'assist_id': a_id,
                     'assist_angle': a_angle, 'assist_dist': a_dist,
                     'attack': attack, 'defend': defend}
            # sequential data
            for ftype, e in attempt:
                if not utils.is_loc(e):  # skip unlocated events
                    continue
                seq.append({
                    'x': e.get('start', e.get('loc'))['x'],
                    'y': e.get('start', e.get('loc'))['y'],
                    'end_x': e.get('end', {'x': ''})['x'],
                    'end_y': e.get('end', {'y': ''})['y'],
                    'mins': e['mins'], 'secs': e['secs'],
                    'ftype': ftype, 'type': e.get('type', ''),
                    'action_type': e.get('action_type', ''),
                    'player_id': e['player_id'],
                    'team_id': utils.get_team_id(e)})
            yield bg, seq, feats
Beispiel #3
0
def _get_assist(pass_e):
    angle = 0.
    from_x, from_y = pass_e['start']['x'], pass_e['start']['y']
    to_x, to_y = pass_e['end']['x'], pass_e['end']['y']
    dist = utils.euclidean(from_x, from_y, to_x, to_y)
    if dist > 0:
        angle = utils.angle((from_x, from_y), (to_x, to_y))
    return from_x, from_y, dist, angle
Beispiel #4
0
 def h(self, node):
     "h function is straight-line distance from a node's state to goal."
     "Override this to be something meaningful in your graph domain"
     locs = getattr(self.graph, 'locations', None)
     if locs:
         return euclidean(locs[node.state], locs[self.goal])
     else:
         return infinity
Beispiel #5
0
 def get_neighbours(self, x, y, radio):
     neighbours = []
     distance = euclidean(np.array([x, y]))
     for i in range(self.k):
         for j in range(self.k):
             if distance(np.array([i, j])) <= radio:
                 neighbours.append((i, j))
     return neighbours
Beispiel #6
0
def DBSCAN(data, min_distance, min_points):
    distances = np.zeros((data.shape[0], data.shape[0]))
    for p in range(data.shape[0]):
        distances[p:, ] = utils.euclidean(data[p], data)

    df1 = pd.DataFrame(distances)
    df1.to_excel('data\distances.xlsx')
    point_indexes = [i for i in range(data.shape[0])
                     ]  # list of indexes of all points

    # Get core points
    core_points = []
    other_points = []
    for p in point_indexes:
        point_distances = distances[p, :]
        if point_distances[
                point_distances <= min_distance].shape[0] >= min_points:
            core_points.append(p)
        else:
            other_points.append(p)
    print(np.array(core_points) + 1)

    # Get border points
    border_points = []
    noise_points = []
    for p in other_points:
        border_point = False
        point_distances = distances[p, :]
        for cp in core_points:
            if point_distances[cp] <= min_distance:
                border_points.append((p, cp))
                border_point = True
                break  # this is a border point to some core point
        if not border_point:
            noise_points.append(p)
    print(np.array(border_points)[:, 0] + 1)
    print(np.array(noise_points) + 1)

    # Put an edge between all core points within min_distance, and make each group of connected points into a cluster
    clusters = [0 for i in range(data.shape[0])]

    cluster_num = 1
    for point in core_points:
        cluster(point, clusters, cluster_num, core_points, distances,
                min_distance)
        cluster_num += 1

    # Add each border point to the cluster of it's associated core point
    for bp_cp_pair in border_points:  # each border point is on the form (point, associated core point)
        border_point = bp_cp_pair[0]
        assoc_core_point = bp_cp_pair[1]
        clusters[border_point] = clusters[assoc_core_point]

    print(clusters)

    return clusters
Beispiel #7
0
    def merge(self, bidx, eq_classes):

        prevb = self.beam[bidx - 1]
        len_prevb = len(prevb)
        self.lqc[10] += len_prevb
        if len_prevb == 1:
            self.lqc[9] += len_prevb
            eq_classes[0] = [prevb[0]]
            return

        used = []
        key = 0

        _memory = [None] * len_prevb
        _mem_p = [None] * len_prevb
        for j in range(len_prevb):  # index of each item in last beam

            if j in used:
                continue

            tmp = []
            if _memory[j]:
                _needed = _memory[j]
                _, _, y_im1_1, y_emb_im1_1, nj = _memory[j]
                assert (j == nj)
            else:
                # calculation
                score_im1_1, s_im1_1, y_im1_1, y_emb_im1_1, bp_im1_1 = prevb[j]
                _needed = _memory[j] = (score_im1_1, s_im1_1, y_im1_1,
                                        y_emb_im1_1, j)

            tmp.append(_needed)

            for jj in range(j + 1, len_prevb):
                if jj in used:
                    continue
                if _memory[jj]:
                    _needed = _memory[jj]
                    _, _, y_im1_2, y_emb_im1_2, njj = _needed
                    assert (jj == njj)
                else:
                    score_im1_2, s_im1_2, y_im1_2, y_emb_im1_2, bp_im1_2 = prevb[
                        jj]
                    _needed = _memory[jj] = (score_im1_2, s_im1_2, y_im1_2,
                                             y_emb_im1_2, jj)

                #print euclidean(y_emb_im1_1, y_emb_im1_2)
                #if y_im1_2 == y_im1_1:
                if euclidean(y_emb_im1_1, y_emb_im1_2) < 3:
                    print 'add, ', j, jj
                    tmp.append(_needed)
                    used.append(jj)

            eq_classes[key] = tmp
            key += 1
        self.lqc[9] += key  # count of total sub-cubes
Beispiel #8
0
 def distance(self, other, type):
     from utils import euclidean
     import haversine
     if type == 'euclidean':
         return euclidean(self.center[0], self.center[1], other.center[0],
                          other.center[1])
     if type == 'haversine':
         pa = (self.center[0], self.center[1])
         pb = (other.center[0], other.center[1])
         return haversine(pa, pb)
Beispiel #9
0
def compute_weights_convergence_constrained(tuning_prop, params, comm=None):
    """
    This function computes for each target the X % of source cells which have the highest
    connection probability to the target cell.

    Arguments:
        tuning_prop: 2 dimensional array with shape (n_cells, 4)
            tp[:, 0] : x-position
            tp[:, 1] : y-position
            tp[:, 2] : u-position (speed in x-direction)
            tp[:, 3] : v-position (speed in y-direction)
    """
    if comm != None:
        pc_id, n_proc = comm.rank, comm.size
        comm.barrier()
    else:
        pc_id, n_proc = 0, 1
    gid_min, gid_max = utils.distribute_n(params['n_exc'], n_proc, pc_id)
    sigma_x, sigma_v = params['w_sigma_x'], params['w_sigma_v'] # small sigma values let p and w shrink
    (delay_min, delay_max) = params['delay_range']
    output_fn = params['conn_list_ee_conv_constr_fn_base'] + 'pid%d.dat' % (pc_id)
    print "Proc %d computes initial weights for gids (%d, %d) to file %s" % (pc_id, gid_min, gid_max, output_fn)
    conn_file = open(output_fn, 'w')
    my_cells = range(gid_min, gid_max)
    n_src_cells = round(params['p_ee'] * params['n_exc']) # number of sources per target neuron
    output = np.zeros((len(my_cells), n_src_cells+1), dtype='int')
    weights = np.zeros((len(my_cells), n_src_cells+1), dtype='int')

    output = ''
    cnt = 0
    for tgt in my_cells:
        p = np.zeros(params['n_exc'])
        latency = np.zeros(params['n_exc'])
        for src in xrange(params['n_exc']):
            if (src != tgt):
                p[src], latency[src] = get_p_conn(tuning_prop[src, :], tuning_prop[tgt, :], sigma_x, sigma_v, self.params['connectivity_radius'])
        sorted_indices = np.argsort(p)
        sources = sorted_indices[-params['n_src_cells_per_neuron']:] 
        w = params['w_tgt_in'] / p[sources].sum() * p[sources]
#        w = utils.linear_transformation(w, params['w_min'], params['w_max'])
        for i in xrange(len(sources)):
#            w[i] = max(params['w_min'], min(w[i], params['w_max']))
            src = sources[i]
            delay = min(max(latency[src], delay_min), delay_max)  # map the delay into the valid range
            d_ij = utils.euclidean(tuning_prop[src, :], tuning_prop[tgt, :])
            output += '%d\t%d\t%.2e\t%.2e\t%.2e\n' % (src, tgt, w[i], delay, d_ij)
            cnt += 1

    print 'PID %d Writing %d connections to file: %s' % (pc_id, cnt, output_fn)
    conn_file.write(output)
    conn_file.close()

    if (comm != None):
        comm.barrier()
Beispiel #10
0
def calculate_distance_offset(pairs, df, offset, offset2=None):
    # CONVERTED TO calculate_distance_mode
    """
    From a set of pairs, calculate the distance of the offset of the 
    pair in relation to the global offset.

    Parameters:
    -----------
    pairs: list
        list containing tuples of IDs of norms [(id1,id2),(id3,id4)...]
    df: pandas.dataframe
        dataframe containing ids and embeddings of sentences
    offset: np.array
        vector containing the global offset (offset of all conflicts)
    """
    label = 0
    vdist = []
    pb = progressbar.ProgressBar(len(pairs))
    for i, arr in enumerate(pairs):
        emb1 = df.id2embed(arr[0])
        emb2 = df.id2embed(arr[1])
        local_offset = emb1 - emb2

        # cosine (similar:0->2:not_similar)
        cos = utils.cosine(local_offset, offset)
        # euclidean distance (similar:0->inf:not_similar)
        euc = utils.euclidean(local_offset, offset)
        if len(offset2) > 0:
            cos2 = utils.cosine(local_offset, offset2)
            euc2 = utils.euclidean(local_offset, offset2)
            vdist.append([cos, euc, cos2, euc2])
        else:
            vdist.append((cos, euc))
        pb.update()
        #if i == 1000: break
    return vdist
Beispiel #11
0
    def recluster(self):

        for i in xrange(self.number_of_elements):
            smallest_distance = [float('inf'), None]

            for j in xrange(self.number_of_clusters):
                current_distance = utils.euclidean(self.elements[i],
                                                   self.centroids[j])

                if current_distance < smallest_distance[0]:
                    smallest_distance = [current_distance, j]

            if smallest_distance[1] != self.elements[i][3]:
                self.store_times[self.elements[i][3]] -= self.elements[i][2]
                self.store_times[smallest_distance[1]] += self.elements[i][2]
                self.elements[i][3] = smallest_distance[1]
Beispiel #12
0
    def init_road_network(self):
        out_connections = [self.__random_border_point()]
        max_road_count = max(
            1,
            min(self.limits.width, self.limits.length) //
            MEAN_ROAD_COVERED_SURFACE)
        logging.debug('Max road count: {}'.format(max_road_count))
        road_count = min(geometric(1. / max_road_count),
                         max_road_count * 3 // 2)
        logging.debug(
            'New settlement will have {} roads B)'.format(road_count))
        logging.debug('First border point @{}'.format(str(out_connections[0])))

        for road_id in xrange(road_count):
            min_distance_to_roads = min(self.limits.width,
                                        self.limits.length) / (road_id + 1)
            logging.debug('Generating road #{}'.format(road_id + 1))
            # generate new border point far enough from existing points
            while True:
                new_road_point = self.__random_border_point()
                distances = [
                    euclidean(road_point, new_road_point)
                    for road_point in out_connections
                ]
                distance_to_roads = min(distances)
                log_args = (str(new_road_point), distance_to_roads,
                            min_distance_to_roads)
                if distance_to_roads >= min_distance_to_roads:
                    out_connections.append(new_road_point)
                    logging.debug(
                        '\tSettled on border point {} at {}m >= {}m'.format(
                            *log_args))
                    break
                else:
                    logging.debug(
                        '\tDismissed point {} at {}m < {}m'.format(*log_args))
                    min_distance_to_roads *= 0.9
            # update road network
            if road_id == 0:
                self._road_network.find_road(out_connections[0],
                                             out_connections[1])
            else:
                self._road_network.connect_to_network(out_connections[-1])
Beispiel #13
0
def simpleGraphCreation():
    '''
    UUUU
    US U
    U  U
    U GU
    UUUU
    '''
    g = Graph(
        {
            (1, 1): {
                (1, 2): 1,
                (2, 1): 1
            },
            (1, 2): {
                (2, 2): 1
            },
            (2, 1): {
                (2, 2): 1,
                (3, 1): 1
            },
            (2, 2): {
                (3, 2): 1
            },
            (3, 1): {
                (3, 2): 1
            },
        },
        directed=False)

    print(g.get((2, 2)))
    print(g.nodes())
    g.locations = Dict()
    for i in g.nodes():
        g.locations.update({i: i})
    print(euclidean(g.locations[(1, 1)], g.locations[(3, 2)]))
Beispiel #14
0
    '933_None': {
        'location': {
            'lat': 27.361603784152422,
            'lng': -97.7250449632622
        }
    },
    '1480_None': {
        'location': {
            'lat': 29.97418558573035,
            'lng': -103.99866183928118
        }
    }
}

Distance = [[
    euclidean(v['location']['lat'], v['location']['lng'],
              v1['location']['lat'], v1['location']['lng'])
    for k, v in EightNodeLocationData.items()
] for k1, v1 in EightNodeLocationData.items()]

AdjacencyMatrix = [[0, 1, 1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 0, 1, 0],
                   [1, 0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 1, 1, 0, 1],
                   [1, 1, 0, 1, 0, 1, 1, 0], [0, 0, 0, 1, 1, 0, 1, 0],
                   [0, 1, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0]]

DistanceMatrix = {
    str(i + 1) + str(j + 1): Distance[i][j] * AdjacencyMatrix[i][j]
    for i in range(8) for j in range(8) if i < j and AdjacencyMatrix[i][j] == 1
}

Reactances = {k: 0.584 * v for k, v in DistanceMatrix.items()}
Beispiel #15
0
 def define_parcels_heights(parcel):
     # type: (Parcel) -> None
     y = percentile(parcel.height_map, 35)
     d = euclidean(parcel.center, self.town_center)
     h = int(MAX_HEIGHT * exp(-d / BUILDING_HEIGHT_SPREAD))
     parcel.set_height(y, h)
Beispiel #16
0
    def merge(self, bidx, eq_classes):

        prevb = self.beam[bidx - 1]
        len_prevb = len(prevb)
        used = []
        key = 0

        _memory = [None] * len_prevb
        _mem_p = [None] * len_prevb
        for j in range(len_prevb):  # index of each item in last beam
            if j in used:
                continue

            tmp = []
            if _memory[j]:
                _needed = _memory[j]
                score_im1_1, s_im1_1, y_im1_1, y_im2_1, y_im3_1, nj = _needed
                if self.ifwatch_adist and _mem_p[j]:
                    pi_1 = _mem_p[j]
                assert (j == nj)
            else:
                # calculation
                score_im1_1, s_im1_1, y_im1_1, bp_im1_1 = prevb[j]
                (y_im2_1,
                 bp_im2_1) = (-1,
                              -1) if bidx < 2 else self.beam[bidx -
                                                             2][bp_im1_1][-2:]
                y_im3_1 = -1 if bidx < 3 else self.beam[bidx - 3][bp_im2_1][-2]

                if self.ifwatch_adist:
                    y_emb_im1_1, hi_1 = self.fn_nh(y_im1_1, s_im1_1)
                    pi_1, ai_1 = self.fn_na(ctx0, self.uh, hi_1)
                    _mem_p[j] = pi_1

                _needed = _memory[j] = (score_im1_1, s_im1_1, y_im1_1, y_im2_1,
                                        y_im3_1, j)

            tmp.append(_needed)

            for jj in range(j + 1, len_prevb):

                if _memory[jj]:
                    _needed = _memory[jj]
                    score_im1_2, s_im1_2, y_im1_2, y_im2_2, y_im3_2, njj = _needed
                    if self.ifwatch_adist and _mem_p[jj]:
                        pi_2 = _mem_p[jj]
                    assert (jj == njj)
                else:  # calculation
                    score_im1_2, s_im1_2, y_im1_2, bp_im1_2 = prevb[jj]
                    (y_im2_2, bp_im2_2) = (-1, -1) if bidx < 2 else \
                        self.beam[bidx - 2][bp_im1_2][-2:]
                    y_im3_2 = -1 if bidx < 3 else self.beam[bidx -
                                                            3][bp_im2_2][-2]

                    if self.ifwatch_adist:
                        y_emb_im1_2, hi_2 = self.fn_nh(y_im1_2, s_im1_2)
                        pi_2, ai_2 = self.fn_na(ctx0, self.uh, hi_2)
                        _mem_p[jj] = pi_2

                    _needed = _memory[jj] = (score_im1_2, s_im1_2, y_im1_2,
                                             y_im2_2, y_im3_2, jj)

                if self.merge_way == 'Him1':

                    distance = euclidean(s_im1_2, s_im1_1)

                    if self.ngram == 2:
                        debug('y11 y12 {} {}, {} {}'.format(
                            y_im1_1, y_im1_2, distance, self.thresh))
                        ifmerge = ((y_im1_2 == y_im1_1)
                                   and (distance < self.thresh))
                    elif self.ngram == 3:
                        debug('y21 y22 {} {}, y11 y12 {} {}, {} {}'.format(
                            y_im2_1, y_im2_2, y_im1_1, y_im1_2, distance,
                            self.thresh))
                        ifmerge = ((y_im2_2 == y_im2_1)
                                   and (y_im1_2 == y_im1_1)
                                   and (distance < self.thresh))
                    elif self.ngram == 4:
                        debug(
                            'y31 y32 {} {}, y21 y22 {} {}, y11 y12 {} {}, {} {}'
                            .format(y_im3_1, y_im3_2, y_im2_1, y_im2_2,
                                    y_im1_1, y_im1_2, distance, self.thresh))
                        ifmerge = ((y_im3_2 == y_im3_1)
                                   and (y_im2_2 == y_im2_1)
                                   and (y_im1_2 == y_im1_1)
                                   and (distance < self.thresh))
                    else:
                        raise NotImplementedError

                elif self.merge_way == 'Hi':
                    raise NotImplementedError
                    ifmerge = (y_im1_2 == y_im1_1
                               and euclidean(hi_2, hi_1) < self.thresh)
                elif self.merge_way == 'AiKL':
                    raise NotImplementedError
                    dist = kl_dist(pi_2, pi_1)
                    debug('attention prob kl distance: {}'.format(dist))
                    ifmerge = (y_im1_2 == y_im1_1 and dist < self.thresh)

                if ifmerge:
                    tmp.append(_needed)
                    used.append(jj)

                if self.ifwatch_adist:
                    dist = kl_dist(pi_2, pi_1)
                    debug('{} {} {}'.format(j, jj, dist))

            eq_classes[key] = tmp
            key += 1
Beispiel #17
0
 def predict_winner(self, sample):
     distances = np.fromiter(map(euclidean(sample), self.kohonen.reshape(self.plain)), dtype=np.float64) \
         .reshape(self.k, self.k)
     return get_indices_of_k_smallest(distances, 1)