Esempio n. 1
0
def scale_spectra(spec1, spec2, wlmin=None, wlmax=None, scale_factor=False):
    '''
    Scale spec1 to spec2 by integrating from wlmin to wlmax and using
    the area as a scaling factor
    Inputs:
        spec1, spec2: two spectrum1d objects with attributes wave and flux
        wlmin: shortest wavelength to be used in scaling
        wlmax: longest wavelength to be used in scaling
    Outputs:
        spec1: spec1d object with scaled flux
        scale_factor (optional): if scale_factor = True, then the multiplicative factor is returned
    '''
    if wlmin is None:
        wlmin = max(spec1.wave.min(), spec2.wave.min())
    if wlmax is None:
        wlmax = min(spec1.wave.max(), spec2.wave.max())
    windx_1 = (spec1.wave >= wlmin) & (spec1.wave <= wlmax)
    windx_2 = (spec2.wave >= wlmin) & (spec2.wave <= wlmax)
    area1 = integrate.trapezoid(spec1.flux[windx_1], x=spec1.wave[windx_1])
    area2 = integrate.trapezoid(spec2.flux[windx_2], x=spec2.wave[windx_2])
    spec1 = spectrum1d(spec1.wave, spec1.flux / area1 * area2)
    if scale_factor is True:
        return spec1, area2 / area1
    else:
        return spec1
Esempio n. 2
0
def calc_tw(tw_in, beta_phiw, beta_psi_charge, charge_pair, rho, f1, f2, z,
            n_component, n_point, z_index):

    tw = np.zeros((n_point, n_component))
    integral_z_infty = np.zeros((n_point, n_component))
    integral_0_z = np.zeros((n_point, n_component))

    TWO_PI = 2.0 * np.pi
    hw = calc_hw(tw_in, n_component, beta_phiw)

    for i in range(n_component):
        for k in range(n_point):
            integral_0_z[k, i] = trapezoid(y=hw[:k, i], x=z[:k])
            integral_z_infty[k, i] = trapezoid(y=z[k:] * hw[k:, i], x=z[k:])

    for i in range(n_component):
        for k in range(n_point):
            z_minus_t = np.flip(z_index[:k])
            t_minus_z = z_index[k:] - k
            for j in range(i, n_component):
                l = calc_l_index(i, j)
                tw[k, i] = beta_psi_charge[i]
                tw[k, i] += TWO_PI * rho[j] * (
                    z[k] * f1[k, l] - f2[k, l] + charge_pair[l] *
                    (integral_z_infty[k, j] + z[k] * integral_0_z[k, j]) +
                    trapezoid(y=hw[:k, j] * f1[z_minus_t, l]) +
                    trapezoid(y=hw[k:, j] * f1[t_minus_z, l]))
    return tw
Esempio n. 3
0
    def test_trapezoid(self):
        y = np.arange(17)
        assert_equal(trapezoid(y), 128)
        assert_equal(trapezoid(y, dx=0.5), 64)
        assert_equal(trapezoid(y, x=np.linspace(0, 4, 17)), 32)

        y = np.arange(4)
        x = 2**y
        assert_equal(trapezoid(y, x=x, dx=0.1), 13.5)
Esempio n. 4
0
    def evaluate(self, heatmap, img):  # noqa
        """# TODO to add docs

        Args:
            heatmap (Tensor): heatmap with shape (H, W) or (3, H, W).
            img (Tensor): image with shape (3, H, W).

        Returns:
            dict[str, Union[Tensor, np.array, float]]: a dictionary containing following fields
                - del_scores: ndarray,
                - ins_scores:
                - del_img:
                - ins_img:
                - ins_auc:
                - del_auc:
        """
        # compress heatmap to 2D if needed
        if heatmap.ndim == 3:
            heatmap = heatmap.mean(0)
            heatmap = heatmap.mean(0)

        # sort pixel in attribution
        num_pixels = torch.numel(heatmap)
        _, indices = torch.topk(heatmap.flatten(), num_pixels)
        indices = np.unravel_index(indices.cpu().numpy(), heatmap.size())

        # apply deletion game
        deletion_perturber = PixelPerturber(img, torch.zeros_like(img))
        deletion_scores = self._procedure_perturb(deletion_perturber,
                                                  num_pixels, indices)

        # apply insertion game
        blurred_img = self.gaussian_blurr(img)
        insertion_perturber = PixelPerturber(blurred_img, img)
        insertion_scores = self._procedure_perturb(insertion_perturber,
                                                   num_pixels, indices)

        # calculate AUC
        insertion_auc = trapezoid(insertion_scores,
                                  dx=1. / len(insertion_scores))
        deletion_auc = trapezoid(deletion_scores, dx=1. / len(deletion_scores))

        # deletion_img and insertion_img are final results, they are only used for debug purpose
        # TODO check if it is necessary to convert the Tensors to np.ndarray
        return {
            "del_scores": deletion_scores,
            "ins_scores": insertion_scores,
            "del_img": deletion_perturber.get_current(),
            "ins_img": insertion_perturber.get_current(),
            "ins_auc": insertion_auc,
            "del_auc": deletion_auc
        }
