Ejemplo n.º 1
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
Ejemplo n.º 2
0
 def to_image(self):
     from hyperspy.signals.image import Image
     dic = self._get_signal_dict()
     dic['mapped_parameters']['record_by'] = 'image'
     dic['data'] = np.rollaxis(dic['data'], -1, 0)
     dic['axes'] = utils_varia.rollelem(dic['axes'],-1,0)
     i = 0
     for axis in dic['axes']:
         axis['index_in_array'] = i
         i += 1
     im = Image(dic)
     if hasattr(self, 'learning_results'):
         im.learning_results = copy.deepcopy(self.learning_results)
         im.learning_results._transpose_results()
         im.learning_results.original_shape = self.data.shape
     return im
Ejemplo n.º 3
0
 def crop_cells(self, idx=0):
     print "cropping cells..."
     from hyperspy.signals.image import Image
     # filter the peaks that are outside the selected threshold
     peaks = np.ma.compress_rows(self.mask_peaks(idx))
     tmp_sz = self.tmp_size
     data = np.zeros((tmp_sz, tmp_sz, peaks.shape[0]))
     if not hasattr(self.sig.mapped_parameters, "original_files"):
         parent = self.sig
     else:
         parent = self.sig.mapped_parameters.original_files[
             self.titles[idx]]
     for i in xrange(peaks.shape[0]):
         # crop the cells from the given locations
         data[:, :, i] = self.sig.data[peaks[i, 1]:peaks[i, 1] + tmp_sz,
                                       peaks[i,
                                             0]:peaks[i, 0] + tmp_sz, idx]
         crop_sig = Image({
             'data': data,
             'mapped_parameters': {
                 'name': 'Cropped cells from %s' % self.titles[idx],
                 'record_by': 'image',
                 'locations': peaks,
                 'parent': parent,
             }
         })
     return crop_sig
     # attach a class member that has the locations from which the images were cropped
     print "Complete.  "
