コード例 #1
0
ファイル: project.py プロジェクト: dngajjar/Courses
def project0(east, west, table, x, y):
    some = 0.000001 # handles a tedious div/zero error
    # compute the dist between the independent variables
    c = dist.dist(east, west, table.indep, table, False) 
    for s in range(len(table.data[0])):
        row = [table.data[k][s] for k in range(len(table.data))]
        a = dist.dist(east, row, table.indep, table, False)
        b = dist.dist(west, row, table.indep, table, False)
        if a > c: x = []; y = []; return project0(east, row, table, x, y)
        if b > c: x = []; y = []; return project0(row, west, table, x, y)
        else:
            temp = (a**2 + c**2 - b**2) / (2*c + some)
            x += [str('%.3f'%temp)]
            y += [str('%.3f'%((a**2 -temp**2)**0.5))]
    return x, y
コード例 #2
0
ファイル: classify.py プロジェクト: gic888/gdblocks
def rocvq(d, dmode='bin', qs=np.arange(1, 100000, 5000), good=1,
          scond=('cond1',), nulls=None):
    '''
    scan over q in qs to find the best discrimination
    '''
    nd = d.new()
    if scond == 'diff':
        pref = prefstim(d, good, ('cond1', 'cond2'), True)
    elif scond == 'man':
        stims = np.intersect1d(d['cond1.stims'], d['cond2.stims'], False)
        if type(good) == int:
            good = (good,)
        pref = (good, np.setdiff1d(stims, good))
    else:
        pref = prefstim(d, good, scond, False)
    nd['qs'] = tuple(qs)
    for cond in d.keys():
        if not cond.startswith('cond'):
            continue
        scla = sclasses(d[cond + '.stims'], pref)
        bvs = []
        for i, q in enumerate(qs):
            dm = dist.dist(d[cond + '.evts'], dmode, q, nulls)
            r = roc(scla, dm, 100)
            nd[cond + '.roc%i' % i] = r
            bvs.append((r[:, 0] - r[:, 1]).max())
        nd[cond + '.bv'] = tuple(bvs)
    return nd
コード例 #3
0
ファイル: se.py プロジェクト: AngelBerihuete/pygp
    def Kgrad_x(self,theta,x1,x2,d):
        """
        The partial derivative of the covariance matrix with
        respect to x, given hyperparameters `theta`.

        **Parameters:**
        See :py:class:`pygp.covar.CovarianceFunction`
        """
        # if we are not meant return zeros:
        if(d not in self.dimension_indices):
            return SP.zeros([x1.shape[0],x2.shape[0]])
        rv = self.K(theta,x1,x2)
#        #1. get inputs and dimension
        x1, x2 = self._filter_input_dimensions(x1,x2)
        d -= self.dimension_indices.min()
#        #2. exponentialte parameters
#        V0 = SP.exp(2*theta[0])
#        L  = SP.exp(theta[1:1+self.n_dimensions])[d]
        L2 = SP.exp(2*theta[1:1+self.n_dimensions])
#        # get squared distance in right dimension:
#        sqd = dist.sq_dist(x1[:,d]/L,x2[:,d]/L)
#        #3. calculate the whole covariance matrix:
#        rv = V0*SP.exp(-0.5*sqd)
        #4. get non-squared distance in right dimesnion:
        nsdist = -dist.dist(x1,x2)[:,:,d]/L2[d]
        
        return rv * nsdist
コード例 #4
0
def greedyMatch(points1, points2):
    len1 = len(points1)
    len2 = len(points2)
    # get number of points
    numPoints = np.amin((len1, len2))
    # form distance matrix
    D = np.zeros((len1, len2))

    # add distances
    for row in range(len1):
        for col in range(len2):
            D[row, col] = dist(points1[row], points2[col])

    # find matches
    ordered_pts1 = []
    ordered_pts2 = []

    for it in range(numPoints):
        min_dist = np.amin(D)
        # get indices of min_dist
        min_row, min_col = np.where(D == min_dist)
        # points1[min_row] matches points2[min_col]
        ordered_pts1.append(points1[min_row])
        ordered_pts2.append(points2[min_col])

        # set D along (min_row, min_col) to a really high number
        D[min_row, :] = 1000000000 * np.ones((1, len2))
        D[:, min_col] = 1000000000 * np.ones((len1, 1))

    # return matched points
    return ordered_pts1, ordered_pts2
コード例 #5
0
ファイル: delta.py プロジェクト: namisan/pygp
 def Kgrad_theta(self, theta, x1, i):
     # 2. exponentiate params:
     A = 2 * SP.exp(2 * theta)
     # 3. calculate similarity and score accross dimensions
     D = 1.0 * (dist.dist(x1, x1) == 0)
     K = D[:, :, i] * A[i]
     # derivative w.r.t. to amplitude
     return K
コード例 #6
0
ファイル: tshortener.py プロジェクト: nave91/miner
def distance_pruner(zlst):
    #Prunes cluster tree i.e. zlst with distance between their centroids
    

    if args['v'] > -1:
        sys.stderr.write("\n#Pruning data based on eucledian distance between clusters.\n")
        print zlst," #Old zlst before distprune"
    
    import dist
    z0 = zlst[0]
    pairs = []
    for _i,i in enumerate(data[z0]):
        for _j,j in enumerate(data[z0]):
            if i != j:
                if dist.dist(i,j,z0,indep,nump) < 0.3:
                    if [_i,_j] not in pairs and [_j,_i] not in pairs:
                        pairs.append(['__'+str(_i+1),'__'+str(_j+1)])
                    
    def repaired(pairs):
        for i in pairs:
            for j in pairs:
                if i != j :
                    if i[0] in j or i[1] in j:
                        pairs = [list(set(i+j))]+\
                        [k for k in pairs if k not in [i,j]]
                        return repaired(pairs)
        return pairs
    
    repairs = repaired(pairs)
    ps = []
    for i in repairs:
        ps+=i
    for i in zlst[1:]:
        if i not in ps:
            repairs.append([i])
    temp_row = {}
    
    for ind,p in enumerate(repairs):
        temp_row[ind] = []
        for i in p:
            temp_row[ind] += data[i]
    col = colname[z0]
    for Z in zlst:
        reader.removeTable(Z)
    zlst = [None]
    for ind,value in enumerate(temp_row.values()):
        Z = '__'+str(ind+1)
        reader.makeTable(col,Z)
        for r in value:
            reader.addRow(r[:len(r)-1]+[ind],Z)
        zlst.append(Z)
    xy_lib.buildzero(zlst,'',args['e'])

    if args['v'] > -1:
        print zlst," #New zlst after distprune"
    return zlst
コード例 #7
0
ファイル: delta.py プロジェクト: namisan/pygp
 def K(self, theta, x1, x2=None):
     # get input data:
     x1, x2 = self._filter_input_dimensions(x1, x2)
     # 2. exponentiate params:
     A = SP.exp(2 * theta)
     # 3. calculate similarity and score accross dimensions
     D = 1.0 * (dist.dist(x1, x2) == 0)
     # dot product with A
     K = SP.dot(D, A)
     return K
コード例 #8
0
ファイル: plot2.py プロジェクト: diogofalmeida/classnotes
def form_tree(points,node,all_points):
    global i
    print 'entering form tree ---'
    print "points",points
    pivot = points[0]
    print "pivot",pivot
    radius = np.max(dist.dist(points,pivot))
    print "radius",radius
    f = plt.figure()
    ax = f.gca()
    plot_points(all_points,'ko',ax)
    plot_points(points,'ro',ax)
    circle(pivot,radius,ax)
    #plt.show()
    plt.savefig('knn%s.png' % str(i))
    i += 1
    node[0] = pivot
    node[1] = radius
    if len(points) <= __rmin__:
        node[2] = points
        return
    print "node",node
    idx = np.argmax(dist.dist(points,pivot))
    furthest = points[idx,:]
    print "new pivot 1", furthest
    idx = np.argmax(dist.dist(points,furthest))
    furthest2 = points[idx,:]
    print "new pivot 2", furthest2
    dist1=dist.dist(points,furthest)
    dist2=dist.dist(points,furthest2)
    diffs = dist1-dist2
    print "points",points
    print diffs
    p1 = points[diffs <= 0]
    p2 = points[diffs > 0]
    print "points 1", p1
    print "points 2", p2
    node[3][0] = new_node() # left child
    node[3][1] = new_node() # right child
    form_tree(p1,node[3][0],all_points)
    form_tree(p2,node[3][1],all_points)
コード例 #9
0
ファイル: delta.py プロジェクト: namisan/pygp
 def K(self, theta, x1, x2=None):
     # get input data:
     x1, x2 = self._filter_input_dimensions(x1, x2)
     # 2. exponentiate params:
     A = SP.exp(2 * theta[0])
     # 3. calculate similarity and score accross dimensions
     D = dist.dist(x1, x2)
     # sum over the numer of zero distances entries accross dimensions
     K = (D == 0).sum(axis=2)
     # multiply with scale factor and done
     RV = A * K
     return RV
