Exemple #1
0
def multi_readout_analyze(folder, ccd_height = 100., plot = True, freq = None):
    """Analyze several readout measurements in different files for readout
    diagnosys

    The readout files in dm3 format must be contained in a folder, preferentely
    numered in the order of acquisition.

    Parameters
    ----------
    folder : string
        Folder where the dm3 readout files are stored
    ccd_heigh : float
    plot : bool
    freq : float
        Frequency of the camera

    Returns
    -------
    Dictionary
    """
    from spectrum import Spectrum
    files = glob.glob1(folder, '*.nc')
    if not files:
        files = glob.glob1(folder, '*.dm3')
    spectra = []
    variances = []
    binnings = []
    for f in files:
        print os.path.join(folder,f)
        s = Spectrum(os.path.join(folder,f))
        variance, channel_mean, norm_time_mean = analyze_readout(s)
        s.readout_analysis = {}
        s.readout_analysis['variance'] = variance.mean()
        s.readout_analysis['pattern'] = channel_mean
        s.readout_analysis['time'] = norm_time_mean
        if not hasattr(s,'binning'):
            s.binning = float(os.path.splitext(f)[0][1:])
            if freq:
                s.readout_frequency = freq
                s.ccd_height = ccd_height
            s.save(f)
        spectra.append(s)
        binnings.append(s.binning)
        variances.append(variance.mean())
    pixels = ccd_height / np.array(binnings)
    plt.scatter(pixels, variances, label = 'data')
    fit = np.polyfit(pixels, variances,1, full = True)
    if plot:
        x = np.linspace(0,pixels.max(),100)
        y = x*fit[0][0] + fit[0][1]
        plt.plot(x,y, label = 'linear fit')
        plt.xlabel('number of pixels')
        plt.ylabel('variance')
        plt.legend(loc = 'upper left')

    print "Variance = %s * pixels + %s" % (fit[0][0], fit[0][1])
    dictio = {'pixels': pixels, 'variances': variances, 'fit' : fit,
    'spectra' : spectra}
    return dictio
 def _extract_background_fired(self):
     if self.pl is None: return
     signal = self.SI() - self.pl.function(self.SI.energy_axis)
     i = self.SI.energy2index(self.bg_span_selector.range[1])
     signal[:i] = 0.
     s = Spectrum({'calibration' : {'data_cube' : signal}})
     s.get_calibration_from(self.SI)
     interactive_ns[self.background_substracted_spectrum_name] = s       
Exemple #3
0
 def __init__(self, *args, **kwards):
     Spectrum.__init__(self, *args, **kwards)
     # Attributes defaults
     self.subshells = set()
     self.elements = set()
     self.edges = list()
     if hasattr(self.mapped_parameters, 'Sample') and \
     hasattr(self.mapped_parameters.Sample, 'elements'):
         print('Elemental composition read from file')
         self.add_elements(self.mapped_parameters.Sample.elements)
Exemple #4
0
    def save_independent_components(self,
                                    elements=None,
                                    spectrum_format='msa',
                                    image_format='tif',
                                    recmatrix=None,
                                    ic=None,
                                    on_peaks=False):
        """Saves the result of the ICA in image and spectrum format.
        Note that to save the image, the NaNs in the map will be converted
        to zeros.

        Parameters
        ----------
        elements : None or tuple of strings
            a list of names (normally an element) to be assigned to IC. If not
            the will be name ic-0, ic-1 ...
        image_format : string
        spectrum_format : string
        recmatrix : None or numpy array
            externally supplied recmatrix
        ic : None or numpy array
            externally supplied IC
        """
        from hyperspy.signals.spectrum import Spectrum
        target = self._get_target(on_peaks)
        pl = self.plot_independent_components_maps(plot=False,
                                                   recmatrix=recmatrix,
                                                   ic=ic,
                                                   no_nans=True,
                                                   on_peaks=on_peaks)
        if ic is None:
            ic = target.ic
        if self.data.shape[2] > 1:
            maps = True
        else:
            maps = False
        for i in xrange(ic.shape[1]):
            axes = (self.axes_manager._slicing_axes[0].get_axis_dictionary(), )
            axes[0]['index_in_array'] = 0
            spectrum = Spectrum({'data': ic[:, i], 'axes': axes})
            spectrum.data_cube = ic[:, i].reshape((-1, 1, 1))

            if elements is None:
                spectrum.save('ic-%s.%s' % (i, spectrum_format))
                if maps is True:
                    pl[i].save('map_ic-%s.%s' % (i, image_format))
                else:
                    pl[i].save('profile_ic-%s.%s' % (i, spectrum_format))
            else:
                element = elements[i]
                spectrum.save('ic-%s.%s' % (element, spectrum_format))
                if maps:
                    pl[i].save('map_ic-%s.%s' % (element, image_format))
                else:
                    pl[i].save('profile_ic-%s.%s' % (element, spectrum_format))
