/
analysis.py
304 lines (244 loc) · 9.36 KB
/
analysis.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Interfaces to functionality from nitime for time-series analysis of fmri data
- nitime.analysis.CoherenceAnalyzer: Coherence/y
- nitime.fmri.io:
- nitime.viz.drawmatrix_channels
"""
import numpy as np
import tempfile
from ...utils.misc import package_check
from ...utils.filemanip import fname_presuffix
from .base import NitimeBaseInterface
from ..base import (
TraitedSpec,
File,
Undefined,
traits,
isdefined,
BaseInterfaceInputSpec,
)
have_nitime = True
try:
package_check("nitime")
except ImportError:
have_nitime = False
class CoherenceAnalyzerInputSpec(BaseInterfaceInputSpec):
# Input either csv file, or time-series object and use _xor_inputs to
# discriminate
_xor_inputs = ("in_file", "in_TS")
in_file = File(
desc=(
"csv file with ROIs on the columns and "
"time-points on the rows. ROI names at the top row"
),
exists=True,
requires=("TR",),
)
# If you gave just a file name, you need to specify the sampling_rate:
TR = traits.Float(
desc=("The TR used to collect the data in your csv file <in_file>")
)
in_TS = traits.Any(desc="a nitime TimeSeries object")
NFFT = traits.Range(
low=32,
value=64,
usedefault=True,
desc=(
"This is the size of the window used for "
"the spectral estimation. Use values between "
"32 and the number of samples in your time-series."
"(Defaults to 64.)"
),
)
n_overlap = traits.Range(
low=0,
value=0,
usedefault=True,
desc=(
"The number of samples which overlap"
"between subsequent windows.(Defaults to 0)"
),
)
frequency_range = traits.List(
value=[0.02, 0.15],
usedefault=True,
minlen=2,
maxlen=2,
desc=(
"The range of frequencies over"
"which the analysis will average."
"[low,high] (Default [0.02,0.15]"
),
)
output_csv_file = File(
desc="File to write outputs (coherence,time-delay) with file-names: "
"``file_name_{coherence,timedelay}``"
)
output_figure_file = File(
desc="""\
File to write output figures (coherence,time-delay) with file-names:
``file_name_{coherence,timedelay}``. Possible formats: .png,.svg,.pdf,.jpg,..."""
)
figure_type = traits.Enum(
"matrix",
"network",
usedefault=True,
desc=(
"The type of plot to generate, where "
"'matrix' denotes a matrix image and"
"'network' denotes a graph representation."
" Default: 'matrix'"
),
)
class CoherenceAnalyzerOutputSpec(TraitedSpec):
coherence_array = traits.Array(
desc=("The pairwise coherence values between the ROIs")
)
timedelay_array = traits.Array(
desc=("The pairwise time delays between the ROIs (in seconds)")
)
coherence_csv = File(desc=("A csv file containing the pairwise coherence values"))
timedelay_csv = File(desc=("A csv file containing the pairwise time delay values"))
coherence_fig = File(desc=("Figure representing coherence values"))
timedelay_fig = File(desc=("Figure representing coherence values"))
class CoherenceAnalyzer(NitimeBaseInterface):
"""Wraps nitime.analysis.CoherenceAnalyzer: Coherence/y"""
input_spec = CoherenceAnalyzerInputSpec
output_spec = CoherenceAnalyzerOutputSpec
def _read_csv(self):
"""
Read from csv in_file and return an array and ROI names
The input file should have a first row containing the names of the
ROIs (strings)
the rest of the data will be read in and transposed so that the rows
(TRs) will becomes the second (and last) dimension of the array
"""
# Check that input conforms to expectations:
first_row = open(self.inputs.in_file).readline()
if not first_row[1].isalpha():
raise ValueError(
"First row of in_file should contain ROI names as strings of characters"
)
roi_names = (
open(self.inputs.in_file).readline().replace('"', "").strip("\n").split(",")
)
# Transpose, so that the time is the last dimension:
data = np.loadtxt(self.inputs.in_file, skiprows=1, delimiter=",").T
return data, roi_names
def _csv2ts(self):
"""Read data from the in_file and generate a nitime TimeSeries object"""
from nitime.timeseries import TimeSeries
data, roi_names = self._read_csv()
TS = TimeSeries(data=data, sampling_interval=self.inputs.TR, time_unit="s")
TS.metadata = dict(ROIs=roi_names)
return TS
# Rewrite _run_interface, but not run
def _run_interface(self, runtime):
import nitime.analysis as nta
lb, ub = self.inputs.frequency_range
if self.inputs.in_TS is Undefined:
# get TS form csv and inputs.TR
TS = self._csv2ts()
else:
# get TS from inputs.in_TS
TS = self.inputs.in_TS
# deal with creating or storing ROI names:
if "ROIs" not in TS.metadata:
self.ROIs = ["roi_%d" % x for x, _ in enumerate(TS.data)]
else:
self.ROIs = TS.metadata["ROIs"]
A = nta.CoherenceAnalyzer(
TS,
method=dict(
this_method="welch",
NFFT=self.inputs.NFFT,
n_overlap=self.inputs.n_overlap,
),
)
freq_idx = np.where(
(A.frequencies > self.inputs.frequency_range[0])
* (A.frequencies < self.inputs.frequency_range[1])
)[0]
# Get the coherence matrix from the analyzer, averaging on the last
# (frequency) dimension: (roi X roi array)
self.coherence = np.mean(A.coherence[:, :, freq_idx], -1)
# Get the time delay from analyzer, (roi X roi array)
self.delay = np.mean(A.delay[:, :, freq_idx], -1)
return runtime
# Rewrite _list_outputs (look at BET)
def _list_outputs(self):
outputs = self.output_spec().get()
# if isdefined(self.inputs.output_csv_file):
# write to a csv file and assign a value to self.coherence_file (a
# file name + path)
# Always defined (the arrays):
outputs["coherence_array"] = self.coherence
outputs["timedelay_array"] = self.delay
# Conditional
if isdefined(self.inputs.output_csv_file) and hasattr(self, "coherence"):
# we need to make a function that we call here that writes the
# coherence values to this file "coherence_csv" and makes the
# time_delay csv file??
self._make_output_files()
outputs["coherence_csv"] = fname_presuffix(
self.inputs.output_csv_file, suffix="_coherence"
)
outputs["timedelay_csv"] = fname_presuffix(
self.inputs.output_csv_file, suffix="_delay"
)
if isdefined(self.inputs.output_figure_file) and hasattr(self, "coherence"):
self._make_output_figures()
outputs["coherence_fig"] = fname_presuffix(
self.inputs.output_figure_file, suffix="_coherence"
)
outputs["timedelay_fig"] = fname_presuffix(
self.inputs.output_figure_file, suffix="_delay"
)
return outputs
def _make_output_files(self):
"""
Generate the output csv files.
"""
for this in zip([self.coherence, self.delay], ["coherence", "delay"]):
tmp_f = tempfile.mkstemp()[1]
np.savetxt(tmp_f, this[0], delimiter=",")
fid = open(
fname_presuffix(self.inputs.output_csv_file, suffix="_%s" % this[1]),
"w+",
)
# this writes ROIs as header line
fid.write("," + ",".join(self.ROIs) + "\n")
# this writes ROI and data to a line
for r, line in zip(self.ROIs, open(tmp_f)):
fid.write(f"{r},{line}")
fid.close()
def _make_output_figures(self):
"""
Generate the desired figure and save the files according to
self.inputs.output_figure_file
"""
import nitime.viz as viz
if self.inputs.figure_type == "matrix":
fig_coh = viz.drawmatrix_channels(
self.coherence, channel_names=self.ROIs, color_anchor=0
)
fig_coh.savefig(
fname_presuffix(self.inputs.output_figure_file, suffix="_coherence")
)
fig_dt = viz.drawmatrix_channels(
self.delay, channel_names=self.ROIs, color_anchor=0
)
fig_dt.savefig(
fname_presuffix(self.inputs.output_figure_file, suffix="_delay")
)
else:
fig_coh = viz.drawgraph_channels(self.coherence, channel_names=self.ROIs)
fig_coh.savefig(
fname_presuffix(self.inputs.output_figure_file, suffix="_coherence")
)
fig_dt = viz.drawgraph_channels(self.delay, channel_names=self.ROIs)
fig_dt.savefig(
fname_presuffix(self.inputs.output_figure_file, suffix="_delay")
)