Skip to content

Commit

Permalink
Merge pull request #155 from pycroscopy/dev_rama
Browse files Browse the repository at this point in the history
Dev rama
  • Loading branch information
ramav87 authored Oct 3, 2022
2 parents 1bada80 + 5857522 commit 60f9b58
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
3 changes: 2 additions & 1 deletion sidpy/sid/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DimensionType(Enum):
RECIPROCAL = 2
SPECTRAL = 3
TEMPORAL = 4
CHANNEL = 5


class Dimension(np.ndarray):
Expand All @@ -53,7 +54,7 @@ def __new__(cls, values, name='none', quantity='generic', units='generic',
set of values will be generated if an integer is provided instead
of an array.
dimension_type : str or sidpy.sid.dimension.DimensionType
For example: 'spectral', 'spatial', 'reciprocal', or 'UNKNOWN'
For example: 'spectral', 'spatial', 'reciprocal', 'channel', or 'UNKNOWN',
'time', 'frame', 'reciprocal'
This will determine how the data are visualized. 'spatial' are
image dimensions. 'spectral' indicate spectroscopy data dimensions.
Expand Down
60 changes: 46 additions & 14 deletions sidpy/viz/dataset_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ class SpectralImageVisualizer(object):
"""
### Interactive spectrum imaging plot
If there is a 4D dataset, and one of them is named 'channel',
then you can plot the channel spectra too
"""

def __init__(self, dset, figure=None, horizontal=True, **kwargs):
Expand All @@ -485,26 +488,42 @@ def __init__(self, dset, figure=None, horizontal=True, **kwargs):

if len(dset.shape) < 3:
raise TypeError('dataset must have at least three dimensions')
if len(dset.shape)>4:
raise TypeError('dataset must have at most four dimensions')

# We need one stack dim and two image dimes as lists in dictionary

selection = []
image_dims = []
spectral_dims = []
spectral_dim = []
channel_dim = []
for dim, axis in dset._axes.items():
if axis.dimension_type in [DimensionType.SPATIAL, DimensionType.RECIPROCAL]:
selection.append(slice(None))
image_dims.append(dim)
elif axis.dimension_type == DimensionType.SPECTRAL:
selection.append(slice(0, 1))
spectral_dims.append(dim)
spectral_dim.append(dim)
elif axis.dimension_type == DimensionType.CHANNEL:
channel_dim.append(dim)
else:
selection.append(slice(0, 1))

if len(image_dims) != 2:
raise TypeError('We need two dimensions with dimension_type SPATIA: to plot an image')

if len(spectral_dims) != 1:
raise TypeError('We need one dimension with dimension_type SPECTRAL for a spectral image plot')
raise TypeError('We need two dimensions with dimension_type SPATIAL: to plot an image')
if len(channel_dim) >1:
raise ValueError("We have more than one Channel Dimension, this won't work for the visualizer")
if len(spectral_dim)>1:
raise ValueError("We have more than one Spectral Dimension, this won't work for the visualizer...")

if len(dset.shape)==4:
if len(channel_dim)!=1:
raise TypeError("We need one dimension with type CHANNEL \
for a spectral image plot for a 4D dataset")
elif len(dset.shape)==3:
if len(spectral_dim) != 1:
raise TypeError("We need one dimension with dimension_type SPECTRAL \
to plot a spectra for a 3D dataset")

self.horizontal = horizontal
self.x = 0
Expand All @@ -516,8 +535,9 @@ def __init__(self, dset, figure=None, horizontal=True, **kwargs):
size_y = dset.shape[image_dims[1]]

self.dset = dset
self.energy_scale = dset._axes[spectral_dims[0]].values

self.energy_axis = spectral_dim[0]
if len(channel_dim)>0: self.channel_axis = channel_dim
self.energy_scale = dset._axes[self.energy_axis].values
self.extent = [0, size_x, size_y, 0]
self.rectangle = [0, size_x, 0, size_y]
self.scaleX = 1.0
Expand All @@ -526,7 +546,7 @@ def __init__(self, dset, figure=None, horizontal=True, **kwargs):
self.plot_legend = False

self.image_dims = image_dims
self.spec_dim = spectral_dims[0]
self.spec_dim = spectral_dim[0]

if horizontal:
self.axes = self.fig.subplots(ncols=2)
Expand All @@ -535,7 +555,12 @@ def __init__(self, dset, figure=None, horizontal=True, **kwargs):

if self.set_title:
self.fig.canvas.manager.set_window_title(self.dset.title)
self.image = dset.mean(axis=spectral_dims[0])

if len(channel_dim)>0:
self.image = dset.mean(axis=(spectral_dim[0],channel_dim[0]))
else:
self.image = dset.mean(axis=(spectral_dim[0]))

if 1 in self.dset.shape:
self.image = dset.squeeze()
# self.extent[2]=self.image.shape[1]
Expand Down Expand Up @@ -563,7 +588,8 @@ def __init__(self, dset, figure=None, horizontal=True, **kwargs):
self.axes[0].add_patch(self.rect)
self.intensity_scale = 1.
self.spectrum = self.get_spectrum()

if len(self.energy_scale)!=self.spectrum.shape[0]:
self.spectrum = self.spectrum.T
self.axes[1].plot(self.energy_scale, self.spectrum)
self.axes[1].set_title('spectrum {}, {}'.format(self.x, self.y))
self.xlabel = self.dset.labels[self.spec_dim]
Expand Down Expand Up @@ -615,7 +641,7 @@ def get_spectrum(self):
selection = []

for dim, axis in self.dset._axes.items():
# print(dim, axis.dimension_type)
print(dim, axis.dimension_type)
if axis.dimension_type == DimensionType.SPATIAL:
if dim == self.image_dims[0]:
selection.append(slice(self.x, self.x + self.bin_x))
Expand All @@ -624,10 +650,13 @@ def get_spectrum(self):

elif axis.dimension_type == DimensionType.SPECTRAL:
selection.append(slice(None))
elif axis.dimension_type == DimensionType.CHANNEL:
selection.append(slice(None))
else:
selection.append(slice(0, 1))

self.spectrum = self.dset[tuple(selection)].mean(axis=tuple(self.image_dims))

# * self.intensity_scale[self.x,self.y]
return self.spectrum.squeeze()

Expand Down Expand Up @@ -674,7 +703,8 @@ def _update(self, ev=None):
ylim = self.axes[1].get_ylim()
self.axes[1].clear()
self.get_spectrum()

if len(self.energy_scale)!=self.spectrum.shape[0]:
self.spectrum = self.spectrum.T
self.axes[1].plot(self.energy_scale, self.spectrum, label='experiment')

if self.set_title:
Expand Down Expand Up @@ -964,6 +994,8 @@ def get_xy(self):
return [self.x, self.y]


#Let's make a

#Let's make a curve fit visualizer

class SpectralImageFitVisualizer(SpectralImageVisualizer):
Expand Down
4 changes: 2 additions & 2 deletions sidpy/viz/plot_utils/curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def plot_complex_spectra(map_stack, x_vec=None, num_comps=4, title=None, x_label
fig, axes = plt.subplots(nrows * 2, ncols, figsize=figsize)
fig.subplots_adjust(hspace=0.1, wspace=0.4)
if title is not None:
fig.canvas.set_window_title(title)
fig.canvas.manager.set_window_title(title)
fig.suptitle(title, y=1.025)

title_prefix = ''
Expand Down Expand Up @@ -528,6 +528,6 @@ def plot_scree(scree, title='Scree', **kwargs):
axis.set_title(title)
axis.set_xlim(left=1, right=len(scree))
axis.set_ylim(bottom=np.min(scree), top=np.max(scree))
fig.canvas.set_window_title(title)
fig.canvas.manager.set_window_title(title)

return fig, axis
2 changes: 1 addition & 1 deletion tests/viz/test_plot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ def test_scree_wrong(self):

def test_scree_h5py_dataset(self):
h5_f = h5py.File('test12.h5', 'a')
scree = h5_f.create_dataset("test", data=np.arange(1,25))
scree = h5_f.create_dataset("test12", data=np.arange(1,25))
plot_utils.plot_scree(scree)

"""
Expand Down

0 comments on commit 60f9b58

Please sign in to comment.