# -*- coding: utf-8 -*-
# aalen additive

if __name__ == "__main__":
    import pandas as pd
    import numpy as np
    import time

    from lifelines import LogNormalFitter

    np.random.seed(1)
    N = 250000
    mu = 3 * np.random.randn()
    sigma = np.random.uniform(0.1, 3.0)

    X, C = np.exp(sigma * np.random.randn(N) +
                  mu), np.exp(np.random.randn(N) + mu)
    E = X <= C
    T = np.minimum(X, C)

    lnf = LogNormalFitter()
    start_time = time.time()
    lnf.fit(T, E)
    print("--- %s seconds ---" % (time.time() - start_time))
    lnf.print_summary(5)
        plot_hist(uptime, obs_up, 99, weib)
        plt.title(f'Probability of failure in time [general]'#, reasons: ' +  ', '.join([str(x) for x in reasons_relative])
                  )
        plt.savefig(rf'./{file_used.split(".")[0]}/figures/fail_prob_{file_used.split(".")[0]}_general.pdf', dpi=2400, layout='tight')
        plt.savefig(rf'./{file_used.split(".")[0]}/figures/fail_prob_{file_used.split(".")[0]}_general.png', dpi=2400, layout='tight')
        plt.close()


    # GENERATE REPAIR TIME DEFINITION
    # Generate a lognormal distribution for the repair time
    print('Repair time')
    print(len('Repair time') * '-')

    lnf = LogNormalFitter()
    try:
        lnf.fit(downtime, obs_down)
        logn = Lognormal(lnf.sigma_, lnf.mu_)
    except:
        raise
    if print_all:
        print(logn)
    if export_all:
        repair_dist = ET.SubElement(root, 'repair_dist')
        repair_dist.text = 'lognormal'
        repair_dist.set("sigma", str(lnf.sigma_))
        repair_dist.set("mu", str(lnf.mu_))
        repair_dist.set("mean", str(logn.mean_time()))
        plot_hist(downtime, obs_down, 99, logn)
        plt.title(f"Probability of repair in time"#, reasons: {', '.join([str(x) for x in reasons_relative])}"
                  )
        plt.savefig(rf'./{file_used.split(".")[0]}/figures/fail_prob_{file_used.split(".")[0]}_repair.pdf', dpi=2400, layout='tight')