1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import numpy as np
- import matplotlib.pyplot as plt
- from scipy.interpolate import interp1d, splrep, splev
- import os
- # Define the data points
- mouse_days = np.array([0, 3, 14, 21, 28, 48])
- human_days = np.array([0, 1, 7, 90, 180, 360])
- phases = ['Hyper-acute', 'Acute', 'Early Subacute', 'Late Subacute', 'Chronic']
- phase_colors = [
- (0.8, 0.2, 0.2), # Hyper-acute
- (0.2, 0.8, 0.2), # Acute
- (0.2, 0.2, 0.8), # Early Subacute
- (0.8, 0.8, 0.2), # Late Subacute
- (0.5, 0.2, 0.5) # Chronic
- ]
- # Create linear interpolation for the first segment
- linear_fit = interp1d(mouse_days[:3], human_days[:3], kind='linear', fill_value='extrapolate')
- # Create a spline fit for the second segment
- spline_fit = splrep(mouse_days[2:], human_days[2:])
- # Generate finer x values for smooth curve plotting
- fine_mouse_days_linear = np.linspace(mouse_days[0], mouse_days[2], 100)
- fine_human_days_linear = linear_fit(fine_mouse_days_linear)
- fine_mouse_days_spline = np.linspace(mouse_days[2], mouse_days[-1], 100)
- fine_human_days_spline = splev(fine_mouse_days_spline, spline_fit)
- # Plot the data points and the fits
- plt.figure()
- plt.plot(mouse_days, human_days, 'o', markerfacecolor='b', label='Data Points')
- plt.plot(fine_mouse_days_linear, fine_human_days_linear, '-r', label='Linear Fit (0-14 days)')
- plt.plot(fine_mouse_days_spline, fine_human_days_spline, '-g', label='Spline Fit (14-48 days)')
- # Fill areas under the curve according to phases
- for i in range(len(mouse_days) - 1):
- if mouse_days[i+1] <= 14:
- fill_x = np.linspace(mouse_days[i], mouse_days[i+1], 100)
- fill_y = linear_fit(fill_x)
- else:
- fill_x = np.linspace(mouse_days[i], mouse_days[i+1], 100)
- fill_y = splev(fill_x, spline_fit)
-
- plt.fill_between(fill_x, 0, fill_y, color=phase_colors[i], alpha=0.5, label=phases[i])
- # Add dashed grey lines from data points to x and y ticks
- for i in range(len(mouse_days)):
- x = mouse_days[i]
- y = human_days[i]
- plt.plot([x, x], [0, y], '--', color='grey')
- plt.plot([0, x], [y, y], '--', color='grey')
- # Set the ticks for the dashed lines
- plt.xticks(mouse_days)
- plt.yticks(human_days)
- # Add labels and title with font size 10
- plt.xlabel('Mouse Days', fontsize=10)
- plt.ylabel('Human Days', fontsize=10)
- plt.title('Mouse Days to Human Days Conversion', fontsize=10)
- # Set the font size for the ticks
- plt.xticks(fontsize=10)
- plt.yticks(fontsize=10)
- # Add a legend for the phases, outside of the plot
- handles, labels = plt.gca().get_legend_handles_labels()
- by_label = dict(zip(labels, handles))
- plt.legend(by_label.values(), by_label.keys(), fontsize=10, bbox_to_anchor=(1.05, 1), loc='upper left')
- # Show grid for better visualization
- plt.grid(True)
- # Get the directory where the code file is located
- code_dir = os.path.dirname(os.path.abspath(__file__))
- # Get the parent directory of the code directory
- parent_dir = os.path.dirname(code_dir)
- # Define the output directory for saving the figure
- output_dir = os.path.join(parent_dir, 'output', 'figures')
- os.makedirs(output_dir, exist_ok=True)
- # Save the plot as SVG and PNG with specified size
- plt.gcf().set_size_inches(9/2.54, 18/2.54) # Convert cm to inches
- plt.savefig(os.path.join(output_dir, 'mouse_to_human_days.svg'), format='svg', bbox_inches='tight')
- plt.savefig(os.path.join(output_dir, 'mouse_to_human_days.png'), format='png', bbox_inches='tight',dpi = 300)
- # Show the plot
- plt.show()
|