Repository: nipy/nitime

Issue:
nitime.analysis.coherence.MTCoherenceAnalyzer causing an error when fed with bandwidth parameter
Greetings,

I have noisy vascular measurements acquired at 30Hz for 89 seconds. I'd like to inspect their multi-taper coherence in the 0.1-2.5Hz range, as done in [1]. In particular, I'd like to use an increased bandwidth of 0.1Hz, trading frequency resolution in favor of reduced variance. I've followed the worked example provided in [2] with the following modifications:


```
multitaper_bandwidth = 0.1 # [Hz]
T = TimeSeries(my_signal, sampling_rate=30.02)
T.metadata['roi'] = vessel_names

C2 = MTCoherenceAnalyzer(T, bandwidth = multitaper_bandwidth)

freq_idx = np.where((C2.frequencies > 0.1) * (C2.frequencies < 2.5))[0]

coh_array[:,:,frequency_band_num] = np.max(C2.coherence[:, :, freq_idx], axis=-1)  # finding the maximal coherence in the frequency domain
```



However, when I inspect freq_idx I see that its bandwidth is always 1/89 = 0.011 Hz, regardless of the value fed to multitaper_bandwidth. Moreover, whenever I feed the bandwidth parameter I get the following TypeError:

```
Traceback (most recent call last):
  File "neurovascular_coherences.py", line 323, in <module>
    coh_array[:,:,frequency_band_num] = np.max(C2.coherence[:, :, freq_idx], axis=-1)  # finding the maximal value on the frequency dimension
  File "/.../envs/nitime/lib/python3.7/site-packages/nitime/descriptors.py", line 140, in __get__
    val = self.getter(obj)
  File "/.../envs/nitime/lib/python3.7/site-packages/nitime/analysis/coherence.py", line 348, in coherence
    (self.weights[i], self.weights[j]),
  File "/.../envs/nitime/lib/python3.7/site-packages/nitime/descriptors.py", line 140, in __get__
    val = self.getter(obj)
  File "/.../envs/nitime/lib/python3.7/site-packages/nitime/analysis/coherence.py", line 318, in weights
    w = np.empty((channel_n, self.df, self._L))
TypeError: 'float' object cannot be interpreted as an integer
```

Could you please suggest what am I doing wrong and how to resolve this error?

Many thanks,
Lior

[1] https://www.pnas.org/content/108/20/8473
[2] http://nipy.org/nitime/examples/multi_taper_coh.html

Discussion:

Patch:
diff --git a/nitime/analysis/coherence.py b/nitime/analysis/coherence.py
index ab577e1..903c1a6 100644
--- a/nitime/analysis/coherence.py
+++ b/nitime/analysis/coherence.py
@@ -304,7 +304,7 @@ class MTCoherenceAnalyzer(BaseAnalyzer):
     @desc.setattr_on_read
     def df(self):
         # The degrees of freedom:
-        return 2 * self.NW - 1
+        return int(2 * self.NW - 1)
 
     @desc.setattr_on_read
     def spectra(self):
@@ -328,9 +328,9 @@ class MTCoherenceAnalyzer(BaseAnalyzer):
         else:
             wshape = [1] * len(self.spectra.shape)
             wshape[0] = channel_n
-            wshape[-2] = int(self.df)
+            wshape[-2] = self.df
             pre_w = np.sqrt(self.eigs) + np.zeros((wshape[0],
-                                                    self.eigs.shape[0]))
+                                                   self.eigs.shape[0]))
 
             w = pre_w.reshape(*wshape)
 


Test patch:
diff --git a/nitime/tests/test_analysis.py b/nitime/tests/test_analysis.py
index 0575da4..3f53337 100644
--- a/nitime/tests/test_analysis.py
+++ b/nitime/tests/test_analysis.py
@@ -314,3 +314,15 @@ def test_MorletWaveletAnalyzer():
     npt.assert_almost_equal(np.sin(HL.phase.data[10:-10]),
                             np.sin(WL.phase.data[10:-10]),
                             decimal=0)
+
+
+def test_MTCoherenceAnalyzer():
+    """
+    Based on gh-188
+    """
+    my_signal = np.random.randn(10, int(np.round(30.02*89)))
+    multitaper_bandwidth = 0.1  # [Hz]
+    TS = ts.TimeSeries(my_signal, sampling_rate=30.02)
+    # T.metadata['roi'] = vessel_names
+    C2 = nta.MTCoherenceAnalyzer(TS, bandwidth=multitaper_bandwidth)
+    npt.assert_equal(C2.coherence.shape, (10, 10, 1337))