Ejemplo n.º 4
0
def load_with_reader(filename, reader, record_by=None,
        signal_type=None, output_level=1, **kwds):
    from hyperspy.signals.image import Image
    from hyperspy.signals.spectrum import Spectrum
    from hyperspy.signals.eels import EELSSpectrum
    if output_level>1:
        messages.information('Loading %s ...' % filename)
    
    file_data_list = reader.file_reader(filename,
                                        record_by=record_by,
                                        output_level=output_level,
                                        **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_type is not None:
            file_data_dict['mapped_parameters']['signal_type'] = signal_type

        if file_data_dict['mapped_parameters']['record_by'] == 'image':
            s = Image(file_data_dict)
        else:
            if ('signal_type' in file_data_dict['mapped_parameters'] 
                and file_data_dict['mapped_parameters']['signal_type'] 
                == 'EELS'):
                s = EELSSpectrum(file_data_dict)
            else:
                s = Spectrum(file_data_dict)
        folder, filename = os.path.split(os.path.abspath(filename))
        filename, extension = os.path.splitext(filename)
        s.tmp_parameters.folder = folder
        s.tmp_parameters.filename = filename
        s.tmp_parameters.extension = extension.replace('.','')
        objects.append(s)
        s.print_summary()

    if len(objects) == 1:
        objects = objects[0]
    if output_level>1:
        messages.information('%s correctly loaded' % filename)
    return objects
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
    def to_image(self, signal_to_index=0):
        """Spectrum to image

        Parameters
        ----------
        signal_to_index : integer
            Position to move the signal axis.        
            
        Examples
        --------        
        >>> s = signals.Spectrum({'data' : np.ones((3,4,5,6))})
        >>> s
        <Spectrum, title: , dimensions: (3L, 4L, 5L, 6L)>

        >>> s.to_image()
        <Image, title: , dimensions: (6L, 3L, 4L, 5L)>

        >>> s.to_image(1)
        <Image, title: , dimensions: (3L, 6L, 4L, 5L)>
        
        """
        from hyperspy.signals.image import Image
        dic = self._get_signal_dict()
        dic['mapped_parameters']['record_by'] = 'image'
        dic['data'] = np.rollaxis(dic['data'], -1, signal_to_index)
        dic['axes'] = utils_varia.rollelem(dic['axes'],-1,signal_to_index)
        i = 0
        for axis in dic['axes']:
            axis['index_in_array'] = i
            i += 1
        im = Image(dic)
        
        if hasattr(self, 'learning_results'):
            if signal_to_index != 0 and self.learning_results.loadings is not None:
                print("The learning results won't be transfered correctly")
            else :
                im.learning_results = copy.deepcopy(self.learning_results)
                im.learning_results._transpose_results()
                im.learning_results.original_shape = self.data.shape

        im.tmp_parameters = self.tmp_parameters.deepcopy()
        return im
Ejemplo n.º 7
0
 def to_image(self):
     from hyperspy.signals.image import Image
     dic = self._get_signal_dict()
     dic['mapped_parameters']['record_by'] = 'image'
     dic['data'] = np.rollaxis(dic['data'], -1, 0)
     dic['axes'] = utils_varia.rollelem(dic['axes'], -1, 0)
     i = 0
     for axis in dic['axes']:
         axis['index_in_array'] = i
         i += 1
     return Image(dic)
Ejemplo n.º 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
Ejemplo n.º 9
0
def load_with_reader(filename, reader, record_by = None, signal_type = None,
                     output_level=1, is_agg = False, **kwds):
    from hyperspy.signals.image import Image
    from hyperspy.signals.spectrum import Spectrum
    from hyperspy.signals.eels import EELSSpectrum
    if output_level>1:
        messages.information(reader.description)
    file_data_list = reader.file_reader(filename,
                                         record_by=record_by,
                                        output_level=output_level,
                                        **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_type is not None:
            file_data_dict['mapped_parameters']['signal_type'] = signal_type

        if file_data_dict['mapped_parameters']['record_by'] == 'image':
            s = Image(file_data_dict)
        else:
            if 'signal_type' in file_data_dict['mapped_parameters'] and \
                file_data_dict['mapped_parameters']['signal_type'] == 'EELS':
                s = EELSSpectrum(file_data_dict)
            else:
                s = Spectrum(file_data_dict)
        objects.append(s)
        print s
        if hyperspy.defaults_parser.preferences.General.plot_on_load is True \
            and is_agg is False:
            s.plot()
    if len(objects) == 1:
        objects = objects[0]
    return objects
Ejemplo n.º 10
0
 def calculate_thickness(self, method='threshold', threshold=3, factor=1):
     """Calculates the thickness from a LL SI.
     
     The resulting thickness map is stored in self.thickness as an image 
     instance. To visualize it: self.thickness.plot()
     
     Parameters
     ----------
     method : {'threshold', 'zl'}
         If 'threshold', it will extract the zero loss by just splittin the 
         spectrum at the threshold value. If 'zl', it will use the 
         self.zero_loss SI (if defined) to perform the calculation.
     threshold : float
         threshold value.
     factor : float
         factor by which to multiple the ZLP
     """
     print "Calculating the thickness"
     # Create the thickness array
     dc = self.data
     axis = self.axes_manager._slicing_axes[0]
     integral = dc.sum(axis.index_in_array)
     if method == 'zl':
         if self.zero_loss is None:
             hyperspy.messages.warning_exit(
                 'To use this method the zero_loss'
                 'attribute must be defined')
         zl = self.zero_loss.data
         zl_int = zl.sum(axis.index_in_array)
     elif method == 'threshold':
         ti = axis.value2index(threshold)
         zl_int = self.data[(slice(None), ) * axis.index_in_array + (
             slice(None, ti),
             Ellipsis,
         )].sum(axis.index_in_array) * factor
     self.thickness = \
     Image({'data' : np.log(integral / zl_int)})
Ejemplo n.º 11
0
"""Creates a 4D 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.image import Image

s = Image({'data': np.random.random((16, 16, 32, 32))})
s.plot()

# If running from hyperspy's interactive console the next line can be
# omitted
plt.show()
Ejemplo n.º 12
0
    def kmeans_cluster_stack(self, clusters=None):
        smp = self.mapped_parameters
        d = self.data
        # if clusters not given, try to determine what it should be.
        if clusters is None:
            pass
        kmeans = mdp.nodes.KMeansClassifier(clusters)
        avg_stack = np.zeros((d.shape[0], d.shape[1], clusters))
        kmeans.train(d.reshape((-1, d.shape[2])).T)
        kmeans.stop_training()
        groups = kmeans.label(d.reshape((-1, d.shape[2])).T)
        cluster_arrays = []

        try:
            # test if location data is available
            smp.locations.values()[0]
        except:
            print "Warning: No cell location information was available."
        for i in xrange(clusters):
            # which file are we pulling from?
            file_index = 0
            address = smp.aggregate_address.values()[file_index]
            fname = smp.locations.keys()[file_index]
            # get number of members of this cluster
            members = groups.count(i)
            cluster_array = np.zeros((d.shape[0], d.shape[1], members))
            cluster_idx = 0
            # positions is a recarray, with each row consisting of a filename and the position from
            # which the crop was taken.
            positions = np.zeros((members, 1),
                                 dtype=[('filename', 'a256'),
                                        ('position', 'i4', (1, 2))])
            for j in xrange(len(groups)):
                if j > (address[1]) and fname <> smp.locations.keys()[-1]:
                    file_index += 1
                    fname = smp.locations.keys()[file_index]
                    address = self.mapped_parameters.aggregate_address.values(
                    )[file_index]
                file_j = j - address[0]
                if groups[j] == i:
                    cluster_array[:, :, cluster_idx] = d[:, :, j]
                    try:
                        positions[cluster_idx] = (
                            fname, smp.locations[fname][file_j, :2])
                    except:
                        pass
                    cluster_idx += 1
            # create a trimmed dict of the original files for this particular
            # cluster.  Only include files that thie cluster includes members
            # from.
            constrained_orig_files = {}
            for key in self.mapped_parameters.original_files.keys():
                if key in positions['filename']:
                    constrained_orig_files[
                        key] = self.mapped_parameters.original_files[key]
            cluster_array_Image = Image({
                'data': cluster_array,
                'mapped_parameters': {
                    'name':
                    'Cluster %s from %s' % (i, self.mapped_parameters.name),
                    'locations': positions,
                    'members': members,
                    'original_files': self.mapped_parameters.original_files,
                }
            })
            cluster_arrays.append(cluster_array_Image)
            avg_stack[:, :, i] = np.sum(cluster_array, axis=2)
        members_list = [groups.count(i) for i in xrange(clusters)]
        avg_stack_Image = Image({
            'data': avg_stack,
            'mapped_parameters': {
                'name':
                'Cluster averages from %s' % self.mapped_parameters.name,
                'member_counts': members_list,
                'original_files': self.mapped_parameters.original_files,
            }
        })
        self.mapped_parameters.avgs = avg_stack_Image
        self.mapped_parameters.clusters = cluster_arrays
        print "Averages and classes stored in mapped_parameters.  Access them as: \n\n\
Ejemplo n.º 13
0
"""Creates a 3D 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.image import Image

s = Image({'data' : np.random.random((16,32,32))})
s.plot()

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

Ejemplo n.º 14
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