Advertisement
ToinasMaker

Untitled

Oct 2nd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.36 KB | None | 0 0
  1. import time
  2. import csv
  3. import pandas as pd
  4. import numpy
  5. import threading
  6. from IPython.display import display as ds
  7.  
  8.  
  9. filename='C:/code/IntelligentSuit/tweet.csv'
  10. intel_blue=[0,0,255]
  11. black=[0,0,0]
  12. white=[255,255,255]
  13. red=[255,0,0]
  14.  
  15. imec_image=[2,2,2,0,0,0,0,0,0,0,0,0,0,2,2,0,
  16. 0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
  17. 0,2,0,0,0,0,0,0,0,0,0,0,0,0,2,0,
  18. 2,2,2,1,0,0,0,1,0,0,0,0,0,2,2,2,
  19. 0,0,0,1,1,0,1,1,0,0,0,0,0,1,1,1,
  20. 0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,
  21. 0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,1,
  22. 0,0,0,0,0,0,0,2,2,2,0,0,0,1,1,1,
  23. 0,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,
  24. 0,0,0,0,0,0,0,2,2,0,0,0,0,0,2,0,
  25. 0,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,
  26. 0,0,0,0,0,0,0,2,2,2,0,0,0,1,1,1,
  27. 0,0,0,0,0,0,0,0,0,1,1,1,0,1,0,1,
  28. 0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,
  29. 0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,
  30. 0,0,0,0,0,0,0,0,0,1,1,1,0,1,1,1]
  31.  
  32. spy_down=[0,0,2,0,0,0,0,0,0,0,0,0,2,0,0,0,
  33. 0,0,2,0,0,2,2,2,2,2,0,0,2,0,0,0,
  34. 0,0,2,0,2,1,1,1,1,1,2,0,2,0,0,0,
  35. 0,0,2,0,2,1,2,1,2,1,2,0,2,0,0,0,
  36. 0,0,2,0,2,1,1,1,1,1,2,0,2,0,0,0,
  37. 0,0,2,0,2,1,1,1,1,1,2,0,2,0,0,0,
  38. 0,0,2,0,0,2,2,2,2,2,0,0,2,0,0,0,
  39. 0,0,0,2,0,0,2,2,2,0,0,2,0,0,0,0,
  40. 0,0,0,0,2,2,2,2,2,2,2,0,0,0,0,0,
  41. 0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,
  42. 0,0,0,0,0,0,2,0,2,0,0,0,0,0,0,0,
  43. 0,0,0,0,0,0,2,0,2,0,0,0,0,0,0,0,
  44. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  45. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  46. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  47. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  48.  
  49. spy_image=[0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
  50.     0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,
  51.     0,0,0,1,1,2,2,2,2,2,2,2,1,1,0,0,
  52.     0,0,0,1,1,2,2,2,2,2,2,2,1,1,0,0,
  53.     0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,
  54.     0,0,0,1,1,2,1,1,2,1,1,2,1,1,0,0,
  55.     0,0,0,1,1,2,1,1,2,1,1,2,1,1,0,0,
  56.     0,0,0,1,1,2,2,2,2,2,2,2,1,1,0,0,
  57.     0,0,0,0,1,1,2,2,2,2,2,1,1,0,0,0,
  58.     0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
  59.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  60.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  61.     0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,
  62.     0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,
  63.     0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,
  64.     0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0]
  65.  
  66.  
  67.    
  68. def Image_Setup():     
  69.     """device = Adafruit_NeoPixel(256)     
  70.     device.setBrightness(0.01)"""
  71.     intel_blue=[0,0,255]
  72.     black=[0,0,0]
  73.     white=[255,255,255]
  74.     red=[255,0,0]  
  75.     image = []
  76.     for idx in range(len(spy_image)):
  77.         if spy_image[idx] == 0:
  78.             image.append(white)
  79.         if spy_image[idx]== 1:
  80.             image.append(intel_blue)
  81.         if spy_image[idx]== 2:
  82.             image.append(black)
  83.     return image
  84.  
  85. def Translate_Image(image_in):
  86.     image_out=[]
  87.     for idx in range(len(image_in)):
  88.         if image_in[idx] == 0:
  89.             image_out.append(white)
  90.         if image_in[idx]== 1:
  91.             image_out.append(intel_blue)
  92.         if image_in[idx]== 2:
  93.             image_out.append(black)
  94.     return image_out
  95.    
  96. def Free_Image_Setup():
  97.     """device = Adafruit_NeoPixel(256)     
  98.     device.setBrightness(0.01)"""
  99.     intel_blue=[0,0,255]
  100.     black=[0,0,0]
  101.     white=[255,255,255]
  102.     red=[255,0,0]
  103.  
  104.     free_image = Translate_Image(imec_image)
  105.     return free_image
  106. def blit(image, img_width, location, frame_size):
  107.     frame=[black] * frame_size ** 2
  108.     vert_offset=0
  109.  
  110.     img_lines = [image[x:x+img_width] for x in range(0,len(image),img_width)]
  111.     for line in img_lines:
  112.         for idx, value in enumerate(line):
  113.         coord = idx+vert_offset+(location[0] % frame_size)+((location[1] % frame_size)*frame_size)
  114.         # HANDLE EDGE SCROLLING
  115.        # print coord, idx, vert_offset, (vert_offset+(location[1]*frame_size)+frame_size)-1, location, frame_size
  116.         if (coord > (vert_offset+(location[1]*frame_size)+frame_size)-1):
  117.         coord = coord - frame_size
  118.  
  119.         coord = coord % (frame_size ** 2)
  120.             frame[coord] = value
  121.         vert_offset += frame_size
  122.     return frame
  123.  
  124. def display(width, frame):
  125.  
  126.     lines = [frame[x:x+width] for x in range(0,len(frame),width)]
  127.     flipped_frame =[]
  128.     for idx,line in enumerate(lines):
  129.     if idx % 2 == 0:
  130.         flipped_frame+=reversed(line)
  131.        
  132.     else:
  133.         flipped_frame+=line
  134.  
  135.     # for idx, color in enumerate(flipped_frame):
  136.     # device.setPixelColorRGB(idx, color[0], color[1], color[2])
  137.     # device.show()
  138.  
  139. def Vertical_Scroll(image):
  140.     x=0
  141.     y=0
  142.     vert=1
  143.     horiz=0
  144.    
  145.     while y < 16:
  146.         frame = blit(image, 16, [x,int(y)], 16)
  147.         print y
  148.         #display(16, frame)
  149.         time.sleep(0.2)
  150.         y+=1       
  151.        
  152. def Horizontal_Scroll(image):
  153.     x=0
  154.     y=0
  155.     vert=0
  156.     horiz=1
  157.    
  158.     while x < 16:                  
  159.         frame = blit(image, 16, [x,int(y)], 16)
  160.         print x    
  161.         #display(16, frame)
  162.         time.sleep(0.2)
  163.         x+=1
  164.  
  165. def printdiffs(df_in)
  166.     df_in = find_diffs(df_in)
  167.     return df_in
  168.    
  169.    
  170.        
  171. def find_diffs(df_in):
  172.     df_latest = pd.read_csv(filename)
  173.     # print("old frame is:\n{}".format(df_in)) 
  174.     # print("newdata frame is:\n{}".format(df_latest))
  175.     diff_df = pd.merge(df_latest, df_in, how='outer', indicator='Exist')
  176.     print 'old dataframe size is' + str(df_in.size)
  177.     print 'new dataframe size is' + str(df_latest.size)
  178.    
  179.     diff_df = diff_df.loc[diff_df['Exist'] != 'both']
  180.     print 'new dataframe size is' + str(diff_df.size)
  181.    
  182.     if diff_df.size >= 1:
  183.         print 'diff found ' + str(diff_df.size)
  184.         print str(diff_df.size)
  185.         ds_list = df_latest.values[-1].tolist()
  186.         print ds_list[1]
  187.         print "just the value" + str(df_latest[1])
  188.         df_in = df_latest
  189.        
  190.     return diff_df
  191.    
  192.    
  193. if __name__== '__main__':
  194.  
  195.     df_last = pd.DataFrame()
  196.     df_in = pd.DataFrame()
  197.     df_in = pd.read_csv(filename)  
  198.     #csvFile.close()
  199.     i=0
  200.    
  201.     while True:  
  202.         print("Polling Tweets")
  203.         imageToDisplay=Free_Image_Setup()
  204.         print(imageToDisplay)
  205.         print "free image"
  206.         Horizontal_Scroll(imageToDisplay)
  207.         df_last= printdiffs(df_in)
  208.         print "# new tweets" + str(df_last.size)
  209.         time.sleep(7)
  210.         i = i +1
  211.    
  212.         if (df_last.size > 0):         
  213.             imageToDisplay=Image_Setup()
  214.             print(imageToDisplay)
  215.             print "spy"
  216.             Horizontal_Scroll(imageToDisplay)
  217.             df_last = df_in
  218.            
  219.         # else:
  220.             # imageToDisplay=Free_Image_Setup()
  221.             # print "free"
  222.             # print(imageToDisplay + "Free Image" )
  223.             # #Horizontal_Scroll(imageToDisplay)
  224.  
  225.    
  226. """
  227. def Vertical_Scroll(image):
  228.     x=0
  229.     y=0
  230.     vert=1
  231.     horiz=0
  232.    
  233.     while y < 16:
  234.         frame = blit(image, 16, [x,int(y)], 16)
  235.         print frame    
  236.         display(16, frame)
  237.         time.sleep(0.2)
  238.         y+=1       
  239.        
  240. def Horizontal_Scroll(image):
  241.     x=0
  242.     y=0
  243.     vert=0
  244.     horiz=1
  245.    
  246.     while x < 16:                  
  247.         frame = blit(image, 16, [x,int(y)], 16)
  248.         print frame    
  249.         display(16, frame)
  250.         time.sleep(0.2)
  251.         x+=1
  252.  
  253. if __name__ == "__main__":
  254.     device = Adafruit_NeoPixel(256)    
  255.     device.setBrightness(0.01)
  256.     df_last = pd.DataFrame()
  257.     df_in = pd.DataFrame()
  258.     df_in = pd.read_csv(filename)  
  259.     i=0
  260.    
  261.     while True:  
  262.         print("Polling Tweets")
  263.         imageToDisplay=Free_Image_Setup()
  264.         print(imageToDisplay)
  265.         Horizontal_Scroll(imageToDisplay)
  266.         df_in= printdiffs(df_in)
  267.         time.sleep(7)
  268.         i = i +1
  269.    
  270.         if (df_in.size >> 1):          
  271.             imageToDisplay=Image_Setup()
  272.             print(imageToDisplay)
  273.             Horizontal_Scroll(imageToDisplay)
  274.         else:
  275.             imageToDisplay=Free_Image_Setup()
  276.             print(imageToDisplay)
  277.             Horizontal_Scroll(imageToDisplay)
  278.  
  279. """
  280.  
  281. # i=0
  282. # j=0
  283. # last_tweet=""
  284. # tweet_date=""
  285. # tweet_text=""
  286.  
  287. # with open(filename, 'r') as f:
  288.     # for row in reversed(list(csv.reader(f))):
  289.         # if i==0:
  290.             # print(', '.join(row))
  291.             # i+=1         
  292.             # if row == last_tweet:
  293.                 # break
  294.             # else:
  295.                 # for column in row:
  296.                     # if j==0:
  297.                         # if tweet_date=="":
  298.                             # tweet_date=column
  299.                             # print tweet_date
  300.                            
  301.                     # if j==1:
  302.                         # tweet_text=column
  303.                         # print tweet_text
  304.                        
  305.                     # j=+1
  306.  
  307.                
  308.             # if last_tweet == "":
  309.                 # last_tweet = row
  310.                 # print last_tweet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement