Пример #1
0
                               bins=40)

    # format plot
    ax.set_axis_bgcolor('#eeeeee')
    yticks = ax.get_yticks()
    ax.set_yticks([])
    spfl.format_axes(ax)
    ax.spines['left'].set_visible(False)
    if c.name == 'toaster':
        #         ax.set_ylim([0, np.max(n)*1.1])
        title_x = 0.3
    else:
        #        ax.set_ylim([0, np.max(n)*1.2])
        title_x = 0.5

    ax.xaxis.set_major_locator(MaxNLocator(5, prune=None, integer=True))

    def fmt_xticks(mins, pos):
        mins = int(mins)
        hours = mins // 60
        mins = mins % 60
        return '{:02d}:{:02d}'.format(hours, mins)

    formatter = FuncFormatter(fmt_xticks)
    ax.xaxis.set_major_formatter(formatter)
    ax.set_title(c.get_long_name(), x=title_x, y=TITLE_Y, ha='center')

plt.subplots_adjust(hspace=0.5, wspace=0.3)
plt.savefig(LATEX_PDF_OUTPUT_FILENAME)
plt.show()
Пример #2
0
def populate_plane_wave_figure(figure, cycle_count, show_colorbar=False):
    figure.set_figheight(3.5)
    axes = figure.add_subplot(1, 1, 1)

    axes.set_xlim(0, total_width)
    axes.set_ylim(-0.5, height + 0.5)
    axes.set_aspect('equal')
    axes.set_yticks([])
    axes.set_frame_on(False)

    # Displacement
    grid_distance_plane = sound_field.distance_plane(
        sound_field.complex_plane(total_width, 1, centered=False),
        source_offset / total_width)
    grid_displacement_plane = sound_field.waveform_plane(
        grid_distance_plane, cycle_count=cycle_count) * displacement_factor
    offsets = np.arange(source_offset, total_width)
    air_offsets = offsets[1:]
    displaced_offsets = offsets + np.real(
        grid_displacement_plane[0, source_offset:])
    displaced_source = displaced_offsets[0]
    displaced_air = displaced_offsets[1:]

    # Column labels
    axes.set_xticks(displaced_air - 0.5)
    axes.xaxis.set_major_formatter(
        FuncFormatter(lambda x, pos: chr(ord('A') + pos)))
    axes.tick_params(bottom=False)

    # Source
    axes.vlines(source_offset, -1, height + 1, linestyle=':', color='green')
    if (np.abs(displaced_source - source_offset) > initial_line_limit):
        axes.vlines(displaced_source, -1, 0, color='green', linestyle=':')
        axes.vlines(displaced_source,
                    height,
                    height + 1,
                    color='green',
                    linestyle=':')
    axes.vlines(displaced_source, 0, height, color='green')

    # Vertical lines for grid
    axes.vlines(displaced_air, 0, height, color='grey')
    axes.vlines(displaced_air, -1, 0, color='grey', linestyle=':')
    axes.vlines(displaced_air, height, height + 1, color='grey', linestyle=':')

    # Horizontal lines for grid
    axes.hlines(np.arange(height + 1),
                displaced_source,
                displaced_air[-1],
                color='grey')
    axes.hlines(np.arange(height + 1),
                displaced_air[-1],
                total_width,
                color='grey',
                linestyle=':')

    # Pressure heat map
    pixel_plane = sound_field.complex_plane(total_width * 2, 1)
    pixel_distance_plane = sound_field.distance_plane(
        pixel_plane, source_offset / total_width) * (air_width /
                                                     (air_width - 1))
    scaled_pressure_amplitude = pressure_amplitude / scale_factor
    pixel_pressure_plane = sound_field.waveform_plane(
        pixel_distance_plane, cycle_count=cycle_count) * np.exp(
            -1j * np.pi / 2) * scaled_pressure_amplitude
    heatmap = sound_field_plot.populate_heatmap(
        axes,
        np.real(pixel_pressure_plane),
        vmin=-scaled_pressure_amplitude,
        vmax=scaled_pressure_amplitude,
        cmap=cm.bwr)
    heatmap.set_clip_path(
        Rectangle((displaced_source, 0),
                  displaced_air[-1] - displaced_source,
                  height,
                  transform=axes.transData))
    if show_colorbar:
        figure.colorbar(heatmap,
                        label='Sound pressure (Pa)',
                        orientation='horizontal')

    return (axes, displaced_source, displaced_air)
Пример #3
0
from MiscBin import q


def ff(x, pos=None):
    if x < -1:
        return "%.2f" % (10**x)
    elif x < 0:
        return "%.1f" % (10**x)
    elif 10**x == 8.5:
        return "%.1f" % (10**x)
    else:
        return "%i" % (10**x)


ax = pylab.subplot(111)
formatter = FuncFormatter(ff)

ax.hist(q.RemoveNaN(numpy.log10(numpy.array(zbestlist))), bins=15)

ax.xaxis.set_major_formatter(formatter)
ax.set_xticks([
    -2, -1,
    numpy.log10(0.3), 0,
    numpy.log10(2),
    numpy.log10(3),
    numpy.log10(4),
    numpy.log10(6),
    numpy.log10(12)
])
ax.set_xlim((numpy.log10(0.005), numpy.log10(13)))
ax.set_ylabel("Number")
Пример #4
0
    def _plot_txt_curve(self, stats, bench_stats=None, ax=None, **kwargs):
        def format_perc(x, pos):
            return '%.0f%%' % x

        if ax is None:
            ax = plt.gca()

        y_axis_formatter = FuncFormatter(format_perc)
        ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))

        # Strategy statistics
        # returns = stats["returns"]
        returns = stats["returns"]
        cum_returns = stats['cum_returns']
        tot_ret = cum_returns[-1] - 1.0
        cagr = perf.create_cagr(cum_returns, self.periods)
        sharpe = perf.create_sharpe_ratio(returns, self.periods)
        sortino = perf.create_sortino_ratio(returns, self.periods)
        dd, dd_max, dd_dur = perf.create_drawdowns(cum_returns)
        calmar = cagr / dd_max

        # Benchmark statistics
        if bench_stats is not None:
            bench_returns = bench_stats["returns"]
            bench_cum_returns = bench_stats['cum_returns']
            bench_tot_ret = bench_cum_returns[-1] - 1.0
            bench_cagr = perf.create_cagr(bench_cum_returns, self.periods)
            bench_sharpe = perf.create_sharpe_ratio(bench_returns,
                                                    self.periods)
            bench_sortino = perf.create_sortino_ratio(bench_returns,
                                                      self.periods)

            bench_dd, bench_dd_max, bench_dd_dur = perf.create_drawdowns(
                bench_cum_returns)
            bench_calmar = bench_cagr / bench_dd_max

        # Strategy Values
        ax.text(7.50,
                8.2,
                'Strategy',
                fontweight='bold',
                horizontalalignment='right',
                fontsize=8,
                color='red')

        ax.text(0.25, 6.9, 'Total Return', fontsize=8)
        ax.text(7.50,
                6.9,
                '{:.2%}'.format(tot_ret),
                fontweight='bold',
                horizontalalignment='right',
                fontsize=8)

        ax.text(0.25, 5.9, 'CAGR', fontsize=8)
        ax.text(7.50,
                5.9,
                '{:.2%}'.format(cagr),
                fontweight='bold',
                horizontalalignment='right',
                fontsize=8)

        ax.text(0.25, 4.9, 'Sharpe Ratio', fontsize=8)
        ax.text(7.50,
                4.9,
                '{:.2f}'.format(sharpe),
                fontweight='bold',
                horizontalalignment='right',
                fontsize=8)

        ax.text(0.25, 3.9, 'Sortino Ratio', fontsize=8)
        ax.text(7.50,
                3.9,
                '{:.2f}'.format(sortino),
                fontweight='bold',
                horizontalalignment='right',
                fontsize=8)

        ax.text(0.25, 2.9, 'Calmar Ratio', fontsize=8)
        ax.text(7.50,
                2.9,
                '{:.2f}'.format(calmar),
                fontweight='bold',
                horizontalalignment='right',
                fontsize=8)

        ax.text(0.25, 1.9, 'Annual Volatility', fontsize=8)
        ax.text(7.50,
                1.9,
                '{:.2%}'.format(returns.std() * np.sqrt(252)),
                fontweight='bold',
                horizontalalignment='right',
                fontsize=8)

        ax.text(0.25, 0.9, 'Max Daily Drawdown', fontsize=8)
        ax.text(7.50,
                0.9,
                '{:.2%}'.format(dd_max),
                color='red',
                fontweight='bold',
                horizontalalignment='right',
                fontsize=8)

        # ax.text(0.25, 0.9, 'Max Drawdown Duration (Days)', fontsize=8)
        # ax.text(7.50, 0.9, '{:.0f}'.format(dd_dur), fontweight='bold', horizontalalignment='right', fontsize=8)

        # Benchmark Values
        if bench_stats is not None:
            ax.text(10.0,
                    8.2,
                    'Benchmark',
                    fontweight='bold',
                    horizontalalignment='right',
                    fontsize=8,
                    color='gray')
            ax.text(10.0,
                    6.9,
                    '{:.2%}'.format(bench_tot_ret),
                    fontweight='bold',
                    horizontalalignment='right',
                    fontsize=8)
            ax.text(10.0,
                    5.9,
                    '{:.2%}'.format(bench_cagr),
                    fontweight='bold',
                    horizontalalignment='right',
                    fontsize=8)
            ax.text(10.0,
                    4.9,
                    '{:.2f}'.format(bench_sharpe),
                    fontweight='bold',
                    horizontalalignment='right',
                    fontsize=8)
            ax.text(10.0,
                    3.9,
                    '{:.2f}'.format(bench_sortino),
                    fontweight='bold',
                    horizontalalignment='right',
                    fontsize=8)
            ax.text(10.0,
                    2.9,
                    '{:.2f}'.format(bench_calmar),
                    fontweight='bold',
                    horizontalalignment='right',
                    fontsize=8)
            ax.text(10.0,
                    1.9,
                    '{:.2%}'.format(bench_returns.std() * np.sqrt(252)),
                    fontweight='bold',
                    horizontalalignment='right',
                    fontsize=8)
            ax.text(10.0,
                    0.9,
                    '{:.2%}'.format(bench_dd_max),
                    color='red',
                    fontweight='bold',
                    horizontalalignment='right',
                    fontsize=8)
            # ax.text(10.0, 0.9, '{:.0f}'.format(bench_dd_dur), fontweight='bold', horizontalalignment='right', fontsize=8)

        ax.set_title('Equity Curve', fontweight='bold')

        ax.grid(False)
        ax.spines['top'].set_linewidth(2.0)
        ax.spines['bottom'].set_linewidth(2.0)
        ax.spines['right'].set_visible(False)
        ax.spines['left'].set_visible(False)
        ax.get_yaxis().set_visible(False)
        ax.get_xaxis().set_visible(False)
        ax.set_ylabel('')
        ax.set_xlabel('')

        ax.axis([0, 10, 0, 10])
        return ax
    def show(self, with_controls: bool = True, verbose: bool = False):
        # graph things...
        self._main_fig, self._x_axis = plt.subplots()
        self._x_axis.set_xlabel('seconds')
        plt.title('Shot at [%s] %.01fg, %.02f%% TDS' %
                  (self.shot.shot_time, self.shot.bw, self.shot.tds))

        time_series = self.shot.elapsed

        self._flow_plt, = plt.plot(time_series,
                                   self.shot.flow,
                                   label='flow (ml/s)',
                                   color='blue',
                                   lw=3.5)
        plt.plot(time_series,
                 self.shot.pressure,
                 label='pressure (bar)',
                 color='green')

        # derived things
        if hasattr(
                self, '_erroneous_weight'
        ):  # only shown when weight curve is derived from raw scale values
            plt.plot(time_series,
                     self._erroneous_weight,
                     label='error weight (g/s)',
                     color='brown',
                     alpha=0.6,
                     linestyle='dashed')
        # tds_ratio = [(tw / w * 100) for tw, w in zip(self._tds_effect, self._smoothing(self.shot.weight, 21))]
        # plt.plot(time_series, tds_ratio, label='TDS ratio (%)')
        plt.plot(time_series,
                 self._gravimetric_water,
                 label='measured water (g/s)',
                 color='brown',
                 lw=2)

        self._error_line, = plt.plot(time_series, [0.0] * len(time_series),
                                     label='error (x10)',
                                     color='grey')
        error_data = self._error_series(self.shot.flow, self._stable_weight_t,
                                        10)
        self._error_line.set_ydata(
            error_data)  # little hack to clip large error values

        self._sugg_line = plt.axhline(self._corr_suggestion * 10,
                                      label='suggestion (x10)',
                                      color='pink',
                                      linestyle='dashed')
        self._corr_line = plt.axhline(10.0,
                                      label='correction (x10)',
                                      color='magenta',
                                      linestyle='dotted')

        if verbose:
            plt.plot(time_series,
                     self.shot.weight,
                     label='weight (g/s)',
                     color='orange',
                     linestyle='dashed')
            resistance, = plt.plot(time_series, [0.0] * len(time_series),
                                   label='resistance',
                                   color='yellow')
            resistance.set_ydata(self._resistance)
            plt.plot(time_series, [v * 10 for v in self._diffs],
                     label='difference (x10)',
                     color='red')
            plt.plot(time_series, [v * 10 for v in self._tds_effect],
                     label='TDS weight (g/s, x10)',
                     color='navy',
                     linestyle='dashed')

        self._window_fill = self._x_axis.axvspan(*self._initial_window,
                                                 ymin=0.0,
                                                 ymax=1.0,
                                                 alpha=0.15,
                                                 color='green')

        if with_controls:
            # sliders
            plt.subplots_adjust(right=0.85, bottom=0.18)
            # correction slider
            correction_ax = plt.axes([0.87, 0.18, 0.03, 0.65])
            correction_slider = Slider(correction_ax,
                                       'correction\nvalue',
                                       orientation='vertical',
                                       valinit=1.0,
                                       valmin=0.3,
                                       valmax=2.5,
                                       valstep=0.01)
            if eq_within(self.shot.current_calibration, 1.0):
                corr_value_fmt = FuncFormatter(lambda v, p: 'x%.02f' % v)
            else:
                corr_value_fmt = FuncFormatter(
                    lambda v, p: 'x%.03f\n(%.02f)' %
                    (self.shot.current_calibration * v, v))
            correction_slider._fmt = corr_value_fmt

            correction_slider.on_changed(
                partial(Analysis._update_flow, self, self.shot.flow))

            # +- buttons
            plus_button_x = plt.axes([0.92, 0.27, 0.035, 0.06])
            minus_button_x = plt.axes([0.92, 0.2, 0.035, 0.06])
            plus_button = Button(plus_button_x, '▲')
            minus_button = Button(minus_button_x, '▼')
            plus_button.on_clicked(lambda _: correction_slider.set_val(
                correction_slider.val + 0.01))
            minus_button.on_clicked(lambda _: correction_slider.set_val(
                correction_slider.val - 0.01))

            # window slider
            window_ax = plt.axes([0.16, 0.05, 0.66, 0.03])
            window_slider = RangeSlider(window_ax,
                                        'opt.\nwindow',
                                        valmin=0.0,
                                        valmax=time_series[-1],
                                        valstep=0.1)
            window_slider.set_val(self._initial_window)
            window_slider.valtext.set_visible(False)

            window_slider.on_changed(partial(Analysis._update_window, self))

        self._x_axis.legend()
        plt.show()
Пример #6
0
plt.xlabel('Policy value in euros per month')
plt.ylabel('Excess monthly rent downtown')
plt.grid(True)
plt.show()

# Create graphs - loss from toll in euro equivalent
plt.plot(df_sim['r_'], df_sim[['gain_level_10'] + ['gain_level_30'] + ['gain_level_50'] + ['gain_level_100']])
plt.xlabel('Agents income')
plt.ylabel('Gains / losses')
plt.grid(True)
plt.show()

# Create graphs - loss from toll as a share of income
df_sim = df_sim.rename(columns = {'gain_10': '1 € toll', 'gain_30': '3 € toll', 'gain_50': '5 € toll', 'gain_100': '10 € toll'})
axes = df_sim[['1 € toll'] + ['3 € toll'] + ['5 € toll'] + ['10 € toll']].plot()
axes.yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.1%}'.format(y)))
plt.xlabel('Agents income')
plt.ylabel('Welfare gains relative to no toll')
axes.legend(
    bbox_to_anchor = (1, 1),
        )
plt.grid(True)
plt.show()

# Plot share of each option - tolls
df_toll['n_d_toll'] = 100 * n_d
to_plot = df_toll[['n_t_toll', 'n_v_toll', 'n_d_toll']].plot.bar(stacked=True, width=1) 
to_plot.legend(
    bbox_to_anchor = (0.75, 0.928),
        )
Пример #7
0
 def show_statistics(self, variant):
     self.activate()
     # Determine variant-dependent formatting.
     if not self.page.y:
         self.display_message(_("No stats available."))
         return
     ticklabels_pos = lambda i, j, k: ["+%d" % x for x in range(i, j, k)]
     ticklabels_neg = lambda i, j, k: ["%d" % x for x in range(i, j, k)]
     if hasattr(self.page, "NEXT_WEEK") and \
            variant == self.page.NEXT_WEEK:
         xticks = list(range(1, 8, 1))
         xticklabels = ticklabels_pos(1, 8, 1)
         show_text_value = True
         linewidth = 1
     elif hasattr(self.page, "NEXT_MONTH") and \
         variant == self.page.NEXT_MONTH:
         xticks = [1] + list(range(5, 32, 5))
         xticklabels = ["+1"] + ticklabels_pos(5, 32, 5)
         show_text_value = False
         linewidth = 1
     elif hasattr(self.page, "NEXT_3_MONTHS") and \
         variant == self.page.NEXT_3_MONTHS:
         xticks = [1] + list(range(10, 92, 10))
         xticklabels = ["+1"] + ticklabels_pos(10, 92, 10)
         show_text_value = False
         linewidth = 1
     elif hasattr(self.page, "NEXT_6_MONTHS") and \
         variant == self.page.NEXT_6_MONTHS:
         xticks = [1] + list(range(30, 183, 30))
         xticklabels = ["+1"] + ticklabels_pos(30, 183, 30)
         show_text_value = False
         linewidth = 0
     elif hasattr(self.page, "NEXT_YEAR") and \
              variant == self.page.NEXT_YEAR:
         xticks = [1] + list(range(60, 365, 60))
         xticklabels = ["+1"] + ticklabels_pos(60, 365, 60)
         show_text_value = False
         linewidth = 0
     elif hasattr(self.page, "LAST_WEEK") and \
         variant == self.page.LAST_WEEK:
         xticks = list(range(-7, 1, 1))
         xticklabels = ticklabels_neg(-7, 1, 1)
         show_text_value = True
         linewidth = 1
     elif hasattr(self.page, "LAST_MONTH") and \
         variant == self.page.LAST_MONTH:
         xticks = list(range(-30, -4, 5)) + [0]
         xticklabels = ticklabels_neg(-30, -4, 5) + ["0"]
         show_text_value = False
         linewidth = 1
     elif hasattr(self.page, "LAST_3_MONTHS") and \
         variant == self.page.LAST_3_MONTHS:
         xticks = list(range(-90, -9, 10)) + [0]
         xticklabels = ticklabels_neg(-90, -9, 10) + ["0"]
         show_text_value = False
         linewidth = 1
     elif hasattr(self.page, "LAST_6_MONTHS") and \
         variant == self.page.LAST_6_MONTHS:
         xticks = list(range(-180, -19, 20)) + [0]
         xticklabels = ticklabels_neg(-180, -19, 20) + ["0"]
         show_text_value = False
         linewidth = 0
     elif hasattr(self.page, "LAST_YEAR") and \
         variant == self.page.LAST_YEAR:
         xticks = list(range(-360, -59, 60)) + [0]
         xticklabels = ticklabels_neg(-360, -59, 60) + ["0"]
         show_text_value = False
         linewidth = 0
     else:
         raise AttributeError("Invalid variant")
     # Plot data.
     self.axes.bar(self.page.x,
                   self.page.y,
                   width=1,
                   align="center",
                   linewidth=linewidth,
                   color=self.colour,
                   alpha=0.75,
                   edgecolor="black")
     self.axes.set_title(self.title)
     self.axes.set_xlabel(_("Days"))
     self.axes.set_xticks(xticks)
     self.axes.set_xticklabels(xticklabels)
     xmin, xmax = min(self.page.x), max(self.page.x)
     self.axes.set_xlim(xmin=xmin - 0.5, xmax=xmax + 0.5)
     # Quick and dirty fix to have retention plot always max out at 100%,
     # except for LAST_WEEK, when we need space to put the numbers.
     if variant == self.page.LAST_WEEK or not hasattr(self, "y_max"):
         self.axes.set_ylim(ymax=int(max(self.page.y) * 1.1) + 1)
     else:
         self.axes.set_ylim(ymax=self.y_max)
     from matplotlib.ticker import FuncFormatter
     self.axes.yaxis.set_major_formatter(FuncFormatter(self.integers_only))
     if show_text_value:
         self._add_numbers_to_bars()
