-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
ENH: Simplify code of classification #28
Open
oesteban
wants to merge
7
commits into
ME-ICA:main
Choose a base branch
from
oesteban:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e38c5c8
ENH: Simplify code of classification
oesteban 1282268
Merge remote-tracking branch 'upstream/main' into pr/28
eurunuela 9edc544
Moved prediction to classification file.
eurunuela 1307fbf
Updates to classification.py and io.py
eurunuela c15a9bd
Updated call to denoising function
eurunuela 1fce555
Removed breakpoint
eurunuela 4b3141d
Update aroma/aroma.py
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
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,110 @@ | ||||||||||
# CHANGES | ||||||||||
# ------- | ||||||||||
# Log of changes as mandated by the original Apache 2.0 License of ICA-AROMA | ||||||||||
# | ||||||||||
# * Drop ``runICA`` and ``register2MNI`` functions | ||||||||||
# * Base ``classifier`` on Pandas, and revise signature (``predict(X)``) | ||||||||||
# to make it more similar to scikit learn | ||||||||||
# * Return classification labels directly on ``predict`` | ||||||||||
# | ||||||||||
"""Classification functions for ICA-AROMA.""" | ||||||||||
import logging | ||||||||||
|
||||||||||
import numpy as np | ||||||||||
|
||||||||||
LGR = logging.getLogger(__name__) | ||||||||||
|
||||||||||
# Define criteria needed for classification (thresholds and | ||||||||||
# hyperplane-parameters) | ||||||||||
THR_CSF = 0.10 | ||||||||||
THR_HFC = 0.35 | ||||||||||
HYPERPLANE = np.array([-19.9751070082159, 9.95127547670627, 24.8333160239175]) | ||||||||||
|
||||||||||
|
||||||||||
def hfc_criteria(x, thr_hfc=THR_HFC): | ||||||||||
""" | ||||||||||
Compute the HFC criteria for classification. | ||||||||||
|
||||||||||
Parameters | ||||||||||
---------- | ||||||||||
x : numpy.ndarray | ||||||||||
Projection of HFC feature scores to new 1D space. | ||||||||||
|
||||||||||
Returns | ||||||||||
------- | ||||||||||
numpy.ndarray | ||||||||||
Classification (``True`` if the component is a motion one). | ||||||||||
""" | ||||||||||
return x > thr_hfc | ||||||||||
|
||||||||||
|
||||||||||
def csf_criteria(x, thr_csf=THR_CSF): | ||||||||||
""" | ||||||||||
Compute the CSF criteria for classification. | ||||||||||
|
||||||||||
Parameters | ||||||||||
---------- | ||||||||||
x : numpy.ndarray | ||||||||||
Projection of CSF-fraction feature scores to new 1D space. | ||||||||||
|
||||||||||
Returns | ||||||||||
------- | ||||||||||
numpy.ndarray | ||||||||||
Classification (``True`` if the component is a CSF one). | ||||||||||
""" | ||||||||||
return x > thr_csf | ||||||||||
|
||||||||||
|
||||||||||
def hplane_criteria(x, hplane=HYPERPLANE): | ||||||||||
""" | ||||||||||
Compute the hyperplane criteria for classification. | ||||||||||
|
||||||||||
Parameters | ||||||||||
---------- | ||||||||||
x : numpy.ndarray | ||||||||||
Projection of edge & max_RP_corr feature scores to new 1D space. | ||||||||||
|
||||||||||
Returns | ||||||||||
------- | ||||||||||
:obj:`pandas.DataFrame` | ||||||||||
Features table with additional column "classification". | ||||||||||
Comment on lines
+69
to
+70
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.
Suggested change
|
||||||||||
|
||||||||||
""" | ||||||||||
return (hplane[0] + np.dot(x, hplane[1:])) > 0 | ||||||||||
|
||||||||||
|
||||||||||
def predict(X, thr_csf=THR_CSF, thr_hfc=THR_HFC, hplane=HYPERPLANE, metric_metadata=None): | ||||||||||
""" | ||||||||||
Classify components as motion or non-motion based on four features. | ||||||||||
|
||||||||||
The four features used for classification are: maximum RP correlation, | ||||||||||
high-frequency content, edge-fraction, and CSF-fraction. | ||||||||||
|
||||||||||
Parameters | ||||||||||
---------- | ||||||||||
X : :obj:`pandas.DataFrame` | ||||||||||
Features table (C x 4), must contain the following columns: | ||||||||||
"edge_fract", "csf_fract", "max_RP_corr", and "HFC". | ||||||||||
|
||||||||||
Returns | ||||||||||
------- | ||||||||||
y : array_like | ||||||||||
Classification (``True`` if the component is a motion one). | ||||||||||
""" | ||||||||||
# Project edge & max_RP_corr feature scores to new 1D space | ||||||||||
proj = hplane_criteria(X[["max_RP_corr", "edge_fract"]].values, hplane=hplane) | ||||||||||
|
||||||||||
# Compute the CSF criteria | ||||||||||
csf = csf_criteria(X["csf_fract"].values, thr_csf=thr_csf) | ||||||||||
|
||||||||||
# Compute the HFC criteria | ||||||||||
hfc = hfc_criteria(X["HFC"].values, thr_hfc=thr_hfc) | ||||||||||
|
||||||||||
# Combine the criteria | ||||||||||
classification = csf | hfc | proj | ||||||||||
|
||||||||||
# Turn classification into a list of string labels with rejected if true, accepted if false | ||||||||||
classification = ["rejected" if c else "accepted" for c in classification] | ||||||||||
|
||||||||||
# Classify the ICs | ||||||||||
return classification |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Input/output functions for the Aroma project.""" | ||
import json | ||
import os.path as op | ||
|
||
|
||
def write_metrics(features_df, out_dir, metric_metadata=None): | ||
"""Write out feature/classification information and metadata. | ||
Parameters | ||
---------- | ||
features_df : (C x 5) :obj:`pandas.DataFrame` | ||
DataFrame with metric values and classifications. | ||
Must have the following columns: "edge_fract", "csf_fract", "max_RP_corr", "HFC", and | ||
"classification". | ||
out_dir : :obj:`str` | ||
Output directory. | ||
metric_metadata : :obj:`dict` or None, optional | ||
Metric metadata in a dictionary. | ||
Returns | ||
------- | ||
motion_ICs : array_like | ||
Array containing the indices of the components identified as motion components. | ||
Output | ||
------ | ||
AROMAnoiseICs.csv : A text file containing the indices of the | ||
components identified as motion components | ||
desc-AROMA_metrics.tsv | ||
desc-AROMA_metrics.json | ||
""" | ||
# Put the indices of motion-classified ICs in a text file (starting with 1) | ||
motion_ICs = features_df["classification"][features_df["classification"] == "rejected"].index | ||
motion_ICs = motion_ICs.values | ||
|
||
with open(op.join(out_dir, "AROMAnoiseICs.csv"), "w") as fo: | ||
out_str = ",".join(motion_ICs.astype(str)) | ||
fo.write(out_str) | ||
|
||
# Create a summary overview of the classification | ||
out_file = op.join(out_dir, "desc-AROMA_metrics.tsv") | ||
features_df.to_csv(out_file, sep="\t", index_label="IC") | ||
|
||
if isinstance(metric_metadata, dict): | ||
with open(op.join(out_dir, "desc-AROMA_metrics.json"), "w") as fo: | ||
json.dump(metric_metadata, fo, sort_keys=True, indent=4) | ||
|
||
return motion_ICs |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""Tests for aroma.classification.""" | ||
import pandas as pd | ||
from aroma import classification | ||
|
||
|
||
def test_classification(classification_overview): | ||
"""Test aroma.utils.classification and ensure classifications come out the same.""" | ||
clf_overview_df = pd.read_table(classification_overview) | ||
test_df = clf_overview_df[["edge_fract", "csf_fract", "max_RP_corr", "HFC"]] | ||
test_classifications = classification.predict(test_df, metric_metadata={}) | ||
true_classifications = clf_overview_df["classification"].tolist() | ||
assert true_classifications == test_classifications |
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
Oops, something went wrong.
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.
Since it's just one criterion.