Guest User

Untitled

a guest
Mar 29th, 2018
206
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import matplotlib.pylab as pl
  3. import ot
  4.  
  5. #Generate data
  6. data1 = np.zeros((16, 16))
  7. data2 = np.zeros((16, 16))
  8. for x in range(16):
  9.     for y in range(16):
  10.         if x < 8 and y < 8:
  11.             data1[x, y] = 1
  12.         if x >= 8 and y >= 8:
  13.             data2[x, y] = 1
  14.    
  15. #Show generated data
  16. pl.figure(1)
  17. pl.imshow(data1)
  18. pl.title("Data 1")
  19. pl.figure(2)
  20. pl.imshow(data2)
  21. pl.title("Data 2")
  22.    
  23. #Generate distance matrix
  24. bins = np.zeros( ((16*16), 2) )
  25. for x in range(16):
  26.     for y in range(16):
  27.         bins[y*16+x][0] = x
  28.         bins[y*16+x][1] = y
  29. distance = ot.dist(bins, bins)
  30. distance /= distance.max()
  31.  
  32. #Show distance matrix
  33. pl.figure(3)
  34. pl.imshow(distance)
  35. pl.title("Distance Matrix")
  36.    
  37. #organize data for barycenter function
  38. d1 = data1.flatten()
  39. d2 = data2.flatten()
  40. sum1 = sum(d1)
  41. sum2 = sum(d2)
  42.  
  43. #this ensures both w1 and w2 will sum to 1 (probably redundant)
  44. d1 /= sum1
  45. d2 /= sum2
  46. d = np.column_stack((d1, d2))
  47.  
  48. #Generate interpolations for the given values
  49. for t in [0, 0.25, 0.5, 0.75, 1]:
  50.     bary = ot.bregman.barycenter(d, distance, 1e-3, weights=np.array([1-t, t]))
  51.     #this "de-normalizes" the data by multiplying it by the sums, interpolated
  52.     bary *= (sum1 + ((sum2-sum1)*t))
  53.    
  54.     #show interpolation results
  55.     pl.figure()
  56.     pl.imshow(bary.reshape((16, 16)))
  57.     pl.title("Interpolation " + str(t))
  58.  
  59. pl.show()
RAW Paste Data