def plotf(self, data=None): ''' Plot in frequency domain (DFT on samples and then plot) ''' if data is None: if (self.raw_dft is None): self.fft() mpl.grid(True) mpl.plot(range(0, len(self.real_dft)), self.real_dft, c='gray', ls='-') mpl.xticks(range( 0, len(self.real_dft), int(self.sampl / 32) if int(self.sampl / 32) > 0 else 1), rotation=0) mpl.margins(0.05) mpl.xlabel('Frequency [Hz]') mpl.ylabel('Amplitude') else: mpl.grid(True) mpl.plot(range(0, len(data)), data, c='gray', ls='-') for i, p in enumerate(data): if (p > 1e-10): point, = mpl.plot(i, p, 'ro', ms=5) else: mpl.plot(i, p, 'ko', ms=3) mpl.margins(0.05) mpl.ylabel('Amplitude')
def show_mask(mask, img=None, fname=None, block=False): contours = measure.find_contours(mask, 0.8) # Display the image and plot all contours found fig, ax = plt.subplots() plt.gca().set_axis_off() plt.gca().xaxis.set_major_locator(matplotlib.ticker.NullLocator()) plt.gca().yaxis.set_major_locator(matplotlib.ticker.NullLocator()) plt.margins(0, 0) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) if img is not None: ax.imshow(img, interpolation='nearest', cmap=plt.cm.gray) else: ax.imshow(mask, interpolation='nearest', cmap=plt.cm.gray) for n, contour in enumerate(contours): ax.plot(contour[:, 1], contour[:, 0], linewidth=2) ax.set_xticks([]) ax.set_yticks([]) plt.show(block=block) if fname is not None: fig.savefig(fname, dpi=600, bbox_inches='tight', pad_inches=0.0) plt.close() else: plt.close()
def compare_cdf(self): # width as measured in inkscape width = 3.987 height = width / 1.618 fig, ax = plt.subplots() fig.subplots_adjust(left=.15, bottom=.19, right=.98, top=.97) data = self.data[151:] forecast = self.forecast[:-10] forecast_no_s = self.forecast_no_s[1:-9] forecast_garch = self.forecast_garch[1:] forecast_holt = self.forecast_holt[1:-9] print(len(data)) print(len(forecast)) print(len(forecast_no_s)) print(len(forecast_garch)) print(len(forecast_holt)) print(data.index[0]) print(forecast.index[0]) print(forecast_no_s.index[0]) print(forecast_garch.index[0]) print(forecast_holt.index[0]) print(data.index[-1]) print(forecast.index[-1]) print(forecast_no_s.index[-1]) print(forecast_garch.index[-1]) print(forecast_holt.index[-1]) forecast = forecast['Prediction'].values forecast_no_s = forecast_no_s['Prediction'].values forecast_garch = forecast_garch['Prediction'].values forecast_holt = forecast_holt['Prediction'].values data = data['BW'].values error = np.abs((data - forecast) / data) error_no_s = np.abs((data - forecast_no_s) / data) error_garch = np.abs((data - forecast_garch) / data) error_holt = np.abs((data - forecast_holt) / data) # error = self.reject_outliers(error) x = np.sort(error) y = np.arange(1, len(x) + 1) / len(x) x1 = np.sort(error_no_s) y1 = np.arange(1, len(x1) + 1) / len(x1) x2 = np.sort(error_holt) y2 = np.arange(1, len(x2) + 1) / len(x2) x3 = np.sort(error_garch) y3 = np.arange(1, len(x3) + 1) / len(x3) # _ = plt.plot(x, y, marker='.', linestyle='None') _ = plt.plot(x, y) _ = plt.plot(x1, y1, linestyle='--') _ = plt.plot(x2, y2, linestyle=':') # _ = plt.plot(x3, y3, linestyle='-.') ax.set_xlabel('Relative Error of Bandwidth Prediction') ax.set_ylabel('CDF') plt.legend(('SARIMA', 'ARIMA', 'Holt-Winters', 'GARCH'), loc='lower right') plt.margins(0.02) fig.set_size_inches(width, height) fig.savefig('cdf_new.pdf')
def roc_curve_plot(self): ## pre-process roc curve self.roc_table['fold'] = range(1, self.k_outer + 1) self.roc_table.set_index('fold', inplace=True) ## plot settings plt.figure(figsize=(6, 6)) plt.margins(y=0, x=0) plt.grid() ## plot roc curve for i in self.roc_table.index: plt.plot(self.roc_table.loc[i]['fpr'], self.roc_table.loc[i]['tpr'], label="Fold {}, AUC = {:.3f}".format( i, self.roc_table.loc[i]['auc'])) ## baseline plt.plot([0, 1], [0, 1], color='black', linestyle='--', linewidth=1) ## title and legend plt.title('Receiver Operating Characteristic Curve') plt.legend(loc='lower right') ## y-axis labels plt.yticks(np.arange(0.0, 1.1, step=0.1)) plt.ylabel("True Positive Rate") ## x-axis labels plt.xticks(np.arange(0.0, 1.1, step=0.1)) plt.xlabel("False Positive Rate")
def plott(self, arg=None): ''' Plot samples in time domain ''' if (arg is not None): arg.grid(True) arg.plot(self.fninputs, self.fnoutputs, label=self.label) else: mpl.grid(True) mpl.plot(self.fninputs, self.fnoutputs, label=self.label) mpl.plot(self.fninputs, np.zeros(len(self.fninputs)), 'k') mpl.margins(0.05)
def show_minutiae_sets(img, minutiae_sets, mask=None, fname=None, block=True): # for the latent or the low quality rolled print fig, ax = plt.subplots(1) ax.set_aspect('equal') plt.gca().set_axis_off() plt.gca().xaxis.set_major_locator(matplotlib.ticker.NullLocator()) plt.gca().yaxis.set_major_locator(matplotlib.ticker.NullLocator()) plt.margins(0, 0) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) arrow_len = 15 if mask is not None: h, w = mask.shape contours = measure.find_contours(mask, 0.8) for n, contour in enumerate(contours): ax.plot(contour[:, 1], contour[:, 0], linewidth=1) ax.imshow(img, cmap='gray') color = ['r', 'b', 'g'] R = [8, 10, 12] for k in range(len(minutiae_sets)): minutiae = minutiae_sets[k] minu_num = len(minutiae) for i in range(0, minu_num): xx = minutiae[i, 0] yy = minutiae[i, 1] circ = Circle((xx, yy), R[k], color=color[k], fill=False, linewidth=1.5) ax.add_patch(circ) ori = -minutiae[i, 2] dx = math.cos(ori) * arrow_len dy = math.sin(ori) * arrow_len ax.arrow(xx, yy, dx, dy, linewidth=1.5, head_width=0.05, head_length=0.1, fc=color[k], ec=color[k]) plt.show(block=block) if fname is not None: fig.savefig(fname, dpi=600, bbox_inches='tight', pad_inches=0.0) plt.close()
def plot_ac_to_compare(filter_data_from_spice, coef_b, coef_a, FS, visualise=False): if visualise: # get and plot data from spice w1, h1 = parse_data(filter_data_from_spice) # get and plot data from coesfs w, h = signal.freqz(coef_b, coef_a) plt.figure() plt.plot(w1, h1) plt.semilogx(w / max(w) * FS / 2, 20 * np.log10(abs(h))) plt.xlabel("Frequency [ Hz ]") plt.ylabel("Amplitude [dB]") plt.margins(0, 0.1) plt.grid(which="both", axis="both") plt.show()
def plot_wf(wf, sr=None, figsize=DFLT_FIGSIZE_FOR_WF_PLOTS, offset_s=0, ax=None, **kwargs): if figsize is not None: plt.figure(figsize=figsize) _ax = ax or plt if sr is not None: _ax.plot( offset_s + linspace(start=0, stop=len(wf) / float(sr), num=len(wf)), wf, **kwargs) plt.margins(x=0) else: _ax.plot(wf, **kwargs) plt.margins(x=0) return if _ax == plt: _xticks, _ = plt.xticks() plt.xticks(_xticks, str_ticks(ticks=_xticks, ticks_unit=1)) plt.margins(x=0) else: _xticks = _ax.get_xticks() _ax.set_xticks(_xticks) _ax.set_xticklabels(str_ticks(ticks=_xticks, ticks_unit=1)) plt.margins(x=0)
def wind_radar_affine(self): ''' 功能是生成仿射,并保存。 把函数直接当属性用的方法: ''' x = np.round(np.linspace(self.stg_X[0], self.stg_X[1], self.pixel[0]), 4) y = np.round(np.linspace(self.stg_Y[0], self.stg_Y[1], self.pixel[1]), 4) [X, Y] = np.meshgrid(x, y) # 得把时间传进来 start_time = self.real_time end_time = self.real_time + self.duration self.time_split = 3 # (end_time - start_time)/self.interval for i in range(self.time_split): ptime = self.real_time + (i + 1) * interval Z = self.read_json_data(self.real_time) # 只读取此刻真实的radar. feng_x = self.read_nc_data('U', ptime) feng_y = self.read_nc_data('V', ptime) model = AffineModel() fx, fy = model(feng_x, feng_y) cmaps = colors.LinearSegmentedColormap.from_list('mylist', self.colors_sys, N=6) fig = pb.gcf() fig.set_size_inches(4, 4) X += fx * 1 # 这里是interval,按小时计算的interval,数值预报默认是1小时出 Y += fy * 1 pb.contourf(X, Y, Z, radar_sys, cmap=cmaps, extend='both') # 循环的生成 pb.xlim(self.stg_X[0], self.stg_X[1]) pb.ylim(self.stg_Y[0], self.stg_Y[1]) pb.gca().xaxis.set_major_locator(plt.NullLocator()) pb.gca().yaxis.set_major_locator(plt.NullLocator()) pb.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) pb.margins(0, 0) affine_image_path = self.save_affine_path + self.real_time + ptime + 'affine' + '.png' fig.savefig(affine_image_path, format='png', transparent=True, dpi=200, pad_inches=0)
def plot_d_ang(slot, slot_ref, key, dt, table): """ Convert this into a chandra_aca tool """ # plot delta yan(or zan) #ylim = [(76, 82), (2272, 2278)] ok1 = table['slot'] == slot ok2 = table['slot'] == slot_ref fig = plt.figure(figsize=(7.5, 2.5)) #ylim = None for i, bgd_class_name in enumerate(Bgd_Class_Names): if i == 0: ax1 = plt.subplot(1, 3, 1) plt.ylabel('delta {} (arcsec)'.format(key)) else: ax = plt.subplot(1, 3, i + 1, sharey=ax1) plt.setp(ax.get_yticklabels(), visible=False) ok = table['bgd_class_name'] == bgd_class_name ang_interp = Ska.Numpy.interpolate(table[ok * ok1][key][0], table[ok * ok1]['time'][0] + dt, table[ok * ok2]['time'][0], method="nearest") d_ang = table[ok * ok2][key][0] - ang_interp time = table[ok * ok2]['time'][0] - table[ok * ok2]['time'][0][0] plt.plot(time, d_ang, color='Darkorange', label='std = {:.5f}'.format(np.std(d_ang - np.median(d_ang)))) plt.xlabel('Time (sec)') plt.title(bgd_class_name) plt.legend() plt.margins(0.05) #if ylim is None: # ylim = plt.gca().get_ylim() #else: # plt.ylim(ylim) plt.subplots_adjust(left=0.05, right=0.99, bottom=0.2, top=0.9, hspace=0.3, wspace=0.1) return
def save_figure(date, folder): global g_imagecount directory = "{}/{}".format(folder, date) if not os.path.exists(directory): os.makedirs(directory) filename = "{}/{}/{}.png".format(folder, date, str(g_imagecount).zfill(5)) extent = plt.gca().get_window_extent().transformed( plt.gcf().dpi_scale_trans.inverted()) plt.gca().set_axis_off() plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.margins(0, 0) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.savefig(filename, bbox_inches='tight', pad_inches=0) g_imagecount += 1
def xgetps10arcmin(ra, dec, size1, otname): ps1img = "%s_ps1_0.jpg" % (otname) # grayscale image #gim = getgrayim(ra,dec,size=size1,filter="i") # color image cim = getcolorim(ra, dec, size=size1, filters="grz") #r image #cim = getgrayim(ra,dec, size=size1, filter="r") #print(dir(cim)) cim.save(ps1img) if os.access(ps1img, os.F_OK): plt.figure(figsize=(4, 4), dpi=50) #set size square img_arr = plt.imread(ps1img) plt.imshow(img_arr) plt.xticks([]) plt.yticks([]) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0) plt.margins(0, 0) x = 1200 y = 1200 plt.scatter(x, y, marker="o", c='', edgecolors='w', s=1000) textc = "10*10 arcmin" RA_Hour = ra2hour(ra) DEC_Hour = dec2hour(dec) radec = "RA=%s, DEC=%s" % (RA_Hour, DEC_Hour) plt.text(800, 200, otname, color="w") plt.text(400, 300, radec, color="w") plt.text(1600, 2200, textc, color="w") #plt.show() pngfilename = "%s_ps1.png" % (otname) plt.savefig(pngfilename, dpi=50) return pngfilename else: print("no ps1 image ")
def plot_ac_to_compare(filter_data_from_spice, coef_b, coef_a, FS, visualise=False): if visualise: #get and plot data from spice w1, h1 = parse_data(filter_data_from_spice) #get and plot data from coesfs w, h = signal.freqz(coef_b, coef_a) plt.figure() plt.plot(w1, h1) plt.semilogx(w / max(w) * FS / 2, 20 * np.log10(abs(h))) plt.xlabel('Frequency [ Hz ]') plt.ylabel('Amplitude [dB]') plt.margins(0, 0.1) plt.grid(which='both', axis='both') plt.show()
def plot_coords(slot, table, coord, mag=None): """ Plot row or col or yan or zan centroid as a function of time. Add corresponding 'true' centroid in case of simulated data. :param slot: slot number :param table: table with results of call to centroids() (see centroids.py) :param coord: name of coordinate, one of 'row', 'col', 'yan', 'zan' """ fig = plt.figure(figsize=(9, 6)) color = ['green', 'red', 'blue'] for i, bgd_class_name in enumerate(Bgd_Class_Names): #print '{:.2f}'.format(np.median(t[coord][slot])) ok1 = table['bgd_class_name'] == bgd_class_name ok2 = table['slot'] == slot ok = ok1 * ok2 if mag is not None: ok3 = table['mag'] == mag ok = ok * ok3 time = table[ok]['time'][0] - table[ok]['time'][0][0] plt.plot(time, table[ok][coord][0], color=color[i], label=bgd_class_name) plt.margins(0.05) text = "" if "true_" + coord in table.colnames: time = table['time'][slot] - table['time'][slot][0] plt.plot(time, table[ok]["true_" + coord][slot], '--', color='k', lw='2', label="True") text = " and true " + coord + " coordinates." plt.ylabel(coord) plt.xlabel("Time (sec)") plt.title("Derived " + coord + text) plt.grid() plt.legend() return
def plotf(self, data=None): ''' Plot in frequency domain (DFT on samples and then plot) ''' if data is None: if (self.raw_dft is None): self.fft() mpl.grid(True) mpl.plot(range(0, len(self.real_dft)), self.real_dft, c='gray', ls='-') for i, p in enumerate(self.real_dft): if (p>1e-10): point, = mpl.plot(i, p, 'ro', ms=5) if self.fig: ''' http://stackoverflow.com/questions/11537374/matplotlib-basemap-popup-box/11556140#11556140 we can set self.fig and hover a mouse over a point to see annotation with frequency and amplitude in dft graph ''' annotation = mpl.annotate("F=%sHz\nA=%s" % (i, p), xy=(i, p), xycoords='data', xytext=(i, p), textcoords='data', horizontalalignment="right", bbox=dict(boxstyle="round", facecolor="w", edgecolor="0.5", alpha=0.9)) annotation.set_visible(False) self.pwa.append([point, annotation]) else: mpl.plot(i, p, 'ko', ms=3) if self.fig: self.fig.canvas.mpl_connect('motion_notify_event', self.on_move) mpl.xticks(range(0, len(self.real_dft), int(self.sampl/32) if int(self.sampl/32)>0 else 1), rotation=0) mpl.margins(0.05) mpl.xlabel('Frequency [Hz]') mpl.ylabel('Amplitude') else: mpl.grid(True) mpl.plot(range(0, len(data)), data, c='gray', ls='-') for i, p in enumerate(data): if (p>1e-10): point, = mpl.plot(i, p, 'ro', ms=5) else: mpl.plot(i, p, 'ko', ms=3) mpl.margins(0.05) mpl.ylabel('Amplitude')
def count_intent_vrm_json(input_fn, logger): with open(input_fn, 'r') as readfile: dialogs = json.load(readfile) vrms = {} for d in dialogs: for speech in d: vrm = speech['vrm'][-1] if vrm in vrms: vrms[vrm] += 1 else: vrms[vrm] = 1 # logger.info("Intent VRM codes...") # for k in vrms.keys(): # logger.info(f"{k}: {vrms[k]}") X = np.array(sorted(vrms, key=vrms.get, reverse=True)) Y = np.array(sorted(vrms.values(), reverse=True)) total_vrms = sum(vrms.values()) mpl.rc('font', **{'size': 12}) plt.title("Intent VRMs in script") plt.bar(X, Y, 1, facecolor="#ef5350", edgecolor="white") for x, y in zip(X, Y): plt.text(x, y + 0.5, '%d' % y, ha='center', va='bottom') if y == Y[-1]: plt.text(x, y + 24, '(%.1f%%)' % (y / total_vrms * 100), ha='center', va='bottom', color="#999999") else: plt.text(x, y - 30, '%.1f%%' % (y / total_vrms * 100), ha='center', va='bottom', color="white") plt.margins(0.05, 0.1) plt.show()
def plot_coords_ratio(table1, table2, coord, slot=0, mag=None, bgd_class_name='FlightBgd'): """ Plot ratio of centroid coordinate (row or col or yan or zan) derived using different number of samples (ndeque). :param table1: table with results of call to centroids() (see centroids.py) :param table2: table with results of call to centroids() (see centroids.py) :param slot: slot number :param coord: name of coordinate, one of 'row', 'col', 'yan', 'zan' :param mag: magnitude :param bgd_class_name: algorithm for background calculation """ coords = [] for i, tab in enumerate([table1, table2]): ok1 = tab['bgd_class_name'] == bgd_class_name ok2 = tab['slot'] == slot ok = ok1 * ok2 if mag is not None: ok3 = tab['mag'] == mag ok = ok * ok3 coords.append(tab[ok][coord][0]) time = tab[ok]['time'][0] - tab[ok]['time'][0][0] plt.plot(time, coords[0] / coords[1], color='darkorange', label=bgd_class_name) plt.xlabel("Time (sec)") plt.ylabel("{} coord ratio".format(coord)) plt.grid() plt.legend() plt.margins(0.05) return
def show_orientation_field(img, dir_map, mask=None, fname=None, block=True): h, w = img.shape[:2] if mask is None: mask = np.ones((h, w), dtype=np.uint8) blkH, blkW = dir_map.shape blk_size = h / blkH R = blk_size / 2 * 0.8 fig, ax = plt.subplots(1) plt.gca().set_axis_off() plt.gca().xaxis.set_major_locator(matplotlib.ticker.NullLocator()) plt.gca().yaxis.set_major_locator(matplotlib.ticker.NullLocator()) plt.margins(0, 0) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) ax.imshow(img, cmap='gray') for i in range(blkH): y0 = i * blk_size + blk_size / 2 y0 = int(y0) for j in range(blkW): x0 = j * blk_size + blk_size / 2 x0 = int(x0) ori = dir_map[i, j] if mask[y0, x0] == 0: continue if ori < -9: continue x1 = x0 - R * math.cos(ori) x2 = x0 + R * math.cos(ori) y1 = y0 - R * math.sin(ori) y2 = y0 + R * math.sin(ori) plt.plot([x1, x2], [y1, y2], 'r-', lw=2) plt.show(block=block) if fname is not None: fig.savefig(fname, dpi=600, bbox_inches='tight', pad_inches=0.0) plt.close()
def plot_coords_excess(slot, table, coord): """ Plot difference between true centroid coordinate (row or col or yan or zan) and derived centroid coordinates, as a function of time. Compute and display standard deviation of residuals. :param slot: slot number :param table: table with results of call to centroids() (see centroids.py) :param coord: name of coordinate, one of 'row', 'col', 'yan', 'zan' """ fig = plt.figure(figsize=(9, 6)) color = ['green', 'red', 'blue'] for i, bgd_class_name in enumerate(Bgd_Class_Names): ok = table['bgd_class_name'] == bgd_class_name excess = table[ok][coord][slot] - table[ok]["true_" + coord][slot] std = np.std(excess - np.median(excess)) time = table['time'][slot] - table['time'][slot][0] plt.plot(time, excess, color=color[i], label="std = {:.3f}, ".format(std) + bgd_class_name) plt.margins(0.05) if coord in ['yan', 'zan']: unit = '(arcsec)' else: unit = '(pixel)' plt.ylabel(coord + " - true " + coord + " " + unit) plt.xlabel("Time (sec)") plt.title("Difference between derived " + coord + " and true " + coord + " coordinates") plt.grid() plt.legend() return
def shap_deep_explainer(self, model_no, num_reference, img_input, ranked_outputs=1, norm_reverse=True, blend_original_image=False, gif_fps=1, base_dir_save='/tmp/DeepExplain', check_additivity=False): #region mini-batch because of GPU memory limitation list_shap_values = [] batch_size = self.dicts_models[model_no]['batch_size'] split_times = math.ceil(num_reference / batch_size) for i in range(split_times): shap_values_tmp1 = self.list_e[model_no][i].shap_values( img_input, ranked_outputs=ranked_outputs, check_additivity=check_additivity) # shap_values ranked_outputs # [0] [0] (1,299,299,3) # [1] predict_class array shap_values_copy = copy.deepcopy(shap_values_tmp1) list_shap_values.append(shap_values_copy) for i in range(ranked_outputs): for j in range(len(list_shap_values)): if j == 0: shap_values_tmp2 = list_shap_values[0][0][i] else: shap_values_tmp2 += list_shap_values[j][0][i] shap_values_results = copy.deepcopy(list_shap_values[0]) shap_values_results[0][i] = shap_values_tmp2 / split_times #endregion #region save files str_uuid = str(uuid.uuid1()) list_classes = [] list_images = [] for i in range(ranked_outputs): predict_class = int( shap_values_results[1][0][i]) #numpy int 64 - int list_classes.append(predict_class) save_filename = os.path.join( base_dir_save, str_uuid, 'Shap_Deep_Explainer{}.jpg'.format(predict_class)) os.makedirs(os.path.dirname(save_filename), exist_ok=True) list_images.append(save_filename) pred_class_num = len(shap_values_results[0]) if blend_original_image: if norm_reverse: img_original = np.uint8(input_norm_reverse(img_input[0])) else: img_original = np.uint8(img_input[0]) img_original_file = os.path.join(os.path.dirname(list_images[0]), 'deepshap_original.jpg') cv2.imwrite(img_original_file, img_original) for i in range(pred_class_num): # predict_max_class = attributions[1][0][i] attribution1 = shap_values_results[0][i] #attributions.shape: (1, 299, 299, 3) data = attribution1[0] data = np.mean(data, -1) abs_max = np.percentile(np.abs(data), 100) abs_min = abs_max # dx, dy = 0.05, 0.05 # xx = np.arange(0.0, data1.shape[1], dx) # yy = np.arange(0.0, data1.shape[0], dy) # xmin, xmax, ymin, ymax = np.amin(xx), np.amax(xx), np.amin(yy), np.amax(yy) # extent = xmin, xmax, ymin, ymax # cmap = 'RdBu_r' # cmap = 'gray' cmap = 'seismic' plt.axis('off') # plt.imshow(data1, extent=extent, interpolation='none', cmap=cmap, vmin=-abs_min, vmax=abs_max) # plt.imshow(data, interpolation='none', cmap=cmap, vmin=-abs_min, vmax=abs_max) # fig = plt.gcf() # fig.set_size_inches(2.99 / 3, 2.99 / 3) # dpi = 300, output = 700*700 pixels plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.margins(0, 0) if blend_original_image: plt.imshow(data, interpolation='none', cmap=cmap, vmin=-abs_min, vmax=abs_max) save_filename1 = list_images[i] plt.savefig(save_filename1, bbox_inches='tight', pad_inches=0) plt.close() img_heatmap = cv2.imread(list_images[i]) (tmp_height, tmp_width) = img_original.shape[:-1] img_heatmap = cv2.resize(img_heatmap, (tmp_width, tmp_height)) img_heatmap_file = os.path.join( os.path.dirname(list_images[i]), 'deepshap_{0}.jpg'.format(i)) cv2.imwrite(img_heatmap_file, img_heatmap) dst = cv2.addWeighted(img_original, 0.65, img_heatmap, 0.35, 0) img_blend_file = os.path.join( os.path.dirname(list_images[i]), 'deepshap_blend_{0}.jpg'.format(i)) cv2.imwrite(img_blend_file, dst) #region create gif import imageio mg_paths = [ img_original_file, img_heatmap_file, img_blend_file ] gif_images = [] for path in mg_paths: gif_images.append(imageio.imread(path)) img_file_gif = os.path.join(os.path.dirname(list_images[i]), 'deepshap_{0}.gif'.format(i)) imageio.mimsave(img_file_gif, gif_images, fps=gif_fps) list_images[i] = img_file_gif #endregion else: plt.imshow(data, interpolation='none', cmap=cmap, vmin=-abs_min, vmax=abs_max) save_filename1 = list_images[i] plt.savefig(save_filename1, bbox_inches='tight', pad_inches=0) plt.close() #endregion return list_classes, list_images
def plot_heatmap_shap(attributions, list_images, img_input, blend_original_image): pred_class_num = len(attributions[0]) if blend_original_image: from LIBS.ImgPreprocess.my_image_norm import input_norm_reverse img_original = np.uint8(input_norm_reverse(img_input[0])) import cv2 img_original = cv2.resize(img_original, (384, 384)) img_original_file = os.path.join(os.path.dirname(list_images[0]), 'deepshap_original.jpg') cv2.imwrite(img_original_file, img_original) for i in range(pred_class_num): # predict_max_class = attributions[1][0][i] attribution1 = attributions[0][i] #attributions.shape: (1, 299, 299, 3) data = attribution1[0] data = np.mean(data, -1) abs_max = np.percentile(np.abs(data), 100) abs_min = abs_max # dx, dy = 0.05, 0.05 # xx = np.arange(0.0, data1.shape[1], dx) # yy = np.arange(0.0, data1.shape[0], dy) # xmin, xmax, ymin, ymax = np.amin(xx), np.amax(xx), np.amin(yy), np.amax(yy) # extent = xmin, xmax, ymin, ymax # cmap = 'RdBu_r' # cmap = 'gray' cmap = 'seismic' plt.axis('off') # plt.imshow(data1, extent=extent, interpolation='none', cmap=cmap, vmin=-abs_min, vmax=abs_max) # plt.imshow(data, interpolation='none', cmap=cmap, vmin=-abs_min, vmax=abs_max) # fig = plt.gcf() # fig.set_size_inches(2.99 / 3, 2.99 / 3) # dpi = 300, output = 700*700 pixels plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.margins(0, 0) if blend_original_image: # cv2.imwrite('/tmp5/tmp/cv2.jpg', np.uint8(img_input[0])) # img_original = cv2.cvtColor(np.uint8(img_input[0]), cv2.COLOR_BGR2RGB) # plt.imshow(img_original) plt.imshow(data, interpolation='none', cmap=cmap, vmin=-abs_min, vmax=abs_max) save_filename1 = list_images[i] plt.savefig(save_filename1, bbox_inches='tight', pad_inches=0) plt.close() img_heatmap = cv2.imread(list_images[i]) img_heatmap = cv2.resize(img_heatmap, (384, 384)) img_heatmap_file = os.path.join(os.path.dirname(list_images[i]), 'deepshap_{0}.jpg'.format(i)) cv2.imwrite(img_heatmap_file, img_heatmap) dst = cv2.addWeighted(img_original, 0.65, img_heatmap, 0.35, 0) # cv2.imwrite('/tmp5/tmp/aaaaa.jpg', dst) #test code img_blend_file = os.path.join(os.path.dirname(list_images[i]), 'deepshap_blend_{0}.jpg'.format(i)) cv2.imwrite(img_blend_file, dst) # fig.savefig('/tmp5/tmp/aaa1.png', format='png', dpi=299, transparent=True, pad_inches=0) # plt.savefig('/tmp5/tmp/aaa.jpg', bbox_inches='tight', pad_inches=0) #region create gif import imageio mg_paths = [img_original_file, img_heatmap_file, img_blend_file] gif_images = [] for path in mg_paths: gif_images.append(imageio.imread(path)) img_file_gif = os.path.join(os.path.dirname(list_images[i]), 'deepshap_{0}.gif'.format(i)) imageio.mimsave(img_file_gif, gif_images, fps=GIF_FPS) list_images[i] = img_file_gif #endregion else: plt.imshow(data, interpolation='none', cmap=cmap, vmin=-abs_min, vmax=abs_max) save_filename1 = list_images[i] plt.savefig(save_filename1, bbox_inches='tight', pad_inches=0) plt.close()
def Morris(): N = 1000 an_m = 'morris' smp_m = 'morris' sa_results = pickle.load( open("SA_results_" + str(N) + "_" + an_m + "_" + smp_m, "rb")) data_mu = [] data_mu_star = [] data_sigma = [] banned = ['default_size', 'c1_size', 'cell_size', '_c1_iron_in_Plasma_0'] for out in sa_results.keys(): mu_row = [] mu_star_row = [] sigma_row = [] for i, n in enumerate(sa_results[out]['names']): if n not in banned: mu_row.append(sa_results[out]['mu'][i]) mu_star_row.append(sa_results[out]['mu_star'][i]) sigma_row.append(sa_results[out]['sigma'][i]) data_mu.append(mu_row) data_mu_star.append(mu_star_row) data_sigma.append(sigma_row) columns = [ x.split('_k1')[0] for x in sa_results['iron_in_Plasma_c1']['names'] if x not in banned ] rows = [k.split('_')[2] for k in sa_results.keys()] n_rows = len(data_mu) index = np.arange(len(columns)) bar_width = 0.8 for data in [data_mu_star, data_sigma]: colors = pl.cm.tab20(np.linspace(0, 1, n_rows)) y_offset = np.zeros(len(columns)) pl.figure(figsize=(30, 8)) cell_text = [] for row in range(n_rows): pl.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row]) y_offset = y_offset + data[row] cell_text.append(['%.3f' % x for x in data[row]]) # Reverse colors and text labels to display the last value at the top. colors = colors[::-1] cell_text.reverse() the_table = pl.table(cellText=cell_text, rowLabels=rows, rowColours=colors, colLabels=columns, loc='bottom') the_table.auto_set_font_size(False) the_table.set_fontsize(9) pl.subplots_adjust(bottom=0.2) pl.xticks([]) pl.margins(x=0) if data == data_mu_star: pl.savefig('Morris_mu_star_results.png', bbox_inches="tight", dpi=300) elif data == data_sigma: pl.savefig('Morris_sigma_results.png', bbox_inches="tight", dpi=300) pl.clf()
plt.plot(sn,alpha=0.5,label='signal+noise') plt.plot(s1,alpha=0.8,label='signal '+str(f1) + 'Hz') plt.plot(s2,alpha=0.8,label='signal '+str(f2) + 'Hz') plt.legend() plt.show() # 時間軸応答を求める plt.plot(sn,alpha=0.5,label='signal ' + str(f1) + ' + ' +str(f2) + \ 'Hz' + ' + noise') #j = signal.lfilter(b,a,sn) j = signal.filtfilt(b,a,sn) plt.plot(j,alpha=0.9,label='Filtered') plt.legend() #plt.ylim(-3,3) plt.show() # 周波数応答を求める w,h = signal.freqz(b,a) plt.plot(w,20*np.log10(abs(h))) plt.xscale('log') plt.xlim(0.01*pi,pi) plt.ylim(-80,10) # plt.title('BUtterworth filter response') plt.xlabel('Normalized Frequency [rad/s]') plt.ylabel('Ampletude [dB]') plt.margins(0,0.1) plt.grid(which='both',axis='both') plt.axvline(cutoff*pi,color='green') plt.show()
plt.figure() # Set style as well as font to Computer Modern Roman to match LaTeX output sns.set(style='ticks', font='cmr10', rc={'mathtext.fontset': 'cm', 'axes.unicode_minus': False}) sns.kdeplot(events, shade=True, cut=0) if name == 'phi': # Show phi x-axis ticks in units of pi/2 plt.gca().xaxis.set_major_formatter(FuncFormatter( lambda val, pos: { 0: r'$-\pi$', 1: r'$\dfrac{-\pi}{2}$', 2: r'0', 3: r'$\dfrac{\pi}{2}$', 4: r'$\pi$', 5: r'_', 6: r'_', }[pos] )) plt.gca().xaxis.set_major_locator(MultipleLocator(base=np.pi / 2)) plt.xlabel(latex_name) plt.margins(x=0) plt.ylabel('Event Density') if args.write_svg is not None: filepath = args.write_svg.replace('%name%', name) bmf.stdout('Writing {}'.format(filepath)) plt.savefig(filepath, format='svg', bbox_inches='tight') else: plt.show()
def Builder( error=False, cv=False, accuracy=False, name="./GraphBuilder_default_name.png", legend_on=True, **kwargs ): # Colors Colors = ["c", "m", "y", "k"] color_idx = 0 # Add declared dict allDicts = dict() allDicts["error"] = error allDicts["cv"] = cv allDicts["accuracy"] = accuracy # Add kwargs for k in kwargs.keys(): allDicts[k] = kwargs[k] # Define valid dicts validDicts = dict() for k in allDicts.keys(): if any(allDicts[k]): validDicts[k] = allDicts[k] # Define longest (with more points) graph lenLongestDict = 0 keylongestDict = None for k in validDicts.keys(): if len(validDicts[k]) > lenLongestDict: lenLongestDict = len(validDicts[k]) keylongestDict = k # Calc axes's step stepsDict = dict() for k in validDicts.keys(): numberOfPoints = len(validDicts[k]) stepsDict[k] = np.round(np.linspace(0, lenLongestDict, numberOfPoints, endpoint=True)) # plot for k in validDicts.keys(): if k == "error": plot(stepsDict[k], validDicts[k], "r,", markeredgewidth=0, label="Error", zorder=0) elif k == "cv": plot(stepsDict[k], validDicts[k], "g.", markeredgewidth=0, label="CV", zorder=3) elif k == "accuracy": plot(stepsDict[k], validDicts[k], "b.", markeredgewidth=0, label="Accuracy", zorder=2) else: plot(stepsDict[k], validDicts[k], str(Colors[color_idx] + "."), markeredgewidth=0, label=k, zorder=1) color_idx += 1 # Titles title("Error vs epochs", fontsize=12) xlabel("epochs", fontsize=10) ylabel("Error", fontsize=10) if legend_on: legend(loc="upper right", fontsize=10, numpoints=3, shadow=True, fancybox=True) # Grid grid() margins(0.04) savefig(name, dpi=120) close()
def Builder(error=False, cv=False, accuracy=False, name='./GraphBuilder_default_name.png', legend_on=True, **kwargs): #Colors Colors = ['c', 'm', 'y', 'k'] color_idx = 0 #Add declared dict allDicts = dict() allDicts['error'] = error allDicts['cv'] = cv allDicts['accuracy'] = accuracy #Add kwargs for k in kwargs.keys(): allDicts[k] = kwargs[k] #Define valid dicts validDicts = dict() for k in allDicts.keys(): if any(allDicts[k]): validDicts[k] = allDicts[k] #Define longest (with more points) graph lenLongestDict = 0 keylongestDict = None for k in validDicts.keys(): if len(validDicts[k]) > lenLongestDict: lenLongestDict = len(validDicts[k]) keylongestDict = k #Calc axes's step stepsDict = dict() for k in validDicts.keys(): numberOfPoints = len(validDicts[k]) stepsDict[k] = np.round( np.linspace(0, lenLongestDict, numberOfPoints, endpoint=True)) #plot for k in validDicts.keys(): if k == 'error': plot(stepsDict[k], validDicts[k], 'r,', markeredgewidth=0, label='Error', zorder=0) elif k == 'cv': plot(stepsDict[k], validDicts[k], 'g.', markeredgewidth=0, label='CV', zorder=3) elif k == 'accuracy': plot(stepsDict[k], validDicts[k], 'b.', markeredgewidth=0, label='Accuracy', zorder=2) else: plot(stepsDict[k], validDicts[k], str(Colors[color_idx] + '.'), markeredgewidth=0, label=k, zorder=1) color_idx += 1 #Titles title('Error vs epochs', fontsize=12) xlabel('epochs', fontsize=10) ylabel('Error', fontsize=10) if legend_on: legend(loc='upper right', fontsize=10, numpoints=3, shadow=True, fancybox=True) #Grid grid() margins(0.04) savefig(name, dpi=120) close()
a = trigfn(sampl=ns, freq=8, phase=90) b = trigfn(sampl=ns, freq=12, phase=90) y=a+b y=y/4 mpl.figure() mpl.subplot(4,1,1) mpl.plot(no) mpl.title('szum') mpl.margins(0.05) mpl.grid() mpl.subplot(4,1,2) mpl.grid() y.plot() mpl.title('fala okresowa') mpl.margins(0.05) ni = y.fnoutputs + no/2 mpl.subplot(4,1,3) mpl.plot(ni) mpl.title('szum + fala okresowa') mpl.margins(0.05) mpl.grid()
import matplotlib.pylab as plt import numpy as np with open('dados_reduzidos.txt') as f: dados = [[float(x) for x in line.split()] for line in f] # print(dados) x = np.linspace(1, 6, 100) for i in range(len(dados)): plt.errorbar(dados[i][0], dados[i][1], yerr=dados[i][2], fmt="or") plt.plot(x, (x/9.81)**(0.5), "b-") plt.xlabel('Altura(m)') plt.ylabel('Tempo(s)') plt.title('Resultados do experimento') plt.margins(0.1) plt.show()
def run(g_inc, L=20e-6, T=900, dt=1, J=40, k_u=1e-33, k_d=1e-30, suffix="a", D=1e-9): u""" T = time, dt - time step, J - x steps, k_u, k_d - recombination """ dx = float(L) / float(J - 1) # Grid parameter x_grid = np.array([j * dx for j in range(J)]) # Grid N = 1 + int(float(T) / float(dt)) # Time step t_grid = np.array([n * dt for n in range(N)]) # Time grid # plt.plot(t_grid,'.') # plt.show() T_membrane = 573.0 # D=2.9e-7*np.exp(-0.23*1.6e-19/(1.38e-23*T_membrane)) # Diffusion coeffitient for U sigma = float(D * dt) / float((2.0 * dx * dx)) if 0: print("%.3e:\tN" % N) print("%.3e:\tdx" % dx) print("%.3e:\tsigma" % sigma) print("%.3e:\tdt" % dt) # suffix = '%.0es %.0e'%(dt, dx) u""" initial concentration """ U = np.array([0.0 for _ in range(J)]) ff = interp1d(g_inc[:, 0], g_inc[:, 1]) g_inc = ff(t_grid) if 0: plt.plot(t_grid, g_inc, ".") plt.margins(0.1) plt.show() def f_vec(U, ti): "ti - time index" upstream = -D / (2.0 * k_u * dx) + 0.5 * np.sqrt( (D / (k_u * dx))**2 + 4 * D * U[1] / (k_u * dx) + 4 * g_inc[ti - 1] / k_u) downstream = -D / (2.0 * k_d * dx) + 0.5 * np.sqrt( (D / (k_d * dx))**2 + 4 * D * U[-2] / (k_d * dx)) vivec = np.array([0.0 for _ in range(J)]) vivec[0] = upstream vivec[-1] = downstream return vivec plt.gca().margins(0.1) u""" Matrixes """ A = (np.diagflat([-sigma for _ in range(J - 1 - 1)] + [0.0], -1) + np.diagflat([1.0] + [1.0 + 2.0 * sigma for _ in range(J - 2)] + [1.0]) + np.diagflat([0.0] + [-sigma for _ in range(J - 1 - 1)], 1)) B = (np.diagflat([sigma for _ in range(J - 1 - 1)] + [0.0], -1) + np.diagflat([0.0] + [1.0 - 2.0 * sigma for _ in range(J - 2)] + [0.0]) + np.diagflat([0.0] + [sigma for _ in range(J - 1 - 1)], 1)) U_record = [] U_record.append(U) # U_record.append([U[0],U[-1]]) u""" Solving matrix equation for all time-layers""" for ti in range(1, N): U_new = np.linalg.solve(A, B.dot(U) + f_vec(U, ti)) # numpy Gauss U = U_new U_record.append(U) # U_record.append([U[0],U[-1]]) fig = plt.figure() if 1: ax2 = fig.add_subplot(2, 1, 2) plt.xlabel("x") plt.ylabel("concentration") nn = 20 tt = np.linspace(0 + T / float(nn), T - T / float(nn), nn) plt.plot(x_grid, U_record[0], "k.", label="t = %.1f" % t_grid[0], lw=2) color_idx = np.linspace(0, 1, nn) for ij, t1 in enumerate(tt): plt.plot( x_grid, U_record[int(t1 / dt)], label="t = %.2f" % t_grid[int(t1 / dt)], color=plt.cm.jet(color_idx[ij]), ) # @UndefinedVariable plt.plot(x_grid, U, "k--", label="t = %.1f" % t_grid[-1], lw=2) # legend(framealpha = 0.8) plt.margins(0.1) pth = os.path.join(tl.docs(0), "pyfit", "diff_profile%s.png" % suffix) dirpth = os.path.dirname(pth) if not os.path.isdir(dirpth): os.makedirs(dirpth) # plt.savefig(pth,bbox_inches = 'tight', dpi = 300) ax2 = fig.add_subplot(2, 1, 1) U_r = np.array(U_record) try: plt.plot(t_grid, U_r[:, -1]**2 * k_d, label="out") except: plt.plot(t_grid, U_r[1]**2 * k_d, label="out") # plot(t_grid,U_r[:,0]**2*k_u,label = 'input') if 0: "plot 24248 PDP6 experimental result" epth = os.path.join(tl.docs(5), "workspace", "fit", "output", "24248_PDP6_gamma_pdp_exp.txt") expdata = np.loadtxt(epth, skiprows=1) plt.plot(expdata[:, 0], expdata[:, 1], label="29005 PDP6") plt.legend(framealpha=0.8) plt.margins(0.1) pth = os.path.join(tl.docs(0), "pyfit", "diff_fluxes%s.png" % suffix) plt.suptitle(r"$%ss\;%sm\;k_u = %s;k_d = %s;D = %s$" % (ltexp( dt, 0), ltexp(dx, 1), ltexp(k_u, 1), ltexp(k_d, 1), ltexp(D, 2))) plt.grid(True) return plt.gcf()
def plot_px_history(table, keys, hot_pixels=None, slot=0, mag=None, bgd_class_name='FlightBgd', legend_text="", title_text='Hot'): """ Plot time series of a given ACA pixel value. :param table: table with results of call to centroids() (see centroids.py) :param keys: list containing pixel coordinates, e.g. [(120, 210), (30, 150)] :param slot: slot number :param mag: magnitude :param bgd_class_name: class used for background calculation (see classes.py) """ n = len(keys) ll = 3 * n fig = plt.figure(figsize=(6, ll)) ok1 = table['slot'] == slot ok2 = table['bgd_class_name'] == bgd_class_name ok = ok1 * ok2 if mag is not None: ok3 = table['mag'] == mag ok = ok * ok3 for i, key in enumerate(keys): plt.subplot(n, 1, i + 1) deques = table[ok]['deque_dict'][0] time = table[ok]['time'][0] - table[ok]['time'][0][0] px_vals = [] bgd_vals = [] current_bgd_val = 0 for i, deque in enumerate(deques): if key in deque.keys(): current_px_val = deque[key][-1] if bgd_class_name == 'DynamBgd_Median': current_bgd_val = np.median(deque[key]) elif bgd_class_name == 'DynamBgd_SigmaClip': current_bgd_val = sigma_clip(deque[key]) px_vals.append(current_px_val) bgd_vals.append(current_bgd_val) else: px_vals.append(-1) bgd_vals.append(-1) if legend_text == 'Simulated': plt.plot(table['time'][0] - table['time'][0][0], hot_pixels[key], label=legend_text, color='gray') plt.plot(time, px_vals, label='Sampled', color='slateblue') plt.plot(time, bgd_vals, label="Derived", color='darkorange', lw=2) plt.xlabel('Time (sec)') plt.ylabel('Pixel value') plt.title('{} pixel coordinates = {}'.format(title_text, key)) plt.legend() plt.grid() plt.margins(0.05) plt.subplots_adjust(left=0.05, right=0.99, top=0.9, bottom=0.2, hspace=0.5, wspace=0.3) return
def Builder(error=False, cv=False, accuracy=False, name='./GraphBuilder_default_name.png', legend_on=True, Xlabel='epochs', Ylabel='Error', Title='Error vs epochs', **kwargs): #Colors Colors = ['c', 'm', 'y', 'k', colors.cnames['darkseagreen'], colors.cnames['darkslateblue'], colors.cnames['darkslategray'], colors.cnames['darkturquoise'], colors.cnames['darkviolet'], colors.cnames['deeppink'], colors.cnames['deepskyblue'], colors.cnames['dimgray']] color_idx = 0 #Add declared dict allDicts = dict() allDicts['error'] = error allDicts['cv'] = cv allDicts['accuracy'] = accuracy #Add kwargs for k in kwargs.keys(): allDicts[k] = kwargs[k] #Define valid dicts validDicts = dict() for k in allDicts.keys(): if any(allDicts[k]): validDicts[k] = allDicts[k] #Define longest (with more points) graph lenLongestDict = 0 keylongestDict = None for k in validDicts.keys(): if len(validDicts[k]) > lenLongestDict: lenLongestDict = len(validDicts[k]) keylongestDict = k #Calc axes's step stepsDict = dict() for k in validDicts.keys(): numberOfPoints = len(validDicts[k]) stepsDict[k] = np.round(np.linspace(0, lenLongestDict, numberOfPoints, endpoint=True)) #plot for k in validDicts.keys(): if k == 'error': plot(stepsDict[k], validDicts[k], 'r,', markeredgewidth=0, label='Error', zorder=0) elif k == 'cv': plot(stepsDict[k], validDicts[k], 'g.', markersize=4, markeredgewidth=0, label='CV', zorder=3) elif k == 'accuracy': plot(stepsDict[k], validDicts[k], 'b.', markeredgewidth=0, label='Accuracy', zorder=2) else: plot(stepsDict[k], validDicts[k], Colors[color_idx], marker='.', ls='-', markeredgewidth=0, label=k, zorder=1) color_idx += 1 #Titles title(Title, fontsize=12) xlabel(Xlabel, fontsize=10) ylabel(Ylabel, fontsize=10) if legend_on: legend(loc='best', fontsize=10, numpoints=3, shadow=True, fancybox=True) #Grid grid() margins(0.04) savefig(name, dpi=120, bbox_inches='tight') close()
def Builder(error=False, cv=False, accuracy=False, name='./GraphBuilder_default_name.png', legend_on=True, Xlabel='epochs', Ylabel='Error', Title='Error vs epochs', **kwargs): #Colors Colors = [ 'c', 'm', 'y', 'k', colors.cnames['darkseagreen'], colors.cnames['darkslateblue'], colors.cnames['darkslategray'], colors.cnames['darkturquoise'], colors.cnames['darkviolet'], colors.cnames['deeppink'], colors.cnames['deepskyblue'], colors.cnames['dimgray'] ] color_idx = 0 #Add declared dict allDicts = dict() allDicts['error'] = error allDicts['cv'] = cv allDicts['accuracy'] = accuracy #Add kwargs for k in kwargs.keys(): allDicts[k] = kwargs[k] #Define valid dicts validDicts = dict() for k in allDicts.keys(): if any(allDicts[k]): validDicts[k] = allDicts[k] #Define longest (with more points) graph lenLongestDict = 0 keylongestDict = None for k in validDicts.keys(): if len(validDicts[k]) > lenLongestDict: lenLongestDict = len(validDicts[k]) keylongestDict = k #Calc axes's step stepsDict = dict() for k in validDicts.keys(): numberOfPoints = len(validDicts[k]) stepsDict[k] = np.round( np.linspace(0, lenLongestDict, numberOfPoints, endpoint=True)) #plot for k in validDicts.keys(): if k == 'error': plot(stepsDict[k], validDicts[k], 'r,', markeredgewidth=0, label='Error', zorder=0) elif k == 'cv': plot(stepsDict[k], validDicts[k], 'g.', markersize=4, markeredgewidth=0, label='CV', zorder=3) elif k == 'accuracy': plot(stepsDict[k], validDicts[k], 'b.', markeredgewidth=0, label='Accuracy', zorder=2) else: plot(stepsDict[k], validDicts[k], Colors[color_idx], marker='.', ls='-', markeredgewidth=0, label=k, zorder=1) color_idx += 1 #Titles title(Title, fontsize=12) xlabel(Xlabel, fontsize=10) ylabel(Ylabel, fontsize=10) if legend_on: legend(loc='best', fontsize=10, numpoints=3, shadow=True, fancybox=True) #Grid grid() margins(0.04) savefig(name, dpi=120, bbox_inches='tight') close()
def Sobol(): N = 1000 an_m = 'sobol' smp_m = 'saltelli' sa_results = pickle.load( open("SA_results_" + str(N) + "_" + an_m + "_" + smp_m, "rb")) names = [ 'r1_k1', 'r19_k1', 'r11_k1', 'r6_k1', 'r23_k1', 'r12_k1', 'r24_k1', 'r8_k1', 'r17_k1', 'r10_k1', 'r26_k1', 'r15_k1', 'r21_k1', 'r20_k1', 'r7_k1', 'r29_k1', 'r25_k1', 'r9_k1', 'r18_k1', 'r27_k1', 'r16_k1', 'r22_k1', 'r4_k1', 'r14_k1', 'r13_k1', 'r28_k1', 'r2_k1', 'r5_k1', 'r3_k1', 'default_size', 'c1_size', 'cell_size', '_c1_iron_in_Plasma_0' ] data_s1 = [] data_st = [] data_s2 = [] banned = ['default_size', 'c1_size', 'cell_size', '_c1_iron_in_Plasma_0'] for out in sa_results.keys(): s1_row = [] st_row = [] s2_row = [] for i, n in enumerate(names): if n not in banned: s1_row.append(sa_results[out]['S1'][i]) st_row.append(sa_results[out]['ST'][i]) s2_row.append(sa_results[out]['S2'][i, :29]) data_s1.append(s1_row) data_st.append(st_row) data_s2.append(s2_row) columns = [x.split('_k1')[0] for x in names if x not in banned] rows = [k.split('_')[2] for k in sa_results.keys()] n_rows = len(data_s1) index = np.arange(len(columns)) bar_width = 0.8 for data in [data_s1, data_st]: colors = pl.cm.tab20(np.linspace(0, 1, n_rows)) y_offset = np.zeros(len(columns)) pl.figure(figsize=(30, 8)) cell_text = [] for row in range(n_rows): pl.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row]) y_offset = y_offset + data[row] cell_text.append(['%.3f' % x for x in data[row]]) # Reverse colors and text labels to display the last value at the top. colors = colors[::-1] cell_text.reverse() the_table = pl.table(cellText=cell_text, rowLabels=rows, rowColours=colors, colLabels=columns, loc='bottom') the_table.auto_set_font_size(False) the_table.set_fontsize(9) pl.subplots_adjust(bottom=0.2) pl.xticks([]) pl.margins(x=0) if data == data_s1: pl.savefig('Sobol_S1_results.png', bbox_inches="tight", dpi=300) elif data == data_st: pl.savefig('Sobol_ST_results.png', bbox_inches="tight", dpi=300) pl.clf() for i in range(n_rows): row_1 = data_s1[i] row_T = data_st[i] for n in range(len(row_1)): if row_T[n] > row_1[n]: # possible higher order interactions interactions = list( np.nonzero(np.nan_to_num(data_s2[i][n]) > 0.05)[0]) if interactions: print(rows[i], columns[n], row_T[n], row_1[n], list(np.array(columns)[interactions]), list(np.array(data_s2[i][n])[interactions]))
import pandas as pd import numpy as np parser = argparse.ArgumentParser() parser.add_argument('--label', type=str) parser.add_argument('--num', type=str) args = parser.parse_args() temp = np.load('temp.npy', allow_pickle=True) a, b, c, d = temp a, b, c = np.array(a, dtype=np.float), np.array(b, dtype=np.float), np.array( c, dtype=np.float) plt.subplot(2, 1, 1) plt.fill_between(np.arange(400), a, b, color='r') plt.fill_between(np.arange(400), c, b, color='b') plt.axis('off') plt.subplot(2, 1, 2) plt.bar(np.arange(400), d, color='g') plt.axis('off') plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0) plt.margins(0, 0) plt.axis('off') plt.savefig('img\{}_{}.jpg'.format(args.num, args.label))