def aplicar_gap(predicao, gabarito):
        import gap

        resultados = {}

        for lexelt in predicao:
            ranklist = predicao[lexelt]
            gold_ranklist = [palavra for palavra, peso in gabarito[lexelt]]
            gold_weights = [peso for palavra, peso in gabarito[lexelt]]
            resultados[lexelt] = gap.gap(ranklist, gold_ranklist, gold_weights)

        return resultados, Util.media(resultados.values())
Beispiel #2
0
    def plot_gap(self, *args, **kwargs):
        if self.clusters != 1:
            raise RuntimeError("GAP only defined for 1 cluster")

        f = self.layer_outputs()
        embedded, _ = self.predict(f)
        embedded2 = np.apply_along_axis(lambda x: x[0], 1, embedded)
        x = list(range(1, 21))
        g = gap.gap(embedded2, ks=x)
        fig, ax = plt.subplots()
        ax.plot(x, g)
        ax.set_title("GAP Statistic on un-clustered dataset")
        ax.set_xlabel("Clusters (k)")
        ax.set_ylabel("GAP statistic")
        ax.set_xticks(x)
        fig.savefig(os.path.join(self.img_dir, "gap.png"), dpi=300)
        plt.close(fig)
Beispiel #3
0
    def estimate_k_clusters(self, X, max_num_clusters):

        from gap import gap
        num_clusters = gap(X, max_num_clusters=max_num_clusters)

        # silhouette_score is between [-1,1],
        # so set score of 0 cluster as -1.0
        # so set score of 1 cluster as 0.0
        score = np.zeros(max_num_clusters + 1)
        score[0] = -1.0

        for k in range(2, max_num_clusters + 1):
            labels = KMeans(n_clusters=k).fit_predict(X)
            score[k] = silhouette_score(X, labels)

        print('silhouette   :', np.argmax(score))
        return num_clusters
Beispiel #4
0
def main():
    global loopCount, scoreCount, touch
    while True:
        gapSize = random.randint(5, 25)
        if loopCount % 90 == 0:
            topPos = random.randint(0, height / 2) - 400
            pipes.add(pipe((width + 100, topPos + gapSize + 800)))
            score.add(gap((width + 100, topPos + gapSize + 800)))
            pipes.add(pipe((width + 100, topPos), True))

        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    player.speed[1] = -10

        player.update()
        pipes.update()
        score.update()

        gets_hit = pygame.sprite.spritecollide(player, pipes, False) \
            or player.rect.center[1] > height

        point_get = pygame.sprite.spritecollide(player, score, True)

        screen.blit(background, [0, 0])
        pipes.draw(screen)
        score.draw(screen)
        screen.blit(player.image, player.rect)
        pygame.display.flip()
        loopCount += 1

        if gets_hit:
            lose()

        #print("Touch = "+str(touch)+" Point Get? = "+str(point_get))
        if point_get and touch == False:
            scoreCount += 1
            print("Score: " + str(scoreCount))
            touch = True
        else:
            touch = False
