# Buggy code
def file_reader(
    filename,
    select_type=None,
    index=0,
    downsample=1,  # noqa
    cutoff_at_kV=None,
    instrument=None,
    lazy=False,
):
    """Reads a bruker bcf file and loads the data into the appropriate class,
    then wraps it into appropriate hyperspy required list of dictionaries
    used by hyperspy.api.load() method.

    Keyword arguments:
    select_type -- One of: spectrum, image. If none specified, then function
      loads everything, else if specified, loads either just sem imagery,
      or just hyper spectral mapping data. (default None)
    index -- index of dataset in bcf v2 (delaut 0)
    downsample -- the downsample ratio of hyperspectral array (downsampling
      height and width only), can be integer from 1 to inf, where '1' means
      no downsampling will be applied (default 1).
    cutoff_at_kV -- if set (can be int of float >= 0) can be used either, to
       crop or enlarge energy range at max values. (default None)
    instrument -- str, either 'TEM' or 'SEM'. Default is None.
    """

    # objectified bcf file:
    obj_bcf = BCF_reader(filename, instrument=instrument)
    if select_type == "image":
        return bcf_imagery(obj_bcf)
    elif select_type == "spectrum":
        return bcf_hyperspectra(
            obj_bcf,
            index=index,
            downsample=downsample,
            cutoff_at_kV=cutoff_at_kV,
            lazy=lazy,
        )
    else:
        return bcf_imagery(obj_bcf) + bcf_hyperspectra(
            obj_bcf,
            index=index,
            downsample=downsample,
            cutoff_at_kV=cutoff_at_kV,
            lazy=lazy,
        )


# Traceback (IndexError)
hs.load(filename)
Traceback (most recent call last):

File "<ipython-input-3-ada763a7934e>", line 1, in <module>
hs.load(filename)

File "C:\ProgramData\Anaconda3\lib\site-packages\hyperspy\io.py", line 243, in load
for filename in filenames]

File "C:\ProgramData\Anaconda3\lib\site-packages\hyperspy\io.py", line 243, in <listcomp>
for filename in filenames]

File "C:\ProgramData\Anaconda3\lib\site-packages\hyperspy\io.py", line 286, in load_single_file
**kwds)

File "C:\ProgramData\Anaconda3\lib\site-packages\hyperspy\io.py", line 295, in load_with_reader
**kwds)

File "C:\ProgramData\Anaconda3\lib\site-packages\hyperspy\io_plugins\bcf.py", line 1189, in file_reader
obj_bcf = BCF_reader(filename, instrument=instrument)

File "C:\ProgramData\Anaconda3\lib\site-packages\hyperspy\io_plugins\bcf.py", line 876, in __init__
self.header = HyperHeader(header_byte_str, instrument=instrument)

File "C:\ProgramData\Anaconda3\lib\site-packages\hyperspy\io_plugins\bcf.py", line 585, in __init__
self._set_sum_edx(root)

File "C:\ProgramData\Anaconda3\lib\site-packages\hyperspy\io_plugins\bcf.py", line 735, in _set_sum_edx
str(i))[0].ClassInstance)

IndexError: list index out of range

# Fixed code
def file_reader(
    filename,
    select_type=None,
    index=None,
    downsample=1,  # noqa
    cutoff_at_kV=None,
    instrument=None,
    lazy=False,
):
    """Reads a bruker bcf file and loads the data into the appropriate class,
    then wraps it into appropriate hyperspy required list of dictionaries
    used by hyperspy.api.load() method.

    Keyword arguments:
    select_type -- One of: spectrum, image. If none specified, then function
      loads everything, else if specified, loads either just sem imagery,
      or just hyper spectral mapping data (default None).
    index -- index of dataset in bcf v2 can be None integer and 'all'
      (default None); None will select first available mapping if more than one.
      'all' will return all maps if more than one present;
      integer will return only selected map.
    downsample -- the downsample ratio of hyperspectral array (downsampling
      height and width only), can be integer from 1 to inf, where '1' means
      no downsampling will be applied (default 1).
    cutoff_at_kV -- if set (can be int of float >= 0) can be used either, to
       crop or enlarge energy range at max values. (default None)
    instrument -- str, either 'TEM' or 'SEM'. Default is None.
    """

    # objectified bcf file:
    obj_bcf = BCF_reader(filename, instrument=instrument)
    if select_type == "image":
        return bcf_imagery(obj_bcf)
    elif select_type == "spectrum":
        return bcf_hyperspectra(
            obj_bcf,
            index=index,
            downsample=downsample,
            cutoff_at_kV=cutoff_at_kV,
            lazy=lazy,
        )
    else:
        return bcf_imagery(obj_bcf) + bcf_hyperspectra(
            obj_bcf,
            index=index,
            downsample=downsample,
            cutoff_at_kV=cutoff_at_kV,
            lazy=lazy,
        )