Advertisement
dan-masek

Untitled

Aug 31st, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import numpy as np
  2. import pickle
  3.  
  4. IMAGE_WIDTH = 3
  5. IMAGE_HEIGHT = 3
  6. FILE_COUNT = 3
  7.  
  8. def gen_sample(count, width, height):
  9.     with open('data/raw_data.pickle', 'wb') as f:
  10.         for i in range(count):
  11.             data = np.arange(9, dtype=np.float64) + i * 10
  12.             data = data.reshape(height, width)
  13.             pickle.dump(data, f)
  14.             print i, data
  15.  
  16. def preprocess(count, height):
  17.     outfiles = []
  18.     for i in range(height):
  19.         outfilename = 'data/row_%03d.dat' % i
  20.         outfiles.append(open(outfilename, 'wb'))
  21.  
  22.  
  23.     with open('data/raw_data.pickle', 'rb') as f:
  24.         for i in range(count):
  25.             data = pickle.load(f)
  26.             for j in range(height):
  27.                 data[j].tofile(outfiles[j])
  28.  
  29.     for i in range(height):
  30.         outfiles[i].close()
  31.  
  32. def process(width, height):
  33.     result_rows = []
  34.  
  35.     for i in range(height):
  36.         outfilename = 'data/row_%03d.dat' % i
  37.         data = np.fromfile(outfilename, dtype=np.float64).reshape(-1, width)
  38.         median_row = np.median(data, axis=0)
  39.         result_rows.append(median_row)
  40.         print i, data
  41.  
  42.     return np.vstack(result_rows)
  43.  
  44. gen_sample(FILE_COUNT, IMAGE_WIDTH, IMAGE_HEIGHT)
  45. preprocess(FILE_COUNT, IMAGE_HEIGHT)
  46. result = process(IMAGE_WIDTH, IMAGE_HEIGHT)
  47. print "Result", result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement