Skip to content

Commit 4d2131f

Browse files
committed
use upper triangle instead of lower in drawmatrix_channels()
This is to fix the issue #173. `GrangerAnalyzer` put its outputs in the upper triangle only, which would be removed by `drawmatrix_channels()` which takes only the lower triangle, creating an array of all nans. This fix changes the `drawmatrix_channels()` to let it take the upper triangle instead, then transpose the matrix before plotting. Another possible fix is to transposes outputs of `GrangerAnalyzer` into the lower triangle. However this would break with the old behavior including failing unit tests (`test_granger.py`). Another reason to change the `drawmatrix_channels()` instead is that other similar functions, including `CorrelationAnalyzer.xcorr`, `CoherenceAnalyzer.coherence` all computes the upper triangle first, then transpose to fill the lower, e.g. ``` idx = tril_indices(tseries_length, -1) xcorr[idx[0], idx[1], ...] = xcorr[idx[1], idx[0], ...] ``` But granger is a bit different in that it is not symmetrical so we don't just mirror the upper triangle into the low. Another reason to keep the upper triangle in granger is that the indices of pairs argument (`ij`) of the `GrangerAnalyzer.__init__()` translates more naturally to the upper triangle indices, e.g. pair (0, 1) -> (row-0, column-1), (2, 4) -> (row-2, column-4). If feels more intuitive (to me at least) to preserve this, rather than doing a transpose here. This also passes the `test_granger.py` unit test. Related changes needed: in `doc/examples/multi_taper_coh.py`, I have to change the nested `for` loop so the results are put into the upper triangle. The old `for` loop: ``` for i in range(nseq): for j in range(i): ``` New: ``` for i in range(nseq): for j in range(i, nseq): ``` This is also consistent with `CoherenceAnalyzer` etc.. I've tested all scripts in the `doc/examples` folder that involve the `drawmatrix_channels()` function. All figures look good.
1 parent 5495fc2 commit 4d2131f

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

doc/examples/multi_taper_coh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
"""
153153

154154
for i in range(nseq):
155-
for j in range(i):
155+
for j in range(i, nseq):
156156

157157
"""
158158

nitime/viz.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import matplotlib.axis as ax
3131
ax.munits = mpl_units
3232

33-
from nitime.utils import triu_indices
33+
from nitime.utils import tril_indices
3434

3535
#Some visualization functions require networkx. Import that if possible:
3636
try:
@@ -272,10 +272,12 @@ def channel_formatter(x, pos=None):
272272
# data provided
273273
m = in_m.copy()
274274

275-
# Null the upper triangle, so that you don't get the redundant and the
275+
# Null the **lower** triangle, so that you don't get the redundant and the
276276
# diagonal values:
277-
idx_null = triu_indices(m.shape[0])
277+
idx_null = tril_indices(m.shape[0])
278278
m[idx_null] = np.nan
279+
# tranpose the upper triangle to lower
280+
m = m.T
279281

280282
# Extract the minimum and maximum values for scaling of the
281283
# colormap/colorbar:

0 commit comments

Comments
 (0)