def load(filenames=None, record_by=None, signal_type=None, signal_origin=None, stack=False, stack_axis=None, new_axis_name="stack_element", mmap=False, mmap_dir=None, **kwds): """ Load potentially multiple supported file into an hyperspy structure Supported formats: HDF5, msa, Gatan dm3, Ripple (rpl+raw) FEI ser and emi and hdf5, tif and a number of image formats. Any extra keyword is passed to the corresponsing reader. For available options see their individual documentation. Parameters ---------- filenames : None, str or list of strings The filename to be loaded. If None, a window will open to select a file to load. If a valid filename is passed in that single file is loaded. If multiple file names are passed in a list, a list of objects or a single object containing the data of the individual files stacked are returned. This behaviour is controlled by the `stack` parameter (see bellow). Multiple files can be loaded by using simple shell-style wildcards, e.g. 'my_file*.msa' loads all the files that starts by 'my_file' and has the '.msa' extension. record_by : {None, 'spectrum', 'image', ""} The value provided may determine the Signal subclass assigned to the data. If None, the value is read or guessed from the file. Any other value overrides the value stored in the file if any. If "spectrum" load the data in a Spectrum (sub)class. If "image" load the data in an Image (sub)class. If "" (empty string) load the data in a Signal class. signal_type : {None, "EELS", "EDS_TEM", "EDS_SEM", "", str} The acronym that identifies the signal type. The value provided may determine the Signal subclass assigned to the data. If None the value is read/guessed from the file. Any other value overrides the value stored in the file if any. For electron energy-loss spectroscopy use "EELS". For energy dispersive x-rays use "EDS_TEM" if acquired from an electron-transparent sample — as it is usually the case in a transmission electron microscope (TEM) —, "EDS_SEM" if acquired from a non electron-transparent sample — as it is usually the case in a scanning electron microscope (SEM) —. If "" (empty string) the value is not read from the file and is considered undefined. signal_origin : {None, "experiment", "simulation", ""} Defines the origin of the signal. The value provided may determine the Signal subclass assigned to the data. If None the value is read/guessed from the file. Any other value overrides the value stored in the file if any. Use "experiment" if loading experimental data. Use "simulation" if loading simulated data. If "" (empty string) the value is not read from the file and is considered undefined. stack : bool If True and multiple filenames are passed in, stacking all the data into a single object is attempted. All files must match in shape. It is possible to store the data in a memory mapped temporary file instead of in memory setting mmap_mode. The title is set to the name of the folder containing the files. stack_axis : {None, int, str} If None, the signals are stacked over a new axis. The data must have the same dimensions. Otherwise the signals are stacked over the axis given by its integer index or its name. The data must have the same shape, except in the dimension corresponding to `axis`. new_axis_name : string The name of the new axis when `axis` is None. If an axis with this name already exists it automatically append '-i', where `i` are integers, until it finds a name that is not yet in use. mmap: bool If True and stack is True, then the data is stored in a memory-mapped temporary file.The memory-mapped data is stored on disk, and not directly loaded into memory. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. mmap_dir : string If mmap_dir is not None, and stack and mmap are True, the memory mapped file will be created in the given directory, otherwise the default directory is used. Returns ------- Signal instance or list of signal instances Examples -------- Loading a single file providing the signal type: >>> d = load('file.dm3', signal_type='EDS_TEM') Loading a single file and overriding its default record_by: >>> d = load('file.dm3', record_by='Image') Loading multiple files: >>> d = load('file1.dm3','file2.dm3') Loading multiple files matching the pattern: >>>d = load('file*.dm3') """ kwds['record_by'] = record_by kwds['signal_type'] = signal_type kwds['signal_origin'] = signal_origin if filenames is None: if hyperspy.defaults_parser.preferences.General.interactive is True: from hyperspy.gui.tools import Load load_ui = Load() load_ui.edit_traits() if load_ui.filename: filenames = load_ui.filename else: raise ValueError("No file provided to reader and " "interactive mode is disabled") if filenames is None: raise ValueError("No file provided to reader") if isinstance(filenames, basestring): filenames = natsorted([f for f in glob.glob(filenames) if os.path.isfile(f)]) if not filenames: raise ValueError('No file name matches this pattern') elif not isinstance(filenames, (list, tuple)): raise ValueError( 'The filenames parameter must be a list, tuple, string or None') if not filenames: raise ValueError('No file provided to reader.') return None else: if len(filenames) > 1: messages.information('Loading individual files') if stack is True: signal = [] for i, filename in enumerate(filenames): obj = load_single_file(filename, **kwds) signal.append(obj) signal = hyperspy.utils.stack(signal, axis=stack_axis, new_axis_name=new_axis_name, mmap=mmap, mmap_dir=mmap_dir) signal.metadata.General.title = \ os.path.split( os.path.split( os.path.abspath(filenames[0]) )[0] )[1] messages.information('Individual files loaded correctly') signal._print_summary() objects = [signal, ] else: objects = [load_single_file(filename, **kwds) for filename in filenames] if hyperspy.defaults_parser.preferences.Plot.plot_on_load: for obj in objects: obj.plot() if len(objects) == 1: objects = objects[0] return objects
def load(filenames=None, record_by=None, signal_type=None, signal_origin=None, stack=False, stack_axis=None, new_axis_name="stack_element", mmap=False, mmap_dir=None, **kwds): """ Load potentially multiple supported file into an hyperspy structure Supported formats: HDF5, msa, Gatan dm3, Ripple (rpl+raw) FEI ser and emi and hdf5, tif and a number of image formats. Any extra keyword is passed to the corresponsing reader. For available options see their individual documentation. Parameters ---------- filenames : None, str or list of strings The filename to be loaded. If None, a window will open to select a file to load. If a valid filename is passed in that single file is loaded. If multiple file names are passed in a list, a list of objects or a single object containing the data of the individual files stacked are returned. This behaviour is controlled by the `stack` parameter (see bellow). Multiple files can be loaded by using simple shell-style wildcards, e.g. 'my_file*.msa' loads all the files that starts by 'my_file' and has the '.msa' extension. record_by : {None, 'spectrum', 'image', ""} The value provided may determine the Signal subclass assigned to the data. If None, the value is read or guessed from the file. Any other value overrides the value stored in the file if any. If "spectrum" load the data in a Spectrum (sub)class. If "image" load the data in an Image (sub)class. If "" (empty string) load the data in a Signal class. signal_type : {None, "EELS", "EDS_TEM", "EDS_SEM", "", str} The acronym that identifies the signal type. The value provided may determine the Signal subclass assigned to the data. If None the value is read/guessed from the file. Any other value overrides the value stored in the file if any. For electron energy-loss spectroscopy use "EELS". For energy dispersive x-rays use "EDS_TEM" if acquired from an electron-transparent sample — as it is usually the case in a transmission electron microscope (TEM) —, "EDS_SEM" if acquired from a non electron-transparent sample — as it is usually the case in a scanning electron microscope (SEM) —. If "" (empty string) the value is not read from the file and is considered undefined. signal_origin : {None, "experiment", "simulation", ""} Defines the origin of the signal. The value provided may determine the Signal subclass assigned to the data. If None the value is read/guessed from the file. Any other value overrides the value stored in the file if any. Use "experiment" if loading experimental data. Use "simulation" if loading simulated data. If "" (empty string) the value is not read from the file and is considered undefined. stack : bool If True and multiple filenames are passed in, stacking all the data into a single object is attempted. All files must match in shape. It is possible to store the data in a memory mapped temporary file instead of in memory setting mmap_mode. The title is set to the name of the folder containing the files. stack_axis : {None, int, str} If None, the signals are stacked over a new axis. The data must have the same dimensions. Otherwise the signals are stacked over the axis given by its integer index or its name. The data must have the same shape, except in the dimension corresponding to `axis`. new_axis_name : string The name of the new axis when `axis` is None. If an axis with this name already exists it automatically append '-i', where `i` are integers, until it finds a name that is not yet in use. mmap: bool If True and stack is True, then the data is stored in a memory-mapped temporary file.The memory-mapped data is stored on disk, and not directly loaded into memory. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. mmap_dir : string If mmap_dir is not None, and stack and mmap are True, the memory mapped file will be created in the given directory, otherwise the default directory is used. Returns ------- Signal instance or list of signal instances Examples -------- Loading a single file providing the signal type: >>> d = load('file.dm3', signal_type='EDS_TEM') Loading a single file and overriding its default record_by: >>> d = load('file.dm3', record_by='Image') Loading multiple files: >>> d = load('file1.dm3','file2.dm3') Loading multiple files matching the pattern: >>>d = load('file*.dm3') """ kwds['record_by'] = record_by kwds['signal_type'] = signal_type kwds['signal_origin'] = signal_origin if filenames is None: if hyperspy.defaults_parser.preferences.General.interactive is True: from hyperspy.gui.tools import Load load_ui = Load() load_ui.edit_traits() if load_ui.filename: filenames = load_ui.filename else: raise ValueError("No file provided to reader and " "interactive mode is disabled") if filenames is None: raise ValueError("No file provided to reader") if isinstance(filenames, basestring): filenames = natsorted( [f for f in glob.glob(filenames) if os.path.isfile(f)]) if not filenames: raise ValueError('No file name matches this pattern') elif not isinstance(filenames, (list, tuple)): raise ValueError( 'The filenames parameter must be a list, tuple, string or None') if not filenames: raise ValueError('No file provided to reader.') return None else: if len(filenames) > 1: messages.information('Loading individual files') if stack is True: signal = [] for i, filename in enumerate(filenames): obj = load_single_file(filename, **kwds) signal.append(obj) signal = hyperspy.utils.stack(signal, axis=stack_axis, new_axis_name=new_axis_name, mmap=mmap, mmap_dir=mmap_dir) signal.mapped_parameters.title = \ os.path.split( os.path.split( os.path.abspath(filenames[0]) )[0] )[1] messages.information('Individual files loaded correctly') signal._print_summary() objects = [ signal, ] else: objects = [ load_single_file(filename, **kwds) for filename in filenames ] if hyperspy.defaults_parser.preferences.General.plot_on_load: for obj in objects: obj.plot() if len(objects) == 1: objects = objects[0] return objects
def load(filenames=None, record_by=None, signal_type=None, stack=False, mmap=False, mmap_dir=None, **kwds): """ Load potentially multiple supported file into an hyperspy structure Supported formats: HDF5, msa, Gatan dm3, Ripple (rpl+raw) FEI ser and emi and hdf5, tif and a number of image formats. Any extra keyword is passed to the corresponsing reader. For available options see their individual documentation. Parameters ---------- filenames : None, str or list of strings The filename to be loaded. If None, a window will open to select a file to load. If a valid filename is passed in that single file is loaded. If multiple file names are passed in a list, a list of objects or a single object containing the data of the individual files stacked are returned. This behaviour is controlled by the `stack` parameter (see bellow). Multiple files can be loaded by using simple shell-style wildcards, e.g. 'my_file*.msa' loads all the files that starts by 'my_file' and has the '.msa' extension. record_by : None | 'spectrum' | 'image' Manually set the way in which the data will be read. Possible values are 'spectrum' or 'image'. signal_type : str Manually set the signal type of the data. Although only setting signal type to 'EELS' will currently change the way the data is loaded, it is good practice to set this parameter so it can be stored when saving the file. Please note that, if the signal_type is already defined in the file the information will be overriden without warning. stack : bool If True and multiple filenames are passed in, stacking all the data into a single object is attempted. All files must match in shape. It is possible to store the data in a memory mapped temporary file instead of in memory setting mmap_mode. mmap: bool If True and stack is True, then the data is stored in a memory-mapped temporary file.The memory-mapped data is stored on disk, and not directly loaded into memory. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. mmap_dir : string If mmap_dir is not None, and stack and mmap are True, the memory mapped file will be created in the given directory, otherwise the default directory is used. Returns ------- Signal instance or list of signal instances Examples -------- Loading a single file providing the signal type: >>> d = load('file.dm3', signal_type='XPS') Loading a single file and overriding its default record_by: >>> d = load('file.dm3', record_by='Image') Loading multiple files: >>> d = load('file1.dm3','file2.dm3') Loading multiple files matching the pattern: >>>d = load('file*.dm3') """ if filenames is None: if hyperspy.defaults_parser.preferences.General.interactive is True: load_ui = Load() load_ui.edit_traits() if load_ui.filename: filenames = load_ui.filename else: raise ValueError("No file provided to reader and " "interactive mode is disabled") if filenames is None: raise ValueError("No file provided to reader") if isinstance(filenames, basestring): filenames=natsorted([f for f in glob.glob(filenames) if os.path.isfile(f)]) if not filenames: raise ValueError('No file name matches this pattern') elif not isinstance(filenames, (list, tuple)): raise ValueError( 'The filenames parameter must be a list, tuple, string or None') if not filenames: raise ValueError('No file provided to reader.') return None else: if len(filenames) > 1: messages.information('Loading individual files') if stack is True: original_shape = None for i, filename in enumerate(filenames): obj = load_single_file(filename, output_level=0,**kwds) if original_shape is None: original_shape = obj.data.shape record_by = obj.mapped_parameters.record_by stack_shape = tuple([len(filenames),]) + original_shape tempf = None if mmap is False: data = np.empty(stack_shape, dtype=obj.data.dtype) else: #filename = os.path.join(tempfile.mkdtemp(), #'newfile.dat') tempf = tempfile.NamedTemporaryFile( dir=mmap_dir) data = np.memmap(tempf, dtype=obj.data.dtype, mode = 'w+', shape=stack_shape,) signal = type(obj)( {'data' : data}) # Store the temporary file in the signal class to # avoid its deletion when garbage collecting if tempf is not None: signal._data_temporary_file = tempf signal.axes_manager.axes[1:] = obj.axes_manager.axes signal.axes_manager._set_axes_index_in_array_from_position() eaxis = signal.axes_manager.axes[0] eaxis.name = 'stack_element' eaxis.navigate = True signal.mapped_parameters = obj.mapped_parameters signal.mapped_parameters.original_filename = '' signal.mapped_parameters.title = \ os.path.split(os.path.split( os.path.abspath(filenames[0]))[0])[1] signal.original_parameters = DictionaryBrowser({}) signal.original_parameters.add_node('stack_elements') if obj.data.shape != original_shape: raise IOError( "Only files with data of the same shape can be stacked") signal.data[i,...] = obj.data signal.original_parameters.stack_elements.add_node( 'element%i' % i) node = signal.original_parameters.stack_elements[ 'element%i' % i] node.original_parameters = \ obj.original_parameters.as_dictionary() node.mapped_parameters = \ obj.mapped_parameters.as_dictionary() del obj messages.information('Individual files loaded correctly') print signal objects = [signal,] else: objects=[load_single_file(filename, output_level=0,**kwds) for filename in filenames] if hyperspy.defaults_parser.preferences.General.plot_on_load: for obj in objects: obj.plot() if len(objects) == 1: objects = objects[0] return objects