def plot_from_results_str(results_file): results = pd.read_csv(results_file) print("all types") results['Clock Rate'] = nan systems = ["aetherling_copies", "halide_to_hardware", "spatial"] systb = {"ae": 0, "h2h": 1, "sp": 2} big_applications = ["big_conv2d", "big_conv2d_b2b", "big_sharpen"] big_16_applications = [ "big_16_conv2d", "big_16_conv2d_b2b", "big_16_sharpen" ] big_32_applications = [ "big_32_conv2d", "big_32_conv2d_b2b", "big_32_sharpen" ] big_real_applications = [ "big_real_conv2d", "big_real_conv2d_b2b", "big_real_sharpen" ] big_real_16_applications = [ "big_real_16_conv2d", "big_real_16_conv2d_b2b", "big_real_16_sharpen" ] big_real_32_applications = [ "big_real_32_conv2d", "big_real_32_conv2d_b2b", "big_real_32_sharpen" ] other_big_apps = ["camera"] real_applications = ["conv2d_real", "conv2d_real_b2b", "sharpen_real"] all_big_apps = real_applications + \ big_applications + big_16_applications + big_32_applications + \ big_real_applications + big_real_16_applications + big_real_32_applications + other_big_apps #applications = ["map", "conv2d", "conv2d_b2b", "conv2d_b2b_3x3_repeat", "pyramid", "sharpen", "camera"] + all_big_apps applications = ["map", "conv2d", "conv2d_b2b", "sharpen"] + all_big_apps applications_paper_just_ae = ["map"] + big_real_32_applications big_apptb = {name: idx + 4 for (idx, name) in enumerate(all_big_apps)} apptb = {"map": 0, "conv2d": 1, "conv2d_b2b": 2, "sharpen": 3} apptb.update(big_apptb) application_lengths = [200, 16, 16, 16] + ([1920 * 1080] * 22) per_system_per_application_results = [] for i, system in enumerate(systems): per_system_results = [] for j, app in enumerate(applications): start_per_app_per_system = results[(results.System == system) & (results.Application == app)] paper_parallelism = fix_parallelism(start_per_app_per_system, application_lengths[j]) #filled_in = add_missing_parallelisms(paper_parallelism, system, app, application_parallelisms[j] if i == 0 else application_parallelisms_others[j]) sorted_by_parallelism = paper_parallelism.sort_values( "Parallelism") results_only_selected_columns = get_output_columns( sorted_by_parallelism) per_system_results.append(results_only_selected_columns) per_system_per_application_results.append(per_system_results) #fig, ((ax1_0, ax1_1, _), (ax1_2, ax1_3, _), (ax1_4, ax1_5, _)) = plt.subplots(nrows=17, ncols=3) res = per_system_per_application_results rcParams['pdf.fonttype'] = 42 rcParams['ps.fonttype'] = 42 rcParams['font.family'] = 'sans-serif' rcParams['font.sans-serif'] = ['Arial'] if False: fig, axes = plt.subplots(nrows=23, ncols=3, figsize=(24, 130)) (ax1_0, ax1_1, _) = axes[0] (ax1_2, ax1_3, _) = axes[1] big_axes = axes[2:] for axis in [ax1_0, ax1_1, ax1_2, ax1_3]: axis.spines['right'].set_visible(False) axis.spines['top'].set_visible(False) #plt.subplots_adjust(hspace=0.5, top=1.6) plt.rc('text', usetex=True) fntsize = 28 plt.rcParams.update({'font.size': fntsize}) #fig.set_figwidth(24) #fig.set_figheight(130) x_label = "Throughput (Input Px / Clk)" y_label = "Area (Slices)" # map map_title = "MAP" ax1_0.set_title(map_title) ax1_0.set_yscale('log') #ax1_0.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) ax1_0.yaxis.set_major_formatter(ticker.ScalarFormatter()) ax1_1.tick_params(axis='both', which='both', labelsize=fntsize) ax1_0.set_xscale('log') ax1_0.xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) ax1_0.set_xticks([1, 2, 5, 10, 20, 40, 200]) res[systb['ae']][apptb['map']].plot(kind='line', y="Slices", x="Parallelism", legend=False, ax=ax1_0, label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw) print("plotting map ae") print(res[systb['ae']][apptb['map']]) ax1_0.set_ylabel(y_label, fontsize=fntsize) ax1_0.set_xlabel("", fontsize=fntsize) #conv2d conv2d_title = 'CONV' ax1_1.set_title(conv2d_title) ax1_1.set_yscale('log') #ax1_1.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) ax1_1.yaxis.set_major_formatter(ticker.ScalarFormatter()) #ax1_1.yaxis.set_minor_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) ax1_1.minorticks_off() #ax1_1.tick_params(axis='both', which='both', labelsize=fntsize) ax1_1.tick_params(axis='y', which='both', pad=20) ax1_1.set_yticks([50, 100, 300]) ax1_1.set_yticks([], minor=True) ax1_1.set_ylim(bottom=40, top=350) ax1_1.set_xscale('log') ax1_1.xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) ax1_1.set_xticks([1 / 9, 1 / 3, 1, 2, 4, 8, 16]) #ax1_1.set_xticks([1/9,1/3]) #ax1_1.set_xticklabels([r'$\frac{1}{3}$']) ax1_1.set_xticklabels([ r'$\frac{1}{9}$', r'$\frac{1}{3}$', r'$1$', r'$2$', r'$4$', r'$8$', r'$16$' ]) res[systb['ae']][apptb['conv2d']].plot(kind='line', y="Slices", x="Parallelism", legend=False, ax=ax1_1, label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw) print("plotting conv2d ae") print(res[systb['ae']][apptb['conv2d']]) ax1_1.set_xlabel("", fontsize=fntsize) #conv2d_b2b conv2d_b2b_title = 'CONVB2B' ax1_2.set_title(conv2d_b2b_title) ax1_2.set_yscale('log') ax1_2.yaxis.set_major_formatter( ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) #ax1_2.yaxis.set_minor_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) ax1_2.minorticks_off() ax1_2.set_yticks([50, 100, 500]) ax1_2.set_ylim(bottom=50, top=600) ax1_2.tick_params(axis='y', which='both', pad=20) ax1_2.set_xscale('log') ax1_2.xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) ax1_2.set_xticks([1 / 9, 1 / 3, 1, 2, 4, 8, 16]) #ax1_2.set_xticks([1/9,1/3]) #ax1_2.set_xticklabels([r'$\frac{1}{3}$']) ax1_2.set_xticklabels([ r'$\frac{1}{9}$', r'$\frac{1}{3}$', r'$1$', r'$2$', r'$4$', r'$8$', r'$16$' ]) res[systb['ae']][apptb['conv2d_b2b']].plot(kind='line', y="Slices", x="Parallelism", legend=False, ax=ax1_2, label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw, clip_on=False) print("plotting conv2d_b2b ae") print(res[systb['ae']][apptb['conv2d_b2b']]) ax1_2.set_ylabel(y_label, fontsize=fntsize) ax1_2.set_xlabel(x_label, fontsize=fntsize) #sharpen sharpen_title = "SHARPEN" ax1_3.set_title(sharpen_title) ax1_3.set_yscale('log') #ax1_3.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) #ax1_3.yaxis.set_minor_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) #ax1_3.majorticks_off() ax1_3.minorticks_off() ax1_3.tick_params(axis='both', which='major', labelsize=fntsize) ax1_3.set_yticks([]) ax1_3.set_ylim(bottom=50, top=600) ax1_3.set_xscale('log') ax1_3.xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) ax1_3.set_xticks([1 / 9, 1 / 3, 1, 2, 4, 8, 16]) #ax1_3.set_xticks([1/9,1/3]) #ax1_3.set_xticklabels([r'$\frac{1}{3}$']) ax1_3.set_xticklabels([ r'$\frac{1}{9}$', r'$\frac{1}{3}$', r'$1$', r'$2$', r'$4$', r'$8$', r'$16$' ]) res[systb['ae']][apptb['sharpen']].plot(kind='line', y="Slices", x="Parallelism", legend=False, ax=ax1_3, label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw, clip_on=False) print("plotting sharpen ae") print(res[systb['ae']][apptb['sharpen']]) ax1_3.set_xlabel(x_label, fontsize=fntsize) axes_slices = [x[0] for x in big_axes] axes_brams = [x[1] for x in big_axes] axes_dsps = [x[2] for x in big_axes] app_name = all_big_apps y_bottom_slices = [40] * len(axes_slices) y_top_slices = [3000] * len(axes_slices) y_ticks_slices = [[50, 100, 1000]] * len(axes_slices) y_ticks_brams = [2, 8, 32] * len(axes_slices) for index in range(len(app_name)): #ax1_4.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) axes_slices[index].set_title(app_name[index].replace("_", " ") + " Slices") axes_slices[index].set_yscale('log') axes_slices[index].minorticks_off() axes_slices[index].set_ylim(bottom=y_bottom_slices[index], top=y_top_slices[index]) axes_slices[index].set_yticks(y_ticks_slices[index]) axes_slices[index].tick_params(axis='both', which='major', labelsize=fntsize) #axes_slices[index].set_ylim(bottom=50, top=600) axes_slices[index].set_xscale('log') axes_slices[index].xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) axes_slices[index].set_xticks([1 / 3, 1, 2, 4, 8, 16]) axes_slices[index].set_xticklabels( [r'$1/3$', r'$1$', r'$2$', r'$4$', r'$8$', r'$16$']) res[systb['ae']][apptb[app_name[index]]].plot( kind='line', y="Slices", x="Parallelism", legend=False, ax=axes_slices[index], label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw) if index == 0: axes_slices[index].set_ylabel(y_label, fontsize=fntsize) #axes_slices[index].set_xlabel(x_label, fontsize=fntsize); axes_brams[index].set_title(app_name[index].replace("_", " ") + " BRAMs") axes_brams[index].set_yscale('log') axes_brams[index].minorticks_off() axes_brams[index].yaxis.set_major_formatter( ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) axes_brams[index].tick_params(axis='both', which='major', labelsize=fntsize) axes_brams[index].set_yticks(y_ticks_brams) #axes_brams[index].set_ylim(bottom=50, top=600) axes_brams[index].set_xscale('log') axes_brams[index].xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) axes_brams[index].set_xticks([1 / 3, 1, 2, 4, 8, 16]) axes_brams[index].set_xticklabels( [r'$\frac{1}{3}$', r'$1$', r'$2$', r'$4$', r'$8$', r'$16$']) res[systb['ae']][apptb[app_name[index]]].plot( kind='line', y="BRAMs", x="Parallelism", legend=False, ax=axes_brams[index], label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw) #print(res[systb['ae']][apptb[app_name[index]]]) if index == 0: axes_brams[index].set_ylabel(y_label, fontsize=fntsize) #axes_brams[index].set_xlabel(x_label, fontsize=fntsize); axes_dsps[index].set_title(app_name[index].replace("_", " ") + " DSPs") axes_dsps[index].set_yscale('log') axes_dsps[index].minorticks_off() axes_dsps[index].yaxis.set_major_formatter( ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) axes_dsps[index].tick_params(axis='both', which='major', labelsize=fntsize) axes_dsps[index].set_yticks(y_ticks_brams) #axes_dsps[index].set_ylim(bottom=50, top=600) axes_dsps[index].set_xscale('log') axes_dsps[index].xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) axes_dsps[index].set_xticks([1 / 3, 1, 2, 4, 8, 16]) axes_dsps[index].set_xticklabels( [r'$1/3$', r'$1$', r'$2$', r'$4$', r'$8$', r'$16$']) res[systb['ae']][apptb[app_name[index]]].plot( kind='line', y="DSPs", x="Parallelism", legend=False, ax=axes_dsps[index], label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw) print("plotting " + app_name[index]) print(res[systb['ae']][apptb[app_name[index]]]) if index == 0: axes_dsps[index].set_ylabel(y_label, fontsize=fntsize) #axes_dsps[index].set_xlabel(x_label, fontsize=fntsize); plt.tight_layout() plt.savefig(os.path.join(figs_dir, 'ae_results_big.pdf')) figs_dir = os.path.join(os.path.dirname(results_file), 'figs') fig_paper_ae, axes_paper_ae = plt.subplots(nrows=3, ncols=5, figsize=(34.28, 18)) fig_paper_ae.set_dpi(1000) #orig_width = 24.0 #orig_height = 13.0 #new_width = 3.333 #new_height = orig_height * new_width / orig_width width_scaling_new_app = 6 / 4.0 #fig_paper_ae.set_size_inches(28, 25) #plt.subplots_adjust(wspace=0.4, top=0.97) axes_slices = axes_paper_ae[0] axes_brams = axes_paper_ae[1] axes_dsps = axes_paper_ae[2] app_name = ["map"] + big_real_32_applications + other_big_apps paper_titles = ["MAP", "CONV", "CONVB2B", "SHARPEN", "CAMERA"] y_bottom_slices = [1] + ([100] * (len(axes_slices) - 1)) y_top_slices = [1400] + ([14000] * (len(axes_slices) - 1)) y_ticks_slices = [[100, 1000, 10000]] * len(axes_slices) y_bottom_brams = [0] + ([3] * (len(axes_slices) - 1)) y_top_brams = [2.2] + ([40] * (len(axes_slices) - 1)) y_ticks_brams = [[0, 1, 2]] + ([[4, 8, 32]] * (len(axes_slices) - 1)) y_bottom_dsps = [0] + ([10] * (len(axes_slices) - 1)) y_top_dsps = [2.2] + ([710] * (len(axes_slices) - 1)) y_ticks_dsps = [[0, 1, 2]] + ([[10, 100, 500]] * (len(axes_slices) - 1)) titlesize = 50 fntsize = 40 ticksize = 30 ms = 18 lw = 7 tick_padding = 8 for index in range(len(app_name)): print("plotting " + app_name[index]) print(res[systb['ae']][apptb[app_name[index]]]) #ax1_4.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) #axes_slices[index].set_title(paper_titles[index]) axes_slices[index].set_yscale('log') axes_slices[index].minorticks_off() axes_slices[index].set_ylim(bottom=y_bottom_slices[index], top=y_top_slices[index]) axes_slices[index].tick_params(axis='both', which='major', pad=tick_padding) #axes_slices[index].set_ylim(bottom=50, top=600) axes_slices[index].set_xscale('log') axes_slices[index].xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) axes_slices[index].yaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) axes_slices[index].minorticks_off() axes_slices[index].spines['right'].set_visible(False) axes_slices[index].spines['top'].set_visible(False) res[systb['ae']][apptb[app_name[index]]].plot(kind='line', y="Slices", x="Parallelism", legend=False, ax=axes_slices[index], label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw, clip_on=False) axes_slices[index].set_xlabel("", fontsize=fntsize) if index == 0: axes_slices[index].set_xticks([1, 2, 5, 10, 40, 200]) axes_slices[index].set_xticklabels( ['1', '2', '5', '10', '40', '200']) elif index == 4: axes_slices[index].set_xticks([1 / 4, 1, 2, 4, 8, 16]) axes_slices[index].set_xticklabels( ['1/4', '1', '2', '4', '8', '16']) else: axes_slices[index].set_xticks([1 / 3, 1, 2, 4, 8, 16]) axes_slices[index].set_xticklabels( ['1/3', '1', '2', '4', '8', '16']) if index == 0: axes_slices[index].set_ylabel("Slices", fontsize=fntsize) axes_slices[index].set_yticks([1, 10, 1000]) else: axes_slices[index].set_yticks(y_ticks_slices[index]) axes_slices[index].grid(which='major', axis='y', linestyle='--') axes_slices[index].tick_params(axis="both", labelsize=ticksize) axes_slices[index].set_title(paper_titles[index], fontsize=fntsize) #axes_slices[index].set_xlabel(x_label, fontsize=fntsize); #axes_brams[index].set_title(app_name[index].replace("_", " ") + " BRAMs") if index > 0: axes_brams[index].set_yscale('log') axes_brams[index].minorticks_off() axes_brams[index].yaxis.set_major_formatter( ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) axes_brams[index].tick_params(axis='both', which='major', pad=tick_padding) axes_brams[index].set_ylim(bottom=y_bottom_brams[index], top=y_top_brams[index]) axes_brams[index].set_xscale('log') axes_brams[index].xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) axes_brams[index].minorticks_off() axes_brams[index].spines['right'].set_visible(False) axes_brams[index].spines['top'].set_visible(False) res[systb['ae']][apptb[app_name[index]]].plot(kind='line', y="BRAMs", x="Parallelism", legend=False, ax=axes_brams[index], label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw, clip_on=False) axes_brams[index].set_xlabel("", fontsize=fntsize) if index == 0: axes_brams[index].set_xticks([1, 2, 5, 10, 40, 200]) axes_brams[index].set_xticklabels( ['1', '2', '5', '10', '40', '200']) elif index == 4: axes_brams[index].set_xticks([1 / 4, 1, 2, 4, 8, 16]) axes_brams[index].set_xticklabels( ['1/4', '1', '2', '4', '8', '16']) else: axes_brams[index].set_xticks([1 / 3, 1, 2, 4, 8, 16]) axes_brams[index].set_xticklabels( ['1/3', '1', '2', '4', '8', '16']) #print(res[systb['ae']][apptb[app_name[index]]]) if index == 0: axes_brams[index].set_ylabel("BRAMs", fontsize=fntsize) axes_brams[index].set_yticks(y_ticks_brams[index]) else: axes_brams[index].set_yticks(y_ticks_brams[index]) axes_brams[index].grid(which='major', axis='y', linestyle='--') axes_brams[index].tick_params(axis="both", labelsize=ticksize) #axes_brams[index].set_xlabel(x_label, fontsize=fntsize); #axes_dsps[index].set_title(app_name[index].replace("_", " ") + " DSPs") if index > 0: axes_dsps[index].set_yscale('log') axes_dsps[index].minorticks_off() axes_dsps[index].yaxis.set_major_formatter( ticker.FuncFormatter(lambda y, _: '{:g}'.format(y))) axes_dsps[index].tick_params(axis='both', which='major', pad=tick_padding) axes_dsps[index].set_ylim(bottom=y_bottom_dsps[index], top=y_top_dsps[index]) axes_dsps[index].set_xscale('log') axes_dsps[index].xaxis.set_major_formatter( ticker.FuncFormatter(lambda x, _: '{:g}'.format(x))) axes_dsps[index].minorticks_off() axes_dsps[index].spines['right'].set_visible(False) axes_dsps[index].spines['top'].set_visible(False) res[systb['ae']][apptb[app_name[index]]].plot(kind='line', y="DSPs", x="Parallelism", legend=False, ax=axes_dsps[index], label="Scheduler Result", color=["#59A14F"], linestyle='-', marker='o', fontsize=fntsize, markersize=ms, linewidth=lw, clip_on=False) if index == 0: axes_dsps[index].set_xticks([1, 2, 5, 10, 40, 200]) axes_dsps[index].set_xticklabels( ['1', '2', '5', '10', '40', '200']) elif index == 4: axes_dsps[index].set_xticks([1 / 4, 1, 2, 4, 8, 16]) axes_dsps[index].set_xticklabels(['1/4', '1', '2', '4', '8', '16']) else: axes_dsps[index].set_xticks([1 / 3, 1, 2, 4, 8, 16]) axes_dsps[index].set_xticklabels(['1/3', '1', '2', '4', '8', '16']) if index == 2: axes_dsps[index].set_xlabel("Throughput (px/clk)", fontsize=fntsize) else: axes_dsps[index].set_xlabel("", fontsize=fntsize) if index == 0: axes_dsps[index].set_ylabel("DSPs", fontsize=fntsize) axes_dsps[index].set_yticks(y_ticks_dsps[index]) else: axes_dsps[index].set_yticks(y_ticks_dsps[index]) axes_dsps[index].grid(which='major', axis='y', linestyle='--') axes_dsps[index].tick_params(axis="both", labelsize=ticksize) axes_dsps[index].xaxis.labelpad = 20 fig_paper_ae.align_ylabels() fig_paper_ae.suptitle("Resources Consumed by Aetherling Designs", fontsize=titlesize) #plt.subplots_adjust(right = 1, left=0, top=1, bottom = 0, wspace=0.33) plt.subplots_adjust(wspace=0.33) #plt.tight_layout(rect=[0,0,1,0.92]) plt.margins(0, 0) plt.savefig(os.path.join(figs_dir, 'ae_results.pdf'), transparent=True) sp_maxes = [] sp_mins = [] hth_maxes = [] hth_mins = [] joined_sp_res_list = [] joined_hth_res_list = [] joined_sp_ratios_list = [] joined_hth_slices_ratios_list = [] joined_hth_brams_ratios_list = [] for app in apptb: #print("App " + str(app)) (joined_sp_res, joined_hth_res, joined_sp_ratios, joined_hth_slices_ratios, joined_hth_brams_ratios) = \ comp_ae_and_others(res[systb['ae']][apptb[app]], res[systb['h2h']][apptb[app]], res[systb['sp']][apptb[app]]) joined_sp_res_list.append(joined_sp_res) joined_hth_res_list.append(joined_hth_res) joined_sp_ratios_list.append(joined_sp_ratios) joined_hth_slices_ratios_list.append(joined_hth_slices_ratios) joined_hth_brams_ratios_list.append(joined_hth_brams_ratios) #print("Max SP" + str(joined_sp_ratios.iloc[:,1].max())) #print("Min SP" + str(joined_sp_ratios.iloc[:,1].min())) #print("Max HTH" + str(joined_hth_ratios.iloc[:,1].max())) #print("Min HTH" + str(joined_hth_ratios.iloc[:,1].min())) sp_maxes.append(joined_sp_ratios.iloc[:, 1].max()) sp_mins.append(joined_sp_ratios.iloc[:, 1].min()) hth_maxes.append(joined_hth_slices_ratios.iloc[:, 1].max()) hth_mins.append(joined_hth_brams_ratios.iloc[:, 1].min()) #print("AE_SP_Slices_Ratio max: " + str(max(sp_maxes))) #print("AE_SP_Slices_Ratio min: " + str(min(sp_mins))) #print("hth_maxes: " + str(hth_maxes)) #print("hth_mins: " + str(hth_mins)) fig, ((ax2_0_0, ax2_0_1, ax2_0_2, ax2_0_3), (ax2_1_0, ax2_1_1, ax2_1_2, ax2_1_3), (ax2_2_0, ax2_2_1, ax2_2_2, ax2_2_3)) = plt.subplots(nrows=3, ncols=4) #plt.rc('text', usetex=True) #plt.rcParams.update({'font.size': fntsize}) fig.set_figwidth(24) fig.set_figheight(16) def plot_bar_comp(axis_slices, axis_brams, axis_dsps, title, appname, leftmost=False, rightmost=False): joined_sp_ratios_list[apptb[appname]].fillna(0) axis_slices.set_yticks([0, 2, 4, 6, 8]) axis_brams.set_yticks([0, 1, 2, 3, 4]) axis_dsps.set_yticks([0., 0.2, 0.4, 0.6, 0.8, 1.0]) if not leftmost: axis_slices.set_yticklabels([''] * 5) axis_slices.tick_params(axis='y', which='both', length=0) axis_brams.set_yticklabels([''] * 5) axis_brams.tick_params(axis='y', which='both', length=0) axis_dsps.set_yticklabels([''] * 6) axis_dsps.tick_params(axis='y', which='both', length=0) joined_sp_ratios_list[apptb[appname]].plot(kind='bar', y="AE_SP_Slices_Ratio", x="Parallelism", rot=0, ax=axis_slices, legend=False, color=["#59A14F"], width=0.8, fontsize=fntsize, zorder=3) axis_slices.axhline(y=1, color='r', zorder=0) joined_sp_ratios_list[apptb[appname]].plot(kind='bar', y="AE_SP_BRAMs_Ratio", x="Parallelism", rot=0, ax=axis_brams, legend=False, color=["#59A14F"], width=0.8, fontsize=fntsize, zorder=3) axis_brams.axhline(y=1, color='r', zorder=0) joined_sp_ratios_list[apptb[appname]].plot(kind='bar', y="AE_SP_DSPs_Ratio", x="Parallelism", rot=0, ax=axis_dsps, legend=False, color=["#59A14F"], width=0.8, fontsize=fntsize, zorder=3) axis_dsps.axhline(y=1, color='r', zorder=0) axis_slices.set_xticklabels(['1', '2', '4', '8']) axis_brams.set_xticklabels(['1', '2', '4', '8']) axis_dsps.set_xticklabels(['1', '2', '4', '8']) axis_slices.tick_params(axis='both', which='major', pad=tick_padding) axis_brams.tick_params(axis='both', which='major', pad=tick_padding) axis_dsps.tick_params(axis='both', which='major', pad=tick_padding) axis_slices.set_ylim(bottom=0, top=8.5) axis_brams.set_ylim(bottom=0, top=4.5) axis_dsps.set_ylim(bottom=0, top=1.1) print("plotting " + str(appname) + " Aetherling vs Spatial") print(joined_sp_ratios_list[apptb[appname]]) axis_slices.spines['right'].set_visible(False) axis_brams.spines['right'].set_visible(False) axis_dsps.spines['right'].set_visible(False) axis_slices.spines['right'].set_linewidth(3) axis_brams.spines['right'].set_linewidth(3) axis_dsps.spines['right'].set_linewidth(3) axis_slices.spines['left'].set_visible(leftmost == True) axis_brams.spines['left'].set_visible(leftmost == True) axis_dsps.spines['left'].set_visible(leftmost == True) axis_slices.spines['top'].set_visible(False) axis_brams.spines['top'].set_visible(False) axis_dsps.spines['top'].set_visible(False) axis_slices.grid(which='major', axis='y', linestyle='--', zorder=0, alpha=0.5) axis_brams.grid(which='major', axis='y', linestyle='--', zorder=0, alpha=0.5) axis_dsps.grid(which='major', axis='y', linestyle='--', zorder=0, alpha=0.5) axis_slices.tick_params(axis="both", labelsize=ticksize) axis_brams.tick_params(axis="both", labelsize=ticksize) axis_dsps.tick_params(axis="both", labelsize=ticksize) if leftmost == True: axis_slices.set_ylabel("Relative Slices", fontsize=fntsize) axis_brams.set_ylabel("Relative BRAMs", fontsize=fntsize) axis_dsps.set_ylabel("Relative DSPs", fontsize=fntsize) axis_slices.set_xlabel("", fontsize=fntsize) axis_brams.set_xlabel("", fontsize=fntsize) axis_dsps.set_xlabel(title.replace("_", " "), fontsize=fntsize) cur_xlim = axis_slices.get_xlim() xlim_left = cur_xlim[0] if leftmost else cur_xlim[0] - 0.5 xlim_right = cur_xlim[1] if rightmost else cur_xlim[1] + 0.5 axis_slices.set_xlim([xlim_left, xlim_right]) axis_brams.set_xlim([xlim_left, xlim_right]) axis_dsps.set_xlim([xlim_left, xlim_right]) plot_bar_comp(ax2_0_0, ax2_1_0, ax2_2_0, 'MAP', 'map', leftmost=True) plot_bar_comp(ax2_0_1, ax2_1_1, ax2_2_1, 'CONV', "big_real_32_conv2d") plot_bar_comp(ax2_0_2, ax2_1_2, ax2_2_2, 'CONVB2B', "big_real_32_conv2d_b2b") plot_bar_comp(ax2_0_3, ax2_1_3, ax2_2_3, 'SHARPEN', "big_real_32_sharpen", rightmost=True) fig.align_ylabels() plt.tight_layout(rect=[0, 0.065, 1, 0.92]) #plt.tight_layout() plt.subplots_adjust(wspace=0, top=0.92) fig.suptitle( "Resources Consumed by Spatial Designs (Relative to Aetherling)", fontsize=titlesize) #plt.xlabel("Throughput (px/clk)", fontsize=fntsize) fig.text(0.5, 0.04, "Throughput (px/clk)", ha='center', fontsize=fntsize) plt.savefig(os.path.join(figs_dir, 'ae_versus_sp.pdf'), transparent=True) hth_p1_slices_values = [] hth_p1_brams_values = [] hth_p1_titles = [ "map", "big_32_conv2d", "big_32_conv2d_b2b", "big_32_sharpen" ] for title in hth_p1_titles: i = apptb[title] hth_p1_slices_values.append(joined_hth_slices_ratios_list[i].loc[ joined_hth_slices_ratios_list[i].loc[:, 'Parallelism'] == 1, :].iloc[0, 1]) hth_p1_brams_values.append(joined_hth_brams_ratios_list[i].loc[ joined_hth_brams_ratios_list[i].loc[:, 'Parallelism'] == 1, :].iloc[0, 1]) hth_p1_slices_df = pd.DataFrame.from_dict({ 'apps': ["MAP", "CONV", "CONVB2B", "SHARPEN"], 'values': hth_p1_slices_values }) print("Halide-HLS Single Chart Slices") print(hth_p1_slices_df) hth_p1_brams_df = pd.DataFrame.from_dict({ 'apps': ["MAP", "CONV", "CONVB2B", "SHARPEN"], 'values': hth_p1_brams_values }) print("Halide-HLS Single Chart BRAMs") print(hth_p1_brams_df) fig, ax3 = plt.subplots() #plt.subplots_adjust(top=0.99, bottom=0.19) #textwidth = 7 in # linewidth = 3.334 in scaling_factor = 3.334 / (7.0 * 0.7) fig.set_figwidth(24 * scaling_factor) fig.set_figheight(14.2 * scaling_factor) fig.suptitle( "Resources Consumed by Halide-HLS Designs \n (Relative to Aetherling)", fontsize=titlesize) #plt.rc('text', usetex=True) #plt.rcParams.update({'font.size': fntsize}) #ax3.set_title("Halide-HLS/Aetherling Ratio of Area (Slices)") ax3.spines['right'].set_visible(False) ax3.spines['top'].set_visible(False) ax3.set_ylabel("Relative Slices", fontsize=fntsize) ax3.set_yticks([1, 2, 3, 4]) hth_p1_slices_df.plot(kind='bar', y='values', x='apps', rot=0, ax=ax3, legend=False, color=["#59A14F"], fontsize=fntsize, zorder=3) ax3.tick_params(axis='x', which='major', pad=2 * tick_padding) ax3.tick_params(axis='y', which='major', pad=tick_padding) ax3.set_xlabel("", fontsize=fntsize) #ax3_1.spines['right'].set_visible(False) #ax3_1.spines['top'].set_visible(False) #hth_p1_brams_df.plot(kind='bar', y='values', x='apps', rot=10, # ax=ax3_1, legend=False, color=["#59A14F"], # fontsize=fntsize) ax3.grid(which='major', axis='y', linestyle='--', zorder=0, alpha=0.5) ax3.axhline(y=1, color='r', zorder=1) plt.tight_layout(rect=[0, 0, 1, 0.86]) plt.savefig(os.path.join(figs_dir, 'ae_versus_hth.pdf'), transparent=True) apps_to_print_sp = [ 'map', "big_real_32_conv2d", "big_real_32_conv2d_b2b", 'big_real_32_sharpen' ] apps_to_print_hth = [ 'map', "big_32_conv2d", "big_32_conv2d_b2b", 'big_32_sharpen' ] for appname in apps_to_print_sp: print("printing all comp res for " + str(appname)) print(joined_sp_res_list[apptb[appname]]) for appname in apps_to_print_hth: print("printing all comp res for " + str(appname)) print(joined_hth_res_list[apptb[appname]])
def plot_clusters_evo(evoked_dict, cinfo, ch_type, i_clu=0, analysis_name='', filter_smooth=False, legend=False, blackfig=False, tmin=None, tmax=None): units = dict(eeg='uV', grad='fT/cm', mag='fT') if legend: fig, ax = plt.subplots(1, 1, figsize=(5, 5)) else: # fig, ax = plt.subplots(1, 1, figsize=(5.5, 3)) fig, ax = plt.subplots(1, 1, figsize=(10 * 0.5, 7 * 0.5)) if blackfig: textcolor = 'white' linecolor = 'white' ax.set_facecolor((.2, .2, .2)) fig.patch.set_facecolor((.2, .2, .2)) else: textcolor = 'black' linecolor = 'black' plt.axvline(0, linestyle='-', color=linecolor, linewidth=2) for xx in range(3): plt.axvline(250 * xx, linestyle='--', color=linecolor, linewidth=0.5) ax.set_xlabel('Time (ms)', color=textcolor, fontsize=14) condnames = list(evoked_dict.keys()) if len(condnames) == 2: colorslist = ['r', 'b'] else: NUM_COLORS = len(condnames) cm = plt.get_cmap('viridis') colorslist = ([ cm(1. * i / (NUM_COLORS - 1)) for i in range(NUM_COLORS) ]) # OR USE PREDEFINED COLORS: if NUM_COLORS == 7: print('7 levels: using "seqcolors"') colorslist = config.seqcolors condnames_lgd = condnames.copy() if condnames[-1][-14:] == 'SequenceID_07-' and len(condnames) == 7: # Use sequences names in order as labels for seqID in range(1, 8): seqname, _, _ = epoching_funcs.get_seqInfo(seqID) condnames_lgd[seqID - 1] = seqname for ncond, condname in enumerate(condnames): data = evoked_dict[condname].copy() evoked_funcs.plot_evoked_with_sem_1cond(data, condnames_lgd[ncond], ch_type, cinfo['channels_cluster'], color=colorslist[ncond], filter=filter_smooth, axis=None) ymin, ymax = ax.get_ylim() ax.fill_betweenx((ymin, ymax), cinfo['sig_times'][0], cinfo['sig_times'][-1], color='grey', alpha=.08, linewidth=0.0) ax.set_ylim([ymin, ymax]) if legend: # plt.legend(loc='best', fontsize=6) l = plt.legend(fontsize=9, bbox_to_anchor=(0., 1.25, 1., .08), loc=2, ncol=3, mode="expand", borderaxespad=.8, frameon=False) for text in l.get_texts(): text.set_color(textcolor) plt.title(ch_type + '_' + analysis_name + '_clust_' + str(i_clu + 1), fontsize=10, weight='bold', color=textcolor) # for key in ('top', 'right'): # Remove spines # ax.spines[key].set(visible=False) # ax.spines['bottom'].set_position('zero') fmt = ticker.ScalarFormatter(useMathText=True) fmt.set_powerlimits((0, 0)) ax.get_yaxis().set_major_formatter(fmt) # ax.get_yaxis().get_offset_text().set_position((-0.08, 0)) # move 'x10-x', does not work with y ax.set_xlim([tmin, tmax]) ax.set_ylabel(units[ch_type], color=textcolor, fontsize=14) ax.spines['bottom'].set_color(linecolor) ax.spines['left'].set_color(linecolor) ax.tick_params(axis='x', colors=textcolor) ax.tick_params(axis='y', colors=textcolor) # fig.tight_layout(pad=0.5, w_pad=0) return fig
value_after_decimal = new_tick_format[index_of_decimal + 1] if value_after_decimal == "0": # remove the 0 after the decimal point since it's not needed new_tick_format = new_tick_format[ 0:index_of_decimal] + new_tick_format[index_of_decimal + 2:] return new_tick_format # Step 5: Configures axes ## A) Format what shows up on axes and how it's displayed date_form = DateFormatter("%d-%m-%Y") ax.xaxis.set_major_locator(WeekdayLocator(byweekday=(0), interval=1)) ax.xaxis.set_major_formatter(date_form) plt.xticks(rotation=45) ax.yaxis.set_major_formatter(mtick.ScalarFormatter()) ax.yaxis.get_major_formatter().set_scientific(False) ax.yaxis.get_major_formatter().set_useOffset(False) ax.yaxis.set_major_formatter(mtick.FuncFormatter(reformat_large_tick_values)) ## B) Customizing axes and adding a grid ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_color('#3f3f3f') ax.spines['left'].set_color('#3f3f3f') ax.tick_params(colors='#3f3f3f') ax.grid(alpha=1) ## C) Adding a title and axis labels plt.ylabel('Total de vacinações por milhão de habitante', fontsize=18,
plt.plot(x, y3, linestyle=line_style[1], marker=dot_style_dark[1], \ markersize=marker_size, linewidth=linewidth, label=labels[2], \ color=colors[2], markerfacecolor='none') plt.plot(x, y4, linestyle=line_style[3], marker=dot_style[1], \ markersize=marker_size1, linewidth=linewidth1, label=labels[3], \ color=colors[1]) plt.xlabel("#vCPUs", fontdict=font) plt.ylabel("Ops/s", fontdict=font) # plt.yscale('log') # plt.tick_params(axis='both', which='major', labelsize=font_size) plt.grid(False) # plt.ylim([0.05, 10000]) plt.legend(loc=0, prop={'size': 6}, frameon=False) plt.xticks(x, size=8) plt.yticks(size=8) plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc="lower center", prop={'size': 8}, ncol=2) f = mticker.ScalarFormatter(useOffset=False, useMathText=True) g = lambda x, pos: "${}$".format(f._formatSciNotation('%1.10e' % x)) plt.gca().yaxis.set_major_formatter(mticker.FuncFormatter(g)) plt.tight_layout() plt.savefig('sysbench-mutex.pdf', dpi=300) plt.show()
def __init__(self, useMathText=True): self._fmt = mticker.ScalarFormatter(useMathText=useMathText, useOffset=False) self._fmt.create_dummy_axis()
def test_use_offset(): for use_offset in [True, False]: with matplotlib.rc_context({'axes.formatter.useoffset': use_offset}): tmp_form = mticker.ScalarFormatter() assert use_offset == tmp_form.get_useOffset()
'names': names, 'formats': formats }) imax = min(len(theory_lensed), len(theory_inflation)) theory_lensed = theory_lensed[2:imax] theory_inflation = theory_inflation[2:imax] plt.plot(theory_lensed['l'], theory_lensed['BB'] + theory_inflation['BB'], 'k') plt.plot(theory_lensed['l'], theory_inflation['BB'], 'k--') plt.plot(theory_lensed['l'], theory_lensed['BB'] + 0.1 * theory_inflation['BB'], 'k') plt.plot(theory_lensed['l'], 0.1 * theory_inflation['BB'], 'k--') plt.plot(theory_lensed['l'], theory_lensed['BB'], 'k:') plt.text(3, 1.5 * (theory_lensed['BB'][0] + theory_inflation['BB'][0]), r'$r=0.1$', size=texsize) plt.text(3, 1.5 * (theory_lensed['BB'][0] + 0.1 * theory_inflation['BB'][0]), r'$r=0.01$', size=texsize) plt.legend(loc='best', fontsize=legsize) ax = plt.gca() ax.xaxis.set_major_formatter(ticker.ScalarFormatter()) ax.tick_params(axis='y', which='minor', left='off') plt.savefig('BB_bounds.eps', bbox_inches='tight') plt.show()
def __init__(self, axes, crs, draw_labels=False, xlocator=None, ylocator=None, collection_kwargs=None): """ Object used by :meth:`cartopy.mpl.geoaxes.GeoAxes.gridlines` to add gridlines and tick labels to a map. Args: * axes The :class:`cartopy.mpl.geoaxes.GeoAxes` object to be drawn on. * crs The :class:`cartopy.crs.CRS` defining the coordinate system that the gridlines are drawn in. * draw_labels Toggle whether to draw labels. For finer control, attributes of :class:`Gridliner` may be modified individually. * xlocator A :class:`matplotlib.ticker.Locator` instance which will be used to determine the locations of the gridlines in the x-coordinate of the given CRS. Defaults to None, which implies automatic locating of the gridlines. * ylocator A :class:`matplotlib.ticker.Locator` instance which will be used to determine the locations of the gridlines in the y-coordinate of the given CRS. Defaults to None, which implies automatic locating of the gridlines. * collection_kwargs Dictionary controlling line properties, passed to :class:`matplotlib.collections.Collection`. """ self.axes = axes #: The :class:`~matplotlib.ticker.Locator` to use for the x #: gridlines and labels. self.xlocator = xlocator or degree_locator #: The :class:`~matplotlib.ticker.Locator` to use for the y #: gridlines and labels. self.ylocator = ylocator or degree_locator #: The :class:`~matplotlib.ticker.Formatter` to use for the x labels. self.xformatter = mticker.ScalarFormatter() self.xformatter.create_dummy_axis() #: The :class:`~matplotlib.ticker.Formatter` to use for the y labels. self.yformatter = mticker.ScalarFormatter() self.yformatter.create_dummy_axis() #: Whether to draw labels on the top of the map. self.xlabels_top = draw_labels #: Whether to draw labels on the bottom of the map. self.xlabels_bottom = draw_labels #: Whether to draw labels on the left hand side of the map. self.ylabels_left = draw_labels #: Whether to draw labels on the right hand side of the map. self.ylabels_right = draw_labels #: Whether to draw the x gridlines. self.xlines = True #: Whether to draw the y gridlines. self.ylines = True #: A dictionary passed through to ``ax.text`` on x label creation #: for styling of the text labels. self.xlabel_style = {} #: A dictionary passed through to ``ax.text`` on y label creation #: for styling of the text labels. self.ylabel_style = {} self.crs = crs # if the user specifies tick labels at this point, check if they can # be drawn. The same check will take place at draw time in case # public attributes are changed after instantiation. if draw_labels: self._assert_can_draw_ticks() #: The number of interpolation points which are used to draw the #: gridlines. self.n_steps = 30 #: A dictionary passed through to #: ``matplotlib.collections.LineCollection`` on grid line creation. self.collection_kwargs = collection_kwargs #: The x gridlines which were created at draw time. self.xline_artists = [] #: The y gridlines which were created at draw time. self.yline_artists = [] #: The x labels which were created at draw time. self.xlabel_artists = [] #: The y labels which were created at draw time. self.ylabel_artists = []
def customizeSubplot(self, mplSubplot): # Don't scale the Y axis mplSubplot.yaxis.set_major_formatter( ticker.ScalarFormatter(useOffset=False))
def test_use_offset(): for use_offset in [True, False]: with matplotlib.rc_context({'axes.formatter.useoffset': use_offset}): tmp_form = mticker.ScalarFormatter() nose.tools.assert_equal(use_offset, tmp_form.get_useOffset())
def plot(observations, show=False): """Plot histogram of the number of SNe with a given number of observations. Inputs: observations (DataFrame): all targets with GALEX observations """ print('\nPlotting histogram of observation frequency...') bands = ['FUV', 'NUV'] fig, axes = plt.subplots(2, 1, sharex=True, sharey=True, figsize=(3.25, 4), tight_layout=True) for ax, band in zip(axes, bands): df = observations[observations['band'] == band] all_epochs = df['total_epochs'] pre_post_epochs = get_pre_post_obs(df)['total_epochs'] bins = np.logspace(0, np.log10(np.max(all_epochs)), 11) color = COLORS[band] all_n = ax.hist(all_epochs, bins=bins, histtype='step', align='mid', lw=1.5, color=color, label='all (%s)' % all_epochs.shape[0])[0] ax.hist(pre_post_epochs, bins=bins, histtype='bar', align='mid', label='before+after (%s)' % pre_post_epochs.shape[0], rwidth=0.95, color=color) ax.set_title(band, x=0.05, y=0.9, va='top', ha='left', size=14) ax.set_xlabel('Total number of epochs') ax.set_ylabel('Number of SNe Ia') ax.set_xscale('log') ax.xaxis.set_major_formatter(tkr.ScalarFormatter()) ax.label_outer() # outside axis labels only ax.legend(handletextpad=0.5, handlelength=1.0, borderaxespad=1., borderpad=0.5) plt.tight_layout(pad=0.3) plt.savefig(Path('out/observation_hist.pdf'), dpi=300) if show: plt.show() else: plt.close() print('Done!')
def draw_pic(): for file_name in file_names: print(file_name) version = 0 sum0 = 0 sum1 = 0 y0 = [] y1 = [] err = [] x = [] init_c = 30 break_c = 30 best_c = 30 with open(out_path + file_name) as f: for line in f: if (line[0] == "+"): version = (version + 1) % 2 if ("iter" in line): tmp = line.strip().split() if (version == 0): sum0 += int(tmp[-1]) else: sum1 += int(tmp[-1]) if ("log2c=" in line): tmp = line.strip().split() if (init_c == 30): init_c = float(tmp[1]) y0.append(sum0) y1.append(sum1) MSE = (tmp[-1].split("="))[-1] err.append(MSE) x.append(tmp[1]) if ("break with" in line): tmp = line.strip().split() break_c = int(tmp[-1]) if ("log2 Best C" in line): tmp = line.strip().split() best_c = int(tmp[-1]) # set x axis size end_c = break_c if break_c >= best_c else best_c c_range = int(end_c - init_c + 5) #c_range = len(y0) y0_arr = np.array(y0) y1_arr = np.array(y1) err_arr = np.array(err) x_arr = np.array(x) print("y0 size %d", y0_arr.size) print("y1 size %d", y1_arr.size) print("x size %d", x_arr.size) # get the best record of data f_n = parce_name(file_name) if f_n[0] in best_result.keys(): best_rc = best_result[f_n[0]] else: print("quit") continue # draw picture fig, ax = plt.subplots() plt.title("[" + file_name + ", " + str(result_record[file_name][1]) + "]" + " " + "[" + best_rc[0] + ", " + str(best_rc[1]) + "]") ax.set_xlabel('Log2 p') ax.set_ylabel('Cumulative CG Iterator') line1 = ax.plot(x_arr, y0_arr, 'b--o', linewidth=2, label='Warm Start') line2 = ax.plot(x_arr, y1_arr, 'g--o', linewidth=2, label='No Warm Start') formatter = tic.ScalarFormatter() formatter.set_scientific(True) formatter.set_powerlimits((0, 0)) ax2 = ax.twinx() ax2.yaxis.set_major_formatter(formatter) ax2.set_ylabel("MSE") line3 = ax2.plot(x_arr, err_arr, 'y-x', linewidth=2, label='MSE') break_c_label = "Break Log2_P: " + str(break_c) best_c_label = "Best Log2_P: " + str(best_c) #v1= plt.axvline(x=break_c, linewidth=2, color="r", label=break_c_label) v2 = plt.axvline(best_c, linewidth=2, color="black", label=best_c_label) lns = line1 + line2 + line3 #lns.append(v1) lns.append(v2) labs = [l.get_label() for l in lns] plt.legend(lns, labs, loc="upper left") fig_name = file_name + ".eps" plt.savefig(pictures_path + fig_name, format="eps", dpi=1000) plt.close()
def plot_para_state(self): all_samples64 = np.loadtxt( os.getcwd() + f'/Gaussian/prediction/inversion_64/pCN_step0.08_0.05_10000_8000_all_samples.dat' ) all_samples16_64 = np.loadtxt( os.getcwd() + f'/Gaussian/prediction/inversion_16_64/pCN_step0.08_0.05_5000_3000_all_samples.dat' ) truth = np.log( np.loadtxt(os.getcwd() + f'/Gaussian/test_data/true_permeability_0.05.dat')) samples = np.full((10, 64, 64), 0.0) latent_sample64 = all_samples64[:, [0, 999, 2999, 4999, 9999]] latent_sample16_64 = all_samples16_64[:, [0, 499, 999, 2999, 4999]] for i in range(15): if i < 5: exp = 'inversion_64' model = self.prediction.model(exp) samples[i, :, :] = self.prediction.permeability( exp, model, latent_sample64[:, i]).cpu().numpy().reshape(64, 64) elif 4 < i < 10: exp = 'inversion_16_64' model = self.prediction.model(exp) zc = latent_sample16_64[:16, i - 5] zf = latent_sample16_64[16:, i - 5] samples[i, :, :] = self.prediction.permeability( exp, model, zc, zf=zf).cpu().numpy().reshape(64, 64) fig, _ = plt.subplots(2, 5, figsize=(15, 6)) vmin1 = np.amin(truth) vmax1 = np.amax(truth) for j, ax in enumerate(fig.axes): ax.set_aspect('equal') ax.set_axis_off() cax = ax.imshow(samples[j], cmap='jet', origin='upper', vmin=vmin1, vmax=vmax1) cbar = plt.colorbar( cax, ax=ax, fraction=0.046, pad=0.04, format=ticker.ScalarFormatter(useMathText=True)) if j == 0: ax.set_title('initial state', fontsize=12) if j == 1: ax.set_title('1000-th state', fontsize=12) if j == 2: ax.set_title('3000-th state', fontsize=12) if j == 3: ax.set_title('5000-th state', fontsize=12) if j == 4: ax.set_title('10000-th state', fontsize=12) if j == 5: ax.set_title('initial state', fontsize=12) if j == 6: ax.set_title('499-th state', fontsize=12) if j == 7: ax.set_title('1000-th state', fontsize=12) if j == 8: ax.set_title('3000-th state', fontsize=12) if j == 9: ax.set_title('5000-th state', fontsize=12) plt.savefig(os.getcwd() + f'/Gaussian/convergence/images/gaussian_para_state.pdf', bbox_inches='tight', dpi=1000, pad_inches=0.0) plt.close()
def plot_clusters_evo(evoked_dict, cinfo, ch_type, i_clu=0, analysis_name='', filter_smooth=False, legend=False, blackfig=False): units = dict(eeg='uV', grad='fT/cm', mag='fT') if legend: fig, ax = plt.subplots(1, 1, figsize=(6, 4)) else: fig, ax = plt.subplots(1, 1, figsize=(6, 3)) if blackfig: textcolor = 'white' linecolor = 'white' ax.set_facecolor((.2, .2, .2)) fig.patch.set_facecolor((.2, .2, .2)) else: textcolor = 'black' linecolor = 'black' plt.axvline(0, linestyle='-', color=linecolor, linewidth=2) for xx in range(3): plt.axvline(250 * xx, linestyle='--', color=linecolor, linewidth=1) ax.set_xlabel('Time (ms)', color=textcolor) condnames = list(evoked_dict.keys()) if len(condnames) == 2: colorslist = ['r', 'b'] else: NUM_COLORS = len(condnames) cm = plt.get_cmap('viridis') colorslist = ([ cm(1. * i / (NUM_COLORS - 1)) for i in range(NUM_COLORS) ]) for ncond, condname in enumerate(condnames): data = evoked_dict[condname].copy() evoked_funcs.plot_evoked_with_sem_1cond(data, condname[:-1], ch_type, cinfo['channels_cluster'], color=colorslist[ncond], filter=filter_smooth, axis=None) ymin, ymax = ax.get_ylim() ax.fill_betweenx((ymin, ymax), cinfo['sig_times'][0], cinfo['sig_times'][-1], color='orange', alpha=0.2) if legend: # plt.legend(loc='best', fontsize=6) l = plt.legend(fontsize=7, bbox_to_anchor=(0., 1.25, 1., .08), loc=2, ncol=3, mode="expand", borderaxespad=.8, frameon=False) for text in l.get_texts(): text.set_color(textcolor) for key in ('top', 'right'): # Remove spines ax.spines[key].set(visible=False) # ax.spines['bottom'].set_position('zero') fmt = ticker.ScalarFormatter(useMathText=True) fmt.set_powerlimits((0, 0)) ax.get_yaxis().set_major_formatter(fmt) ax.get_yaxis().get_offset_text().set_position( (-0.08, 0)) # move 'x10-x', does not work with y ax.set_xlim([-50, 600]) ax.set_ylim([ymin, ymax]) ax.set_ylabel(units[ch_type], color=textcolor) ax.spines['bottom'].set_color(linecolor) ax.spines['left'].set_color(linecolor) ax.tick_params(axis='x', colors=textcolor) ax.tick_params(axis='y', colors=textcolor) plt.title(ch_type + '_' + analysis_name + '_clust_' + str(i_clu + 1), fontsize=10, weight='bold', color=textcolor) fig.tight_layout(pad=0.5, w_pad=0) return fig
def plotting_from_data_folder(): ### basic statistic data logging.info('plotting statistics from data folder') stats_fig_data = json.loads(open('data/data_of_figs/stats_fig_data.json').read()) xs,ys = stats_fig_data['wos_stats_cc'] plt.plot(xs,ys,'o',fillstyle='none',c=color_sequence[0], linewidth=2) total = np.sum(ys) low_sum = 0 medium_sum = 0 high_sum = 0 for i,x in enumerate(xs): if x < 64: low_sum+=ys[i] elif x < 985: medium_sum += ys[i] else: high_sum +=ys[i] print 'total:',total print 'low:',low_sum print 'medium:',medium_sum print 'high:',high_sum print 'other:', low_sum-medium_sum-high_sum plt.xscale('log') plt.yscale('log') plt.xlabel('number of citations') plt.ylabel('number of publications') plt.xlim(0.9,10**5) plt.ylim(0.9,10**6) plt.tight_layout() plt.savefig('pdf/figs/wos_stats_cc.jpg',dpi=400) logging.info('Plotting number of publications VS. published year ...') fig,ax2 = plt.subplots(figsize=(6,4)) xs,ys = stats_fig_data['number_of_papers'] # print xs # print ys l2 = ax2.plot(xs,ys,label='number of publications',c=color_sequence[0], linewidth=2) print 'total number of publications:',np.sum(ys) ax2.set_xlabel('published year') ax2.set_ylabel('number of publications') ax2.set_yscale('log') ## average citation count VS. published year ax3 = ax2.twinx() xs,ys = stats_fig_data['average_citation'] l3 = ax3.plot(xs,ys,label='average number of citation',c='r', linewidth=2) l4 = ax3.plot([2004]*10,np.linspace(0,np.max(ys),10),'--',c=color_sequence[2],label='year=2004') ax3.set_ylabel('average number of citations') ax3.set_yscale('log') ls = l2+l3+l4 labels = [l.get_label() for l in ls] ax2.legend(ls,labels,loc=4) plt.tight_layout() plt.savefig('pdf/figs/wos_stats_year.jpg',dpi=400) # ## t2: number of publications VS. number of references # ax4 = axes[2] fig,ax = plt.subplots(figsize=(6,4)) xs,ys = stats_fig_data['wos_stats_refs'] logging.info('Plotting number of papers VS. number of references ...') ax.plot(xs,ys,c=color_sequence[0], linewidth=2) ax.plot([2]*10,np.linspace(0,np.max(ys),10),'--',label='x=2') ax.set_xlabel('number of references') ax.set_ylabel('number of publications') ax.set_xscale('log') ax.set_xlim(0.9,3*10**2) ax.legend() from matplotlib import ticker formatter = ticker.ScalarFormatter(useMathText=True) formatter.set_scientific(True) formatter.set_powerlimits((-1,1)) ax.yaxis.set_major_formatter(formatter) plt.tight_layout() plt.savefig('pdf/figs/wos_stats_refs.jpg',dpi=400) ### temporal diversity changes # citation count diversity fig,axes = plt.subplots(3,1,figsize=(6,12)) group_xys = json.loads(open('data/data_of_figs/temporal_citation_count_diversity_xys.json').read()) # plt.figure(figsize=(6,4)) ax = axes[0] for i,group in enumerate(group_xys.keys()): xs,ys = group_xys[group] ax.plot(xs,ys,c=color_sequence[i],linewidth=2,label=group) ax.set_xlabel('published year\n(a)') ax.set_ylabel('average impact diversity') ax.legend(loc=4) # plt.tight_layout() # plt.savefig('pdf/figs/temporal_citation_count_diversity.jpg',dpi=400) # logging.info('saved to pdf/figs/temporal_citation_count_diversity.jpg') ## subject diversity group_xys = json.loads(open('data/data_of_figs/temporal_subject_diversity_xys.json').read()) ax1 = axes[2] # plt.figure(figsize=(6,4)) for i,group in enumerate(group_xys.keys()): xs,ys = group_xys[group] ax1.plot(xs,ys,c=color_sequence[i],linewidth=2,label=group) ax1.set_xlabel('published year\n(c)') ax1.set_ylabel('average subject diversity') ax1.legend(loc=4) # plt.tight_layout() # plt.savefig('pdf/figs/temporal_subject_diversity.jpg',dpi=400) # logging.info('saved to pdf/figs/temporal_subject_diversity.jpg') ## year diversity group_xys = json.loads(open('data/data_of_figs/temporal_year_differences_diversity_xys.json').read()) # plt.figure(figsize=(6,4)) ax2 = axes[1] for i,group in enumerate(group_xys.keys()): xs,ys = group_xys[group] ax2.plot(xs,ys,c=color_sequence[i],linewidth=2,label=group) ax2.set_xlabel('published year') ax2.legend(loc=4) ax2.set_ylabel('average published year diversity') plt.tight_layout() plt.savefig('pdf/figs/temporal_diversity.jpg',dpi=400) logging.info('saved to pdf/figs/temporal_diversity.jpg') ### basic diversity distribution fig,axes = plt.subplots(3,1,figsize=(6,12)) three_diversity_values = json.loads(open('data/data_of_figs/three_diversity.json').read()) n = three_diversity_values['cc']['n'] bins = three_diversity_values['cc']['bins'] bins = [float('{:2f}'.format(l)) for l in bins] width = np.max(bins)/(len(n)+5) # plt.figure(figsize=(6,4)) ax = axes[0] ax.bar(bins[:-1],n,width=width,align='edge') ax.set_xlabel('impact diversity\n(a)') ax.set_ylabel('number of publications') ax.set_yscale('log') # plt.tight_layout() # plt.savefig('pdf/figs/cc_diversity_dis.jpg',dpi=400) # plt.figure(figsize=(6,4)) ax1 = axes[2] n = three_diversity_values['sd']['n'] bins = three_diversity_values['sd']['bins'] bins = [float('{:2f}'.format(l)) for l in bins] width = np.max(bins)/(len(n)+5) ax1.bar(bins[:-1],n,width=width,align='edge') ax1.set_xlabel('subject diversity\n(c)') ax1.set_ylabel('number of publications') ax1.set_yscale('log') # plt.tight_layout() # plt.savefig('pdf/figs/subject_diversity_dis.jpg',dpi=400) # plt.figure(figsize=(6,4)) ax2 = axes[1] n = three_diversity_values['yd']['n'] bins = three_diversity_values['yd']['bins'] bins = [float('{:2f}'.format(l)) for l in bins] width = np.max(bins)/(len(n)+5) ax2.bar(bins[:-1],n,width=width,align='edge') ax2.set_xlabel('published year diversity\n(b)') ax2.set_ylabel('number of publications') ax2.set_yscale('log') plt.tight_layout() plt.savefig('pdf/figs/diversity_dis.jpg',dpi=400) ### relation between fields and relations # open('data/data_of_figs/diversity_impact_data.json','w').write(json.dumps(fig_data)) dvs_imp_fig_data = json.loads(open('data/data_of_figs/diversity_impact_data.json').read()) logging.info('plot citation count vs. impact diversity ...') xs,ys = dvs_imp_fig_data['cc_cd'] plt.figure() plt.plot(xs,ys,c=color_sequence[0]) plt.xlabel('citation count') plt.ylabel('average impact diversity') plt.tight_layout() plt.savefig('pdf/figs/cc_cd.jpg',dpi=400) xs,ys = dvs_imp_fig_data['cd_cc'] plt.figure() plt.plot(xs,ys,c=color_sequence[0]) plt.xlabel('impact diversity') plt.ylabel('average citation count') plt.tight_layout() plt.savefig('pdf/figs/cd_cc.jpg',dpi=400) logging.info('plot subject diversity vs. impact diversity ...') xs,ys = dvs_imp_fig_data['cc_sd'] plt.figure() plt.plot(xs,ys,c=color_sequence[0]) plt.xlabel('citation count') plt.ylabel('average subject diversity') plt.tight_layout() plt.savefig('pdf/figs/cc_sd.jpg',dpi=400) xs,ys = dvs_imp_fig_data['sd_cc'] plt.figure() plt.plot(xs,ys,c=color_sequence[0]) plt.xlabel('subject diversity') plt.ylabel('average citation count') plt.tight_layout() plt.savefig('pdf/figs/sd_cc.jpg',dpi=400) logging.info('plot year diversity vs. impact diversity ...') xs,ys = dvs_imp_fig_data['cc_yd'] plt.figure() plt.plot(xs,ys,c=color_sequence[0]) plt.xlabel('citation count') plt.ylabel('average year diversity') plt.tight_layout() plt.savefig('pdf/figs/cc_yd.jpg',dpi=400) xs,ys = dvs_imp_fig_data['ys_cc'] plt.figure() plt.plot(xs,ys,c=color_sequence[0]) plt.ylabel('average citation count') plt.xlabel('year diversity') plt.tight_layout() plt.savefig('pdf/figs/yd_cc.jpg',dpi=400) logging.info('Done ...')
def plot_SVM_projection_for_seqID_window_allseq_heatmap(epochs_list, sensor_type, save_path=None, vmin=-1, vmax=1,compute_reg_complexity = False, window_CBPT_violation = None,plot_betas=True): color_viol = ['lightgreen','mediumseagreen','mediumslateblue','darkviolet'] import matplotlib.colors as mcolors colors = [(0, 0, 0, c) for c in np.linspace(0, 1, 2)] cmapsig = mcolors.LinearSegmentedColormap.from_list('significance_cmpa', colors, N=5) # window info, just for figure title win_tmin = epochs_list['test'][0][0].metadata.SVM_filter_tmin_window[0] * 1000 win_tmax = epochs_list['test'][0][0].metadata.SVM_filter_tmax_window[0] * 1000 n_plots = 7 if compute_reg_complexity: n_plots = 8 fig, axes = plt.subplots(n_plots, 1, figsize=(12, 12), sharex=True, sharey=False, constrained_layout=True) fig.suptitle('SVM %s - window %d-%dms; N subjects = %d' % ( sensor_type, win_tmin, win_tmax, len(epochs_list['test'])), fontsize=12) ax = axes.ravel()[::1] ax[0].set_title('Repeat', loc='left', weight='bold') ax[1].set_title('Alternate', loc='left', weight='bold') ax[2].set_title('Pairs', loc='left', weight='bold') ax[3].set_title('Quadruplets', loc='left', weight='bold') ax[4].set_title('Pairs+Alt', loc='left', weight='bold') ax[5].set_title('Shrinking', loc='left', weight='bold') ax[6].set_title('Complex', loc='left', weight='bold') seqtxtXY = ['xxxxxxxxxxxxxxxx', 'xYxYxYxYxYxYxYxY', 'xxYYxxYYxxYYxxYY', 'xxxxYYYYxxxxYYYY', 'xxYYxYxYxxYYxYxY', 'xxxxYYYYxxYYxYxY', 'xYxxxYYYYxYYxxxY'] if compute_reg_complexity: if plot_betas: ax[7].set_title('Beta_complexity', loc='left', weight='bold') else: ax[7].set_title('t-values-betas', loc='left', weight='bold') seqtxtXY.append('') print("vmin = %0.02f, vmax = %0.02f" % (vmin, vmax)) n = 0 violation_significance = {i:[] for i in range(1, 8)} epochs_data_hab_allseq = [] epochs_data_test_allseq = [] for seqID in range(1, 8): print("=== running for sequence %i ==="%seqID) # this provides us with the position of the violations and the times epochs_seq_subset = epochs_list['test'][0]['SequenceID == ' + str(seqID) ] times = epochs_seq_subset.times times = times + 0.3 violpos_list = np.unique(epochs_seq_subset.metadata['ViolationInSequence']) violation_significance[seqID] = {'times':times,'window_significance':window_CBPT_violation} # ----------- habituation trials ----------- epochs_data_hab_seq = [] y_list_epochs_hab = [] data_nanmean = [] where_sig = [] for epochs in epochs_list['hab']: epochs_subset = epochs['SequenceID == ' + str(seqID) ] avg_epo = np.nanmean(np.squeeze(epochs_subset.get_data()), axis=0) # avg_epo = np.nanmean(np.squeeze(epochs_subset.savgol_filter(20).get_data()), axis=0) y_list_epochs_hab.append(avg_epo) epochs_data_hab_seq.append(avg_epo) epochs_data_hab_allseq.append(epochs_data_hab_seq) nanmean_hab = np.nanmean(y_list_epochs_hab, axis=0) data_nanmean.append(nanmean_hab) where_sig.append(np.zeros(nanmean_hab.shape)) where_sig.append(np.zeros(nanmean_hab.shape)) # ----------- test trials ----------- epochs_data_test_seq = [] for viol_pos in violpos_list: y_list = [] contrast_viol_pos = [] for epochs in epochs_list['test']: epochs_subset = epochs[ 'SequenceID == ' + str(seqID) + ' and ViolationInSequence == ' + str(viol_pos) ] # avg_epo = np.nanmean(np.squeeze(epochs_subset.savgol_filter(20).get_data()), axis=0) avg_epo = np.nanmean(np.squeeze(epochs_subset.get_data()), axis=0) y_list.append(avg_epo) if viol_pos==0: avg_epo_standard = np.nanmean(np.squeeze(epochs_subset.get_data()), axis=0) # avg_epo_standard = np.nanmean(np.squeeze(epochs_subset.savgol_filter(20).get_data()), axis=0) epochs_data_test_seq.append(avg_epo_standard) if viol_pos !=0 and window_CBPT_violation is not None: epochs_standard = epochs[ 'SequenceID == ' + str(seqID) + ' and ViolationInSequence == 0'] avg_epo_standard = np.nanmean(np.squeeze(epochs_standard.get_data()), axis=0) # avg_epo_standard = np.nanmean(np.squeeze(epochs_standard.savgol_filter(20).get_data()), axis=0) contrast_viol_pos.append(avg_epo - avg_epo_standard) # --------------- CBPT to test for significance --------------- if window_CBPT_violation is not None and viol_pos !=0: time_start_viol = 0.250 * (viol_pos - 1) time_stop_viol = time_start_viol + window_CBPT_violation inds_stats = np.where(np.logical_and(times>time_start_viol,times<=time_stop_viol)) contrast_viol_pos = np.asarray(contrast_viol_pos) p_vals = np.asarray([1]*contrast_viol_pos.shape[1]) p_values = stats_funcs.stats(contrast_viol_pos[:,inds_stats[0]],tail=1) p_vals[inds_stats[0]] = p_values violation_significance[seqID][int(viol_pos)] = p_vals y_list_alpha = 1*(p_vals<0.05) where_sig.append(y_list_alpha) nanmean_y = np.nanmean(y_list, axis=0) data_nanmean.append(nanmean_y) epochs_data_test_allseq.append(epochs_data_test_seq) where_sig = np.asarray(where_sig) width = 75 # Add vertical lines, and "xY" for xx in range(16): ax[n].axvline(250 * xx,ymin=0,ymax= width, linestyle='--', color='black', linewidth=0.8) txt = seqtxtXY[n][xx] ax[n].text(250 * (xx + 1) - 125, width * 6 + (width / 3), txt, horizontalalignment='center', fontsize=16) # return data_nanmean ax[n].spines["top"].set_visible(False) ax[n].spines["right"].set_visible(False) ax[n].spines["bottom"].set_visible(False) ax[n].spines["left"].set_visible(False) im = ax[n].imshow(data_nanmean, extent=[min(times) * 1000, max(times) * 1000, 0, 6 * width], cmap='RdBu_r', vmin=vmin, vmax=vmax) # add colorbar fmt = ticker.ScalarFormatter(useMathText=True) fmt.set_powerlimits((0, 0)) cb = fig.colorbar(im, ax=ax[n], location='right', format=fmt, shrink=.50, aspect=10, pad=.005) cb.ax.yaxis.set_offset_position('left') cb.set_label('a. u.') # if window_CBPT_violation: # masked = np.ma.masked_where(where_sig == 0, where_sig) # im = ax[n].imshow(masked, extent=[min(times) * 1000, max(times) * 1000, 0, 6 * width], cmap=cmapsig, # vmin=vmin, vmax=vmax,alpha=0.7) ax[n].set_yticks(np.arange(width / 2, 6 * width, width)) ax[n].set_yticklabels(['Deviant - %d' % violpos_list[4], 'Deviant - %d' % violpos_list[3], 'Deviant - %d' % violpos_list[2], 'Deviant - %d' % violpos_list[1], 'Standard', 'Habituation']) ax[n].axvline(0, linestyle='-', color='black', linewidth=2) # add deviant marks for k in range(4): viol_pos = violpos_list[k + 1] x = 250 * (viol_pos - 1) y1 = (4 - k) * width y2 = (4 - 1 - k) * width ax[n].plot([x, x], [y1, y2], linestyle='-', color='black', linewidth=6) ax[n].plot([x, x], [y1, y2], linestyle='-', color=color_viol[k], linewidth=3) find_where_sig = np.where(where_sig[k+2,:]==1)[0] if len(find_where_sig)!=0: ax[n].plot([1000 * times[find_where_sig[0]], 1000 * times[find_where_sig[-1]]], [-(k+1)*width/3, -(k+1)*width/3], linestyle='-', color=color_viol[k], linewidth=3) n += 1 if compute_reg_complexity: epochs_data_hab_allseq = np.asarray(epochs_data_hab_allseq) epochs_data_test_allseq = np.asarray(epochs_data_test_allseq) coeff_const_hab, coeff_complexity_hab, t_const_hab, t_complexity_hab = SVM_funcs.compute_regression_complexity(epochs_data_hab_allseq) coeff_const_test, coeff_complexity_test, t_const_test, t_complexity_test = SVM_funcs.compute_regression_complexity(epochs_data_test_allseq) for xx in range(16): ax[7].axvline(250 * xx, linestyle='--', color='black', linewidth=1) # return data_nanmean if plot_betas: im = ax[7].imshow(np.asarray([np.nanmean(coeff_complexity_hab,axis=0),np.nanmean(coeff_complexity_test,axis=0)]), extent=[min(times) * 1000, max(times) * 1000, 0, 6 * width], cmap='RdBu_r', vmin=-0.5, vmax=0.5) else: im = ax[7].imshow(np.asarray([t_complexity_hab,t_complexity_test]), extent=[min(times) * 1000, max(times) * 1000, 0, 6 * width], cmap='RdBu_r', vmin=-6, vmax=6) fmt = ticker.ScalarFormatter(useMathText=True) fmt.set_powerlimits((0, 0)) cb = fig.colorbar(im, ax=ax[n], location='right', format=fmt, shrink=.50, aspect=10, pad=.005) cb.ax.yaxis.set_offset_position('left') width = width*3 ax[7].set_yticks(np.arange(width / 2, 2 * width, width)) ax[7].set_yticklabels(['Standard', 'Habituation']) ax[7].axvline(0, linestyle='-', color='black', linewidth=2) axes.ravel()[-1].set_xlabel('Time (ms)') figure = plt.gcf() if save_path is not None: figure.savefig(save_path, bbox_inches='tight', dpi=300) plt.close('all') return figure
plt.xlim(xmin=borders[region_index][0], xmax=borders[region_index][1]) plt.ylim(ymin=0, ymax=ymax) axes.xaxis.set_ticks_position('bottom') axes.xaxis.set_ticks(cage_peaks_dict[sample]) axes.xaxis.set_ticklabels(map(calc_relative_coordinate_label, cage_peaks_dict[sample]), fontsize=9) axes.xaxis.set_tick_params(direction='out') axes.yaxis.tick_left() plt.xlim(xmin=borders[region_index][0], xmax=borders[region_index][1]) plt.ylim(ymin=0, ymax=ymax) ax = plt.gca() ax.invert_xaxis() subplot_index += 1 for subplot_index, limits in zip([9, 10], borders): subplot = plt.subplot((number_of_samples + 1)*2, 2, subplot_index) x_formatter = tck.ScalarFormatter(useOffset=False) x_formatter.set_scientific(False) axes = figure.add_subplot((number_of_samples + 1)*2, 2, subplot_index) axes.get_yaxis().set_visible(False) axes.get_xaxis().set_visible(True) axes.xaxis.set_ticks_position('bottom') #axes.xaxis.set_major_formatter(x_formatter) #axes.spines['bottom'].set_color('none') axes.spines['right'].set_color('none') axes.spines['left'].set_color('none') axes.spines['top'].set_color('none') """
def ortho_proj(): # Plot properties cumulative = False add_DM = False#1.682 plot_stars = False img_dir = '/n/pan1/www/ggreen/paper-plots/' img_fname = img_dir + 'ortho_slices_hq' map_fname = '/n/fink1/ggreen/bayestar/output/allsky_2MASS/compact_10samp.h5' stellar_loc_fname = '/n/home09/ggreen/documents/3d-map-release/scripts/stellar_locs.h5' if cumulative: img_fname += '_cumulative' if plot_stars: img_fname += '_stars' if add_DM: img_fname += '_DM' dpi = 400 n_averaged = 5 beta = 90. + np.hstack([0., 0., np.arange(0., 180., 15.)]) alpha = np.hstack([180, 180., 90.*np.ones(beta.size-2)]) degrade = 1 #scale = np.array([0.25, 0.25, 0.25]) * degrade scale = np.array([0.5, 0.5, 0.5]) * degrade n_x = np.hstack([12000, 3000 * np.ones(beta.size, dtype='i4')]) / degrade n_y = np.hstack([12000, 3000, 1000 * np.ones(beta.size-1, dtype='i4')]) / degrade n_z = 10 * np.ones(beta.size, dtype='i4') / degrade # Load map print 'Loading map ...' mapper = maptools.LOSMapper([map_fname], max_samples=10) nside = mapper.data.nside[0] pix_idx = mapper.data.pix_idx[0] los_EBV = mapper.data.los_EBV[0] DM_min, DM_max = mapper.data.DM_EBV_lim[:2] mapper3d = maptools.Mapper3D(nside, pix_idx, los_EBV, DM_min, DM_max, remove_nan=False, keep_cumulative=cumulative) # Load stellar locations star_pos = load_stellar_locs(stellar_loc_fname) # Normal vector to projection ca = np.cos(np.radians(alpha)) sa = np.sin(np.radians(alpha)) cb = np.cos(np.radians(beta)) sb = np.sin(np.radians(beta)) # Render orthographic projections of map samples and take median np.seterr(divide='ignore') img_list = [] wh_list = [] vmax_list = [] for a, b, x, y, z in zip(alpha, beta, n_x, n_y, n_z): print 'Rendering (a, b) = (%.1f deg, %.1f deg) ...' % (a, b) img = np.empty((n_averaged, 2*y+1, 2*x+1), dtype='f8') for i in xrange(n_averaged): print 'Rendering %d of %d sampled maps ...' % (i+1, n_averaged) img[i] = mapper3d.proj_map_in_slices('ortho', 2*z, 'sample', a, b, y, x, z, scale, cumulative=cumulative, add_DM=add_DM, verbose=True) img = np.median(img, axis=0)[::-1,::-1] img[img < 1.e-30] = np.nan ds_factor = 1 if (img.shape[0] > 12000) or (img.shape[1] > 12000): ds_factor = 3 elif (img.shape[0] > 6000) or (img.shape[1] > 6000): ds_factor = 2 img = downsample_by_two(img, times=ds_factor) if not cumulative: img *= 1000. img_list.append(img) w = scale[0] * x h = scale[1] * y wh_list.append([w, h]) idx = np.isfinite(img) vmax_list.append(np.percentile(img[idx], 98.)) print vmax_list[-1], np.max(img[idx]) vmax_list = np.array(vmax_list) # Plot results print 'Plotting ...' #c_bg = '#8DC9C1' #c_bg = '#9FCFC8' c_bg = '#CADEDB' fig = plt.figure(figsize=(9, 8), dpi=dpi) im = None stroke = [PathEffects.withStroke(linewidth=0.5, foreground='w')] stroke_thick = [PathEffects.withStroke(linewidth=1.5, foreground='w')] for k, (img, (w, h)) in enumerate(zip(img_list, wh_list)): extent = [-w, w, -h, h] if k == 1: fig = plt.figure(figsize=(9, 8), dpi=dpi) elif k == 2: fig = plt.figure(figsize=(18, 8), dpi=dpi) vmax = None # Add subplot if k in [0, 1]: vmax = vmax_list[k] ax = fig.add_subplot(1, 1, 1, axisbg=c_bg) else: vmax = np.max(vmax_list[2:]) ax = fig.add_subplot(4, 3, k-1, axisbg=c_bg) # Solar-centric distance labels if k in [0, 1]: theta = np.linspace(0., 2.*np.pi, 1000) circ_x = np.cos(theta) circ_y = np.sin(theta) dists = np.arange(300., 2100.1, 300.) delta = 50. if k == 0: dists *= 4. delta *= 4. for d in dists: ax.plot(d*circ_x, d*circ_y, lw=2., ls='--', c='k', alpha=0.2, zorder=-1) ax.plot(d*circ_x, d*circ_y, lw=0.8, ls='--', c='k', alpha=0.5, zorder=-2) a = 0.5 * np.sqrt(2.) x_txt = a * (d + delta) y_txt = -a * (d + delta) if (x_txt < w) and (y_txt > -h): ax.text(x_txt, y_txt, r'$%d \, \mathrm{pc}$' % d, fontsize=14, rotation=45., ha='center', va='center', zorder=-3) # Dust density image cmap = 'Greys' vmin = 0. if add_DM: vmin = 4. im = ax.imshow(img, origin='lower', cmap=cmap, interpolation='bilinear', aspect='auto', extent=extent, vmin=vmin, vmax=vmax, rasterized=True) prune = None if k in [12, 13]: prune = 'lower' ax.xaxis.set_major_locator(ticker.MaxNLocator(nbins=6, prune=prune)) ax.yaxis.set_major_locator(ticker.MaxNLocator(nbins=6, prune=prune)) ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) ax.yaxis.set_minor_locator(ticker.AutoMinorLocator()) # Stellar locations if plot_stars: u = np.array([[ca[k]*cb[k], -sb[k], sa[k]*cb[k]], [ca[k]*sb[k], cb[k], sa[k]*sb[k]], [ -sa[k], 0, ca[k] ]]) plane_dist = np.einsum('in,ki->nk', u, star_pos) idx = np.abs(plane_dist[2]) < 5. print '# of stars: %d' % (np.sum(idx)) xlim = ax.get_xlim() ylim = ax.get_ylim() s = 2. lw = 0.5 alpha = 0.7 if k == 0: s = 2. lw = 0.5 alpha = 1. elif k == 1: s = 4. lw = 0.5 alpha = 1. ax.scatter(-plane_dist[1,idx], -plane_dist[0,idx], facecolor='#246AB5', edgecolor='#82BEFF', s=s, lw=lw, alpha=alpha) ax.set_xlim(xlim) ax.set_ylim(ylim) # Axis labeling if k in [0, 1]: ax.set_xlabel(r'$\mathrm{pc}$', fontsize=14) ax.set_ylabel(r'$\mathrm{pc}$', fontsize=14) elif k == 2: fig.text(0.475, 0.07, r'$\mathrm{pc}$', fontsize=14, ha='center', va='top') fig.text(0.04, 0.525, r'$\mathrm{pc}$', fontsize=14, ha='right', va='center', rotation=90., alpha=0.8) # \ell Arrows if k in [0, 1]: ax.arrow(0.60*w, 0., 0.30*w, 0., head_width=0.04*h, head_length=0.05*w, width=0.015*h, ec='w', fc='k', lw=1.) ax.text(0.75*w, 0.03*h, r'${\boldmath \ell = 0^{\circ}}$', fontsize=20, ha='center', va='bottom', path_effects=stroke_thick) ax.arrow(0., 0.60*h, 0., 0.30*h, head_width=0.04*w, head_length=0.05*h, width=0.015*w, ec='w', fc='k', lw=1.) ax.text(-0.03*w, 0.75*h, r'${\boldmath \ell = 90^{\circ}}$', fontsize=20, ha='right', va='center', path_effects=stroke_thick, rotation=90) else: ax.arrow(0.60*w, 0.6*h, 0.30*w, 0., head_width=0.04*h, head_length=0.05*w, ec='k', fc='k') ax.text(0.75*w, 0.65*h, r'$\ell = %d^{\circ}$' % (beta[k]-90.), fontsize=10, ha='center', va='bottom') ax.arrow(-0.60*w, 0.6*h, -0.30*w, 0., head_width=0.04*h, head_length=0.05*w, ec='k', fc='k') ax.text(-0.75*w, 0.65*h, r'$\ell = %d^{\circ}$' % (np.mod(beta[k]+90., 360.)), fontsize=10, ha='center', va='bottom') # Remove extraneous tick labels if k not in [0, 1, 11, 12, 13]: ax.set_xticklabels([]) if k not in [0, 1, 2, 5, 8, 11]: ax.set_yticklabels([]) # Color bar, save figure if k in [0, 1, 13]: fig.subplots_adjust(left=0.08, right=0.87, bottom=0.10, top=0.95, wspace=0.02, hspace=0.02) cax = fig.add_axes([0.88, 0.10, 0.02, 0.85]) formatter = ticker.ScalarFormatter() formatter.set_scientific(True) formatter.set_powerlimits((-2,2)) cbar = fig.colorbar(im, cax=cax, format=formatter) quantity = r'\frac{\mathrm{d}}{\mathrm{d} s} \mathrm{E} \left( B - V \right)' units = r'\left( \mathrm{mag} / \mathrm{kpc} \right)' if cumulative: quantity = r'\mathrm{E} \left( B - V \right)' units = r'\left( \mathrm{mag} \right)' if add_DM: quantity = r'R_{i} \, ' + quantity quantity += r' + \mu' cbar.set_label(r'$%s \ %s$' % (quantity, units), fontsize=14) cbar.ax.tick_params(labelsize=11) cbar.solids.set_edgecolor('face') # Fix for matplotlib svg-rendering bug suffix = '' if k == 0: suffix = '_z0_wide' elif k == 1: suffix = '_z0' else: suffix = '_vertical' fig.savefig(img_fname + suffix + '.svg', dpi=dpi, bbox_inches='tight')
def plotGain(prefix, spw): #-------- Load tables antFile = prefix + '.Ant.npy' antList = np.load(antFile) antNum = len(antList) timeFile = '%s-SPW%d.TS.npy' % (prefix, spw) GainFile = '%s-SPW%d.GA.npy' % (prefix, spw) pp = PdfPages('GA_%s-SPW%d.pdf' % (prefix, spw)) DT, timeStamp, Gain = [], np.load(timeFile), np.load(GainFile) polNum = Gain.shape[1] for mjdSec in timeStamp.tolist(): DT.append( datetime.datetime.strptime( qa.time('%fs' % (mjdSec), form='fits', prec=9)[0], '%Y-%m-%dT%H:%M:%S.%f')) #-------- Prepare Plots figAmp, figPhs = plt.figure(figsize=(8, 11)), plt.figure(figsize=(8, 11)) figAmp.suptitle(GainFile + ' Gain Amplitude') figPhs.suptitle(GainFile + ' Gain Phase') figAmp.text(0.45, 0.05, 'UTC on %s' % (DT[0].strftime('%Y-%m-%d'))) figPhs.text(0.45, 0.05, 'UTC on %s' % (DT[0].strftime('%Y-%m-%d'))) figAmp.text(0.03, 0.7, 'Gain Amplitude = sqrt(correlated flux / SEFD)', rotation=90) figPhs.text(0.03, 0.55, 'Gain Phase [deg]', rotation=90) plotMin, plotMax = 0.0, 1.1 * np.percentile(abs(Gain), 80) #-------- Plot Gain for ant_index in list(range(antNum)): AmpPL = figAmp.add_subplot(int(np.ceil(antNum / 2.0)), 2, ant_index + 1) PhsPL = figPhs.add_subplot(int(np.ceil(antNum / 2.0)), 2, ant_index + 1) for pol_index in list(range(polNum)): AmpPL.plot(DT, abs(Gain[ant_index, pol_index]), '.', markersize=3, color=polColor[pol_index]) for pol_index in list(range(polNum)): PhsPL.plot(DT, np.angle(Gain[ant_index, pol_index]) * 180.0 / pi, '.', markersize=3, color=polColor[pol_index]) ampMed = np.median(abs(Gain[ant_index]), axis=1) AmpPL.yaxis.set_major_formatter( ptick.ScalarFormatter(useMathText=True)) AmpPL.yaxis.offsetText.set_fontsize(3) PhsPL.yaxis.offsetText.set_fontsize(3) AmpPL.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) AmpPL.tick_params(labelsize=4) PhsPL.tick_params(labelsize=4) AmpPL.axis([np.min(DT), np.max(DT), plotMin, plotMax]) PhsPL.axis([np.min(DT), np.max(DT), -180.0, 180.0]) if polNum == 2: text_sd = '%s : Gain(median) = (%.2f%% %.2f%%)' % ( antList[ant_index], 100.0 * ampMed[0], 100.0 * ampMed[1]) else: text_sd = '%s : Gain(median) = (%.2f%%)' % (antList[ant_index], 100.0 * ampMed[0]) AmpPL.text(0.05, 1.02, text_sd, transform=AmpPL.transAxes, fontsize=5) PhsPL.text(0.05, 1.02, antList[ant_index], transform=PhsPL.transAxes, fontsize=5) # figAmp.savefig(pp, format='pdf') figPhs.savefig(pp, format='pdf') plt.close('all') pp.close() return
def drawLineGraph(X, Y, X_labels, x_val, y_val, fig_name, is_math): if x_val is Xvalue.RWT: X, Y = rwtSort(X, Y, X_labels) # elif len(X) != len(Y): # X = [X for i in range(len(Y))] elif np.array(X).shape != np.array(Y).shape: X = [X for i in range(len(Y))] if x_val is Xvalue.RTO and y_val is Yvalue.RWT: # print(X, Y) X = [x[1:] for x in X] Y = [y[1:] for y in Y] # X_labels = X_labels[1:] # print(X, Y) # print("\n---X---\n") # for x in X: # print(x) # print("\n---Y---\n") # for y in Y: # print(y) for i in range(len(Y)): plt.plot(X[i], Y[i], color=COLOR_LIST[i], label=X_labels[i], markersize="5", linestyle=STYLE_LIST[i]) plt.xlabel(defineXlabel(x_val)) plt.ylabel(defineYlabel(y_val)) plt.grid(True) if x_val is Xvalue.DISTANCE: plt.xticks(X[0]) plt.legend(loc='upper left') elif x_val is Xvalue.DUPLICATION: plt.xticks(X[0]) plt.legend(loc='upper right') elif x_val is Xvalue.DC: x = [round(i, 1) for i in X[0]] plt.xticks(x) plt.legend(loc='upper right') elif x_val is Xvalue.RTO: x = ["RTO-{}".format(i) for i in X[0]] plt.xticks(X[0], x) plt.legend(loc='upper right') elif x_val is Xvalue.DECOMPOSINGMETHOD: x = ["Decomposing{}".format(i) for i in X[0]] plt.xticks(X[0], x) plt.legend(loc='upper right') elif x_val is Xvalue.PACKETNUM: plt.xticks(X[0]) plt.legend(loc='upper right') elif x_val is Xvalue.RATE: plt.xticks(X[0]) plt.legend(loc='upper right') else: plt.legend(loc='upper right') # if x_val is Xvalue.DUPLICATION and y_val is Yvalue.COLLISION: # plt.legend(loc='upper left') if x_val is Xvalue.RTO and y_val is Yvalue.RETRANSMITFAILURE: plt.ylim([-5, 80]) plt.locator_params(axis='y',min_n_ticks=4) if is_math: plt.gca().yaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) plt.gca().ticklabel_format(style="sci", axis="y",scilimits=(0,0)) plt.savefig(fig_name) plt.close('all')
fig, ax = plt.subplots(1) ax.scatter(nodes, times, label="timestep", marker="s") c, t = trend_line(nodes, times) ax.plot(c, t) ax.scatter(nodes, hydro, label="hydro") ax.scatter(nodes, react, label="reactions") ax.scatter(nodes, diffuse, label="diffusion") ax.scatter(nodes, copy, label="MPI") ax.set_xscale("log") ax.xaxis.set_minor_formatter(mticker.ScalarFormatter()) ax.xaxis.set_major_formatter(mticker.ScalarFormatter()) ax.yaxis.set_minor_formatter(mticker.FormatStrFormatter("%4.1f")) ax.yaxis.set_major_formatter(mticker.ScalarFormatter()) ax.set_xlim(200, 2500) #ax.set_ylim(5, 50) ax.set_ylabel("wallclock time / step") ax.set_xlabel("number of nodes") leg = ax.legend(facecolor="#555555", framealpha=0.1) fig.savefig(f"flame_wave_scaling_{year}.png") # now show the change through years
def do_wavelet(time, mag, mother, period, dj, unit, filename, display): bad = np.isnan(mag) if np.any(bad): mag[bad] = np.interp(time[bad], time[~bad], mag[~bad]) dj = 1 / dj dt = (time[2] - time[1]).total_seconds() dt = np.round(dt, 3) Tmin = period[0] Tmax = period[1] lag1 = 0.72 # lag-1 autocorrelation for red noise background if mother == 'Morlet': fac = 1.033 elif mother == 'Paul': fac = 1.3963 elif mother == 'DOG': fac = 3.9738 else: assert 'method not understood' min_power_of2_filt = np.floor(np.log2(Tmin / dt)) max_power_of2_filt = np.ceil(np.log2(Tmax / dt)) min_power_of2 = np.floor(np.log2(Tmin / fac / dt)) max_power_of2 = np.ceil(np.log2(Tmax / fac / dt)) s0 = 2**(min_power_of2) * dt j1 = (max_power_of2 - min_power_of2) / dj # this says do (max_power_of2-min_power_of2) powers-of-two above min_power_of2 with dj sub-octaves each mag = bandpass_filter(mag, args={ 'lower cut-off (s)': 2**(min_power_of2_filt) * dt, 'upper cut-off (s)': 2**(max_power_of2_filt) * dt }) mag = mag.values n = len(mag) variance = np.nanstd(mag, ddof=1)**2 if len(mag) * j1 > 30000000: assert 'Number of data too large to perform wavelet analysis, please:\n'+\ '- reduce the time window, or\n- reduce the sub-octave number, or\n- reduce the period window' # Wavelet transform: wave, period, scale, coi = wavelet(mag, dt, 1, dj, s0, j1, mother.upper()) power = (np.abs(wave))**2 # compute wavelet power spectrum # Global wavelet spectrum global_ws = variance * (np.nansum(power, axis=1) / n ) # % time-average over all times # Scale-average between Tmin and Tmax avg = np.logical_and(period >= Tmin, period < Tmax) Cdelta = 0.776 # % this is for the MORLET wavelet scale_avg = scale[:, np.newaxis].dot( np.ones(n)[np.newaxis, :]) # expand scale --> (J+1)x(N) array scale_avg = power / scale_avg # % [Eqn(24)] scale_avg = variance * dj * dt / Cdelta * sum( scale_avg[avg, :]) # % [Eqn(24)] scaleavg_signif = wave_signif(variance, dt=dt, scale=scale, sigtest=2, lag1=lag1, dof=([2, 7.9]), mother=mother.upper()) # Significance levels: signif = wave_signif(([variance]), dt=dt, sigtest=0, scale=scale, lag1=lag1, mother=mother.upper()) sig95 = signif[:, np.newaxis].dot( np.ones(n)[np.newaxis, :]) # expand signif --> (J+1)x(N) array sig95 = power / sig95 # where ratio > 1, power is significant # Global wavelet spectrum & significance levels: dof = n - scale # the -scale corrects for padding at edges global_signif = wave_signif(variance, dt=dt, scale=scale, sigtest=1, lag1=lag1, dof=dof, mother=mother.upper()) # fig=figure; # %------------------------------------------------------ Plotting Tmin_plot = 2**(min_power_of2_filt) * dt Tmax_plot = 2**(max_power_of2_filt) * dt #find the appropriate period unit if Tmax_plot > 3600 * 24 * 2: period = period / (3600 * 24) Tmin = Tmin / (3600 * 24) Tmax = Tmax / (3600 * 24) Tmin_plot = Tmin_plot / (3600 * 24) Tmax_plot = Tmax_plot / (3600 * 24) t_unit = 'days' elif Tmax_plot > 3600 * 6: period = period / 3600 Tmin = Tmin / 3600 Tmax = Tmax / 3600 Tmin_plot = Tmin_plot / 3600 Tmax_plot = Tmax_plot / 3600 t_unit = 'h' else: t_unit = 's' #--- Plot time series fig = plt.figure(figsize=(8.27, 11.69), dpi=100) gs = GridSpec(3, 4, hspace=0.4, wspace=0.75) plt.subplots_adjust(left=0.1, bottom=0.05, right=0.9, top=0.95, wspace=0, hspace=0) plt.subplot(gs[0, 0:3]) plt.plot(time, mag, 'k') plt.xlim([min(time), max(time)]) plt.xlabel('Time (year)') plt.ylabel('Elevation [' + unit + ']') plt.title('Sea Surface elevation') # #increase the time interval if the contour is too dense if len(time) > 10000: #import pdb;pdb.set_trace() while len(time) > 20000: time = time[::2] scale_avg = np.convolve(scale_avg, np.ones(2) / 2, mode='same') for i in range(0, power.shape[0]): power[i, :] = np.convolve(power[i, :], np.ones(2) / 2, mode='same') scale_avg = scale_avg[::2] power = power[:, ::2] coi = coi[::2] plt3 = plt.subplot(gs[1, 0:3]) levels = np.linspace(np.min(power), np.max(power), 5) CS = plt.contourf(time, period, power, len(levels)) #*** or use 'contour' im = plt.contourf( CS, levels=levels, colors=['white', 'bisque', 'orange', 'orangered', 'darkred']) plt.ylabel('Period (years)') plt.title('Wavelet Power Spectrum') plt.xlim([min(time), max(time)]) # 95# significance contour, levels at -99 (fake) and 1 (95# signif) #plt.contour(time, period, sig95, [-99, 1], colors='k') # cone-of-influence, anything "below" is dubious plt.plot(time, coi, 'k') # format y-scale #plt.show() plt3.set_yscale('log', basey=2, subsy=None) plt.ylim([np.min(period), np.max(period)]) ax = plt.gca().yaxis ax.set_major_formatter(ticker.ScalarFormatter()) plt3.ticklabel_format(axis='y', style='plain') plt3.invert_yaxis() plt.colorbar() #--- Plot global wavelet spectrum plt4 = plt.subplot(gs[1, -1]) plt.plot(global_ws, period) #plt.plot(global_signif, period, '--') plt.xlabel('Power (' + unit + '$^2$)') #plt.show() plt.title('Global Wavelet Spectrum') plt.xlim([0, 1.25 * np.nanmax(global_ws)]) # format y-scale plt4.set_yscale('log', basey=2, subsy=None) plt.ylim([np.min(period), np.nanmax(period)]) ax = plt.gca().yaxis ax.set_major_formatter(ticker.ScalarFormatter()) plt4.ticklabel_format(axis='y', style='plain') plt4.invert_yaxis() # --- Plot 2--8 yr scale-average time series plt.subplot(gs[2, 0:3]) plt.plot(time, scale_avg, 'k') plt.xlim([min(time), max(time)]) plt.ylabel('Avg variance (' + unit + '$^2$)') plt.title('Scale-average Time Series') xlim = [min(time), max(time)] #plt.plot(xlim, scaleavg_signif + [0, 0], '--') #fig.autofmt_xdate() plt.subplots_adjust(bottom=0.02) fig.autofmt_xdate() if display: plt.show(block=~display) plt.savefig(filename)
def test_use_offset(self, use_offset): with matplotlib.rc_context({'axes.formatter.useoffset': use_offset}): tmp_form = mticker.ScalarFormatter() assert use_offset == tmp_form.get_useOffset()
def dynamic_risk_plot(risk_exposed, risk_unexposed, measure='RD', loess=True, loess_value=0.25, point_color='darkblue', line_color='b', scale='linear'): """Creates a plot of risk measures over time. See Cole et al. "Estimation of standardized risk difference and ratio in a competing risks framework: application to injection drug use and progression to AIDS after initiation of antiretroviral therapy." Am J Epidemiol. 2015 for an example of this plot risk_exposed: -pandas Series with the probability of the outcome among the exposed group. Index by 'timeline' where 'timeline' is the time. If you directly output the 1 - survival_function_ from lifelines.KaplanMeierFitter(), this should create a valid input risk_unexposed: -pandas Series with the probability of the outcome among the exposed group. Index by 'timeline' where 'timeline' is the time measure: -whether to generate the risk difference (RD) or risk ratio (RR). Default is 'RD' loess: -whether to generate LOESS curve fit to the calculated points. Default is True loess_value: -fraction of values to fit LOESS curve to. Default is 0.25 point_color: -color of the points line_color: -color of the LOESS line generated and plotted scale: -change the y-axis scale. Options are 'linear' (default), 'log', 'log-transform'. 'log' and 'log-transform' is only a valid option for Risk Ratio plots """ re = risk_exposed.drop_duplicates( keep='first').iloc[:, 0].rename('exposed').reset_index() ru = risk_unexposed.drop_duplicates( keep='first').iloc[:, 0].rename('unexposed').reset_index() re.timeline = np.round(re.timeline * 100000).astype( int) # This avoids a merge issue on floats ru.timeline = np.round(ru.timeline * 100000).astype(int) r = pd.merge(re, ru, how='outer', left_on='timeline', right_on='timeline').sort_values(by='timeline') r.timeline /= 100000 r.ffill(inplace=True) if measure == 'RD': r['m'] = r['exposed'] - r['unexposed'] elif measure == 'RR': r['m'] = r['exposed'] / r['unexposed'] if scale == 'log-transform': r['m'] = np.log(r['m']) else: raise ValueError('Only "RD" and "RR" are currently supported') # Generating the plot ax = plt.gca() ax.plot(r['timeline'], r['m'], 'o', c=point_color) if loess is True: l = lowess(list(r['m']), list(r['timeline']), frac=loess_value) lowess_x = list(zip(*l))[0] lowess_y = list(zip(*l))[1] ax.plot(lowess_x, lowess_y, '-', c=line_color, linewidth=4) if measure == 'RD': ax.hlines(0, 0, np.max(r['timeline'] + 0.5), linewidth=1.5) ax.set_ylabel('Risk Difference') if measure == 'RR': if scale == 'log-transform': ax.hlines(0, 0, np.max(r['timeline'] + 0.5), linewidth=1.5) ax.set_ylabel('ln(Risk Ratio)') elif scale == 'log': ax.set_ylabel('Risk Ratio') ax.set_yscale('log') ax.yaxis.set_major_formatter(mticker.ScalarFormatter()) ax.yaxis.get_major_formatter().set_scientific(False) ax.yaxis.get_major_formatter().set_useOffset(False) ax.hlines(1, 0, np.max(r['timeline'] + 0.5), linewidth=1.5) else: ax.hlines(1, 0, np.max(r['timeline'] + 0.5), linewidth=1.5) ax.set_xlabel('Time') ax.set_xlim([0, np.max(r['timeline']) + 0.5]) return ax
def plot_clusters(cluster_info, ch_type, T_obs_max=5., fname='', figname_initial='', filter_smooth=False, outfile=None): """ This function plots the clusters :param cluster_info: :param good_cluster_inds: indices of the cluster to plot :param T_obs_max: colormap limit :param fname: :param figname_initial: :return: """ color = 'r' linestyle = '-' T_obs_min = -T_obs_max for i_clu in range(cluster_info['ncluster']): cinfo = cluster_info[i_clu] T_obs_map = cluster_info['T_obs'][cinfo['time_inds'], ...].mean(axis=0) mask = np.zeros((T_obs_map.shape[0], 1), dtype=bool) mask[cinfo['channels_cluster'], :] = True fig, ax_topo = plt.subplots(1, 1, figsize=(7, 2.)) if (mne.__version__ == '0.22.0' or mne.__version__ == '0.23.0') & (ch_type != 'grad'): # issue when plotting grad (pairs) when there is a mask ??! image, _ = plot_topomap(T_obs_map, cluster_info['data_info'], extrapolate='head', mask=mask, axes=ax_topo, vmin=T_obs_min, vmax=T_obs_max, show=False, ch_type=ch_type) else: image, _ = plot_topomap(T_obs_map, cluster_info['data_info'], extrapolate='head', mask=mask, axes=ax_topo, vmin=T_obs_min, vmax=T_obs_max, show=False, ch_type='mag') divider = make_axes_locatable(ax_topo) # add axes for colorbar ax_colorbar = divider.append_axes('right', size='5%', pad=0.05) plt.colorbar(image, cax=ax_colorbar, format='%0.1f') ax_topo.set_xlabel('Averaged t-map\n({:0.1f} - {:0.1f} ms)'.format( *cinfo['sig_times'][[0, -1]])) ax_topo.set(title=ch_type + ': ' + fname) # signal average & sem (over subjects) ax_signals = divider.append_axes('right', size='300%', pad=1.2) # for signal, name, col, ls in zip(cinfo['signal'], [fname], colors, linestyles): # ax_signals.plot(cluster_info['times'], signal * 1e6, color=col, linestyle=ls, label=name) mean = np.mean(cinfo['signal'], axis=0) ub = mean + sem(cinfo['signal'], axis=0) lb = mean - sem(cinfo['signal'], axis=0) if filter_smooth: mean = savgol_filter(mean, 9, 3) ub = savgol_filter(ub, 9, 3) lb = savgol_filter(lb, 9, 3) ax_signals.fill_between(cluster_info['times'], ub, lb, color=color, alpha=.2) ax_signals.plot(cluster_info['times'], mean, color=color, linestyle=linestyle, label=fname) # ax_signals.axvline(0, color='k', linestyle=':', label='stimulus onset') ax_signals.axhline(0, color='k', linestyle='-', linewidth=0.5) ax_signals.set_xlim( [cluster_info['times'][0], cluster_info['times'][-1]]) ax_signals.set_xlabel('Time [ms]') ax_signals.set_ylabel('Amplitude') ymin, ymax = ax_signals.get_ylim() ax_signals.fill_betweenx((ymin, ymax), cinfo['sig_times'][0], cinfo['sig_times'][-1], color='orange', alpha=0.3) # ax_signals.legend(loc='lower right') fmt = ticker.ScalarFormatter(useMathText=True) fmt.set_powerlimits((0, 0)) ax_signals.get_yaxis().set_major_formatter(fmt) ax_signals.get_yaxis().get_offset_text().set_position( (-0.07, 0)) # move 'x10-x', does not work with y title = 'Cluster #{0} (p < {1:0.3f})'.format(i_clu + 1, cinfo['p_values']) # title = 'Cluster #{0} (p = %0.03f)'.format(i_clu + 1, cinfo['p_values']) if outfile is not None: outfile.write("\n") outfile.write('----- Cluster number %i ------ \n' % (i_clu + 1)) time_str = str(cinfo['sig_times'][0]) + ' to ' + str( cinfo['sig_times'][-1]) + ' ms' cluster_value_str = ', cluster-value= ' + str(cinfo['clust_val']) p_value_str = ', p = ' + str(cinfo['p_values']) outfile.write(time_str + cluster_value_str + p_value_str) ax_signals.set(ylim=[ymin, ymax], title=title) fig.tight_layout(pad=0.5, w_pad=0) fig.subplots_adjust(bottom=.05) fig_name = figname_initial + '_clust_' + str(i_clu + 1) + '.svg' print('Saving ' + fig_name) plt.savefig(fig_name, dpi=300) plt.close('all)') return True
linestyles = OrderedDict([('solid', (0, ())), ('loosely dotted', (0, (1, 10))), ('dotted', (0, (1, 5))), ('densely dotted', (0, (1, 1))), ('loosely dashed', (0, (5, 10))), ('dashed', (0, (5, 5))), ('densely dashed', (0, (5, 1))), ('loosely dashdotted', (0, (3, 10, 1, 10))), ('dashdotted', (0, (3, 5, 1, 5))), ('densely dashdotted', (0, (3, 1, 1, 1))), ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))), ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))), ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]) import matplotlib.ticker as mticker f = mticker.ScalarFormatter(useMathText=True) f.set_powerlimits((-3, 3)) "${}$".format(f.format_data(0.0001)) default_x = 5 * 1.4 default_y = 3 * 1.4 plt.gcf().set_size_inches(default_x, default_y) #%% ## load data ## ode_mem = np.loadtxt('new_run/ODE.txt', skiprows=1) ode_mem[:, 1:9] *= 0.2209 pde_mem = np.loadtxt('new_run/PDE.txt', skiprows=1)
def plot_clusters_evo_bars(evoked_dict, cinfo, ch_type, i_clu=0, analysis_name='', filter_smooth=False, legend=False, blackfig=False): units = dict(eeg='uV', grad='fT/cm', mag='fT') if legend: fig, ax = plt.subplots(1, 1, figsize=(3, 4)) else: fig, ax = plt.subplots(1, 1, figsize=(5, 5)) if blackfig: textcolor = 'white' linecolor = 'white' ax.set_facecolor((.2, .2, .2)) fig.patch.set_facecolor((.2, .2, .2)) else: textcolor = 'black' linecolor = 'black' plt.axhline(0, linestyle='-', color=linecolor, linewidth=1) # for xx in range(3): # plt.axvline(250 * xx, linestyle='--', color=linecolor, linewidth=1) # ax.set_xlabel('Time (ms)', color=textcolor) ch_inds = cinfo[ 'channels_cluster'] # channel indices from 366 (mag+grad+eeg) ??? t_inds = cinfo['time_inds'] condnames = list(evoked_dict.keys()) if len(condnames) == 2: colorslist = ['r', 'b'] else: NUM_COLORS = len(condnames) cm = plt.get_cmap('viridis') colorslist = ([ cm(1. * i / (NUM_COLORS - 1)) for i in range(NUM_COLORS) ]) for ncond, condname in enumerate(condnames): data = evoked_dict[condname].copy() group_data_seq = [] for nn in range(len(data)): sub_data = data[nn][0].copy() if ch_type == 'eeg': sub_data = np.array( sub_data.pick_types(meg=False, eeg=True)._data) elif ch_type == 'mag': sub_data = np.array( sub_data.pick_types(meg='mag', eeg=False)._data) elif ch_type == 'grad': sub_data = np.array( sub_data.pick_types(meg='grad', eeg=False)._data) if np.size(ch_inds) > 1: sub_data = sub_data[:, t_inds].mean( axis=1) # average times indices sub_data = sub_data[ch_inds].mean( axis=0) # average channel indices group_data_seq.append(sub_data) else: sub_data = sub_data[:, t_inds].mean( axis=1) # average times indices group_data_seq.append(sub_data) mean = np.mean(group_data_seq, axis=0) ub = mean + sem(group_data_seq, axis=0) lb = mean - sem(group_data_seq, axis=0) plt.bar(ncond, mean, color=colorslist[ncond]) plt.errorbar(ncond, mean, yerr=sem(group_data_seq, axis=0), ecolor='black', capsize=5) ymin, ymax = ax.get_ylim() if legend: # plt.legend(loc='best', fontsize=6) l = plt.legend(fontsize=7, bbox_to_anchor=(0., 1.25, 1., .08), loc=2, ncol=3, mode="expand", borderaxespad=.8, frameon=False) for text in l.get_texts(): text.set_color(textcolor) for key in ('top', 'right', 'bottom'): # Remove spines ax.spines[key].set(visible=False) # ax.spines['bottom'].set_position('zero') fmt = ticker.ScalarFormatter(useMathText=True) fmt.set_powerlimits((0, 0)) ax.get_yaxis().set_major_formatter(fmt) ax.get_yaxis().get_offset_text().set_position( (-0.08, 0)) # move 'x10-x', does not work with y # ax.set_xlim([-100, 600]) # ax.set_ylim([ymin, ymax]) if condnames[0][-14:] == 'SequenceID_01-': condnames = [ 'SeqID1', 'SeqID2', 'SeqID3', 'SeqID4', 'SeqID5', 'SeqID6', 'SeqID7' ] plt.xticks(np.arange(len(condnames)), condnames, rotation=40) ax.set_ylabel(units[ch_type], color=textcolor) ax.spines['bottom'].set_color(linecolor) ax.spines['left'].set_color(linecolor) ax.tick_params(axis='x', colors=textcolor, length=0) ax.tick_params(axis='y', colors=textcolor) times = data[0][ 0].times * 1000 + 50 # since clusters starts at 0 and epochs at -50 plt.title(ch_type + '_' + analysis_name + '_clust_' + str(i_clu + 1) + '_[%d-%dms]' % (times[t_inds[0]], times[t_inds[-1]]), fontsize=9, color=textcolor) fig.tight_layout(pad=0.5, w_pad=0) return fig
'patch.linewidth': 2., 'axes.linewidth': 2., 'lines.linewidth': 1.8, 'lines.markersize': 8.0, 'axes.formatter.limits': (-4, 6) } plt.rcParams.update(params) plt.subplots_adjust(left=0.21, right=0.93, bottom=0.18, top=0.88, wspace=None, hspace=None) yfmt = ptk.ScalarFormatter(useOffset=True, useMathText=True) figure = plt.figure(1, figsize=(8, 5.5)) ax = figure.add_subplot(111) ax.text(0.5, -0.13, xlabel, size=fontlabel, transform=ax.transAxes, ha='center', va='center') ax.text(-0.19, 0.5, ylabel, size=fontlabel, transform=ax.transAxes,
#psi_min_all[144:216] = psi_min_fq line_low_all, consts_low = fit_power_law(edge_ll_all, psi_min_all, maxmin=[0., 9.]) line_high_all, consts_high = fit_power_law(edge_ll_all, psi_min_all, maxmin=[9., 30.]) plt.plot(np.arange(0., 10.), line_low_all, 'k-', linewidth=2) plt.plot(np.arange(9., 31.), line_high_all, 'k-', linewidth=2) plt.plot(edge_ll_ap, psi_min_ap, 'kx', ms=10, mew=2) plt.plot(edge_ll_20, psi_min_20, 'bx', ms=10, mew=2) plt.plot(edge_ll_fq, psi_min_fq, 'rx', ms=10, mew=2) ax = plt.gca() plt.xlabel('Lat. of ascending branch of SH Hadley cell') plt.ylabel('Peak strength of SH Hadley circulation, 10$^9$ kg/s') plt.xscale('log', subsx=[2, 3, 4, 5]) plt.yscale('log', subsx=[2, 3, 4, 5]) plt.xlim(1, 30) plt.ylim(150, 400) plt.xticks([1, 5, 10, 20, 30]) plt.yticks([200, 300, 400]) ax.get_xaxis().set_major_formatter(tk.ScalarFormatter()) ax.get_yaxis().set_major_formatter(tk.ScalarFormatter()) plt.tight_layout() figname = 'psi_latvsstrength_above850_mean.pdf' plt.savefig(plot_dir + figname, format='pdf') plt.close()
def plot_intensity(file_name_in): # iTemporal = 0 # iPeriodic = 1 ################################################################## # ## filename = file_name_in filename_base = (file_name_in.split('.')[0]).strip() mdata = fdata(filename) # H5PY option below #hf=h5py.File(filename, 'r') #n1=hf.get('aperp') #n1=np.array(n1) hf = tables.open_file(file_name_in, 'r') n1 = hf.root.aperp.read() xpol = n1[0, :, :, :] ypol = n1[1, :, :, :] z2axis = np.multiply((np.arange(0, mdata.vars.nz2)), mdata.vars.dz2) intensity2 = np.divide( np.trapz(np.add(np.square(xpol), np.square(ypol)), x=z2axis, axis=0), (np.multiply(mdata.vars.nz2, mdata.vars.dz2))) intensity = np.transpose(intensity2) dx = mdata.vars.dx dy = mdata.vars.dy nx = mdata.vars.nx ny = mdata.vars.ny #x=(np.arange(-nx/2,nx/2)*dx*1e3) #y=(np.arange(-ny/2,ny/2)*dy *1e3) #X,Y=np.meshgrid(x,y) # font = matplotlib.font_manager.FontProperties(family='serif') plt.clf() plt.figure(1) ax2 = plt.subplot(111) ax2.set_ylabel(r'y [$mm$]', fontsize=12) ax2.yaxis.set_major_formatter(mtick.ScalarFormatter(useMathText=True)) ax2.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) ax2.set_xlabel(r'x [$mm$]', fontsize=12) ax2.xaxis.set_major_formatter(mtick.ScalarFormatter(useMathText=True)) ax2.ticklabel_format(style='sci', axis='x', scilimits=(0, 0)) plt.imshow(np.multiply(intensity, mdata.vars.intensScale), cmap='jet', interpolation='bilinear', extent=[ -nx * dx * 1e3 / 2., nx * dx * 1e3 / 2., -ny * dy * 1e3 / 2., ny * dy * 1e3 / 2. ]) #plt.xlabel('x ($mm$)', fontproperties=font) #plt.ylabel('y ($mm$)', fontproperties=font) #============================================================================== # ax= plt.axes() # for label in ax.get_xticklabels(): # label.set_fontproperties(font) # for label in ax.get_yticklabels(): # label.set_fontproperties(font) #============================================================================== #cbar=plt.colorbar() def fmt(x, pos): a, b = '{:.1e}'.format(x).split('e') b = int(b) #return r'${} {}$'.format(a, b) return r'${} \times 10^{{{}}}$'.format(a, b) cbar = plt.colorbar(format=ticker.FuncFormatter(fmt), label=r'Intensity [$W/m^2$]') #cbar=plt.colorbar(format=ticker.ScalarFormatter(useMathText=True),label='Intensity $W/m^2$') #cbar.set_label(family=font) for label in cbar.ax.yaxis.get_ticklabels(): label.set_family('serif') plt.savefig("Intensity" + filename_base + ".png") plt.show()