-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
49 lines (40 loc) · 1.56 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Utilities used by other modules in the cis-processing workflow."""
import os
import os.path as op
import subprocess
import pandas as pd
def run(command, env=None):
"""Run a given command with certain environment variables set."""
merged_env = os.environ
if env:
merged_env.update(env)
process = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True,
env=merged_env)
while True:
line = process.stdout.readline()
line = str(line, 'utf-8')[:-1]
print(line)
if line == '' and process.poll() is not None:
break
if process.returncode != 0:
raise Exception("Non zero return code: {0}\n"
"{1}\n\n{2}".format(process.returncode, command,
process.stdout.read()))
def clean_csv(in_file):
"""Convert NaNs to zeroes.
Convert NaNs in a csv to zeros. Necessary for using the MRIQC
classifier on datasets which may have skull-stripped data.
This problem is discussed here (with no solution):
https://github.com/poldracklab/mriqc/issues/546
Writes out a file with the same name as the input file, but with the suffix
"_cleaned".
"""
fname = op.basename(in_file)
d = op.dirname(in_file)
fname, ext = op.splitext(fname)
out_fname = fname + '_cleaned'
out_file = op.join(d, out_fname + ext)
df = pd.read_csv(in_file)
df = df.fillna(0)
df.to_csv(out_file, line_terminator='\n', index=False)