Skip to content

Commit

Permalink
Merge pull request #147 from pycroscopy/dev_mani
Browse files Browse the repository at this point in the history
Dev mani
  • Loading branch information
ramav87 authored Apr 29, 2022
2 parents 4ba28b0 + ff6eadb commit cc44744
Show file tree
Hide file tree
Showing 10 changed files with 990 additions and 76 deletions.
2 changes: 2 additions & 0 deletions extras_require.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mpi4py
pyqt5
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
'ipywidgets>=5.2.2',
'ipython>=5.1.0,<6;python_version<"3.3"', # IPython 6.0+ does not support Python < 3.5
'ipython>=6.0;python_version>="3.3"', # Beginning with IPython 6.0, Python 3.3 and above is required.
'sklearn',
'scipy'
]

setup(
Expand Down Expand Up @@ -66,8 +68,8 @@
include_package_data=True,
# https://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-dependencies
extras_require={
'MPI': ["mpi4py"],
'File_Widgets': ['pyqt5'],
'MPI': ["mpi4py"],
'File_Widgets': ['pyqt5']
},
# If there are data files included in your packages that need to be
# installed, specify them here. If using Python 2.6 or less, then these
Expand Down
3 changes: 2 additions & 1 deletion sidpy/proc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"""

from . import comp_utils
from . import fitter

__all__ = ['comp_utils']
__all__ = ['comp_utils', 'fitter']
445 changes: 445 additions & 0 deletions sidpy/proc/fitter.py

Large diffs are not rendered by default.

72 changes: 52 additions & 20 deletions sidpy/sid/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def hdf_close(self):
print(self.h5_dataset)

@classmethod
def from_array(cls, x, title='generic', chunks='auto', lock=False,
def from_array(cls, x, title='generic', chunks='auto', lock=False,
datatype='UNKNOWN', units='generic', quantity='generic',
modality='generic', source='generic', **kwargs):
"""
Expand Down Expand Up @@ -262,7 +262,7 @@ def like_data(self, data, title=None, chunks='auto', lock=False, **kwargs):
checkdims = kwargs.get('checkdims', True)

new_data = self.from_array(data, chunks=chunks, lock=lock)

new_data.data_type = self.data_type

# units
Expand All @@ -278,9 +278,9 @@ def like_data(self, data, title=None, chunks='auto', lock=False, **kwargs):
new_data.title = self.title
else:
new_data.title = self.title + '_new'
new_data.title = title_prefix+new_data.title+title_suffix

new_data.title = title_prefix + new_data.title + title_suffix

# quantity
if reset_quantity:
new_data.quantity = 'generic'
Expand All @@ -300,7 +300,7 @@ def like_data(self, data, title=None, chunks='auto', lock=False, **kwargs):
try:
scale = get_slope(self._axes[dim])
# axis = self._axes[dim].copy()
axis = Dimension(np.arange(new_data.shape[dim])*scale, self._axes[dim].name)
axis = Dimension(np.arange(new_data.shape[dim]) * scale, self._axes[dim].name)
axis.quantity = self._axes[dim].quantity
axis.units = self._axes[dim].units
axis.dimension_type = self._axes[dim].dimension_type
Expand Down Expand Up @@ -393,7 +393,7 @@ def __validate_dim(self, ind, name):
raise TypeError('Dimension must be an integer')
if (0 > ind) or (ind >= self.ndim):
raise IndexError('Dimension must be an integer between 0 and {}'
''.format(self.ndim-1))
''.format(self.ndim - 1))
for key, dim in self._axes.items():
if key != ind:
if name == dim.name:
Expand Down Expand Up @@ -571,7 +571,7 @@ def set_thumbnail(self, figure=None, thumbnail_size=128):

import imageio
# Thumbnail configurations for matplotlib
kwargs = {'figsize': (1,1), 'colorbar': False, 'set_title': False}
kwargs = {'figsize': (1, 1), 'colorbar': False, 'set_title': False}
view = self.plot(figure=figure, **kwargs)
for axis in view.axes:
axis.set_axis_off()
Expand Down Expand Up @@ -609,8 +609,8 @@ def get_extent(self, dimensions):
extent = []
for ind, dim in enumerate(dimensions):
temp = self._axes[dim].values
start = temp[0] - (temp[1] - temp[0])/2
end = temp[-1] + (temp[-1] - temp[-2])/2
start = temp[0] - (temp[1] - temp[0]) / 2
end = temp[-1] + (temp[-1] - temp[-2]) / 2
if ind == 1:
extent.append(end) # y-axis starts on top
extent.append(start)
Expand Down Expand Up @@ -950,7 +950,6 @@ def min(self, axis=None, keepdims=False, split_every=None, out=None):
checkdims=False)
return result, locals().copy()


