Guest User

Untitled

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