Plot loss curves given a calculation id and an asset ordinal.
170110
def plot_lc(calc_id, aid=None):
    
    # read the hazard data
    dstore = util.read(calc_id)
    dset = dstore['agg_curves-rlzs']
    if aid is None:  # plot the global curves
        plt = make_figure(dset.attrs['return_periods'], dset.value)
    else:
        sys.exit('Not implemented yet')
    plt.show()
592007
def plot_series_residuals(self, xres, varied_data, varied_idx, params, **kwargs):
        
        nf = len(self.f_cb(*self.pre_process(xres[0], params)))
        xerr = np.empty((xres.shape[0], nf))
        new_params = np.array(params)

        for idx, row in enumerate(xres):
            new_params[varied_idx] = varied_data[idx]
            xerr[idx, :] = self.f_cb(*self.pre_process(row, params))
        return self.plot_series(xerr, varied_data, varied_idx, **kwargs)
1212146
def traceplot(trace: sample_types, labels: List[Union[str, Tuple[str, str]]] = None, ax: Any = None,
              x0: int = 0) -> Any:
    

    if labels is None:
        labels = list(trace.keys())

    if ax is None:
        _, ax = plt.subplots(len(labels), 1, squeeze=False)

    for index, label in enumerate(labels):
        data = [sample for sample in trace[label]]

        ax[index][0].set_title(label)
        ax[index][0].plot(__integer_xaxis(ax[index][0], x0, len(data)), data)

    __pause_for_crude_animation()

    return ax
849104
def plot(self, plot_cmd=None, tf=lambda y: y):
        """"""
        if not plot_cmd:
            plot_cmd = self.plot_cmd
        colors = 'bgrcmyk'
        pyplot.hold(False)
        res = self.res

        flatx, flatf = self.flattened()
        minf = np.inf
        for i in flatf:
            minf = min((minf, min(flatf[i])))
        addf = 1e-9 - minf if minf <= 1e-9 else 0
        for i in sorted(res.keys()):  # we plot not all values here
            if isinstance(i, int):
                color = colors[i % len(colors)]
                arx = sorted(res[i].keys())
                plot_cmd(arx, [tf(np.median(res[i][x]) + addf) for x in arx], color + '-')
                pyplot.text(arx[-1], tf(np.median(res[i][arx[-1]])), i)
                pyplot.hold(True)
                plot_cmd(flatx[i], tf(np.array(flatf[i]) + addf), color + 'o')
        pyplot.ylabel('f + ' + str(addf))
        pyplot.draw()
        pyplot.ion()
        pyplot.show()
        # raw_input('press return')
        return self
342707
def get_loss_ratios(self):
        
        lst = [('user_provided', numpy.bool)]
        for cp in self.curve_params:
            lst.append((cp.loss_type, F32, len(cp.ratios)))
        loss_ratios = numpy.zeros(1, numpy.dtype(lst))
        for cp in self.curve_params:
            loss_ratios['user_provided'] = cp.user_provided
            loss_ratios[cp.loss_type] = tuple(cp.ratios)
        return loss_ratios
