Ejemplo n.º 1
0
def createHDF5_file(signal, parm_dict, h5_path='', ds_name='FF_Raw'):
	"""
	Generates the HDF5 file given path to a specific file and a parameters dictionary

	Parameters
	----------
	h5_path : string
		Path to desired h5 file.

	signal : str, ndarray
		Path to the data file to be converted or a workspace array

	parm_dict : dict
		Scan parameters

	Returns
	-------
	h5_path: str
		The filename path to the H5 file create

	"""

	sg = signal

	if 'str' in str(type(signal)):
		sg = load.signal(signal)

	if not any(h5_path):  # if not passed, auto-generate name
		fname = signal.replace('/', '\\')
		h5_path = fname[:-4] + '.h5'
	else:
		fname = h5_path

	hdf = px.ioHDF5(h5_path)
	usid.hdf_utils.print_tree(hdf.file)

	ff_group = px.MicroDataGroup('FF_Group', parent='/')
	root_group = px.MicroDataGroup('/')

	#    fname = fname.split('\\')[-1][:-4]
	sg = px.MicroDataset(ds_name, data=sg, dtype=np.float32, parent=ff_group)

	if 'pnts_per_pixel' not in parm_dict.keys():
		parm_dict['pnts_per_avg'] = signal.shape[1]
		parm_dict['pnts_per_pixel'] = 1
		parm_dict['pnts_per_line'] = parm_dict['num_cols']

	ff_group.addChildren([sg])
	ff_group.attrs = parm_dict

	# Get reference for writing the data
	h5_refs = hdf.writeData(ff_group, print_log=True)

	hdf.flush()
Ejemplo n.º 2
0
    def _create_results_datasets(self):
        """
        Creates the datasets an datagroups necessary to store the results.
        Just as the raw data is stored in the pycroscopy format, the results also need to conform to the same
        standards. Hence, the create_datasets function can appear to be a little longer than one might expect.
        """
        h5_spec_inds = px.hdf_utils.getAuxData(
            self.h5_main, auxDataName=['Spectroscopic_Indices'])[0]
        h5_spec_vals = px.hdf_utils.getAuxData(
            self.h5_main, auxDataName=['Spectroscopic_Values'])[0]

        self.step_start_inds = np.where(h5_spec_inds[0] == 0)[0]
        self.num_udvs_steps = len(self.step_start_inds)

        ds_guess = px.MicroDataset('Guess',
                                   data=[],
                                   maxshape=(self.h5_main.shape[0],
                                             self.num_udvs_steps),
                                   chunking=(1, self.num_udvs_steps),
                                   dtype=sho32)

        not_freq = px.hdf_utils.get_attr(h5_spec_inds, 'labels') != 'Frequency'

        ds_sho_inds, ds_sho_vals = px.hdf_utils.buildReducedSpec(
            h5_spec_inds, h5_spec_vals, not_freq, self.step_start_inds)

        dset_name = self.h5_main.name.split('/')[-1]
        sho_grp = px.MicroDataGroup('-'.join([dset_name, 'SHO_Fit_']),
                                    self.h5_main.parent.name[1:])
        sho_grp.addChildren([ds_guess, ds_sho_inds, ds_sho_vals])
        sho_grp.attrs['SHO_guess_method'] = "pycroscopy BESHO"

        h5_sho_grp_refs = self.hdf.writeData(sho_grp)

        self.h5_guess = px.hdf_utils.getH5DsetRefs(['Guess'],
                                                   h5_sho_grp_refs)[0]
        self.h5_results_grp = self.h5_guess.parent
        h5_sho_inds = px.hdf_utils.getH5DsetRefs(['Spectroscopic_Indices'],
                                                 h5_sho_grp_refs)[0]
        h5_sho_vals = px.hdf_utils.getH5DsetRefs(['Spectroscopic_Values'],
                                                 h5_sho_grp_refs)[0]

        # Reference linking before actual fitting
        px.hdf_utils.linkRefs(self.h5_guess, [h5_sho_inds, h5_sho_vals])
        # Linking ancillary position datasets:
        aux_dsets = px.hdf_utils.getAuxData(
            self.h5_main, auxDataName=['Position_Indices', 'Position_Values'])
        px.hdf_utils.linkRefs(self.h5_guess, aux_dsets)
        print('Finshed creating datasets')
import numpy as np
import pycroscopy as px

##############################################################################
# Create some MicroDatasets and MicroDataGroups that will be written to the file.
# With h5py, groups and datasets must be created from the top down,
# but the Microdata objects allow us to build them in any order and link them later.

# First create some data
data1 = np.random.rand(5, 7)

##############################################################################
# Now use the array to build the dataset.  This dataset will live
# directly under the root of the file.  The MicroDataset class also implements the
# compression and chunking parameters from h5py.Dataset.
ds_main = px.MicroDataset('Main_Data', data=data1, parent='/')

##############################################################################
# We can also create an empty dataset and write the values in later
# With this method, it is neccessary to specify the dtype and maxshape kwarg parameters.
ds_empty = px.MicroDataset('Empty_Data',
                           data=[],
                           dtype=np.float32,
                           maxshape=[7, 5, 3])

##############################################################################
# We can also create groups and add other MicroData objects as children.
# If the group's parent is not given, it will be set to root.
data_group = px.MicroDataGroup('Data_Group', parent='/')

root_group = px.MicroDataGroup('/')
ds_labels_spec_vals.name = 'Label_Spectroscopic_Values'
ds_cluster_inds.name = 'Cluster_Indices'
ds_cluster_vals.name = 'Cluster_Values'

print('Spectroscopic Dataset for Labels', ds_labels_spec_inds.shape)
print('Position Dataset for Centroids', ds_cluster_inds.shape)
print('Centroids', centroids.shape)
print('Labels', labels_mat.shape)

###############################################################################
# Create the Main MicroDataset objects
# ====================================
# Remember that it is important to either inherit or add the `quantity` and `units` attributes to each **main** dataset

# The two main datasets
ds_label_mat = px.MicroDataset('Labels', labels_mat, dtype=np.uint32)
# Adding the mandatory attributes
ds_label_mat.attrs = {'quantity': 'Cluster ID', 'units': 'a. u.'}

ds_cluster_centroids = px.MicroDataset('Mean_Response',
                                       centroids,
                                       dtype=h5_main.dtype)
# Inhereting / copying the mandatory attributes
px.hdf_utils.copy_main_attributes(h5_main, ds_cluster_centroids)

###############################################################################
# Create the group that will contain these datasets
# =================================================
# We will be appending data to the existing h5 file and since HDF5 uses a tree structure to store information, we
# would need to specify where to add the sub-tree that we are building.
#