def test_receptionmatrix(printvaleur=True): Map_hauteur = np.zeros(shape=(50, 50)) Map_energie = np.zeros(shape=(50, 50)) Map_hauteur[25, 25] = 5 Map_energie[25, 25] = 100000000 # Map_hauteur[5,6] =1 # Map_energie[5, 6] =100000 R = 2 dteta = 5*10**-2 dh = 0.5*10**-1 nphoton = 20 mat = sender(Map_hauteur, 25, 25, Map_energie, R, dteta, dh, nphoton, 1) def f(x): return np.log10(x) f = np.vectorize(f) np.set_printoptions(precision=3) print(mat) fig, ax = plt.subplots() ax.matshow(f(mat), cmap=plt.get_cmap("afmhot")) if printvaleur: for (i, j), z in np.ndenumerate(mat): ax.text(j, i, '{:0.01f}'.format(z), ha='center', va='center') plt.savefig('uniform_high_res.png', format='svg', dpi=2000) fig.show()
def hist_quant(ax, x, y, xlog, ylog, xmin, xmax, Nxbins, Nybins, xbinedges, ybinedges, dath): if not (xmin): xmin = np.min(x) if not (xmax): xmax = np.max(x) # Find the bin edges for the x axis if not xbinedges: if not Nxbins: Nxbins = 20 if xlog: xbinedges = np.logspace(np.log10(xmin), np.log10(xmax), Nxbins + 1) else: xbinedges = np.linspace(xmin, xmax, Nxbins + 1) # Now make the arrays for the quantiles in the y direction if not ybinedges: if not Nybins: Nybins = 10 ybinedges = np.array(range(Nybins + 1)) / float(Nybins) # Final array is quantiles = np.zeros((Nxbins, Nybins)) # ...and crank! for i in range(len(x)): id_near = (np.abs(xbinedges - x[i])).argmin() if not ((x[i] - xbinedges[id_near] >= 0) and (id_near != Nxbins)): id_near -= 1 qu_near = (np.abs(ybinedges - y[i])).argmin() if not ((y[i] - ybinedges[qu_near] >= 0) and (qu_near != Nybins)): qu_near -= 1 quantiles[id_near, qu_near] += 1 # Now have to normalise over each x value heatmap = np.zeros(quantiles.shape) norm = 1.0 / len(y) for xcol in range(Nxbins): ycol = quantiles[xcol, :] for i in range(len(ycol)): heatmap[xcol, i] = norm * (np.sum(ycol[:i + 1])) # And plot mappable = ax.imshow(heatmap, cmap=mpl.get_cmap(dath), interpolation='nearest') return mappable
def draw_boxes_on_image(image_path: str, boxes: np.ndarray, scores: np.ndarray, labels: np.ndarray, label_names: List[str], score_thresh: float = 0.5, save_path: str = 'result'): """Draw boxes on images.""" image = np.array(PIL.Image.open(image_path)) plt.figure() _, ax = plt.subplots(1) ax.imshow(image) image_name = image_path.split('/')[-1] print("Image {} detect: ".format(image_name)) colors = {} for box, score, label in zip(boxes, scores, labels): if score < score_thresh: continue if box[2] <= box[0] or box[3] <= box[1]: continue label = int(label) if label not in colors: colors[label] = plt.get_cmap('hsv')(label / len(label_names)) x1, y1, x2, y2 = box[0], box[1], box[2], box[3] rect = plt.Rectangle((x1, y1), x2 - x1, y2 - y1, fill=False, linewidth=2.0, edgecolor=colors[label]) ax.add_patch(rect) ax.text(x1, y1, '{} {:.4f}'.format(label_names[label], score), verticalalignment='bottom', horizontalalignment='left', bbox={ 'facecolor': colors[label], 'alpha': 0.5, 'pad': 0 }, fontsize=8, color='white') print("\t {:15s} at {:25} score: {:.5f}".format( label_names[int(label)], str(list(map(int, list(box)))), score)) image_name = image_name.replace('jpg', 'png') plt.axis('off') plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.savefig("{}/{}".format(save_path, image_name), bbox_inches='tight', pad_inches=0.0) plt.cla() plt.close('all')
def __plot_sample_tsne(self, axes, samples, cmap): ''' Creates a tSNE plot with points labelled according to sample. ''' idxs = [] # This will be a list of numpy arrays. for sample in samples: sample_idxs = np.where(self.sample_labels == sample) idxs.append(sample_idxs) cmap = plt.get_cmap( cmap, len(idxs) ) # Use len(idxs) instead of len(samples) to account for possible rep merging. axes.scatter(self.tsne[:, 0], self.tsne[:, 1], cmap=cmap)
def test_results(): # testing and plotting curtailment results to compare to aviva's plots (plantType, hr, fuelAndCoalType, coolType, fgdType, state, capac) = getKeyCurtailParams(gen, genFleet) coeffs = getCoeffsForGenOrTech(plantType, coolType, genparam.ptCurtailed, regCoeffs, genparam.coolDesignT) x = np.arange(start=70, stop=110.1, step=0.1) y = np.arange(start=20, stop=110.1, step=0.1) xy = np.array(np.meshgrid(x, y)).reshape(2, x.shape[0] * y.shape[0]).T metAndWaterData = pd.DataFrame({ 'airF': xy[:, 0], 'rh': xy[:, 1], 'airF:rh': xy[:, 0] * xy[:, 1] }) hourlyCurtailments = runCurtailRegression(metAndWaterData, coeffs, genparam.incCurtailments, plantType, coolType, genparam.ptCurtailed) xx, yy = np.meshgrid(x, y) zz = np.array(hourlyCurtailments).reshape(xx.shape) fig, ax = plt.subplots(1) N = 10 base = plt.cm.get_cmap(plt.get_cmap('YlGn')) color_list = base(np.linspace(0, 1, N)) cmap_name = base.name + str(N) my_map = base.from_list(cmap_name, color_list, N) bins = np.concatenate(([-np.inf], np.arange(start=0.1, stop=1, step=0.1), [np.inf])) values = np.digitize(zz, bins) im = ax.pcolormesh(xx, yy, values, cmap=my_map) plt.xlim(70, 110) plt.ylim(20, 100) cbar = fig.colorbar(im, orientation='vertical') cbar.set_ticks([]) for j in np.arange(N + 1, step=2): cbar.ax.text(1, j / N, j / N, ha='left', va='center') plt.savefig('./example.png') plt.close(fig)
def display_images_bkp(outputs, inputs=None, gt=None, is_colormap=True, is_rescale=True): import matplotlib.pyplot as plt import skimage from skimage.transform import resize plasma = plt.get_cmap('plasma') shape = (outputs[0].shape[0], outputs[0].shape[1], 3) all_images = [] for i in range(outputs.shape[0]): imgs = [] if isinstance(inputs, (list, tuple, np.ndarray)): x = to_multichannel(inputs[i]) x = resize(x, shape, preserve_range=True, mode='reflect', anti_aliasing=True) imgs.append(x) if isinstance(gt, (list, tuple, np.ndarray)): x = to_multichannel(gt[i]) x = resize(x, shape, preserve_range=True, mode='reflect', anti_aliasing=True) imgs.append(x) if is_colormap: rescaled = outputs[i][:, :, 0] if is_rescale: rescaled = rescaled - np.min(rescaled) rescaled = rescaled / np.max(rescaled) imgs.append(plasma(rescaled)[:, :, :3]) else: imgs.append(to_multichannel(outputs[i])) img_set = np.hstack(imgs) all_images.append(img_set) all_images = np.stack(all_images) return skimage.util.montage(all_images, multichannel=True, fill=(0, 0, 0))
def display_images(outputs, outputPath=None, inputs=None, gt=None, is_colormap=True, is_rescale=True, start=0, end=10, imgName=None): plt.rcParams.update({'figure.max_open_warning': 0}) gc.disable() # Create a folder if outputPath is None: #if not os.path.exists(os.path.join(os.getcwd(),'depthMapOutput')): if not os.path.exists(os.path.join(args.outputpath(), 'depthMapOutput')): os.mkdir(os.path.join(args.outputpath(), 'depthMapOutput')) print('Directory for depth mask outputs : {}'.format( os.path.join(args.outputpath(), 'depthMapOutput'))) plasma = plt.get_cmap('plasma') shape = (outputs[0].shape[0], outputs[0].shape[1], 3) #print(shape) #for i in range(end-start): for i in np.arange(end - start): if is_colormap: rescaled = outputs[i][:, :, 0] #print(rescaled.shape) if is_rescale: rescaled = rescaled - np.min(rescaled) rescaled = rescaled / np.max(rescaled) #print(rescaled.shape) # img = Image.fromarray(plasma(rescaled)[:,:,:3],mode="RGB") # img.save(f"test{str(start)}.jpg") plt.figure(figsize=(2.24, 2.24), dpi=100) plt.imshow(plasma(rescaled)[:, :, :3]) plt.axis("off") #plt.savefig(f"test{str(start)}.jpg") name = os.path.basename(imgName[i]) plt.savefig(os.path.join(os.getcwd(), 'depthMapOutput', name)) start += 1 [print(start) if start / 500 in range(1000) else None] gc.enable() #gc.get_threshold() gc.collect()
# Create a test set def splitTrain(data, ratio): ixShuffle = np.random.permutation(len(data)) testSetSize = int(len(data) * ratio) ixTest = ixShuffle[:testSetSize] ixTrain = ixShuffle[testSetSize:] return (data.iloc[ixTrain], data.iloc[ixTest]) (train,test) = splitTrain(data, .05) test # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% # Plotting data.plot(kind="scatter", x="longitude", y="latitude") data.plot(kind="scatter", x="longitude", y="latitude", alpha=.2) data.plot( kind="scatter", x="longitude", y="latitude", alpha=.15, s=data["population"]/100, figsize=(10,7), c="median_house_value", cmap=plt.get_cmap("RdPu"), colorbar=True ) plt.savefig('./images/california.png',dpi=500) # %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% # Correlation (Pearson) data.corr() attr = ["median_house_value", "median_income", "total_rooms", "housing_median_age"] scatter_matrix(data[attr], figsize=(12,8)) data.plot(kind="scatter", x="median_income", y="median_house_value", alpha=.025)
ax.xaxis.labelpad = -3 ax.yaxis.labelpad = -3 w = N.where(flx<3) smb2 = smb[w] flx2 = flx[w] name2 = N.array(d.name)[w] #ax.scatter(smb, flx,'ok',markersize=5) #ax.scatter(smb2,flx2,'o',markersize=5) colorbar = 'RdBu' jet = cm = plt.get_cmap(colorbar) cNorm = colors.Normalize(vmin=-1, vmax=1) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet) colorbarim1 = ax.imshow([d.bm_length.astype(float)],extent=[0,0.1,0,0.1],cmap=colorbar,vmin=-1,vmax=1) plt.clf() ax = fig.add_axes([0.15,0.1,0.8,0.88])############################################## wnans=N.where(N.isnan(d.bm_length.astype(float))) ax.scatter(smb,flx,c=d.bm_length.astype(float),cmap=jet,norm=cNorm,s=40) ax.scatter(smb[wnans],flx[wnans],c='0.5',s=40) z,x_new,y_new,corr = lin(smb2,flx2)
def plot_compared_intervals_ahead(original, models, colors, distributions, time_from, time_to, intervals=True, save=False, file=None, tam=[20, 5], resolution=None, cmap='Blues', linewidth=1.5): """ Plot the forecasts of several one step ahead models, by point or by interval :param original: Original time series data (list) :param models: List of models to compare :param colors: List of models colors :param distributions: True to plot a distribution :param time_from: index of data poit to start the ahead forecasting :param time_to: number of steps ahead to forecast :param interpol: Fill space between distribution plots :param save: Save the picture on file :param file: Filename to save the picture :param tam: Size of the picture :param resolution: :param cmap: Color map to be used on distribution plot :param option: Distribution type to be passed for models :return: """ fig = plt.figure(figsize=tam) ax = fig.add_subplot(111) cm = plt.get_cmap(cmap) cNorm = pltcolors.Normalize(vmin=0, vmax=1) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm) if resolution is None: resolution = (max(original) - min(original)) / 100 mi = [] ma = [] for count, fts in enumerate(models, start=0): if fts.has_probability_forecasting and distributions[count]: density = fts.forecast_ahead_distribution( original[time_from - fts.order:time_from], time_to, resolution=resolution) #plot_density_scatter(ax, cmap, density, fig, resolution, time_from, time_to) plot_density_rectange(ax, cm, density, fig, resolution, time_from, time_to) if fts.has_interval_forecasting and intervals: forecasts = fts.forecast_ahead_interval( original[time_from - fts.order:time_from], time_to) lower = [kk[0] for kk in forecasts] upper = [kk[1] for kk in forecasts] mi.append(min(lower)) ma.append(max(upper)) for k in np.arange(0, time_from - fts.order): lower.insert(0, None) upper.insert(0, None) ax.plot(lower, color=colors[count], label=fts.shortname, linewidth=linewidth) ax.plot(upper, color=colors[count], linewidth=linewidth * 1.5) ax.plot(original, color='black', label="Original", linewidth=linewidth * 1.5) handles0, labels0 = ax.get_legend_handles_labels() if True in distributions: lgd = ax.legend(handles0, labels0, loc=2) else: lgd = ax.legend(handles0, labels0, loc=2, bbox_to_anchor=(1, 1)) _mi = min(mi) if _mi < 0: _mi *= 1.1 else: _mi *= 0.9 _ma = max(ma) if _ma < 0: _ma *= 0.9 else: _ma *= 1.1 ax.set_ylim([_mi, _ma]) ax.set_ylabel('F(T)') ax.set_xlabel('T') ax.set_xlim([0, len(original)]) cUtil.show_and_save_image(fig, file, save, lgd=lgd)
lr_b = 0 lr_w = 0 # Iterations for i in range(iteration): b_grad = 0.0 w_grad = 0.0 for n in range(len(x_data)): b_grad = b_grad - 2.0 * (y_data[n] - b - w * x_data[n]) * 1.0 w_grad = w_grad - 2.0 * (y_data[n] - b - w * x_data[n]) * x_data[n] lr_b = lr_b + b_grad**2 lr_w = lr_w + w_grad**2 # Update parameters b = b - lr / np.sqrt(lr_b) * b_grad w = w - lr / np.sqrt(lr_w) * w_grad # Store parameters for plotting b_history.append(b) w_history.append(w) #plot the figure plt.contourf(x, y, z, 50, alpha=0.5, cmap=plt.get_cmap('jet')) plt.plot([-188.4], [2.67], 'x', ms=12, markeredgewidth=3, color='orange') plt.plot(b_history, w_history, 'o-', ms=3, lw=1.5, color='black') plt.xlim(-200, -100) plt.ylim(-5, 5) plt.xlabel(r'$b$', fontsize=16) plt.ylabel(r'$w$', fontsize=16) plt.show()
bm = 0.20 h = 0.77 w = 0.28 mb = 0.02 blue = N.array([45, 80, 150]) / 255. fig = plt.figure(figsize=[7, 3]) ax = fig.add_axes([lm, bm, w, h]) ax1 = fig.add_axes([lm + w + mb, bm, w, h]) ax2 = fig.add_axes([lm + w * 2 + mb * 2, bm, w, h]) #ax = fig.add_axes([lm,bm+h*2+mb*2,w,h]) #ax1 = fig.add_axes([lm,bm+h+mb,w,h]) #ax2 = fig.add_axes([lm,bm,w,h]) jet = cm = plt.get_cmap('RdBu') cNorm = colors.Normalize(vmin=-1, vmax=1) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet) colorVal = scalarMap.to_rgba(d.rlt_totalkgm2) ax.scatter(d.bm_length.astype(float) / (d.interval / 365.), d.eb_bm_flx.astype(float), s=60, c=colorVal) ax.set_xlabel("Length Change (km yr" + "$^{-1}$" + ")", fontsize=10) ax.set_ylabel("Calving Flux (Gt yr" + "$^{-1}$" + ")", fontsize=10) label_points(ax, d.bm_length.astype(float) / (d.interval / 365.), d.eb_bm_flx.astype(float), d.name, labelthese=['Hubbard', 'Columbia', 'LeConte', 'Yahtse'],