Exemple #5
0
    def save_principal_components(self,
                                  n,
                                  spectrum_prefix='pc',
                                  image_prefix='im',
                                  spectrum_format='msa',
                                  image_format='tif',
                                  on_peaks=False):
        """Save the `n` first principal components  and score maps
        in the specified format

        Parameters
        ----------
        n : int
            Number of principal components to save_als_ica_results
        image_prefix : string
            Prefix for the image file names
        spectrum_prefix : string
            Prefix for the spectrum file names
        spectrum_format : string
        image_format : string

        """
        from spectrum import Spectrum
        target = self._get_target(on_peaks)
        im_list = self.plot_principal_components_maps(n,
                                                      plot=False,
                                                      on_peaks=on_peaks)
        s = Spectrum({'calibration': {'data_cube': target.pc[:, 0]}})
        s.get_calibration_from(self)
        for i in xrange(n):
            s.data_cube = target.pc[:, i]
            s.get_dimensions_from_cube()
            s.save('%s-%i.%s' % (spectrum_prefix, i, spectrum_format))
            im_list[i].save('%s-%i.%s' % (image_prefix, i, image_format))
Exemple #6
0
    def save_independent_components(self, elements=None,
                                    spectrum_format='msa',
                                    image_format='tif',
                                    recmatrix=None, ic=None,
                                    on_peaks=False):
        """Saves the result of the ICA in image and spectrum format.
        Note that to save the image, the NaNs in the map will be converted
        to zeros.

        Parameters
        ----------
        elements : None or tuple of strings
            a list of names (normally an element) to be assigned to IC. If not
            the will be name ic-0, ic-1 ...
        image_format : string
        spectrum_format : string
        recmatrix : None or numpy array
            externally supplied recmatrix
        ic : None or numpy array
            externally supplied IC
        """
        from hyperspy.signals.spectrum import Spectrum
        target=self._get_target(on_peaks)
        pl = self.plot_independent_components_maps(plot=False,
                                                   recmatrix=recmatrix,
                                                   ic=ic,
                                                   no_nans=True,
                                                   on_peaks=on_peaks)
        if ic is None:
            ic = target.ic
        if self.data.shape[2] > 1:
            maps = True
        else:
            maps = False
        for i in xrange(ic.shape[1]):
            axes = (self.axes_manager._slicing_axes[0].get_axis_dictionary(),)
            axes[0]['index_in_array'] = 0
            spectrum = Spectrum({'data' : ic[:,i], 'axes' : axes})
            spectrum.data_cube = ic[:,i].reshape((-1,1,1))

            if elements is None:
                spectrum.save('ic-%s.%s' % (i, spectrum_format))
                if maps is True:
                    pl[i].save('map_ic-%s.%s' % (i, image_format))
                else:
                    pl[i].save('profile_ic-%s.%s' % (i, spectrum_format))
            else:
                element = elements[i]
                spectrum.save('ic-%s.%s' % (element, spectrum_format))
                if maps:
                    pl[i].save('map_ic-%s.%s' % (element, image_format))
                else:
                    pl[i].save('profile_ic-%s.%s' % (element, spectrum_format))
