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

[ENH.STY] add heartrate processing #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.ipynb_checkpoints
.DS_Store
.vscode
.pytest_cache
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"args": ["data", "output/testOut"],
"console": "integratedTerminal"
}
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"python.pythonPath": "/opt/miniconda-latest/envs/physio/bin/python",
"python.linting.flake8Enabled": true,
"gitlens.showWhatsNewAfterUpgrades": false,
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.linting.enabled": true,
"python.testing.pytestArgs": [],
"python.linting.pylintEnabled": false,
"python.dataScience.debugJustMyCode": false
}
16 changes: 15 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated by Neurodocker version 0.5.0
# Timestamp: 2019-09-13 17:44:50 UTC
# Timestamp: 2019-09-13 19:33:04 UTC
#
# Thank you for using Neurodocker. If you discover any issues
# or ways to improve this software, please submit an issue or
Expand Down Expand Up @@ -52,6 +52,8 @@ USER neuro

WORKDIR /home/neuro

ENV SHELL="/bin/bash"

RUN mkdir /home/neuro/physioAnalysis

COPY [".", "/home/neuro/physioAnalysis"]
Expand All @@ -72,6 +74,8 @@ RUN export PATH="/opt/miniconda-latest/bin:$PATH" \
&& conda env create -q --name physio --file /home/neuro/physioAnalysis/environment.yml \
&& rm -rf ~/.cache/pip/*

RUN conda init

USER root

RUN curl -o /tmp/code-server.tar.gz -SL https://github.com/cdr/code-server/releases/download/2.1472-vsc1.38.1/code-server2.1472-vsc1.38.1-linux-x86_64.tar.gz
Expand Down Expand Up @@ -107,6 +111,12 @@ RUN echo '{ \
\n "/home/neuro" \
\n ], \
\n [ \
\n "env", \
\n { \
\n "SHELL": "/bin/bash" \
\n } \
\n ], \
\n [ \
\n "run", \
\n "mkdir /home/neuro/physioAnalysis" \
\n ], \
Expand All @@ -125,6 +135,10 @@ RUN echo '{ \
\n } \
\n ], \
\n [ \
\n "run", \
\n "conda init" \
\n ], \
\n [ \
\n "user", \
\n "root" \
\n ], \
Expand Down
118 changes: 69 additions & 49 deletions code/physio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import matplotlib
matplotlib.use('Agg')


Copy link
Member

Choose a reason for hiding this comment

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

why is there a blank line here?

# functions to analyze gsr data
def analyze_gsr(acqs, txts, outdir):
"""
Expand All @@ -25,9 +26,9 @@ def analyze_gsr(acqs, txts, outdir):
if acq_txt_dict is None:
acq_nonmatches.append(acq_nonmatch)
continue

fname_dict = gen_filenames(acq_txt_dict)

if fname_dict is None:
warnings.warn("Skipping acq File")
continue
Expand All @@ -36,18 +37,18 @@ def analyze_gsr(acqs, txts, outdir):
os.path.isfile(fname_dict['data']) and
os.path.isfile(fname_dict['smry'])
):
with open(fname_dict['smry']) as json_file:
with open(fname_dict['smry']) as json_file:
sub_dict = json.load(json_file)
else:
acq_txt_dict_proc = filter_gsr(acq_txt_dict)
sub_dict = write_results(acq_txt_dict_proc, fname_dict)

group_msr.append(sub_dict)

group_df = pd.DataFrame(group_msr)
group_df.to_csv(group_tsv, sep="\t")
txt_nonmatches = txts

return acq_txt_dict_proc, acq_nonmatches, txt_nonmatches, group_df


Expand All @@ -65,38 +66,38 @@ def match_acq_txt(acq, txts):
# there should be enough rows in the dataframes
if acq_df.shape[0] < min_len:
return None, acq, txts

# big loop to see which txt file goes with the acknowledge file
for txt in txts:
# only look at txt files in the same directory as acknowledge file
if os.path.dirname(acq) != os.path.dirname(txt):
continue
# attempt 1 to read the txt file
# assumes no header information and no time column
txt_df = pd.read_csv(txt,
header=None,
index_col=False,
names=acq_df.columns,
sep="\t")
txt_df = pd.read_csv(txt, header=None,
index_col=False,
names=acq_df.columns,
sep="\t")

# the file may have a header
if np.any(txt_df.iloc[0,:].isnull()):
if np.any(txt_df.iloc[0, :].isnull()):
# assume the txt file has a header
try:
txt_df = pd.read_csv(txt,
header=14,
index_col=0,
delim_whitespace=True,
names=acq_df.columns)
header=14,
index_col=0,
delim_whitespace=True,
names=acq_df.columns)
except:
# assume the acknowledge file does not have the "Scanner TTl column"
# assume the acknowledge file does
# not have the "Scanner TTl column"
columns = list(acq_df.columns)
columns.insert(-1, "Scanner TTL")
txt_df = pd.read_csv(txt,
header=14,
index_col=0,
delim_whitespace=True,
names=columns)
header=14,
index_col=0,
delim_whitespace=True,
names=columns)
# assume in the two above cases,
# there is a time column being treated as the index
txt_df.reset_index(drop=True, inplace=True)
Expand All @@ -111,7 +112,7 @@ def match_acq_txt(acq, txts):
acq_txt_dict[acq] = {'txt': txt, 'df': acq_df}
txts.remove(txt)
return acq_txt_dict, None, txts

# nothing matches
return None, acq, txts

Expand All @@ -121,21 +122,28 @@ def filter_gsr(acq_txt_dict):
Filter/Preprocess the GSR data
"""
key = list(acq_txt_dict.keys())[0]

acq_df = acq_txt_dict[key]['df']
# where the Scanner Trigger is 0 initiates a slice acquition in the scanner
# this may be false in some scenerios (e.g. when scan trigger is always 0)
start = acq_df["Scanner Trigger"].where(acq_df["Scanner Trigger"]==0.0).first_valid_index()
end = acq_df["Scanner Trigger"].where(acq_df["Scanner Trigger"]==0.0).last_valid_index()
start = acq_df["Scanner Trigger"].where(
acq_df["Scanner Trigger"] == 0.0).first_valid_index()
end = acq_df["Scanner Trigger"].where(
acq_df["Scanner Trigger"] == 0.0).last_valid_index()
if start is None or end is None:
warnings.warn("Scan Trigger Not Recognized, keeping all rows")
acq_cut_df = acq_df
else:
acq_cut_df = acq_df.iloc[(acq_df.index >= start) & (acq_df.index <= end)]
acq_cut_df = acq_df.iloc[(acq_df.index >= start) &
(acq_df.index <= end)]

# process the GSR data
res = nk.bio_eda.eda_process(acq_df['GSR100C'], filter_type='butter',
band="lowpass", order=1, frequency=1,
res = nk.bio_ecg.ecg_process(acq_cut_df['PPG100C'],
rsp=None,
filter_type='FIR',
band="bandpass",
order=1,
frequency=[3, 45],
sampling_rate=200)

acq_txt_dict[key]['df'] = res['df']
Expand All @@ -162,7 +170,7 @@ def gen_filenames(acq_txt_dict):
for acq, tgt_dct in acq_txt_dict.items():
atrain_mch = atrain_ptrn.match(tgt_dct['txt'])
extend_mch = extend_ptrn.match(tgt_dct['txt'])

if atrain_mch:
fdict = atrain_mch.groupdict()
# make task lowercase
Expand All @@ -176,30 +184,41 @@ def gen_filenames(acq_txt_dict):
if fdict["run_id"]:
fdict["run_id"] = int(fdict["run_id"].lstrip("run-"))
else:
warnings.warn("FileName did not match either atrain or extend pattern")
warnings.warn("FileName did not match either "
"atrain or extend pattern")
return None

# strip the keys from the labels
fdict["sub_id"] = fdict["sub_id"].lstrip("sub-")
fdict["ses_id"] = fdict["ses_id"].lstrip("ses-")
fdict["task_id"] = fdict["task_id"].lstrip("task-")

# template output file changes depending if run is a key or not
if not fdict["run_id"]:
tmplt = os.path.join(outdir,
"sub-{sub_id}",
"ses-{ses_id}",
"sub-{sub_id}_ses-{ses_id}_task-{task_id}_{typ}.{ext}")
tmplt = os.path.join(
outdir,
"sub-{sub_id}",
"ses-{ses_id}",
"sub-{sub_id}_ses-{ses_id}_task-{task_id}_{typ}.{ext}"
)

elif fdict["run_id"]:
tmplt = os.path.join(outdir,
"sub-{sub_id}",
"ses-{ses_id}",
"sub-{sub_id}_ses-{ses_id}_task-{task_id}_run-{run_id}_{typ}.{ext}")
tmplt = os.path.join(
outdir,
"sub-{sub_id}",
"ses-{ses_id}",
("sub-{sub_id}_ses-{ses_id}_"
"task-{task_id}_run-{run_id}_{typ}.{ext}")
)

fig_file = tmplt.format(**fdict, typ="qa", ext="svg")
data_file = tmplt.format(**fdict, typ="physio", ext="tsv")
sum_file = tmplt.format(**fdict, typ="summary", ext="json")

return {'fig': fig_file, 'data': data_file, 'smry': sum_file, 'fdict': fdict}

return {'fig': fig_file,
'data': data_file,
'smry': sum_file,
'fdict': fdict}


def write_results(acq_txt_dict, filenames):
Expand All @@ -209,7 +228,7 @@ def write_results(acq_txt_dict, filenames):
# make directory to place results
os.makedirs(os.path.dirname(filenames['data']), exist_ok=True)
key = list(acq_txt_dict.keys())[0]

fdict = filenames['fdict']
out_df = acq_txt_dict[key]['df']
out_df.to_csv(filenames['data'], sep="\t", index=False)
Expand All @@ -229,12 +248,12 @@ def write_results(acq_txt_dict, filenames):
"EDA_std": out_df["EDA_Filtered"].std()
}

with open(filenames['smry'], 'w') as outfile:
with open(filenames['smry'], 'w') as outfile:
json.dump(sub_dict, outfile)

return sub_dict


if __name__ == '__main__':
from argparse import ArgumentParser
from glob import glob
Expand All @@ -245,9 +264,10 @@ def write_results(acq_txt_dict, filenames):
help='path to the top level of the data directory')
parser.add_argument('outdir', action='store',
help='path to output directory')
parser.add_argument('--participant_label', '--participant-label', action='store',
help='a single participant identifier ')

parser.add_argument('--participant_label', '--participant-label',
action='store',
help='a single participant identifier')

opts = parser.parse_args()

participant_label = "sub-*"
Expand Down
2 changes: 2 additions & 0 deletions generate_physio_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ generate_docker() {
--install net-tools git \
--user=neuro \
--workdir="/home/neuro" \
--env "SHELL=/bin/bash" \
--run 'mkdir /home/neuro/physioAnalysis' \
--copy . /home/neuro/physioAnalysis \
--miniconda create_env='physio' \
yaml_file='/home/neuro/physioAnalysis/environment.yml' \
--run "conda init" \
--user=root \
--run 'curl -o /tmp/code-server.tar.gz -SL https://github.com/cdr/code-server/releases/download/2.1472-vsc1.38.1/code-server2.1472-vsc1.38.1-linux-x86_64.tar.gz' \
--run 'mkdir -p /opt/codeserver && tar -xvf /tmp/code-server.tar.gz -C /opt/codeserver --strip-components=1' \
Expand Down