Exemple #1
0
def test_Fit_Weibull_2P():
    data = Weibull_Distribution(alpha=50, beta=2).random_samples(20, seed=5)
    fit = Fit_Weibull_2P(failures=data, show_probability_plot=False, print_results=False)
    assert fit.alpha == 47.507980313141516
    assert fit.beta == 2.492960611854891

#need to add more tests.
def test_Fit_Weibull_2P():
    dist = Weibull_Distribution(alpha=50, beta=2)
    rawdata = dist.random_samples(20, seed=5)
    data = make_right_censored_data(data=rawdata, threshold=dist.mean)
    fit = Fit_Weibull_2P(failures=data.failures, right_censored=data.right_censored, show_probability_plot=False, print_results=False)
    assert_allclose(fit.alpha, 45.099010886086354,rtol=rtol,atol=atol)
    assert_allclose(fit.beta, 2.7827531773597975,rtol=rtol,atol=atol)
    assert_allclose(fit.gamma, 0,rtol=rtol,atol=atol)
    assert_allclose(fit.AICc, 115.66971887883678,rtol=rtol,atol=atol)
    assert_allclose(fit.Cov_alpha_beta, 0.9178064889295382,rtol=rtol,atol=atol)
    assert_allclose(fit.loglik, -55.4819182629478,rtol=rtol,atol=atol)
    assert_allclose(fit.initial_guess[1], 2.96571536864614,rtol=rtol,atol=atol)
Exemple #3
0
def test_Fit_Weibull_2P():
    dist = Weibull_Distribution(alpha=50, beta=2)
    rawdata = dist.random_samples(20, seed=5)
    data = make_right_censored_data(data=rawdata, threshold=dist.mean)

    MLE = Fit_Weibull_2P(failures=data.failures,
                         right_censored=data.right_censored,
                         method='MLE',
                         show_probability_plot=False,
                         print_results=False)
    assert_allclose(MLE.alpha, 45.099010886086354, rtol=rtol, atol=atol)
    assert_allclose(MLE.beta, 2.7827531773597984, rtol=rtol, atol=atol)
    assert_allclose(MLE.gamma, 0, rtol=rtol, atol=atol)
    assert_allclose(MLE.AICc, 115.66971887883678, rtol=rtol, atol=atol)
    assert_allclose(MLE.BIC, 116.95530107300358, rtol=rtol, atol=atol)
    assert_allclose(MLE.loglik, -55.4819182629478, rtol=rtol, atol=atol)
    assert_allclose(MLE.AD, 55.60004028891652, rtol=rtol, atol=atol)
    assert_allclose(MLE.Cov_alpha_beta,
                    -0.9178064889295378,
                    rtol=rtol,
                    atol=atol)

    LS = Fit_Weibull_2P(failures=data.failures,
                        right_censored=data.right_censored,
                        method='LS',
                        show_probability_plot=False,
                        print_results=False)
    assert_allclose(LS.alpha, 42.91333312142757, rtol=rtol, atol=atol)
    assert_allclose(LS.beta, 2.9657153686461033, rtol=rtol, atol=atol)
    assert_allclose(LS.gamma, 0, rtol=rtol, atol=atol)
    assert_allclose(LS.AICc, 115.93668384456019, rtol=rtol, atol=atol)
    assert_allclose(LS.BIC, 117.222266038727, rtol=rtol, atol=atol)
    assert_allclose(LS.loglik, -55.61540074580951, rtol=rtol, atol=atol)
    assert_allclose(LS.AD, 55.62807482958476, rtol=rtol, atol=atol)
    assert_allclose(LS.Cov_alpha_beta,
                    -0.1119680481788733,
                    rtol=rtol,
                    atol=atol)
