Repository: gammapy/gammapy

Issue:
Issues in energy dispersion apply and to_sherpa
@joleroi The current [EnergyDispersion.apply](https://github.com/gammapy/gammapy/blob/bc822b86/gammapy/irf/energy_dispersion.py#L63) has the following issues:
* arguments `e_reco=None, e_true=None` are never used.
* The ValueError message accesses the undefined name `true_nodes`, i.e. will error out with a NameError.

Also, can you please resolve the TODO in the current [EnergyDispersion.to_sherpa](https://github.com/gammapy/gammapy/blob/bc822b86/gammapy/irf/energy_dispersion.py#L487)?
The code under the if can't work with Python 3 (because xrange and izip don't exist there) and is also broken (NameError for `numpy`) and thus not covered by a test.



Discussion:

Resolving patch:
diff --git a/gammapy/irf/energy_dispersion.py b/gammapy/irf/energy_dispersion.py
--- a/gammapy/irf/energy_dispersion.py
+++ b/gammapy/irf/energy_dispersion.py
@@ -60,7 +60,7 @@ def __str__(self):
 
         return ss
 
-    def apply(self, data, e_reco=None, e_true=None):
+    def apply(self, data):
         """Apply energy dispersion.
 
         Computes the matrix product of ``data``
@@ -79,7 +79,7 @@ def apply(self, data, e_reco=None, e_true=None):
         """
         if len(data) != self.e_true.nbins:
             raise ValueError("Input size {} does not match true energy axis {}".format(
-                len(data), len(true_nodes)))
+                len(data), self.e_true.nbins))
         return np.dot(data, self.data.data)
 
     @property
@@ -484,22 +484,9 @@ def to_sherpa(self, name):
         matrix = np.concatenate([row for row in matrix])
         matrix = matrix.astype(SherpaFloat)
 
-        # TODO: Not sure if we need this if statement
-        if f_chan.ndim > 1 and n_chan.ndim > 1:
-            f_chan = []
-            n_chan = []
-            for grp, fch, nch, in izip(n_grp, f_chan, n_chan):
-                for i in xrange(grp):
-                    f_chan.append(fch[i])
-                    n_chan.append(nch[i])
-
-            f_chan = numpy.asarray(f_chan, SherpaUInt)
-            n_chan = numpy.asarray(n_chan, SherpaUInt)
-        else:
-            if len(n_grp) == len(f_chan):
-                good = n_grp > 0
-                f_chan = f_chan[good]
-                n_chan = n_chan[good]
+        good = n_grp > 0
+        f_chan = f_chan[good]
+        n_chan = n_chan[good]
 
         return DataRMF(
             name=name,


Test patch:
diff --git a/gammapy/irf/tests/test_energy_dispersion.py b/gammapy/irf/tests/test_energy_dispersion.py
--- a/gammapy/irf/tests/test_energy_dispersion.py
+++ b/gammapy/irf/tests/test_energy_dispersion.py
@@ -1,6 +1,7 @@
 # Licensed under a 3-clause BSD style license - see LICENSE.rst
 from __future__ import absolute_import, division, print_function, unicode_literals
 import numpy as np
+import pytest
 from numpy.testing import assert_allclose, assert_equal
 from astropy.coordinates import Angle
 import astropy.units as u
@@ -47,7 +48,10 @@ def test_apply(self):
         actual = self.edisp.apply(counts)
         assert_allclose(actual[0], 3.9877484855864265)
 
-        actual = self.edisp.apply(counts, e_true=self.e_true)
+        counts = np.arange(len(self.e_true) - 4)
+        with pytest.raises(ValueError) as exc:
+            self.edisp.apply(counts)
+        assert str(len(counts)) in str(exc.value)
         assert_allclose(actual[0], 3.9877484855864265)
 
     @requires_dependency('matplotlib')