Esempio n. 5
0
    def price(self, strike, spot, texp, cp=1):
        '''
        Your MC routine goes here
        Generate paths for vol only. Then compute integrated variance and normal price.
        You may fix the random number seed
        '''
        np.random.seed(12345)
        option_num = strike.shape[0]
        self.option_prices = np.zeros(option_num)
        dt = 0.01
        time_num = round(texp / dt)
        path_num = 10000
        stockpath = np.zeros((time_num + 1, path_num))
        stockpath[0, :] = spot
        sigmapath = np.zeros((time_num + 1, path_num))
        sigmapath[0, :] = self.sigma

        for i in range(time_num):
            t_row = i + 1
            z = np.random.randn(path_num)
            sigmapath[t_row, :] = sigmapath[i, :] * np.exp(
                self.vov * np.sqrt(dt) * z - 0.5 * self.vov**2 * dt)
        self.sigmapath = sigmapath
        self.I_T = spint.trapezoid(sigmapath**2, dx=dt, axis=0)
        self.stock_prices = spot + self.rho / self.vov * (sigmapath[-1, :] -
                                                          self.sigma)
        self.sigma_n = np.sqrt((1 - self.rho**2) * self.I_T / texp)
        self.option_prices = np.mean(self.normal_formula(
            strike, self.stock_prices, texp, self.sigma_n),
                                     axis=1)
        return self.option_prices
Esempio n. 6
0
 def get_auprc(self, scores):
     res = prc_curve(scores, self.anomaly.y[self.data.I[0]])
     _res = pd.DataFrame(
         res,
         columns=('ppv', 'tpr'),
     ).groupby('tpr').max().reset_index()[['ppv', 'tpr']].values
     return trapezoid(*_res.T)
Esempio n. 7
0
def integral_z_infty_dr_r2_c_short(c_short, n_pair, n_point, z):
    integrand = np.zeros(n_point)
    f2 = np.zeros((n_point, n_pair))
    for ij in range(n_pair):
        integrand[:] = z * z * c_short[:, ij]
        for k, _ in enumerate(z):
            f2[k:, ij] = trapezoid(y=integrand[k:], x=z[k:])
    return f2
Esempio n. 8
0
    def __compute_energy(self):
        energy = []
        for state in [0, 1]:  #0 is discharge, 1 is charge
            current = self.filter_row("ox/red", state)["(Q-Qo)/mA.h"]
            voltage = self.filter_row("ox/red", state)["Ewe/V"]
            energy.append(integrate.trapezoid(voltage, x=current))

        self.energy_charge = energy[1]
        self.energy_discharge = -energy[0]
Esempio n. 9
0
def sirens_dist(label):
    # redshift limits
    z_min = 0
    z_max = 10

    # Pop III
    if label == "Pop III":
        min = 0
        max = 8.0

        z = [0, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10]
        n = [0, 2, 7, 8, 5.25, 3.25, 1.5, 0.5, 0.25, 0, 0, 0]

        N = trapezoid(n, z)
        f = interp1d(z, n)

    # Delay
    elif label == "Delay":
        min = 0
        max = 6.0

        z = [0, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10]
        n = [0, 1, 4.1, 6, 5.2, 4.8, 2.7, 1.8, 0.7, 0.4, 0.1, 0]

        N = trapezoid(n, z)
        f = interp1d(z, n)

    # No Delay
    elif label == "No Delay":
        min = 0
        max = 10.3

        z = [0, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10]
        n = [0, 3.7, 10.3, 9.4, 7.7, 5, 2.9, 1.2, 0.5, 0.2, 0, 0]

        N = trapezoid(n, z)
        f = interp1d(z, n)

    # no such label exists
    else:
        raise Exception("Label not available, available labels are: Pop III, Delay, No Delay")

    return (f, z_min, z_max, min, max, N)