Exemple #7
0
    def save_principal_components(self, n, spectrum_prefix = 'pc',
    image_prefix = 'im', spectrum_format = 'msa', image_format = 'tif',
                                  on_peaks=False):
        """Save the `n` first principal components  and score maps
        in the specified format

        Parameters
        ----------
        n : int
            Number of principal components to save_als_ica_results
        image_prefix : string
            Prefix for the image file names
        spectrum_prefix : string
            Prefix for the spectrum file names
        spectrum_format : string
        image_format : string

        """
        from spectrum import Spectrum
        target=self._get_target(on_peaks)
        im_list = self.plot_principal_components_maps(n, plot = False,
                                                      on_peaks=on_peaks)
        s = Spectrum({'calibration' : {'data_cube' : target.pc[:,0]}})
        s.get_calibration_from(self)
        for i in xrange(n):
            s.data_cube = target.pc[:,i]
            s.get_dimensions_from_cube()
            s.save('%s-%i.%s' % (spectrum_prefix, i, spectrum_format))
            im_list[i].save('%s-%i.%s' % (image_prefix, i, image_format))
Exemple #8
0
def load_with_reader(filename, reader, record_by=None, signal=None, **kwds):
    from hyperspy.signals.image import Image
    from hyperspy.signals.spectrum import Spectrum
    from hyperspy.signals.eels import EELSSpectrum
    messages.information(reader.description)
    file_data_list = reader.file_reader(filename, record_by=record_by, **kwds)
    objects = []
    for file_data_dict in file_data_list:
        if record_by is not None:
            file_data_dict['mapped_parameters']['record_by'] = record_by
        # The record_by can still be None if it was not defined by the reader
        if file_data_dict['mapped_parameters']['record_by'] is None:
            print "No data type provided.  Defaulting to image."
            file_data_dict['mapped_parameters']['record_by'] = 'image'

        if signal is not None:
            file_data_dict['mapped_parameters']['signal'] = signal

        if file_data_dict['mapped_parameters']['record_by'] == 'image':
            s = Image(file_data_dict)
        else:
            if file_data_dict['mapped_parameters']['signal'] == 'EELS':
                s = EELSSpectrum(file_data_dict)
            else:
                s = Spectrum(file_data_dict)
        if defaults.plot_on_load is True:
            s.plot()
        objects.append(s)

    if len(objects) == 1:
        objects = objects[0]
    return objects
Exemple #9
0
 def to_spectrum(self):
     from hyperspy.signals.spectrum import Spectrum
     dic = self._get_signal_dict()
     dim = len(self.data.shape)
     dic['mapped_parameters']['record_by'] = 'spectrum'
     dic['data'] = np.rollaxis(dic['data'], 0, dim)
     dic['axes'] = utils_varia.rollelem(dic['axes'],0, dim)
     i = 0
     for axis in dic['axes']:
         axis['index_in_array'] = i
         i += 1
     sp = Spectrum(dic)
     sp.axes_manager._set_axes_index_in_array_from_position()
     if hasattr(self, 'learning_results'):
         sp.learning_results = copy.deepcopy(self.learning_results)
         sp.learning_results._transpose_results()
         sp.learning_results.original_shape = self.data.shape
     return sp
Exemple #10
0
 def to_spectrum(self):
     from hyperspy.signals.spectrum import Spectrum
     dic = self._get_signal_dict()
     dic['mapped_parameters']['record_by'] = 'spectrum'
     dic['data'] = np.swapaxes(dic['data'], 0, -1)
     utils_varia.swapelem(dic['axes'], 0, -1)
     dic['axes'][0]['index_in_array'] = 0
     dic['axes'][-1]['index_in_array'] = len(dic['axes']) - 1
     return Spectrum(dic)
