# Buggy code
def _setup_vfs(self):
    """Setup the virtual file system tree represented as python dictionary
    with values populated with SFSTreeItem instances

    See also:
    SFSTreeItem
    """
    with open(self.filename, "rb") as fn:
        # file tree do not exceed one chunk in bcf:
        fn.seek(self.chunksize * self.tree_address + 0x138)
        raw_tree = fn.read(0x200 * self.n_tree_items)
        temp_item_list = [
            SFSTreeItem(raw_tree[i * 0x200 : (i + 1) * 0x200], self)
            for i in range(self.n_tree_items)
        ]
        # temp list with parents of items
        paths = [[h.parent] for h in temp_item_list]
    # checking the compression header which can be different per file:
    self._check_the_compresion(temp_item_list)
    if self.compression == "zlib":
        for c in temp_item_list:
            if not c.is_dir:
                c.setup_compression_metadata()
    # convert the items to virtual file system tree
    dict_tree = self._flat_items_to_dict(paths, temp_item_list)
    # and finaly set the Virtual file system:
    self.vfs = dict_tree["root"]


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-e233613e1d5d> in <module>()
----> 1 bk=hs.load('eds.bcf')

~/Documents/hyperspy/hyperspy/io.py in load(filenames, signal_type, stack, stack_axis, new_axis_name, lazy, **kwds)
    241             objects = [load_single_file(filename, lazy=lazy,
    242                                         **kwds)
--> 243                        for filename in filenames]
    244 
    245         if len(objects) == 1:

~/Documents/hyperspy/hyperspy/io.py in <listcomp>(.0)
    241             objects = [load_single_file(filename, lazy=lazy,
    242                                         **kwds)
--> 243                        for filename in filenames]
    244 
    245         if len(objects) == 1:

~/Documents/hyperspy/hyperspy/io.py in load_single_file(filename, signal_type, **kwds)
    284                                 reader=reader,
    285                                 signal_type=signal_type,
--> 286                                 **kwds)
    287 
    288 

~/Documents/hyperspy/hyperspy/io.py in load_with_reader(filename, reader, signal_type, **kwds)
    293     lazy = kwds.get('lazy', False)
    294     file_data_list = reader.file_reader(filename,
--> 295                                         **kwds)
    296     objects = []
    297 

~/Documents/hyperspy/hyperspy/io_plugins/bcf.py in file_reader(filename, select_type, index, downsample, cutoff_at_kV, instrument, lazy)
   1213 
   1214     # objectified bcf file:
-> 1215     obj_bcf = BCF_reader(filename, instrument=instrument)
   1216     if select_type == 'image':
   1217         return bcf_imagery(obj_bcf)

~/Documents/hyperspy/hyperspy/io_plugins/bcf.py in __init__(self, filename, instrument)
    880         header_byte_str = header_file.get_as_BytesIO_string().getvalue()
    881         hd_bt_str = fix_dec_patterns.sub(b'\\1.\\2', header_byte_str)
--> 882         self.header = HyperHeader(header_byte_str, self.available_indexes, instrument=instrument)
    883         self.hypermap = {}
    884 

~/Documents/hyperspy/hyperspy/io_plugins/bcf.py in __init__(self, xml_str, indexes, instrument)
    569         self._set_microscope(root)
    570         self._get_mode(instrument)
--> 571         self._set_images(root)
    572         self.elements = {}
    573         self._set_elements(root)

~/Documents/hyperspy/hyperspy/io_plugins/bcf.py in _set_images(self, root)
    692             if not(n.get('Name')):
    693                 image_node = n
--> 694         self.image = self._parse_image(image_node)
    695         if self.version == 2:
    696             overview_node = root.findall(

~/Documents/hyperspy/hyperspy/io_plugins/bcf.py in _parse_image(self, xml_node, overview)
    671             if any(array1):
    672                 item = self.gen_hspy_item_dict_basic()
--> 673                 data = array1.reshape((image.height, image.width))
    674                 detector_name = str(img.find('./Description').text)
    675                 item['data'] = data

ValueError: cannot reshape array of size 114688 into shape (448,512)


# Fixed code
def _setup_vfs(self):
    """Setup the virtual file system tree represented as python dictionary
    with values populated with SFSTreeItem instances

    See also:
    SFSTreeItem
    """
    with open(self.filename, "rb") as fn:
        # check if file tree do not exceed one chunk:
        n_file_tree_chunks = ceil((self.n_tree_items * 0x200) / (self.chunksize - 0x20))
        if n_file_tree_chunks == 1:
            # file tree do not exceed one chunk in bcf:
            fn.seek(self.chunksize * self.tree_address + 0x138)
            raw_tree = fn.read(0x200 * self.n_tree_items)
        else:
            temp_str = io.BytesIO()
            tree_address = self.tree_address
            tree_items_in_chunk = (self.chunksize - 0x20) // 0x200
            for i in range(n_file_tree_chunks):
                # jump to tree/list address:
                fn.seek(self.chunksize * tree_address + 0x118)
                # next tree/list address:
                tree_address = strct_unp("<I", fn.read(4))[0]
                fn.seek(28, 1)
                temp_str.write(fn.read(tree_items_in_chunk * 0x200))
            temp_str.seek(0)
            raw_tree = temp_str.read(self.n_tree_items * 0x200)
            temp_str.close()
        temp_item_list = [
            SFSTreeItem(raw_tree[i * 0x200 : (i + 1) * 0x200], self)
            for i in range(self.n_tree_items)
        ]
        # temp list with parents of items
        paths = [[h.parent] for h in temp_item_list]
    # checking the compression header which can be different per file:
    self._check_the_compresion(temp_item_list)
    if self.compression == "zlib":
        for c in temp_item_list:
            if not c.is_dir:
                c.setup_compression_metadata()
    # convert the items to virtual file system tree
    dict_tree = self._flat_items_to_dict(paths, temp_item_list)
    # and finaly set the Virtual file system:
    self.vfs = dict_tree["root"]