@reduce_dims
def max(self, axis=None, keepdims=False, split_every=None, out=None):

Expand All @@ -974,9 +973,9 @@ def mean(self, axis=None, dtype=None, keepdims=False, split_every=None, out=None
split_every=split_every, out=out), title_prefix='mean_aggregate_',
checkdims=False)
return result, locals().copy()

@reduce_dims
def std(self, axis=None, dtype=None, keepdims=False, ddof = 0, split_every=None, out=None):
def std(self, axis=None, dtype=None, keepdims=False, ddof=0, split_every=None, out=None):

result = self.like_data(super().std(axis=axis, dtype=dtype, keepdims=keepdims,
ddof=0, split_every=split_every, out=out),
Expand All @@ -991,7 +990,7 @@ def var(self, axis=None, dtype=None, keepdims=False, ddof=0, split_every=None, o
ddof=ddof, split_every=split_every, out=out),
title_prefix='var_aggregate_', checkdims=False)
return result, locals().copy()

@reduce_dims
def argmin(self, axis=None, split_every=None, out=None):

Expand Down Expand Up @@ -1106,7 +1105,6 @@ def reshape(self, *shape, merge_chunks=True):
new_shape = shape[0]
else:
new_shape = shape
print(new_shape)
return super().reshape(*new_shape, merge_chunks)

@reduce_dims
Expand Down Expand Up @@ -1183,13 +1181,17 @@ def fold(self, dim_order=None, method=None):
----------
dim_order: List of lists or tuple of tuples
-Each element correponds to the order of axes in the corresponding
-Each element corresponds to the order of axes in the corresponding
new axis after the collapse
-Default: None
method: str
-'spaspec' collapses the original dataset to a 2D dataset, where
-'spaspec': collapses the original dataset to a 2D dataset, where
spatail dimensions form the zeroth axis and spectral dimensions
form the first axis
-'spa': combines all the spatial dimensions into a single dimension and
the combined dimension will be first
-'spec': combines all the spectral dimensions into a single dimension and
the combined dimension will be last
-Uses the user defined dim_order when set to None
-Default: None
Expand All @@ -1207,7 +1209,7 @@ def fold(self, dim_order=None, method=None):

dim_order_list = [list(x) for x in dim_order]

# Book-keeping for uncollapsing
# Book-keeping for unfolding
fold_attr = {'shape': self.shape, '_axes': self._axes.copy()}

if method == 'spaspec':
Expand All @@ -1223,6 +1225,34 @@ def fold(self, dim_order=None, method=None):
part of the last collapsed dimension')
dim_order_list[1].extend(dim)

if method == 'spa':
dim_order_list = [[]]
for dim, axis in self._axes.items():
if axis.dimension_type == DimensionType.SPATIAL:
dim_order_list[0].extend(dim)
else:
dim_order_list.append([dim])

if len(dim_order_list[0]) == 0:
raise NotImplementedError("No spatial dimensions found and the method is set to 'spa' ")
if len(dim_order_list[0]) == 1:
warnings.warn('Only one spatial dimension found\
Folding returns the original dataset')

if method == 'spec':
dim_order_list = [[]]
for dim, axis in self._axes.items():
if axis.dimension_type == DimensionType.Spectral:
dim_order_list[-1].extend(dim)
else:
dim_order_list.insert(-1, [dim])

if len(dim_order_list[-1]) == 0:
raise NotImplementedError("No spectral dimensions found and the method is set to 'spec'")
if len(dim_order_list[-1]) == 1:
warnings.warn('Only one spatial dimension found\
Folding returns the original dataset')

# We need the flattened list to transpose the original array
dim_order_flattened = [item for sublist in dim_order_list for item in sublist]

Expand Down Expand Up @@ -1280,8 +1310,10 @@ def unfold(self):
title=self.title.replace('folded_', ''), checkdims=False)

unfolded_dset._axes = {}
for i, dim in enumerate(old_axes):
unfolded_dset._axes[dim] = old_axes[dim].copy()
for i, dim in old_axes.items():
unfolded_dset.set_dimension(i, dim.copy())

del unfolded_dset.metadata['fold_attr']
return unfolded_dset

# Following methods are to be edited
Expand Down
103 changes: 60 additions & 43 deletions sidpy/viz/dataset_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,53 +316,67 @@ def __init__(self, dset, figure=None, **kwargs):

# self.axis = self.fig.add_axes([0.0, 0.2, .9, .7])
self.ind = 0
self.plot_fit_labels = False

self.number_of_slices = self.dset.shape[self.stack_dim]

