def nxmx3(m): sh = m.shape m2 = m.copy() # working copy, might chance assert (len(sh) == 2 or len(sh) == 3) if len(sh) == 3: if sh[2] == 3: # matrix is already mxnx3 return m2 else: # matrix is MxNxD with D!=3, best we can do: m2 = pylab.mean(m2, 2) # here, m2 is MxN... return pylab.tile(m2.reshape(sh[0], sh[1], 1), (1, 1, 3))
def make_error(data_file, xin=None): databox = spinmob.data.load(data_file) errs = [] xs = databox.c('c8') xs -= xs[0] for i in range(6): vals = databox.c('c{:d}'.format(i)) if xin: vals = pylab.array([v for x, v in zip(xs, vals) if xin[0] <= x <= xin[1]]) std = pylab.std(vals) errs.append(std) err = pylab.mean(errs) return err, errs
def image_derivative(im, derivative, do_scale=True): assert (len(im.shape) == 2 or len(im.shape) == 3) assert (im.dtype == 'float') assert (derivative in ['x', 'y', 'grad']) # obtain a gray image if len(im.shape) == 3: gray_im = pylab.mean(im, 2) else: gray_im = im # either x,y derivative or full gradient if derivative == 'grad': dy = ndimage.sobel(gray_im, 0) dx = ndimage.sobel(gray_im, 1) deriv_im = pylab.sqrt(dy * dy + dx * dx) else: deriv_im = ndimage.sobel(gray_im, {'x': 1, 'y': 0}[derivative]) return imscale(deriv_im) if do_scale else deriv_im
image = Image.open(tv03_file) image2 = Image.open(bmp_time) #img1 = image.crop((64,0,512,512)) #TV03 Image cropping img1 = image.crop((210, 80, 512, 492)) #TV03 Image cropping img1.save('tv03_tmp.png') #img2 = image2.crop((80,54,528,566)) #TV03 Image cropping img2 = image2.crop((150, 80, 520, 540)) #TV01 Image cropping img2.save('tv01_tmp.png') #a = pylab.imread(bmp_time) a = pylab.imread('tv01_tmp.png') #generates a RGB image, so do fig_bmp = pylab.mean(a, 2) # to get a 2-D array #DPI = pylab.gcf().get_dpi() #Size = fig_bmp.shape #print 'Size: ', Size #GCF = pylab.gcf() #DPI = GCF.get_dpi() #print "DPI: ", DPI #b_box_1 = (top, left, bottom,right) #b_box_1 = (10,10,200,200) #a = a.crop(b_box_1); ax3 = pylab.subplot(gs[0]) #b = pylab.imread(tv03_file) b = pylab.imread('tv03_tmp.png')
def print_stats(list_1): print("\t N\t", len(list_1)) print("\t mean\t", pylab.mean(list_1)) print("\t error\t", pylab.std(list_1) / pylab.sqrt(len(list_1)))
def float3d_to_uint2d(im): return pylab.array(pylab.mean(im, 2) * 255, dtype='uint8')
#GCF = pylab.gcf() #DPI = GCF.get_dpi() #print "DPI: ", DPI #b_box_1 = (top, left, bottom,right) #b_box_1 = (10,10,200,200) #a = a.crop(b_box_1); ax3 = pylab.subplot(gs[1]) pylab.title(string_time,fontsize=40) #b = pylab.imread(tv03_file) b = pylab.imread('tv03_tmp.png') fig_tv03=pylab.mean(b,2) print 'done' #fig_bmp = Image.open(datafile) #dpi = pylab.rcParams['figure.dpi'] #figsize = fig_bmp.size[0]/dpi, fig_bmp.size[1]/dpi #figure(figsize=figsize) #ax2.axis('scaled') #ax2.get_xaxis().set_ticks([]); #ax2.get_yaxis().set_ticks([]);
def mean_path_score(G, paths): paths = list(paths) if len(paths) == 0: return 0 scores = map(lambda path : path_score(G, path), paths) return PP.mean(scores)
#GCF = pylab.gcf() #DPI = GCF.get_dpi() #print "DPI: ", DPI #b_box_1 = (top, left, bottom,right) #b_box_1 = (10,10,200,200) #a = a.crop(b_box_1); ax3 = pylab.subplot(gs[1]) pylab.title(string_time, fontsize=40) #b = pylab.imread(tv03_file) b = pylab.imread('tv03_tmp.png') fig_tv03 = pylab.mean(b, 2) print 'done' #fig_bmp = Image.open(datafile) #dpi = pylab.rcParams['figure.dpi'] #figsize = fig_bmp.size[0]/dpi, fig_bmp.size[1]/dpi #figure(figsize=figsize) #ax2.axis('scaled') #ax2.get_xaxis().set_ticks([]); #ax2.get_yaxis().set_ticks([]);
image = Image.open(tv03_file) image2 = Image.open(bmp_time) #img1 = image.crop((64,0,512,512)) #TV03 Image cropping img1 = image.crop((210,80,512,492)) #TV03 Image cropping img1.save('tv03_tmp.png') #img2 = image2.crop((80,54,528,566)) #TV03 Image cropping img2 = image2.crop((150,80,520,540)) #TV01 Image cropping img2.save('tv01_tmp.png') #a = pylab.imread(bmp_time) a = pylab.imread('tv01_tmp.png') #generates a RGB image, so do fig_bmp=pylab.mean(a,2) # to get a 2-D array #DPI = pylab.gcf().get_dpi() #Size = fig_bmp.shape #print 'Size: ', Size #GCF = pylab.gcf() #DPI = GCF.get_dpi() #print "DPI: ", DPI #b_box_1 = (top, left, bottom,right) #b_box_1 = (10,10,200,200) #a = a.crop(b_box_1); ax3 = pylab.subplot(gs[0]) #b = pylab.imread(tv03_file) b = pylab.imread('tv03_tmp.png')
result = [] for year in years: daily_temp_365 = pylab.zeros(365) daily_temp_366 = pylab.zeros(366) for city in multi_cities: if len(climate.get_yearly_temp(city, year)) == 365: daily_temp_365 += climate.get_yearly_temp(city, year) else: daily_temp_366 += climate.get_yearly_temp(city, year) if sum(daily_temp_365) > sum(daily_temp_366): daily_temp = daily_temp_365 else: daily_temp = daily_temp_366 daily_temp = daily_temp/len(multi_cities) mean = pylab.mean(daily_temp) var = 0.0 for temp in list(daily_temp): var += (temp - mean)**2 result.append(math.sqrt(var/len(daily_temp))) return pylab.array(result) def evaluate_models_on_testing(x, y, models): """ For each regression model, compute the RMSE for this model and plot the test data along with the model’s estimation. For the plots, you should plot data points (x,y) as blue dots and your best fit curve (aka model) as a red solid line. You should also label the axes of this figure appropriately and have a title reporting the following
# x = P.hstack([x + P.randn(len(x))*.02*v, x + P.randn(len(x))*.01*v, x]) # y = P.hstack([y, y, y]) print("Calculating error stats...") ah = 8 a = y.reshape(-1)[:-ah] ax = x.reshape(-1) print("VARIANCE") print(P.var(a)) print("STD") print(P.std(a)) A = P.vstack([a[i:(i - ah)] for i in range(ah)]) AX = P.vstack([ax[i:(i - 2 * ah)] for i in range(2 * ah)]) A = P.vstack([A, AX, ax[2 * ah:]]) b = a[ah:] A = A - P.mean(A, 1).reshape(-1, 1) b = b - P.mean(b) print("LMMSE with {0} taps".format(ah)) LMMSE = P.mean((b - A.T.dot(P.inv(P.dot(A, A.T)).dot(A.dot(b))))**2) print(LMMSE) print("LMRMSE with {0} taps".format(ah)) print(P.sqrt(LMMSE)) def mu_law(a, mu=256, MAX=None): mu = mu - 1 a = P.array(a) MAX = a.max() if MAX is None else MAX a = a / MAX y = (1 + P.sign(a) * P.log(1 + mu * abs(a)) / P.log(1 + mu)) / 2 inds = P.around(y * mu).astype(P.uint8)