# Buggy code
def _unpack_data(self, file, encoding="latin-1"):
    """This needs to be special because it reads until the end of
    file. This causes an error in the series of data"""

    # Size of datapoints in bytes. Always int16 (==2) or 32 (==4)
    Psize = int(self._get_work_dict_key_value("_15_Size_of_Points") / 8)
    dtype = np.int16 if Psize == 2 else np.int32

    if self._get_work_dict_key_value("_01_Signature") != "DSCOMPRESSED":
        # If the points are not compressed we need to read the exact
        # size occupied by datapoints

        # Datapoints in X and Y dimensions
        Npts_tot = self._get_work_dict_key_value("_20_Total_Nb_of_Pts")
        # Datasize in WL
        Wsize = self._get_work_dict_key_value("_14_W_Size")

        # We need to take into account the fact that Wsize is often
        # set to 0 instead of 1 in non-spectral data to compute the
        # space occupied by data in the file
        readsize = Npts_tot * Psize
        if Wsize is not 0:
            readsize *= Wsize
        # if Npts_channel is not 0:
        #    readsize*=Npts_channel

        # Read the exact size of the data
        _points = np.frombuffer(file.read(readsize), dtype=dtype)
        # _points = np.fromstring(file.read(readsize),dtype=dtype)

    else:
        # If the points are compressed do the uncompress magic. There
        # the space occupied by datapoints is self-taken care of.
        # Number of streams
        _directoryCount = self._get_uint32(file)

        # empty lists to store the read sizes
        rawLengthData = []
        zipLengthData = []
        for i in range(_directoryCount):
            # Size of raw and compressed data sizes in each stream
            rawLengthData.append(self._get_uint32(file))
            zipLengthData.append(self._get_uint32(file))

        # We now initialize an empty binary string to store the results
        rawData = b""
        for i in range(_directoryCount):
            # And for each stream we uncompress using zip lib
            # and add it to raw string
            rawData += zlib.decompress(file.read(zipLengthData[i]))

        # Finally numpy converts it to a numeric object
        _points = np.frombuffer(rawData, dtype=dtype)
        # _points = np.fromstring(rawData, dtype=dtype)

    # rescale data
    # We set non measured points to nan according to .sur ways
    nm = []
    if self._get_work_dict_key_value("_11_Special_Points") == 1:
        # has unmeasured points
        nm = _points == self._get_work_dict_key_value("_16_Zmin") - 2

    # We set the point in the numeric scale
    _points = _points.astype(np.float) * self._get_work_dict_key_value(
        "_23_Z_Spacing"
    ) * self._get_work_dict_key_value(
        "_35_Z_Unit_Ratio"
    ) + self._get_work_dict_key_value("_55_Z_Offset")

    _points[nm] = np.nan
    # Return the points, rescaled
    return _points


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-9a0d450cf112> in <module>
1 s = hs.signals.Signal1D(np.random.random((10,100)))
2 s.set_signal_type("EDS_SEM")
----> 3 s.get_lines_intensity("Ca_Kb") # should raise KeyError

~/.local/anaconda3/lib/python3.7/site-packages/hyperspy/_signals/eds.py in get_lines_intensity(self, xray_lines, integration_windows, background_windows, plot_result, only_one, only_lines, **kwargs)
618         """
619
--> 620         xray_lines = self._parse_xray_lines(xray_lines, only_one, only_lines)
621         if hasattr(integration_windows, '__iter__') is False:
622             integration_windows = self.estimate_integration_windows(

~/.local/anaconda3/lib/python3.7/site-packages/hyperspy/_signals/eds.py in _parse_xray_lines(self, xray_lines, only_one, only_lines)
523                                           only_lines=only_lines)
524         xray_lines, xray_not_here = self._get_xray_lines_in_spectral_range(
--> 525             xray_lines)
526         for xray in xray_not_here:
527             warnings.warn("%s is not in the data energy range." % xray +

~/.local/anaconda3/lib/python3.7/site-packages/hyperspy/_signals/eds.py in _get_xray_lines_in_spectral_range(self, xray_lines)
156         xray_lines_not_in_range = []
157         for xray_line in xray_lines:
--> 158             line_energy = self._get_line_energy(xray_line)
159             if low_value < line_energy < high_value:
160                 xray_lines_in_range.append(xray_line)

~/.local/anaconda3/lib/python3.7/site-packages/hyperspy/_signals/eds.py in _get_line_energy(self, Xray_line, FWHM_MnKa)
87                     "`set_signal_type(\"EDS_SEM\")` to convert to one of these"
88                     "signal types.")
---> 89         line_energy = utils_eds._get_energy_xray_line(Xray_line)
90         if units_name == 'eV':
91             line_energy *= 1000

~/.local/anaconda3/lib/python3.7/site-packages/hyperspy/misc/eds/utils.py in _get_energy_xray_line(xray_line)
30     By example, if xray_line = 'Mn_Ka' this function returns 5.8987
31     """
---> 32     element, line = _get_element_and_line(xray_line)
33     return elements_db[element]['Atomic_properties']['Xray_lines'][
34         line]['energy (keV)']