コード例 #10
0
def form_tree(points, node, all_points):
    pivot = points[0]
    radius = np.max(dist.dist(points, pivot))
    plot_circles(pivot, radius, points, all_points)

    node[0] = pivot
    node[1] = radius
    if len(points) <= __rmin__:
        node[2] = points
        return
    idx = np.argmax(dist.dist(points, pivot))
    furthest = points[idx, :]
    idx = np.argmax(dist.dist(points, furthest))
    furthest2 = points[idx, :]
    dist1 = dist.dist(points, furthest)
    dist2 = dist.dist(points, furthest2)
    diffs = dist1 - dist2
    p1 = points[diffs <= 0]
    p2 = points[diffs > 0]
    node[3][0] = new_node()  # left child
    node[3][1] = new_node()  # right child
    form_tree(p1, node[3][0], all_points)
    form_tree(p2, node[3][1], all_points)
コード例 #11
0
ファイル: se_periodic.py プロジェクト: jennyhong/pygp
 def K(self, theta, x1, x2=None, p=2):
     """
     See covPeriodic.m from GPML
     K(x1, x2) = A * exp( -nll*sin^2 (||x1-x2||/p) )
     """
     # print inspect.stack()[1][3], x1.shape, x2
     x1, x2 = self._filter_input_dimensions(x1, x2)
     A = SP.exp(theta[0])
     nll = SP.exp(theta[1])
     distance = dist.dist(x1, x2) / p
     sin_distance = numpy.sin(distance)
     rv = -nll * numpy.multiply(sin_distance, sin_distance)
     rv = A * SP.exp(rv)
     # This next line is kind of hacky. I get a (len, len, 1)
     # shape, so I just want to make it (len, len)
     # I should figure out the reason behind this in the first place, though
     rv.resize(rv.shape[:-1])
     return rv
コード例 #12
0
def nearest_edge(rects, *args):
  x,y = xy.extract(*args)
  
  class Result:
    min_dist = nearest_rect = nearest_edge = None

  result = Result()

  for rect in rects:
    edge = directions.west
    for midpoint in rect.midpoints():
      d = dist.dist([x,y], midpoint)
      if result.min_dist == None or d < result.min_dist:
        result.min_dist = d
        result.nearest_rect = rect
        result.nearest_edge = edge
      edge += 1
  return result
コード例 #13
0
ファイル: selectUI.py プロジェクト: gic888/gdblocks
def dist_discrim_incond(d, mode='vdps', q=6000, nstim=None):
    """
    d: IntIO, mode: DistMode, q: x ->
        ret: ICD(d[cond], mode, q)

    norm is a flag. If True, the inter-class distances are normalized by the
    expected inclass distance:

    """
    dm = dist.dist(d['evts'], mode, q)
    dc = dist.classDM(dm, d['stims'], nstim)
    dca = np.zeros((len(dc), len(dc)))
    for i in range(len(dc)):
        for j in range(len(dc[i])):
            dca[i, j] = np.mean(dc[i][j])
            if i != j:
                dca[j, i] = dca[i, j]
    return dca
コード例 #14
0
ファイル: classify.py プロジェクト: gic888/gdblocks
def sc_dm(d, cond='cond1', dmode='ed_bin', q=20000, good=1, scond=('cond1',),
          doroc=100, nulls=None):
    '''
    construct a pair (scla, dm) for d[cond] using the arguments good, scond for
    prefstim, and q, dmode, nulls for dist.dist.

    If doroc, use these to build a doroc-point ROC curve and return that.
    Otherwise return the tuple (scla, dm).

    '''
    if scond == 'diff':
        pref = prefstim(d, good, ('cond1', 'cond2'), True)
    else:
        pref = prefstim(d, good, scond, False)
    scla = sclasses(d[cond]['stims'], pref)
    dm = dist.dist(d[cond + '.evts'], dmode, q, nulls)
    if doroc:
        return roc(scla, dm, doroc)
    else:
        return (scla, dm)
コード例 #15
0
ファイル: se_periodic.py プロジェクト: jennyhong/pygp
    def Kgrad_theta(self, theta, x1, i, x2=None, p=2):
        """
        The derivatives of the covariance matrix for
        each hyperparameter, respectively.

        **Parameters:**
        See :py:class:`pygp.covar.CovarianceFunction`
        """
        x1, x2 = self._filter_input_dimensions(x1, x2)
        # 2. exponentiate params:
        A = SP.exp(2*theta[0])
        nll  = SP.exp(theta[1:1+self.n_dimensions])
        # calculate sin^2 (|x1-x2|/p)
        sin_distance = numpy.sin(dist.dist(x1, x2) / p)
        sinsq = numpy.multiply(sin_distance, sin_distance)
        #3. calcualte withotu derivatives, need this anyway:
        derivative_A = SP.exp(-nll * sinsq)
        if i==0:
            return derivative_A
        else:
            return -A * sinsq * derivative_A
