# Buggy code
def _export_factors(
    self,
    factors,
    folder=None,
    comp_ids=None,
    multiple_files=None,
    save_figures=False,
    save_figures_format="png",
    factor_prefix=None,
    factor_format=None,
    comp_label=None,
    cmap=plt.cm.gray,
    plot_shifts=True,
    plot_char=4,
    img_data=None,
    same_window=False,
    calibrate=True,
    quiver_color="white",
    vector_scale=1,
    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 factor_format is None:
        factor_format = preferences.MachineLearning.export_factors_default_file_format

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

    if save_figures is True:
        plt.ioff()
        fac_plots = self._plot_factors_or_pchars(
            factors,
            comp_ids=comp_ids,
            same_window=same_window,
            comp_label=comp_label,
            img_data=img_data,
            plot_shifts=plot_shifts,
            plot_char=plot_char,
            cmap=cmap,
            per_row=per_row,
            quiver_color=quiver_color,
            vector_scale=vector_scale,
        )
        for idx in range(len(comp_ids)):
            filename = "%s_%02i.%s" % (
                factor_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}
            fac_plots[idx].savefig(filename, **_args)
        plt.ion()

    elif multiple_files is False:
        if self.axes_manager.signal_dimension == 2:
            # factor images
            axes_dicts = []
            axes = self.axes_manager.signal_axes[::-1]
            shape = (axes[1].size, axes[0].size)
            factor_data = np.rollaxis(factors.reshape((shape[0], shape[1], -1)), 2)
            axes_dicts.append(axes[0].get_axis_dictionary())
            axes_dicts.append(axes[1].get_axis_dictionary())
            axes_dicts.append(
                {
                    "name": "factor_index",
                    "scale": 1.0,
                    "offset": 0.0,
                    "size": int(factors.shape[1]),
                    "units": "factor",
                    "index_in_array": 0,
                }
            )
            s = Image(
                factor_data,
                axes=axes_dicts,
                metadata={
                    "General": {
                        "title": "%s from %s"
                        % (factor_prefix, self.metadata.General.title),
                    }
                },
            )
        elif self.axes_manager.signal_dimension == 1:
            axes = [
                self.axes_manager.signal_axes[0].get_axis_dictionary(),
                {
                    "name": "factor_index",
                    "scale": 1.0,
                    "offset": 0.0,
                    "size": int(factors.shape[1]),
                    "units": "factor",
                    "index_in_array": 0,
                },
            ]
            axes[0]["index_in_array"] = 1
            s = Spectrum(
                factors.T,
                axes=axes,
                metadata={
                    "General": {
                        "title": "%s from %s"
                        % (factor_prefix, self.metadata.General.title),
                    }
                },
            )
        filename = "%ss.%s" % (factor_prefix, factor_format)
        if folder is not None:
            filename = os.path.join(folder, filename)
        s.save(filename)
    else:  # Separate files
        if self.axes_manager.signal_dimension == 1:
            axis_dict = self.axes_manager.signal_axes[0].get_axis_dictionary()
            axis_dict["index_in_array"] = 0
            for dim, index in zip(comp_ids, range(len(comp_ids))):
                s = Spectrum(
                    factors[:, index],
                    axes=[
                        axis_dict,
                    ],
                    metadata={
                        "General": {
                            "title": "%s from %s"
                            % (factor_prefix, self.metadata.General.title),
                        }
                    },
                )
                filename = "%s-%i.%s" % (factor_prefix, dim, factor_format)
                if folder is not None:
                    filename = os.path.join(folder, filename)
                s.save(filename)

        if self.axes_manager.signal_dimension == 2:
            axes = self.axes_manager.signal_axes
            axes_dicts = [axes[0].get_axis_dictionary(), axes[1].get_axis_dictionary()]
            axes_dicts[0]["index_in_array"] = 0
            axes_dicts[1]["index_in_array"] = 1

            factor_data = factors.reshape(
                self.axes_manager._signal_shape_in_array
                + [
                    -1,
                ]
            )

            for dim, index in zip(comp_ids, range(len(comp_ids))):
                im = Image(
                    factor_data[..., index],
                    axes=axes_dicts,
                    metadata={
                        "General": {
                            "title": "%s from %s"
                            % (factor_prefix, self.metadata.General.title),
                        }
                    },
                )
                filename = "%s-%i.%s" % (factor_prefix, dim, factor_format)
                if folder is not None:
                    filename = os.path.join(folder, filename)
                im.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_factors(
    self,
    factors,
    folder=None,
    comp_ids=None,
    multiple_files=None,
    save_figures=False,
    save_figures_format="png",
    factor_prefix=None,
    factor_format=None,
    comp_label=None,
    cmap=plt.cm.gray,
    plot_shifts=True,
    plot_char=4,
    img_data=None,
    same_window=False,
    calibrate=True,
    quiver_color="white",
    vector_scale=1,
    no_nans=True,
    per_row=3,
):
    from hyperspy.signals import Spectrum, Image

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

    if factor_format is None:
        factor_format = preferences.MachineLearning.export_factors_default_file_format

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

    if save_figures is True:
        plt.ioff()
        fac_plots = self._plot_factors_or_pchars(
            factors,
            comp_ids=comp_ids,
            same_window=same_window,
            comp_label=comp_label,
            img_data=img_data,
            plot_shifts=plot_shifts,
            plot_char=plot_char,
            cmap=cmap,
            per_row=per_row,
            quiver_color=quiver_color,
            vector_scale=vector_scale,
        )
        for idx in range(len(comp_ids)):
            filename = "%s_%02i.%s" % (
                factor_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}
            fac_plots[idx].savefig(filename, **_args)
        plt.ion()

    elif multiple_files is False:
        if self.axes_manager.signal_dimension == 2:
            # factor images
            axes_dicts = []
            axes = self.axes_manager.signal_axes[::-1]
            shape = (axes[1].size, axes[0].size)
            factor_data = np.rollaxis(factors.reshape((shape[0], shape[1], -1)), 2)
            axes_dicts.append(axes[0].get_axis_dictionary())
            axes_dicts.append(axes[1].get_axis_dictionary())
            axes_dicts.append(
                {
                    "name": "factor_index",
                    "scale": 1.0,
                    "offset": 0.0,
                    "size": int(factors.shape[1]),
                    "units": "factor",
                    "index_in_array": 0,
                }
            )
            s = Image(
                factor_data,
                axes=axes_dicts,
                metadata={
                    "General": {
                        "title": "%s from %s"
                        % (factor_prefix, self.metadata.General.title),
                    }
                },
            )
        elif self.axes_manager.signal_dimension == 1:
            axes = [
                self.axes_manager.signal_axes[0].get_axis_dictionary(),
                {
                    "name": "factor_index",
                    "scale": 1.0,
                    "offset": 0.0,
                    "size": int(factors.shape[1]),
                    "units": "factor",
                    "index_in_array": 0,
                },
            ]
            axes[0]["index_in_array"] = 1
            s = Spectrum(
                factors.T,
                axes=axes,
                metadata={
                    "General": {
                        "title": "%s from %s"
                        % (factor_prefix, self.metadata.General.title),
                    }
                },
            )
        filename = "%ss.%s" % (factor_prefix, factor_format)
        if folder is not None:
            filename = os.path.join(folder, filename)
        s.save(filename)
    else:  # Separate files
        if self.axes_manager.signal_dimension == 1:
            axis_dict = self.axes_manager.signal_axes[0].get_axis_dictionary()
            axis_dict["index_in_array"] = 0
            for dim, index in zip(comp_ids, range(len(comp_ids))):
                s = Spectrum(
                    factors[:, index],
                    axes=[
                        axis_dict,
                    ],
                    metadata={
                        "General": {
                            "title": "%s from %s"
                            % (factor_prefix, self.metadata.General.title),
                        }
                    },
                )
                filename = "%s-%i.%s" % (factor_prefix, dim, factor_format)
                if folder is not None:
                    filename = os.path.join(folder, filename)
                s.save(filename)

        if self.axes_manager.signal_dimension == 2:
            axes = self.axes_manager.signal_axes
            axes_dicts = [axes[0].get_axis_dictionary(), axes[1].get_axis_dictionary()]
            axes_dicts[0]["index_in_array"] = 0
            axes_dicts[1]["index_in_array"] = 1

            factor_data = factors.reshape(
                self.axes_manager._signal_shape_in_array
                + [
                    -1,
                ]
            )

            for dim, index in zip(comp_ids, range(len(comp_ids))):
                im = Image(
                    factor_data[..., index],
                    axes=axes_dicts,
                    metadata={
                        "General": {
                            "title": "%s from %s"
                            % (factor_prefix, self.metadata.General.title),
                        }
                    },
                )
                filename = "%s-%i.%s" % (factor_prefix, dim, factor_format)
                if folder is not None:
                    filename = os.path.join(folder, filename)
                im.save(filename)