Esempio n. 10
0
def frac_area_latency(inst, mode='abs', frac=None, tmin=None, tmax=None):

    # Get Data vector and sample indicies for tmin and tmax
    ch_names = inst.info['ch_names']
    data = np.squeeze(inst.data) * 1e6
    times = inst.times
    speriod = 1 / inst.info['sfreq']
    time_win, smin, smax = _get_time_win(times,
                                         tmin=tmin,
                                         tmax=tmax,
                                         return_sample=True)

    # Process data based on mode
    if mode == 'pos':
        data[data < 0] = 0
    elif mode == 'neg':
        data[data > 0] = 0
    data = np.abs(data)  # Always rectify

    # Compute area between tmin and tmax (in time_win)
    area = trapezoid(data[:, time_win], dx=speriod, axis=1)
    if frac is None or frac == 1.0:
        return ch_names, area

    # Compute cumulative area by finding nearest 'cumulative' area
    frac_area = area * frac
    running_area = np.ones_like(data) * 10
    for i, sx in enumerate(np.arange(smin + 1, smax + 1)):
        a = trapezoid(data[:, smin:sx], dx=speriod, axis=1)
        running_area[:, smin + i] = a
    search_samples = np.arange(smin, smax + 1)
    frac_lat_samples = _find_nearest(running_area[:, time_win],
                                     frac_area[:, None],
                                     axis=1,
                                     return_index=True)
    frac_true_samples = search_samples[frac_lat_samples]
    frac_lat_times = times[frac_true_samples]

    # Return computed values
    return ch_names, area, frac_lat_times
Esempio n. 11
0
def trapeziod_integral_df(waveforms, dx=1):
    '''
    waveforms: the waveforms collection DataFrame
    dx: the interval between the y-values
    '''

    Integrals = []

    for i in range(waveforms.shape[1]):
        event = waveforms[waveforms.columns[i]]
        integral = trapezoid(y=event, dx=dx)
        Integrals.append(integral)

    return (pd.DataFrame(Integrals, index=waveforms.columns))
Esempio n. 12
0
def plot_roc_precrec(df):
    """ Plot ROC and PrecRec, also calc and return AUC
        Pass perf df from calc.calc_binary_performance_measures
    """

    roc_auc = integrate.trapezoid(y=df['tpr'], x=df['fpr'])
    prec_rec_auc = integrate.trapezoid(y=df['precision'], x=df['recall'])

    f, axs = plt.subplots(1, 2, figsize=(11.5, 6), sharex=True, sharey=True)
    _ = f.suptitle('ROC and Precision Recall Curves', y=1.0)

    _ = axs[0].plot(df['fpr'],
                    df['tpr'],
                    lw=2,
                    marker='d',
                    alpha=0.8,
                    label=f"ROC (auc={roc_auc:.2f})")
    _ = axs[0].plot((0, 1), (0, 1), '--', c='#cccccc', label='line of equiv')
    _ = axs[0].legend(loc='upper left')
    _ = axs[0].set(title='ROC curve', xlabel='FPR', ylabel='TPR')

    _ = axs[1].plot(df['recall'],
                    df['precision'],
                    lw=2,
                    marker='o',
                    alpha=0.8,
                    color='C3',
                    label=f"PrecRec (auc={prec_rec_auc:.2f})")
    _ = axs[1].legend(loc='upper right')
    _ = axs[1].set(title='Precision Recall curve',
                   xlabel='Recall',
                   ylabel='Precision')

    f.tight_layout()

    return roc_auc, prec_rec_auc