Пример #8
0
def plot_log(args):

    formatter = lambda x, pos: f'{(x // 1000):g}k' if x >= 1000 else f'{x:g}'
    formatter = FuncFormatter(formatter)

    exps = expman.gather(args.run).filter(args.filter)
    exps = exps.sort(key=lambda exp: exp.params.category)

    with PdfPages(args.output) as pdf:
        for exp in tqdm(exps):
            category = exp.params.category
            train_log = exp.path_to(f'log_{category}.csv.gz')
            train_log = pd.read_csv(train_log)

            metric_log = exp.path_to(f'metrics_{category}.csv')
            metric_log = pd.read_csv(metric_log)

            best_recon_file = exp.path_to(f'best_recon_{category}.png')
            best_recon = plt.imread(best_recon_file)

            last_recon_file = exp.path_to(f'last_recon_{category}.png')
            last_recon = plt.imread(last_recon_file)

            # prepare figure
            zoom = 0.7
            fig = plt.figure(figsize=(20 * zoom, 8 * zoom))
            # gridspec and axes for plots
            gs = fig.add_gridspec(ncols=2,
                                  nrows=2,
                                  hspace=0.05,
                                  wspace=0.05,
                                  right=0.5)
            ax1 = fig.add_subplot(gs[0, 0])
            ax2 = fig.add_subplot(gs[0, 1])
            ax3 = fig.add_subplot(gs[1, 0], sharex=ax1)
            ax4 = fig.add_subplot(gs[1, 1], sharex=ax2)
            # gridspec and axes for preview images
            gs2 = fig.add_gridspec(ncols=1,
                                   nrows=2,
                                   hspace=0,
                                   wspace=0,
                                   left=0.55)
            ax5 = fig.add_subplot(gs2[:, 0])
            # ticklabels format
            ax1.xaxis.set_major_formatter(formatter)
            ax2.xaxis.set_major_formatter(formatter)
            ax3.xaxis.set_major_formatter(formatter)
            ax4.xaxis.set_major_formatter(formatter)
            ax5.axis('off')
            # minor ticks position
            ax3.xaxis.set_minor_locator(AutoMinorLocator())
            ax4.xaxis.set_minor_locator(AutoMinorLocator())
            # minor grid style
            ax1.grid(b=True, which='minor', linewidth=0.5)
            ax2.grid(b=True, which='minor', linewidth=0.5)
            ax3.grid(b=True, which='minor', linewidth=0.5)
            ax4.grid(b=True, which='minor', linewidth=0.5)
            # right y-axes
            ax2.yaxis.set_label_position("right")
            ax2.yaxis.tick_right()
            ax4.yaxis.set_label_position("right")
            ax4.yaxis.tick_right()

            # generator losses
            gen = [
                'generator_encoder_loss', 'images_reconstruction_loss',
                'latent_reconstruction_loss', 'generator_encoder_total_loss'
            ]
            train_log.plot(x='step', y=gen, logy='sym', ax=ax1)
            ax1.legend(loc='lower left', bbox_to_anchor=(0.0, 1.0))

            # discriminator losses
            dis = [
                'discriminator_loss', 'gradient_penalty_loss',
                'discriminator_total_loss'
            ]
            train_log.plot(x='step', y=dis, logy='sym', ax=ax3)
            ax3.legend(loc='upper left', bbox_to_anchor=(0.0, -0.2))

            # scores
            scores = ['real_score', 'fake_score']
            train_log.plot(x='step', y=scores, logy='sym', ax=ax2)
            ax2.legend(loc='lower right', bbox_to_anchor=(1.0, 1.0))

            # metrics
            metric_log.plot(x='step', y=['auc', 'balanced_accuracy'], ax=ax4)
            best_recon_step = train_log[(
                train_log.step %
                1000) == 0].images_reconstruction_loss.idxmin()
            best_recon_step = train_log.loc[best_recon_step, 'step']
            ax4.axvline(best_recon_step, color='black', lw=1)
            ax4.legend(loc='upper right', bbox_to_anchor=(1.0, -0.2))

            # best/last reconstruction
            hw = best_recon.shape[1] // 2
            recon = np.hstack((best_recon[:, :hw, :], last_recon[:, :hw, :]))
            ax5.imshow(recon)
            ax5.margins(0)

            # params
            params_str = exp.params.to_string()
            plt.figtext(0.08,
                        0.5,
                        params_str,
                        ha='right',
                        va='center',
                        family='monospace')

            # save sigle figure in exp folder
            log_pdf = exp.path_to(f'log_{category}.pdf')
            fig.savefig(log_pdf, bbox_inches='tight')

            # add as page in PDF
            pdf.savefig(fig, bbox_inches='tight')
            plt.close(fig)
Пример #9
0
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1, aspect=1)


def minor_tick(x, pos):
    if not x % 1.0:
        return ""
    return "%.2f" % x


ax.xaxis.set_major_locator(MultipleLocator(1.000))
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
ax.yaxis.set_major_locator(MultipleLocator(1.000))
ax.yaxis.set_minor_locator(AutoMinorLocator(4))
ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick))

ax.set_xlim(0, 4)
ax.set_ylim(0, 4)

ax.tick_params(which='major', width=1.0)
ax.tick_params(which='major', length=10)
ax.tick_params(which='minor', width=1.0, labelsize=10)
ax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25')

ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)

