Commit 737d571d authored by Somnath, Suhas's avatar Somnath, Suhas
Browse files

combined get_position_slicing and get_spectral_slicing into new function

get_aux_dset_slicing
parent f09d173b
import numpy as np
from collections import Iterable
from .microdata import MicroDataset
from .dtype_utils import contains_integers
import warnings
__all__ = ['build_ind_val_dsets', 'get_position_slicing', 'get_spectral_slicing', 'make_indices_matrix']
__all__ = ['build_ind_val_dsets', 'get_aux_dset_slicing', 'make_indices_matrix']
def build_ind_val_dsets(dimensions, is_spectral=True, steps=None, initial_values=None, labels=None,
......@@ -99,10 +100,10 @@ def build_ind_val_dsets(dimensions, is_spectral=True, steps=None, initial_values
mode = 'Spectroscopic_'
indices = indices.transpose()
values = values.transpose()
region_slices = get_spectral_slicing(labels)
else:
mode = 'Position_'
region_slices = get_position_slicing(labels)
region_slices = get_aux_dset_slicing(labels, is_spectroscopic=is_spectral)
# Create the MicroDatasets for both Indices and Values
ds_indices = MicroDataset(mode + 'Indices', indices, dtype=np.uint32)
......@@ -125,57 +126,35 @@ def build_ind_val_dsets(dimensions, is_spectral=True, steps=None, initial_values
return ds_indices, ds_values
def get_position_slicing(pos_lab, curr_pix=None):
def get_aux_dset_slicing(dim_names, last_ind=None, is_spectroscopic=False):
"""
Returns a dictionary of slice objects to help in creating region references
to the position indices and values H5 datasets
Returns a dictionary of slice objects to help in creating region references in the position or spectroscopic
indices and values datasets
Parameters
------------
pos_lab : List of strings
Labels of each of the position axes
curr_pix : (Optional) unsigned int
Last pixel in the positon matrix. Useful in experiments where the
parameters have changed (eg. BEPS new data format)
dim_names : iterable
List of strings denoting the names of the position axes or spectroscopic dimensions arranged in the same order
that matches the dimensions in the indices / values dataset
last_ind : (Optional) unsigned int, default = None
Last pixel in the positon or spectroscopic matrix. Useful in experiments where the
parameters have changed (eg. BEPS new data format) during the experiment.
is_spectroscopic : bool, optional. default = True
set to True for position datasets and False for spectroscopic datasets
Returns
------------
slice_dict : dictionary
Dictionary of tuples containing slice objects corresponding to
each position axis.
"""
assert isinstance(pos_lab, Iterable)
slice_dict = dict()
for spat_ind, spat_dim in enumerate(pos_lab):
slice_dict[spat_dim] = (slice(curr_pix), slice(spat_ind, spat_ind+1))
return slice_dict
def get_spectral_slicing(spec_lab, curr_spec=None):
"""
Returns a dictionary of slice objects to help in creating region references
to the spectroscopic indices and values H5 datasets
Parameters
------------
spec_lab : List of strings
Labels of each of the Spectroscopic axes
curr_spec : (Optional) unsigned int
Last position in the spectroscopic matrix. Useful in experiments where the
parameters have changed (eg. BEPS new data format)
Returns
------------
slice_dict : dictionary
Dictionary of tuples containing slice objects corresponding to
each Spectroscopic axis.
"""
assert isinstance(spec_lab, Iterable)
assert isinstance(dim_names, Iterable)
slice_dict = dict()
for spat_ind, spat_dim in enumerate(spec_lab):
slice_dict[spat_dim] = (slice(spat_ind, spat_ind + 1), slice(curr_spec))
for spat_ind, curr_dim_name in enumerate(dim_names):
val = (slice(last_ind), slice(spat_ind, spat_ind + 1))
if is_spectroscopic:
val = val[::-1]
slice_dict[str(curr_dim_name)] = val
return slice_dict
......@@ -198,8 +177,7 @@ def make_indices_matrix(num_steps, is_position=True):
indices_matrix : 2D unsigned int numpy array
arranged as [steps, spatial dimension]
"""
assert isinstance(num_steps, Iterable)
# assert np.all([isinstance(x, int) for x in num_steps])
assert contains_integers(num_steps, min_val=2)
num_steps = np.array(num_steps)
spat_dims = max(1, len(np.where(num_steps > 1)[0]))
......
......@@ -16,7 +16,7 @@ from scipy.io.matlab import loadmat # To load parameters stored in Matlab .mat
from .df_utils.be_utils import trimUDVS, getSpectroscopicParmLabel, generatePlotGroups, createSpecVals, maxReadPixels, \
nf32
from ...core.io.translator import Translator, generate_dummy_main_parms
from ...core.io.write_utils import make_indices_matrix, get_position_slicing
from ...core.io.write_utils import make_indices_matrix, get_aux_dset_slicing
from ...core.io.hdf_utils import get_h5_obj_refs
from ...core.io.hdf_writer import HDFwriter
from ...core.io.microdata import MicroDataGroup, MicroDataset
......@@ -124,7 +124,7 @@ class BEodfRelaxationTranslator(Translator):
self.FFT_BE_wave = bin_FFT
pos_mat = make_indices_matrix([num_cols, num_rows])
pos_slices = get_position_slicing(['X', 'Y'], num_pix)
pos_slices = get_aux_dset_slicing(['X', 'Y'], last_ind=num_pix, is_spectroscopic=False)
ds_ex_wfm = MicroDataset('Excitation_Waveform', ex_wfm)
ds_pos_ind = MicroDataset('Position_Indices', pos_mat, dtype=np.uint32)
......
......@@ -15,7 +15,7 @@ from scipy.io.matlab import loadmat # To load parameters stored in Matlab .mat
from .df_utils.gmode_utils import readGmodeParms
from ...core.io.translator import Translator, \
generate_dummy_main_parms # Because this class extends the abstract Translator class
from ...core.io.write_utils import make_indices_matrix, get_position_slicing
from ...core.io.write_utils import make_indices_matrix, get_aux_dset_slicing
from ...core.io.hdf_utils import get_h5_obj_refs, link_h5_objects_as_attrs
from ...core.io.hdf_writer import HDFwriter # Now the translator is responsible for writing the data.
# The building blocks for defining heirarchical storage in the H5 file
......@@ -84,7 +84,7 @@ class GDMTranslator(Translator):
num_pix = num_rows * num_cols
pos_mat = make_indices_matrix([num_cols, num_rows])
pos_slices = get_position_slicing(['X', 'Y'], num_pix)
pos_slices = get_aux_dset_slicing(['X', 'Y'], last_ind=num_pix, is_spectroscopic=False)
# Now start creating datasets and populating:
ds_pos_ind = MicroDataset('Position_Indices', np.uint32(pos_mat))
......
......@@ -17,8 +17,7 @@ from skimage.util import crop
from .df_utils.io_image import unnest_parm_dicts, read_dm3
from ...core.io.translator import Translator, generate_dummy_main_parms
from ...core.io.write_utils import make_indices_matrix, get_position_slicing, get_spectral_slicing, \
build_ind_val_dsets
from ...core.io.write_utils import make_indices_matrix, get_aux_dset_slicing, build_ind_val_dsets
from ...core.io.hdf_utils import get_h5_obj_refs, get_group_refs, calc_chunks, link_as_main
from ...core.io.hdf_writer import HDFwriter
from ...core.io.microdata import MicroDataGroup, MicroDataset
......@@ -185,7 +184,7 @@ class NDataTranslator(Translator):
ds_spec_inds = MicroDataset('Spectroscopic_Indices', spec_mat)
ds_spec_vals = MicroDataset('Spectroscopic_Values', spec_mat, dtype=np.float32)
spec_lab = get_spectral_slicing(['Image'])
spec_lab = get_aux_dset_slicing(['Image'], is_spectroscopic=True)
ds_spec_inds.attrs['labels'] = spec_lab
ds_spec_inds.attrs['units'] = ''
ds_spec_vals.attrs['labels'] = spec_lab
......@@ -194,7 +193,7 @@ class NDataTranslator(Translator):
ds_pos_inds = MicroDataset('Position_Indices', pos_mat)
ds_pos_vals = MicroDataset('Position_Values', pos_mat, dtype=np.float32)
pos_lab = get_position_slicing(['X', 'Y'])
pos_lab = get_aux_dset_slicing(['X', 'Y'], is_spectroscopic=False)
ds_pos_inds.attrs['labels'] = pos_lab
ds_pos_inds.attrs['units'] = ['pixel', 'pixel']
ds_pos_vals.attrs['labels'] = pos_lab
......
......@@ -16,8 +16,7 @@ from skimage.util import crop
from .df_utils import dm4reader
from .df_utils.io_image import read_image, read_dm3, parse_dm4_parms
from ...core.io.translator import Translator, generate_dummy_main_parms
from ...core.io.write_utils import make_indices_matrix, get_position_slicing, get_spectral_slicing, \
build_ind_val_dsets
from ...core.io.write_utils import make_indices_matrix, get_aux_dset_slicing, build_ind_val_dsets
from ...core.io.hdf_utils import get_h5_obj_refs, calc_chunks, link_as_main
from ...core.io.hdf_writer import HDFwriter
from ...core.io.microdata import MicroDataGroup, MicroDataset
......@@ -206,7 +205,7 @@ class OneViewTranslator(Translator):
ds_spec_inds = MicroDataset('Spectroscopic_Indices', spec_mat)
ds_spec_vals = MicroDataset('Spectroscopic_Values', spec_mat, dtype=np.float32)
spec_lab = get_spectral_slicing(['Image'])
spec_lab = get_aux_dset_slicing(['Image'], is_spectroscopic=True)
ds_spec_inds.attrs['labels'] = spec_lab
ds_spec_inds.attrs['units'] = ''
ds_spec_vals.attrs['labels'] = spec_lab
......@@ -215,7 +214,7 @@ class OneViewTranslator(Translator):
ds_pos_inds = MicroDataset('Position_Indices', pos_mat)
ds_pos_vals = MicroDataset('Position_Values', pos_mat, dtype=np.float32)
pos_lab = get_position_slicing(['X', 'Y'])
pos_lab = get_aux_dset_slicing(['X', 'Y'], is_spectroscopic=False)
ds_pos_inds.attrs['labels'] = pos_lab
ds_pos_inds.attrs['units'] = ['pixel', 'pixel']
ds_pos_vals.attrs['labels'] = pos_lab
......
......@@ -15,7 +15,7 @@ from scipy.io.matlab import loadmat # To load parameters stored in Matlab .mat
from ...core.io.translator import Translator, \
generate_dummy_main_parms # Because this class extends the abstract Translator class
from ...core.io.write_utils import make_indices_matrix, get_position_slicing
from ...core.io.write_utils import make_indices_matrix, get_aux_dset_slicing
from ...core.io.hdf_utils import get_h5_obj_refs, link_h5_objects_as_attrs
from ...core.io.hdf_writer import HDFwriter # Now the translator is responsible for writing the data.
# The building blocks for defining heirarchical storage in the H5 file
......@@ -62,7 +62,7 @@ class SporcTranslator(Translator):
num_pix = num_rows * num_cols
pos_mat = make_indices_matrix([num_cols, num_rows])
pos_slices = get_position_slicing(['X', 'Y'], num_pix)
pos_slices = get_aux_dset_slicing(['X', 'Y'], last_ind=num_pix, is_spectroscopic=False)
# new data format
spec_ind_mat = np.transpose(np.float32(spec_ind_mat))
......
......@@ -19,7 +19,7 @@ from ..core.io.hdf_utils import get_h5_obj_refs, copy_attributes, link_h5_object
from ..core.io.hdf_writer import HDFwriter
from ..core.io.io_utils import get_available_memory
from ..core.io.microdata import MicroDataGroup, MicroDataset
from ..core.io.write_utils import make_indices_matrix, get_position_slicing, get_spectral_slicing
from ..core.io.write_utils import make_indices_matrix, get_aux_dset_slicing
from .svd_utils import get_component_slice
windata32 = np.dtype({'names': ['Image Data'],
......@@ -417,12 +417,12 @@ class ImageWindow(object):
ds_pix_inds = MicroDataset('Spectroscopic_Indices', data=win_pix_mat, dtype=np.int32)
ds_pos_vals = MicroDataset('Position_Values', data=win_pos_mat, dtype=np.float32)
ds_pix_vals = MicroDataset('Spectroscopic_Values', data=win_pix_mat, dtype=np.float32)
pos_labels = get_position_slicing(['Window Origin X', 'Window Origin Y'])
pos_labels = get_aux_dset_slicing(['Window Origin X', 'Window Origin Y'], is_spectroscopic=False)
ds_pos_inds.attrs['labels'] = pos_labels
ds_pos_inds.attrs['units'] = ['pixel', 'pixel']
ds_pos_vals.attrs['labels'] = pos_labels
ds_pos_vals.attrs['units'] = ['pixel', 'pixel']
pix_labels = get_spectral_slicing(['U', 'V'])
pix_labels = get_aux_dset_slicing(['U', 'V'], is_spectroscopic=True)
ds_pix_inds.attrs['labels'] = pix_labels
ds_pix_inds.attrs['units'] = ['pixel', 'pixel']
ds_pix_vals.attrs['labels'] = pix_labels
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment