mouse_to_human_years.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy.interpolate import interp1d, splrep, splev
  4. import os
  5. # Define the data points
  6. mouse_days = np.array([0, 3, 14, 21, 28, 48])
  7. human_days = np.array([0, 1, 7, 90, 180, 360])
  8. phases = ['Hyper-acute', 'Acute', 'Early Subacute', 'Late Subacute', 'Chronic']
  9. phase_colors = [
  10. (0.8, 0.2, 0.2), # Hyper-acute
  11. (0.2, 0.8, 0.2), # Acute
  12. (0.2, 0.2, 0.8), # Early Subacute
  13. (0.8, 0.8, 0.2), # Late Subacute
  14. (0.5, 0.2, 0.5) # Chronic
  15. ]
  16. # Create linear interpolation for the first segment
  17. linear_fit = interp1d(mouse_days[:3], human_days[:3], kind='linear', fill_value='extrapolate')
  18. # Create a spline fit for the second segment
  19. spline_fit = splrep(mouse_days[2:], human_days[2:])
  20. # Generate finer x values for smooth curve plotting
  21. fine_mouse_days_linear = np.linspace(mouse_days[0], mouse_days[2], 100)
  22. fine_human_days_linear = linear_fit(fine_mouse_days_linear)
  23. fine_mouse_days_spline = np.linspace(mouse_days[2], mouse_days[-1], 100)
  24. fine_human_days_spline = splev(fine_mouse_days_spline, spline_fit)
  25. # Plot the data points and the fits
  26. plt.figure()
  27. plt.plot(mouse_days, human_days, 'o', markerfacecolor='b', label='Data Points')
  28. plt.plot(fine_mouse_days_linear, fine_human_days_linear, '-r', label='Linear Fit (0-14 days)')
  29. plt.plot(fine_mouse_days_spline, fine_human_days_spline, '-g', label='Spline Fit (14-48 days)')
  30. # Fill areas under the curve according to phases
  31. for i in range(len(mouse_days) - 1):
  32. if mouse_days[i+1] <= 14:
  33. fill_x = np.linspace(mouse_days[i], mouse_days[i+1], 100)
  34. fill_y = linear_fit(fill_x)
  35. else:
  36. fill_x = np.linspace(mouse_days[i], mouse_days[i+1], 100)
  37. fill_y = splev(fill_x, spline_fit)
  38. plt.fill_between(fill_x, 0, fill_y, color=phase_colors[i], alpha=0.5, label=phases[i])
  39. # Add dashed grey lines from data points to x and y ticks
  40. for i in range(len(mouse_days)):
  41. x = mouse_days[i]
  42. y = human_days[i]
  43. plt.plot([x, x], [0, y], '--', color='grey')
  44. plt.plot([0, x], [y, y], '--', color='grey')
  45. # Set the ticks for the dashed lines
  46. plt.xticks(mouse_days)
  47. plt.yticks(human_days)
  48. # Add labels and title with font size 10
  49. plt.xlabel('Mouse Days', fontsize=10)
  50. plt.ylabel('Human Days', fontsize=10)
  51. plt.title('Mouse Days to Human Days Conversion', fontsize=10)
  52. # Set the font size for the ticks
  53. plt.xticks(fontsize=10)
  54. plt.yticks(fontsize=10)
  55. # Add a legend for the phases, outside of the plot
  56. handles, labels = plt.gca().get_legend_handles_labels()
  57. by_label = dict(zip(labels, handles))
  58. plt.legend(by_label.values(), by_label.keys(), fontsize=10, bbox_to_anchor=(1.05, 1), loc='upper left')
  59. # Show grid for better visualization
  60. plt.grid(True)
  61. # Get the directory where the code file is located
  62. code_dir = os.path.dirname(os.path.abspath(__file__))
  63. # Get the parent directory of the code directory
  64. parent_dir = os.path.dirname(code_dir)
  65. # Define the output directory for saving the figure
  66. output_dir = os.path.join(parent_dir, 'output', 'figures')
  67. os.makedirs(output_dir, exist_ok=True)
  68. # Save the plot as SVG and PNG with specified size
  69. plt.gcf().set_size_inches(9/2.54, 18/2.54) # Convert cm to inches
  70. plt.savefig(os.path.join(output_dir, 'mouse_to_human_days.svg'), format='svg', bbox_inches='tight')
  71. plt.savefig(os.path.join(output_dir, 'mouse_to_human_days.png'), format='png', bbox_inches='tight',dpi = 300)
  72. # Show the plot
  73. plt.show()