Exemple #11
0
    def to_spectrum(self, signal_axis=0):
        """Image to spectrum

        Parameters
        ----------
        signal_axis : integer
            Selected the signal axis.        
            
        Examples
        --------        
        >>> img = signals.Image({'data' : np.ones((3,4,5,6))})
        >>> img
        <Image, title: , dimensions: (3L, 4L, 5L, 6L)>

        >>> img.to_spectrum()
        <Spectrum, title: , dimensions: (4L, 5L, 6L, 3L)>

        >>> img.to_spectrum(1)
        <Spectrum, title: , dimensions: (3L, 5L, 6L, 4L)>
        
        """
        from hyperspy.signals.spectrum import Spectrum
        dic = self._get_signal_dict()
        dim = len(self.data.shape)
        dic['mapped_parameters']['record_by'] = 'spectrum'        
        dic['data'] = np.rollaxis(dic['data'], signal_axis, dim)
        dic['axes'] = utils_varia.rollelem(dic['axes'], signal_axis, dim)
        i = 0
        for axis in dic['axes']:
            axis['index_in_array'] = i
            i += 1
        sp = Spectrum(dic)
        sp.axes_manager._set_axes_index_in_array_from_position()
        if hasattr(self, 'learning_results'):
            if signal_axis != 0 and self.learning_results.loadings is not None:
                print("The learning results won't be transfered correctly")
            else :
                sp.learning_results = copy.deepcopy(self.learning_results)
                sp.learning_results._transpose_results()
                sp.learning_results.original_shape = self.data.shape
                
        sp.tmp_parameters = self.tmp_parameters.deepcopy()
        return sp
Exemple #12
0
def chrono_align_and_sum(spectrum, energy_range = (None, None),
                         spatial_shape = None):
    """Alignment and sum of a chrono-spim SI

    Parameters
    ----------
    spectrum : Spectrum instance
        Chrono-spim
    energy_range : tuple of floats
        energy interval in which to perform the alignment in energy units
    axis : int
    """
    from spectrum import Spectrum
    dc = spectrum.data_cube
    min_energy_size = dc.shape[0]
#    i = 0
    new_dc = None

    # For the progress bar to work properly we must capture the output of the
    # functions that are called during the alignment process
    import cStringIO
    import sys
    capture_output = cStringIO.StringIO()

    from hyperspy.misc.progressbar import progressbar
    pbar = progressbar(maxval = dc.shape[2] - 1)
    for i in xrange(dc.shape[2]):
        pbar.update(i)
        sys.stdout = capture_output
        s = Spectrum({'calibration': {'data_cube' : dc[:,:,i]}})
        s.get_calibration_from(spectrum)
        s.find_low_loss_origin()
        s.align(energy_range, progress_bar = False)
        min_energy_size = min(s.data_cube.shape[0], min_energy_size)
        if new_dc is None:
            new_dc = s.data_cube.sum(1)
        else:
            new_dc = np.concatenate([new_dc[:min_energy_size],
                                     s.data_cube.sum(1)[:min_energy_size]], 1)
        sys.stdout = sys.__stdout__
    pbar.finish()
    spectrum.data_cube = new_dc
    spectrum.get_dimensions_from_cube()
    spectrum.find_low_loss_origin()
    spectrum.align(energy_range)
    spectrum.find_low_loss_origin()
    if spatial_shape is not None:
        spectrum.data_cube = spectrum.data_cube.reshape(
        [spectrum.data_cube.shape[0]] + list(spatial_shape))
        spectrum.data_cube = spectrum.data_cube.swapaxes(1,2)
        spectrum.get_dimensions_from_cube()
 def _extract_signal_fired(self):
     if self.signal_map is None: return
     if len(self.signal_map.squeeze().shape) == 2:
         s = Image(
         {'calibration' : {'data_cube' : self.signal_map.squeeze()}})
         s.xscale = self.SI.xscale
         s.yscale = self.SI.yscale
         s.xunits = self.SI.xunits
         s.yunits = self.SI.yunits
         interactive_ns[self.signal_name] = s
     else:
         s = Spectrum(
         {'calibration' : {'data_cube' : self.signal_map.squeeze()}})
         s.energyscale = self.SI.xscale
         s.energyunits = self.SI.xunits
         interactive_ns[self.signal_name] = s
 def __init__(self, dic = None, shape = None, dtype = 'float64'):
     if dic is None and shape is not None:
         data = np.zeros((shape), dtype = dtype)
         dic = {'data' : data}
     Spectrum.__init__(self, dic)
