def get_info_on_events(fault_tree):
    events = []

    event_dictionary = {
        'name': get_object_name(fault_tree.top_event.name),
        'mtbf': fault_tree.top_event.MTTF,
        'mtbr': fault_tree.top_event.MTTR,
        'reliability': fault_tree.top_event.reliability_distribution,
        'maintainability': fault_tree.top_event.maintainability_distribution,
        'oper_avail': fault_tree.top_event.availability_operational
    }

    events.append(event_dictionary)

    for basic_event in fault_tree.get_basic_events():
        event_dictionary = {
            'name': get_object_name(basic_event.name),
            'mtbf': basic_event.MTTF,
            'mtbr': basic_event.MTTR,
            'reliability': basic_event.reliability_distribution,
            'maintainability': basic_event.maintainability_distribution,
            'oper_avail': basic_event.availability_operational
        }
        events.append(event_dictionary)

    return events
def plot_arbitrary_distribution_compare(name, metric, times, linspace,
                                        theoretical):
    # Reliability for now
    # Maybe fix inconsistencies with the numbers on the axis
    fig, subplots = setup_fig_subplots(metric)
    #fig.suptitle(name)

    # PDF
    subplots[0].set_title('PDF')

    theoretical_cdf = 0
    if metric == 'Reliability':
        theoretical_cdf = 1 - theoretical
        sns.distplot(times,
                     hist=True,
                     ax=subplots[0],
                     label='Time to failures')
    if metric == 'Maintainability':
        theoretical_cdf = theoretical
        sns.distplot(times, hist=True, ax=subplots[0], label='Time to repairs')
    index = get_index_of_first_zero_in_array(1 - theoretical_cdf)
    theoretical_pdf = differentiate(linspace, theoretical_cdf)

    x, pdf = subplots[0].lines[0].get_data()
    subplots[0].plot(x, pdf, 'b-', lw=1, alpha=0.6, label='Reconstructed')
    subplots[0].plot(linspace[:index],
                     theoretical_pdf[:index],
                     'r-',
                     lw=1.5,
                     alpha=0.6,
                     label='Theoretical')
    subplots[0].set_xlabel('time (t)')
    subplots[0].set_ylabel('P(t)')
    subplots[0].legend()
    #subplots[0].set_xlim([0, 30])

    # CDF
    sns.kdeplot(times, cumulative=True, ax=subplots[1], label='Reconstructed')
    subplots[1].plot(linspace[:index],
                     theoretical_cdf[:index],
                     'r-',
                     lw=1.5,
                     alpha=0.6,
                     label='Theoretical')
    subplots[1].legend()
    subplots[1].set_xlabel('time (t)')
    subplots[1].set_ylabel('P(T\u2264t)')
    #subplots[1].set_xlim([0, 30])
    _, cdf = subplots[1].lines[0].get_data()

    if metric == 'Maintainability':
        subplots[1].set_title('CDF (Maintainability)')

        subplots[2].plot(x,
                         pdf / (1 - cdf),
                         'b-',
                         lw=1.5,
                         alpha=0.6,
                         label='Reconstructed')

        subplots[2].plot(linspace[:index],
                         theoretical_pdf[:index] /
                         (1 - theoretical_cdf[:index]),
                         'r-',
                         lw=1.5,
                         alpha=0.6,
                         label='Theoretical')
        subplots[2].set_title('Repair Rate')
        subplots[2].set_title('Repair Rate')
        subplots[2].set_xlabel('time (t)')
        subplots[2].set_ylabel('\u03BC(t)')
        #subplots[2].set_xlim([0, 50])
        subplots[2].set_ylim([0, 5])
        subplots[2].legend()
        #subplots[3].set_xlim([0, 320])
    if metric == 'Reliability':
        theoretical_reliability = theoretical
        reliability = 1 - cdf
        subplots[1].set_title('CDF')

        #times.sort()
        #samples = len(times)
        #one_minus_cdf = [1 - (x / samples) for x in range(1, samples + 1)]
        #cdf = [(x / samples) for x in range(1, samples + 1)]

        # Get length of this and see if its the same length as the reliability of the top event
        # Better yet when calculating relability function use the same number of elements as the
        # length of top events time of failure or time of repair.
        # Reliability
        subplots[2].plot(x,
                         reliability,
                         'b-',
                         lw=1.5,
                         alpha=0.6,
                         label='Reconstructed')
        subplots[2].plot(linspace[:index],
                         theoretical_reliability[:index],
                         'r-',
                         lw=1.5,
                         alpha=0.6,
                         label='Theoretical')
        subplots[2].set_ylim([0, 1.05])
        # For comparing graphs
        subplots[2].set_title('Reliability')
        subplots[2].set_xlabel('time (t)')
        subplots[2].set_ylabel('R(t)')
        subplots[2].legend()
        subplots[3].plot(x,
                         pdf / reliability,
                         'b-',
                         lw=1.5,
                         alpha=0.6,
                         label='Reconstructed')
        #subplots[2].set_ylim([0, 0.01])

        subplots[3].plot(linspace[:index],
                         theoretical_pdf[:index] /
                         theoretical_reliability[:index],
                         'r-',
                         lw=1.5,
                         alpha=0.6,
                         label='Theoretical')
        subplots[3].set_title('Failure Rate')
        subplots[3].set_xlabel('time (t)')
        subplots[3].set_ylabel('\u03BB(t)')
        #subplots[3].set_ylim([0, 0.1])
        subplots[3].legend()
        #subplots[3].set_xlim([300, 325])

    plt.tight_layout()
    #if EXPORT_PNG is True:
    fig.savefig(os.getcwd() + '/static/images/' + get_object_name(name) + '_' +
                metric + '.png')
    #else:
    plt.show(block=False)
def plot_arbitrary__distribution_no_compare(name, metric, times):
    # Reliability for now
    # Maybe fix inconsistencies with the numbers on the axis
    fig, subplots = setup_fig_subplots(metric)
    #fig.suptitle(name + '\n\n', fontsize=16)

    # PDF
    subplots[0].set_title('PDF')

    theoretical_cdf = 0
    if metric == 'Reliability':
        sns.distplot(times,
                     hist=True,
                     ax=subplots[0],
                     label='Time to failures')
    if metric == 'Maintainability':
        sns.distplot(times, hist=True, ax=subplots[0], label='Time to repairs')

    x, pdf = subplots[0].lines[0].get_data()
    subplots[0].plot(x, pdf, 'b-', lw=1, alpha=0.6, label='Reconstructed')
    subplots[0].set_xlabel('time (t)')
    subplots[0].set_ylabel('P(t)')
    subplots[0].legend()
    #subplots[0].set_xlim([0, 30])

    # CDF
    sns.kdeplot(times, cumulative=True, ax=subplots[1])
    _, cdf = subplots[1].lines[0].get_data()
    subplots[1].plot(x, cdf, 'b-', lw=1, alpha=0.6, label='Reconstructed')
    subplots[1].set_xlabel('time (t)')
    subplots[1].set_ylabel('P(T\u2264t)')
    subplots[1].legend()

    if metric == 'Maintainability':
        subplots[1].set_title('CDF (Maintainability)')

        subplots[2].plot(x,
                         pdf / (1 - cdf),
                         'b-',
                         lw=1.5,
                         alpha=0.6,
                         label='Reconstructed')
        subplots[2].set_title('Repair Rate')
        subplots[2].set_xlabel('time (t)')
        subplots[2].set_ylabel('\u03BC(t)')
        subplots[2].set_ylim([0, 5])
        subplots[2].legend()

    if metric == 'Reliability':
        reliability = 1 - cdf
        subplots[1].set_title('CDF')

        # Get length of this and see if its the same length as the reliability of the top event
        # Better yet when calculating relability function use the same number of elements as the
        # length of top events time of failure or time of repair.
        # Reliability
        subplots[2].plot(x,
                         reliability,
                         'b-',
                         lw=1.5,
                         alpha=0.6,
                         label='Reconstructed')
        subplots[2].set_ylim([0, 1.05])
        subplots[2].legend()
        # For comparing graphs
        # subplots[2].set_xlim([0, 100])
        subplots[2].set_title('Reliability')
        subplots[2].set_xlabel('time (t)')
        subplots[2].set_ylabel('R(t)')

        subplots[3].plot(x,
                         pdf / reliability,
                         'b-',
                         lw=1.5,
                         alpha=0.6,
                         label='Reconstructed')
        #subplots[2].set_ylim([0, 0.01])

        subplots[3].set_title('Failure Rate')
        subplots[3].set_xlabel('time (t)')
        subplots[3].set_ylabel('\u03BB(t)')
        #subplots[3].set_ylim([0, 0.1])
        subplots[3].legend()
        #subplots[3].set_xlim([300, 325])

    # plt.show()
    plt.tight_layout()
    #if EXPORT_PNG is True:
    fig.savefig(os.getcwd() + '/static/images/' + get_object_name(name) + '_' +
                metric + '.png')
    #else:
    plt.show(block=False)