~/.local/anaconda3/lib/python3.7/site-packages/hyperspy/misc/eds/utils.py in _get_element_and_line(xray_line)
20     lim = xray_line.find('_')
21     if lim == -1:
---> 22         raise ValueError("Invalid xray-line: %" % xray_line)
23     return xray_line[:lim], xray_line[lim + 1:]
24

ValueError: incomplete format

# Fixed code
def _unpack_data(self, file, encoding="latin-1"):
    """This needs to be special because it reads until the end of
    file. This causes an error in the series of data"""

    # Size of datapoints in bytes. Always int16 (==2) or 32 (==4)
    Psize = int(self._get_work_dict_key_value("_15_Size_of_Points") / 8)
    dtype = np.int16 if Psize == 2 else np.int32

    if self._get_work_dict_key_value("_01_Signature") != "DSCOMPRESSED":
        # If the points are not compressed we need to read the exact
        # size occupied by datapoints

        # Datapoints in X and Y dimensions
        Npts_tot = self._get_work_dict_key_value("_20_Total_Nb_of_Pts")
        # Datasize in WL
        Wsize = self._get_work_dict_key_value("_14_W_Size")

        # We need to take into account the fact that Wsize is often
        # set to 0 instead of 1 in non-spectral data to compute the
        # space occupied by data in the file
        readsize = Npts_tot * Psize
        if Wsize != 0:
            readsize *= Wsize
        # if Npts_channel is not 0:
        #    readsize*=Npts_channel

        # Read the exact size of the data
        _points = np.frombuffer(file.read(readsize), dtype=dtype)
        # _points = np.fromstring(file.read(readsize),dtype=dtype)

    else:
        # If the points are compressed do the uncompress magic. There
        # the space occupied by datapoints is self-taken care of.
        # Number of streams
        _directoryCount = self._get_uint32(file)

        # empty lists to store the read sizes
        rawLengthData = []
        zipLengthData = []
        for i in range(_directoryCount):
            # Size of raw and compressed data sizes in each stream
            rawLengthData.append(self._get_uint32(file))
            zipLengthData.append(self._get_uint32(file))

        # We now initialize an empty binary string to store the results
        rawData = b""
        for i in range(_directoryCount):
            # And for each stream we uncompress using zip lib
            # and add it to raw string
            rawData += zlib.decompress(file.read(zipLengthData[i]))

        # Finally numpy converts it to a numeric object
        _points = np.frombuffer(rawData, dtype=dtype)
        # _points = np.fromstring(rawData, dtype=dtype)

    # rescale data
    # We set non measured points to nan according to .sur ways
    nm = []
    if self._get_work_dict_key_value("_11_Special_Points") == 1:
        # has unmeasured points
        nm = _points == self._get_work_dict_key_value("_16_Zmin") - 2

    # We set the point in the numeric scale
    _points = _points.astype(np.float) * self._get_work_dict_key_value(
        "_23_Z_Spacing"
    ) * self._get_work_dict_key_value(
        "_35_Z_Unit_Ratio"
    ) + self._get_work_dict_key_value("_55_Z_Offset")

    _points[nm] = np.nan
    # Return the points, rescaled
    return _points