Beispiel #5
0
def main():
	parser = argparse.ArgumentParser(prog='invocaGap.py',
	 description='Testing gap staticstics',
	 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
	parser.add_argument('--sourceFolder',
	 help='Source folder',
	 type=str, required=True)
	parser.add_argument('--outputFolder',
	 help='Output folder',
	 type=str, required=True)
	 
	args = parser.parse_args()

	#Source folder of the files with the timestamps
	sourceFolder = rfe.fixPath(args.sourceFolder)
	if not os.path.exists(sourceFolder):
		print ''
		print 'Source folder does not exists ' + sourceFolder
		print ''
		sys.exit()

	#Output folder for the graphics and files
	outputFolder = rfe.fixPath(args.outputFolder)
	if not os.path.exists(outputFolder):
		try:
			os.makedirs(outputFolder)
		except:
			print ''
			print 'Unable to create folder ' + outputFolder
			print ''
			sys.exit()
		
	#dataCluster stores the data to be used for the clustering process
	#the size is equal to the number of frames, aka, the time component
	#plus 5 as we are incorporating the 2 dimensions of the ellipse, 
	#x position, y position and angle
	dataCluster = zeros((1,27))
	units = []
	dato = empty((1,1))
	for unitFile in os.listdir(sourceFolder):
		if os.path.isdir(sourceFolder+unitFile):	
			dato = empty((1,1))		
			unitName = unitFile.rsplit('_', 1)[0]
			#print unitName
			dataUnit, coordinates = rfe.loadSTACurve(sourceFolder,unitFile,unitName)
			xSize = dataUnit.shape[0]
			ySize = dataUnit.shape[1]
			fitResult = rfe.loadFitMatrix(sourceFolder,unitFile)
			#Time data from STA with gauss fit
			#dataUnitTemporal = scipy.ndimage.gaussian_filter(dataUnit[coordinates[0][0],[coordinates[1][0]],:],2)
			#Time data from STA without  gauss fit
			dataUnitTemporal = dataUnit[coordinates[0][0],[coordinates[1][0]],:]
			#Time data from FITResult
			#dataUnitTemporal = rfe.loadVectorAmp(sourceFolder,unitFile).T
			#A radius of the RF ellipse
			aRadius = fitResult[0][2]
			dato[0] = aRadius
			dataUnitCompleta = concatenate((dataUnitTemporal,dato),1)
			#B radius of the RF ellipse
			bRadius = fitResult[0][3]
			dato[0] = bRadius
			dataUnitCompleta = concatenate((dataUnitCompleta,dato),1)
			#angle of the RF ellipse
			angle = fitResult[0][1]
			dato[0] = angle
			dataUnitCompleta = concatenate((dataUnitCompleta,dato),1)
			#X coordinate of the RF ellipse
			xCoordinate = fitResult[0][4]
			#print 'xCoordinate',xCoordinate
			dato[0] = xCoordinate
			dataUnitCompleta = concatenate((dataUnitCompleta,dato),1)
			#Y coordinate of the RF ellipse
			yCoordinate = fitResult[0][5]
			#print 'yCoordinate',yCoordinate
			dato[0] = yCoordinate
			dataUnitCompleta = concatenate((dataUnitCompleta,dato),1)
			#Area of the RF ellipse
			area = aRadius*bRadius*pi
			dato[0] = area
			dataUnitCompleta = concatenate((dataUnitCompleta,dato),1)
			#UnitName
			dato=empty(1, dtype='|S16')
			dato[0]=unitName
			dataUnitCompleta = concatenate((dataUnitCompleta,dato.reshape(1, 1)),1)
			
			dataCluster = append(dataCluster,dataUnitCompleta, axis=0)
			
			units.append(unitName)
	# remove the first row of zeroes
	dataCluster = dataCluster[1:,:]	
	
	data = dataCluster[:,0:19]
	data = data.astype(float64, copy=False)
	
	gaps = gap.gap(data, refs=None, nrefs=len(data), ks=range(1,10))

	dgap = zeros(len(gaps))
	for i in range(len(gaps)-1):
		dgap[i] = gaps[i]-gaps[i+1]
	
	plt.plot(gaps)
	plt.show()
	
	plt.plot(dgap)
	plt.show()
	
	return 0
Beispiel #6
0
from gap import gap
import os

pwd = os.getcwd()
files = os.listdir(pwd + "/bands")

fo = open("phase3d.dat", "w")

for f in files:
    os.system("cp bands/" + f + "  BANDS.OUT")
    l = f.split("_")
    mu = l[2]
    zee = l[4]
    g = gap()
    fo.write(str(mu) + "  " + str(zee) + "  " + str(g) + "\n")

fo.close()
Beispiel #7
0
from gap import gap
import os

pwd = os.getcwd()
files = os.listdir(pwd+"/bands")

for f in files:
  os.system("cp bands/"+f+"  BANDS.OUT")
  l = f.split("_")
  mu = l[2]
  zee = l[4]
  g = gap()
  os.system("tb90-bands -noshow")
  os.system("cp BANDS.png images/"+f+".png")
  print mu,zee,g
Beispiel #8
0
import io
import sys
import gap

# my_ranklist is the output of any paraphrase-ranking algorithm (let's say it gives top 5 words)
my_ranklist = ['clever', 'intelligent', 'luminous', 'hopeful', 'intelligent'];
# gold_ranklist is the actual gold data that was ranked by people (assume 5 people were asked, and this is what they chose)
gold_ranklist = ['clever', 'intelligent', 'smart'];
# the i-th element in gold_weights gives the weight associated with corresponding element in gold_ranklist.
# for example, 3 people told 'clever', and 1 each told 'intelligent' and 'smart'.
gold_weights = [3, 1, 1];

print >>sys.stderr,  my_ranklist;
print >>sys.stderr,  gold_ranklist;
print >>sys.stderr,  gold_weights;

print 'average precision = ' + str(gap.average_precision(my_ranklist, gold_ranklist));
print 'GAP = ' + str(gap.gap(my_ranklist, gold_ranklist, gold_weights));


my_ranklist = ['luminous', 'hopeful', 'intelligent', 'clever', 'intelligent'];
gold_ranklist = ['clever', 'intelligent', 'smart'];
gold_weights = [3, 1, 1];
print >>sys.stderr,  my_ranklist;
print >>sys.stderr,  gold_ranklist;

print 'average precision = ' + str(gap.average_precision(my_ranklist, gold_ranklist));
print 'GAP = ' + str(gap.gap(my_ranklist, gold_ranklist, gold_weights));