Skip to content

Commit

Permalink
Merge pull request #161 from pycroscopy/use_dict3
Browse files Browse the repository at this point in the history
Use dict3
  • Loading branch information
gduscher authored Feb 26, 2024
2 parents 3b18a5f + efca3e2 commit 5a97200
Show file tree
Hide file tree
Showing 6 changed files with 1,123 additions and 531 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,5 @@ _autosummary/pyTEMlib.sidpy_tools.rst
_autosummary/pyTEMlib.simulation_tools.rst
_autosummary/pyTEMlib.version.rst
_autosummary/pyTEMlib.xrpa_x_sections.rst
notebooks/Imaging/Adaptive_Fourier_Filter-Copy1.ipynb
notebooks/Spectroscopy/EELS_286.hf5
370 changes: 361 additions & 9 deletions notebooks/Imaging/Adaptive_Fourier_Filter.ipynb

Large diffs are not rendered by default.

686 changes: 553 additions & 133 deletions notebooks/Spectroscopy/Analyse_Low_Loss.ipynb

Large diffs are not rendered by default.

501 changes: 129 additions & 372 deletions notebooks/Spectroscopy/EDS.ipynb

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions pyTEMlib/file_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ class FileWidget(ipywidgets.DOMWidget):
"""

def __init__(self, dir_name=None, extension=['*']):
def __init__(self, dir_name=None, extension=['*'], sum_frames=False):
self.save_path = False
self.dir_dictionary = {}
self.dir_list = ['.', '..']
self.display_list = ['.', '..']
self.sum_frames = sum_frames

self.dir_name = '.'
if dir_name is None:
Expand Down Expand Up @@ -148,7 +149,7 @@ def select_main(self, value=0):
self.datasets = {}
#self.loaded_datasets.value = self.dataset_list[0]
self.dataset_list = []
self.datasets = open_file(self.file_name)
self.datasets = open_file(self.file_name, sum_frames=self.sum_frames)
self.dataset_list = []
for key in self.datasets.keys():
self.dataset_list.append(f'{key}: {self.datasets[key].title}')
Expand Down Expand Up @@ -593,7 +594,7 @@ def h5_group_to_dict(group, group_dict={}):
return group_dict


def open_file(filename=None, h5_group=None, write_hdf_file=False): # save_file=False,
def open_file(filename=None, h5_group=None, write_hdf_file=False, sum_frames=False): # save_file=False,
"""Opens a file if the extension is .hf5, .ndata, .dm3 or .dm4
If no filename is provided the QT open_file windows opens (if QT_available==True)
Expand Down Expand Up @@ -680,7 +681,7 @@ def open_file(filename=None, h5_group=None, write_hdf_file=False): # save_file
print('This file type needs hyperspy to be installed to be able to be read')
return
elif extension == '.emd':
reader = SciFiReaders.EMDReader(filename)
reader = SciFiReaders.EMDReader(filename, sum_frames=sum_frames)

elif 'edax' in extension.lower():
if 'h5' in extension:
Expand Down
86 changes: 73 additions & 13 deletions pyTEMlib/image_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,15 +731,27 @@ def onmove(self, event):
self.line_verts[moved_point] = self.new_point
self.set_linewidth()

def get_profile(dataset, line):
def get_profile(dataset, line, spline_order=-1):
"""
This function extracts a line profile from a given dataset. The line profile is a representation of the data values
along a specified line in the dataset. This function works for both image and spectral image data types.
Args:
dataset (sidpy.Dataset): The input dataset from which to extract the line profile.
line (list): A list specifying the line along which the profile should be extracted.
spline_order (int, optional): The order of the spline interpolation to use. Default is -1, which means no interpolation.
Returns:
profile_dataset (sidpy.Dataset): A new sidpy.Dataset containing the line profile.
"""
xv, yv = get_line_selection_points(line)


if dataset.data_type.name == 'IMAGE':
dataset.get_image_dims()
xv /= (dataset.x[1] - dataset.x[0])
yv /= (dataset.y[1] - dataset.y[0])
profile = scipy.ndimage.map_coordinates(np.array(dataset), [xv,yv])
profile = scipy.ndimage.map_coordinates(np.array(dataset), [xv, yv])

profile_dataset = sidpy.Dataset.from_array(profile.sum(axis=0))
profile_dataset.data_type='spectrum'
Expand All @@ -753,19 +765,21 @@ def get_profile(dataset, line):

if dataset.data_type.name == 'SPECTRAL_IMAGE':
spectral_axis = dataset.get_spectral_dims(return_axis=True)[0]
profile = np.zeros([xv.shape[1], 2, len(spectral_axis)])
data =np.array(dataset)

for index_x in range(xv.shape[1]):
for index_y in range(xv.shape[0]):
x = xv[index_y, index_x]
y = yv[index_y, index_x]
profile[index_x, 0] +=data[int(x),int(y)]
if spline_order > -1:
xv, yv, zv = get_line_selection_points_interpolated(line, z_length=dataset.shape[2])
profile = scipy.ndimage.map_coordinates(np.array(dataset), [xv, yv, zv], order=spline_order)
profile = profile.sum(axis=0)
profile = np.stack([profile, profile], axis=1)
start = xv[0, 0, 0]
else:
profile = get_line_profile(np.array(dataset), xv, yv, len(spectral_axis))
start = xv[0, 0]
print(profile.shape)
profile_dataset = sidpy.Dataset.from_array(profile)
profile_dataset.data_type='spectral_image'
profile_dataset.units = dataset.units
profile_dataset.quantity = dataset.quantity
profile_dataset.set_dimension(0, sidpy.Dimension(np.linspace(xv[0,0], xv[-1,-1], profile_dataset.shape[0]),
profile_dataset.set_dimension(0, sidpy.Dimension(np.arange(profile_dataset.shape[0])+start,
name='x', units=dataset.x.units, quantity=dataset.x.quantity,
dimension_type='spatial'))
profile_dataset.set_dimension(1, sidpy.Dimension([0, 1],
Expand All @@ -776,6 +790,42 @@ def get_profile(dataset, line):
return profile_dataset



def get_line_selection_points_interpolated(line, z_length=1):

start_point = line.line_verts[3]
right_point = line.line_verts[0]
low_point = line.line_verts[2]

if start_point[0] > right_point[0]:
start_point = line.line_verts[0]
right_point = line.line_verts[3]
low_point = line.line_verts[1]
m = (right_point[1] - start_point[1]) / (right_point[0] - start_point[0])
length_x = int(abs(start_point[0]-right_point[0]))
length_v = int(np.linalg.norm(start_point-right_point))

linewidth = int(abs(start_point[1]-low_point[1]))
x = np.linspace(0,length_x, length_v)
y = np.linspace(0,linewidth, line.line_width)
if z_length > 1:
z = np.linspace(0, z_length, z_length)
xv, yv, zv = np.meshgrid(x, y, np.arange(z_length))
x = np.atleast_2d(x).repeat(z_length, axis=0).T
y = np.atleast_2d(y).repeat(z_length, axis=0).T
else:
xv, yv = np.meshgrid(x, y)


yv = yv + x*m + start_point[1]
xv = (xv.swapaxes(0,1) -y*m ).swapaxes(0,1) + start_point[0]

if z_length > 1:
return xv, yv, zv
else:
return xv, yv


def get_line_selection_points(line):

start_point = line.line_verts[3]
Expand All @@ -801,6 +851,16 @@ def get_line_selection_points(line):
return xx, yy


def get_line_profile(data, xv, yv, z_length):
profile = np.zeros([len(xv[0]), 2, z_length])
for index_x in range(xv.shape[1]):
for index_y in range(xv.shape[0]):
x = int(xv[index_y, index_x])
y = int(yv[index_y, index_x])
if x< data.shape[0] and x>0 and y < data.shape[1] and y>0:
profile[index_x, 0] +=data[x, y]
return profile


def histogram_plot(image_tags):
"""interactive histogram"""
Expand Down

0 comments on commit 5a97200

Please sign in to comment.