w-d-plot.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from mpl_toolkits.mplot3d import Axes3D
  5. import matplotlib.cm as cm
  6. import matplotlib.colors as colors
  7. import argparse
  8. import sys
  9. from model_properties import model_properties
  10. def calculate_metric(model_type, metric, W, D):
  11. """
  12. Calculates the Z-axis data (metric) for the given model type over the meshgrid W, D.
  13. Uses the logic defined in model_properties.py.
  14. """
  15. # Create a vectorized wrapper around the model_properties function
  16. def get_value(w, d):
  17. props = model_properties(model_type, int(w), int(d))
  18. if metric == "models":
  19. return props.get("Atomic Models", 0)
  20. elif metric == "events":
  21. return props.get("Events", 0)
  22. return 0
  23. # Vectorize allows the function to operate on the numpy meshgrids W and D efficiently
  24. vec_calc = np.vectorize(get_value)
  25. return vec_calc(W, D)
  26. def plot_data(model_type, metric, max_val):
  27. w_vals = np.arange(1, max_val + 1)
  28. d_vals = np.arange(1, max_val + 1)
  29. W, D = np.meshgrid(w_vals, d_vals)
  30. print(f"Calculating '{metric}' for model '{model_type}'...")
  31. Z_data = calculate_metric(model_type, metric, W, D)
  32. x = W.ravel()
  33. y = D.ravel()
  34. z = np.zeros_like(x)
  35. dx = dy = 0.6
  36. dz = Z_data.ravel()
  37. norm = colors.Normalize(vmin=dz.min(), vmax=dz.max())
  38. colors_map = cm.viridis(norm(dz))
  39. fig = plt.figure(figsize=(12, 8))
  40. ax = fig.add_subplot(111, projection='3d')
  41. ax.bar3d(x - dx / 2, y - dy / 2, z, dx, dy, dz, shade=True, color=colors_map, zsort='average')
  42. metric_label = "# Atomic Models" if metric == "models" else "# Events"
  43. ax.set_xlabel('Width')
  44. ax.set_ylabel('Depth')
  45. ax.set_zlabel(metric_label)
  46. tick_step = max(1, max_val // 10)
  47. ax.set_xticks(w_vals[::tick_step])
  48. ax.set_yticks(d_vals[::tick_step])
  49. ax.set_title(f"{metric_label} for {model_type} Model")
  50. ax.view_init(elev=30, azim=-135)
  51. mappable = cm.ScalarMappable(norm=norm, cmap=cm.viridis)
  52. mappable.set_array(dz)
  53. cbar = fig.colorbar(mappable, ax=ax, shrink=0.6, pad=0.1)
  54. cbar.set_label('Count')
  55. plt.tight_layout()
  56. print("Displaying plot...")
  57. plt.show()
  58. def main():
  59. parser = argparse.ArgumentParser(description="Plot 3D bar chart for DEVS Model metrics.")
  60. parser.add_argument(
  61. "-m", "--model",
  62. type=str,
  63. choices=["LI", "HI", "HO", "HOmod"],
  64. default="LI",
  65. help="The model type to analyze (default: LI)"
  66. )
  67. parser.add_argument(
  68. "-z", "--z-axis",
  69. type=str,
  70. choices=["models", "events"],
  71. default="models",
  72. help="The metric to plot on the Z-axis: 'models' or 'events' (default: models)"
  73. )
  74. parser.add_argument(
  75. "-l", "--limit",
  76. type=int,
  77. default=25,
  78. help="Maximum value for Width and Depth ranges (default: 25)"
  79. )
  80. args = parser.parse_args()
  81. plot_data(args.model, args.z_axis, args.limit)
  82. if __name__ == "__main__":
  83. main()