Conversation
Added plot of CRM with confidence intervals, and removed some old notes
improved CRM plotting with confidence intervals and made a function in utilities.py
Merge Ubuntu version of CRM
Reviewer's Guide by SourceryThis pull request introduces a new plotting function for visualizing CRM dynamics with credible intervals and modifies the shape of the sigma parameter in the No diagrams generated as the changes look simple and do not need a visual representation. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @ChaniaClare - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider extracting the plotting logic into a separate module to improve code organization.
- It might be helpful to add argument validation to the plotting functions.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
mimic/utilities/utilities.py
Outdated
|
|
||
| return fig, ax | ||
|
|
||
| def plot_CRM_with_intervals(observed_species, observed_resources, species_lower, species_upper, |
There was a problem hiding this comment.
issue (complexity): Consider extracting the repeated plotting patterns into helper functions to reduce code duplication and improve readability in the plot_CRM_with_intervals function, such as plotting trajectories with intervals and plotting true data points.
The new function introduces duplicated loops for plotting trajectories, intervals, and optional true data points. Consider extracting the repeated patterns into small helper functions. For example, create one helper to plot a series with its ribbon and another to plot true data points:
def plot_trajectory_with_interval(ax, times, data, lower, upper, label_prefix, linestyle='-', linewidth=2, color_offset=0):
cmap = plt.cm.tab10
for i in range(data.shape[1]):
color = cmap(i + color_offset)
ax.plot(times, data[:, i], label=f'{label_prefix} {i+1}', linestyle=linestyle, linewidth=linewidth, color=color)
ax.fill_between(times, lower[:, i], upper[:, i], alpha=0.2, color=color)
def plot_true_data(ax, times, true_data, data_prefix, color_offset=0):
cmap = plt.cm.tab10
col_template = f'{data_prefix.lower()}_{{}}'
for i in range(1, true_data.shape[1]+1): # Adjust range if needed
col_name = col_template.format(i)
if col_name in true_data.columns:
marker = 'o' if data_prefix.lower() == 'species' else 's'
ax.scatter(times, true_data[col_name], marker=marker, s=30,
color=cmap(i - 1 + color_offset), label=f'True {data_prefix} {i}')
# In plot_CRM_with_intervals, use:
def plot_CRM_with_intervals(observed_species, observed_resources, species_lower, species_upper,
resource_lower, resource_upper, times, filename=None):
fig, ax = plt.subplots(figsize=(12, 8))
plot_trajectory_with_interval(ax, times, observed_species, species_lower, species_upper, 'Species', linestyle='-', linewidth=2)
plot_trajectory_with_interval(ax, times, observed_resources, resource_lower, resource_upper, 'Resource', linestyle='--', linewidth=2, color_offset=observed_species.shape[1])
if filename:
true_data = pd.read_csv(filename)
true_times = true_data['time'].values
plot_true_data(ax, true_times, true_data, 'species')
plot_true_data(ax, true_times, true_data, 'resource', color_offset=observed_species.shape[1])
ax.set_xlabel('Time', fontsize=14)
ax.set_ylabel('Concentration', fontsize=14)
ax.set_title('Consumer-Resource Model Dynamics with 95% Credible Intervals', fontsize=16)
ax.legend(loc='best', fontsize=12)
ax.grid(True, alpha=0.3)
plt.tight_layout()
if filename:
plt.savefig(f"{filename.split('.')[0]}_with_intervals.png", dpi=300)
plt.show()This reduces duplication while keeping functionality intact.
Pull Request To-Do List
Description of the changes:
I have done the following to make this PR ready for review:
For the reviewer, make sure this PR meets these criteria before merging:
Summary by Sourcery
Enhance the Consumer-Resource Model (CRM) plotting utilities by adding a new function to visualize model dynamics with confidence intervals and true data points
New Features:
plot_CRM_with_intervalsthat supports visualization of model trajectories with 95% credible intervals and optional true data overlayEnhancements:
Chores: