# Buggy code
def _export_loadings(
    self,
    loadings,
    folder=None,
    comp_ids=None,
    multiple_files=None,
    loading_prefix=None,
    loading_format=None,
    save_figures_format="png",
    comp_label=None,
    cmap=plt.cm.gray,
    save_figures=False,
    same_window=False,
    calibrate=True,
    no_nans=True,
    per_row=3,
):
    from hyperspy._signals.image import Image
    from hyperspy._signals.spectrum import Spectrum

    if multiple_files is None:
        multiple_files = preferences.MachineLearning.multiple_files

    if loading_format is None:
        loading_format = preferences.MachineLearning.export_loadings_default_file_format

    if comp_ids is None:
        comp_ids = range(loadings.shape[0])
    elif not hasattr(comp_ids, "__iter__"):
        comp_ids = range(comp_ids)
    mask = np.zeros(loadings.shape[0], dtype=np.bool)
    for idx in comp_ids:
        mask[idx] = 1
    loadings = loadings[mask]

    if save_figures is True:
        plt.ioff()
        sc_plots = self._plot_loadings(
            loadings,
            comp_ids=comp_ids,
            calibrate=calibrate,
            same_window=same_window,
            comp_label=comp_label,
            cmap=cmap,
            no_nans=no_nans,
            per_row=per_row,
        )
        for idx in range(len(comp_ids)):
            filename = "%s_%02i.%s" % (
                loading_prefix,
                comp_ids[idx],
                save_figures_format,
            )
            if folder is not None:
                filename = os.path.join(folder, filename)
            ensure_directory(filename)
            _args = {"dpi": 600, "format": save_figures_format}
            sc_plots[idx].savefig(filename, **_args)
        plt.ion()
    elif multiple_files is False:
        if self.axes_manager.navigation_dimension == 2:
            axes_dicts = []
            axes = self.axes_manager.navigation_axes[::-1]
            shape = (axes[1].size, axes[0].size)
            loading_data = loadings.reshape((-1, shape[0], shape[1]))
            axes_dicts.append(axes[0].get_axis_dictionary())
            axes_dicts[0]["index_in_array"] = 1
            axes_dicts.append(axes[1].get_axis_dictionary())
            axes_dicts[1]["index_in_array"] = 2
            axes_dicts.append(
                {
                    "name": "loading_index",
                    "scale": 1.0,
                    "offset": 0.0,
                    "size": int(loadings.shape[0]),
                    "units": "factor",
                    "index_in_array": 0,
                }
            )
            s = Image(
                loading_data,
                axes=axes_dicts,
                metadata={
                    "General": {
                        "title": "%s from %s"
                        % (loading_prefix, self.metadata.General.title),
                    }
                },
            )
        elif self.axes_manager.navigation_dimension == 1:
            cal_axis = self.axes_manager.navigation_axes[0].get_axis_dictionary()
            cal_axis["index_in_array"] = 1
            axes = [
                {
                    "name": "loading_index",
                    "scale": 1.0,
                    "offset": 0.0,
                    "size": int(loadings.shape[0]),
                    "units": "comp_id",
                    "index_in_array": 0,
                },
                cal_axis,
            ]
            s = Image(
                loadings,
                axes=axes,
                metadata={
                    "General": {
                        "title": "%s from %s"
                        % (loading_prefix, self.metadata.General.title),
                    }
                },
            )
        filename = "%ss.%s" % (loading_prefix, loading_format)
        if folder is not None:
            filename = os.path.join(folder, filename)
        s.save(filename)
    else:  # Separate files
        if self.axes_manager.navigation_dimension == 1:
            axis_dict = self.axes_manager.navigation_axes[0].get_axis_dictionary()
            axis_dict["index_in_array"] = 0
            for dim, index in zip(comp_ids, range(len(comp_ids))):
                s = Spectrum(
                    loadings[index],
                    axes=[
                        axis_dict,
                    ],
                )
                filename = "%s-%i.%s" % (loading_prefix, dim, loading_format)
                if folder is not None:
                    filename = os.path.join(folder, filename)
                s.save(filename)
        elif self.axes_manager.navigation_dimension == 2:
            axes_dicts = []
            axes = self.axes_manager.navigation_axes[::-1]
            shape = (axes[0].size, axes[1].size)
            loading_data = loadings.reshape((-1, shape[0], shape[1]))
            axes_dicts.append(axes[0].get_axis_dictionary())
            axes_dicts[0]["index_in_array"] = 0
            axes_dicts.append(axes[1].get_axis_dictionary())
            axes_dicts[1]["index_in_array"] = 1
            for dim, index in zip(comp_ids, range(len(comp_ids))):
                s = Image(
                    loading_data[index, ...],
                    axes=axes_dicts,
                    metadata={
                        "General": {
                            "title": "%s from %s"
                            % (loading_prefix, self.metadata.General.title),
                        }
                    },
                )
                filename = "%s-%i.%s" % (loading_prefix, dim, loading_format)
                if folder is not None:
                    filename = os.path.join(folder, filename)
                s.save(filename)


# Traceback (ImportError)
ImportErrorTraceback (most recent call last)
<ipython-input-8-b1971df6874d> in <module>()
----> 1 sW.export_decomposition_results(factor_format='msa',loading_format='tif')

/home/smc204/anaconda2/envs/hyperspy/lib/python3.5/site-packages/hyperspy/signal.py in export_decomposition_results(self, comp_ids, folder, calibrate, factor_prefix, factor_format, loading_prefix, loading_format, comp_label, cmap, same_window, multiple_files, no_nans, per_row, save_figures, save_figures_format)
951             same_window=same_window,
952             no_nans=no_nans,
--> 953             per_row=per_row)
954
955     def export_bss_results(self,

/home/smc204/anaconda2/envs/hyperspy/lib/python3.5/site-packages/hyperspy/signal.py in _export_loadings(self, loadings, folder, comp_ids, multiple_files, loading_prefix, loading_format, save_figures_format, comp_label, cmap, save_figures, same_window, calibrate, no_nans, per_row)
424                          per_row=3):
425
--> 426         from hyperspy._signals.image import Image
427         from hyperspy._signals.spectrum import Spectrum
428

ImportError: No module named 'hyperspy._signals.image'

# Fixed code
def _export_loadings(
    self,
    loadings,
    folder=None,
    comp_ids=None,
    multiple_files=None,
    loading_prefix=None,
    loading_format=None,
    save_figures_format="png",
    comp_label=None,
    cmap=plt.cm.gray,
    save_figures=False,
    same_window=False,
    calibrate=True,
    no_nans=True,
    per_row=3,
):
    from hyperspy.signals import Image, Spectrum

    if multiple_files is None:
        multiple_files = preferences.MachineLearning.multiple_files

    if loading_format is None:
        loading_format = preferences.MachineLearning.export_loadings_default_file_format

    if comp_ids is None:
        comp_ids = range(loadings.shape[0])
    elif not hasattr(comp_ids, "__iter__"):
        comp_ids = range(comp_ids)
    mask = np.zeros(loadings.shape[0], dtype=np.bool)
    for idx in comp_ids:
        mask[idx] = 1
    loadings = loadings[mask]

    if save_figures is True:
        plt.ioff()
        sc_plots = self._plot_loadings(
            loadings,
            comp_ids=comp_ids,
            calibrate=calibrate,
            same_window=same_window,
            comp_label=comp_label,
            cmap=cmap,
            no_nans=no_nans,
            per_row=per_row,
        )
        for idx in range(len(comp_ids)):
            filename = "%s_%02i.%s" % (
                loading_prefix,
                comp_ids[idx],
                save_figures_format,
            )
            if folder is not None:
                filename = os.path.join(folder, filename)
            ensure_directory(filename)
            _args = {"dpi": 600, "format": save_figures_format}
            sc_plots[idx].savefig(filename, **_args)
        plt.ion()
    elif multiple_files is False:
        if self.axes_manager.navigation_dimension == 2:
            axes_dicts = []
            axes = self.axes_manager.navigation_axes[::-1]
            shape = (axes[1].size, axes[0].size)
            loading_data = loadings.reshape((-1, shape[0], shape[1]))
            axes_dicts.append(axes[0].get_axis_dictionary())
            axes_dicts[0]["index_in_array"] = 1
            axes_dicts.append(axes[1].get_axis_dictionary())
            axes_dicts[1]["index_in_array"] = 2
            axes_dicts.append(
                {
                    "name": "loading_index",
                    "scale": 1.0,
                    "offset": 0.0,
                    "size": int(loadings.shape[0]),
                    "units": "factor",
                    "index_in_array": 0,
                }
            )
            s = Image(
                loading_data,
                axes=axes_dicts,
                metadata={
                    "General": {
                        "title": "%s from %s"
                        % (loading_prefix, self.metadata.General.title),
                    }
                },
            )
        elif self.axes_manager.navigation_dimension == 1:
            cal_axis = self.axes_manager.navigation_axes[0].get_axis_dictionary()
            cal_axis["index_in_array"] = 1
            axes = [
                {
                    "name": "loading_index",
                    "scale": 1.0,
                    "offset": 0.0,
                    "size": int(loadings.shape[0]),
                    "units": "comp_id",
                    "index_in_array": 0,
                },
                cal_axis,
            ]
            s = Image(
                loadings,
                axes=axes,
                metadata={
                    "General": {
                        "title": "%s from %s"
                        % (loading_prefix, self.metadata.General.title),
                    }
                },
            )
        filename = "%ss.%s" % (loading_prefix, loading_format)
        if folder is not None:
            filename = os.path.join(folder, filename)
        s.save(filename)
    else:  # Separate files
        if self.axes_manager.navigation_dimension == 1:
            axis_dict = self.axes_manager.navigation_axes[0].get_axis_dictionary()
            axis_dict["index_in_array"] = 0
            for dim, index in zip(comp_ids, range(len(comp_ids))):
                s = Spectrum(
                    loadings[index],
                    axes=[
                        axis_dict,
                    ],
                )
                filename = "%s-%i.%s" % (loading_prefix, dim, loading_format)
                if folder is not None:
                    filename = os.path.join(folder, filename)
                s.save(filename)
        elif self.axes_manager.navigation_dimension == 2:
            axes_dicts = []
            axes = self.axes_manager.navigation_axes[::-1]
            shape = (axes[0].size, axes[1].size)
            loading_data = loadings.reshape((-1, shape[0], shape[1]))
            axes_dicts.append(axes[0].get_axis_dictionary())
            axes_dicts[0]["index_in_array"] = 0
            axes_dicts.append(axes[1].get_axis_dictionary())
            axes_dicts[1]["index_in_array"] = 1
            for dim, index in zip(comp_ids, range(len(comp_ids))):
                s = Image(
                    loading_data[index, ...],
                    axes=axes_dicts,
                    metadata={
                        "General": {
                            "title": "%s from %s"
                            % (loading_prefix, self.metadata.General.title),
                        }
                    },
                )
                filename = "%s-%i.%s" % (loading_prefix, dim, loading_format)
                if folder is not None:
                    filename = os.path.join(folder, filename)
                s.save(filename)