def grade5(): print '=' * 20 + ' TASK 5 ' + '=' * 20 testcases = { 'read_image': [(['testcases/test_image.pgm'], [map(lambda x: i * x, range(15)) for i in range(1, 19)]), (['testcases/test_image2.ppm'], [ map(lambda x: [i * x, i * x + 1, i * x + 2], range(15)) for i in range(1, 19) ])], 'preprocess_image': [([[ map(lambda x: [i * x, i * x + 1, i * x + 2], range(15)) for i in range(19, 1, -1) ]], cluster.readfile('testcases/test_data.csv')), ([[ map(lambda x: [i * x, i * x + 1, i * x + 2], range(15)) for i in range(1, 19) ]], cluster.readfile('testcases/test_data2.csv'))] } grade = 0 for function in testcases: passed = True for inp, out in testcases[function]: try: ret = getattr(image, function)(*inp) except Exception: ret = None if ret != out: print 'Function {} failed a testcase.\n\tInput: {}\n\tExpected return: {}\n\tRecieved return: {}\n'.format( function, str(inp)[1:-1], out, ret) passed = False print ' {} Function {}'.format([u'\u2718', u'\u2713'][passed].encode('utf8'), function) print '-' * 30 grade += passed grade *= 0.5 print 'grade: {}'.format(grade) print '' return grade
def grade8(): print '=' * 20 + ' TASK 8 ' + '=' * 20 with open('testcases/test_labels', 'r') as f: labels = map(lambda x: map(int, x.split()), f.readlines()) with open('testcases/test_labels2', 'r') as f: labels2 = map(lambda x: map(int, x.split()), f.readlines()) with open('testcases/test_decomp', 'r') as f: img = [[map(int, j.split(', ')) for j in i.split('], [')] for i in f.read()[3:-3].split(']], [[')] with open('testcases/test_decomp2', 'r') as f: img2 = [[map(int, j.split(', ')) for j in i.split('], [')] for i in f.read()[3:-3].split(']], [[')] testcases = { 'decompress_image': [([labels, cluster.readfile('testcases/test_centroids.csv')], img), ([labels2, cluster.readfile('testcases/test_centroids2.csv')], img2)] } grade = 0 function = 'decompress_image' passed = True for inp, out in testcases[function]: try: ret = getattr(image, function)(*inp) except Exception: ret = None if ret != out: print 'Function {} failed a testcase.\n\tInput: {}\n\tExpected return: {}\n\tRecieved return: {}\n'.format( function, str(inp)[1:-1], out, ret) passed = False print ' {} Function {}'.format([u'\u2718', u'\u2713'][passed].encode('utf8'), function) print '-' * 30 grade += passed grade *= 0.5 print 'grade: {}'.format(grade) print '' return grade
__author__ = 'esauali' import cluster # blognames, words, data = cluster.readfile('blogdata.txt') # clust = cluster.hcluster(data) # # Print cluster # cluster.printclust(clust, labels=blognames) # # Generate cluster tree image # cluster.drawdendrogram(clust, blognames, jpeg='blogclust.jpg') # # clust_k_means = cluster.kcluster(data, k = 10) # print(clust_k_means) # wants, people, data=cluster.readfile('zebo.txt') # clust = cluster.hcluster(data, distance=cluster.tanimoto) # cluster.drawdendrogram(clust, wants, jpeg='zeboclust.jpg') blognames,words,data=cluster.readfile('blogdata.txt') coords=cluster.scaledown(data) cluster.draw2d(coords,blognames,jpeg='blogs2d.jpg')
import PIL from PIL import Image, ImageDraw from cluster import readfile, dendrogram, rotatematrix, hierarchical_cluster, printcluster blognames, words, data = readfile('database.txt') cluster = hierarchical_cluster(data) printcluster(cluster, labels=blognames) dendrogram(cluster, labels=blognames, jpeg='blogoutput.jpg') rdata = rotatematrix(data) wordcluster = hierarchical_cluster(rdata) dendrogram(wordcluster, labels=words, jpeg='wordoutput.jpg')
def grade7(): print '=' * 20 + ' TASK 7 ' + '=' * 20 with open('testcases/test_labels', 'r') as f: labels = map(lambda x: map(int, x.split()), f.readlines()) with open('testcases/test_labels2', 'r') as f: labels2 = map(lambda x: map(int, x.split()), f.readlines()) with open('testcases/test_image.pgm', 'r') as f: img = f.read() img = img.split(None, 4) with open('testcases/test_image2.ppm', 'r') as f: img2 = f.read() img2 = img2.split(None, 4) testcases = { 'label_image': [([[ map(lambda x: [i * x, i * x + 1, i * x + 2], range(15)) for i in range(19, 1, -1) ], cluster.readfile('testcases/test_centroids.csv')], labels), ([[ map(lambda x: [i * x, i * x + 1, i * x + 2], range(15)) for i in range(1, 19) ], cluster.readfile('testcases/test_centroids2.csv')], labels2)], 'write_image': [(['t1.pgm', [map(lambda x: i * x, range(15)) for i in range(1, 19)]], img), ([ 't2.ppm', [ map(lambda x: [i * x, i * x + 1, i * x + 2], range(15)) for i in range(1, 19) ] ], img2)] } grade = 0 function = 'label_image' passed = True for inp, out in testcases[function]: try: ret = getattr(image, function)(*inp) except Exception: ret = None if ret != out: print 'Function {} failed a testcase.\n\tInput: {}\n\tExpected return: {}\n\tRecieved return: {}\n'.format( function, str(inp)[1:-1], out, ret) passed = False print ' {} Function {}'.format([u'\u2718', u'\u2713'][passed].encode('utf8'), function) print '-' * 30 grade += passed function = 'write_image' passed = True for inp, out in testcases[function]: try: getattr(image, function)(*inp) with open(inp[0], 'r') as f: ret = f.read() ret = ret.split(None, 4) except Exception: ret = None if ret != out: print 'Function {} failed a testcase.\n\tInput: {}\n'.format( function, str(inp)[1:-1]) passed = False print ' {} Function {}'.format([u'\u2718', u'\u2713'][passed].encode('utf8'), function) print '-' * 30 grade += passed grade *= 0.5 print 'grade: {}'.format(grade) print '' return grade
# clusterTest.py import cluster blognames, words, data = cluster.readfile('blogdata.txt') ''' clust = cluster.hcluster(data) # cluster.printclust(clust, labels = blognames) cluster.drawdendrogram(clust, blognames, jpeg = 'blogclust.jpg') ''' ''' kclust = cluster.kcluster(data, k = 10) print([blognames[r] for r in kclust[2]]) ''' ''' wants, people, data = cluster.readfile('zebo.txt') clust = cluster.hcluster(data, distance = cluster.tanimoto) cluster.drawdendrogram(clust, wants) ''' coords = cluster.scaledown(data) cluster.draw2d(coords, blognames, jpeg='blogs2d.jpg')
import cluster sBlogNames, sWords, sData = cluster.readfile('blogdata.txt') sClust = cluster.hcluster(sData) cluster.printclust(sClust, aLabels=sBlogNames) cluster.drawdendrogram(sClust, sBlogNames, 'blogclust.jpg')