Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update correlation functions to get subsections of matrix #103

Merged
merged 10 commits into from
Dec 19, 2024

Conversation

strixy16
Copy link
Collaborator

@strixy16 strixy16 commented Dec 19, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced correlation analysis methods for improved clarity and functionality.
    • Introduced a new method to retrieve both self and cross correlations in a single call.
  • Bug Fixes

    • Improved error handling for cases with no matching features, providing specific error messages.
  • Documentation

    • Updated public API to reflect new method names and signatures.

Copy link
Contributor

coderabbitai bot commented Dec 19, 2024

Walkthrough

The pull request introduces significant modifications to the correlation analysis module in the src/readii/analyze/correlation.py file. The changes focus on renaming methods, updating method signatures, and improving error handling for correlation retrieval. The core functionality now emphasizes feature type names over numeric feature counts, with a new method getSelfAndCrossCorrelations consolidating previous separate methods. These updates aim to enhance the clarity and flexibility of correlation matrix operations.

Changes

File Changes
src/readii/analyze/correlation.py - Renamed getVerticalSelfCorrelations to getSelfCorrelations
- Renamed getHorizontalSelfCorrelations to getCrossCorrelations
- Added getSelfAndCrossCorrelations method
- Updated parameter handling to use feature type names
src/readii/analyze/__init__.py - Updated import and __all__ declarations
- Renamed exported function names to match new method signatures

Sequence Diagram

sequenceDiagram
    participant User
    participant CorrelationModule
    participant CorrelationMatrix

    User->>CorrelationModule: getSelfAndCrossCorrelations(correlation_matrix)
    CorrelationModule->>CorrelationMatrix: Filter self correlations
    CorrelationModule->>CorrelationMatrix: Filter cross correlations
    CorrelationModule-->>User: Return combined correlation results
Loading

Poem

🐰 Correlation Rabbit's Rhyme 🧮

In matrices where numbers dance and sway,
Renamed methods find a clearer way,
Self and cross, now elegantly combined,
A rabbit's logic, precisely refined!
Hop through data with newfound grace! 📊


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 278ebaa and 01ea94a.

📒 Files selected for processing (1)
  • src/readii/analyze/__init__.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/readii/analyze/init.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
src/readii/analyze/correlation.py (3)

82-84: Confirm naming consistency in docstring.
The docstring references "vertical self correlations" even though the method has been renamed to getSelfCorrelations, which works for any selected suffix. Updating the docstring will help maintain consistency.


101-102: Fix grammar in error message.
The error message "No features with found with ..." is missing a word and may be confusing.

Apply this diff to correct the grammar:

-    msg = f"No features with found with {feature_type_name} suffix in the correlation matrix."
+    msg = f"No features found with {feature_type_name} suffix in the correlation matrix."

132-133: Fix grammar in error message.
Similar to the earlier comment, the message "No features with found with ..." is missing a word.

Apply this diff:

-    msg = f"No features with found with {vertical_feature_name} and {horizontal_feature_name} suffix in the correlation matrix."
+    msg = f"No features found with {vertical_feature_name} and {horizontal_feature_name} suffix in the correlation matrix."
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 206ffd6 and 8b34c25.

📒 Files selected for processing (1)
  • src/readii/analyze/correlation.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
src/readii/analyze/correlation.py

173-173: Undefined name getCrossCorrelationMatrix

(F821)

🔇 Additional comments (1)
src/readii/analyze/correlation.py (1)

110-113: Assess filter approach for cross correlations.
Using filter(like=vertical_feature_name) and filter(like=horizontal_feature_name) may inadvertently match columns if their names simply contain the substring rather than specifically ending with the name. Consider being more strict if you anticipate columns that contain these substrings but are not actually the intended features.

Comment on lines 144 to 179
def getSelfAndCrossCorrelations(correlation_matrix:pd.DataFrame,
vertical_feature_name:str = '_vertical',
horizontal_feature_name:str = '_horizontal') -> pd.DataFrame:
"""Get the vertical and horizontal self correlations and cross correlations from a correlation matrix.

Parameters
----------
correlation_matrix : pd.DataFrame
Dataframe containing the correlation matrix to get the cross correlation matrix subsection from.
num_vertical_features : int
Number of vertical features in the correlation matrix.

Dataframe containing the correlation matrix to get the self and cross correlations from.
vertical_feature_name : str
Name of the vertical feature type to get self correlations for. Must be the suffix of some feature names in the correlation matrix.
horizontal_feature_name : str
Name of the horizontal feature type to get self correlations for. Must be the suffix of some feature names in the correlation matrix.

Returns
-------
pd.DataFrame
vertical_correlations : pd.DataFrame
Dataframe containing the vertical self correlations from the correlation matrix.
horizontal_correlations : pd.DataFrame
Dataframe containing the horizontal self correlations from the correlation matrix.
cross_correlations : pd.DataFrame
Dataframe containing the cross correlations from the correlation matrix.
"""
try:
validateDataframeSubsetSelection(correlation_matrix, num_vertical_features, num_vertical_features)
except ValueError as e:
msg = "Number of vertical features provided is greater than the number of rows or columns in the correlation matrix."
vertical_correlations, horizontal_correlations = getSelfCorrelations(correlation_matrix, vertical_feature_name), getSelfCorrelations(correlation_matrix, horizontal_feature_name)

except Exception as e:
msg = f"Error getting self correlations from correlation matrix: {e}"
logger.exception(msg)
raise e

return correlation_matrix.iloc[0:num_vertical_features, num_vertical_features:] No newline at end of file
try:
cross_correlations = getCrossCorrelationMatrix(correlation_matrix, vertical_feature_name, horizontal_feature_name)
except Exception as e:
msg = f"Error getting cross correlations from correlation matrix: {e}"
logger.exception(msg)
raise e

return vertical_correlations, horizontal_correlations, cross_correlations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Undefined function call: getCrossCorrelationMatrix.
The function getCrossCorrelationMatrix is no longer defined, which triggers an error and contradicts the new consolidated structure. Replace this call with getCrossCorrelations to align with the new architecture.

Apply this diff to fix the undefined call:

-        cross_correlations = getCrossCorrelationMatrix(correlation_matrix, vertical_feature_name, horizontal_feature_name)
+        cross_correlations = getCrossCorrelations(correlation_matrix, vertical_feature_name, horizontal_feature_name)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def getSelfAndCrossCorrelations(correlation_matrix:pd.DataFrame,
vertical_feature_name:str = '_vertical',
horizontal_feature_name:str = '_horizontal') -> pd.DataFrame:
"""Get the vertical and horizontal self correlations and cross correlations from a correlation matrix.
Parameters
----------
correlation_matrix : pd.DataFrame
Dataframe containing the correlation matrix to get the cross correlation matrix subsection from.
num_vertical_features : int
Number of vertical features in the correlation matrix.
Dataframe containing the correlation matrix to get the self and cross correlations from.
vertical_feature_name : str
Name of the vertical feature type to get self correlations for. Must be the suffix of some feature names in the correlation matrix.
horizontal_feature_name : str
Name of the horizontal feature type to get self correlations for. Must be the suffix of some feature names in the correlation matrix.
Returns
-------
pd.DataFrame
vertical_correlations : pd.DataFrame
Dataframe containing the vertical self correlations from the correlation matrix.
horizontal_correlations : pd.DataFrame
Dataframe containing the horizontal self correlations from the correlation matrix.
cross_correlations : pd.DataFrame
Dataframe containing the cross correlations from the correlation matrix.
"""
try:
validateDataframeSubsetSelection(correlation_matrix, num_vertical_features, num_vertical_features)
except ValueError as e:
msg = "Number of vertical features provided is greater than the number of rows or columns in the correlation matrix."
vertical_correlations, horizontal_correlations = getSelfCorrelations(correlation_matrix, vertical_feature_name), getSelfCorrelations(correlation_matrix, horizontal_feature_name)
except Exception as e:
msg = f"Error getting self correlations from correlation matrix: {e}"
logger.exception(msg)
raise e
return correlation_matrix.iloc[0:num_vertical_features, num_vertical_features:]
\ No newline at end of file
try:
cross_correlations = getCrossCorrelationMatrix(correlation_matrix, vertical_feature_name, horizontal_feature_name)
except Exception as e:
msg = f"Error getting cross correlations from correlation matrix: {e}"
logger.exception(msg)
raise e
return vertical_correlations, horizontal_correlations, cross_correlations
def getSelfAndCrossCorrelations(correlation_matrix:pd.DataFrame,
vertical_feature_name:str = '_vertical',
horizontal_feature_name:str = '_horizontal') -> pd.DataFrame:
"""Get the vertical and horizontal self correlations and cross correlations from a correlation matrix.
Parameters
----------
correlation_matrix : pd.DataFrame
Dataframe containing the correlation matrix to get the self and cross correlations from.
vertical_feature_name : str
Name of the vertical feature type to get self correlations for. Must be the suffix of some feature names in the correlation matrix.
horizontal_feature_name : str
Name of the horizontal feature type to get self correlations for. Must be the suffix of some feature names in the correlation matrix.
Returns
-------
vertical_correlations : pd.DataFrame
Dataframe containing the vertical self correlations from the correlation matrix.
horizontal_correlations : pd.DataFrame
Dataframe containing the horizontal self correlations from the correlation matrix.
cross_correlations : pd.DataFrame
Dataframe containing the cross correlations from the correlation matrix.
"""
try:
vertical_correlations, horizontal_correlations = getSelfCorrelations(correlation_matrix, vertical_feature_name), getSelfCorrelations(correlation_matrix, horizontal_feature_name)
except Exception as e:
msg = f"Error getting self correlations from correlation matrix: {e}"
logger.exception(msg)
raise e
try:
cross_correlations = getCrossCorrelations(correlation_matrix, vertical_feature_name, horizontal_feature_name)
except Exception as e:
msg = f"Error getting cross correlations from correlation matrix: {e}"
logger.exception(msg)
raise e
return vertical_correlations, horizontal_correlations, cross_correlations
🧰 Tools
🪛 Ruff (0.8.2)

173-173: Undefined name getCrossCorrelationMatrix

(F821)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/readii/analyze/correlation.py (2)

82-106: Fix minor grammar in error message.
The message "No features with found with {feature_type_name}" is slightly off.

Here’s a quick fix:

-if self_correlations.empty:
-    msg = f"No features with found with {feature_type_name} suffix in the correlation matrix."
+if self_correlations.empty:
+    msg = f"No features found with {feature_type_name} suffix in the correlation matrix."

110-137: Correct grammar in error message.
Similarly, correct the message "No features with found with {vertical_feature_name} and {horizontal_feature_name}" to be more precise.

-if cross_correlations.empty:
-    msg = f"No features with found with {vertical_feature_name} and {horizontal_feature_name} suffix in the correlation matrix."
+if cross_correlations.empty:
+    msg = f"No features found with {vertical_feature_name} and {horizontal_feature_name} suffix in the correlation matrix."
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b34c25 and 278ebaa.