Exemple #15
0
 def __init__(self, *args, **kwards):
     Spectrum.__init__(self, *args, **kwards)
     # Attributes defaults
     self.subshells = set()
     self.elements = set()
     self.edges = list()
Exemple #16
0
"""Creates a spectrum image and plots it
"""

# If running from hyperspy's interactive the next two imports can be omitted
# omitted (i.e. the next 2 lines)
import numpy as np
import matplotlib.pyplot as plt

from hyperspy.signals.spectrum import Spectrum

s = Spectrum({'data': np.random.random((64, 64, 1024))})
s.plot()

# If running from hyperspy's interactive console the next line can be
# omitted
plt.show()
Exemple #17
0
 def __init__(self, *args, **kwards):
     Spectrum.__init__(self, *args, **kwards)
     # Attributes defaults
     self.subshells = set()
     self.elements = set()
     self.edges = list()
Exemple #18
0
"""Creates a line spectrum and plots it
"""

# If running from hyperspy's interactive the next two imports can be omitted 
# omitted (i.e. the next 2 lines)
import numpy as np
import matplotlib.pyplot as plt

from hyperspy.signals.spectrum import Spectrum

s = Spectrum({'data' : np.random.random((100,1024))})
s.plot()

# If running from hyperspy's interactive console the next line can be 
# omitted
plt.show()

Exemple #19
0
"""Creates several random spectrum images, then aggregates them.
"""

# If running from hyperspy's interactive the next two imports can be omitted
# omitted (i.e. the next 2 lines)
import numpy as np
import matplotlib.pyplot as plt

from hyperspy.signals.spectrum import Spectrum
from hyperspy.signals.aggregate import AggregateSpectrum

# create the spectrum objects