184497
def plot_assets_curve(self, length=14, height=12):
        
        plt.style.use('ggplot')
        plt.figure(figsize=(length, height))
        plt.subplot(211)
        plt.title('BASIC INFO', fontsize=12)
        plt.axis([0, length, 0, 0.6])
        plt.axis('off')
        i = 0
        for item in ['account_cookie', 'portfolio_cookie', 'user_cookie']:
            plt.text(
                i,
                0.5,
                '{} : {}'.format(item,
                                 self.message[item]),
                fontsize=10,
                rotation=0,
                wrap=True
            )
            i += (length / 2.8)
        i = 0
        for item in ['benchmark_code', 'time_gap', 'max_dropback']:
            plt.text(
                i,
                0.4,
                '{} : {}'.format(item,
                                 self.message[item]),
                fontsize=10,
                ha='left',
                rotation=0,
                wrap=True
            )
            i += (length / 2.8)
        i = 0
        for item in ['annualize_return', 'bm_annualizereturn', 'profit']:
            plt.text(
                i,
                0.3,
                '{} : {} %'.format(item,
                                   self.message.get(item,
                                                    0) * 100),
                fontsize=10,
                ha='left',
                rotation=0,
                wrap=True
            )
            i += length / 2.8
        i = 0
        for item in ['init_cash', 'last_assets', 'volatility']:
            plt.text(
                i,
                0.2,
                '{} : {} '.format(item,
                                  self.message[item]),
                fontsize=10,
                ha='left',
                rotation=0,
                wrap=True
            )
            i += length / 2.8
        i = 0
        for item in ['alpha', 'beta', 'sharpe']:
            plt.text(
                i,
                0.1,
                '{} : {}'.format(item,
                                 self.message[item]),
                ha='left',
                fontsize=10,
                rotation=0,
                wrap=True
            )
            i += length / 2.8
        plt.subplot(212)
        self.assets.plot()
        self.benchmark_assets.xs(self.benchmark_code, level=1).plot()

        asset_p = mpatches.Patch(
            color='red',
            label='{}'.format(self.account.account_cookie)
        )
        asset_b = mpatches.Patch(
            label='benchmark {}'.format(self.benchmark_code)
        )
        plt.legend(handles=[asset_p, asset_b], loc=0)
        plt.title('ASSET AND BENCKMARK')

        return plt
253899
def plot_iv_curve(a, hold_v, i, *plt_args, **plt_kwargs):    
    """"""
    grid = plt_kwargs.pop('grid',True)
    same_fig = plt_kwargs.pop('same_fig',False) 
    if not len(plt_args):
        plt_args = ('ko-',)
    if 'label' not in plt_kwargs: 
        plt_kwargs['label'] = 'Current'
    if not same_fig:
        make_iv_curve_fig(a, grid=grid)
    if type(i) is dict:
        i = [i[v] for v in hold_v]
    plt.plot([v*1e3 for v in hold_v], [ii*1e12 for ii in i], *plt_args, **plt_kwargs)
    plt.legend(loc=2)
1304283
def plot_lr(self, show_text=True, show_moms=True):
        """"""
        phase_limits = [0]
        for nb_batch, phase in zip(self.nb_batches, self.phases):
            phase_limits.append(phase_limits[-1] + nb_batch * phase.epochs)
        if not in_ipynb():
            plt.switch_backend('agg')
        np_plts = 2 if show_moms else 1
        fig, axs = plt.subplots(1,np_plts,figsize=(6*np_plts,4))
        if not show_moms: axs = [axs]
        for i in range(np_plts): axs[i].set_xlabel('iterations')
        axs[0].set_ylabel('learning rate')
        axs[0].plot(self.iterations,self.lrs)
        if show_moms:
            axs[1].set_ylabel('momentum')
            axs[1].plot(self.iterations,self.momentums)
        if show_text:   
            for i, phase in enumerate(self.phases):
                text = phase.opt_fn.__name__
                if phase.wds is not None: text+='\nwds='+str(phase.wds)
                if phase.beta is not None: text+='\nbeta='+str(phase.beta)
                for k in range(np_plts):
                    if i < len(self.phases)-1:
                        draw_line(axs[k], phase_limits[i+1])
                    draw_text(axs[k], (phase_limits[i]+phase_limits[i+1])/2, text) 
        if not in_ipynb():
            plt.savefig(os.path.join(self.save_path, 'lr_plot.png'))
982667
def view_portfolio_loss(token, dstore):
    
    data = portfolio_loss(dstore)  # shape (R, L)
    loss_types = list(dstore['oqparam'].loss_dt().names)
    header = ['portfolio_loss'] + loss_types
    mean = ['mean'] + [row.mean() for row in data.T]
    stddev = ['stddev'] + [row.std(ddof=1) for row in data.T]
    return rst_table([mean, stddev], header)