ax.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10)
ax.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal")
ax.plot(X,
        Y3,
Пример #10
0
def multi_corner_plot(chains, weights=None, axis_labels=None, chain_labels=None, fname = None, \
                      nbins=40, figsize = (7.,7.), truths = None,fontsize=20 , tickfontsize=15,\
                      nticks=4, linewidth=1., truthlinewidth=2., linecolors = None, truthcolor = 'k', \
                      markersize = 10, wspace=0.5, hspace=0.5):
    """
    Plot the results of several chains at once.

    Parameters
    ----------
    chains : tuple
        Tuple containing multiple MCMC chains. Each chain should have shape [n_samples,n_dim], although 
        n_samples need not be the same for each chain. n_dim must be equal for all of the chains and >= 2.
    axis_labels : array_like[ndim]
        Strings corresponding to axis labels.
    chain_labels: array_like[nchains]
        Strings corresponding to the MCMC chain labels.
    fname : str 
        The name of the file to save the figure to.
    nbins : int 
        The number of bins to use in each dimension for the histograms. If a single number is passed, then 
        the same number of bins is used for each chain. If a list [n1,n2,...] is passed, then ni bins is used 
        for chain i.
    figsize : tuple
        The height and width of the plot in inches.
    truths : array_like[ndim]
        A list of true values. These are marked on the 2D and 1D histograms. If None, none are added.
    fontsize : float
        The size font to use for axis labels.
    tickfontsize : float
        The size font to use for tick labels.
    nticks : int
        The number of ticks to use on each axis.
    linewidth: float
        The width of the lines surrounding the contours and histograms.
    truthlinewidth:
        The width of the dashed lines in 1D histograms at 'true' values.
    linecolors: list
        The color of the lines surrounding the contours and histograms.
    truthcolor: str
        The color of the marker at the 'true' values in the 2D subplots and the dashed line in the 1D subplots.
    markersize: float
        The size of the marker to put on the 2D subplots for the 'true' value.
    wspace : float
        The amount of whitespace to place vertically between subplots.
    hspace : float
        The amount of whitespace to place horizontally between subplots.
    """

    major_formatter = FuncFormatter(my_formatter)

    if weights is None:
        weights = (None, ) * len(chains)

    try:
        assert len(nbins) == len(chains)
    except:
        nbins = (np.ones(len(chains)) * nbins).astype(int)

    if linecolors is None:
        linecolors = [
            list(mpl.rcParams['axes.prop_cycle'])[i]['color']
            for i in np.arange(len(chains))
        ]

    if len(set([c.shape[1] for c in chains])) != 1:
        raise Exception("Each chain must have the same dimension.")

    traces_list = [c.T for c in chains]

    if axis_labels is None:
        axis_labels = [''] * len(traces_list[0])

    if len(traces_list[0]) != len(axis_labels):
        raise Exception(
            "There must be the same number of axis labels as traces")

    if len(linecolors) != len(traces_list):
        raise Exception(
            "Provided list of colors must match the number of chains")

    if truths != None and (len(truths) != len(traces_list[0])):
        raise Exception(
            "There must be the same number of true values as traces")

    n_traces = len(traces_list[0])

    #Set up the figure
    fig = plt.figure(num=None, figsize=figsize)
    gs = gridspec.GridSpec(2 * n_traces, 2 * n_traces)
    gs.update(wspace=wspace, hspace=hspace)

    #Create axes for 2D histograms
    hist_2d_axes = {}
    for x_pos in xrange(n_traces - 1):
        for y_pos in range(n_traces - 1 - x_pos):
            x_var = x_pos
            y_var = n_traces - 1 - y_pos

            hist_2d_axes[(x_var, y_var)] = fig.add_subplot( \
                                           gs[ -1-(2*y_pos):-1-(2*y_pos), \
                                               2*x_pos:(2*x_pos+2) ] )
            hist_2d_axes[(x_var,
                          y_var)].xaxis.set_major_formatter(major_formatter)
            hist_2d_axes[(x_var,
                          y_var)].yaxis.set_major_formatter(major_formatter)

    #Create axes for 1D histograms
    hist_1d_axes = {}
    for var in xrange(n_traces - 1):
        hist_1d_axes[var] = fig.add_subplot(gs[(2 * var):(2 * var + 2),
                                               2 * var:(2 * var + 2)])
        hist_1d_axes[var].xaxis.set_major_formatter(major_formatter)
        hist_1d_axes[var].yaxis.set_major_formatter(major_formatter)
    hist_1d_axes[n_traces - 1] = fig.add_subplot(gs[-2:, -2:])
    hist_1d_axes[n_traces - 1].xaxis.set_major_formatter(major_formatter)
    hist_1d_axes[n_traces - 1].yaxis.set_major_formatter(major_formatter)

    for_legend = [None] * len(chains)

    #Remove the ticks from the axes which don't need them
    for x_var in xrange(n_traces - 1):
        for y_var in xrange(1, n_traces - 1):
            try:
                hist_2d_axes[(x_var, y_var)].xaxis.set_visible(False)
            except KeyError:
                continue
    for var in xrange(n_traces - 1):
        hist_1d_axes[var].set_xticklabels([])
        hist_1d_axes[var].xaxis.set_major_locator(MaxNLocator(nticks))
        hist_1d_axes[var].yaxis.set_visible(False)

    for y_var in xrange(1, n_traces):
        for x_var in xrange(1, n_traces - 1):
            try:
                hist_2d_axes[(x_var, y_var)].yaxis.set_visible(False)
            except KeyError:
                continue

    for z, traces in enumerate(traces_list):
        #Do the plotting
        #Firstly make the 1D histograms
        num_samples = min([len(trace) for trace in traces])
        vals, walls = np.histogram(traces[-1][:num_samples],
                                   bins=nbins[z],
                                   weights=weights[z],
                                   normed=True)

        xplot = np.zeros(nbins[z] * 2 + 2)
        yplot = np.zeros(nbins[z] * 2 + 2)
        for i in xrange(1, nbins[z] * 2 + 1):
            xplot[i] = walls[int((i - 1) / 2)]
            yplot[i] = vals[int((i - 2) / 2)]

        xplot[0] = walls[0]
        xplot[-1] = walls[-1]
        yplot[0] = yplot[1]
        yplot[-1] = yplot[-2]

        #this one's special, so do it on it's own
        for_legend[z] = hist_1d_axes[n_traces - 1].plot(xplot,
                                                        yplot,
                                                        color=linecolors[z],
                                                        lw=linewidth)
        if z == 0: hist_1d_axes[n_traces - 1].set_xlim(walls[0], walls[-1])
        xlo, xhi = hist_1d_axes[n_traces - 1].get_xlim()
        if walls[0] < xlo:
            xlo = walls[0]
        if walls[-1] > xhi:
            xhi = walls[-1]
        hist_1d_axes[n_traces - 1].set_xlim(xlo, xhi)
        if truths is not None:
            if truths[n_traces - 1] < xlo:
                dx = xlo - truths[n_traces - 1]
                hist_1d_axes[n_traces - 1].set_xlim(
                    (xlo - dx - 0.05 * (xhi - xlo), xhi))
            elif truths[n_traces - 1] > xhi:
                dx = truths[n_traces - 1] - xhi
                hist_1d_axes[n_traces - 1].set_xlim(
                    (xlo, xhi + dx + 0.05 * (xhi - xlo)))
            if z == 0:
                hist_1d_axes[n_traces - 1].axvline(truths[n_traces - 1],ls='--',c=truthcolor,\
                    lw=truthlinewidth,zorder=100)

        #Now Make the 2D histograms
        for x_var in xrange(n_traces):
            for y_var in xrange(n_traces):
                try:
                    H, y_edges, x_edges = np.histogram2d( traces[y_var][:num_samples], traces[x_var][:num_samples],\
                                                               weights=weights[z], bins = nbins[z] )
                    confidence_2d(traces[x_var][:num_samples],traces[y_var][:num_samples],weights=weights[z],\
                        ax=hist_2d_axes[(x_var,y_var)],nbins=nbins[z],intervals=None,linecolor=linecolors[z],\
                        filled=False,cmap="Blues",linewidth=linewidth, gradient=False,scatter=False,scatter_color='k', \
                        scatter_alpha=0., scatter_size=0.1)
                    if z == 0:
                        hist_2d_axes[(x_var,
                                      y_var)].set_xlim(x_edges[0], x_edges[-1])
                        hist_2d_axes[(x_var,
                                      y_var)].set_ylim(y_edges[0], y_edges[-1])
                    xlo, xhi = hist_2d_axes[(x_var, y_var)].get_xlim()
                    if x_edges[0] < xlo:
                        xlo = x_edges[0]
                    if x_edges[-1] > xhi:
                        xhi = x_edges[-1]
                    hist_2d_axes[(x_var, y_var)].set_xlim(xlo, xhi)
                    ylo, yhi = hist_2d_axes[(x_var, y_var)].get_ylim()
                    if y_edges[0] < ylo:
                        ylo = y_edges[0]
                    if y_edges[-1] > yhi:
                        yhi = y_edges[-1]
                    hist_2d_axes[(x_var, y_var)].set_ylim(ylo, yhi)
                    if truths is not None:
                        xlo, xhi = hist_2d_axes[(x_var, y_var)].get_xlim()
                        ylo, yhi = hist_2d_axes[(x_var, y_var)].get_ylim()
                        if truths[x_var] < xlo:
                            dx = xlo - truths[x_var]
                            hist_2d_axes[(x_var, y_var)].set_xlim(
                                (xlo - dx - 0.05 * (xhi - xlo), xhi))
                        elif truths[x_var] > xhi:
                            dx = truths[x_var] - xhi
                            hist_2d_axes[(x_var, y_var)].set_xlim(
                                (xlo, xhi + dx + 0.05 * (xhi - xlo)))
                        if truths[y_var] < ylo:
                            dy = ylo - truths[y_var]
                            hist_2d_axes[(x_var, y_var)].set_ylim(
                                (ylo - dy - 0.05 * (yhi - ylo), yhi))
                        elif truths[y_var] > yhi:
                            dy = truths[y_var] - yhi
                            hist_2d_axes[(x_var, y_var)].set_ylim(
                                (ylo, yhi + dy + 0.05 * (yhi - ylo)))
                        if z == 0:
                            hist_2d_axes[(x_var,y_var)].plot( truths[x_var], truths[y_var], '*', \
                                color = truthcolor, markersize = markersize, \
                                markeredgecolor = 'none',zorder=100)
                except KeyError:
                    pass
            if x_var < n_traces - 1:
                vals, walls = np.histogram(traces[x_var][:num_samples],
                                           bins=nbins[z],
                                           weights=weights[z],
                                           normed=True)

                xplot = np.zeros(nbins[z] * 2 + 2)
                yplot = np.zeros(nbins[z] * 2 + 2)
                for i in xrange(1, nbins[z] * 2 + 1):
                    xplot[i] = walls[int((i - 1) / 2)]
                    yplot[i] = vals[int((i - 2) / 2)]

                xplot[0] = walls[0]
                xplot[-1] = walls[-1]
                yplot[0] = yplot[1]
                yplot[-1] = yplot[-2]

                hist_1d_axes[x_var].plot(xplot,
                                         yplot,
                                         color=linecolors[z],
                                         lw=linewidth)
                if z == 0: hist_1d_axes[x_var].set_xlim(walls[0], walls[-1])
                xlo, xhi = hist_1d_axes[x_var].get_xlim()
                if x_edges[0] < xlo:
                    xlo = x_edges[0]
                if x_edges[-1] > xhi:
                    xhi = x_edges[-1]
                hist_1d_axes[x_var].set_xlim(xlo, xhi)
                if truths is not None:
                    xlo, xhi = hist_1d_axes[x_var].get_xlim()
                    if truths[x_var] < xlo:
                        dx = xlo - truths[x_var]
                        hist_1d_axes[x_var].set_xlim(
                            (xlo - dx - 0.05 * (xhi - xlo), xhi))
                    elif truths[x_var] > xhi:
                        dx = truths[x_var] - xhi
                        hist_1d_axes[x_var].set_xlim(
                            (xlo, xhi + dx + 0.05 * (xhi - xlo)))
                    if z == 0:
                        hist_1d_axes[x_var].axvline(truths[x_var],
                                                    ls='--',
                                                    c=truthcolor,
                                                    lw=truthlinewidth,
                                                    zorder=100)

    #Finally Add the Axis Labels
    for x_var in xrange(n_traces - 1):
        hist_2d_axes[(x_var, n_traces - 1)].set_xlabel(axis_labels[x_var],
                                                       fontsize=fontsize)
        hist_2d_axes[(x_var, n_traces - 1)].tick_params(labelsize=tickfontsize)
        hist_2d_axes[(x_var, n_traces - 1)].xaxis.set_major_locator(
            MaxNLocator(nticks))
        plt.setp(hist_2d_axes[(x_var,
                               n_traces - 1)].xaxis.get_majorticklabels(),
                 rotation=45)
    for y_var in xrange(1, n_traces):
        hist_2d_axes[(0, y_var)].set_ylabel(axis_labels[y_var],
                                            fontsize=fontsize)
        hist_2d_axes[(0, y_var)].tick_params(labelsize=tickfontsize)
        plt.setp(hist_2d_axes[(0, y_var)].yaxis.get_majorticklabels(),
                 rotation=45)
        hist_2d_axes[(0, y_var)].yaxis.set_major_locator(MaxNLocator(nticks))
    hist_1d_axes[n_traces - 1].set_xlabel(axis_labels[-1], fontsize=fontsize)
    hist_1d_axes[n_traces - 1].tick_params(labelsize=tickfontsize)
    hist_1d_axes[n_traces - 1].xaxis.set_major_locator(MaxNLocator(nticks))
    hist_1d_axes[n_traces - 1].yaxis.set_visible(False)
    plt.setp(hist_1d_axes[n_traces - 1].xaxis.get_majorticklabels(),
             rotation=45)

    plt.gcf().subplots_adjust(
        bottom=0.15)  #make sure nothing is getting chopped off

    if chain_labels is not None:
        for_legend = [f[0] for f in for_legend]
        fig.legend(tuple(for_legend),
                   tuple(chain_labels), (0.6, 0.7),
                   fontsize=fontsize)

    if fname != None:
        if len(fname.split('.')) == 1:
            fname += '.pdf'
        plt.savefig(fname, transparent=True)

    return None
Пример #11
0
def corner_plot( chain, weights=None, axis_labels=None,  print_values=True, fname = None, nbins=40, \
                 figsize = (7.,7.), filled=True, gradient=False, cmap="Blues", truths = None,\
                 fontsize=20 , tickfontsize=15, nticks=4, linewidth=1., truthlinewidth=2., linecolor = 'k', \
                 markercolor = 'k', markersize = 10, wspace=0.5, hspace=0.5, scatter=False, scatter_size=2., \
                 scatter_color='k', scatter_alpha=0.5):
    """
    Make a corner plot from MCMC output.
    Parameters
    ----------
    chain : array_like[nsamples, ndim]
        Samples from an MCMC chain. ndim should be >= 2.
    weights: array_like[nsamples]
        Weights to be applied to the samples (if e.g. nested sampling has been used to obtain the samples)
    axis_labels : array_like[ndim]
        Strings corresponding to axis labels.
    print_values:
        If True, print median values from 1D posteriors and +/- uncertainties 
        from the 84th and 16th percentiles above the 1D histograms.
    fname : str 
        The name of the file to save the figure to.
    nbins : int 
        The number of bins to use in each dimension for the histograms.
    figsize : tuple
        The height and width of the plot in inches.
    filled : bool
        If True, the histograms and contours will be filled.
    gradient: bool
        If True, then instead of filled contours, bicubic interpolation is applied to the 2D histograms (appropriate 
            when your posterior is densely sampled).
    cmap : str
        Name of the colormap to use.
    truths : array_like[ndim]
        A list of true values. These are marked on the 2D and 1D histograms. If None, none are added.
    fontsize : float
        The size font to use for axis labels.
    tickfontsize : float
        The size font to use for tick labels.
    nticks : int
        The number of ticks to use on each axis.
    linewidth: float
        The width of the lines surrounding the contours and histograms.
    truthlinewidth:
        The width of the dashed lines in 1D histograms at 'true' values.
    linecolor: str
        The color of the lines surrounding the contours and histograms.
    markercolor: str
        The color of the marker at the 'true' values in the 2D subplots.
    markersize: float
        The size of the marker to put on the 2D subplots.
    wspace : float
        The amount of whitespace to place vertically between subplots.
    hspace : float
        The amount of whitespace to place horizontally between subplots.
    scatter: bool
        If true, do scatter plots instead of contour plots in the 2D projections.
    scatter_size: float
        The size of the points in the scatter plot.
    scatter_color: str 
        The color of the points in the scatter plot. 
    scatter_alpha: float 
        The alpha value for the scatter points. 
    """

    major_formatter = FuncFormatter(my_formatter)

    if weights is not None:
        print_values = False  #current method for extracting results won't work for that

    if print_values is True:
        res = chain_results(chain)

    traces = chain.T

    if axis_labels is None:
        axis_labels = [''] * len(traces)

    if len(traces) != len(axis_labels):
        print("There must be the same number of axis labels as traces",
              file=sys.stderr)

    if truths != None and (len(truths) != len(traces)):
        print("There must be the same number of true values as traces",
              file=sys.stderr)

    num_samples = min([len(trace) for trace in traces])
    n_traces = len(traces)

    #Set up the figure
    fig = plt.figure(num=None, figsize=figsize)

    dim = 2 * n_traces - 1

    gs = gridspec.GridSpec(dim + 1, dim + 1)
    gs.update(wspace=wspace, hspace=hspace)

    hist_2d_axes = {}

    #Create axes for 2D histograms
    for x_pos in xrange(n_traces - 1):
        for y_pos in range(n_traces - 1 - x_pos):
            x_var = x_pos
            y_var = n_traces - 1 - y_pos

            hist_2d_axes[(x_var, y_var)] = fig.add_subplot( \
                                           gs[ -1-(2*y_pos):-1-(2*y_pos), \
                                               2*x_pos:(2*x_pos+2) ] )
            hist_2d_axes[(x_var,
                          y_var)].xaxis.set_major_formatter(major_formatter)
            hist_2d_axes[(x_var,
                          y_var)].yaxis.set_major_formatter(major_formatter)

    #Create axes for 1D histograms
    hist_1d_axes = {}
    for var in xrange(n_traces - 1):
        hist_1d_axes[var] = fig.add_subplot(gs[(2 * var):(2 * var + 2),
                                               2 * var:(2 * var + 2)])
        hist_1d_axes[var].xaxis.set_major_formatter(major_formatter)
        hist_1d_axes[var].yaxis.set_major_formatter(major_formatter)
    hist_1d_axes[n_traces - 1] = fig.add_subplot(gs[-2:, -2:])
    hist_1d_axes[n_traces - 1].xaxis.set_major_formatter(major_formatter)
    hist_1d_axes[n_traces - 1].yaxis.set_major_formatter(major_formatter)

    #Remove the ticks from the axes which don't need them
    for x_var in xrange(n_traces - 1):
        for y_var in xrange(1, n_traces - 1):
            try:
                hist_2d_axes[(x_var, y_var)].xaxis.set_visible(False)
            except KeyError:
                continue
    for var in xrange(n_traces - 1):
        hist_1d_axes[var].set_xticklabels([])
        hist_1d_axes[var].xaxis.set_major_locator(MaxNLocator(nticks))
        hist_1d_axes[var].yaxis.set_visible(False)

    for y_var in xrange(1, n_traces):
        for x_var in xrange(1, n_traces - 1):
            try:
                hist_2d_axes[(x_var, y_var)].yaxis.set_visible(False)
            except KeyError:
                continue

    #Do the plotting
    #Firstly make the 1D histograms
    vals, walls = np.histogram(traces[-1][:num_samples],
                               bins=nbins,
                               weights=weights,
                               normed=True)

    xplot = np.zeros(nbins * 2 + 2)
    yplot = np.zeros(nbins * 2 + 2)
    for i in xrange(1, nbins * 2 + 1):
        xplot[i] = walls[int((i - 1) / 2)]
        yplot[i] = vals[int((i - 2) / 2)]

    xplot[0] = walls[0]
    xplot[-1] = walls[-1]
    yplot[0] = yplot[1]
    yplot[-1] = yplot[-2]

    if not scatter:
        Cmap = colors.Colormap(cmap)
        cNorm = colors.Normalize(vmin=0., vmax=1.)
        scalarMap = cm.ScalarMappable(norm=cNorm, cmap=cmap)
        cVal = scalarMap.to_rgba(0.65)
    else:
        cVal = scatter_color

    #this one's special, so do it on it's own
    if print_values is True:
        hist_1d_axes[n_traces - 1].set_title("${0:.2f}^{{ +{1:.2f} }}_{{ -{2:.2f} }}$".\
            format(res[n_traces - 1][0],res[n_traces - 1][1],res[n_traces - 1][2]),fontsize=fontsize)
    hist_1d_axes[n_traces - 1].plot(xplot,
                                    yplot,
                                    color=linecolor,
                                    lw=linewidth)
    if filled:
        hist_1d_axes[n_traces - 1].fill_between(xplot, yplot, color=cVal)
    hist_1d_axes[n_traces - 1].set_xlim(walls[0], walls[-1])
    hist_1d_axes[n_traces - 1].set_xlabel(axis_labels[-1], fontsize=fontsize)
    hist_1d_axes[n_traces - 1].tick_params(labelsize=tickfontsize)
    hist_1d_axes[n_traces - 1].xaxis.set_major_locator(MaxNLocator(nticks))
    hist_1d_axes[n_traces - 1].yaxis.set_visible(False)
    plt.setp(hist_1d_axes[n_traces - 1].xaxis.get_majorticklabels(),
             rotation=45)
    if truths is not None:
        xlo, xhi = hist_1d_axes[n_traces - 1].get_xlim()
        if truths[n_traces - 1] < xlo:
            dx = xlo - truths[n_traces - 1]
            hist_1d_axes[n_traces - 1].set_xlim(
                (xlo - dx - 0.05 * (xhi - xlo), xhi))
        elif truths[n_traces - 1] > xhi:
            dx = truths[n_traces - 1] - xhi
            hist_1d_axes[n_traces - 1].set_xlim(
                (xlo, xhi + dx + 0.05 * (xhi - xlo)))
        hist_1d_axes[n_traces - 1].axvline(truths[n_traces - 1],
                                           ls='--',
                                           c='k',
                                           lw=truthlinewidth)

    #Now Make the 2D histograms
    for x_var in xrange(n_traces):
        for y_var in xrange(n_traces):
            try:
                H, y_edges, x_edges = np.histogram2d( traces[y_var][:num_samples], traces[x_var][:num_samples],\
                                                           weights=weights, bins = nbins )
                confidence_2d(traces[x_var][:num_samples],traces[y_var][:num_samples],weights=weights,\
                    ax=hist_2d_axes[(x_var,y_var)],nbins=nbins,intervals=None,linecolor=linecolor,\
                    filled=filled,cmap=cmap,linewidth=linewidth, gradient=gradient,scatter=scatter,\
                    scatter_color=scatter_color, scatter_alpha=scatter_alpha, scatter_size=scatter_size)
                hist_2d_axes[(x_var, y_var)].set_xlim(x_edges[0], x_edges[-1])
                hist_2d_axes[(x_var, y_var)].set_ylim(y_edges[0], y_edges[-1])
                if truths is not None:
                    xlo, xhi = hist_2d_axes[(x_var, y_var)].get_xlim()
                    ylo, yhi = hist_2d_axes[(x_var, y_var)].get_ylim()
                    if truths[x_var] < xlo:
                        dx = xlo - truths[x_var]
                        hist_2d_axes[(x_var, y_var)].set_xlim(
                            (xlo - dx - 0.05 * (xhi - xlo), xhi))
                    elif truths[x_var] > xhi:
                        dx = truths[x_var] - xhi
                        hist_2d_axes[(x_var, y_var)].set_xlim(
                            (xlo, xhi + dx + 0.05 * (xhi - xlo)))
                    if truths[y_var] < ylo:
                        dy = ylo - truths[y_var]
                        hist_2d_axes[(x_var, y_var)].set_ylim(
                            (ylo - dy - 0.05 * (yhi - ylo), yhi))
                    elif truths[y_var] > yhi:
                        dy = truths[y_var] - yhi
                        hist_2d_axes[(x_var, y_var)].set_ylim(
                            (ylo, yhi + dy + 0.05 * (yhi - ylo)))
                    #TODO: deal with the pesky case of a prior edge
                    hist_2d_axes[(x_var, y_var)].set_axis_bgcolor(
                        scalarMap.to_rgba(0.))  #so that the contours blend
                    hist_2d_axes[(x_var,y_var)].plot( truths[x_var], truths[y_var], '*',\
                                 color = markercolor, markersize = markersize, \
                                 markeredgecolor = 'none')
            except KeyError:
                pass
        if x_var < n_traces - 1:
            vals, walls = np.histogram(traces[x_var][:num_samples],
                                       bins=nbins,
                                       weights=weights,
                                       normed=True)

            xplot = np.zeros(nbins * 2 + 2)
            yplot = np.zeros(nbins * 2 + 2)
            for i in xrange(1, nbins * 2 + 1):
                xplot[i] = walls[int((i - 1) / 2)]
                yplot[i] = vals[int((i - 2) / 2)]

            xplot[0] = walls[0]
            xplot[-1] = walls[-1]
            yplot[0] = yplot[1]
            yplot[-1] = yplot[-2]

            if print_values is True:
                hist_1d_axes[x_var].set_title("${0:.2f}^{{ +{1:.2f} }}_{{ -{2:.2f} }}$".\
                                format(res[x_var][0],res[x_var][1],res[x_var][2]),fontsize=fontsize)
            hist_1d_axes[x_var].plot(xplot,
                                     yplot,
                                     color=linecolor,
                                     lw=linewidth)
            if filled:
                hist_1d_axes[x_var].fill_between(xplot, yplot, color=cVal)
            hist_1d_axes[x_var].set_xlim(x_edges[0], x_edges[-1])
            if truths is not None:
                xlo, xhi = hist_1d_axes[x_var].get_xlim()
                if truths[x_var] < xlo:
                    dx = xlo - truths[x_var]
                    hist_1d_axes[x_var].set_xlim(
                        (xlo - dx - 0.05 * (xhi - xlo), xhi))
                elif truths[x_var] > xhi:
                    dx = truths[x_var] - xhi
                    hist_1d_axes[x_var].set_xlim(
                        (xlo, xhi + dx + 0.05 * (xhi - xlo)))
                hist_1d_axes[x_var].axvline(truths[x_var],
                                            ls='--',
                                            c='k',
                                            lw=truthlinewidth)

    #Finally Add the Axis Labels
    for x_var in xrange(n_traces - 1):
        hist_2d_axes[(x_var, n_traces - 1)].set_xlabel(axis_labels[x_var],
                                                       fontsize=fontsize)
        hist_2d_axes[(x_var, n_traces - 1)].tick_params(labelsize=tickfontsize)
        hist_2d_axes[(x_var, n_traces - 1)].xaxis.set_major_locator(
            MaxNLocator(nticks))
        plt.setp(hist_2d_axes[(x_var,
                               n_traces - 1)].xaxis.get_majorticklabels(),
                 rotation=45)
    for y_var in xrange(1, n_traces):
        hist_2d_axes[(0, y_var)].set_ylabel(axis_labels[y_var],
                                            fontsize=fontsize)
        hist_2d_axes[(0, y_var)].tick_params(labelsize=tickfontsize)
        plt.setp(hist_2d_axes[(0, y_var)].yaxis.get_majorticklabels(),
                 rotation=45)
        hist_2d_axes[(0, y_var)].yaxis.set_major_locator(MaxNLocator(nticks))

    plt.gcf().subplots_adjust(
        bottom=0.15)  #make sure nothing is getting chopped off

    if fname != None:
        if len(fname.split('.')) == 1:
            fname += '.pdf'
        plt.savefig(fname, transparent=True)

    return None
Пример #12
0
from keras.layers import Dense, Input


def weierstrass(x, n, step=0.0001):
    a = 0.5
    b = 12.6
    intervalBegin = 0
    intervalEnd = 1
    data = []
    for k in range(int((1 / step) * abs(intervalEnd - intervalBegin))):
        output = 0
        for i in range(n):
            output += pow(a, i) * sin(pow(b, i) * i * k * step)
        data.append(output)

    return np.array(data)


plot_num = 10000
x = np.linspace(0, 1, num=plot_num)
#colorlist = ['r', 'b', 'g']

#for n, color in enumerate(colorlist):
y0 = weierstrass(x=x, n=3, step=1/plot_num)
rcParams['figure.figsize'] = 16, 9
plt.gca().xaxis.grid(True)
plt.gca().yaxis.grid(True)
plt.gca().get_xaxis().set_major_formatter(FuncFormatter(lambda x, p: format(x * 1/plot_num, ',')))
plt.plot(y0, color='r')

plt.show()
Пример #13
0
def create_figure(feature_name, weighted_sum, weight_total, report_dir,
                  min_total, filetype):
    assert filetype in ('png', 'pdf')

    mean_by_head = weighted_sum / weight_total
    exclude_mask = np.array(weight_total) < min_total

    masked_mean_by_head = np.ma.masked_array(mean_by_head, mask=exclude_mask)
    layer_macro = masked_mean_by_head.mean(-1)

    plt.figure(figsize=(3, 2.2))
    ax1 = plt.subplot2grid((100, 85), (0, 0), colspan=65, rowspan=99)
    ax2 = plt.subplot2grid((100, 85), (12, 70), colspan=15, rowspan=75)
    heatmap = sns.heatmap(
        (mean_by_head * 100).tolist(),
        center=0.0,
        ax=ax1,
        square=True,
        cbar=False,
        linewidth=0.1,
        linecolor='#D0D0D0',
        cmap=LinearSegmentedColormap.from_list('rg',
                                               ["#F14100", "white", "#3ED134"],
                                               N=256),
        mask=exclude_mask,
        xticklabels=['', '2', '', '4', '', '6', '', '8', '', '10', '', '12'],
        yticklabels=['', '2', '', '4', '', '6', '', '8', '', '10', '', '12'])

    for _, spine in heatmap.spines.items():
        spine.set_visible(True)
    plt.setp(heatmap.get_yticklabels(), fontsize=7)
    plt.setp(heatmap.get_xticklabels(), fontsize=7)
    heatmap.tick_params(axis='x', pad=1, length=2)
    heatmap.tick_params(axis='y', pad=1, length=2)
    heatmap.yaxis.labelpad = 2
    heatmap.invert_yaxis()
    heatmap.set_facecolor('#E7E6E6')
    # split axes of heatmap to put colorbar
    ax_divider = make_axes_locatable(ax1)
    cax = ax_divider.append_axes('left', size='7%', pad='33%')
    # # make colorbar for heatmap.
    # # Heatmap returns an axes obj but you need to get a mappable obj (get_children)
    cbar = colorbar(ax1.get_children()[0],
                    cax=cax,
                    orientation='vertical',
                    format='%.0f%%')
    cax.yaxis.set_ticks_position('left')
    cbar.solids.set_edgecolor("face")
    cbar.ax.tick_params(labelsize=7, length=4, pad=2)
    ax1.set_title('% Attention', size=9)
    ax1.set_xlabel('Head', size=8)
    ax1.set_ylabel('Layer', size=8)
    for _, spine in ax1.spines.items():
        spine.set_visible(True)
    ax2.set_title('      Layer Avg.', size=9)
    # bp = sns.barplot(x=layer_macro * 100, ax=ax2, y=list(range(layer_macro.shape[0])), color="#3D4FC4", orient="h")
    bp = sns.barplot(x=layer_macro * 100,
                     ax=ax2,
                     y=list(range(layer_macro.shape[0])),
                     color="#556FAB",
                     orient="h")
    formatter = FuncFormatter(lambda y, pos: '0' if (y == 0) else "%d%%" % (y))
    ax2.xaxis.set_major_formatter(formatter)
    plt.setp(bp.get_xticklabels(), fontsize=7)
    bp.tick_params(axis='x', pad=1, length=3)
    ax2.invert_yaxis()
    ax2.set_yticklabels([])
    ax2.spines['top'].set_visible(False)
    ax2.spines['right'].set_visible(False)
    ax2.spines['left'].set_visible(False)
    ax2.xaxis.set_ticks_position('bottom')
    ax2.axvline(0, linewidth=.85, color='black')
    fname = report_dir / to_filename(feature_name, filetype)
    print('Saving', fname)
    plt.savefig(fname, format=filetype)
    plt.close()
Пример #14
0
def plot_series(*series, labels=None):
    """Plot one or more time series

    Parameters
    ----------
    series : pd.Series
        One or more time series
    labels : list, optional (default=None)
        Names of series, will be displayed in figure legend

    Returns
    -------
    fig : plt.Figure
    ax : plt.Axis
    """
    _check_soft_dependencies("matplotlib", "seaborn")
    import matplotlib.pyplot as plt
    from matplotlib.ticker import FuncFormatter, MaxNLocator
    from matplotlib.cbook import flatten
    import seaborn as sns

    n_series = len(series)
    if labels is not None:
        if n_series != len(labels):
            raise ValueError(
                "There must be one label for each time series, "
                "but found inconsistent numbers of series and "
                "labels."
            )
        legend = True
    else:
        labels = ["" for _ in range(n_series)]
        legend = False

    for y in series:
        check_y(y)

    # create combined index
    index = series[0].index
    for y in series[1:]:
        # check types, note that isinstance() does not work here because index
        # types inherit from each other, hence we check for type equality
        if not type(index) is type(y.index):  # noqa
            raise TypeError("Found series with different index types.")
        index = index.union(y.index)

    # generate integer x-values
    xs = [np.argwhere(index.isin(y.index)).ravel() for y in series]

    # create figure
    fig, ax = plt.subplots(1, figsize=plt.figaspect(0.25))
    colors = sns.color_palette("colorblind", n_colors=n_series)

    # plot series
    for x, y, color, label in zip(xs, series, colors, labels):

        # scatter if little data is available or index is not complete
        if len(x) <= 3 or not np.array_equal(np.arange(x[0], x[-1] + 1), x):
            plot_func = sns.scatterplot
        else:
            plot_func = sns.lineplot

        plot_func(x=x, y=y, ax=ax, marker="o", label=label, color=color)

    # combine data points for all series
    xs_flat = list(flatten(xs))

    # set x label of data point to the matching index
    def format_fn(tick_val, tick_pos):
        if int(tick_val) in xs_flat:
            return index[int(tick_val)]
        else:
            return ""

    # dynamically set x label ticks and spacing from index labels
    ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
    ax.xaxis.set_major_locator(MaxNLocator(integer=True))

    if legend:
        ax.legend()

    return fig, ax
Пример #15
0
def sample_plot_1():
    """
    This is a sample visualization function to show what one looks like.
    The code is borrowed from https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/

    This function takes no arguments and shows a nice visualization without having all your code in the notebook itself.
    """

    # Set size of figure
    fig = plt.figure(figsize=(16, 10), dpi=80)

    # Import dataset
    midwest = pd.read_csv(
        "https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv"
    )

    # Prepare Data
    # Create as many colors as there are unique midwest['category']
    categories = np.unique(midwest['category'])
    colors = [
        plt.cm.tab10(i / float(len(categories) - 1))
        for i in range(len(categories))
    ]

    # create ax element
    fig, ax = plt.subplots(figsize=(16, 10),
                           dpi=80,
                           facecolor='w',
                           edgecolor='k')

    # Draw Plot for Each Category
    for i, category in enumerate(categories):
        plt.scatter('area',
                    'poptotal',
                    data=midwest.loc[midwest.category == category, :],
                    s=20,
                    c=colors[i],
                    label=str(category))

    # Decorations
    plt.gca().set(xlim=(0.0, 0.1),
                  ylim=(0, 90000),
                  xlabel='Area',
                  ylabel='Population')

    plt.xticks(fontsize=12)
    plt.yticks(fontsize=12)
    ax.yaxis.set_major_formatter(
        FuncFormatter(lambda x, p: format(int(x), ',')))

    plt.title("Scatterplot of Midwest Area vs Population", fontsize=22)
    plt.legend(fontsize=12)
    plt.savefig('./images_and_code/viz1.png', transparent=True)

    plt.show()

    # little resizing
    im = Image.open("./images_and_code/viz1.png")
    resized = im.resize((768, 480), PIL.Image.ANTIALIAS)
    resized.save('./images_and_code/viz1.png')

    pass
Пример #16
0
    async def serverstats(self, ctx, period=None):
        SQL_USER_BASE = 'SELECT COUNT(DISTINCT ucid) AS dcs_users, COUNT(DISTINCT discord_id)-1 AS discord_users FROM players WHERE ban = false'
        SQL_SERVER_USAGE = 'SELECT trim(m.server_name) as server_name, ROUND(SUM(EXTRACT(EPOCH FROM (s.hop_off - s.hop_on))) / 3600) AS playtime, ROUND(AVG(EXTRACT(EPOCH FROM (s.hop_off - s.hop_on))) / 60) AS avg FROM statistics s, players p, missions m WHERE s.player_ucid = p.ucid AND m.id = s.mission_id AND s.hop_off IS NOT NULL'
        SQL_TOP3_MISSION_UPTIMES = 'SELECT mission_name, ROUND(SUM(EXTRACT(EPOCH FROM (COALESCE(mission_end, NOW()) - mission_start))) / 3600) AS total, ROUND(AVG(EXTRACT(EPOCH FROM (COALESCE(mission_end, NOW()) - mission_start))) / 3600) AS avg FROM missions'
        SQL_TOP5_MISSIONS_USAGE = 'SELECT m.mission_name, COUNT(distinct s.player_ucid) AS players FROM missions m, statistics s WHERE s.mission_id = m.id'
        SQL_LAST_14DAYS = 'SELECT d.date AS date, COUNT(DISTINCT s.player_ucid) AS players FROM statistics s, generate_series(DATE(NOW()) - INTERVAL \'2 weeks\', DATE(NOW()), INTERVAL \'1 day\') d WHERE d.date BETWEEN DATE(s.hop_on) AND DATE(s.hop_off) GROUP BY d.date'
        SQL_MAIN_TIMES = 'SELECT to_char(s.hop_on, \'ID\') as weekday, to_char(h.time, \'HH24\') AS hour, COUNT(DISTINCT s.player_ucid) AS players FROM statistics s, generate_series(TIMESTAMP \'01.01.1970 00:00:00\', TIMESTAMP \'01.01.1970 23:00:00\', INTERVAL \'1 hour\') h WHERE date_part(\'hour\', h.time) BETWEEN date_part(\'hour\', s.hop_on) AND date_part(\'hour\', s.hop_off)'

        embed = discord.Embed(color=discord.Color.blue())
        embed.title = 'Server Statistics'
        if (period):
            SQL_SERVER_USAGE += ' AND DATE(s.hop_on) > (DATE(NOW()) - interval \'1 {}\')'.format(
                period)
            SQL_TOP3_MISSION_UPTIMES += ' WHERE date(mission_start) > (DATE(NOW()) - interval \'1 {}\')'.format(
                period)
            SQL_TOP5_MISSIONS_USAGE += ' AND DATE(s.hop_on) > (DATE(NOW()) - interval \'1 {}\')'.format(
                period)
            SQL_MAIN_TIMES += ' AND DATE(s.hop_on) > (DATE(NOW()) - interval \'1 {}\')'.format(
                period)
            embed.title = string.capwords(
                period if period != 'day' else 'dai') + 'ly ' + embed.title
        else:
            embed.title = 'Overall ' + embed.title
        SQL_SERVER_USAGE += ' GROUP BY trim(m.server_name)'
        SQL_TOP3_MISSION_UPTIMES += ' GROUP BY mission_name ORDER BY 2 DESC LIMIT 3'
        SQL_TOP5_MISSIONS_USAGE += ' GROUP BY m.mission_name ORDER BY 2 DESC LIMIT 5'
        SQL_MAIN_TIMES += ' GROUP BY 1, 2'
        conn = self.bot.pool.getconn()
        try:
            with closing(conn.cursor(
                    cursor_factory=psycopg2.extras.DictCursor)) as cursor:
                cursor.execute(SQL_USER_BASE)
                row = cursor.fetchone()
                embed.add_field(name='Unique Users on Servers',
                                value=str(row[0]))
                embed.add_field(name='Including Discord Members',
                                value=str(row[1]))
                embed.add_field(name='_ _', value='_ _')
                # Server Usages
                servers = ''
                playtimes = ''
                avgs = ''
                cursor.execute(SQL_SERVER_USAGE)
                for row in cursor.fetchall():
                    servers += re.sub(
                        self.bot.config['FILTER']['SERVER_FILTER'], '',
                        row['server_name']).strip() + '\n'
                    playtimes += '{:.0f}\n'.format(row['playtime'])
                    avgs += '{:.0f}\n'.format(row['avg'])
                if (len(servers) > 0):
                    embed.add_field(name='Server', value=servers)
                    embed.add_field(name='Total Playtime (h)', value=playtimes)
                    embed.add_field(name='AVG Playtime (m)', value=avgs)
                # TOP 3 Missions (uptime / avg runtime)
                missions = ''
                totals = ''
                avgs = ''
                cursor.execute(SQL_TOP3_MISSION_UPTIMES)
                for row in cursor.fetchall():
                    missions += re.sub(
                        self.bot.config['FILTER']['MISSION_FILTER'], ' ',
                        row['mission_name']).strip()[:20] + '\n'
                    totals += '{:.0f}\n'.format(row['total'])
                    avgs += '{:.0f}\n'.format(row['avg'])
                if (len(missions) > 0):
                    embed.add_field(name='Mission (Top 3)', value=missions)
                    embed.add_field(name='Total Uptime (h)', value=totals)
                    embed.add_field(name='AVG Uptime (h)', value=avgs)
                # TOP 5 Missions by Playerbase
                missions = ''
                players = ''
                cursor.execute(SQL_TOP5_MISSIONS_USAGE)
                for row in cursor.fetchall():
                    missions += re.sub(
                        self.bot.config['FILTER']['MISSION_FILTER'], ' ',
                        row['mission_name']).strip()[:20] + '\n'
                    players += str(row['players']) + '\n'
                if (len(missions) > 0):
                    embed.add_field(name='Mission (Top 5)', value=missions)
                    embed.add_field(name='Unique Players', value=players)
                    embed.add_field(name='_ _', value='_ _')
                # Draw charts
                plt.style.use('dark_background')
                plt.rcParams['axes.facecolor'] = '2C2F33'
                figure = plt.figure(figsize=(15, 10))
                # Last 7 days
                axis = plt.subplot2grid((2, 1), (0, 0), colspan=1, fig=figure)
                labels = []
                values = []
                cursor.execute(SQL_LAST_14DAYS)
                for row in cursor.fetchall():
                    labels.append(row['date'].strftime('%a %m/%d'))
                    values.append(row['players'])
                axis.bar(labels, values, width=0.5, color='dodgerblue')
                axis.set_title('Unique Players past 14 Days',
                               color='white',
                               fontsize=25)
                axis.set_yticks([])
                for label in axis.get_xticklabels():
                    label.set_rotation(30)
                    label.set_ha('right')
                for i in range(0, len(values)):
                    axis.annotate(values[i],
                                  xy=(labels[i], values[i]),
                                  ha='center',
                                  va='bottom',
                                  weight='bold')
                if (len(values) == 0):
                    axis.set_xticks([])
                    axis.text(0,
                              0,
                              'No data available.',
                              ha='center',
                              va='center',
                              rotation=45,
                              size=15)
                # Times & Days
                axis = plt.subplot2grid((2, 1), (1, 0), colspan=1, fig=figure)
                values = np.zeros((24, 7))
                cursor.execute(SQL_MAIN_TIMES)
                for row in cursor.fetchall():
                    values[int(row['hour'])][int(row['weekday']) -
                                             1] = row['players']
                axis.imshow(values, cmap='cividis', aspect='auto')
                axis.set_title('Users per Day/Time (UTC)',
                               color='white',
                               fontsize=25)
                # axis.invert_yaxis()
                axis.xaxis.set_major_formatter(
                    FuncFormatter(
                        lambda x, pos: self.WEEKDAYS[np.clip(x, 0, 6)]))
                plt.subplots_adjust(hspace=0.5, wspace=0.0)
                filename = 'serverstats.png'
                figure.savefig(filename,
                               bbox_inches='tight',
                               facecolor='#2C2F33')
                plt.close(figure)
                file = discord.File(filename)
                embed.set_image(url='attachment://' + filename)
                embed.set_footer(text='Click on the image to zoom in.')
                await ctx.send(file=file, embed=embed)
                os.remove(filename)
        except (Exception, psycopg2.DatabaseError) as error:
            self.bot.log.exception(error)
        finally:
            self.bot.pool.putconn(conn)
Пример #17
0
    def vert_cbar(self, resolution, log_scale, ax, label=None, label_fmt=None):
        r"""Display an image of the transfer function

        This function loads up matplotlib and displays the current transfer function.

        Parameters
        ----------

        Examples
        --------

        >>> tf = TransferFunction( (-10.0, -5.0) )
        >>> tf.add_gaussian(-9.0, 0.01, 1.0)
        >>> tf.show()
        """
        from matplotlib.ticker import FuncFormatter

        if label is None:
            label = ""
        alpha = self.alpha.y
        max_alpha = alpha.max()
        i_data = np.zeros((self.alpha.x.size, self.funcs[0].y.size, 3))
        i_data[:, :, 0] = np.outer(self.funcs[0].y, np.ones(self.alpha.x.size))
        i_data[:, :, 1] = np.outer(self.funcs[1].y, np.ones(self.alpha.x.size))
        i_data[:, :, 2] = np.outer(self.funcs[2].y, np.ones(self.alpha.x.size))

        ax.imshow(i_data, origin="lower", aspect="auto")
        ax.plot(alpha, np.arange(self.alpha.y.size), "w")

        # Set TF limits based on what is visible
        visible = np.argwhere(self.alpha.y > 1.0e-3 * self.alpha.y.max())

        # Display colobar values
        xticks = (
            np.arange(np.ceil(self.alpha.x[0]), np.floor(self.alpha.x[-1]) + 1, 1)
            - self.alpha.x[0]
        )
        xticks *= (self.alpha.x.size - 1) / (self.alpha.x[-1] - self.alpha.x[0])
        if len(xticks) > 5:
            xticks = xticks[:: len(xticks) // 5]

        # Add colorbar limits to the ticks (May not give ideal results)
        xticks = np.append(visible[0], xticks)
        xticks = np.append(visible[-1], xticks)
        # remove dupes
        xticks = list(set(xticks))
        ax.yaxis.set_ticks(xticks)

        def x_format(x, pos):
            val = (
                x * (self.alpha.x[-1] - self.alpha.x[0]) / (self.alpha.x.size - 1)
                + self.alpha.x[0]
            )
            if log_scale:
                val = 10 ** val
            if label_fmt is None:
                if abs(val) < 1.0e-3 or abs(val) > 1.0e4:
                    if not val == 0.0:
                        e = np.floor(np.log10(abs(val)))
                        return r"${:.2f}\times 10^{{ {:d} }}$".format(
                            val / 10.0 ** e, int(e)
                        )
                    else:
                        return r"$0$"
                else:
                    return "%.1g" % (val)
            else:
                return label_fmt % (val)

        ax.yaxis.set_major_formatter(FuncFormatter(x_format))

        yticks = np.linspace(0, 1, 2, endpoint=True) * max_alpha
        ax.xaxis.set_ticks(yticks)

        def y_format(y, pos):
            s = "%0.2f" % (y)
            return s

        ax.xaxis.set_major_formatter(FuncFormatter(y_format))
        ax.set_xlim(0.0, max_alpha)
        ax.get_xaxis().set_ticks([])
        ax.set_ylim(visible[0], visible[-1])
        ax.tick_params(axis="y", colors="white", size=10)
        ax.set_ylabel(label, color="white", size=10 * resolution / 512.0)
Пример #18
0
def plotBar(mixcrlist, plotcdr3counts, cdr3colors, axisBreak, uChain):
    axisBreak = list(map(lambda x: float(x), axisBreak))
    # 'break' or 'cut-out' the y-axis
    # into two portions - use the top (ax) for the outliers, and the bottom
    # (ax2) for the details of the majority of our data
    fig, (ax, ax2) = plt.subplots(2,
                                  1,
                                  sharex=True,
                                  gridspec_kw={'height_ratios': [1, 2]})

    # X-axis positions
    xpos = np.arange(len(mixcrlist), dtype=np.float32)
    # Now adjust indices with '-' to have smaller width
    emptyIndex = [i for i, x in enumerate(mixcrlist) if x == '-']

    counter = 0
    stillbar = 0
    for i, x in enumerate(xpos):
        xpos[i] -= counter

        if i in emptyIndex:
            counter += 1
            xpos[i] -= 0.5
            stillbar = i + 1
            try:
                if xpos[stillbar + 1] in emptyIndex:
                    for n in range(stillbar + 1, len(xpos)):
                        xpos[n] += 0.2
            except:
                pass
        elif (i not in emptyIndex) and (i != stillbar):
            xpos[i] -= 0.2

    print(xpos)

    for k, f in enumerate(mixcrlist):
        # Plot same thing in both ax and ax2
        bar = {}
        bar2 = {}
        nextbot = 0

        # If empty...
        if len(plotcdr3counts[f].keys()) == 0:
            data = [0] * len(mixcrlist)
            data[k] = 1.
            bar[index + 1] = ax.bar(xpos,
                                    data,
                                    width=0.2,
                                    linewidth=0.1,
                                    color='white',
                                    bottom=nextbot)

            bar2[index + 1] = ax2.bar(xpos,
                                      data,
                                      width=0.2,
                                      linewidth=0.1,
                                      color='white',
                                      bottom=nextbot)

        # Color the rest of the stacked bars
        for index, cdr3 in enumerate(
                sorted(plotcdr3counts[f].keys(),
                       key=lambda u: plotcdr3counts[f][u])):
            data = [0] * len(mixcrlist)
            data[k] = plotcdr3counts[f][cdr3]

            if cdr3 != 'Others':
                bar[index + 1] = ax.bar(np.arange(len(mixcrlist)),
                                        data,
                                        width=0.7,
                                        linewidth=0.1,
                                        color=cdr3colors[cdr3],
                                        bottom=nextbot)

                bar2[index + 1] = ax2.bar(np.arange(len(mixcrlist)),
                                          data,
                                          width=0.7,
                                          linewidth=0.1,
                                          color=cdr3colors[cdr3],
                                          bottom=nextbot)
                nextbot += plotcdr3counts[f][cdr3]

        # Color 'Others' bar last if it exists
        if 'Others' in plotcdr3counts[f].keys():
            data = [0] * len(mixcrlist)
            data[k] = plotcdr3counts[f]['Others']
            bar[0] = ax.bar(np.arange(len(mixcrlist)),
                            data,
                            width=0.7,
                            linewidth=0.1,
                            color='0.85',
                            bottom=nextbot)

            bar2[0] = ax2.bar(np.arange(len(mixcrlist)),
                              data,
                              width=0.7,
                              linewidth=0.1,
                              color='0.85',
                              bottom=nextbot)

    # Zoom-in / limit the view to different portion of the data

    ax.set_ylim(axisBreak[1], 1.)
    ax2.set_ylim(0., axisBreak[0])

    # Hide the spines b/t ax and ax2
    ax2.spines['top'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    #ax.xaxis.tick_bottom()
    #ax2.xaxis.tick_top()
    #ax2.tick_params(labeltop=False)

    # set other bar parameters
    ax.set_xticks(np.arange(len(mixcrlist)))
    xtickNames = ax.set_xticklabels(['' if x == '-' else x for x in mixcrlist])
    plt.setp(xtickNames, rotation=0)
    ax.tick_params(axis='y', direction='in', length=1)
    ax2.tick_params(axis='y', direction='in', length=1)
    ax.tick_params(axis='x', length=0)
    ax2.tick_params(axis='x', length=0, labelsize=16)
    ax2.yaxis.set_major_locator(plt.MaxNLocator(4))
    ax.yaxis.set_major_formatter(
        FuncFormatter(lambda x, pos: '%s%%' % (int(x * 100.0))))
    ax2.yaxis.set_major_formatter(
        FuncFormatter(lambda x, pos: '%s%%' % (int(x * 100.0))))
    #ax2.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: '%s%%' %(round(x*100.0,2))))
    #ax.set_title('%s (%s%%)     %s (%s%%)' % (name1, mut1, name2, mut2))
    #ax[n+1].axhline(y=0.02)

    pdf = PdfPages('bar_' + uChain + '.pdf')
    plt.tight_layout()
    plt.savefig(pdf, format='pdf')
    plt.close()
    pdf.close()
def render_result(name, *measurements):
    res = {}
    for m in measurements:
        res.update(m)
    tools = list(set([g for g in res.keys()]))
    measurements = list(set([(m, res[g][m][1]) for g in res.keys() for m in res[g].keys()]))
    units = {}
    for measurement, unit in measurements:
        if unit not in units: units[unit] = {}
        store = units[unit]

        for tool in tools:
            if measurement not in store: store[measurement] = {}
            if measurement in res[tool] and unit == res[tool][measurement][1]:
                store[measurement][tool] = res[tool][measurement]

    subplots = len(units)
    unit_keys = sorted(units.keys(), key=lambda x: units_order.index(x))
    width_ratios = [sum([len(units[u][m]) for m in units[u]]) + (len(units[u]) - 1) for u in unit_keys]
    _, subplots = plt.subplots(1, subplots, gridspec_kw={'width_ratios': width_ratios},
                               figsize=(sum(width_ratios) / 2 + 2, 5))
    if len(units) == 1:
        subplots = [subplots]

    for u, unit in enumerate(unit_keys):
        measurements = units[unit]
        width = 1

        ax = subplots[u]
        mx_value = 0
        mn_value = pow(10, 100)
        spacing = 0.02

        def auto_label(rects):
            """Attach a text label above each bar in *rects*, displaying its height."""
            for rect in rects:
                height = rect.get_height()
                s = f"{int(height)}"
                extra = ""
                if height == 0:
                    pass
                elif height == 0.000001:
                    s = ''
                elif height < 10:
                    s = f'{height:.2f}'
                elif height < 100:
                    s = f'{height:.1f}'
                elif height >= 1000 and height < 10000:
                    s = f'{height / 1000:.1f}'
                    extra = "k"
                elif height >= 10000:
                    s = f'{int(height / 1000)}'
                    extra = "k"
                while "." in s and s.endswith("0"):
                    s = s[:-1]
                    if s.endswith("."):
                        s = s[:-1]
                s += extra
                ax.annotate(s,
                            xy=(rect.get_x() + rect.get_width() / 2, height),
                            xytext=(0, 0),  # 3 points vertical offset
                            textcoords="offset points",
                            ha='center', va='bottom',
                            fontsize="x-small",
                            color="grey",
                            bbox=dict(boxstyle='square,pad=0.1', fc='white', ec='none'),
                            zorder=10)

        brs = []
        i = 0
        xticks = []
        def get_mes_pos(x):
            try:
                return mes_order.index(x)
            except ValueError:
                return x
        for measurement in sorted(measurements, key=get_mes_pos):
            group = measurements[measurement]
            xticks.append(i * width + width * len(group) / 2 - width / 2 + spacing)
            for tool in sorted(tools, key=lambda x: tool_order.index(x)):
                try:
                    value = max(group[tool][0], 0)
                    brs.append((width * i + spacing, value, tool))
                    mx_value = max(mx_value, value)
                    mn_value = min(mn_value, value)
                    i += 1
                except KeyError:
                    brs.append((0, 0.000001, tool))
            i += 1

        bars = {}

        for r, v, t in brs:
            if t not in bars: bars[t] = {"r": [], "v": []}
            bars[t]["r"].append(r)
            bars[t]["v"].append(v)

        for t in bars:
            r, v = bars[t]["r"], bars[t]["v"]
            auto_label(ax.bar(r, v, width=width - spacing * 2, label=t, zorder=100, linewidth=0,
                              color=[tool_colors[t] for _ in range(len(r))]))

        ax.set_title(f"[{unit}]   ", position=(0, 1), fontweight='ultralight', ha="right", fontsize="small", alpha=0.5,
                     stretch=0)
        ax.xaxis.set_ticks_position('none')
        ax.yaxis.set_ticks_position('left')
        ax.spines['right'].set_visible(False)
        ax.spines['top'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.spines['left'].set_visible(False)

        ax.yaxis.set_tick_params(labelcolor="grey", color="lightgrey")
        ax.ticklabel_format(style='plain')

        def plain(x, pos):
            if x >= 1:
                return str(int(x))
            return str(x)

        # if mn_value * 100 < mx_value:
        #     ax.set_yscale('log')
        #     ax.yaxis.set_major_formatter(FuncFormatter(plain))
        ax.set_xticks(xticks)
        ax.set_xticklabels([s.replace(" ", "\n") for s in sorted(measurements, key=get_mes_pos)])

        ax.yaxis.grid(which="major", color='lightgrey', zorder=0)
        ax.set_yticks(ax.get_yticks()[1:-1])
        if len(ax.get_yticks()) > 0 and ax.get_yticks()[-1] > mx_value:
            ax.set_yticks(ax.get_yticks()[:-1])

        def millions(x, pos):
            return f'{x / 1000:1.0f}k'

        if unit.startswith("msg") and ax.get_yticks()[0] > 1000:
            formatter = FuncFormatter(millions)
            ax.yaxis.set_major_formatter(formatter)

        if u + 1 == len(unit_keys):

            plt.text(left_title[name], 0, name.replace(" ", "\n") + "\n\n", ha="left", va="bottom", transform=ax.transAxes,
                     ma="left",
                     size="x-large", fontstyle="italic", fontfamily="serif")
        # plt.setp(ax.get_yticklabels()[0], visible=False)
        # plt.setp(ax.get_yticks(), visible=False)
        # plt.setp(ax.get_yticklabels()[-1], visible=False)

    right_spacing = sum(width_ratios) / (sum(width_ratios) + 3) - 0.08
    wspace = (len(units) + 1) / len(units)
    plt.subplots_adjust(right=right_spacing, left=min(0.15 - sum(width_ratios)/400, right_spacing-0.01), bottom=0.2, wspace=wspace - 0.5)
    # plt.tight_layout(w_pad=1)

    if len(tools) > 1:
        subplots[-1].legend(loc='upper left', edgecolor="none", facecolor="none", bbox_to_anchor=(wspace*2-1, 1))

    if not os.path.exists(benchmarks_path):
        os.mkdir(benchmarks_path)

    # Update the plot
    plt.savefig(os.path.join(benchmarks_path, f"{name}.png"), dpi=300)
Пример #20
0
def main():
    # argparse
    parser = argparse.ArgumentParser(
        description='Draw box plot for ars comparison')
    parser.add_argument('ars',
                        type=argparse.FileType('r'),
                        help='Ars region frequency file')
    parser.add_argument(
        '-m',
        type=int,
        default=100,
        help='Minimum number of ribose as threshold, default=100')
    parser.add_argument('-o', default='', help='Output basename')
    parser.add_argument('--ppb',
                        action='store_true',
                        help='PPB data is available')
    parser.add_argument('--bar',
                        action='store_true',
                        help='Draw bar plot of each library')
    parser.add_argument('--box',
                        action='store_true',
                        help='Draw box plot for ratios')
    parser.add_argument('--line',
                        action='store_true',
                        help='Draw  plot line chart for ratio trend')
    parser.add_argument('--no-label',
                        action='store_true',
                        help='Hide label, header and legend in the plot')
    parser.add_argument('--same_scale',
                        action='store_true',
                        help='Use same scale for all plots')
    parser.add_argument('--hy',
                        action='store_true',
                        help='HydEn-seq libraries')
    args = parser.parse_args()
    if not any([args.bar, args.box, args.line]):
        args.line = True
    if args.o == '':
        args.o = args.ars.name.split('.')[0]

    # get information for bed file
    df = pd.read_csv(args.ars, sep='\t')
    # convert to kbp
    df.Position = df.Position / 1000
    # set Categorical data
    lib_params = {'Genotype':['WT', 'pip', 'rnh1', 'rnh201', 'RED', 'PolWT','Pol2M644G',\
         'Pol3L612M', 'Pol3L612G','Pol1L868M','Pol1Y869A'],\
            'RE':['RE1','RE2','RE3'],\
            'Time': ['early', 'medium', 'late', 'high','low'],\
            'Strand': ['leading', 'lagging'],\
            'String':['E134', 'BY4741', 'BY4742', 'YFP17', 'W303', 'S288C'] }
    categorize_df(df, lib_params)

    # set genotype needed for plot
    genotype_needed = [
        'WT', 'rnh201', 'PolWT', 'Pol2M644G', 'Pol3L612M', 'Pol3L612G',
        'Pol1L868M', 'Pol1Y869A'
    ]
    # column number for first rnmp column
    RNMP_COL_NUM = 9 if not args.ppb else 10

    # formatter for scientific
    def my_scientific_format(y):
        a = f'{y:.1E}'
        m, e = a.split('E')
        e = e.replace('+0', '')
        e = e.replace('-0', '-')
        return f'{m}E{e}'

    f_scientific = FuncFormatter(lambda y, _: my_scientific_format(y))

    # get available categories
    libs = df.Library.unique()
    times = df.Time.unique().sort_values()
    treatments = df.Genotype.unique()
    p = sorted(df.Position.unique())
    len_part = p[1] - p[0]
    flank_start = df.Position.min()
    len_flank = df.Position.max()
    print('Finish reading data!')
    print('Available replication peak times: {}.\n'.format(', '.join(times)) + \
            'Available treatments: {}.\n'.format(', '.join(treatments)) + \
            'Flank:{} - {}kbp, Part length: {}kbp\n'.format(flank_start, len_flank, len_part) + \
            'Libraries: {}.'.format(', '.join([str(x) for x in libs])))

    # set color parameters
    sns.set(style='whitegrid')
    colorlist = sns.hls_palette(8, l=.5, s=1)
    # color for bases
    colorb = [colorlist[i] for i in [6, 4, 1, 2]]
    # barplot count
    co = {'leading': colorlist[0], 'lagging': colorlist[1]}
    # build color dict for time
    # remove yellow color which is not print friendly
    colorlist = ['#a570f3', '#1bce77', '#d95f02']
    colort = {}
    for i in range(len(times)):
        colort[times[i]] = colorlist[i]

    # draw barplot for libs
    # array to store ratios:
    ratios = []
    # array to store percentage of leading and lagging
    # lib_percentage = []
    lib_needed = {}
    for t in times:
        lib_needed[t] = []
        for l in libs:
            da = df[(df.Time == t) & (df.Library == l) &
                    (df.Strand == 'leading')].sort_values(by='Position')
            db = df[(df.Time == t) & (df.Library == l) &
                    (df.Strand == 'lagging')].sort_values(by='Position')
            dc = da.merge(
                db,
                suffixes=['_leading', '_lagging'],
                on=['Library', 'String', 'Genotype', 'Time', 'RE', 'Position'])
            dc['Total'] = dc.Sum_lagging + dc.Sum_leading
            # filter for threshold
            total_ribo = dc.Total.sum()
            if total_ribo >= args.m:
                lib_needed[t].append(l)
            ratios.append(dc)
    # generate data frame
    dmerge = pd.concat(ratios, sort=True)

    if args.line:
        # feature
        features = ['Sum']
        for i in df.columns[RNMP_COL_NUM:]:
            if i.find('%') == -1:
                features.append(i)
        # calculate the estimators for ratio
        flanks = df.Position.unique()
        # store average
        dsummary = []
        dleading = []
        dlagging = []
        for tr in genotype_needed:
            for t in times:
                for f in flanks:
                    db = dmerge[(dmerge.Genotype == tr) & (dmerge.Time == t) &
                                (dmerge.Position == f) &
                                (dmerge.Library.isin(lib_needed[t]))]
                    if db.empty:
                        continue
                    db['Ratio'] = db['Sum_leading'] / db['Sum_lagging']
                    db['Length'] = db['Sum_leading'] / db['RPB_leading']
                    dsummary.append([
                        tr, t, f,
                        db.Total.sum(),
                        db.Ratio.median(),
                        db.Ratio.std()
                    ] + [
                        db['{}_leading'.format(fe)].sum() /
                        db['{}_lagging'.format(fe)].sum() for fe in features
                    ])
                    if args.ppb:
                        dleading.append([
                            tr, t, f,
                            db.Sum_leading.sum(),
                            db.RPB_leading.mean(),
                            db.RPB_leading.std(),
                            db.PPB_leading.mean(),
                            db.PPB_leading.std()
                        ] + [
                            db['{}_leading'.format(fe)].mean()
                            for fe in features
                        ])
                        dlagging.append([
                            tr, t, f,
                            db.Sum_lagging.sum(),
                            db.RPB_lagging.mean(),
                            db.RPB_lagging.std(),
                            db.PPB_lagging.mean(),
                            db.PPB_lagging.std()
                        ] + [
                            db['{}_lagging'.format(fe)].mean()
                            for fe in features
                        ])
                    else:
                        dleading.append([
                            tr, t, f,
                            db.Sum_leading.sum(),
                            db.RPB_leading.mean(),
                            db.RPB_leading.std()
                        ] + [
                            db['{}_leading'.format(fe)].mean()
                            for fe in features
                        ])
                        dlagging.append([
                            tr, t, f,
                            db.Sum_lagging.sum(),
                            db.RPB_lagging.mean(),
                            db.RPB_lagging.std()
                        ] + [
                            db['{}_lagging'.format(fe)].mean()
                            for fe in features
                        ])
        df_summary = pd.DataFrame(
            dsummary,
            columns=['Genotype', 'Time', 'Position', 'Total', 'Median', 'Std'
                     ] + ['{}'.format(fe) for fe in features])
        if args.ppb:
            df_leading = pd.DataFrame(
                dleading,
                columns=[
                    'Genotype', 'Time', 'Position', 'Total', 'RPB', 'RPB_std',
                    'PPB', 'PPB_std'
                ] + ['{}'.format(fe) for fe in features])
            df_lagging = pd.DataFrame(
                dlagging,
                columns=[
                    'Genotype', 'Time', 'Position', 'Total', 'RPB', 'RPB_std',
                    'PPB', 'PPB_std'
                ] + ['{}'.format(fe) for fe in features])
        else:
            df_leading = pd.DataFrame(
                dleading,
                columns=[
                    'Genotype', 'Time', 'Position', 'Total', 'RPB', 'RPB_std'
                ] + ['{}'.format(fe) for fe in features])
            df_lagging = pd.DataFrame(
                dlagging,
                columns=[
                    'Genotype', 'Time', 'Position', 'Total', 'RPB', 'RPB_std'
                ] + ['{}'.format(fe) for fe in features])
        df_summary = categorize_df(df_summary, lib_params)
        df_leading = categorize_df(df_leading, lib_params)
        df_lagging = categorize_df(df_lagging, lib_params)
        # draw line chart for small window summaries
        feature_groups = {'Raw': [], 'Normalized': []}
        columns = list(df_summary.columns)
        for i in columns[6:]:
            if i.find('n') == -1:
                feature_groups['Raw'].append(i)
            else:
                feature_groups['Normalized'].append(i)
        # start plotting
        for tr in genotype_needed:
            db = df_summary[(df_summary.Genotype == tr)].sort_values(by='Time')
            if db.empty:
                continue
            # Median, MLE_Sum
            for r in ['Sum']:
                fig, ax = plt.subplots(figsize=(9, 9))
                sns.lineplot(x='Position',
                             y=r,
                             hue='Time',
                             data=db,
                             palette=colort,
                             linewidth=4)
                # add std
                facecolors = ['#cc99cc', '#99cc99']
                c = 0
                for t in db.Time.unique():
                    dc = db[db.Time == t].sort_values('Position')
                    ax.fill_between(dc.Position,
                                    dc.Sum + dc.Std,
                                    dc.Sum - dc.Std,
                                    facecolor=facecolors[c],
                                    alpha=0.4)
                    c += 1
                if not args.hy:
                    plt.ylim((max(0,
                                  ax.get_ylim()[0]), min(ax.get_ylim()[1], 6)))
                plt.suptitle(
                    'Leading/lagging ratio with increasing distance to ARS\n ({}, {})'
                    .format(tr, r))
                ax.set_ylabel('Leading/Lagging ratio')
                ax.set_xlabel('Distance to ARS (kb)')
                if args.no_label:
                    hide_label(fig, ax)
                fig.subplots_adjust(top=0.99,
                                    left=0.15,
                                    right=0.99,
                                    bottom=0.08)
                if args.same_scale:
                    plt.ylim([0.2, 6])
                    plt.yscale('log')
                    ax.yaxis.set_major_formatter(ScalarFormatter())
                    ax.yaxis.set_ticks([0.2, 0.5, 1, 2, 5])
                plt.savefig(args.o + '_ratio_{}_{}.png'.format(tr, r).lower())
                plt.close('all')
            # Compositions for leading/lagging ratio
            # build dataframe
            for k, v in feature_groups.items():
                dcomp = []
                for index, l in db.iterrows():
                    for fe in v:
                        dcomp.append([l.Time, l.Position, fe, l[fe]])
                df_comp = pd.DataFrame(
                    dcomp, columns=['Time', 'Position', 'Feature', 'Value'])
                for w in ['Time']:
                    for v in df_summary[w].unique():
                        if v not in lib_params[w]:
                            lib_params[w].append(v)
                    df_comp[w] = pd.Categorical(df_comp[w], lib_params[w])
        # leading and lagging
        for tr in genotype_needed:
            for df_lela, slela in [[df_leading, 'leading'],
                                   [df_lagging, 'lagging']]:
                dlela = df_lela[(df_lela.Genotype == tr)].sort_values(
                    by='Time')
                if dlela.empty:
                    continue
                # total
                for ydata in ['RPB', 'PPB']:
                    fig, ax = plt.subplots(figsize=(9, 9))
                    sns.lineplot(x='Position',
                                 y=ydata,
                                 hue='Time',
                                 data=dlela,
                                 palette=colort,
                                 linewidth=4)
                    ylims = ax.get_ylim()
                    # add std
                    facecolors = ['#cc99cc', '#99cc99']
                    c = 0
                    for t in dlela.Time.unique():
                        dc = dlela[dlela.Time == t].sort_values('Position')
                        ax.fill_between(dc.Position,
                                        dc[ydata] + dc[f'{ydata}_std'],
                                        dc[ydata] - dc[f'{ydata}_std'],
                                        facecolor=facecolors[c],
                                        alpha=0.4)
                        c += 1
                    if not args.hy:
                        plt.ylim(
                            (max(0,
                                 ax.get_ylim()[0]), min(ax.get_ylim()[1], 3)))
                    plt.suptitle(
                        f'Ribonucleotides incorporation with increasing distance to ARS \n({slela} strand, {tr})'
                    )
                    # ax.ticklabel_format(useOffset=False)
                    if ydata in ['Total', 'RPB']:
                        ax.set_ylabel('Counts')
                    else:
                        ax.set_ylabel(ydata)
                        #                         ax.set_ylim([2.2e-8,7.8e-8])
                        #                         ax.yaxis.set_ticks([3e-8,5e-8,7e-8])
                        ax.yaxis.set_major_formatter(f_scientific)
                    ax.set_xlabel('Distance to ARS (kb)')
                    for tick in ax.yaxis.get_major_ticks():
                        tick.label.set_fontsize(24)
                    for tick in ax.xaxis.get_major_ticks():
                        tick.label.set_fontsize(24)
                    if args.no_label:
                        hide_label(fig, ax)
                    # plt.ylim((0.03, 0.082))
                    plt.savefig(args.o + f'_{ydata}_{slela}_{tr}.png'.lower())
                    plt.close('all')
        print('line chart generated!')
        print('Done!')
Пример #21
0
xhaxis = df.index
#xHTTP = HTTP.index
#y1axis = Ratio
y2axis = dx['Bytes Received']
y3axis = dx['Bytes Sent']
y4axis = dx['Requests']
y2haxis = df['Bytes Received']
y3haxis = df['Bytes Sent']


def millions(y4axis, pos):
    #'The two args are the value and tick position'
    return '%1.1f M' % (y4axis * 1e-6)


formatter = FuncFormatter(millions)

fig, (ax4, ax2, ax3) = plt.subplots(3, sharey=False, figsize=(15, 9))
plt.subplots_adjust(hspace=0.5)

ax4.plot(xaxis, y4axis, color='black', linewidth=0.4)
ax4.grid(which='major', axis='y', linestyle='dotted')
ax4.text(dt.date(2020, 3, 23),
         35000000,
         "C-19 Lock Down --->",
         color='gray',
         fontsize=6,
         rotation=270)
ax4.legend(['Total Requests per Day'], loc='upper right', fontsize='x-small')
# Set the locator
locator = mdates.MonthLocator()  # every month
Пример #22
0
def make_cosine_timeline_fig(
    test_embeddings: np.ndarray,
    reference_embeddings: np.ndarray,
    test_labels: List[str],
    reference_labels: List[str],
) -> plt.Figure:
    """
    Returns fig showing time course of correlation between embeddings and reference embeddings
    """

    assert np.ndim(test_embeddings) == 3  # (ticks, words, embed size)
    assert np.ndim(reference_embeddings) == 3

    # calculate correlations between each reference and test embedding, for each time step
    # pairwise cosine return shape (num test, num reference) - correlations is (num ticks, num test, num ref)
    correlations = np.asarray([
        cosine_similarity(test_embeddings[tick], reference_embeddings[tick])
        for tick in range(len(reference_embeddings))
    ])
    print(correlations)
    print(correlations.shape)

    # fig
    num_test = len(test_labels)
    assert num_test % 2 != 0  # need odd number of axes to insert legend into an empty axis
    num_rows = num_test // 2 if num_test % 2 == 0 else num_test // 2 + 1
    test_label_iterator = iter(test_labels)
    res, axes = plt.subplots(num_rows,
                             2,
                             figsize=(7, 2 * num_rows),
                             dpi=config.Fig.dpi)
    for axes_row_id, axes_row in enumerate(axes):
        for axes_col_id, ax in enumerate(axes_row):

            # a single axis belongs to a single test word

            # axis
            try:
                test_label = next(test_label_iterator)
            except StopIteration:
                ax.axis('off')
                last_ax = axes[axes_row_id,
                               axes_col_id - 1]  # make legend for last ax
                handle, label = last_ax.get_legend_handles_labels()
                ax.legend(handle,
                          label,
                          loc=6,
                          ncol=num_test // 3,
                          frameon=False)
                continue
            else:
                ax.set_title(test_label, fontsize=config.Fig.ax_label_fontsize)
                if axes_col_id % 2 == 0:
                    ax.set_ylabel('Cosine'.format(test_label),
                                  fontsize=config.Fig.ax_label_fontsize)
                if axes_row_id == num_rows - 1:
                    ax.set_xlabel('Training Time',
                                  fontsize=config.Fig.ax_label_fontsize)
                ax.spines['right'].set_visible(False)
                ax.spines['top'].set_visible(False)
                ax.xaxis.set_major_formatter(FuncFormatter(human_format))
                ax.set_ylim([-1.0, 1.0])

            # plot
            for ref_label in reference_labels:
                # need shape (num ticks, num ref)
                y = correlations[:,
                                 test_labels.index(test_label),
                                 reference_labels.index(ref_label)]
                ax.plot(y,
                        '-',
                        linewidth=config.Fig.line_width,
                        label=ref_label)

    return res
Пример #23
0
def coaddM5Analysis(path,
                    dbfile,
                    runName,
                    slair=False,
                    WFDandDDFs=False,
                    noDithOnly=False,
                    bestDithOnly=False,
                    someDithOnly=False,
                    specifiedDith=None,
                    nside=128,
                    filterBand='r',
                    includeDustExtinction=False,
                    saveunMaskedCoaddData=False,
                    pixelRadiusForMasking=5,
                    cutOffYear=None,
                    plotSkymap=True,
                    plotCartview=True,
                    unmaskedColorMin=None,
                    unmaskedColorMax=None,
                    maskedColorMin=None,
                    maskedColorMax=None,
                    nTicks=5,
                    plotPowerSpectrum=True,
                    showPlots=True,
                    saveFigs=True,
                    almAnalysis=True,
                    raRange=[-50, 50],
                    decRange=[-65, 5],
                    saveMaskedCoaddData=True):
    """

    Analyze the artifacts induced in the coadded 5sigma depth due to imperfect observing strategy.
      - Creates an output directory for subdirectories containing the specified things to save.
      - Creates, shows, and saves comparison plots.
      - Returns the metricBundle object containing the calculated coadded depth, and the output directory name.

    Required Parameters
    -------------------
      * path: str: path to the main directory where output directory is to be saved.
      * dbfile: str: path to the OpSim output file, e.g. to a copy of enigma_1189
      * runName: str: run name tag to identify the output of specified OpSim output, e.g. 'enigma1189' 

    Optional Parameters
    -------------------
      * slair: boolean: set to True if analysis on a SLAIR output.
                        Default: False
      * WFDandDDFs: boolean: set to True if want to consider both WFD survet and DDFs. Otherwise will only work
                             with WFD. Default: False
      * noDithOnly: boolean: set to True if only want to consider the undithered survey. Default: False
      * bestDithOnly: boolean: set to True if only want to consider RandomDitherFieldPerVisit.
                               Default: False
      * someDithOnly: boolean: set to True if only want to consider undithered and a few dithered surveys. 
                               Default: False
      * specifiedDith: str: specific dither strategy to run.
                            Default: None
      * nside: int: HEALpix resolution parameter. Default: 128
      * filterBand: str: any one of 'u', 'g', 'r', 'i', 'z', 'y'. Default: 'r'
      * includeDustExtinction: boolean: set to include dust extinction. Default: False
      * saveunMaskedCoaddData: boolean: set to True to save data before border masking. Default: False
      * pixelRadiusForMasking: int: number of pixels to mask along the shallow border. Default: 5

      * cutOffYear: int: year cut to restrict analysis to only a subset of the survey. 
                         Must range from 1 to 9, or None for the full survey analysis (10 yrs).
                         Default: None
      * plotSkymap: boolean: set to True if want to plot skymaps. Default: True
      * plotCartview: boolean: set to True if want to plot cartview plots. Default: False
      * unmaskedColorMin: float: lower limit on the colorscale for unmasked skymaps. Default: None
      * unmaskedColorMax: float: upper limit on the colorscale for unmasked skymaps. Default: None

      * maskedColorMin: float: lower limit on the colorscale for border-masked skymaps. Default: None
      * maskedColorMax: float: upper limit on the colorscale for border-masked skymaps. Default: None
      * nTicks: int: (number of ticks - 1) on the skymap colorbar. Default: 5
      * plotPowerSpectrum: boolean: set to True if want to plot powerspectra. Default: True

      * showPlots: boolean: set to True if want to show figures. Default: True
      * saveFigs: boolean: set to True if want to save figures. Default: True
      
      * almAnalysis: boolean: set to True to perform the alm analysis. Default: True
      * raRange: float array: range of right ascention (in degrees) to consider in alm  cartview plot;
                              applicable when almAnalysis=True. Default: [-50,50]
      * decRange: float array: range of declination (in degrees) to consider in alm cartview plot; 
                               applicable when almAnalysis=True. Default: [-65,5]
      * saveMaskedCoaddData: boolean: set to True to save the coadded depth data after the border
                                      masking. Default: True

    """
    # ------------------------------------------------------------------------
    # read in the database
    if slair:
        # slair database
        opsdb = db.Database(dbfile, defaultTable='observations')
    else:
        # OpSim database
        opsdb = db.OpsimDatabase(dbfile)

    # ------------------------------------------------------------------------
    # set up the outDir
    zeropt_tag = ''
    if cutOffYear is not None: zeropt_tag = '%syearCut' % cutOffYear
    else: zeropt_tag = 'fullSurveyPeriod'

    if includeDustExtinction: dust_tag = 'withDustExtinction'
    else: dust_tag = 'noDustExtinction'

    regionType = ''
    if WFDandDDFs: regionType = 'WFDandDDFs_'

    outDir = 'coaddM5Analysis_%snside%s_%s_%spixelRadiusForMasking_%sBand_%s_%s_directory' % (
        regionType, nside, dust_tag, pixelRadiusForMasking, filterBand,
        runName, zeropt_tag)
    print('# outDir: %s' % outDir)
    resultsDb = db.ResultsDb(outDir=outDir)

    # ------------------------------------------------------------------------
    # set up the sql constraint
    if WFDandDDFs:
        if cutOffYear is not None:
            nightCutOff = (cutOffYear) * 365.25
            sqlconstraint = 'night<=%s and filter=="%s"' % (nightCutOff,
                                                            filterBand)
        else:
            sqlconstraint = 'filter=="%s"' % filterBand
    else:
        # set up the propID and units on the ra, dec
        if slair:  # no prop ID; only WFD is simulated.
            wfdWhere = ''
            raDecInDeg = True
        else:
            propIds, propTags = opsdb.fetchPropInfo()
            wfdWhere = '%s and ' % opsdb.createSQLWhere('WFD', propTags)
            raDecInDeg = opsdb.raDecInDeg
        # set up the year cutoff
        if cutOffYear is not None:
            nightCutOff = (cutOffYear) * 365.25
            sqlconstraint = '%snight<=%s and filter=="%s"' % (
                wfdWhere, nightCutOff, filterBand)
        else:
            sqlconstraint = '%sfilter=="%s"' % (wfdWhere, filterBand)
    print('# sqlconstraint: %s' % sqlconstraint)

    # ------------------------------------------------------------------------
    # setup all the slicers
    slicer = {}
    stackerList = {}

    if specifiedDith is not None:  # would like to add all the stackers first and then keep only the one that is specified
        bestDithOnly, noDithOnly = False, False

    if bestDithOnly:
        stackerList['RandomDitherFieldPerVisit'] = [
            mafStackers.RandomDitherFieldPerVisitStacker(degrees=raDecInDeg,
                                                         randomSeed=1000)
        ]
        slicer['RandomDitherFieldPerVisit'] = slicers.HealpixSlicer(
            lonCol='randomDitherFieldPerVisitRa',
            latCol='randomDitherFieldPerVisitDec',
            latLonDeg=raDecInDeg,
            nside=nside,
            useCache=False)
    else:
        if slair:
            slicer['NoDither'] = slicers.HealpixSlicer(lonCol='RA',
                                                       latCol='dec',
                                                       latLonDeg=raDecInDeg,
                                                       nside=nside,
                                                       useCache=False)
        else:
            slicer['NoDither'] = slicers.HealpixSlicer(lonCol='fieldRA',
                                                       latCol='fieldDec',
                                                       latLonDeg=raDecInDeg,
                                                       nside=nside,
                                                       useCache=False)
        if someDithOnly and not noDithOnly:
            #stackerList['RepulsiveRandomDitherFieldPerVisit'] = [myStackers.RepulsiveRandomDitherFieldPerVisitStacker(degrees=raDecInDeg,
            #                                                                                                          randomSeed=1000)]
            #slicer['RepulsiveRandomDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherFieldPerVisitRa',
            #                                                                    latCol='repulsiveRandomDitherFieldPerVisitDec',
            #                                                                    latLonDeg=raDecInDeg, nside=nside,
            #                                                                    useCache=False)
            slicer['SequentialHexDitherFieldPerNight'] = slicers.HealpixSlicer(
                lonCol='hexDitherFieldPerNightRa',
                latCol='hexDitherFieldPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['PentagonDitherPerSeason'] = slicers.HealpixSlicer(
                lonCol='pentagonDitherPerSeasonRa',
                latCol='pentagonDitherPerSeasonDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
        elif not noDithOnly:
            # random dithers on different timescales
            stackerList['RandomDitherPerNight'] = [
                mafStackers.RandomDitherPerNightStacker(degrees=raDecInDeg,
                                                        randomSeed=1000)
            ]
            stackerList['RandomDitherFieldPerNight'] = [
                mafStackers.RandomDitherFieldPerNightStacker(
                    degrees=raDecInDeg, randomSeed=1000)
            ]
            stackerList['RandomDitherFieldPerVisit'] = [
                mafStackers.RandomDitherFieldPerVisitStacker(
                    degrees=raDecInDeg, randomSeed=1000)
            ]

            # rep random dithers on different timescales
            #stackerList['RepulsiveRandomDitherPerNight'] = [myStackers.RepulsiveRandomDitherPerNightStacker(degrees=raDecInDeg,
            #                                                                                                randomSeed=1000)]
            #stackerList['RepulsiveRandomDitherFieldPerNight'] = [myStackers.RepulsiveRandomDitherFieldPerNightStacker(degrees=raDecInDeg,
            #                                                                                                          randomSeed=1000)]
            #stackerList['RepulsiveRandomDitherFieldPerVisit'] = [myStackers.RepulsiveRandomDitherFieldPerVisitStacker(degrees=raDecInDeg,
            #                                                                                                          randomSeed=1000)]
            # set up slicers for different dithers
            # random dithers on different timescales
            slicer['RandomDitherPerNight'] = slicers.HealpixSlicer(
                lonCol='randomDitherPerNightRa',
                latCol='randomDitherPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['RandomDitherFieldPerNight'] = slicers.HealpixSlicer(
                lonCol='randomDitherFieldPerNightRa',
                latCol='randomDitherFieldPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['RandomDitherFieldPerVisit'] = slicers.HealpixSlicer(
                lonCol='randomDitherFieldPerVisitRa',
                latCol='randomDitherFieldPerVisitDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            # rep random dithers on different timescales
            #slicer['RepulsiveRandomDitherPerNight'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherPerNightRa',
            #                                                               latCol='repulsiveRandomDitherPerNightDec',
            #                                                               latLonDeg=raDecInDeg, nside=nside, useCache=False)
            #slicer['RepulsiveRandomDitherFieldPerNight'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherFieldPerNightRa',
            #                                                                    latCol='repulsiveRandomDitherFieldPerNightDec',
            #                                                                    latLonDeg=raDecInDeg, nside=nside,
            #                                                                    useCache=False)
            #slicer['RepulsiveRandomDitherFieldPerVisit'] = slicers.HealpixSlicer(lonCol='repulsiveRandomDitherFieldPerVisitRa',
            #                                                                    latCol='repulsiveRandomDitherFieldPerVisitDec',
            #                                                                    latLonDeg=raDecInDeg, nside=nside,
            #                                                                    useCache=False)
            # spiral dithers on different timescales
            slicer['FermatSpiralDitherPerNight'] = slicers.HealpixSlicer(
                lonCol='fermatSpiralDitherPerNightRa',
                latCol='fermatSpiralDitherPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['FermatSpiralDitherFieldPerNight'] = slicers.HealpixSlicer(
                lonCol='fermatSpiralDitherFieldPerNightRa',
                latCol='fermatSpiralDitherFieldPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['FermatSpiralDitherFieldPerVisit'] = slicers.HealpixSlicer(
                lonCol='fermatSpiralDitherFieldPerVisitRa',
                latCol='fermatSpiralDitherFieldPerVisitDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            # hex dithers on different timescales
            slicer['SequentialHexDitherPerNight'] = slicers.HealpixSlicer(
                lonCol='hexDitherPerNightRa',
                latCol='hexDitherPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['SequentialHexDitherFieldPerNight'] = slicers.HealpixSlicer(
                lonCol='hexDitherFieldPerNightRa',
                latCol='hexDitherFieldPerNightDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['SequentialHexDitherFieldPerVisit'] = slicers.HealpixSlicer(
                lonCol='hexDitherFieldPerVisitRa',
                latCol='hexDitherFieldPerVisitDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            # per season dithers
            slicer['PentagonDitherPerSeason'] = slicers.HealpixSlicer(
                lonCol='pentagonDitherPerSeasonRa',
                latCol='pentagonDitherPerSeasonDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['PentagonDiamondDitherPerSeason'] = slicers.HealpixSlicer(
                lonCol='pentagonDiamondDitherPerSeasonRa',
                latCol='pentagonDiamondDitherPerSeasonDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
            slicer['SpiralDitherPerSeason'] = slicers.HealpixSlicer(
                lonCol='spiralDitherPerSeasonRa',
                latCol='spiralDitherPerSeasonDec',
                latLonDeg=raDecInDeg,
                nside=nside,
                useCache=False)
    if specifiedDith is not None:
        stackerList_, slicer_ = {}, {}
        if specifiedDith in slicer.keys():
            if specifiedDith.__contains__(
                    'Random'
            ):  # only Random dithers have a stacker object for rand seed specification
                stackerList_[specifiedDith] = stackerList[specifiedDith]
            slicer_[specifiedDith] = slicer[specifiedDith]
        else:
            raise ValueError(
                'Invalid value for specifiedDith: %s. Allowed values include one of the following:\n%s'
                % (specifiedDith, slicer.keys()))
        stackerList, slicer = stackerList_, slicer_

    # ------------------------------------------------------------------------
    if slair:
        m5Col = 'fivesigmadepth'
    else:
        m5Col = 'fiveSigmaDepth'
    # set up the metric
    if includeDustExtinction:
        # include dust extinction when calculating the co-added depth
        coaddMetric = metrics.ExgalM5(m5Col=m5Col, lsstFilter=filterBand)
    else:
        coaddMetric = metrics.Coaddm5Metric(m5col=m5col)
    dustMap = maps.DustMap(
        interp=False, nside=nside
    )  # include dustMap; actual in/exclusion of dust is handled by the galaxyCountMetric

    # ------------------------------------------------------------------------
    # set up the bundle
    coaddBundle = {}
    for dither in slicer:
        if dither in stackerList:
            coaddBundle[dither] = metricBundles.MetricBundle(
                coaddMetric,
                slicer[dither],
                sqlconstraint,
                stackerList=stackerList[dither],
                runName=runName,
                metadata=dither,
                mapsList=[dustMap])
        else:
            coaddBundle[dither] = metricBundles.MetricBundle(
                coaddMetric,
                slicer[dither],
                sqlconstraint,
                runName=runName,
                metadata=dither,
                mapsList=[dustMap])

    # ------------------------------------------------------------------------
    # run the analysis
    if includeDustExtinction:
        print('\n# Running coaddBundle with dust extinction ...')
    else:
        print('\n# Running coaddBundle without dust extinction ...')
    cGroup = metricBundles.MetricBundleGroup(coaddBundle,
                                             opsdb,
                                             outDir=outDir,
                                             resultsDb=resultsDb,
                                             saveEarly=False)
    cGroup.runAll()

    # ------------------------------------------------------------------------
    # plot and save the data
    plotBundleMaps(path,
                   outDir,
                   coaddBundle,
                   dataLabel='$%s$-band Coadded Depth' % filterBand,
                   filterBand=filterBand,
                   dataName='%s-band Coadded Depth' % filterBand,
                   skymap=plotSkymap,
                   powerSpectrum=plotPowerSpectrum,
                   cartview=plotCartview,
                   colorMin=unmaskedColorMin,
                   colorMax=unmaskedColorMax,
                   nTicks=nTicks,
                   showPlots=showPlots,
                   saveFigs=saveFigs,
                   outDirNameForSavedFigs='coaddM5Plots_unmaskedBorders')
    print('\n# Done saving plots without border masking.\n')

    # ------------------------------------------------------------------------
    plotHandler = plots.PlotHandler(outDir=outDir,
                                    resultsDb=resultsDb,
                                    thumbnail=False,
                                    savefig=False)

    print(
        '# Number of pixels in the survey region (before masking the border):')
    for dither in coaddBundle:
        print(
            '  %s: %s' %
            (dither,
             len(np.where(coaddBundle[dither].metricValues.mask == False)[0])))

    # ------------------------------------------------------------------------
    # save the unmasked data?
    if saveunMaskedCoaddData:
        outDir_new = 'unmaskedCoaddData'
        if not os.path.exists('%s%s/%s' % (path, outDir, outDir_new)):
            os.makedirs('%s%s/%s' % (path, outDir, outDir_new))
        saveBundleData_npzFormat('%s%s/%s' % (path, outDir, outDir_new),
                                 coaddBundle, 'coaddM5Data_unmasked',
                                 filterBand)

    # ------------------------------------------------------------------------
    # mask the edges
    print('\n# Masking the edges for coadd ...')
    coaddBundle = maskingAlgorithmGeneralized(
        coaddBundle,
        plotHandler,
        dataLabel='$%s$-band Coadded Depth' % filterBand,
        nside=nside,
        pixelRadius=pixelRadiusForMasking,
        plotIntermediatePlots=False,
        plotFinalPlots=False,
        printFinalInfo=True)
    if (pixelRadiusForMasking > 0):
        # plot and save the masked data
        plotBundleMaps(path,
                       outDir,
                       coaddBundle,
                       dataLabel='$%s$-band Coadded Depth' % filterBand,
                       filterBand=filterBand,
                       dataName='%s-band Coadded Depth' % filterBand,
                       skymap=plotSkymap,
                       powerSpectrum=plotPowerSpectrum,
                       cartview=plotCartview,
                       colorMin=maskedColorMin,
                       colorMax=maskedColorMax,
                       nTicks=nTicks,
                       showPlots=showPlots,
                       saveFigs=saveFigs,
                       outDirNameForSavedFigs='coaddM5Plots_maskedBorders')
        print('\n# Done saving plots with border masking. \n')

    # ------------------------------------------------------------------------
    # Calculate total power
    summarymetric = metrics.TotalPowerMetric()
    for dither in coaddBundle:
        coaddBundle[dither].setSummaryMetrics(summarymetric)
        coaddBundle[dither].computeSummaryStats()
        print('# Total power for %s case is %f.' %
              (dither, coaddBundle[dither].summaryValues['TotalPower']))
    print('')

    # ------------------------------------------------------------------------
    # run the alm analysis
    if almAnalysis:
        almPlots(path,
                 outDir,
                 copy.deepcopy(coaddBundle),
                 nside=nside,
                 filterband=filterBand,
                 raRange=raRange,
                 decRange=decRange,
                 showPlots=showPlots)
    # ------------------------------------------------------------------------
    # save the masked data?
    if saveMaskedCoaddData and (pixelRadiusForMasking > 0):
        outDir_new = 'maskedCoaddData'
        if not os.path.exists('%s%s/%s' % (path, outDir, outDir_new)):
            os.makedirs('%s%s/%s' % (path, outDir, outDir_new))
        saveBundleData_npzFormat('%s%s/%s' % (path, outDir, outDir_new),
                                 coaddBundle, 'coaddM5Data_masked', filterBand)

    # ------------------------------------------------------------------------
    # plot comparison plots
    if len(coaddBundle.keys()) > 1:  # more than one key
        # set up the directory
        outDir_comp = 'coaddM5ComparisonPlots'
        if not os.path.exists('%s%s/%s' % (path, outDir, outDir_comp)):
            os.makedirs('%s%s/%s' % (path, outDir, outDir_comp))
        # ------------------------------------------------------------------------
        # plot for the power spectra
        cl = {}
        for dither in plotColor:
            if dither in coaddBundle:
                cl[dither] = hp.anafast(hp.remove_dipole(
                    coaddBundle[dither].metricValues.filled(
                        coaddBundle[dither].slicer.badval)),
                                        lmax=500)
                ell = np.arange(np.size(cl[dither]))
                plt.plot(ell, (cl[dither] * ell * (ell + 1)) / 2.0 / np.pi,
                         color=plotColor[dither],
                         linestyle='-',
                         label=dither)
        plt.xlabel(r'$\ell$')
        plt.ylabel(r'$\ell(\ell+1)C_\ell/(2\pi)$')
        plt.xlim(0, 500)
        fig = plt.gcf()
        fig.set_size_inches(12.5, 10.5)
        leg = plt.legend(labelspacing=0.001)
        for legobj in leg.legendHandles:
            legobj.set_linewidth(4.0)
        filename = 'powerspectrum_comparison_all.png'
        plt.savefig('%s%s/%s/%s' % (path, outDir, outDir_comp, filename),
                    bbox_inches='tight',
                    format='png')
        plt.show()

        # create the histogram
        scale = hp.nside2pixarea(nside, degrees=True)

        def tickFormatter(y, pos):
            return '%d' % (y * scale)  # convert pixel count to area

        binsize = 0.01
        for dither in plotColor:
            if dither in coaddBundle:
                ind = np.where(
                    coaddBundle[dither].metricValues.mask == False)[0]
                binAll = int(
                    (max(coaddBundle[dither].metricValues.data[ind]) -
                     min(coaddBundle[dither].metricValues.data[ind])) /
                    binsize)
                plt.hist(coaddBundle[dither].metricValues.data[ind],
                         bins=binAll,
                         label=dither,
                         histtype='step',
                         color=plotColor[dither])
        ax = plt.gca()
        ymin, ymax = ax.get_ylim()
        nYticks = 10.
        wantedYMax = ymax * scale
        wantedYMax = 10. * np.ceil(float(wantedYMax) / 10.)
        increment = 5. * np.ceil(float(wantedYMax / nYticks) / 5.)
        wantedArray = np.arange(0, wantedYMax, increment)
        ax.yaxis.set_ticks(wantedArray / scale)
        ax.yaxis.set_major_formatter(FuncFormatter(tickFormatter))
        plt.xlabel('$%s$-band Coadded Depth' % filterBand)
        plt.ylabel('Area (deg$^2$)')
        fig = plt.gcf()
        fig.set_size_inches(12.5, 10.5)
        leg = plt.legend(labelspacing=0.001, loc=2)
        for legobj in leg.legendHandles:
            legobj.set_linewidth(2.0)
        filename = 'histogram_comparison.png'
        plt.savefig('%s%s/%s/%s' % (path, outDir, outDir_comp, filename),
                    bbox_inches='tight',
                    format='png')
        plt.show()
        # ------------------------------------------------------------------------
        # plot power spectra for the separte panel
        totKeys = len(list(coaddBundle.keys()))
        if (totKeys > 1):
            plt.clf()
            nCols = 2
            nRows = int(np.ceil(float(totKeys) / nCols))
            fig, ax = plt.subplots(nRows, nCols)
            plotRow = 0
            plotCol = 0
            for dither in list(plotColor.keys()):
                if dither in list(coaddBundle.keys()):
                    ell = np.arange(np.size(cl[dither]))
                    ax[plotRow, plotCol].plot(ell, (cl[dither] * ell *
                                                    (ell + 1)) / 2.0 / np.pi,
                                              color=plotColor[dither],
                                              label=dither)
                    if (plotRow == nRows - 1):
                        ax[plotRow, plotCol].set_xlabel(r'$\ell$')
                    ax[plotRow,
                       plotCol].set_ylabel(r'$\ell(\ell+1)C_\ell/(2\pi)$')
                    ax[plotRow,
                       plotCol].yaxis.set_major_locator(MaxNLocator(3))
                    if (dither != 'NoDither'):
                        ax[plotRow, plotCol].set_ylim(0, 0.0035)
                    ax[plotRow, plotCol].set_xlim(0, 500)
                    plotRow += 1
                    if (plotRow > nRows - 1):
                        plotRow = 0
                        plotCol += 1
            fig.set_size_inches(20, int(nRows * 30 / 7.))
            filename = 'powerspectrum_sepPanels.png'
            plt.savefig('%s%s/%s/%s' % (path, outDir, outDir_comp, filename),
                        bbox_inches='tight',
                        format='png')
            plt.show()
    return coaddBundle, outDir
def plotBar(mixcrlist, plotcdr3counts, cdr3colors, axisBreak):
    axisBreak = list(map(lambda x: float(x), axisBreak))
    # 'break' or 'cut-out' the y-axis
    # into two portions - use the top (ax) for the outliers, and the bottom
    # (ax2) for the details of the majority of our data
    fig, (ax, ax2) = plt.subplots(2,
                                  1,
                                  sharex=True,
                                  gridspec_kw={'height_ratios': [1, 2]})

    for k, f in enumerate(mixcrlist):
        # Plot same thing in both ax and ax2
        bar = {}
        bar2 = {}
        nextbot = 0

        # If empty...
        if len(plotcdr3counts[f].keys()) == 0:
            data = [0] * len(mixcrlist)
            data[k] = 1.
            bar[index + 1] = ax.bar(np.arange(len(mixcrlist)),
                                    data,
                                    width=0.2,
                                    linewidth=0.1,
                                    color='black',
                                    bottom=nextbot)

            bar2[index + 1] = ax2.bar(np.arange(len(mixcrlist)),
                                      data,
                                      width=0.2,
                                      linewidth=0.1,
                                      color='black',
                                      bottom=nextbot)

        # Color the rest of the stacked bars
        for index, cdr3 in enumerate(
                sorted(plotcdr3counts[f].keys(),
                       key=lambda u: plotcdr3counts[f][u])):
            data = [0] * len(mixcrlist)
            data[k] = plotcdr3counts[f][cdr3]

            if cdr3 != 'Others':
                bar[index + 1] = ax.bar(np.arange(len(mixcrlist)),
                                        data,
                                        width=0.7,
                                        linewidth=0.1,
                                        color=cdr3colors[cdr3],
                                        bottom=nextbot)

                bar2[index + 1] = ax2.bar(np.arange(len(mixcrlist)),
                                          data,
                                          width=0.7,
                                          linewidth=0.1,
                                          color=cdr3colors[cdr3],
                                          bottom=nextbot)
                nextbot += plotcdr3counts[f][cdr3]

        # Color 'Others' bar last if it exists
        if 'Others' in plotcdr3counts[f].keys():
            data = [0] * len(mixcrlist)
            data[k] = plotcdr3counts[f]['Others']
            bar[0] = ax.bar(np.arange(len(data)),
                            data,
                            width=0.7,
                            linewidth=0.1,
                            color='grey',
                            bottom=nextbot)

            bar2[0] = ax2.bar(np.arange(len(data)),
                              data,
                              width=0.7,
                              linewidth=0.1,
                              color='grey',
                              bottom=nextbot)

    # Zoom-in / limit the view to different portion of the data

    ax.set_ylim(axisBreak[1], 1.)
    ax2.set_ylim(0., axisBreak[0])

    # Hide the spines b/t ax and ax2
    ax2.spines['top'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    #ax.xaxis.tick_bottom()
    #ax2.xaxis.tick_top()
    #ax2.tick_params(labeltop=False)

    # set other bar parameters
    ax.set_xticks(np.arange(len(mixcrlist)))
    xtickNames = ax.set_xticklabels(['' if x == '-' else x for x in mixcrlist])
    plt.setp(xtickNames, rotation=0, fontsize=10)
    ax.tick_params(axis='y', direction='in', length=1)
    ax2.tick_params(axis='y', direction='in', length=1)
    ax.tick_params(axis='x', length=0)
    ax2.tick_params(axis='x', length=0, labelsize=24)
    ax2.yaxis.set_major_locator(plt.MaxNLocator(4))
    ax.yaxis.set_major_formatter(
        FuncFormatter(lambda x, pos: '%s%%' % (int(x * 100.0))))
    ax2.yaxis.set_major_formatter(
        FuncFormatter(lambda x, pos: '%s%%' % (int(x * 100.0))))
    #ax.set_title('%s (%s%%)     %s (%s%%)' % (name1, mut1, name2, mut2))
    #ax[n+1].axhline(y=0.02)

    pdf = PdfPages('bar.pdf')
    plt.tight_layout()
    plt.savefig(pdf, format='pdf')
    plt.close()
    pdf.close()
Пример #25
0
X, lamda = np.meshgrid(x, lam)
lamda[1] = 0
lamda[0] = 0
lamda[2] = 0
lamda[3] = 0

####transition Xf
Xf = xf.T
#plot
fig, ax = plt.subplots()
im = ax.pcolormesh(X, lamda, Xf, cmap=plt.get_cmap('rainbow'))
fig.colorbar(im, ax=ax)


#fig.savefig('Xf.png',dpi=200)
#set ticker
def pi_formatter(x, pos):
    delta_x = 60 * 1e-6 / 2400
    a = delta_x * x * 1e6
    return "%d" % a


ax.xaxis.set_major_locator(MultipleLocator(5000))
ax.xaxis.set_major_formatter(FuncFormatter(pi_formatter))
ax.xaxis.set_minor_locator(MultipleLocator(500))
ax.set_xlabel('um')
ax.set_ylabel('um')
#print and save
plt.show()
fig.savefig("fig/lamda.png", dpi=200)
Пример #26
0
    def plot_various_confidences(
        self,
        graph_name,
        drop_rate,
        data_format,
        Y,
        directory='./attack_data/',
        confs=(0.0, 10.0, 20.0, 30.0, 40.0),
        get_attack_data_name=lambda c: "example_carlini_" + str(c)):
        """
        Test defense performance against Carlini L2 attack of various confidences.

        graph_name: Name of graph file.
        drop_rate: How many normal examples should each detector drops?
        idx_file: Index of adversarial examples in standard test set.
        confs: A series of confidence to test against.
        get_attack_data_name: Function mapping confidence to corresponding file.
        """
        import matplotlib.pyplot as plt
        import pylab

        pylab.rcParams['figure.figsize'] = 6, 4
        fig = plt.figure(1, (6, 4))
        ax = fig.add_subplot(1, 1, 1)

        det_only = []
        ref_only = []
        both = []
        none = []

        print("Drop Rate:", drop_rate)
        thrs = self.operator.get_thrs(drop_rate)

        all_pass, _detector = self.operator.filter(
            self.operator.data.test_data, thrs)
        all_on_acc, _, _, _ = self.get_normal_acc(all_pass)

        print(_detector)
        print("Classification accuracy with all defense on:", all_on_acc)

        for confidence in confs:
            f = get_attack_data_name(confidence)
            attack_data = AttackData(f,
                                     Y,
                                     "Carlini L2 " + str(confidence),
                                     directory=directory,
                                     input_data_format=CHANNELS_LAST,
                                     data_format=data_format)
            # compute number of all input data and filter out valid data
            total = len(attack_data.data)
            valid_adv_idx = np.argwhere(
                np.sum(attack_data.data, axis=(1, 2,
                                               3)) > [0] * total).flatten()
            attack_data.data = attack_data.data[valid_adv_idx]
            attack_data.labels = attack_data.labels[valid_adv_idx]
            self.load_data(attack_data)

            print("Confidence:", confidence)
            valid_adv_len = len(valid_adv_idx)
            print("valid attack %d/%d" % (valid_adv_len, total))
            all_pass, detector_breakdown = self.operator.filter(
                self.untrusted_data.data, thrs)
            both_acc, det_only_acc, ref_only_acc, none_acc = self.get_attack_acc(
                all_pass)
            print(detector_breakdown)
            both.append(both_acc)
            det_only.append(det_only_acc)
            ref_only.append(ref_only_acc)
            none.append(none_acc)

        size = 2.5
        plt.plot(confs,
                 none,
                 c="green",
                 label="No fefense",
                 marker="x",
                 markersize=size)
        plt.plot(confs,
                 det_only,
                 c="orange",
                 label="With detector",
                 marker="o",
                 markersize=size)
        plt.plot(confs,
                 ref_only,
                 c="blue",
                 label="With reformer",
                 marker="^",
                 markersize=size)
        plt.plot(confs,
                 both,
                 c="red",
                 label="With detector & reformer",
                 marker="s",
                 markersize=size)

        pylab.legend(loc='lower left',
                     bbox_to_anchor=(0.02, 0.1),
                     prop={'size': 8})
        plt.grid(linestyle='dotted')
        plt.xlabel(r"Confidence in Carlini $L^2$ attack")
        plt.ylabel("Classification accuracy")
        plt.xlim(min(confs) - 1.0, max(confs) + 1.0)
        plt.ylim(-0.05, 1.05)
        ax.yaxis.set_major_formatter(FuncFormatter('{0:.0%}'.format))

        save_path = os.path.join(self.graph_dir, graph_name + ".pdf")
        plt.savefig(save_path)
        plt.clf()
Пример #27
0
# draw coastlines, fill land and lake areas.
m.drawcoastlines()
m.fillcontinents(color='coral', lake_color='aqua')
# background color will be used for oceans.
m.drawmapboundary(fill_color='aqua')
# get axes instance.
ax = plt.gca()


# add custom ticks.
# This only works for projection='cyl'.
def xformat(x, pos=None):
    return lon2str(x)


xformatter = FuncFormatter(xformat)
ax.xaxis.set_major_formatter(xformatter)


def yformat(y, pos=None):
    return lat2str(y)


yformatter = FuncFormatter(yformat)
ax.yaxis.set_major_formatter(yformatter)
ax.fmt_xdata = lambda x: lon2str(x)
ax.fmt_ydata = lambda y: lat2str(y)
ax.grid()
ax.set_title('Hawaii')

# (2) use Basemap labelling methods, but pass a
Пример #28
0
def main(dictAlg, order=None, outputdir='.', info='default',
         dimension=None, parentHtmlFileName=None, plotType=PlotType.ALG, settings = genericsettings):
    """Generates a figure showing the performance of algorithms.

    From a dictionary of :py:class:`DataSetList` sorted by algorithms,
    generates the cumulative distribution function of the bootstrap
    distribution of ERT for algorithms on multiple functions for
    multiple targets altogether.

    :param dict dictAlg: dictionary of :py:class:`DataSetList` instances
                         one instance is equivalent to one algorithm,
    :param list targets: target function values
    :param list order: sorted list of keys to dictAlg for plotting order
    :param str outputdir: output directory
    :param str info: output file name suffix
    :param str parentHtmlFileName: defines the parent html page 

    """
    global x_limit  # late assignment of default, because it can be set to None in config 
    global divide_by_dimension  # not fully implemented/tested yet
    if 'x_limit' not in globals() or x_limit is None:
        x_limit = x_limit_default

    tmp = pp.dictAlgByDim(dictAlg)
    algorithms_with_data = [a for a in dictAlg.keys() if dictAlg[a] != []]
    # algorithms_with_data.sort()  # dictAlg now is an OrderedDict, hence sorting isn't desired

    if len(algorithms_with_data) > 1 and len(tmp) != 1 and dimension is None:
        raise ValueError('We never integrate over dimension for more than one algorithm.')
    if dimension is not None:
        if dimension not in tmp.keys():
            raise ValueError('dimension %d not in dictAlg dimensions %s'
                             % (dimension, str(tmp.keys())))
        tmp = {dimension: tmp[dimension]}
    dimList = list(tmp.keys())

    # The sort order will be defined inside this function.    
    if plotType == PlotType.DIM:
        order = []

    # Collect data
    # Crafting effort correction: should we consider any?
    CrEperAlg = {}
    for alg in algorithms_with_data:
        CrE = 0.
        if 1 < 3 and dictAlg[alg][0].algId == 'GLOBAL':
            tmp = dictAlg[alg].dictByNoise()
            assert len(tmp.keys()) == 1
            if list(tmp.keys())[0] == 'noiselessall':
                CrE = 0.5117
            elif list(tmp.keys())[0] == 'nzall':
                CrE = 0.6572
        if plotType == PlotType.DIM:
            for dim in dimList:
                keyValue = '%d-D' % (dim)
                CrEperAlg[keyValue] = CrE
        elif plotType == PlotType.FUNC:
            tmp = pp.dictAlgByFun(dictAlg)
            for f, dictAlgperFunc in tmp.items():
                keyValue = 'f%d' % (f)
                CrEperAlg[keyValue] = CrE
        else:
            CrEperAlg[alg] = CrE
        if CrE != 0.0:
            print('Crafting effort for', alg, 'is', CrE)

    dictData = {}  # list of (ert per function) per algorithm
    dictMaxEvals = {}  # list of (maxevals per function) per algorithm

    # funcsolved = [set()] * len(targets) # number of functions solved per target
    xbest = []
    maxevalsbest = []
    target_values = testbedsettings.current_testbed.pprldmany_target_values

    dictDimList = pp.dictAlgByDim(dictAlg)
    dims = sorted(dictDimList)
    for i, dim in enumerate(dims):
        divisor = dim if divide_by_dimension else 1

        dictDim = dictDimList[dim]
        dictFunc = pp.dictAlgByFun(dictDim)
        for f, dictAlgperFunc in sorted(dictFunc.items()):
            # print(target_values((f, dim)))
            for j, t in enumerate(target_values((f, dim))):
                # for j, t in enumerate(testbedsettings.current_testbed.ecdf_target_values(1e2, f)):
                # funcsolved[j].add(f)

                for alg in algorithms_with_data:
                    x = [np.inf] * perfprofsamplesize
                    runlengthunsucc = []
                    try:
                        entry = dictAlgperFunc[alg][0]  # one element per fun and per dim.
                        evals = entry.detEvals([t])[0]
                        assert entry.dim == dim
                        runlengthsucc = evals[np.isnan(evals) == False] / divisor
                        runlengthunsucc = entry.maxevals[np.isnan(evals)] / divisor
                        if len(runlengthsucc) > 0:  # else x == [inf, inf,...]
                            if testbedsettings.current_testbed.instances_are_uniform:
                                x = toolsstats.drawSP(runlengthsucc, runlengthunsucc,
                                                      percentiles=[50],
                                                      samplesize=perfprofsamplesize)[1]
                            else:
                                nruns = len(runlengthsucc) + len(runlengthunsucc)
                                if perfprofsamplesize % nruns:
                                    warnings.warn("without simulated restarts nbsamples=%d"
                                                  " should be a multiple of nbruns=%d"
                                                  % (perfprofsamplesize, nruns))
                                idx = toolsstats.randint_derandomized(nruns, size=perfprofsamplesize)
                                x = np.hstack((runlengthsucc, len(runlengthunsucc) * [np.inf]))[idx]
                    except (KeyError, IndexError):
                        # set_trace()
                        warntxt = ('Data for algorithm %s on function %d in %d-D '
                                   % (alg, f, dim)
                                   + 'are missing.\n')
                        warnings.warn(warntxt)

                    keyValue = alg
                    if plotType == PlotType.DIM:
                        keyValue = '%d-D' % (dim)
                        if keyValue not in order:
                            order.append(keyValue)
                    elif plotType == PlotType.FUNC:
                        keyValue = 'f%d' % (f)
                    dictData.setdefault(keyValue, []).extend(x)
                    dictMaxEvals.setdefault(keyValue, []).extend(runlengthunsucc)

            displaybest = plotType == PlotType.ALG
            if displaybest:
                # set_trace()
                refalgentries = bestalg.load_reference_algorithm(testbedsettings.current_testbed.reference_algorithm_filename)

                if not refalgentries:
                    displaybest = False
                else:
                    refalgentry = refalgentries[(dim, f)]
                    refalgevals = refalgentry.detEvals(target_values((f, dim)))
                    # print(refalgevals)
                    for j in range(len(refalgevals[0])):
                        if refalgevals[1][j]:
                            evals = refalgevals[0][j]
                            # set_trace()
                            assert dim == refalgentry.dim
                            runlengthsucc = evals[np.isnan(evals) == False] / divisor
                            runlengthunsucc = refalgentry.maxevals[refalgevals[1][j]][np.isnan(evals)] / divisor
                            x = toolsstats.drawSP(runlengthsucc, runlengthunsucc,
                                                  percentiles=[50],
                                                  samplesize=perfprofsamplesize)[1]
                        else:
                            x = perfprofsamplesize * [np.inf]
                            runlengthunsucc = []
                        xbest.extend(x)
                        maxevalsbest.extend(runlengthunsucc)

    if order is None:
        order = dictData.keys()

    # Display data
    lines = []
    if displaybest:
        args = {'ls': '-', 'linewidth': 4, 'marker': 'D', 'markersize': 11.,
                'markeredgewidth': 1.5, 'markerfacecolor': refcolor,
                'markeredgecolor': refcolor, 'color': refcolor,
                'label': testbedsettings.current_testbed.reference_algorithm_displayname,
                'zorder': -1}
        lines.append(plotdata(np.array(xbest), x_limit, maxevalsbest,
                              CrE=0., **args))

    def algname_to_label(algname, dirname=None):
        """to be extended to become generally useful"""
        if isinstance(algname, (tuple, list)):  # not sure this is needed
            return ' '.join([str(name) for name in algname])
        return str(algname)

    plotting_style_list = ppfig.get_plotting_styles(order)
    for plotting_style in plotting_style_list:
        for i, alg in enumerate(plotting_style.algorithm_list):
            try:
                data = dictData[alg]
                maxevals = dictMaxEvals[alg]
            except KeyError:
                continue

            args = styles[i % len(styles)]
            args = args.copy()
            args['linewidth'] = 1.5
            args['markersize'] = 12.
            args['markeredgewidth'] = 1.5
            args['markerfacecolor'] = 'None'
            args['markeredgecolor'] = args['color']
            args['label'] = algname_to_label(alg)
            if plotType == PlotType.DIM:
                args['marker'] = genericsettings.dim_related_markers[i]
                args['markeredgecolor'] = genericsettings.dim_related_colors[i]
                args['color'] = genericsettings.dim_related_colors[i]

                # args['markevery'] = perfprofsamplesize # option available in latest version of matplotlib
                # elif len(show_algorithms) > 0:
                # args['color'] = 'wheat'
                # args['ls'] = '-'
                # args['zorder'] = -1
            # plotdata calls pprldistr.plotECDF which calls ppfig.plotUnifLog... which does the work

            args.update(plotting_style.pprldmany_styles)

            lines.append(plotdata(np.array(data), x_limit, maxevals,
                                  CrE=CrEperAlg[alg], **args))

    labels, handles = plotLegend(lines, x_limit)
    if True:  # isLateXLeg:
        if info:
            file_name = os.path.join(outputdir, '%s_%s.tex' % (genericsettings.pprldmany_file_name, info))
        else:
            file_name = os.path.join(outputdir, '%s.tex' % genericsettings.pprldmany_file_name)
        with open(file_name, 'w') as file_obj:
            file_obj.write(r'\providecommand{\nperfprof}{7}')
            algtocommand = {}  # latex commands
            for i, alg in enumerate(order):
                tmp = r'\alg%sperfprof' % pptex.numtotext(i)
                file_obj.write(r'\providecommand{%s}{\StrLeft{%s}{\nperfprof}}' %
                        (tmp, toolsdivers.str_to_latex(
                            toolsdivers.strip_pathname2(algname_to_label(alg)))))
                algtocommand[algname_to_label(alg)] = tmp
            if displaybest:
                tmp = r'\algzeroperfprof'
                refalgname = testbedsettings.current_testbed.reference_algorithm_displayname
                file_obj.write(r'\providecommand{%s}{%s}' % (tmp, refalgname))
                algtocommand[algname_to_label(refalgname)] = tmp

            commandnames = []
            for label in labels:
                commandnames.append(algtocommand[label])
            # file_obj.write(headleg)
            if len(
                    order) > 28:  # latex sidepanel won't work well for more than 25 algorithms, but original labels are also clipped
                file_obj.write(r'\providecommand{\perfprofsidepanel}{\mbox{%s}\vfill\mbox{%s}}'
                        % (commandnames[0], commandnames[-1]))
            else:
                fontsize_command = r'\tiny{}' if len(order) > 19 else ''
                file_obj.write(r'\providecommand{\perfprofsidepanel}{{%s\mbox{%s}' %
                        (fontsize_command, commandnames[0]))  # TODO: check len(labels) > 0
                for i in range(1, len(labels)):
                    file_obj.write('\n' + r'\vfill \mbox{%s}' % commandnames[i])
                file_obj.write('}}\n')
            # file_obj.write(footleg)
            if genericsettings.verbose:
                print('Wrote right-hand legend in %s' % file_name)

    if info:
        figureName = os.path.join(outputdir, '%s_%s' % (genericsettings.pprldmany_file_name, info))
    else:
        figureName = os.path.join(outputdir, '%s' % genericsettings.pprldmany_file_name)
    # beautify(figureName, funcsolved, x_limit*x_annote_factor, False, fileFormat=figformat)
    beautify()

    if plotType == PlotType.FUNC:
        dictFG = pp.dictAlgByFuncGroup(dictAlg)
        dictKey = list(dictFG.keys())[0]
        functionGroups = dictAlg[list(dictAlg.keys())[0]].getFuncGroups()
        text = '%s\n%s, %d-D' % (testbedsettings.current_testbed.name,
                                 functionGroups[dictKey],
                                 dimList[0])
    else:
        text = '%s %s' % (testbedsettings.current_testbed.name,
                            ppfig.consecutiveNumbers(sorted(dictFunc.keys()), 'f'))
        if not (plotType == PlotType.DIM):
            text += ', %d-D' % dimList[0]
    # add information about smallest and largest target and their number
    text += '\n'
    targetstrings = target_values.labels()
    if isinstance(target_values, pp.RunlengthBasedTargetValues):
        text += (str(len(targetstrings)) + ' targets RLs/dim: ' +
                 targetstrings[0] + '..' +
                 targetstrings[len(targetstrings)-1] + '\n')
        text += '  from ' + testbedsettings.current_testbed.reference_algorithm_filename
    else:
        text += (str(len(targetstrings)) + ' targets: ' +
                 targetstrings[0] + '..' +
                 targetstrings[len(targetstrings)-1])        
    # add number of instances 
    text += '\n'
    num_of_instances = []
    for alg in algorithms_with_data:
        try:
            num_of_instances.append(len((dictAlgperFunc[alg])[0].instancenumbers))
        except IndexError:
            pass
    # issue a warning if number of instances is inconsistant, but always
    # display only the present number of instances, i.e. remove copies
    if len(set(num_of_instances)) > 1:
        warnings.warn('Number of instances inconsistent over all algorithms: %s instances found.' % str(num_of_instances))
    num_of_instances = set(num_of_instances)
    for n in num_of_instances:
        text += '%d, ' % n
            
    text = text.rstrip(', ')
    text += ' instances'

    plt.text(0.01, 0.99, text,
             horizontalalignment="left",
             verticalalignment="top",
             transform=plt.gca().transAxes,
             fontsize=0.6*label_fontsize)
    if len(dictFunc) == 1:
        plt.title(' '.join((str(list(dictFunc.keys())[0]),
                            testbedsettings.current_testbed.short_names[list(dictFunc.keys())[0]])),
                  fontsize=title_fontsize)
    a = plt.gca()

    plt.xlim(1e-0, x_limit)
    xmaxexp = int(np.floor(np.log10(x_limit)))
    xmajorticks = [10 ** exponent for exponent in range(0, xmaxexp + 1, 2)]
    xminorticks = [10 ** exponent for exponent in range(0, xmaxexp + 1)]
    def formatlabel(val, pos):
        labeltext = '{:d}'.format(int(round(np.log10(val))))
        return labeltext
    a.xaxis.set_major_locator(FixedLocator(xmajorticks))
    a.xaxis.set_major_formatter(FuncFormatter(formatlabel))
    a.xaxis.set_minor_locator(FixedLocator(xminorticks))
    a.xaxis.set_minor_formatter(NullFormatter())

    if save_figure:
        ppfig.save_figure(figureName,
                          dictAlg[algorithms_with_data[0]][0].algId,
                          layout_rect=(0, 0, 0.735, 1),
                          # Prevent clipping in matplotlib >=3:
                          # Relative additional space numbers are
                          # bottom, left, 1 - top, and 1 - right.
                          # bottom=0.13 still clips g in the log(#evals) xlabel
                          subplots_adjust=dict(bottom=0.135, right=0.735,
                                               top=0.92 if len(dictFunc) == 1 else 0.99  # space for a title
                                               ),
                          )
        if plotType == PlotType.DIM:
            file_name = genericsettings.pprldmany_file_name
            ppfig.save_single_functions_html(
                os.path.join(outputdir, file_name),
                '',  # algorithms names are clearly visible in the figure
                htmlPage=ppfig.HtmlPage.NON_SPECIFIED,
                parentFileName='../%s' % parentHtmlFileName if parentHtmlFileName else None,
                header=ppfig.pprldmany_per_func_dim_header)

    if close_figure:
        plt.close()
Пример #29
0
    which='both',  # both major and minor ticks are affected
    bottom='off',  # ticks along the bottom edge are off
    top='off',  # ticks along the top edge are off
    labelright='on',
    labelleft='off')  # labels along the bottom edge are off
yticks = ax2v.get_yticks()

ax2v.set_yticks([0, 500000, 1000000, 1500000])


def millions(x, pos):
    'The two args are the value and tick position'
    return '%0.1fM' % (x * 1e-6)


formatter = FuncFormatter(millions)
ax2v.yaxis.set_major_formatter(formatter)

equity['total'].plot(ax=ax1, figsize=[10, 8], legend=True)
equity['cash'].plot(ax=ax1, legend=True)
s['close'].plot(ax=ax2, legend=True)
positions.plot(ax=ax3, legend=True, secondary_y=True)

#
multi = MultiCursor(fig.canvas, (ax1, ax2, ax3),
                    color='gray',
                    lw=1,
                    ls=':',
                    horizOn=True)
# a = Cursor(ax2,useblit=True,color='gray', lw=1,ls=':',)
plt.subplots_adjust(left=0.07,
    # Formatting
    ax[row, col].set_xscale("log")
    ax[row, col].set_yscale("log")
    ax[row, col].grid(which="both", ls=":")
    ax[row, col].set_xlim(low * 1e+3 * min(c / FREQ),
                          high * 1e+3 * max(c / FREQ))
    ax[row, col].set_ylim(
        low * min(np.append(Fmin, flux[i][::2] - flux[i][1::2]) / Jy),
        high * max(np.append(Fmax, flux[i][::2] + flux[i][1::2]) / Jy))
    if j == 243: ax[row, col].set_ylim(0.3, )  # ylim exception
    # disable minor y tick labels
    ax[row, col].yaxis.set_minor_formatter(FormatStrFormatter(""))
    # format y axis tick labels
    ax[row, col].yaxis.set_major_formatter(
        FuncFormatter(lambda y, pos: ('{{:.{:1d}f}}'.format(
            int(np.maximum(-np.log10(np.abs(y)), 0)))).format(y)))
    # legend
    ax[row, col].annotate("$%d$" % j,
                          xy=(.97, .92),
                          xycoords="axes fraction",
                          size=10,
                          ha="right",
                          va="top",
                          bbox=dict(boxstyle="round", fc="w"))

    # Plotting
    # 1-sigma range
    ax[row, col].fill_between(1e+3 * c / FREQ,
                              Fmax / Jy,
                              Fmin / Jy,
                              color="lightgreen")