-
Notifications
You must be signed in to change notification settings - Fork 8
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
Scripts to address datapaper3 revision #56
Draft
ferponcem
wants to merge
7
commits into
master
Choose a base branch
from
scripts_revision_datapaper3
base: master
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.
Draft
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8691a7e
script to show the corr using a mask
ferponcem 80477c5
motion gistogram script
ferponcem 7cd9699
FD script
ferponcem 6c807f6
create just one plot with an axis limit
ferponcem 24d22d8
plot with subplots
ferponcem c72281f
remove sub-04 and last frames
ferponcem 73902e7
ENH: update plot
alpinho 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
80 changes: 80 additions & 0 deletions
80
papers_scripts/scidata2023/from_revision/framewise_displacement.py
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,80 @@ | ||
# %% | ||
"""Framewise-displacement (FD) calculation. | ||
from: https://gist.github.com/JulianKlug/68ca5379935e0eedb9bdeed5ab03cf3a | ||
""" | ||
import os | ||
import numpy as np | ||
import pandas as pd | ||
import seaborn as sns | ||
import matplotlib.pyplot as plt | ||
from ibc_public.utils_data import (data_parser, get_subject_session, | ||
DERIVATIVES, CONDITIONS) | ||
# %% | ||
# ############################ FUNCTIONS ##################################### | ||
def framewise_displacement(motion_params: np.ndarray): | ||
"""Calculate framewise Displacement (FD) as per Power et al., 2012""" | ||
motion_diff = np.diff(motion_params, axis=0, prepend=0) | ||
FD = np.sum(np.abs(motion_diff[:, 0:3]) + 50 * np.abs(motion_diff[:, 3:]), | ||
axis=1) | ||
return FD | ||
# %% | ||
def framewise_displacement_from_file(in_file: str): | ||
"""Get the motion params from a motion file.""" | ||
head_motion = np.loadtxt(in_file) | ||
FD = framewise_displacement(head_motion) | ||
return FD | ||
# %% | ||
def FD_subject(sub:str, task:str, db:pd.DataFrame): | ||
"""Get the framewise displacement for a subject in a single array""" | ||
db_sub = db[(db['subject'] == sub) & (db['task'].str.contains(task)) & \ | ||
(db['contrast'] == 'motion')] | ||
if db_sub.empty: | ||
print(f'No motion files found for {sub} {task}') | ||
return None | ||
all_FD = [framewise_displacement_from_file(row['path']) | ||
for _, row in db_sub.iterrows()] | ||
sub_FD = np.concatenate(all_FD) | ||
return sub_FD | ||
|
||
# %% | ||
def plot_subs_FD_distribution(all_subs_FD, PTS, task, out_dir=''): | ||
"""Plot the distribution of framewise displacement for all subjects.""" | ||
plt.figure(figsize=(10, 6)) | ||
sns.boxplot(data=all_subs_FD, orient='v') | ||
plt.ylabel('Framewise Displacement [mm]') | ||
plt.xticks(np.arange(len(PTS)), [f"{sub}" for sub in PTS], rotation=45) | ||
plt.suptitle(f'Framewise Displacement for {task}') | ||
plt.tight_layout() | ||
plt.savefig(os.path.join(out_dir, f'FD_{task}.png'), dpi=300) | ||
|
||
# %% | ||
if __name__ == '__main__': | ||
# ########################### INPUTS ##################################### | ||
cache = mem = '/storage/store3/work/aponcema/IBC_paper3/cache_two' | ||
sub_num = [1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15] | ||
TASKS = ['ClipsTrn','ClipsVal','Raiders','WedgeAnti','WedgeClock', | ||
'ContRing','ExpRing'] | ||
sess_names = ["clips1","clips2","clips3","clips4","raiders1","raiders2"] | ||
sub_path = [os.path.join(DERIVATIVES, 'sub-%02d' % s) for s in sub_num] | ||
PTS = [os.path.basename(full_path) for full_path in sub_path] | ||
# %% | ||
# ############################## RUN ##################################### | ||
db = data_parser(derivatives=DERIVATIVES,subject_list = PTS, | ||
task_list=TASKS,conditions=CONDITIONS,) | ||
# %% | ||
# Make a sub_db with the sessions for each subject | ||
subject_sessions = get_subject_session(sess_names) | ||
sub_sess = { | ||
sub: sorted(set(ses for s, ses in subject_sessions if s == sub)) | ||
for sub in PTS | ||
} | ||
# %% | ||
new_db_ = [db[(db['subject'] == sub) & (db['session'] == ses)] | ||
for sub, ses_list in sub_sess.items() for ses in ses_list] | ||
new_db = pd.concat(new_db_, ignore_index=True) | ||
|
||
grouped_tasks = ["Clips", "Raiders", "Wedge", "Ring"] | ||
for task in grouped_tasks: | ||
all_subs_FD = [FD_subject(sub, task, new_db) for sub in PTS] | ||
plot_subs_FD_distribution(all_subs_FD, PTS, task, out_dir=mem) | ||
# %% |
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.
You should skip 2 lines btw functions. LGTM otherwise.