Exemple #4
0
def get_image(repository, must_not_have_labels):
    issues = get_filted_issues(repository, must_not_have_labels)

    months = get_months(issues)
    
    hist = Counter(months)
    axis_y = [hist[month] for month in months]

    fig, ax1 = plt.subplots()

    ax1.set_xlabel('Meses')
    ax1.set_ylabel('Bugs reportados', color='tab:red')
    ax1.plot(months, axis_y, color='tab:red')

    repository_name = repository.url.replace('https://github.com/', '')

    plt.suptitle('Padrão de chegada de issues de\nBug do Repositório %s' % (repository_name))

    wb = Fit_Weibull_2P(
        failures=months,
        show_probability_plot=False,
        print_results=False
    )

    weibull = wb.distribution

    X = generate_X_array(dist=weibull, xvals=None, xmin=None, xmax=None)
    Y = ss.weibull_min.pdf(X, weibull.beta, scale=weibull.alpha, loc=weibull.gamma)

    count = len([i for i in X if i < months[-1]+1])

    X = X[:count]
    Y = Y[:count]

    ax2 = ax1.twinx()
    ax2.set_ylabel('Função de densidade de probabilidade de Weibull', color='tab:blue')
    ax2.plot(X, Y, color='tab:blue')

    fig.tight_layout()

    img_bytes = io.BytesIO()
    plt.savefig(img_bytes, format='png')
    img_bytes.seek(0)

    return img_bytes
 def __BIC_minimizer(common_shape_X): #lgtm [py/similar-function]
     '''
     __BIC_minimizer is used by the minimize function to get the shape which gives the lowest overall BIC
     '''
     BIC_tot = 0
     for stress in unique_stresses_f:
         failure_current_stress_df = f_df[f_df['stress'] == stress]
         FAILURES = failure_current_stress_df['times'].values
         if right_censored is not None:
             if stress in unique_stresses_rc:
                 right_cens_current_stress_df = rc_df[rc_df['stress'] == stress]
                 RIGHT_CENSORED = right_cens_current_stress_df['times'].values
             else:
                 RIGHT_CENSORED = None
         else:
             RIGHT_CENSORED = None
         weibull_fit_common_shape = Fit_Weibull_2P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False, force_beta=common_shape_X)
         BIC_tot += weibull_fit_common_shape.BIC
     return BIC_tot
Exemple #6
0
def weibull_coeff(tseries):
    '''
    This function fits a time series of wind speed data with a
    Weibull distribution and returns shape (k) and scale (c) parameter.
    These are used to calculate the Weibull PDF.

    Inputs
        tseries: array-like. distrubtion of wind speeds.

    Outputs
        k: float. shape parameter, describes the Weibull slope in a probability
        plot.
        c: float. scale parameter, describes height and width of the Weibull
        PDF.
    '''
    data = list(tseries)
    wb = Fit_Weibull_2P(failures=data, show_probability_plot=False,
                        print_results=False)
    k = wb.beta
    c = wb.alpha

    return k, c
def HistogramPLOT_all(data, month, year):
    #Initiate
    Situation = []
    mon = [
        'January', 'Febuary', 'March', 'April', 'May', 'June', 'July',
        'August', 'September', 'October', 'November', 'December'
    ]
    #Get just Full day data
    logicF = (data["isFULL"].apply(lambda x: x) == (1))
    data01 = data[logicF].copy()
    data01.fillna(method='ffill', inplace=True)

    logicY = (data01["DateTime"].apply(lambda x: x.year) == (year))
    data01 = data01[logicY].copy()

    fig = plt.figure(figsize=(24, 18), dpi=80, facecolor='w', edgecolor='r')
    #Plotting 12 graph
    xvals = np.linspace(0, 30, 1000)
    for i in range(month):
        ax = plt.subplot2grid((4, 3), (int(np.floor(i / 3)), int(i % 3)))
        logic = (data01["DateTime"].apply(lambda x: x.month)) == (i + 1)
        ws = data01['WS95'][logic]
        ws = ws + 0.0001
        failures = []
        censored = []
        threshold = 30
        for item in ws:
            if item > threshold:
                censored.append(threshold)
            else:
                failures.append(item)
        xvals = np.linspace(0, 30, 1000)
        print(ws.shape)
        if (np.sum(logic) != 0):
            ax.hist(ws, bins=30, normed=True)
            hist, edge = np.histogram(np.array(ws),
                                      bins=1000,
                                      range=(0, 30),
                                      normed=True)
            wb2 = Fit_Weibull_2P(failures=failures,
                                 show_probability_plot=False,
                                 print_results=False)
            wb3 = Fit_Weibull_3P(failures=failures,
                                 show_probability_plot=False,
                                 print_results=False)
            gm2 = Fit_Gamma_2P(failures=failures,
                               show_probability_plot=False,
                               print_results=False)
            gm3 = Fit_Gamma_3P(failures=failures,
                               show_probability_plot=False,
                               print_results=False)
            ln2 = Fit_Lognormal_2P(failures=failures,
                                   show_probability_plot=False,
                                   print_results=False)
            wbm = Fit_Weibull_Mixture(failures=failures,
                                      right_censored=censored,
                                      show_plot=False,
                                      print_results=False)

            wb2_pdf = Weibull_Distribution(alpha=wb2.alpha, beta=wb2.beta).PDF(
                xvals=xvals, show_plot=True, label='Weibull_2P')
            wb3_pdf = Weibull_Distribution(alpha=wb3.alpha,
                                           beta=wb3.beta,
                                           gamma=wb3.gamma).PDF(
                                               xvals=xvals,
                                               show_plot=True,
                                               label='Weibull_3P')
            gm2_pdf = Gamma_Distribution(alpha=gm2.alpha,
                                         beta=gm2.beta).PDF(xvals=xvals,
                                                            show_plot=True,
                                                            label='Gamma_2P')
            gm3_pdf = Gamma_Distribution(alpha=gm3.alpha,
                                         beta=gm3.beta,
                                         gamma=gm3.gamma).PDF(xvals=xvals,
                                                              show_plot=True,
                                                              label='Gamma_3P')
            ln2_pdf = Lognormal_Distribution(mu=ln2.mu, sigma=ln2.sigma).PDF(
                xvals=xvals, show_plot=True, label='Lognormal_2P')

            part1_pdf = Weibull_Distribution(alpha=wbm.alpha_1,
                                             beta=wbm.beta_1).PDF(
                                                 xvals=xvals, show_plot=False)
            part2_pdf = Weibull_Distribution(alpha=wbm.alpha_2,
                                             beta=wbm.beta_2).PDF(
                                                 xvals=xvals, show_plot=False)
            Mixture_PDF = part1_pdf * wbm.proportion_1 + part2_pdf * wbm.proportion_2
            ax.plot(xvals, Mixture_PDF, label='Weibull_Mixture')
        ax.legend()
        ax.set_ylim(0, 0.16)
        ax.set_xlim(0, 30)
        ax.set_xticks([0, 5, 10, 15, 20, 25, 30])
        ax.tick_params(axis="x", labelsize=20)
        ax.tick_params(axis="y", labelsize=20)
        ax.set_title('{}'.format(mon[i]), fontweight='bold', size=20)
    plt.tight_layout()
    plt.show()
    def __init__(self, failures, failure_stress, right_censored=None, right_censored_stress=None, print_results=True, show_plot=True, common_shape_method='BIC'):

        # input type checking and converting to arrays in preperation for creation of dataframe
        if common_shape_method not in ['BIC', 'weighted_average', 'average']:
            raise ValueError('common_shape_method must be either BIC, weighted_average, or average. Default is BIC.')
        if len(failures) != len(failure_stress):
            raise ValueError('The length of failures does not match the length of failure_stress')
        if type(failures) is list:
            failures = np.array(failures)
        elif type(failures) is np.ndarray:
            pass
        else:
            raise ValueError('failures must be an array or list')
        if type(failure_stress) is list:
            failure_stress = np.array(failure_stress)
        elif type(failure_stress) is np.ndarray:
            pass
        else:
            raise ValueError('failure_stress must be an array or list')
        if right_censored is not None:
            if len(right_censored) != len(right_censored_stress):
                raise ValueError('The length of right_censored does not match the length of right_censored_stress')
            if type(right_censored) is list:
                right_censored = np.array(right_censored)
            elif type(right_censored) is np.ndarray:
                pass
            else:
                raise ValueError('right_censored must be an array or list')
            if type(right_censored_stress) is list:
                right_censored_stress = np.array(right_censored_stress)
            elif type(right_censored_stress) is np.ndarray:
                pass
            else:
                raise ValueError('right_censored_stress must be an array or list')

        xmin = np.floor(np.log10(min(failures))) - 1
        xmax = np.ceil(np.log10(max(failures))) + 1
        xvals = np.logspace(xmin, xmax, 100)

        if right_censored is not None:
            TIMES = np.hstack([failures, right_censored])
            STRESS = np.hstack([failure_stress, right_censored_stress])
            CENS_CODES = np.hstack([np.ones_like(failures), np.zeros_like(right_censored)])
        else:
            TIMES = failures
            STRESS = failure_stress
            CENS_CODES = np.ones_like(failures)

        data = {'times': TIMES, 'stress': STRESS, 'cens_codes': CENS_CODES}
        df = pd.DataFrame(data, columns=['times', 'stress', 'cens_codes'])
        df_sorted = df.sort_values(by=['cens_codes', 'stress', 'times'])
        is_failure = df_sorted['cens_codes'] == 1
        is_right_cens = df_sorted['cens_codes'] == 0
        f_df = df_sorted[is_failure]
        rc_df = df_sorted[is_right_cens]
        unique_stresses_f = f_df.stress.unique()
        if right_censored is not None:
            unique_stresses_rc = rc_df.stress.unique()
            for item in unique_stresses_rc:  # check that there are no unique right_censored stresses that are not also in failure stresses
                if item not in unique_stresses_f:
                    raise ValueError('The right_censored_stress array contains values that are not in the failure_stress array. This is equivalent to trying to fit a distribution to only censored data and cannot be done.')

        weibull_fit_alpha_array = []
        weibull_fit_beta_array = []
        weibull_fit_alpha_array_common_shape = []
        color_list = ['steelblue', 'darkorange', 'red', 'green', 'purple', 'blue', 'grey', 'deeppink', 'cyan', 'chocolate']
        weights_array = []
        # within this loop, each list of failures and right censored values will be unpacked for each unique stress to find the common shape parameter
        for stress in unique_stresses_f:
            failure_current_stress_df = f_df[f_df['stress'] == stress]
            FAILURES = failure_current_stress_df['times'].values
            len_f = len(FAILURES)
            if right_censored is not None:
                if stress in unique_stresses_rc:
                    right_cens_current_stress_df = rc_df[rc_df['stress'] == stress]
                    RIGHT_CENSORED = right_cens_current_stress_df['times'].values
                    len_rc = len(RIGHT_CENSORED)
                else:
                    RIGHT_CENSORED = None
                    len_rc = 0
            else:
                RIGHT_CENSORED = None
                len_rc = 0

            weights_array.append(len_f + len_rc)
            weibull_fit = Fit_Weibull_2P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False)
            weibull_fit_alpha_array.append(weibull_fit.alpha)
            weibull_fit_beta_array.append(weibull_fit.beta)
        common_shape_guess = np.average(weibull_fit_beta_array)

        def __BIC_minimizer(common_shape_X): #lgtm [py/similar-function]
            '''
            __BIC_minimizer is used by the minimize function to get the shape which gives the lowest overall BIC
            '''
            BIC_tot = 0
            for stress in unique_stresses_f:
                failure_current_stress_df = f_df[f_df['stress'] == stress]
                FAILURES = failure_current_stress_df['times'].values
                if right_censored is not None:
                    if stress in unique_stresses_rc:
                        right_cens_current_stress_df = rc_df[rc_df['stress'] == stress]
                        RIGHT_CENSORED = right_cens_current_stress_df['times'].values
                    else:
                        RIGHT_CENSORED = None
                else:
                    RIGHT_CENSORED = None
                weibull_fit_common_shape = Fit_Weibull_2P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False, force_beta=common_shape_X)
                BIC_tot += weibull_fit_common_shape.BIC
            return BIC_tot

        if common_shape_method == 'BIC':
            optimized_beta_results = minimize(__BIC_minimizer, x0=common_shape_guess, method='nelder-mead')
            common_shape = optimized_beta_results.x[0]
        elif common_shape_method == 'weighted_average':
            total_data = sum(weights_array)
            weights = np.array(weights_array) / total_data
            common_shape = sum(weights * np.array(weibull_fit_beta_array))
        elif common_shape_method == 'average':
            common_shape = common_shape_guess  # this was just the numerical average obtained above
        self.common_shape = common_shape

        # within this loop, each list of failures and right censored values will be unpacked for each unique stress and plotted as a probability plot as well as the CDF of the common beta plot
        AICc_total = 0
        BIC_total = 0
        AICc = True
        for i, stress in enumerate(unique_stresses_f):
            failure_current_stress_df = f_df[f_df['stress'] == stress]
            FAILURES = failure_current_stress_df['times'].values
            if right_censored is not None:
                if stress in unique_stresses_rc:
                    right_cens_current_stress_df = rc_df[rc_df['stress'] == stress]
                    RIGHT_CENSORED = right_cens_current_stress_df['times'].values
                else:
                    RIGHT_CENSORED = None
            else:
                RIGHT_CENSORED = None
            weibull_fit_common_shape = Fit_Weibull_2P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False, force_beta=common_shape)
            weibull_fit_alpha_array_common_shape.append(weibull_fit_common_shape.alpha)
            if type(weibull_fit_common_shape.AICc) == str:
                AICc = False
            else:
                AICc_total += weibull_fit_common_shape.AICc
            BIC_total += weibull_fit_common_shape.BIC
            if show_plot is True:
                weibull_fit_common_shape.distribution.CDF(linestyle='--', color=color_list[i], xvals=xvals, plot_CI=False) # plotting of the confidence intervals has been turned off
                Probability_plotting.Weibull_probability_plot(failures=FAILURES, right_censored=RIGHT_CENSORED,plot_CI=False, color=color_list[i], label=str(stress))
                plt.legend(title='Stress')
                plt.xlim(10 ** (xmin + 1), 10 ** (xmax - 1))
                if common_shape_method == 'BIC':
                    plt.title(str('ALT Weibull Probability Plot\nOptimal BIC ' + r'$\beta$ = ' + str(round(common_shape, 4))))
                elif common_shape_method == 'weighted_average':
                    plt.title(str('ALT Weibull Probability Plot\nWeighted average ' + r'$\beta$ = ' + str(round(common_shape, 4))))
                elif common_shape_method == 'average':
                    plt.title(str('ALT Weibull Probability Plot\nAverage ' + r'$\beta$ = ' + str(round(common_shape, 4))))
        self.BIC_sum = np.sum(BIC_total)
        if AICc is True:
            self.AICc_sum = np.sum(AICc_total)
        else:
            self.AICc_sum = 'Insufficient Data'
        beta_difs = (common_shape - np.array(weibull_fit_beta_array)) / np.array(weibull_fit_beta_array)
        beta_differences = []
        for item in beta_difs:
            if item > 0:
                beta_differences.append(str('+' + str(round(item * 100, 2)) + '%'))
            else:
                beta_differences.append(str(str(round(item * 100, 2)) + '%'))
        results = {'stress': unique_stresses_f, 'original alpha': weibull_fit_alpha_array, 'original beta': weibull_fit_beta_array, 'new alpha': weibull_fit_alpha_array_common_shape, 'common beta': np.ones_like(unique_stresses_f) * common_shape, 'beta change': beta_differences}
        results_df = pd.DataFrame(results, columns=['stress', 'original alpha', 'original beta', 'new alpha', 'common beta', 'beta change'])
        blankIndex = [''] * len(results_df)
        results_df.index = blankIndex
        self.results = results_df
        if print_results is True:
            pd.set_option('display.width', 200)  # prevents wrapping after default 80 characters
            pd.set_option('display.max_columns', 9)  # shows the dataframe without ... truncation
            print('\nALT Weibull probability plot results:')
            print(self.results)
            print('Total AICc:', self.AICc_sum)
            print('Total BIC:', self.BIC_sum)
    def __init__(self, failures, failure_stress, right_censored=None, right_censored_stress=None, print_results=True, show_plot=True):

        # input type checking and converting to arrays in preperation for creation of dataframe
        if len(failures) != len(failure_stress):
            raise ValueError('The length of failures does not match the length of failure_stress')
        if type(failures) is list:
            failures = np.array(failures)
        elif type(failures) is np.ndarray:
            pass
        else:
            raise ValueError('failures must be an array or list')
        if type(failure_stress) is list:
            failure_stress = np.array(failure_stress)
        elif type(failure_stress) is np.ndarray:
            pass
        else:
            raise ValueError('failure_stress must be an array or list')
        if right_censored is not None:
            if len(right_censored) != len(right_censored_stress):
                raise ValueError('The length of right_censored does not match the length of right_censored_stress')
            if type(right_censored) is list:
                right_censored = np.array(right_censored)
            elif type(right_censored) is np.ndarray:
                pass
            else:
                raise ValueError('right_censored must be an array or list')
            if type(right_censored_stress) is list:
                right_censored_stress = np.array(right_censored_stress)
            elif type(right_censored_stress) is np.ndarray:
                pass
            else:
                raise ValueError('right_censored_stress must be an array or list')

        xmin = np.floor(np.log10(min(failures))) - 1
        xmax = np.ceil(np.log10(max(failures))) + 1
        xvals = np.logspace(xmin, xmax, 100)

        if right_censored is not None:
            TIMES = np.hstack([failures, right_censored])
            STRESS = np.hstack([failure_stress, right_censored_stress])
            CENS_CODES = np.hstack([np.ones_like(failures), np.zeros_like(right_censored)])
        else:
            TIMES = failures
            STRESS = failure_stress
            CENS_CODES = np.ones_like(failures)

        data = {'times': TIMES, 'stress': STRESS, 'cens_codes': CENS_CODES}
        df = pd.DataFrame(data, columns=['times', 'stress', 'cens_codes'])
        df_sorted = df.sort_values(by=['cens_codes', 'stress', 'times'])
        is_failure = df_sorted['cens_codes'] == 1
        is_right_cens = df_sorted['cens_codes'] == 0
        f_df = df_sorted[is_failure]
        rc_df = df_sorted[is_right_cens]
        unique_stresses_f = f_df.stress.unique()
        if right_censored is not None:
            unique_stresses_rc = rc_df.stress.unique()
            for item in unique_stresses_rc:  # check that there are no unique right_censored stresses that are not also in failure stresses
                if item not in unique_stresses_f:
                    raise ValueError('The right_censored_stress array contains values that are not in the failure_stress array. This is equivalent to trying to fit a distribution to only censored data and cannot be done.')

        weibull_fit_alpha_array = []
        weibull_fit_beta_array = []
        expon_fit_lambda_array = []
        color_list = ['steelblue', 'darkorange', 'red', 'green', 'purple', 'blue', 'grey', 'deeppink', 'cyan', 'chocolate']
        # within this loop, each list of failures and right censored values will be unpacked for each unique stress to find the common beta parameter
        for stress in unique_stresses_f:
            failure_current_stress_df = f_df[f_df['stress'] == stress]
            FAILURES = failure_current_stress_df['times'].values
            if right_censored is not None:
                if stress in unique_stresses_rc:
                    right_cens_current_stress_df = rc_df[rc_df['stress'] == stress]
                    RIGHT_CENSORED = right_cens_current_stress_df['times'].values
                else:
                    RIGHT_CENSORED = None
            else:
                RIGHT_CENSORED = None

            weibull_fit = Fit_Weibull_2P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False)
            weibull_fit_alpha_array.append(weibull_fit.alpha)
            weibull_fit_beta_array.append(weibull_fit.beta)

        # within this loop, each list of failures and right censored values will be unpacked for each unique stress and plotted as a probability plot as well as the CDF of the common beta plot
        AICc_total = 0
        BIC_total = 0
        AICc_total_weib = 0
        BIC_total_weib = 0
        AICc = True
        AICc_weib = True
        for i, stress in enumerate(unique_stresses_f):
            failure_current_stress_df = f_df[f_df['stress'] == stress]
            FAILURES = failure_current_stress_df['times'].values
            if right_censored is not None:
                if stress in unique_stresses_rc:
                    right_cens_current_stress_df = rc_df[rc_df['stress'] == stress]
                    RIGHT_CENSORED = right_cens_current_stress_df['times'].values
                else:
                    RIGHT_CENSORED = None
            else:
                RIGHT_CENSORED = None
            expon_fit = Fit_Expon_1P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False)
            weib_fit = Fit_Weibull_2P(failures=FAILURES, right_censored=RIGHT_CENSORED, show_probability_plot=False, print_results=False, force_beta=np.average(weibull_fit_beta_array))
            expon_fit_lambda_array.append(expon_fit.Lambda)
            if type(expon_fit.AICc) == str:
                AICc = False
            else:
                AICc_total += expon_fit.AICc
            if type(weib_fit.AICc) == str:
                AICc_weib = False
            else:
                AICc_total_weib += weib_fit.AICc
            BIC_total += expon_fit.BIC
            BIC_total_weib += weib_fit.BIC
            if show_plot is True:
                expon_fit.distribution.CDF(linestyle='--', color=color_list[i], xvals=xvals, plot_CI=False) # plotting of the confidence intervals has been turned off
                Probability_plotting.Weibull_probability_plot(failures=FAILURES, right_censored=RIGHT_CENSORED,plot_CI=False, color=color_list[i], label=str(stress))
                plt.legend(title='Stress')
                plt.xlim(10 ** (xmin + 1), 10 ** (xmax - 1))
                plt.title('ALT Exponential Probability Plot')
        self.BIC_sum = np.sum(BIC_total)
        self.BIC_sum_weibull = np.sum(BIC_total_weib)
        if AICc is True:
            self.AICc_sum = np.sum(AICc_total)
        else:
            self.AICc_sum = 'Insufficient Data'
        if AICc_weib is True:
            self.AICc_sum_weibull = np.sum(AICc_total_weib)
        else:
            self.AICc_sum_weibull = 'Insufficient Data'
        beta_difs = (1 - np.array(weibull_fit_beta_array)) / np.array(weibull_fit_beta_array)
        beta_differences = []
        for item in beta_difs:
            if item > 0:
                beta_differences.append(str('+' + str(round(item * 100, 2)) + '%'))
            else:
                beta_differences.append(str(str(round(item * 100, 2)) + '%'))
        results = {'stress': unique_stresses_f, 'weibull alpha': weibull_fit_alpha_array, 'weibull beta': weibull_fit_beta_array, 'new 1/Lambda': 1 / np.array(expon_fit_lambda_array), 'common shape': np.ones_like(unique_stresses_f), 'shape change': beta_differences}
        results_df = pd.DataFrame(results, columns=['stress', 'weibull alpha', 'weibull beta', 'new 1/Lambda', 'common shape', 'shape change'])
        blankIndex = [''] * len(results_df)
        results_df.index = blankIndex
        self.results = results_df
        if print_results is True:
            pd.set_option('display.width', 200)  # prevents wrapping after default 80 characters
            pd.set_option('display.max_columns', 9)  # shows the dataframe without ... truncation
            print('\nALT Exponential probability plot results:')
            print(self.results)
            print('Total AICc:', self.AICc_sum)
            print('Total BIC:', self.BIC_sum)
            print('Total AICc (weibull):', self.AICc_sum_weibull)
            print('Total BIC (weibull):', self.BIC_sum_weibull)
        if self.BIC_sum > self.BIC_sum_weibull:
            print('WARNING: The Weibull distribution would be a more appropriate fit for this data set as it has a lower BIC (using the average method to obtain BIC) than the Exponential distribution.')