Esempio n. 13
0
def plot_coverage(df, title_add=''):
    """ Convenience plot coverage from mt.calc_ppc_coverage """

    txt_kws = dict(color='#333333',
                   xycoords='data',
                   xytext=(2, -4),
                   textcoords='offset points',
                   fontsize=11,
                   backgroundcolor='w')

    g = sns.lmplot(x='cr',
                   y='coverage',
                   col='method',
                   hue='method',
                   data=df,
                   fit_reg=False,
                   height=5,
                   scatter_kws={'s': 70})

    for i, method in enumerate(df['method'].unique()):
        idx = df['method'] == method
        y = df.loc[idx, 'coverage'].values
        x = df.loc[idx, 'cr'].values
        ae = np.abs(y - x)
        auc = integrate.trapezoid(ae, x)

        g.axes[0][i].plot((0, 1), (0, 1), ls='--', color='#aaaaaa', zorder=-1)
        g.axes[0][i].fill_between(x,
                                  y,
                                  x,
                                  color='#bbbbbb',
                                  alpha=0.8,
                                  zorder=-1)
        g.axes[0][i].annotate(f'AUC={auc:.3f}', xy=(0, 1), **txt_kws)

    if title_add != '':
        title_add = f': {title_add}'
    g.fig.suptitle((f'PPC Coverage vs CR{title_add}'), y=1.05)

    return None
Esempio n. 14
0
def integrate_variable_data(data):
    """
    """
    # TODO: Only integrate certain sets
    # TODO: actually test this function...
    sets = data["sets"]
    indices = data["indices"]
    variables = data["variables"]

    if sets is None:
        return {name: 0.0 for name in variables}

    else:
        indices = list(indices)
        variables = dict(variables)
        for name, values in variables.items():
            array = np.array(values)
            for i in range(len(indices)):
                axis = len(indices) - i - 1
                x = indices[axis]
                array = integrate.trapezoid(array, x=x, axis=axis)
            variables[name] = array.tolist()
        return variables
Esempio n. 15
0
def get_area(scan, area_method, lorentz):
    if area_method not in AREA_METHODS:
        raise ValueError(f"Unknown area method: {area_method}.")

    if area_method == "fit_area":
        area_v = 0
        area_s = 0
        for name, param in scan["fit"].params.items():
            if "amplitude" in name:
                if param.stderr is None:
                    area_v = np.nan
                    area_s = np.nan
                else:
                    area_v += param.value
                    area_s += param.stderr

    else:  # area_method == "int_area"
        y_val = scan["counts"]
        x_val = scan[scan["scan_motor"]]
        y_bkg = scan["fit"].eval_components(x=x_val)["f0_"]
        area_v = simpson(y_val, x=x_val) - trapezoid(y_bkg, x=x_val)
        area_s = np.sqrt(area_v)

    if lorentz:
        # lorentz correction to area
        if scan["zebra_mode"] == "bi":
            twotheta = np.deg2rad(scan["twotheta"])
            corr_factor = np.sin(twotheta)
        else:  # zebra_mode == "nb":
            gamma = np.deg2rad(scan["gamma"])
            nu = np.deg2rad(scan["nu"])
            corr_factor = np.sin(gamma) * np.cos(nu)

        area_v = np.abs(area_v * corr_factor)
        area_s = np.abs(area_s * corr_factor)

    scan["area"] = (area_v, area_s)