726196
def eval_rs(gains, losses):
        
        # Number of days that the data was collected through
        count = len(gains) + len(losses)

        avg_gains = stats.avg(gains, count=count) if gains else 1
        avg_losses = stats.avg(losses,count=count) if losses else 1
        if avg_losses == 0:
            return avg_gains
        else:
            return avg_gains / avg_losses
1361448
def plot_lr(self):
        ''''''
        if not in_ipynb():
            plt.switch_backend('agg')
        if self.record_mom:
            fig, axs = plt.subplots(1,2,figsize=(12,4))
            for i in range(0,2): axs[i].set_xlabel('iterations')
            axs[0].set_ylabel('learning rate')
            axs[1].set_ylabel('momentum')
            axs[0].plot(self.iterations,self.lrs)
            axs[1].plot(self.iterations,self.momentums)   
        else:
            plt.xlabel("iterations")
            plt.ylabel("learning rate")
            plt.plot(self.iterations, self.lrs)
        if not in_ipynb():
            plt.savefig(os.path.join(self.save_path, 'lr_plot.png'))
37917
def log_loss(preds, labels):
    """"""
    log_likelihood = np.sum(labels * np.log(preds)) / len(preds)
    return -log_likelihood
664765
def build_loss_tables(dstore):
    
    oq = dstore['oqparam']
    L = len(oq.loss_dt().names)
    R = dstore['csm_info'].get_num_rlzs()
    serials = dstore['ruptures']['serial']
    idx_by_ser = dict(zip(serials, range(len(serials))))
    tbl = numpy.zeros((len(serials), L), F32)
    lbr = numpy.zeros((R, L), F32)  # losses by rlz
    for rec in dstore['losses_by_event'].value:  # call .value for speed
        idx = idx_by_ser[rec['eid'] // TWO32]
        tbl[idx] += rec['loss']
        lbr[rec['rlzi']] += rec['loss']
    return tbl, lbr
35798
def plot_comp(df_var, fig=None, ax=None, **kwargs):
    
    if fig is None and ax is None:
        fig, ax = plt.subplots()
    elif fig is None:
        fig = ax.get_figure()
    elif ax is None:
        ax = fig.gca()
    # plt.clf()
    # plt.cla()
    # ax = sns.regplot(
    #     x='Obs', y='Sim',
    #     data=df_var,
    #     fit_reg=True)

    # add regression expression
    df_var_fit = df_var.dropna(how='any')
    # regr = linear_model.LinearRegression()
    # val_x = df_var_fit['Obs'].values.reshape(-1, 1)
    # val_y = df_var_fit['Sim'].values.reshape(-1, 1)
    # regr.fit(val_x, val_y)
    val_x = df_var_fit['Obs']
    val_y = df_var_fit['Sim']
    slope, intercept, r_value, p_value, std_err = stats.linregress(
        val_x, val_y)
    mae = (val_y - val_x).abs().mean()

    sns.regplot(
        x='Obs', y='Sim',
        data=df_var,
        ax=ax,
        fit_reg=True,
        line_kws={
            'label': "y={0:.2f}x{1}{2:.2f}".format(slope, '+' if intercept>0 else '', intercept) +
            '\n' + '$R^2$={0:.4f}'.format(r_value) +
            '\n' + 'MAE={0:.2f}'.format(mae) +
            '\n' + 'n={}'.format(df_var.shape[0])
        },
        **kwargs
    )
    # ax.plot(val_x, y_pred, color='red', linewidth=2,
    #         label='r2= ' + str("%.3f" % r2) + '\n' +
    #         'y=' + str("%.3f" % a[0][0]) + 'x+' + str("%.2f" % b[0]))

    # ax.legend(fontsize=15)
    ax.legend()
    # ax.set_title(var + '_' + title)

    # set equal plotting range
    x0, x1 = ax.get_xlim()
    y0, y1 = ax.get_ylim()
    lim_low, lim_high = np.min([x0, y0]), np.max([x1, y1])
    ax.set_xlim(lim_low, lim_high)
    ax.set_ylim(lim_low, lim_high)

    # set 1:1 aspect ratio
    ax.set_aspect('equal')

    # add 1:1 line
    ax.plot([lim_low, lim_high], [lim_low, lim_high],
            color='red', linewidth=1, zorder=0)

    # fig = ax.figure

    return fig, ax
250924
def build_loss_maps(self, losses, clp, stats=()):
        
        array = numpy.zeros((len(clp), len(losses)), F32)
        for r, ls in enumerate(losses):
            if len(ls) < 2:
                continue
            for c, poe in enumerate(clp):
                array[c, r] = conditional_loss_ratio(ls, self.poes, poe)
        return self.pair(array, stats)
1317016
def make_plot(self):
        
        # sets colormap for ratio comparison plot
        cmap = getattr(cm, self.colormap)

        # set values of ratio comparison contour
        normval = 2.0
        num_contours = 40  # must be even
        levels = np.linspace(-normval, normval, num_contours)
        norm = colors.Normalize(-normval, normval)

        # find Loss/Gain contour and Ratio contour
        self.set_comparison()
        diff_out, loss_gain_contour = self.find_difference_contour()

        cmap.set_bad(color='white', alpha=0.001)
        # plot ratio contours

        sc = self.axis.contourf(self.xvals[0], self.yvals[0], diff_out,
                                levels=levels, norm=norm,
                                extend='both', cmap=cmap)

        self.colorbar.setup_colorbars(sc)

        # toggle line contours of orders of magnitude of ratio comparisons
        if self.order_contour_lines:
                self.axis.contour(self.xvals[0], self.yvals[0], diff_out, np.array(
                    [-2.0, -1.0, 1.0, 2.0]), colors='black', linewidths=1.0)

        # plot loss gain contour
        if self.loss_gain_status is True:
            # if there is no loss/gain contours, this will produce an error,
            # so we catch the exception.
            try:
                # make hatching
                cs = self.axis.contourf(self.xvals[0], self.yvals[0],
                                        loss_gain_contour, levels=[-2, -0.5, 0.5, 2], colors='none',
                                        hatches=['x', None, '+'])
                # make loss/gain contour outline
                self.axis.contour(self.xvals[0], self.yvals[0],
                                  loss_gain_contour, 3, colors='black', linewidths=2)

            except ValueError:
                pass

        if self.add_legend:
            loss_patch = Patch(fill=None, label='Loss', hatch='x', linestyle='--', linewidth=2)
            gain_patch = Patch(fill=None, label='Gain', hatch='+', linestyle='-', linewidth=2)
            legend = self.axis.legend(handles=[loss_patch, gain_patch], **self.legend_kwargs)

        return
265490
def make_figure(losses_by_rlzi, loss_types, nbins):
    
    # NB: matplotlib is imported inside since it is a costly import
    import matplotlib.pyplot as plt
    R = len(losses_by_rlzi)
    L = len(loss_types)
    fig = plt.figure()
    for rlz in losses_by_rlzi:
        rlzi = int(rlz[4:])  # strip rlz-
        losses = losses_by_rlzi[rlz]['loss'].reshape(-1, L)
        print('%s, num_events=%d' % (rlz, len(losses)))
        for lti, lt in enumerate(loss_types):
            ls = losses[:, lti]
            if numpy.isnan(ls).all():
                continue
            ax = fig.add_subplot(R, L, rlzi * L + lti + 1)
            ax.set_xlabel('%s, loss_type=%s' % (rlz, lt))
            ax.hist(ls, nbins, rwidth=.9)
            ax.set_title('loss=%.5e$\pm$%.5e' % (ls.mean(), ls.std(ddof=1)))
    return plt