def plot_unidentified_distribution_comparison(name, metric, times,
                                              theoretical_distribution):
    # Maybe fix inconsistencies with the numbers on the axis
    fig, subplots = setup_fig_subplots(metric)

    linspace = calculate_linspace(theoretical_distribution)
    theoretical_pdf = calculate_pdf(theoretical_distribution, linspace)
    theoretical_cdf = calculate_cdf(theoretical_distribution, linspace)
    theoretical_reliability = calculate_reliability(theoretical_distribution,
                                                    linspace)

    if metric == 'Reliability':
        sns.distplot(times,
                     hist=True,
                     ax=subplots[0],
                     label='Time to failures')
    if metric == 'Maintainability':
        sns.distplot(times, hist=True, ax=subplots[0], label='Time to repairs')

    x, pdf = subplots[0].lines[0].get_data()
    subplots[0].plot(x, pdf, 'b-', lw=1, alpha=0.6, label='Reconstructed')
    subplots[0].set_xlabel('time (t)')

    subplots[0].plot(linspace,
                     theoretical_pdf,
                     'r-',
                     lw=1,
                     alpha=0.6,
                     label='Theoretical')
    subplots[0].set_title('PDF')
    subplots[0].set_xlabel('time (t)')
    subplots[0].set_ylabel('P(t)')
    subplots[0].legend()

    # CDF
    sns.kdeplot(times, cumulative=True, ax=subplots[1])
    _, cdf = subplots[1].lines[0].get_data()
    # Second plot CDF and/or Maintainability
    subplots[1].plot(x, cdf, 'b-', lw=1, alpha=0.6, label='Reconstructed')
    subplots[1].plot(linspace,
                     theoretical_cdf,
                     'r-',
                     lw=1,
                     alpha=0.6,
                     label='Theoretical')
    subplots[1].set_xlabel('time (t)')
    subplots[1].set_ylabel('P(T\u2264t)')
    subplots[1].legend()

    if metric == 'Maintainability':
        subplots[1].set_title('CDF (Maintainability)')
        subplots[2].plot(x,
                         pdf / (1 - cdf),
                         'b-',
                         lw=1.5,
                         alpha=0.6,
                         label='Reconstructed')
        subplots[2].plot(linspace,
                         theoretical_pdf / theoretical_reliability,
                         'r-',
                         lw=1,
                         alpha=0.6,
                         label='Theoretical')
        subplots[2].set_title('Repair Rate')
        subplots[2].set_xlabel('time (t)')
        subplots[2].set_ylabel('\u03BC(t)')
        subplots[2].set_ylim([0, 5])
        subplots[2].legend()

    if metric == 'Reliability':
        reliability = 1 - cdf
        subplots[1].set_title('CDF')

        subplots[2].plot(x,
                         reliability,
                         'b-',
                         lw=1.5,
                         alpha=0.6,
                         label='Reconstructed')
        subplots[2].set_ylim([0, 1.05])
        # Get length of this and see if its the same length as the reliability of the top event
        # Better yet when calculating relability function use the same number of elements as the
        # length of top events time of failure or time of repair.
        subplots[2].plot(linspace,
                         theoretical_reliability,
                         'r-',
                         lw=1,
                         alpha=0.6,
                         label='Theoretical')
        # For comparing graphs
        # subplots[2].set_xlim([0, 100])
        subplots[2].set_title('Reliability')
        subplots[2].set_xlabel('time (t)')
        subplots[2].set_ylabel('R(t)')
        subplots[2].legend()

        subplots[3].plot(x,
                         pdf / reliability,
                         'b-',
                         lw=1.5,
                         alpha=0.6,
                         label='Reconstructed')
        subplots[3].plot(linspace,
                         theoretical_pdf / theoretical_reliability,
                         'r-',
                         lw=1,
                         alpha=0.6,
                         label='Theoretical')
        subplots[3].set_title('Failure Rate')
        subplots[3].set_xlabel('time (t)')
        subplots[3].set_ylabel('\u03BB(t)')
        subplots[3].legend()

    # plt.show()
    plt.tight_layout()
    #if EXPORT_PNG is True:
    fig.savefig(os.getcwd() + '/static/images/' + get_object_name(name) + '_' +
                metric + '.png')
    plt.show(block=False)
def plot_identified_distribution_comparison(name, metric, distribution, times,
                                            theoretical_distribution):
    fig, subplots = setup_fig_subplots(metric)
    #fig.suptitle(name + '\n\n', fontsize=16)

    linspace = calculate_linspace(distribution)
    pdf = calculate_pdf(distribution, linspace)
    cdf = calculate_cdf(distribution, linspace)

    theoretical_pdf = calculate_pdf(theoretical_distribution, linspace)
    theoretical_cdf = calculate_cdf(theoretical_distribution, linspace)

    # First plot PDF
    subplots[0].plot(linspace,
                     pdf,
                     'b-',
                     lw=1,
                     alpha=0.6,
                     label='Reconstructed')
    subplots[0].plot(linspace,
                     theoretical_pdf,
                     'r-',
                     lw=1,
                     alpha=0.6,
                     label='Theoretical')
    subplots[0].set_xlabel('time (t)')
    subplots[0].set_ylabel('P(t)')
    subplots[0].legend()

    if times != EMPTY_LIST:
        if metric == 'Reliability':
            subplots[0].hist(times,
                             bins=20,
                             normed=True,
                             histtype='stepfilled',
                             alpha=0.2,
                             label='Time to failures')
        if metric == 'Maintainability':
            subplots[0].hist(times,
                             bins=20,
                             normed=True,
                             histtype='stepfilled',
                             alpha=0.2,
                             label='Time to repairs')
        subplots[0].legend()
    subplots[0].set_title('PDF')

    # Second plot CDF and/or Maintainability
    subplots[1].plot(linspace,
                     cdf,
                     'b-',
                     lw=1,
                     alpha=0.6,
                     label='Reconstructed')
    subplots[1].plot(linspace,
                     theoretical_cdf,
                     'r-',
                     lw=1,
                     alpha=0.6,
                     label='Theoretical')
    subplots[1].set_xlabel('time (t)')
    subplots[1].set_ylabel('P(T\u2264t)')
    subplots[1].legend()

    if metric == 'Maintainability':
        subplots[1].set_title('CDF (Maintainability)')
        subplots[2].plot(linspace,
                         pdf / (1 - cdf),
                         'b-',
                         lw=1,
                         alpha=0.6,
                         label='Reconstructed')
        subplots[2].plot(linspace,
                         theoretical_pdf / (1 - theoretical_cdf),
                         'r-',
                         lw=1,
                         alpha=0.6,
                         label='Theoretical')
        subplots[2].set_title('Repair Rate')
        subplots[2].set_xlabel('time (t)')
        subplots[2].set_ylabel('\u03BC(t)')
        subplots[2].legend()
        if distribution[0] == 'EXP':
            subplots[2].set_ylim([0, 1])

    # Third plot Reliability
    if metric == 'Reliability':
        subplots[1].set_title('CDF')
        reliability = calculate_reliability(distribution, linspace)
        theoretical_reliability = calculate_reliability(
            theoretical_distribution, linspace)
        subplots[2].plot(linspace,
                         reliability,
                         'b-',
                         lw=1,
                         alpha=0.6,
                         label='Reconstructed')
        subplots[2].plot(linspace,
                         theoretical_reliability,
                         'r-',
                         lw=1,
                         alpha=0.6,
                         label='Theoretical')
        subplots[2].set_xlabel('time (t)')
        subplots[2].set_ylabel('R(t)')
        subplots[2].legend()
        subplots[2].set_title(metric)

        subplots[3].plot(linspace,
                         pdf / reliability,
                         'b-',
                         lw=1,
                         alpha=0.6,
                         label='Reconstructed')
        subplots[3].plot(linspace,
                         theoretical_pdf / theoretical_reliability,
                         'r-',
                         lw=1,
                         alpha=0.6,
                         label='Theoretical')
        subplots[3].set_title('Failure Rate')
        subplots[3].set_xlabel('time (t)')
        subplots[3].set_ylabel('\u03BB(t)')
        subplots[3].legend()
        if distribution[0] == 'EXP':
            subplots[3].set_ylim([0, 1])

    # plt.show()
    plt.tight_layout()
    #if EXPORT_PNG is True:
    fig.savefig(os.getcwd() + '/static/images/' + get_object_name(name) + '_' +
                metric + '.png')
    #else:
    plt.show(block=False)