⛔ Files ignored due to path filters (1)
  • pixi.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • src/readii/analyze/__init__.py (1 hunks)
  • src/readii/analyze/correlation.py (1 hunks)
  • src/readii/analyze/plot_correlation.py (2 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
src/readii/analyze/plot_correlation.py

11-11: readii.io.writers.base_writer.BaseWriter imported but unused

Remove unused import: readii.io.writers.base_writer.BaseWriter

(F401)

🔇 Additional comments (3)
src/readii/analyze/plot_correlation.py (1)

191-191: Return tuple matches function docstring.
This return statement aligns with the documented triple return (figure, bin values, and bin edges).

src/readii/analyze/__init__.py (1)

3-6: Reorganized imports and all look consistent.
These changes properly align the public API with the newly renamed methods.

Also applies to: 12-14

src/readii/analyze/correlation.py (1)

141-179: Overall logic for consolidating self and cross correlations looks good.
The new structure is more modular and clear, allowing users to retrieve multiple subsets of the correlation matrix in a single function call.

@@ -7,6 +7,9 @@
from matplotlib.figure import Figure
from scipy.linalg import issymmetric

from readii.analyze.correlation import getSelfCorrelations
from readii.io.writers.base_writer import BaseWriter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove unused import.
The static analysis tool indicates that BaseWriter is imported but never used.

You can remove it as shown below:

-from readii.io.writers.base_writer import BaseWriter
🧰 Tools
🪛 Ruff (0.8.2)

11-11: readii.io.writers.base_writer.BaseWriter imported but unused

Remove unused import: readii.io.writers.base_writer.BaseWriter

(F401)

Comment on lines 193 to 231

def plotSelfCorrHeatmap(correlation_matrix:pd.DataFrame,
feature_type_name:str,
correlation_method:str = "pearson",
cmap='nipy_spectral',
save_path:Optional[str] = None,) -> None:

self_corr = getSelfCorrelations(correlation_matrix, feature_type_name)

self_corr_heatmap = plotCorrelationHeatmap(self_corr,
diagonal=True,
cmap=cmap,
xlabel=feature_type_name,
ylabel=feature_type_name,
title=f"{correlation_method.capitalize()} Self Correlations", subtitle=f"{feature_type_name}")


if save_path is not None:
heatmap_writer = PlotWriter(root_directory = save_path / "heatmap",
filename_format = "{ColorMap}/" + "{FeatureType}_{CorrelationType}_self_correlation_heatmap.png",
overwrite = False,
create_dirs = True
)

self_corr_save_path = heatmap_writer.resolve_path(FeatureType = feature_type_name,
CorrelationType = correlation_method,
ColorMap = cmap)
if self_corr_save_path.exists():
logger.warning(f"Correlation heatmap already exists at {self_corr_save_path}.")

else:
logger.debug("Saving correlation heatmaps.")

self_corr_save_path = heatmap_writer.save(self_corr_heatmap, FeatureType = feature_type_name, CorrelationType = correlation_method, ColorMap = cmap)

return self_corr_heatmap, self_corr_save_path

else:
return self_corr_heatmap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Update the function’s declared return type for clarity.
Currently, the docstring and type hint (→ None) do not match the actual return values. This inconsistency might confuse users since the function returns a tuple (figure, path) when save_path is provided or just one object otherwise.

Consider unifying the return type so it's consistent, for example:

-def plotSelfCorrHeatmap(correlation_matrix:pd.DataFrame,
-                        feature_type_name:str,
-                        correlation_method:str = "pearson",
-                        cmap='nipy_spectral',
-                        save_path:Optional[str] = None,) -> None:
+def plotSelfCorrHeatmap(correlation_matrix: pd.DataFrame,
+                        feature_type_name: str,
+                        correlation_method: str = "pearson",
+                        cmap: str = 'nipy_spectral',
+                        save_path: Optional[str] = None,) -> Union[Figure, Tuple[Figure, Path]]:

And update the docstring to reflect the actual return(s).

Committable suggestion skipped: line range outside the PR's diff.

Copy link

codecov bot commented Dec 19, 2024

Codecov Report

Attention: Patch coverage is 13.04348% with 20 lines in your changes missing coverage. Please review.

Project coverage is 41.91%. Comparing base (6272a03) to head (01ea94a).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/readii/analyze/correlation.py 13.04% 20 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #103      +/-   ##
==========================================
- Coverage   43.76%   41.91%   -1.85%     
==========================================
  Files          33       33              
  Lines        1451     1453       +2     
==========================================
- Hits          635      609      -26     
- Misses        816      844      +28     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@strixy16 strixy16 merged commit 343d876 into main Dec 19, 2024
18 checks passed
@strixy16 strixy16 deleted the katys/add-correlation-helpers branch December 19, 2024 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant