NO_BREAK_RELIABILITY / NO_BREAK_RELIABILITY_app.py
cadu70's picture
Upload 2 files
8669933 verified
import numpy as np
import matplotlib.pyplot as plt
import math
import gradio as gr
# Function to calculate binomial coefficient
def binom(n, k):
return math.factorial(n) // (math.factorial(k) * math.factorial(n - k))
# Function to calculate total reliability with redundancy
def total_reliability_with_redundancy(N, X, MTBF, t):
R = np.exp(-t / MTBF) # Reliability of a single component at time t
R_total = 0
for k in range(X + 1): # Summing up to account for up to X failures
R_total += binom(N + X, k) * (R ** (N + X - k)) * ((1 - R) ** k)
return R_total * 100 # Convert to percentage
# Function to generate the plot
def plot_reliability(N, X, MTBF, time_hours):
t = np.linspace(0, 20000, 500) # Time range (0 to 20,000 hours)
R_total = [total_reliability_with_redundancy(N, X, MTBF, ti) for ti in t]
# Calculate reliability at the selected time in percentage
R_at_time = total_reliability_with_redundancy(N, X, MTBF, time_hours)
# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(t, R_total, label=f'Total reliability (N={N}, X={X}, MTBF={MTBF} hours)')
# Draw vertical line at the selected time point
plt.axvline(x=time_hours, color='red', linestyle='--', label=f'Time = {time_hours} hours')
# Draw horizontal line at the corresponding reliability
plt.axhline(y=R_at_time, color='blue', linestyle='--', label=f'Reliability = {R_at_time:.2f}%')
# Plot the intersection point (green)
plt.scatter(time_hours, R_at_time, color='green', s=100, zorder=5, label='Intersection Point')
# Set labels and title
plt.xlabel('Time (hours)')
plt.ylabel('Reliability (%)')
plt.title('Total System Reliability with Redundancy')
plt.ylim(0, 100)
plt.grid(True)
plt.legend()
# Save the plot to a file and return it
plt.savefig("reliability_plot.png")
plt.close()
return "reliability_plot.png"
# Define Gradio interface
def gradio_interface(N, X, MTBF, time_hours):
return plot_reliability(N, X, MTBF, time_hours)
# Create the Gradio interface
gradio_app = gr.Interface(
fn=gradio_interface,
inputs=[
gr.Slider(1, 10, value=3, label="N (components)"),
gr.Slider(0, 5, value=1, label="X (redundancy)"),
gr.Slider(1000, 50000, step=1000, value=10000, label="MTBF (hours)"),
gr.Slider(0, 20000, step=100, value=5000, label="Hours")
],
outputs=gr.Image(label="Reliability Plot"),
title="System Reliability with Redundancy",
description="Adjust the sliders to change the parameters of the system and view the corresponding reliability curve."
)
# Launch the app
gradio_app.launch()