Esempio n. 16
0
 def test_trapz(self):
     # Basic coverage test for the alias
     y = np.arange(4)
     x = 2**y
     assert_equal(trapezoid(y, x=x, dx=0.5, axis=0),
                  trapz(y, x=x, dx=0.5, axis=0))
    def evaluate(self,
                 heatmap,
                 img,
                 target,
                 do_insertion=True,
                 do_deletion=True,
                 cutoff=0.5):  # noqa
        """# TODO to add docs

        Args:
            heatmap (Tensor): heatmap with shape (H, W) or (3, H, W).
            img (Tensor): image with shape (3, H, W).
            target (int): class index of the image.

        Returns:
            dict[str, Union[Tensor, np.array, float]]: a dictionary containing following fields
                - del_scores: ndarray,
                - ins_scores:
                - del_img:
                - ins_img:
                - ins_auc:
                - del_auc:
        """
        # compress heatmap to 2D if needed
        if heatmap.ndim == 3:
            heatmap = heatmap.mean(0)
            heatmap = heatmap.mean(0)

        # sort pixel in attribution
        num_pixels = torch.numel(heatmap)
        _, indices = torch.topk(heatmap.flatten(), num_pixels)
        indices = np.unravel_index(indices.cpu().numpy(), heatmap.size())

        blurred_img = self.gaussian_blurr(img)
        # apply deletion game
        deletion_scores = None
        deletion_auc = None
        num_pixels = int(cutoff * num_pixels)
        if do_deletion:
            deletion_perturber = PixelPerturber(img, blurred_img)
            deletion_scores = self._procedure_perturb(deletion_perturber,
                                                      num_pixels, indices,
                                                      target)

            # calculate AUC
            deletion_auc = trapezoid(deletion_scores,
                                     dx=1. / len(deletion_scores))

        # apply insertion game
        insertion_scores = None
        insertion_auc = None
        if do_insertion:
            insertion_perturber = PixelPerturber(blurred_img, img)
            insertion_scores = self._procedure_perturb(insertion_perturber,
                                                       num_pixels, indices,
                                                       target)

            # calculate AUC
            insertion_auc = trapezoid(insertion_scores,
                                      dx=1. / len(insertion_scores))

        # TODO check if it is necessary to convert the Tensors to np.ndarray
        return {
            "del_scores": deletion_scores,
            "ins_scores": insertion_scores,
            "ins_auc": insertion_auc,
            "del_auc": deletion_auc
        }
Esempio n. 18
0
    def evaluate(self, heatmap: torch.Tensor, image: torch.Tensor) -> dict:
        self.model.eval()

        # compress heatmap to 2D if needed
        if heatmap.ndim == 3:
            heatmap = heatmap.mean(0)

        # get 2d tile attribution
        perturber = GridPerturber(image, torch.zeros_like(image),
                                  self.tile_size)
        grid_heatmap = torch.zeros(perturber.get_grid_shape())
        for r in range(grid_heatmap.shape[0]):
            for c in range(grid_heatmap.shape[1]):
                grid_heatmap[r][c] = heatmap[perturber.view.tile_slice(
                    r, c)].sum()

        # sort tile in attribution
        num_pixels = torch.numel(grid_heatmap)
        _, indices = torch.topk(grid_heatmap.flatten(), num_pixels)
        indices = np.unravel_index(indices.cpu().numpy(), grid_heatmap.size())
        _, reverse_indices = torch.topk(grid_heatmap.flatten(),
                                        num_pixels,
                                        largest=False)
        reverse_indices = np.unravel_index(reverse_indices.cpu().numpy(),
                                           grid_heatmap.size())

        # TODO to make it compatible with multi-label classification setting
        # TODO to make baseline_score and morf_scores local variables rather than object attributes
        # get baseline score
        self.baseline_score = torch.nn.functional.softmax(
            self.model(
                image.unsqueeze(0).to(next(
                    self.model.parameters()).device)))[:, self.target]
        self.baseline_score = self.baseline_score.detach().cpu().numpy()

        # apply deletion game using MoRF
        print("MoRF deletion")
        self.morf_scores = self._procedure_perturb(perturber, num_pixels,
                                                   indices,
                                                   self.img_history_morf)
        MoRF_img = perturber.get_current()

        # apply deletion game using LeRF
        perturber = GridPerturber(image, torch.zeros_like(image),
                                  self.tile_size)
        print("LeRF deletion")
        self.lerf_scores = self._procedure_perturb(perturber, num_pixels,
                                                   reverse_indices,
                                                   self.img_history_lerf)
        LeRF_img = perturber.get_current()

        #remove bias
        self.lerf_scores = self.lerf_scores - self.baseline_score
        self.morf_scores = self.morf_scores - self.baseline_score

        # calculate AUC
        lerf_auc = trapezoid(self.lerf_scores,
                             dx=1. / float(len(self.lerf_scores)))
        morf_auc = trapezoid(self.morf_scores,
                             dx=1. / float(len(self.morf_scores)))

        # deletion_img and insertion_img are final results, they are only used for debug purpose
        return {
            "MoRF_scores": self.morf_scores,
            "LeRF_scores": self.lerf_scores,
            "MoRF_img": MoRF_img,
            "LeRF_img": LeRF_img,
            "LeRF_auc": lerf_auc,
            "MoRF_auc": morf_auc,
            "MoRF_img_history": self.img_history_morf,
            "LeRF_img_history": self.img_history_lerf
        }
Esempio n. 19
0
    if galaxy in ["ngc7793", "ngc1566"]:
        ax_peak.scatter([peak_r], len(cat), c=bpl.color_cycle[3])
        ax_peak.add_text(
            x=peak_r + 0.1,
            y=len(cat),
            text=galaxy.upper(),
            fontsize=12,
            ha="left",
            va="center",
        )
    else:
        ax_peak.scatter([peak_r], len(cat), c=bpl.color_cycle[0])

    # KL is done elementwise, then we integrate
    kl_values = special.kl_div(stacked_pdf, cat_pdf)
    kl_value = integrate.trapezoid(kl_values, radii_plot)

    ax.set_xscale("log")
    ax.set_limits(0.1, 25, 0, 1.0)
    ax.set_xticks([0.1, 1, 10])
    ax.set_xticklabels(["0.1", "1", "10"])
    ax.add_labels("$R_{eff}$ [pc]", "Normalized KDE Density")
    ax.easy_add_text(
        f"{galaxy.upper()}\n"
        f"N={len(cat)}\n"
        f"P={pvalue:.3g}\n"
        f"KL={kl_value:.3f}\n"
        "$R_{peak} = $" + f"{peak_r:.2f}pc",
        "upper left",
    )
# last axis isn't needed, we only have 31 galaxies
Esempio n. 20
0
 def auprc(self):
     prc = (
         np.hstack((1, self.precision[::-1], 0)),
         np.hstack((0, self.recall[::-1], 1)),
     )
     return trapezoid(*prc)
Esempio n. 21
0
 def auroc(self):
     roc = (
         np.hstack((0, self.tpr[::-1], 1)),  # true  positive rate (y)
         np.hstack((0, self.fpr[::-1], 1)),  # false positive rate (x)
     )
     return trapezoid(*roc)
Esempio n. 22
0
 def get_auprc(self):
     scores = self.get_scores()
     res = np.array(list(map(prc_curve, scores.T, repeat(self.y))))
     auc = np.array(list(map(lambda x: trapezoid(*x.T), res)))
     return auc
Esempio n. 23
0
def plot_binary_performance(df, n=1):
    """ Plot ROC, PrecRec, F-score, Accuracy
        Pass perf df from calc.calc_binary_performance_measures
        Return summary stats
    """
    roc_auc = integrate.trapezoid(y=df['tpr'], x=df['fpr'])
    prec_rec_auc = integrate.trapezoid(y=df['precision'], x=df['recall'])

    f, axs = plt.subplots(1, 4, figsize=(18, 5), sharex=False, sharey=False)
    _ = f.suptitle(
        ('Evaluations of Binary Classifier made by sweeping across ' +
         f'PPC quantiles\n(requires large n, here n={n})'),
        y=1.0)

    minpos = np.argmin(np.sqrt(df['fpr']**2 + (1 - df['tpr'])**2))
    _ = axs[0].plot(df['fpr'],
                    df['tpr'],
                    lw=2,
                    marker='d',
                    alpha=0.8,
                    label=f"ROC (auc={roc_auc:.2f})")
    _ = axs[0].plot((0, 1), (0, 1), '--', c='#cccccc', label='line of equiv')
    _ = axs[0].plot(df.loc[minpos, 'fpr'],
                    df.loc[minpos, 'tpr'],
                    lw=2,
                    marker='D',
                    color='w',
                    markeredgewidth=1,
                    markeredgecolor='b',
                    markersize=9,
                    label=f"Optimum ROC @ {minpos} pct")
    _ = axs[0].legend(loc='lower right')
    _ = axs[0].set(title='ROC curve', xlabel='FPR', ylabel='TPR', ylim=(0, 1))

    _ = axs[1].plot(df['recall'],
                    df['precision'],
                    lw=2,
                    marker='o',
                    alpha=0.8,
                    color='C3',
                    label=f"PrecRec (auc={prec_rec_auc:.2f})")
    _ = axs[1].legend(loc='upper right')
    _ = axs[1].set(title='Precision Recall curve',
                   ylim=(0, 1),
                   xlabel='Recall',
                   ylabel='Precision')

    f1_at = df['f1'].argmax()
    dfm = df.reset_index()[['pct', 'f0.5', 'f1',
                            'f2']].melt(id_vars='pct',
                                        var_name='f-measure',
                                        value_name='f-score')

    _ = sns.lineplot(x='pct',
                     y='f-score',
                     hue='f-measure',
                     data=dfm,
                     palette='Greens',
                     lw=2,
                     ax=axs[2])
    _ = axs[2].plot(f1_at,
                    df.loc[f1_at, 'f1'],
                    lw=2,
                    marker='D',
                    color='w',
                    markeredgewidth=1,
                    markeredgecolor='b',
                    markersize=9,
                    label=f"Optimum F1 @ {f1_at} pct")
    _ = axs[2].legend(loc='upper left')
    _ = axs[2].set(title='F-scores across the PPC pcts' +
                   f'\nBest F1 = {df.loc[f1_at, "f1"]:.3f} @ {f1_at} pct',
                   xlabel='pct',
                   ylabel='F-Score',
                   ylim=(0, 1))

    acc_at = df['accuracy'].argmax()
    _ = sns.lineplot(x='pct',
                     y='accuracy',
                     color='C1',
                     data=df,
                     lw=2,
                     ax=axs[3])
    _ = axs[3].text(
        x=0.04,
        y=0.04,
        s=('Class imbalance:' + f'\n0: {df["accuracy"].values[0]:.1%}' +
           f'\n1: {df["accuracy"].values[-1]:.1%}'),
        transform=axs[3].transAxes,
        ha='left',
        va='bottom',
        backgroundcolor='w',
        fontsize=10)
    _ = axs[3].set(title='Accuracy across the PPC pcts' +
                   f'\nBest = {df.loc[acc_at, "accuracy"]:.1%} @ {acc_at} pct',
                   xlabel='pct',
                   ylabel='Accuracy',
                   ylim=(0, 1))

    f.tight_layout()
    return None
Esempio n. 24
0
def f2(ts):
    return  4 * params.np_coef**2 * params.sigma * integrate.trapezoid([get_k(ti) * \
             (pow(ti, 4) - pow(params.T0, 4)) for ti in ts], np.arange(0, params.l + params.h, params.h))
Esempio n. 25
0
    d_u__d_theta__entire_trajectory = solution_sensitivities_at_t
    # del_J__del_u has the shape (1, n_dim, n_time_points)  - the leading 1
    # (i.e. a proxy axis) is required in order to mimic the batch of row vectors
    del_J__del_u__entire_trajectory = (y_at_t - y_at_t_ref).reshape(
        (1, 2, time_points_inbetween))

    d_j__d_theta__entire_trajectory = np.einsum(
        "EiN,iPN->EPN",
        del_J__del_u__entire_trajectory,
        d_u__d_theta__entire_trajectory,
    )

    # Integrate d_j__d_theta over entire trajectory to obtain d_J__d_theta
    d_J__d_theta__entire_trajectory__forward = integrate.trapezoid(
        d_j__d_theta__entire_trajectory,
        t_discrete,
        axis=-1,
    )

    ########################
    ########################
    ##### Adjoint Sensitivities
    ########################
    ########################

    #######
    # (1.1) Using an additional trajectory that runs backwards alongside with
    # the backwards ODE for loss only at the end
    #######
    time_adjoint__at_end = time.time_ns()
 def integrate(ratios, quantiles):
     return trapezoid(ratios, quantiles)
Esempio n. 27
0
    def loss_function_entire_trajectory(y_at_t, theta):
        difference_at_t = y_at_t - y_at_t_ref
        quadratic_loss_at_t = 0.5 * np.einsum("iN,iN->N", difference_at_t,
                                              difference_at_t)

        return integrate.trapezoid(quadratic_loss_at_t, t_discrete, axis=-1)