if self.set_title:
if 'fit_dataset' in dir(dset):
if dset.fit_dataset:
if dset.metadata['fit_parms_dict']['fit_parameters_labels'] is not None:
self.plot_fit_labels = True
img_titles = dset.metadata['fit_parms_dict']['fit_parameters_labels']
self.image_titles = ['Fitting Parm: ' + img_titles[k] for k in range(len(img_titles))]
else:
self.image_titles = 'Fitting Maps: ' + dset.title + '\n use scroll wheel to navigate images'
else:
self.image_titles = 'Fitting Maps: ' + dset.title + '\n use scroll wheel to navigate images'
else:
self.image_titles = 'Image stack: ' + dset.title + '\n use scroll wheel to navigate images'

self.axis = None
self.plot_image(**kwargs)
self.axis = plt.gca()
if self.set_title:
self.axis.set_title('image stack: ' + dset.title + '\n use scroll wheel to navigate images')
self.img.axes.figure.canvas.mpl_connect('scroll_event', self._onscroll)


import ipywidgets as iwgt
self.play = iwgt.Play(
value=0,
min=0,
max=self.number_of_slices,
step=1,
interval=500,
description="Press play",
disabled=False
)
self.slider = iwgt.IntSlider(
value=0,
min=0,
max=self.number_of_slices,
continuous_update=False,
description="Frame:")
# set the slider function
iwgt.interactive(self._update, frame=self.slider)
# link slider and play function
iwgt.jslink((self.play, 'value'), (self.slider, 'value'))

# We add a button to average the images
self.button = iwgt.widgets.ToggleButton(
value=False,
description='Average',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Average Images of Stack')

self.button.observe(self._average_slices, 'value')

# set play and slider widgets next to each other
widg = iwgt.HBox([self.play, self.slider, self.button])
display(widg)

# self.anim = animation.FuncAnimation(self.fig, self._updatefig, interval=200, blit=False, repeat=True)
self._update()
#self.axis.set_title(image_titles)
self.img.axes.figure.canvas.mpl_connect('scroll_event', self._onscroll)

import ipywidgets as iwgt
self.play = iwgt.Play(
value=0,
min=0,
max=self.number_of_slices,
step=1,
interval=500,
description="Press play",
disabled=False
)
self.slider = iwgt.IntSlider(
value=0,
min=0,
max=self.number_of_slices,
continuous_update=False,
description="Frame:")
# set the slider function
iwgt.interactive(self._update, frame=self.slider)
# link slider and play function
iwgt.jslink((self.play, 'value'), (self.slider, 'value'))

# We add a button to average the images
self.button = iwgt.widgets.ToggleButton(
value=False,
description='Average',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Average Images of Stack')

self.button.observe(self._average_slices, 'value')

# set play and slider widgets next to each other
widg = iwgt.HBox([self.play, self.slider, self.button])
display(widg)

# self.anim = animation.FuncAnimation(self.fig, self._updatefig, interval=200, blit=False, repeat=True)
self._update()

def plot_image(self, **kwargs):

Expand Down Expand Up @@ -436,7 +450,10 @@ def _update(self, frame=0):
self.selection[self.stack_dim] = slice(frame, frame + 1)
self.img.set_data((self.dset[tuple(self.selection)].squeeze()).T)
self.img.axes.figure.canvas.draw_idle()

if self.plot_fit_labels:
self.axis.set_title(self.image_titles[frame])
else:
self.axis.set_title(self.image_titles)

class SpectralImageVisualizer(object):
"""
Expand Down
11 changes: 7 additions & 4 deletions tests/hdf/test_hdf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import division, print_function, unicode_literals, \
absolute_import
import unittest
import tempfile
import os
import sys
import tempfile
Expand All @@ -19,6 +20,7 @@
from sidpy.base.dict_utils import flatten_dict
from sidpy.hdf import hdf_utils


from . import data_utils

if sys.version_info.major == 3:
Expand Down Expand Up @@ -638,8 +640,9 @@ def test_wrong_type(self):
class TestValidateH5ObjsInSameFile(unittest.TestCase):

def test_diff_file(self):
file_path_1 = 'source.h5'
file_path_2 = 'sink.h5'
with tempfile.TemporaryDirectory() as tmp_dir:
file_path_1 = tmp_dir + 'source.h5'
file_path_2 = tmp_dir + 'sink.h5'
data_utils.delete_existing_file(file_path_1)
h5_f1 = h5py.File(file_path_1, mode='w')
h5_main = h5_f1.create_dataset('main', data=np.arange(5))
Expand All @@ -649,8 +652,8 @@ def test_diff_file(self):
with self.assertRaises(ValueError):
hdf_utils.validate_h5_objs_in_same_h5_file(h5_main, h5_other)

os.remove(file_path_1)
os.remove(file_path_2)
#os.remove(file_path_1)
#os.remove(file_path_2)

def test_same_file(self):
file_path = 'test_same_file.h5'
Expand Down
Loading

0 comments on commit cc44744

Please sign in to comment.