# Buggy code
def load_1D_EDS_SEM_spectrum():
    """
    Load an EDS-SEM spectrum

    Notes
    -----
    - Sample: EDS-TM002 provided by BAM (www.webshop.bam.de)
    - SEM Microscope: Nvision40 Carl Zeiss
    - EDS Detector: X-max 80 from Oxford Instrument
    """
    from hyperspy.io import load

    file_path = os.sep.join(
        [
            os.path.dirname(__file__),
            "eds",
            "example_signals",
            "1D_EDS_SEM_Spectrum.hdf5",
        ]
    )
    return load(file_path)


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
c:\users\thomasaar\documents\github\hyperspy\hyperspy\io_plugins\hspy.py in overwrite_dataset(group, data, key, signal_axes, chunks, **kwds)
570             # contains the chunk shape guessed by h5py
--> 571             dset = group.require_dataset(key, **these_kwds)
572             got_data = True

~\.conda\envs\py5\lib\site-packages\h5py\_hl\group.py in require_dataset(self, name, shape, dtype, exact, **kwds)
190             if not name in self:
--> 191                 return self.create_dataset(name, *(shape, dtype), **kwds)
192

~\.conda\envs\py5\lib\site-packages\h5py\_hl\group.py in create_dataset(self, name, shape, dtype, data, **kwds)
135         with phil:
--> 136             dsid = dataset.make_new_dset(self, shape, dtype, data, **kwds)
137             dset = dataset.Dataset(dsid)

~\.conda\envs\py5\lib\site-packages\h5py\_hl\dataset.py in make_new_dset(parent, shape, dtype, data, chunks, compression, shuffle, fletcher32, maxshape, compression_opts, fillvalue, scaleoffset, track_times, external, track_order, dcpl)
117             dtype = numpy.dtype(dtype)
--> 118         tid = h5t.py_create(dtype, logical=1)
119

h5py\h5t.pyx in h5py.h5t.py_create()

h5py\h5t.pyx in h5py.h5t.py_create()

h5py\h5t.pyx in h5py.h5t.py_create()

TypeError: No conversion path for dtype: dtype('<U2')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-7-56c3607e5bfd> in <module>
1 s = hs.datasets.example_signals.EDS_TEM_Spectrum()
----> 2 s.save('eds.hspy')

c:\users\thomasaar\documents\github\hyperspy\hyperspy\signal.py in save(self, filename, overwrite, extension, **kwds)
2229             basename, ext = os.path.splitext(filename)
2230             filename = basename + '.' + extension
-> 2231         io.save(filename, self, overwrite=overwrite, **kwds)
2232
2233     def _replot(self):

c:\users\thomasaar\documents\github\hyperspy\hyperspy\io.py in save(filename, signal, overwrite, **kwds)
602                              "False.")
603         if write:
--> 604             writer.file_writer(filename, signal, **kwds)
605             _logger.info('The %s file was created' % filename)
606             folder, filename = os.path.split(os.path.abspath(filename))

c:\users\thomasaar\documents\github\hyperspy\hyperspy\io_plugins\hspy.py in file_writer(filename, signal, *args, **kwds)
753             smd.record_by = ""
754         try:
--> 755             write_signal(signal, expg, **kwds)
756         except BaseException:
757             raise

c:\users\thomasaar\documents\github\hyperspy\hyperspy\io_plugins\hspy.py in write_signal(signal, group, **kwds)
708         metadata_dict["_internal_parameters"] = \
709             metadata_dict.pop("_HyperSpy")
--> 710     dict2hdfgroup(metadata_dict, mapped_par, **kwds)
711     original_par = group.create_group(original_metadata)
712     dict2hdfgroup(signal.original_metadata.as_dictionary(), original_par,

c:\users\thomasaar\documents\github\hyperspy\hyperspy\io_plugins\hspy.py in dict2hdfgroup(dictionary, group, **kwds)
441     for key, value in dictionary.items():
442         if isinstance(value, dict):
--> 443             dict2hdfgroup(value, group.create_group(key),
444                           **kwds)
445         elif isinstance(value, DictionaryTreeBrowser):

c:\users\thomasaar\documents\github\hyperspy\hyperspy\io_plugins\hspy.py in dict2hdfgroup(dictionary, group, **kwds)
451             write_signal(value, group.require_group(kn))
452         elif isinstance(value, (np.ndarray, h5py.Dataset, da.Array)):
--> 453             overwrite_dataset(group, value, key, **kwds)
454         elif value is None:
455             group.attrs[key] = '_None_'

c:\users\thomasaar\documents\github\hyperspy\hyperspy\io_plugins\hspy.py in overwrite_dataset(group, data, key, signal_axes, chunks, **kwds)
574             # if the shape or dtype/etc do not match,
575             # we delete the old one and create new in the next loop run
--> 576             del group[key]
577     if dset == data:
578         # just a reference to already created thing

h5py\_objects.pyx in h5py._objects.with_phil.wrapper()

h5py\_objects.pyx in h5py._objects.with_phil.wrapper()

~\.conda\envs\py5\lib\site-packages\h5py\_hl\group.py in __delitem__(self, name)
397     def __delitem__(self, name):
398         """ Delete (unlink) an item from this group. """
--> 399         self.id.unlink(self._e(name))
400
401     @with_phil

h5py\_objects.pyx in h5py._objects.with_phil.wrapper()

h5py\_objects.pyx in h5py._objects.with_phil.wrapper()

h5py\h5g.pyx in h5py.h5g.GroupID.unlink()

KeyError: "Couldn't delete link (callback link pointer is NULL (specified link may be '.' or not exist))"```

# Fixed code
def load_1D_EDS_SEM_spectrum():
    """
    Load an EDS-SEM spectrum

    Notes
    -----
    - Sample: EDS-TM002 provided by BAM (www.webshop.bam.de)
    - SEM Microscope: Nvision40 Carl Zeiss
    - EDS Detector: X-max 80 from Oxford Instrument
    """
    from hyperspy.io import load

    file_path = os.sep.join(
        [
            os.path.dirname(__file__),
            "eds",
            "example_signals",
            "1D_EDS_SEM_Spectrum.hspy",
        ]
    )
    return load(file_path)