s1 = Spectrum({
    'data': np.random.random((64, 64, 1024)),
    'mapped_parameters': {
        'name': 'file1'
    }
})
s2 = Spectrum({
    'data': np.random.random((64, 64, 1024)),
    'mapped_parameters': {
        'name': 'file2'
    }
})
s3 = Spectrum({
    'data': np.random.random((64, 64, 1024)),
    'mapped_parameters': {
        'name': 'file3'
    }
})
"""
Exemple #20
0
    def plot_maps(self,
                  components,
                  mva_type=None,
                  scores=None,
                  factors=None,
                  cmap=plt.cm.gray,
                  no_nans=False,
                  with_components=True,
                  plot=True,
                  on_peaks=False,
                  directory=None):
        """
        Plot component maps for the different MSA types

        Parameters
        ----------
        components : None, int, or list of ints
            if None, returns maps of all components.
            if int, returns maps of components with ids from 0 to given int.
            if list of ints, returns maps of components with ids in given list.
        mva_type: string, currently either 'pca' or 'ica'
        scores: numpy array, the array of score maps
        factors: numpy array, the array of components, with each column as a component.
        cmap: matplotlib colormap instance
        no_nans: bool,
        with_components: bool,
        plot: bool,
        """
        from hyperspy.signals.image import Image
        from hyperspy.signals.spectrum import Spectrum

        target = self._get_target(on_peaks)

        if scores is None or (factors is None and with_components is True):
            print "Either recmatrix or components were not provided."
            print "Loading existing values from object."
            if mva_type is None:
                print "Neither scores nor analysis type specified.  Cannot proceed."
                return

            elif mva_type.lower() == 'pca':
                scores = target.v.T
                factors = target.pc
            elif mva_type.lower() == 'ica':
                scores = self._get_ica_scores(target)
                factors = target.ic
                if no_nans:
                    print 'Removing NaNs for a visually prettier plot.'
                    scores = np.nan_to_num(scores)  # remove ugly NaN pixels
            else:
                print "No scores provided and analysis type '%s' unrecognized. Cannot proceed." % mva_type
                return


#        if len(self.axes_manager.axes)==2:
#            shape=self.data.shape[0],1
#        else:
#            shape=self.data.shape[0],self.data.shape[1]
        im_list = []

        if components is None:
            components = xrange(factors.shape[1])

        elif type(components).__name__ != 'list':
            components = xrange(components)

        for i in components:
            if plot is True:
                figure = plt.figure()
                if with_components:
                    ax = figure.add_subplot(121)
                    ax2 = figure.add_subplot(122)
                else:
                    ax = figure.add_subplot(111)
            if self.axes_manager.navigation_dimension == 2:
                toplot = scores[i, :].reshape(
                    self.axes_manager.navigation_shape)
                im_list.append(
                    Image({
                        'data':
                        toplot,
                        'axes':
                        self.axes_manager._get_non_slicing_axes_dicts()
                    }))
                if plot is True:
                    mapa = ax.matshow(toplot, cmap=cmap)
                    if with_components:
                        ax2.plot(self.axes_manager.axes[-1].axis, factors[:,
                                                                          i])
                        ax2.set_title('%s component %i' %
                                      (mva_type.upper(), i))
                        ax2.set_xlabel('Energy (eV)')
                    figure.colorbar(mapa)
                    figure.canvas.draw()
                    #pointer = widgets.DraggableSquare(self.coordinates)
                    #pointer.add_axes(ax)
            elif self.axes_manager.navigation_dimension == 1:
                toplot = scores[i, :]
                im_list.append(
                    Spectrum({
                        "data":
                        toplot,
                        'axes':
                        self.axes_manager._get_non_slicing_axes_dicts()
                    }))
                im_list[-1].get_dimensions_from_data()
                if plot is True:
                    ax.step(range(len(toplot)), toplot)

                    if with_components:
                        ax2.plot(self.axes_manager.axes[-1].axis, factors[:,
                                                                          i])
                        ax2.set_title('%s component %s' %
                                      (mva_type.upper(), i))
                        ax2.set_xlabel('Energy (eV)')
            else:
                messages.warning_exit('View not supported')
            if plot is True:
                ax.set_title('%s component number %s map' %
                             (mva_type.upper(), i))
                figure.canvas.draw()
                if directory is not None:
                    if not os.path.isdir(directory):
                        os.makedirs(directory)
                    figure.savefig(os.path.join(directory, 'IC-%i.png' % i),
                                   dpi=600)
        return im_list
Exemple #21
0
"""Creates a 2D hyperspectrum consisting of two gaussians and plots it.

This example can serve as starting point to test other functionalities on the simulated hyperspectrum.

