-
Notifications
You must be signed in to change notification settings - Fork 95
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
Print optimal number of maPCA components and plot optimization curves #839
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f6f5438
Retrieve optimal number of PCA components and plot optimization curves
eurunuela 524903e
Updated to retrieve variance explained criteria from maPCA
eurunuela 2f004e8
Added prints for PCA info and tested the plot gets generated correctly
eurunuela 3f162cc
Trigger tests
eurunuela 0348d7c
Increase minimum maPCA version
eurunuela a199ecf
Merge remote-tracking branch 'upstream/main' into enh/pca_ncomps
eurunuela 5e48e81
Updated expected results for three-echo test
eurunuela 2cd50ec
Generate variance explained plot and save cross component metrics
eurunuela 0efdf7a
Update __init__.py
eurunuela 66cad57
Save maPCA results into dictionary
eurunuela 07b5d2c
Update minimum maPCA version required
eurunuela e361e16
Merge branch 'main' into enh/pca_ncomps
81259cb
Fixes numpy int issues
8c96560
Pins later mapca version
e4cc27d
Fix bad merge
09aa092
Fix explained variance figure
eurunuela 52ffad8
Removed breakpoint
eurunuela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,3 +288,144 @@ def comp_figures(ts, mask, comptable, mmix, io_generator, png_cmap): | |
compplot_name = os.path.join(io_generator.out_dir, "figures", plot_name) | ||
plt.savefig(compplot_name) | ||
plt.close() | ||
|
||
|
||
def pca_results(criteria, n_components, all_varex, io_generator): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function has a large amount of repetition. Could we break it into smaller functions, parametrized perhaps by something like the input data and the label only, and if matplotlib requires this, the figure itself? |
||
""" | ||
Plot the PCA optimization curve for each criteria, and the variance explained curve. | ||
|
||
Parameters | ||
---------- | ||
criteria : array-like | ||
AIC, KIC, and MDL optimization values for increasing number of components. | ||
n_components : array-like | ||
Number of optimal components given by each criteria. | ||
io_generator : object | ||
An object containing all the information needed to generate the output. | ||
""" | ||
|
||
# Plot the PCA optimization curve for each criteria | ||
plt.figure(figsize=(10, 9)) | ||
plt.title("PCA Criteria") | ||
plt.xlabel("PCA components") | ||
plt.ylabel("Arbitrary Units") | ||
|
||
# AIC curve | ||
plt.plot(criteria[0, :], color="tab:blue", label="AIC") | ||
# KIC curve | ||
plt.plot(criteria[1, :], color="tab:orange", label="KIC") | ||
# MDL curve | ||
plt.plot(criteria[2, :], color="tab:green", label="MDL") | ||
|
||
# Vertical line depicting the optimal number of components given by AIC | ||
plt.vlines( | ||
n_components[0], | ||
ymin=np.min(criteria), | ||
ymax=np.max(criteria), | ||
color="tab:blue", | ||
linestyles="dashed", | ||
) | ||
# Vertical line depicting the optimal number of components given by KIC | ||
plt.vlines( | ||
n_components[1], | ||
ymin=np.min(criteria), | ||
ymax=np.max(criteria), | ||
color="tab:orange", | ||
linestyles="dashed", | ||
) | ||
# Vertical line depicting the optimal number of components given by MDL | ||
plt.vlines( | ||
n_components[2], | ||
ymin=np.min(criteria), | ||
ymax=np.max(criteria), | ||
color="tab:green", | ||
linestyles="dashed", | ||
) | ||
# Vertical line depicting the optimal number of components for 90% variance explained | ||
plt.vlines( | ||
n_components[3], | ||
ymin=np.min(criteria), | ||
ymax=np.max(criteria), | ||
color="tab:red", | ||
linestyles="dashed", | ||
label="90% varexp", | ||
) | ||
# Vertical line depicting the optimal number of components for 95% variance explained | ||
plt.vlines( | ||
n_components[4], | ||
ymin=np.min(criteria), | ||
ymax=np.max(criteria), | ||
color="tab:purple", | ||
linestyles="dashed", | ||
label="95% varexp", | ||
) | ||
|
||
plt.legend() | ||
|
||
# Save the plot | ||
plot_name = "pca_criteria.png" | ||
pca_criteria_name = os.path.join(io_generator.out_dir, "figures", plot_name) | ||
plt.savefig(pca_criteria_name) | ||
plt.close() | ||
|
||
# Plot the variance explained curve | ||
plt.figure(figsize=(10, 9)) | ||
plt.title("Variance Explained") | ||
plt.xlabel("PCA components") | ||
plt.ylabel("Variance Explained") | ||
|
||
plt.plot(all_varex, color="black", label="Variance Explained") | ||
|
||
# Vertical line depicting the optimal number of components given by AIC | ||
plt.vlines( | ||
n_components[0], | ||
ymin=0, | ||
ymax=1, | ||
color="tab:blue", | ||
linestyles="dashed", | ||
label="AIC", | ||
) | ||
# Vertical line depicting the optimal number of components given by KIC | ||
plt.vlines( | ||
n_components[1], | ||
ymin=0, | ||
ymax=1, | ||
color="tab:orange", | ||
linestyles="dashed", | ||
label="KIC", | ||
) | ||
# Vertical line depicting the optimal number of components given by MDL | ||
plt.vlines( | ||
n_components[2], | ||
ymin=0, | ||
ymax=1, | ||
color="tab:green", | ||
linestyles="dashed", | ||
label="MDL", | ||
) | ||
# Vertical line depicting the optimal number of components for 90% variance explained | ||
plt.vlines( | ||
n_components[3], | ||
ymin=0, | ||
ymax=1, | ||
color="tab:red", | ||
linestyles="dashed", | ||
label="90% varexp", | ||
) | ||
# Vertical line depicting the optimal number of components for 95% variance explained | ||
plt.vlines( | ||
n_components[4], | ||
ymin=0, | ||
ymax=1, | ||
color="tab:purple", | ||
linestyles="dashed", | ||
label="95% varexp", | ||
) | ||
|
||
plt.legend() | ||
|
||
# Save the plot | ||
plot_name = "pca_variance_explained.png" | ||
pca_variance_explained_name = os.path.join(io_generator.out_dir, "figures", plot_name) | ||
plt.savefig(pca_variance_explained_name) | ||
plt.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like we're accessing private components of the object. Could we either link to some documentation about the returned object or elaborate on what we're accessing here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trailing underscore indicates that the attribute is estimated from the data (via
fit
), rather than that it's private. We adopted this convention from scikit-learn.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that makes sense. Sorry had a momentary flip in my brain, where I put the underscore on the wrong side. I do think it's worth a quick reference to the docs.