Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -437,6 +437,86 @@ def call_r_script(
|
|
| 437 |
raise
|
| 438 |
|
| 439 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
def analyze_excel_single(file_path):
|
| 441 |
"""
|
| 442 |
Analyzes a single Excel file containing data and generates plots for Trust, NPS, Loyalty, Consideration, and Satisfaction models.
|
|
|
|
| 437 |
raise
|
| 438 |
|
| 439 |
|
| 440 |
+
|
| 441 |
+
def plot_trust_driver_bubbles(trust_df, title):
|
| 442 |
+
"""
|
| 443 |
+
Creates a bubble plot for Trust Drivers using preset positions and colors.
|
| 444 |
+
|
| 445 |
+
Args:
|
| 446 |
+
trust_df (DataFrame): DataFrame containing Trust driver data with an "Importance_percent" column.
|
| 447 |
+
title (str): Title of the plot.
|
| 448 |
+
|
| 449 |
+
Returns:
|
| 450 |
+
Image: PIL Image of the bubble plot.
|
| 451 |
+
"""
|
| 452 |
+
# Set the background image path
|
| 453 |
+
image_path = "./images/image.png"
|
| 454 |
+
|
| 455 |
+
# Load background image
|
| 456 |
+
try:
|
| 457 |
+
img = Image.open(image_path)
|
| 458 |
+
except FileNotFoundError:
|
| 459 |
+
raise FileNotFoundError(f"❌ Error: Background image '{image_path}' not found!")
|
| 460 |
+
|
| 461 |
+
# Define the fixed bubble order (for Trust Drivers)
|
| 462 |
+
bubble_order = ["Vision", "Development", "Benefit", "Competence", "Stability", "Relationship"]
|
| 463 |
+
|
| 464 |
+
# Colors for each bubble (in the same order)
|
| 465 |
+
colors = ["#DF8859", "#E3B05B", "#418387", "#6D93AB", "#375570", "#C63F48"]
|
| 466 |
+
|
| 467 |
+
# Bubble positions (aligned with the background image)
|
| 468 |
+
bubble_positions = [
|
| 469 |
+
(0.66, 1.20), # Vision (moved up)
|
| 470 |
+
(1.3, -0.1), # Development
|
| 471 |
+
(0.66, -1.10), # Benefit
|
| 472 |
+
(-0.70, -1.20), # Competence
|
| 473 |
+
(-1.42, 0.14), # Stability
|
| 474 |
+
(-0.70, 1.15) # Relationship (shifted left)
|
| 475 |
+
]
|
| 476 |
+
|
| 477 |
+
# Extract importance percentages for each predictor.
|
| 478 |
+
# If a predictor is missing, default to 0.
|
| 479 |
+
values_dict = trust_df.set_index("Predictor")["Importance_percent"].to_dict()
|
| 480 |
+
percentages = [values_dict.get(pred, 0) for pred in bubble_order]
|
| 481 |
+
|
| 482 |
+
# Scale bubble sizes dynamically based on the percentages
|
| 483 |
+
max_size = 0.35 # Maximum bubble size
|
| 484 |
+
min_size = 0.23 # Minimum bubble size
|
| 485 |
+
min_value, max_value = min(percentages), max(percentages)
|
| 486 |
+
bubble_sizes = [
|
| 487 |
+
min_size + (max_size - min_size) * ((p - min_value) / (max_value - min_value + 1e-5))
|
| 488 |
+
for p in percentages
|
| 489 |
+
]
|
| 490 |
+
|
| 491 |
+
# Create the figure and axis
|
| 492 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
| 493 |
+
ax.set_xlim(-2, 2)
|
| 494 |
+
ax.set_ylim(-2, 2)
|
| 495 |
+
ax.set_aspect('equal') # Lock the aspect ratio
|
| 496 |
+
ax.axis("off")
|
| 497 |
+
|
| 498 |
+
# Display the background image (ensure the extent aligns with your coordinate system)
|
| 499 |
+
ax.imshow(img, extent=[-1.5, 1.5, -1.5, 1.5])
|
| 500 |
+
|
| 501 |
+
# Draw bubbles and add centered percentage text
|
| 502 |
+
for i, (x, y) in enumerate(bubble_positions):
|
| 503 |
+
size = bubble_sizes[i]
|
| 504 |
+
circle = patches.Circle((x, y), size, facecolor=colors[i], alpha=1.0, lw=1.5)
|
| 505 |
+
ax.add_patch(circle)
|
| 506 |
+
ax.text(
|
| 507 |
+
x, y, f"{percentages[i]:.1f}%", fontsize=12, fontweight="bold",
|
| 508 |
+
ha="center", va="center", color="white"
|
| 509 |
+
)
|
| 510 |
+
|
| 511 |
+
# Optionally, add a title (if desired)
|
| 512 |
+
plt.title(title, fontsize=14)
|
| 513 |
+
|
| 514 |
+
# Save the plot to a bytes buffer and return a PIL Image
|
| 515 |
+
img_buffer = io.BytesIO()
|
| 516 |
+
plt.savefig(img_buffer, format="png", bbox_inches="tight", facecolor=fig.get_facecolor())
|
| 517 |
+
img_buffer.seek(0)
|
| 518 |
+
plt.close(fig)
|
| 519 |
+
return Image.open(img_buffer
|
| 520 |
def analyze_excel_single(file_path):
|
| 521 |
"""
|
| 522 |
Analyzes a single Excel file containing data and generates plots for Trust, NPS, Loyalty, Consideration, and Satisfaction models.
|