Advertisement
Guest User

Untitled

a guest
Feb 25th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from PIL import Image, ImageDraw
  4. import random
  5. import re
  6. import gc
  7. import codecs
  8. import operator
  9.  
  10. wlinks = dict()
  11. wpos = dict()
  12. grid = dict()
  13.  
  14. GRID_SIZE = 3000
  15.  
  16. def intToRGB(x,maxa):
  17.  
  18.     if x>maxa: x = maxa
  19.     hmaxa = maxa/3
  20.  
  21.     if x>2*hmaxa:
  22.         red = 255
  23.         green = (3-x/hmaxa)*255
  24.         blue = 0
  25.  
  26.     elif x>hmaxa:
  27.         red = (2-x/hmaxa)*255
  28.         green = 255
  29.         blue = 0
  30.     else:
  31.         red = 0
  32.         green = 255
  33.         blue = (1-x/hmaxa)*255
  34.  
  35.  
  36.     return (int(red),int(green),int(blue))
  37.  
  38. def main():
  39.     lnum = 0
  40.     ind = 0
  41.  
  42.     wlist = dict()
  43.     wsize = dict()
  44.  
  45.     for line in codecs.open('indexes.csv','r', 'utf-8', 'strict', 20000000):
  46.         words = line.split('\t')
  47.         index2 = int(words[0])
  48.         wsize[index2] = int(words[2])
  49.         wlist[words[1]] = index2
  50.         del words
  51.         del line
  52.        
  53.    
  54.     for line in codecs.open('mygraph2.csv','r', 'utf-8', 'strict', 20000000):
  55.         lnum += 1
  56.         if (lnum % 100000) == 0: print 'Line number: %i'%(lnum/100000)
  57.         words = line.split('\t')
  58.         del line
  59.  
  60.         words0 = words[0].strip().lower()
  61.         words1 = words[1].strip().lower()
  62.         del words
  63.  
  64.         words0 = wlist[words0]
  65.         words1 = wlist[words1]
  66.  
  67.         if not words0 in wlinks:
  68.             wlinks[words0] = set()
  69.    
  70.             x = random.randint(0,GRID_SIZE-1)
  71.             y = random.randint(0,GRID_SIZE-1)
  72.    
  73.             wpos[words0] = (x,y)
  74.            
  75.  
  76.         if not words1 in wlinks:
  77.             wlinks[words1] = set()
  78.    
  79.             x = random.randint(0,GRID_SIZE-1)
  80.             y = random.randint(0,GRID_SIZE-1)
  81.    
  82.             wpos[words1] = (x,y)
  83.  
  84.  
  85.         wlinks[words0].add(words1)
  86.         wlinks[words1].add(words0)
  87.  
  88.  
  89.     del wlist      
  90.  
  91. #   for x in xrange(0,GRID_SIZE):
  92. #       for y in xrange(0,GRID_SIZE):
  93. #           grid[x,y] = None
  94.  
  95.  
  96.     for it in xrange(1,1000):
  97.         print 'Iteration %i' % it
  98.    
  99.         for wordname, links in wlinks.iteritems():
  100.  
  101.             oldpos = wpos[wordname]
  102.  
  103.             x = oldpos[0]
  104.             y = oldpos[1]
  105.  
  106.             for link in iter(links):
  107.                 x += wpos[link][0]
  108.                 y += wpos[link][1]
  109.  
  110.             x = int(x/(len(links)+1))+random.randint(-5,5)
  111.             y = int(y/(len(links)+1))+random.randint(-5,5)
  112.  
  113.  
  114.             if x>0 and x<GRID_SIZE-1 and y>0 and y<GRID_SIZE-1:
  115.  
  116.                 for a in range(-1,2):
  117.                     for b in range(-1,2):
  118.                         if (x+a,y+b) in grid and grid[x+a,y+b] != None:
  119.                             x-=a*10
  120.                             y-=b*10
  121.  
  122.             if x<0: x = 0
  123.             if y<0: y = 0
  124.  
  125.             if x>=GRID_SIZE: x = GRID_SIZE-1
  126.             if y>=GRID_SIZE: y = GRID_SIZE-1
  127.  
  128.             wpos[wordname] = [int(x),int(y)]
  129.  
  130.             old = None
  131.  
  132.             if (x,y) in grid: old = grid[x,y]
  133.                
  134.  
  135.             grid[x,y] = wordname
  136.             grid[oldpos[0],oldpos[1]] = old
  137.             if old != None: wpos[old] = [oldpos[0],oldpos[1]]
  138.  
  139.        
  140.         if it%10 == 0:
  141.             im = Image.new('RGB', (GRID_SIZE, GRID_SIZE), "black")
  142.             pixels = im.load()
  143.  
  144.             for x in xrange(0,GRID_SIZE):
  145.                 for y in xrange(0,GRID_SIZE):
  146.                     if not (x,y) in grid or grid[x,y] == None:
  147.                         pixels[x,y] = (0,0,0)
  148.                     else:
  149.                         bri = wsize[grid[x,y]]
  150.                         pixels[x,y] = intToRGB(bri,100)
  151.  
  152.  
  153.             im.save("output_image_"+str(it)+".png", "PNG")
  154.             del im             
  155.            
  156.  
  157. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement