Skip to content

Commit

Permalink
Merge pull request #157 from pycroscopy/dev_mani
Browse files Browse the repository at this point in the history
changes to set_dimension
  • Loading branch information
saimani5 authored Oct 3, 2022
2 parents 60f9b58 + 0c6fba4 commit a5414e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
40 changes: 30 additions & 10 deletions sidpy/sid/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,13 @@ def rename_dimension(self, ind, name):
self.__validate_dim(ind, name)
if not isinstance(name, str):
raise TypeError('New Dimension name must be a string')
delattr(self, self._axes[ind].name)
if hasattr(self, self._axes[ind].name):
delattr(self, self._axes[ind].name)
if hasattr(self, 'dim_{}'.format(ind)):
delattr(self, 'dim_{}'.format(ind))
self._axes[ind].name = name
setattr(self, name, self._axes[ind])
setattr(self, 'dim_{}'.format(ind), self._axes[ind])

def set_dimension(self, ind, dimension):
"""
Expand All @@ -434,10 +438,26 @@ def set_dimension(self, ind, dimension):
if not isinstance(dimension, Dimension):
raise TypeError('dimension needs to be a sidpy.Dimension object')
self.__validate_dim(ind, dimension.name)
# delattr(self, self._axes[ind].name)
setattr(self, dimension.name, dimension)
setattr(self, 'dim_{}'.format(ind), dimension)
self._axes[ind] = dimension
if len(dimension.values) != self.shape[ind]:
raise ValueError('The length of the dimension array does not match the shape of the '
'dataset at {}th dimension. {} != {}'.format(ind, len(dimension.values), self.shape[ind])
)

dim = dimension.copy()

try:
if hasattr(self, self._axes[ind].name):
delattr(self, self._axes[ind].name)
except KeyError:
pass

setattr(self, dimension.name, dim)

if hasattr(self, 'dim_{}'.format(ind)):
delattr(self, 'dim_{}'.format(ind))

setattr(self, 'dim_{}'.format(ind), dim)
self._axes[ind] = dim

def view_metadata(self):
"""
Expand Down Expand Up @@ -1465,11 +1485,11 @@ def __array_ufunc__(self, numpy_ufunc, method, *inputs, **kwargs):
out = kwargs.get("out", ())

if method == "__call__":
if numpy_ufunc is np.matmul:
from dask.array.routines import matmul

# special case until apply_gufunc handles optional dimensions
return self.like_data(matmul(*inputs, **kwargs))
# if numpy_ufunc is np.matmul:
# from dask.array.routines import matmul
#
# # special case until apply_gufunc handles optional dimensions
# return self.like_data(matmul(*inputs, **kwargs))
if numpy_ufunc.signature is not None:
from dask.array.gufunc import apply_gufunc

Expand Down
2 changes: 1 addition & 1 deletion tests/sid/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def test_spectrum_image_fft(self):
dataset.y.units = 'nm'
dataset.y.quantity = 'distance'

dataset.set_dimension(2, sidpy.Dimension(np.arange(dataset.shape[0]) * .02, 'spec'))
dataset.set_dimension(2, sidpy.Dimension(np.arange(dataset.shape[2]) * .02, 'spec'))
dataset.spec.dimension_type = 'spectral'
dataset.spec.units = 'i'
dataset.spec.quantity = 'energy'
Expand Down

0 comments on commit a5414e3

Please sign in to comment.