コード例 #16
0
ファイル: selectUI.py プロジェクト: gic888/gdblocks
def pairwise_ROC(d, cond='cond1', mode='ed_bin', q=20000, nroc=100):
    """
    d: CellExp, cond: CondName(d), mode: DistMode, q: DistQ, nroc: i ->
        ret: {(i,i) of Roc(nroc)

    Constructs every pairwise comparison between stimuli in d[cond] as a
    classify.roc ROC curve. Returns a dictionary of pairs (i, j) onto Rocs,
    where (i, j) are the two stimuli being separated.
    """
    dm = dist.dist(d[cond]['evts'], mode, q)
    st = np.array(d[cond]['stims'])
    stn = _stimnames(d, cond, True)
    stpairs = clust.pairs(sorted(stn))
    ret = {}
    for p in stpairs:
        s1 = np.nonzero(st == stn[p[0]])[0]
        s2 = np.nonzero(st == stn[p[1]])[0]
        ind = np.union1d(s1, s2)
        scla = np.array([(x in s1 or 2) for x in ind])
        dm_p = dm[ind, :][:, ind]
        ret[p] = roc(scla, dm_p, nts=nroc)
    return ret
コード例 #17
0
ファイル: se.py プロジェクト: AngelBerihuete/pygp
    def Kgrad_theta(self, theta, x1, i):
        """
        The derivatives of the covariance matrix for
        each hyperparameter, respectively.

        **Parameters:**
        See :py:class:`pygp.covar.CovarianceFunction`
        """
        x1 = self._filter_x(x1)
        # 2. exponentiate params:
        V0 = SP.exp(2*theta[0])
        L  = SP.exp(theta[1:1+self.n_dimensions])
        # calculate squared distance manually as we need to dissect this below
        x1_ = x1/L
        d  = dist.dist(x1_,x1_)
        sqd = (d*d)
        sqdd = sqd.sum(axis=2)
        #3. calcualte withotu derivatives, need this anyway:
        rv0 = V0*SP.exp(-0.5*sqdd)
        if i==0:
            return 2*rv0
        else:
            return rv0*sqd[:,:,i-1]
コード例 #18
0
ファイル: rdj.py プロジェクト: gic888/gdblocks
def distance(lot, et, q, meth):
    if et == None:
        return dist.dist(lot, meth, q)
    else:
        return dist.dist_toset(et, lot, meth, q)
コード例 #19
0
ファイル: timingInfo.py プロジェクト: gic888/gdblocks
 def run(self, pars, out, messages):
     lot = pars['io']['evts']
     out['dm'] = dist.dist(lot, pars['dmeth'], pars['q'], pars['nulls'])
コード例 #20
0
ファイル: grouping.py プロジェクト: gic888/gdblocks
def distthresh(cond, q, dmethod='bin', thresh=.9, nulls=None, trel=False):
    lot = cond['evts']
    dists = dist.dist(lot, q, dmethod, nulls)
    ids = identdthresh(dists, thresh, trel)
    return cond.fuse({'evts': ids})
コード例 #21
0
ファイル: grouping.py プロジェクト: gic888/gdblocks
def dclust(cond, q=1, dmethod='ibl', nulls=None,
           cmeth='tree', nclust=16, cargs={}):
    dists = dist.dist(cond['evts'], dmethod, q, nulls)
    clsts = clustdm(cmeth, dists, nclust, cargs)
    return cond.fuse(cond.new(evts=clsts))
コード例 #22
0
ファイル: knn.py プロジェクト: dngajjar/Courses
def neighbors(this, trainT):
    lst = {}
    for d in range(len(trainT['0'].data[0])):
        that = [trainT['0'].data[s][d] for s in range(len(trainT['0'].data))]
        lst[d] = dist.dist(this, that, range(len(trainT['0'].data)-1), trainT['0'])
    return sorted(lst.items(), key=lambda d:d[1])
コード例 #23
0
ファイル: projection.py プロジェクト: dngajjar/Courses
def project0(east, west, table):
    print '+'
    some = 0.000001 # handles a tedious div/zero error
    c = dist.dist(east, west)