def imageOut(filename, _outputs, _targets, saveTargets=False, normalize=False, saveMontage=True): outputs = np.copy(_outputs) targets = np.copy(_targets) s = outputs.shape[1] # should be 128 if saveMontage: new_im = Image.new('RGB', ( (s+10)*3, s*2) , color=(255,255,255) ) BW_im = Image.new('RGB', ( (s+10)*3, s*3) , color=(255,255,255) ) for i in range(3): outputs[i] = np.flipud(outputs[i].transpose()) targets[i] = np.flipud(targets[i].transpose()) min_value = min(np.min(outputs[i]), np.min(targets[i])) max_value = max(np.max(outputs[i]), np.max(targets[i])) if normalize: outputs[i] -= min_value targets[i] -= min_value max_value -= min_value outputs[i] /= max_value targets[i] /= max_value else: # from -1,1 to 0,1 outputs[i] -= -1. targets[i] -= -1. outputs[i] /= 2. targets[i] /= 2. if not saveMontage: suffix = "" if i==0: suffix = "_pressure" elif i==1: suffix = "_velX" else: suffix = "_velY" im = Image.fromarray(cm.magma(outputs[i], bytes=True)) im = im.resize((512,512)) im.save(filename + suffix + "_pred.png") im = Image.fromarray(cm.magma(targets[i], bytes=True)) if saveTargets: im = im.resize((512,512)) im.save(filename + suffix + "_target.png") if saveMontage: im = Image.fromarray(cm.magma(targets[i], bytes=True)) new_im.paste(im, ( (s+10)*i, s*0)) im = Image.fromarray(cm.magma(outputs[i], bytes=True)) new_im.paste(im, ( (s+10)*i, s*1)) im = Image.fromarray(targets[i] * 256.) BW_im.paste(im, ( (s+10)*i, s*0)) im = Image.fromarray(outputs[i] * 256.) BW_im.paste(im, ( (s+10)*i, s*1)) imE = Image.fromarray( np.abs(targets[i]-outputs[i]) * 10. * 256. ) BW_im.paste(imE, ( (s+10)*i, s*2)) if saveMontage: new_im.save(filename + ".png") BW_im.save( filename + "_bw.png")
def imageOut(filename, outputs_param, targets_param, saveTargets=False): outputs = np.copy(outputs_param) targets = np.copy(targets_param) for i in range(3): min_value = min(np.min(outputs[i]), np.min(targets[i])) max_value = max(np.max(outputs[i]), np.max(targets[i])) outputs[i] -= min_value targets[i] -= min_value max_value -= min_value outputs[i] /= max_value targets[i] /= max_value suffix = "" if i == 0: suffix = "_pressure" elif i == 1: suffix = "_velX" else: suffix = "_velY" im = Image.fromarray(cm.magma(outputs[i], bytes=True)) im = im.resize((512, 512)) im.save(filename + suffix + "_pred.png") if saveTargets: im = Image.fromarray(cm.magma(targets[i], bytes=True)) im = im.resize((512, 512)) im.save(filename + suffix + "_target.png")
def plot_loop(asteroids, axes, ymaxes, strengths, lts, ds, dl, rescale=1.): colors = [cm.magma(i) for i in [0.2, 0.4, 1, 0.6, 0.8]][::-1] colors2 = [cm.magma(i) for i in [0.2, 0.4, 0.6, 0.8]][::1] for asteroid, ax, ymax in zip(asteroids, axes, ymaxes): for Y, c, lw in zip(strengths, colors, (1.5, 1.5, 2.5, 1.5, 1.5)): ds, dl = process_model(asteroid, Y, c, lw, 100, '-', 'o', ax, ds, dl, rescale=rescale) for lt, c, lw in zip(lts, colors2, (1.5, 1.5, 1.5, 1.5)): ds, dl = process_model(asteroid, '1e3', c, lw, lt, '--', 's', ax, ds, dl, rescale=rescale) ax.axhline(1, color='#AAAAAA', zorder=-20, ls='--', lw=1) ax.set_ylabel( 'Cumulative nunmber of\ncraters $\geq D$ on {}'.format(asteroid)) ax.set_ylim(3e-1, ymax) ax.set_xlim(2e1, 1e3) ax.loglog(10, 1e-1, 'w-', label=' ') ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:4g}')) if len(axes) > 1: axes[0].set_xticklabels([]) axes[-1].set_xlabel('Rim-to-rim crater diameter, $D$ [m]') return ds, dl
def plot_h85_trends(H85): x = cmip5.get_plottable_time(H85) nmod = H85.shape[0] cmap = cm.magma for i in range(nmod): if not H85[i].mask[0]: time_plot(H85[i], lw=.5, alpha=.5, color=cm.magma(float(i) / float(nmod))) p = np.ma.polyfit(x, H85[i], 1) plt.plot(x, np.polyval(p, x), color=cm.magma(float(i) / float(nmod)))
def plot_separability_curves(prefix, q1, list_euclidean_dst, listQ1_listSamples_listDst_listTrials, listQ1_listSamples_MC): max_mc = np.max(listQ1_listSamples_MC) for i, q in enumerate(q1): arDst_arTrials = listQ1_listSamples_listDst_listTrials[i].reshape( listQ1_listSamples_listDst_listTrials.shape[2], -1) norm_mc = np.mean(listQ1_listSamples_MC[i]) / max_mc # high = np.percentile(arDst_arTrials, 95, axis=1) - np.mean(arDst_arTrials, axis=1) # low = np.mean(arDst_arTrials, axis=1) - np.percentile(arDst_arTrials, 5, axis=1) mean_err = sem(arDst_arTrials, axis=1) mean = np.mean(arDst_arTrials, axis=1) high = mean_err low = mean_err plt.errorbar(list_euclidean_dst, np.mean(arDst_arTrials, axis=1), yerr=[low, high], label="$\mu=$" + str(q), c=cm.magma(norm_mc)) plt.legend(loc="upper left") plt.xlabel("input distance") plt.ylabel("state distance") plt.tight_layout() plt.savefig(prefix + "_separability_curve.png", dpi=300) plt.clf() plt.close()
def plot_separability_performance_curves_by_reservoir( prefix, q1, list_euclidean_dst, listQ1_listSamples_listDst_listTrials, listQ1_listSamples_MC): max_mc = np.max(listQ1_listSamples_MC) for i, q in enumerate(q1): for j in range(listQ1_listSamples_listDst_listTrials.shape[1]): arDst_arTrials = listQ1_listSamples_listDst_listTrials[i, j, :, :] norm_mc = listQ1_listSamples_MC[i, j] / max_mc # high = np.percentile(arDst_arTrials, 95, axis=1) - np.mean(arDst_arTrials, axis=1) # low = np.mean(arDst_arTrials, axis=1) - np.percentile(arDst_arTrials, 5, axis=1) mean_err = sem(arDst_arTrials, axis=1) mean = np.mean(arDst_arTrials, axis=1) high = mean_err low = mean_err plt.errorbar(list_euclidean_dst, np.mean(arDst_arTrials, axis=1), yerr=[low, high], c=cm.magma(norm_mc)) plt.legend(loc="upper left") plt.xlabel("input distance") plt.ylabel("state distance") plt.xlim(0, list_euclidean_dst[-1]) plt.ylim(0, 5) plt.tight_layout() plt.savefig(prefix + "_separability_performance_mu" + str(q) + ".png", dpi=300) plt.clf() plt.close()
def imageOut(filename, _outputs, saveTargets=True, normalize=False): outputs = np.copy(_outputs) for i in range(3): outputs[i] = np.flipud(outputs[i].transpose()) min_value = np.min(outputs[i]) max_value = np.max(outputs[i]) if normalize: outputs[i] -= min_value max_value -= min_value outputs[i] /= max_value else: # from -1,1 to 0,1 outputs[i] -= -1. outputs[i] /= 2. suffix = "" if i == 0: suffix = "_pressure" elif i == 1: suffix = "_velX" else: suffix = "_velY" im = Image.fromarray(cm.magma(outputs[i], bytes=True)) im = im.resize((128, 128)) im.save(filename + suffix + "_pred.png")
def plot_seasonal_trends(self, ax=None, scenario="ssp585"): if ax is None: fig = plt.figure() ax = plt.subplot(111) if scenario.find("+") > 0: ssp = scenario.split("+")[-1] test = self.splice_historical(ssp) else: test = self.ensemble_average(scenario) #test=self.ensemble_average("ssp585") mma = MV.average(test, axis=0) nyears = int(len(mma) / 12) tst = mma.reshape((nyears, 12)) tmp = [ ax.plot(tst.asma()[i], c=cm.magma(i / float(nyears))) for i in range(nyears) ] months = [ "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" ] ax.set_xticks(np.arange(12)) ax.set_xticklabels(months) ax.set_ylabel(self.variable) ax.set_title(self.region)
def color_scale(img, norm,text=None): if len(img.shape) == 2: img = cm.magma(norm(img),bytes=True) img = imutils.resize(img, height=300) if img.shape[2] == 4: img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) if text is not None: img = cv2.putText(img, text, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA) return img
def saveAsImage(filename, field_param): field = np.copy(field_param) field = np.flipud(field.transpose()) min_value = np.min(field) max_value = np.max(field) field -= min_value max_value -= min_value field /= max_value im = Image.fromarray(cm.magma(field, bytes=True)) im = im.resize((512, 512)) im.save(filename)
def _base_3d_plotter(x_ranges, y_ranges, fun, step=.1, color_fun=None, angles=None): xx = np.arange(*x_ranges, step) yy = np.arange(*y_ranges, step) xx, yy = np.meshgrid(xx, yy) zz = fun(xx, yy) if color_fun is not None: colors = cm.magma(color_fun(xx, yy)) cmap = 'magma' else: colors = None cmap = 'viridis' fig, ax = _get_3dwindow(angles=angles) return fig, ax, xx, yy, zz, cmap, colors
def plot_circ_dist(self, flag='good', num_bins=12*4, figsize=(8,8)): title = 'Calendar distribution of ' if flag == 'good': slct = self.daily_flags.no_errors title += 'good days' elif flag == 'bad': slct = ~self.daily_flags.no_errors title += 'bad days' elif flag in ['clear', 'sunny']: slct = self.daily_flags.clear title += 'clear days' elif flag == 'cloudy': slct = self.daily_flags.cloudy title += 'cloudy days' circ_data = (self.start_doy + np.arange(self.num_days)[slct]) % 365 \ * 2 * np.pi / 365 circ_hist = np.histogram(circ_data, bins=num_bins) fig = plt.figure(figsize=figsize) ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True) start = (circ_hist[1][0] + circ_hist[1][1]) / 2 end = (circ_hist[1][-1] + circ_hist[1][-2]) / 2 theta = np.linspace(start, end, num_bins) radii = circ_hist[0] width = 2 * np.pi / num_bins bars = ax.bar(theta, radii, width=width, bottom=0.0, edgecolor='none') for r, bar in zip(radii, bars): bar.set_facecolor(cm.magma(r / np.max(circ_hist[0]))) bar.set_alpha(0.75) ax.set_theta_zero_location('N') ax.set_theta_direction(-1) ax.set_rorigin(-2.) ax.set_rlabel_position(0) ax.set_xticks(np.linspace(0, 2 * np.pi, 12, endpoint=False)) ax.set_xticklabels( ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] ) ax.set_title(title) # print(np.sum(circ_hist[0] <= 1)) return fig
def extract(self, inputFilename, outputFilename=None, color=True, fps = 25.0 ): adjWavFilename = inputFilename[:-4] + ".16k1c.wav" os.system("sox %s -c %d -r %dk -t wav %s" % ( inputFilename, 1, 16, adjWavFilename ) ) y, sr = librosa.load(adjWavFilename) os.system("rm %s"%adjWavFilename) hopLen = int(sr / fps) mels = librosa.feature.melspectrogram( y=y, sr=sr, n_mels=self.imageHeight, fmax = 8000, # for voice, let's trim exceeding 8000Hz n_fft=hopLen*2, hop_length=hopLen ) # some of the approaches used in this class referenced following link # https://stackoverflow.com/questions/56719138/how-can-i-save-a-librosa-spectrogram-plot-as-a-specific-sized-image/57204349#57204349 mels = self._normalize(np.log(mels + 1e-9)) mels = np.flip(mels, axis=0) if color: # render grayscale to colormap and librosa default cmap # https://stackoverflow.com/questions/10965417/how-to-convert-a-numpy-array-to-pil-image-applying-matplotlib-colormap # https://librosa.github.io/librosa/generated/librosa.display.cmap.html im = Image.fromarray(np.uint8(cm.magma(mels)*255)) else: im = Image.fromarray(np.uint8(mels*255)) if outputFilename != None: im.convert("RGB").save(outputFilename) else: return im
def prepare_spec_image(spectrogram): """ Prepare an image from spectrogram to be written to tensorboardX summary writer. Args: spectrogram (numpy.ndarray): Shape(T, C), spectrogram to be visualized, where T means the time steps of the spectrogram, and C means the channels of the spectrogram. Return: np.ndarray: Shape(C, T, 4), the generated image of the spectrogram, where T means the time steps of the spectrogram. It is treated as the width of the image. And C means the channels of the spectrogram, which is treated as the height of the image. And 4 means it is a 'ARGB' format. """ # [0, 1] spectrogram = (spectrogram - np.min(spectrogram)) / (np.max(spectrogram) - np.min(spectrogram)) spectrogram = np.flip(spectrogram, axis=1) # flip against freq axis return np.uint8(cm.magma(spectrogram.T) * 255)
def batch(self, iternum, itemnum, irgbrec, ialpharec=None, image=None, irgbsqerr=None, **kwargs): irgbrec = irgbrec.data.to("cpu").numpy().transpose((0, 2, 3, 1)) if ialpharec is not None: ialpharec = ialpharec.data.to("cpu").numpy()[:, 0, :, :, None] else: ialpharec = 1.0 # color correction imgout = irgbrec * self.colcorrect[None, None, None, :] # composite background color imgout = imgout + (1. - ialpharec) * self.bgcolor[None, None, None, :] # concatenate ground truth image if self.showtarget and image is not None: image = image.data.to("cpu").numpy().transpose((0, 2, 3, 1)) image = image * self.colcorrect[None, None, None, :] imgout = np.concatenate((imgout, image), axis=2) # concatenate difference image if self.showdiff and imagediff is not None: irgbsqerr = np.mean(irgbsqerr.data.to("cpu").numpy(), axis=1) irgbsqerr = (cm.magma(4. * irgbsqerr / 255.)[:, :, :, :3] * 255.) imgout = np.concatenate((imgout, irgbsqerr), axis=2) self.writepool.map( writeimage, zip([self.randid for i in range(itemnum.size(0))], itemnum.data.to("cpu").numpy(), imgout)) self.nitems += itemnum.size(0)
def spectwrite(filename, spectrogram, color="viridis", db=100): """ Save a spectrogram as an image. The data is normalized to dynamic range of 100 dB, and a logarithmic Viridis color scale is used. Parameters ---------- filename : string Name of the image file spectrogram : array_like Spectrogram color : string or NoneType Whether to make a color plot """ spectrogram = np.asarray(spectrogram) print("spect energy: {}".format(np.sum(np.square(spectrogram)))) print("spect max: {}".format(np.amax(spectrogram))) spectrogram = spectrogram / np.amax(spectrogram) floor = 10**(-db / 20) logspect = np.log10((floor + spectrogram) / (floor + 1)) + db / 20 maxspect = np.amax(logspect) scalespect = logspect / maxspect scalespect = scalespect[::-1, :] scalespect = np.maximum(scalespect, 0) scalespect = np.minimum(scalespect, 1) if not color: imageio.imwrite(filename, 1 - scalespect) elif color == "magma": plotspect = cm.magma(1 - scalespect) imageio.imwrite(filename, plotspect) else: plotspect = cm.viridis(scalespect) imageio.imwrite(filename, plotspect)
def prepare_spec_image(spectrogram): spectrogram = (spectrogram - np.min(spectrogram)) / ((np.max(spectrogram)) - np.min(spectrogram)) spectrogram = np.flip(spectrogram, axis=0) return np.uint8(cm.magma(spectrogram) * 255)
def prepare_spec_image(spectrogram): # [0, 1] spectrogram = (spectrogram - np.min(spectrogram)) / (np.max(spectrogram) - np.min(spectrogram)) spectrogram = np.flip(spectrogram, axis=1) # flip against freq axis return np.uint8(cm.magma(spectrogram.T) * 255)
import matplotlib.pyplot as plt import matplotlib.ticker as ticker import matplotlib.cm as cm import matplotlib.gridspec as gs # Set the fonts plt.rcParams['text.usetex'] = True plt.rcParams[ 'text.latex.preamble'] = r'\usepackage{sfmath} \usepackage{amsmath}' plt.rcParams['font.size'] = 8 # Parameters of interest strengths = ['1e1', '1e2', '1e3', '1e4', '1e5'] lts = [10, 30, 300, 1000] colors = [cm.magma(i) for i in [0.8, 0.6, 1, 0.4, 0.2]] colors2 = [cm.magma(i) for i in [0.8, 0.6, 0.4, 0.2]][::-1] rbennu = 262.5 sarea = 4. * np.pi * rbennu**2 gridspec = gs.GridSpec(ncols=1, nrows=26) datasets = [] dlabels = [] def process_model(asteroid, Y, c, lw, lt, ls, medmark): # list of surviving (non-disrupted) parent bodies and their largest craters slist0, lc0 = np.genfromtxt(
def plot_res_curves_2d(data, m_name, m_date, m_type, show=True, save=False): fig1 = plt.figure() # plotting the data proper_date = m_date[:2]+'.'+m_date[2:4]+'.'+m_date[4:] title_name = m_type + '\n' + 'along Py stripe' + ' at ' + m_name[-4:] + ' DC' fig1.suptitle(title_name, fontweight='bold', fontsize=12) ax1 = fig1.add_subplot(111) i = 0 colors = cm.magma(np.linspace(0, 1, len(data.T)/2)) real_distance = np.linspace(0, 22.3, 101) print(real_distance[30]) for i, color in zip(range(0, len(data.T), 2), colors): x = data.T[i] y = data.T[i+1] print(x) print(len(x)) # plotting the data ax1.scatter(real_distance, y, c=color, label='data') # plotting the DC step position y_line = np.arange(0, max(y) + 15000, 5) x_line = np.array([real_distance[30] for i in range(len(y_line))]) print(real_distance) ax1.plot(x_line, y_line, linestyle='--', color='red', label='DC-line\nstep pos (6.7' + r'$\mu m$' + ')') print(real_distance[30]) # fitting part # for the measurement without DC if '00' in m_name: guess_amplitude = np.mean(y) # params[0] guess_decay = 0.2 # params[1] guess_x0 = 1.69 # params[2] guess_wave_number = 0.85 # params[3] guess_phase = 4.88 # params[4] guess_y0 = 10000 # params[5] guess_params = np.array([guess_amplitude, guess_decay, guess_x0, guess_wave_number, guess_phase, guess_y0]) decaying_func = lambda params, coord: params[0] * np.exp(-params[1] * coord + params[2]) * \ np.power(np.cos(params[3] * coord + params[4]), 2) + params[5] errfunc = lambda params, coord, y: decaying_func(params, coord) - y est_amplitude, est_decay, est_x0, est_wave_number, est_phase, est_y0 = \ leastsq(errfunc, guess_params, args=(real_distance[3:95], y[3:95]))[0] fit_params = np.array([est_amplitude, est_decay, est_x0, est_wave_number, est_phase, est_y0]) print(fit_params) wave_number = fit_params[3] wave_length = np.pi / wave_number x_coord = np.linspace(real_distance[3], real_distance[91], 1000) data_fit = decaying_func(fit_params, x_coord) ax1.plot(x_coord, data_fit, label='fit ' + r'$\lambda$' + '=' + "{0:.2f}".format(wave_length) + r'$\mu m$') # for the measurement with DC else: # fitting before step guess_amplitude = np.mean(y) # params[0] guess_decay = -0.03 # params[1] guess_x0 = -1.69 # params[2] guess_wave_number = 0.7 # params[3] # guess_wave_number = 0.1 # params[3] guess_phase = 2.88 # params[4] guess_y0 = 10000 # params[5] guess_params = np.array([guess_amplitude, guess_decay, guess_x0, guess_wave_number, guess_phase, guess_y0]) decaying_func = lambda params, coord: params[0] * np.exp(-params[1] * coord + params[2]) * \ np.power(np.cos(params[3] * coord + params[4]), 2) + params[5] errfunc = lambda params, coord, y: decaying_func(params, coord) - y est_amplitude, est_decay, est_x0, est_wave_number, est_phase, est_y0 = \ leastsq(errfunc, guess_params, args=(real_distance[3:31], y[3:31]))[0] fit_params = np.array([est_amplitude, est_decay, est_x0, est_wave_number, est_phase, est_y0]) print(fit_params) wave_number_before = fit_params[3] wave_length_before = np.pi/wave_number_before x_coord = np.linspace(real_distance[3], real_distance[29], 500) data_fit = decaying_func(fit_params, x_coord) ax1.plot(x_coord, data_fit, color='blue', label='fit '+r'$\lambda$'+'='+"{0:.2f}".format(wave_length_before) + r'$\mu m$') # fitting after the step guess_amplitude = np.mean(y) # params[0] guess_decay = -0.1 # params[1] guess_x0 = -0.27 # params[2] guess_wave_number = 1.1 # params[3] guess_phase = 5.13 # params[4] guess_y0 = 10000 # params[5] guess_params = np.array([guess_amplitude, guess_decay, guess_x0, guess_wave_number, guess_phase, guess_y0]) decaying_func = lambda params, coord: params[0] * np.exp(-params[1] * coord + params[2]) * \ np.power(np.cos(params[3] * coord + params[4]), 2) + params[5] errfunc = lambda params, coord, y: decaying_func(params, coord) - y fit_params = leastsq(errfunc, guess_params, args=(real_distance[31:91], y[31:91]))[0] # fit_params = np.array([est_amplitude, est_decay, est_x0, est_wave_number, est_phase, est_y0]) print(fit_params) wave_number_after = fit_params[3] wave_length_before = np.pi / wave_number_after x_coord = np.linspace(real_distance[30], real_distance[91], 1000) data_fit2 = decaying_func(fit_params, x_coord) ax1.plot(x_coord, data_fit2, color='green', label='fit '+r'$\lambda$'+'='+"{0:.2f}".format(wave_length_before) + r'$\mu m$') # some things to make plot beautiful ax1.grid() new_xticks = np.linspace(0, 22.3, 12) new_xticks = [int(xtick) for xtick in new_xticks] ax1.set_xticks(new_xticks) ax1.set_xlim([0, 22]) ax1.set_ylim([0, max(y)+2000]) ax1.set_xlabel('Position along Py stripe'+r' ($\mu m$)') ax1.set_ylabel('Intensity (a.u.)') ax1.ticklabel_format(axis='y', style='sci', scilimits=(-3, 3), useOffset=False) ax1.yaxis.major.formatter._useMathText = True ax1.legend() fig1.tight_layout() fig1.subplots_adjust(top=0.89) if save: # m_name_new = m_name[:1]+'p'+m_name[2:] save_name = m_date + '_' + m_name + '_' + m_type + '.png' plt.savefig('./output_pics/'+save_name, format='png', dpi=100) if show: plt.show()
def plot_learned_models_dynamics( qmd, model_ids=None, include_expec_vals=True, include_bayes_factors=True, include_times_learned=True, include_param_estimates=False, save_to_file=None, ): model_ids = list(sorted(set(model_ids))) # only uniques values true_expec_vals = pickle.load( open(qmd.qmla_controls.system_measurements_file, "rb") ) times_to_plot = list(sorted(true_expec_vals.keys())) # TODO this is overwritten within for loop below so that large # Hamiltonians don't have to work out each time step true_exp = [true_expec_vals[t] for t in times_to_plot] qmd.log_print( [ "[Dynamics plot] plot probe file:", qmd.qmla_controls.probes_plot_file, "\n true expectation value path:", qmd.qmla_controls.system_measurements_file, ] ) plot_probes = pickle.load(open(qmd.qmla_controls.probes_plot_file, "rb")) num_models_to_plot = len(model_ids) all_bayes_factors = qmd.all_bayes_factors max_time = max(times_to_plot) individual_terms_already_in_legend = [] # ncols = int(np.ceil(np.sqrt(num_models_to_plot))) # nrows = 3*int(np.ceil(num_models_to_plot/ncols)) + 1 # 1 extra row for # "master" ncols = ( include_expec_vals + include_bayes_factors + include_times_learned + include_param_estimates ) # ncols = 4 nrows = num_models_to_plot fig = plt.figure( figsize=(18, 10), # constrained_layout=True, tight_layout=True, ) gs = GridSpec( nrows, ncols, # figure=fig # not available on matplotlib 2.1.1 (on BC) ) row = 0 col = 0 for mod_id in model_ids: qmd.log_print(["Plotting dynamics for model {}".format(mod_id)]) reduced = qmd.get_model_storage_instance_by_id(mod_id) reduced.compute_expectation_values(times=qmd.times_to_plot) # exploration_rule = reduced.exploration_strategy_of_true_model desc = str("ID:{}\n".format(mod_id) + reduced.model_name_latex) times_to_plot = list(sorted(true_expec_vals.keys())) plot_colour = "blue" name_colour = "black" dynamics_label = str(mod_id) try: true_model_id = qmd.true_model_id except BaseException: true_model_id = -1 if mod_id == qmd.champion_model_id and mod_id == true_model_id: plot_colour = "green" name_colour = "green" dynamics_label += " [true + champ]" desc += str("\n[True + Champ]") elif mod_id == qmd.champion_model_id: plot_colour = "orange" name_colour = "orange" dynamics_label += " [champ]" desc += str("\n[Champ]") elif mod_id == true_model_id: plot_colour = "green" name_colour = "green" dynamics_label += " [true]" desc += str("\n[True]") ############ --------------- ############ ############ Plot dynamics in left most column ############ ############ --------------- ############ if include_expec_vals is True: ham = reduced.learned_hamiltonian dim = np.log2(np.shape(ham)[0]) probe = plot_probes[reduced.probe_num_qubits] qmd.log_print( [ "[plot_learned_models_dynamics]", "\n\tModel ", reduced.model_name_latex, "\n\tnum qubits:", dim, "\n\tprobe:", probe, ] ) # expec_vals = {} if dim > 4: times_to_plot = times_to_plot[0::5] times_to_plot = sorted(list(true_expec_vals.keys())) true_exp = [true_expec_vals[t] for t in times_to_plot] # choose an axis to plot on ax = fig.add_subplot(gs[row, col]) # first plot true dynamics ax.plot(times_to_plot, true_exp, c="r") # now plot learned dynamics expec_vals = reduced.expectation_values sim_times = sorted(list(expec_vals.keys())) sim_exp = [expec_vals[t] for t in sim_times] # print( # "[plotDynamics]", # "\nsim exp:", sim_exp, # "\nsim_times:", sim_times # ) ax.plot( sim_times, sim_exp, marker="o", markevery=10, c=plot_colour, # label = dynamics_label label=desc, ) # qmd.log_print([ # "[Dynamics plot]", # "sim_exp:", sim_exp[0:20], # "true exp:", true_exp[0:20] # ]) # ax.legend() ax.set_ylim(-0.05, 1.05) if row == 0: ax.set_title("Expectation Values") if ncols == 1: ax.legend() col += 1 if col == ncols: col = 0 row += 1 ############ --------------- ############ ############ Plot Bayes factors ############ ############ --------------- ############ if include_bayes_factors == True: bayes_factors_this_mod = [] bf_opponents = [] for b in model_ids: if b != mod_id: if b in list(all_bayes_factors[mod_id].keys()): # bf_opponents.append( # qmd.get_model_storage_instance_by_id(b).model_name_latex # ) bayes_factors_this_mod.append( np.log10(all_bayes_factors[mod_id][b][-1]) ) bf_opponents.append(str(b)) ax = fig.add_subplot(gs[row, col]) ax.bar(bf_opponents, bayes_factors_this_mod, color=plot_colour) ax.axhline(0, color="black") if row == 0: ax.set_title("Bayes Factors [$log_{10}$]") col += 1 if col == ncols: col = 0 row += 1 ############ --------------- ############ ############ Plot times learned over ############ ############ --------------- ############ if include_times_learned == True: ax = fig.add_subplot(gs[row, col]) if row == 0: ax.set_title("Times learned") ax.yaxis.set_label_position("right") times_learned_over = sorted( qmla.utilities.flatten(reduced.times_learned_over) ) qmd.log_print(["[single instance plot] Times for bin:", times_learned_over]) n, bins, patches = ax.hist( times_learned_over, # histtype='step', color=plot_colour, # fill=False, label=desc, ) ax.legend() # ax.semilogy() for bin_value in bins: ax.axvline(bin_value, linestyle="--", alpha=0.3) plot_time_max = max(times_to_plot) max_time = max(times_learned_over) if max_time > plot_time_max: ax.axvline( plot_time_max, color="red", linestyle="--", label="Dynamics plot cutoff", ) ax.legend() ax.set_xlim(0, max_time) col += 1 if col == ncols: col = 0 row += 1 ############ --------------- ############ ############ Plot parameters estimates ############ ############ --------------- ############ if include_param_estimates == True: ax = fig.add_subplot(gs[row, col]) name = reduced.model_name terms = model_building_utilities.get_constituent_names_from_name(name) num_terms = len(terms) term_positions = {} param_estimate_by_term = {} std_devs = {} for t in range(num_terms): term_positions[terms[t]] = t term = terms[t] param_position = term_positions[term] param_estimates = reduced.track_param_means[:, param_position] # std_dev = mod.cov_matrix[param_position,param_position] # std_dev = reduced.track_covariance_matrices[ # :,param_position,param_position # ] std_dev = reduced.track_param_uncertainties[:, param_position] param_estimate_by_term[term] = param_estimates std_devs[term] = std_dev cm_subsection = np.linspace(0, 0.8, num_terms) colours = [cm.magma(x) for x in cm_subsection] # TODO use color map as list num_epochs = reduced.num_experiments i = 0 # for term in list(param_estimate_by_term.keys()): for term in terms: colour = colours[i % len(colours)] i += 1 try: y_true = qmd.true_param_dict[term] true_term_latex = qmd.exploration_class.latex_name(name=term) ax.axhline( y_true, ls="--", label=str(true_term_latex + " True"), color=colour, ) except BaseException: pass y = np.array(param_estimate_by_term[term]) s = np.array(std_devs[term]) x = range(1, 1 + len(param_estimate_by_term[term])) latex_term = qmd.exploration_class.latex_name(name=term) if latex_term not in individual_terms_already_in_legend: individual_terms_already_in_legend.append(latex_term) plot_label = str(latex_term) else: plot_label = "" # print("[pQMD] latex_term:", latex_term) ax.plot( x, y, # s=max(1,50/num_epochs), label=plot_label, color=colour, ) # ax.set_yscale('symlog') # print("[pQMD] scatter done" ) # ax.fill_between( # x, # y+s, # y-s, # alpha=0.2, # facecolor=colour # ) ax.legend() if row == 0: ax.set_title("Parameter Estimates") col += 1 if col == ncols: col = 0 row += 1 if save_to_file is not None: plt.savefig(save_to_file, bbox_inches="tight")
def plot_velocity_map(self): for it in range(len(self.ss)): vel_arr = [np.sqrt(arr[2]**2 + arr[3]**2) for arr in self.ss[it]]/self.max_vel colors = cm.magma(vel_arr) for j in range(len(vel_arr)): plt.plot(self.ss[it][j][0], self.ss[it][j][1], marker='s', color=colors[j])
def convert(im, b): imarray = np.array(im) imarray = np.uint8(cm.magma(np.uint16(imarray * b)) * 255) return Image.fromarray(imarray)
def plot_parameter_estimates(qmd, model_id, use_experimental_data=False, save_to_file=None): from matplotlib import cm mod = qmd.get_model_storage_instance_by_id(model_id) name = mod.model_name if name not in list(qmd.model_name_id_map.values()): print("True model ", name, "not in studied models", list(qmd.model_name_id_map.values())) return False terms = construct_models.get_constituent_names_from_name(name) num_terms = len(terms) term_positions = {} param_estimate_by_term = {} std_devs = {} for t in range(num_terms): term_positions[terms[t]] = t term = terms[t] param_position = term_positions[term] param_estimates = mod.track_param_means[:, param_position] #std_dev = mod.cov_matrix[param_position,param_position] std_dev = mod.track_covariance_matrices[:, param_position, param_position] param_estimate_by_term[term] = param_estimates std_devs[term] = std_dev cm_subsection = np.linspace(0, 0.8, num_terms) colours = [cm.magma(x) for x in cm_subsection] # colours = [ cm.Set1(x) for x in cm_subsection ] # colours = ['b','r','g','orange', 'pink', 'grey'] # TODO use color map as list # num_epochs = qmd.num_experiments num_epochs = mod.num_experiments # fig = plt.figure() # ax = plt.subplot(111) # ncols=3 # nrows=3 # TODO -- make safe ncols = int(np.ceil(np.sqrt(num_terms))) nrows = int(np.ceil(num_terms / ncols)) # nrows=int(np.ceil( num_terms/ncols )) fig, axes = plt.subplots(figsize=(10, 7), nrows=nrows, ncols=ncols, squeeze=False) row = 0 col = 0 axes_so_far = 0 i = 0 # for term in list(param_estimate_by_term.keys()): for term in terms: ax = axes[row, col] colour = colours[i % len(colours)] i += 1 try: if use_experimental_data == False: y_true = qmd.true_param_dict[term] true_term_latex = qmd.exploration_class.latex_name(name=term) true_term_latex = true_term_latex[:-1] + '_{0}' + '$' ax.axhline(y_true, label=str(true_term_latex), color='red', linestyle='--') except BaseException: pass y = np.array(param_estimate_by_term[term]) s = np.array(std_devs[term]) x = range(1, 1 + len(param_estimate_by_term[term])) latex_term = mod.exploration_class.latex_name(term) latex_term = latex_term[:-1] + r'^{\prime}' + '$' # print("[pQMD] latex_term:", latex_term) ax.scatter(x, y, s=max(1, 50 / num_epochs), label=str(latex_term), color=colour) # ax.set_yscale('symlog') # print("[pQMD] scatter done" ) ax.fill_between( x, y + s, y - s, alpha=0.2, facecolor='green', # label='$\sigma$' ) # print("[pQMD] fill between done") ax.legend(loc=1, fontsize=20) axes_so_far += 1 col += 1 if col == ncols: col = 0 row += 1 # ax.set_title(str(latex_term)) # print("[pQMD] title set") # ax = plt.subplot(111) plt.xlabel('Epoch', fontsize=20) plt.ylabel('Parameter Estimate', fontsize=15) # plt.legend(bbox_to_anchor=(1.1, 1.05)) # # TODO put title at top; Epoch centred bottom; Estimate centre y-axis if save_to_file is not None: print("[plot_parameter_estimates] saving to file", save_to_file, "type:", type(save_to_file)) plt.savefig(save_to_file, bbox_inches='tight')
fig = plt.figure(figsize=(number_of_files, 1)) from matplotlib import gridspec #from PIL import Image import cv2 gs = gridspec.GridSpec(1, number_of_files) gs.update(wspace=0.0, hspace=0.0, left=0.1, right=0.9, bottom=0.1, top=0.9) i = 0 for im in X: heatmap = visualize_cam(model, layer_idx=-1, filter_indices=0, seed_input=im, backprop_modifier=None) magma_heatmap = np.uint8(cm.magma(heatmap)[..., :3] * 255) plt.subplot(gs[i]) i = i + 1 plt.xticks([]) plt.yticks([]) # Overlay is used to alpha blend heatmap onto img. #newImage = cv2.addWeighted(0.5 ,magma_heatmap ,0.5 ,im) imagex = (im * 255).round().astype(np.uint8) plt.imshow(overlay(magma_heatmap, imagex, alpha=.5)) plt.axis('off') plt.show()
handles = [] labels = [] def hg(theta, g): theta = np.pi * theta / 180.0 return (1 - g**2) / (1 + g**2 - 2 * g * np.cos(theta))**1.5 theta = np.linspace(0, 180, 101) g = np.array([-0.8, -0.4, 0.0, 0.4, 0.8]) ps = hg(theta.reshape(1, -1), g.reshape(-1, 1)) ps /= np.trapz(ps, theta, axis=-1).reshape(-1, 1) * 4 * np.pi from matplotlib.cm import magma cs = [magma(i / 5) for i in range(ps.shape[0])] ax = plt.subplot(gs[0, 0]) for i in range(ps.shape[0]): handles += ax.plot(theta, ps[i, :], c=cs[i]) labels += ["g = {}".format(g[i])] ax.set_xlabel(r"$\theta$ [$^\circ$]") ax.set_ylabel(r"$p(\theta)$") ax.set_yscale("log") ax.set_xlim(0, 180) ax = plt.subplot(gs[0, 1]) ax.set_axis_off() ax.legend(handles=handles, labels=labels, loc="center")
import scipy.interpolate as interp # Set the fonts plt.rcParams['text.usetex'] = True plt.rcParams['font.size'] = 8 plt.rcParams[ 'text.latex.preamble'] = r'\usepackage{sfmath} \usepackage{amsmath}' magma = cm.magma # Models of interest strengths = ['4e0', '1e1', '1e2', '1e3', '1e4', '1e5'] ages = [0.3, 0.5, 1, 2, 3, 5, 10, 20, 30, 50, 100, 200, 300, 500, 1000] markers = ['*', 'o', 's', 'D', '^', 'v'] colours = [cm.magma(i) for i in [0.9, 0.75, 0.6, 0.45, 0.3, 0.15]] # Set up the figure fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(3.74, 4.6)) fig.subplots_adjust(left=0.17, top=0.92, hspace=0.1, bottom=0.11, right=0.97) ax1.set_xscale('log') b_crater = 160. r_crater = 300. with open('largest_crater.txt', 'w') as f: for asteroid, ax, crater, in zip(['Bennu', 'Ryugu'], (ax1, ax2), (b_crater, r_crater)):
values='frac_active') fig, ax = plt.subplots(figsize=(5, 8)) sns.heatmap(freq_thresh_hmap, ax=ax, cmap='viridis', robust=True, cbar_kws={'label': 'Fraction active cells'}) plt.tight_layout() plt.savefig(plot_dir + 'hmap_rep_activecellfrac.pdf', bbox_inches='tight') ############################################################################### # plot replicates in stripplot instead of heatmap import matplotlib.cm as cm fig, axes = plt.subplots(2, 8, sharex=False, sharey=False, figsize=(22, 6)) time_pal = cm.magma(np.linspace(0, 0.8, len(parts.time_postinduction.unique()))) for (thresh, _freq_df), ax in zip(freq_df.groupby('thresh'), axes.ravel()): ax.tick_params(axis='x', labelsize=10, rotation=60) ax.tick_params(axis='y', labelsize=10, rotation=60) sns.stripplot(x='strain', y='frac_active', data=_freq_df, ax=ax, alpha=0.5, hue=_freq_df.time_postinduction, palette=time_pal) #sns.pointplot(x='strain', y='frac_active', data=_freq_df, ax=ax, alpha=0.5, size=5, join=False) ax.legend('', frameon=False) ax.annotate(' thresh={}'.format(thresh), (ax.get_xlim()[0], ax.get_ylim()[1]), fontsize=12)
def colorcode(speed): speed1 = int((speed - 50) * 4.3) co = np.array(cm.magma(speed1)) * 255 return "rgb(" + str(int(co[0])) + "," + str(int(co[1])) + "," + str( int(co[2])) + ")"
# This program uses matplotlib colour maps # and the Image class of PIL # to convert a grayscale DTM image to a # pseudocolour version import numpy as np import matplotlib.cm as cm import glob from PIL import Image # pattern for input files is curently hard-coded DTMs = glob.glob("*DTM*2.png") for d in DTMs: img = Image.open(d) #img.show() im_arr = np.asarray(img) im_arr = im_arr.astype(np.float32) # convert to int im_arr -= im_arr.min() # ensure the minimal value is 0.0 im_arr /= im_arr.max() # maximum value in image is now 1.0 # the 'magma' colourmap is hard-coded at the moment im = Image.fromarray(np.uint8(cm.magma(im_arr) * 255)) im.save(d)