#    else:
#        failures.append(item)
x = []
file_name = "mtbf_total_failures_gpu-cpu-mem_epoch1.txt"
with open(file_name) as log:
    for line in log:
        #if int(line) > threshold:
        #    censored.append(threshold)
        #else:
        x.append(int(line))

#fit the Weibull Mixture and Weibull_2P
#mixture = Fit_Weibull_Mixture(failures=failures,right_censored=censored)
fig, ax = plt.subplots(figsize=(5, 2))

single = Fit_Weibull_2P(
    failures=x, right_censored=censored)  #, show_probability_plot = False)

#plot the histogram of all the data and shade the censored part white
#n_bins = int(len(x)/100)
#N,bins,patches = plt.hist(x, density=True, alpha=0.2, color='k', bins=n_bins, edgecolor='k')
#for i in range(np.argmin(abs(np.array(bins)-threshold)),len(patches)): #this is to shade the censored part of the histogram as white
#    patches[i].set_facecolor('white')

#extract the y_vals from the mixture and build the Mixture PDF using the proportions
x = np.sort(x)
xvals2 = np.linspace(0, max(x), 5211)
xvals = np.arange(1, len(x) + 1) / len(x)
#xvals2 = np.arange(1, len(x)+1)

alpha_1 = 1197
beta_1 = 0.873
Exemple #11
0
def plot_event_dist(channels_input, Tend, xlim=175, bins=50, fit='weibull', filepath=None):
    """Plot wait-time distributions in the ESC and EPI states.
    
    Args:
        n_Tcounter (np.ndarray): array of wait-times for each cell type
        channels_input (dict): contains Channel objects with r0 and alpha params for each reaction c1hannel
        Tend (float): length of simulation
        fit (str): (weibull, gengamma) choose what distribution to fit
    
    """ 
    #sns.set_theme()
    #sns.set_style("ticks")
    plt.style.use('default')
    #sns.set_theme(style="ticks", font_scale=1.2)
    sns.set_context('notebook', font_scale=1.5)
    channels = copy.deepcopy(channels_input)
    n_samples_max = 15000

    
    ########################
    # FOR ESC
    ########################
    y_esc = [item for sublist in channels['esc'].wait_times for item in sublist] # flatten list
    y_esc = np.array(y_esc)
    
    # subsample distribution if too many samples
    if n_samples_max >= len(y_esc):
        n_samples_max = len(y_esc)-1
            
    nkeep = np.random.choice(len(y_esc), size=n_samples_max, replace=False)
    y_esc = y_esc[nkeep]

    #y_esc = channels['esc'].wait_times.reshape(-1) # reshape combines all simulations
    #y_esc = y_esc[y_esc != 0] # fix plot bug when t_wait==0
    
    fig, ax = plt.subplots(1,2)
    fig.set_size_inches(15,5)
    
    sns.histplot(y_esc, bins=bins, color=sns.color_palette()[0], stat='density', ax=ax[0])
    
    # fit
    x = np.linspace(0, Tend, 10000)
    if fit =='weibull':
        wbf = Fit_Weibull_2P(failures=y_esc, show_probability_plot=False, print_results=False)
        pdf = wbf.distribution.PDF(x,show_plot=False)  
    else:
        P = gengamma.fit(y_esc)
        pdf = gengamma.pdf(x, *P)
    sns.lineplot(x=x, y=pdf, color='r', lw=3, ax=ax[0])
    ax[0].set_xlim(0,xlim)
    ax[0].set_xlabel('Time before reaction [h]',fontsize=18)
    ax[0].set_ylabel('Density', fontsize=18)
    ax[0].set_title('Wait-time distribution for ESC to EPI differentiation', fontsize=18)
    
    # annotate
    ax[0].text(-0.1, 1.05, string.ascii_uppercase[1], transform=ax[0].transAxes, size=20, weight='bold')
    
    #plt.xlim(0, Tend)
    
    # overlay theoretical weibull
    
    esc_r0 = channels['esc'].r0
    esc_alpha = channels['esc'].alpha
    esc_weibull = weibull_density(esc_alpha,esc_r0, x)
    sns.lineplot(x=x,y=esc_weibull, color='y', lw=3, style=True, dashes=[(3,3)], ax=ax[0])
    ax[0].legend(['Simulation','Theoretical'])
    
    
    
    
    
    ########################
    # FOR EPI
    ########################
    y_epi = [item for sublist in channels['epi'].wait_times for item in sublist] # flatten
    y_epi = np.array(y_epi)
    if n_samples_max >= len(y_epi):
        n_samples_max = len(y_epi)-1
            
    nkeep = np.random.choice(len(y_epi), size=n_samples_max, replace=False)
    y_epi = y_epi[nkeep]
    
    sns.histplot(y_epi, bins=bins, color=sns.color_palette()[4], stat='density', ax=ax[1])
    
    # fit gamma
    x = np.linspace(0, Tend, 10000)
    if fit =='weibull':
        wbf = Fit_Weibull_2P(failures=y_epi, show_probability_plot=False, print_results=False)
        pdf = wbf.distribution.PDF(x,show_plot=False)  
    else:
        P = gengamma.fit(y_epi)
        pdf = gengamma.pdf(x, *P)
        
    sns.lineplot(x=x, y=pdf, color='r', lw=3, ax=ax[1])
    ax[1].set_xlim(0,xlim)
    ax[1].set(xlabel='Time before reaction [h]', ylabel='Density', title='Wait-time distribution for EPI to NPC differentiation')
    
    # overlay theoretical weibull
    
    epi_r0 = channels['epi'].r0
    epi_alpha = channels['epi'].alpha
    epi_weibull = weibull_density(epi_alpha,epi_r0, x)
    sns.lineplot(x=x,y=epi_weibull, color='y', lw=3,style=True, dashes=[(3,3)], ax=ax[1])
    ax[1].legend(['Simulation','Theoretical'])
    
    fig.tight_layout(h_pad=1, w_pad=0)
    
    if filepath: 
        fig.savefig(filepath, dpi=400, bbox_inches = 'tight', facecolor='w')
    
    plt.show()
    print("ESC:")
    print("   Obtained rate is %.3f vs %.3f" % (1/np.mean(y_esc),esc_r0))
    print("   Corresponding to %.1fh vs %.1fh" % (np.mean(y_esc),1/esc_r0))
    print("   std is %.1fh" % (np.std(y_esc)))
    print("EPI:")
    print("   Obtained rate is %.3f vs %.3f" % (1/np.mean(y_epi),epi_r0))
    print("   Corresponding to %.1fh vs %.1fh" % (np.mean(y_epi),1/epi_r0))
    print("   std is %.1fh" % (np.std(y_epi)))
    
    
    
    return None