"""


# If running from hyperspy's interactive the next two imports can be omitted 
# omitted (i.e. the next 4 lines)
import numpy as np
import matplotlib.pyplot as plt

from hyperspy.signals.spectrum import Spectrum

# Create an empty spectrum
s = Spectrum({'data' : np.zeros((32,32,1024))})

# Generate some simple data: two Gaussians with random centers and area

# First we create a model
m = create_model(s)

## Define the first gaussian
gs1 = components.Gaussian()
# Add it to the model
m.append(gs1)

# Set the sparameters
gs1.sigma.value = 10
# Make the center vary in the -5,5 range around 128
gs1.origin.map['values'][:] = 256 + (np.random.random((32,32)) - 0.5) * 10
Exemple #22
0
    def independent_components_analysis(
        self, number_of_components=None, algorithm='CuBICA', diff_order=1,
        factors=None, comp_list = None, mask = None, on_peaks=False, on_scores=False,
        pretreatment = None, **kwargs):
        """Independent components analysis.

        Available algorithms: FastICA, JADE, CuBICA, and TDSEP

        Parameters
        ----------
        number_of_components : int
            number of principal components to pass to the ICA algorithm
        algorithm : {FastICA, JADE, CuBICA, TDSEP}
        diff_order : int
        factors : numpy array
            externally provided components
        comp_list : boolen numpy array
            choose the components to use by the boolean list. It permits to
            choose non contiguous components.
        mask : numpy boolean array with the same dimension as the PC
            If not None, only the selected channels will be used by the
            algorithm.
        pretreatment: dict
        
        Any extra parameter is passed to the ICA algorithm
        """
        target=self._get_target(on_peaks)

        if not hasattr(target, 'factors') or target.factors==None:
            self.decomposition(on_peaks=on_peaks)

        else:
            if factors is None:
                if on_scores:
                    factors = target.scores
                else:
                    factors = target.factors
            bool_index = np.zeros((factors.shape[0]), dtype = 'bool')
            if number_of_components is not None:
                bool_index[:number_of_components] = True
            else:
                if self.output_dimension is not None:
                    number_of_components = self.output_dimension
                    bool_index[:number_of_components] = True

            if comp_list is not None:
                for ifactors in comp_list:
                    bool_index[ifactors] = True
                number_of_components = len(comp_list)
            factors = factors[:,bool_index]
            if diff_order > 0 and pretreatment is None:
                factors = np.diff(factors, diff_order, axis = 0)
            if pretreatment is not None:
                from hyperspy.signals.spectrum import Spectrum
                sfactors = Spectrum({'data' : factors.T})
                if pretreatment['algorithm'] == 'savitzky_golay':
                    sfactors.smooth_savitzky_golay(
                        number_of_points = pretreatment['number_of_points'],
                        polynomial_order = pretreatment['polynomial_order'],
                        differential_order = diff_order)
                if pretreatment['algorithm'] == 'tv':
                    sfactors.smooth_tv(
                        pretreatment_parameter= pretreatment[
                            'pretreatment_parameter'],
                        differential_order = diff_order)
                factors = sfactors.data.T
                if pretreatment['algorithm'] == 'butter':
                    b, a = sp.signal.butter(pretreatment['order'],
                        pretreatment['cutoff'], pretreatment['type'])
                    for i in range(factors.shape[1]):
                        factors[:,i] = sp.signal.filtfilt(b, a, factors[:,i])
            
            if mask is not None:
                factors = factors[mask.ravel(), :]

            # first centers and scales data
            factors,invsqcovmat = centering_and_whitening(factors)
            if algorithm == 'orthomax':
                _, unmixing_matrix = utils.orthomax(factors, **kwargs)
                unmixing_matrix = unmixing_matrix.T
            
            elif algorithm == 'sklearn_fastica':
                target.ica_node = sklearn.decomposition.FastICA(**kwargs)
                target.ica_node.whiten = False
                target.ica_node.fit(factors)
                unmixing_matrix = target.ica_node.unmixing_matrix_
            
            else:
                to_exec = 'target.ica_node=mdp.nodes.%sNode(' % algorithm
                for key, value in kwargs.iteritems():
                    to_exec += '%s=%s,' % (key, value)
                to_exec += ')'
                exec(to_exec)
                target.ica_node.train(factors)
                unmixing_matrix = target.ica_node.get_recmatrix()

            target.unmixing_matrix = np.dot(unmixing_matrix, invsqcovmat)
            self._unmix_factors(target)
            self._unmix_scores(target)
            self._auto_reverse_ic(target)
            target.ica_algorithm = algorithm
Exemple #23
0
    def blind_source_separation(
        self,
        number_of_components=None,
        algorithm="sklearn_fastica",
        diff_order=1,
        factors=None,
        comp_list=None,
        mask=None,
        on_loadings=False,
        pretreatment=None,
        **kwargs
    ):
        """Blind source separation (BSS) on the result on the 
        decomposition.

        Available algorithms: FastICA, JADE, CuBICA, and TDSEP

        Parameters
        ----------
        number_of_components : int
            number of principal components to pass to the BSS algorithm
        algorithm : {FastICA, JADE, CuBICA, TDSEP}
        diff_order : int
            Sometimes it is convenient to perform the BSS on the derivative 
            of the signal. If diff_order is 0, the signal is not differentiated.
        factors : numpy.array
            Factors to decompose. If None, the BSS is performed on the result
            of a previous decomposition.
        comp_list : boolen numpy array
            choose the components to use by the boolean list. It permits
             to choose non contiguous components.
        mask : numpy boolean array with the same dimension as the signal
            If not None, the signal locations marked as True (masked) will 
            not be passed to the BSS algorithm.
        on_loadings : bool
            If True, perform the BSS on the loadings of a previous 
            decomposition. If False, performs it on the factors.
        pretreatment: dict
        
        **kwargs : extra key word arguments
            Any keyword arguments are passed to the BSS algorithm.
        
        """
        target = self.learning_results
        if not hasattr(target, "factors") or target.factors == None:
            raise AttributeError(
                "A decomposition must be performed before blind " "source seperation or factors must be provided."
            )
        else:
            if factors is None:
                if on_loadings:
                    factors = target.loadings
                else:
                    factors = target.factors
            bool_index = np.zeros((factors.shape[0]), dtype="bool")
            if number_of_components is not None:
                bool_index[:number_of_components] = True
            else:
                if target.output_dimension is not None:
                    number_of_components = target.output_dimension
                    bool_index[:number_of_components] = True

            if comp_list is not None:
                for ifactors in comp_list:
                    bool_index[ifactors] = True
                number_of_components = len(comp_list)
            factors = factors[:, bool_index]

            if pretreatment is not None:
                from hyperspy.signals.spectrum import Spectrum

                sfactors = Spectrum({"data": factors.T})
                if pretreatment["algorithm"] == "savitzky_golay":
                    sfactors.smooth_savitzky_golay(
                        number_of_points=pretreatment["number_of_points"],
                        polynomial_order=pretreatment["polynomial_order"],
                        differential_order=diff_order,
                    )
                if pretreatment["algorithm"] == "tv":
                    sfactors.smooth_tv(
                        smoothing_parameter=pretreatment["smoothing_parameter"], differential_order=diff_order
                    )
                factors = sfactors.data.T
                if pretreatment["algorithm"] == "butter":
                    b, a = sp.signal.butter(pretreatment["order"], pretreatment["cutoff"], pretreatment["type"])
                    for i in range(factors.shape[1]):
                        factors[:, i] = sp.signal.filtfilt(b, a, factors[:, i])
            elif diff_order > 0:
                factors = np.diff(factors, diff_order, axis=0)

            if mask is not None:
                factors = factors[~mask]

            # first center and scale the data
            factors, invsqcovmat = centering_and_whitening(factors)
            if algorithm == "orthomax":
                _, unmixing_matrix = utils.orthomax(factors, **kwargs)
                unmixing_matrix = unmixing_matrix.T

            elif algorithm == "sklearn_fastica":
                # if sklearn_installed is False:
                # raise ImportError(
                #'sklearn is not installed. Nothing done')
                if "tol" not in kwargs:
                    kwargs["tol"] = 1e-10
                target.bss_node = FastICA(**kwargs)
                target.bss_node.whiten = False
                target.bss_node.fit(factors)
                unmixing_matrix = target.bss_node.unmixing_matrix_
            else:
                if mdp_installed is False:
                    raise ImportError("MDP is not installed. Nothing done")
                to_exec = "target.bss_node=mdp.nodes.%sNode(" % algorithm
                for key, value in kwargs.iteritems():
                    to_exec += "%s=%s," % (key, value)
                to_exec += ")"
                exec (to_exec)
                target.bss_node.train(factors)
                unmixing_matrix = target.bss_node.get_recmatrix()

            target.unmixing_matrix = np.dot(unmixing_matrix, invsqcovmat)
            self._unmix_factors(target)
            self._unmix_loadings(target)
            self._auto_reverse_bss_component(target)
            target.bss_algorithm = algorithm