Skip to content

Commit

Permalink
eels_tools clean up
Browse files Browse the repository at this point in the history
Cleaned up eels-tools
Added Austin's parallelization
Improved interactive eels dash board
  • Loading branch information
gduscher committed Jan 8, 2024
1 parent e5aabb6 commit 2c29c9c
Show file tree
Hide file tree
Showing 15 changed files with 5,514 additions and 2,172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,4 @@ notebooks/Imaging/data/lineweight.txt
.pyTEMlib.files.pkl
notebooks/EELS/relax.csv
Untitled.ipynb
example_data/GOLD-NP-DIFF-2-3.hf5
170 changes: 122 additions & 48 deletions notebooks/EELS/Analyse_Low_Loss.ipynb

Large diffs are not rendered by default.

635 changes: 460 additions & 175 deletions notebooks/EELS/Analysis_Core_Loss.ipynb

Large diffs are not rendered by default.

30 changes: 26 additions & 4 deletions notebooks/EELS/EDS.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,14 @@
],
"source": [
"import sys\n",
"import importlib.metadata \n",
"import importlib.metadata\n",
"\n",
"def test_package(package_name):\n",
" \"\"\"Test if package exists and returns version or -1\"\"\"\n",
" try:\n",
" version = importlib.metadata.version(package_name)\n",
" except (DistributionNotFound, ImportError) as err:\n",
" except importlib.metadata.PackageNotFoundError:\n",
" version = -1\n",
"\n",
" return version\n",
"\n",
"\n",
Expand Down Expand Up @@ -272,6 +271,29 @@
"__notebook_version__ = '2023_1_20'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4 5 6\n"
]
}
],
"source": [
"l = [4,5,6]\n",
"l[0::1]\n",
"\n",
"def f(h,k,l):\n",
" print(h,k,l)\n",
"\n",
"f(*l)"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -6606,7 +6628,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
"version": "3.11.7"
},
"toc": {
"base_numbering": "2",
Expand Down
795 changes: 756 additions & 39 deletions notebooks/Imaging/Register_Image_Stack.ipynb

Large diffs are not rendered by default.

138 changes: 120 additions & 18 deletions pyTEMlib/eels_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@


import numpy as np
import warnings

import ipywidgets
import IPython.display
from IPython.display import display
# from IPython.display import display
import matplotlib
import matplotlib.pylab as plt
import matplotlib.patches as patches
Expand Down Expand Up @@ -87,7 +88,11 @@ def __init__(self, datasets=None):
initial_elements=initial_elements)
self.pt_dialog.signal_selected[list].connect(self.set_elements)

self.dataset.plot()
if self.dataset.data_type.name =='SPECTRAL_IMAGE':
self.view = eels_dialog_utilities.SIPlot(self.dataset)
else:
self.view = eels_dialog_utilities.SpectrumPlot(self.dataset)
self.dataset.view = self.view

if hasattr(self.dataset.view, 'axes'):
self.axis = self.dataset.view.axes[-1]
Expand Down Expand Up @@ -811,7 +816,7 @@ def onpick(self, event):
self.fig.canvas.draw()

def get_sidebar():
side_bar = ipywidgets.GridspecLayout(13, 3,width='auto', grid_gap="0px")
side_bar = ipywidgets.GridspecLayout(14, 3,width='auto', grid_gap="0px")


row = 0
Expand Down Expand Up @@ -896,23 +901,33 @@ def get_sidebar():
tooltip='Changes y-axis to probability of flux is given',
layout=ipywidgets.Layout(width='100px')
)
return side_bar


row += 1
side_bar[row,0] = ipywidgets.ToggleButton(
description='Do All',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Fits all spectra of spectrum image',
layout=ipywidgets.Layout(width='100px')
)

import ipywidgets
side_bar[row,1] = ipywidgets.IntProgress(value=0, min=0, max=10, description=' ', bar_style='', # 'success', 'info', 'warning', 'danger' or ''
style={'bar_color': 'maroon'}, orientation='horizontal')
return side_bar


class CompositionWidget(object):
def __init__(self, datasets=None, index=0):
def __init__(self, datasets=None, key=None):

if not isinstance(datasets, dict):
raise TypeError('dataset or first item has to be a sidpy dataset')
self.datasets = datasets
self.dataset = datasets[list(datasets)[0]]


self.model = []
self.sidebar = get_sidebar()

self.set_dataset()
self.set_dataset(key)

self.periodic_table = eels_dialog_utilities.PeriodicTableWidget(self.energy_scale)
self.elements_cancel_button = ipywidgets.Button(description='Cancel')
Expand All @@ -931,7 +946,7 @@ def __init__(self, datasets=None, index=0):
pane_widths=[4, 10, 0],
)
self.set_action()
display(self.app_layout)
IPython.display.display(self.app_layout)


def line_select_callback(self, x_min, x_max):
Expand All @@ -947,7 +962,7 @@ def plot(self, scale=True):
self.energy_scale = self.dataset.energy_loss.values

if self.dataset.data_type == sidpy.DataType.SPECTRAL_IMAGE:
spectrum = self.dataset.view.get_spectrum()
spectrum = self.view.get_spectrum()
else:
spectrum = self.dataset
if len(self.model) > 1:
Expand Down Expand Up @@ -1012,7 +1027,24 @@ def show_edges(self):



def set_dataset(self, index=0):
def set_dataset(self, set_key):
spectrum_list = []
self.spectrum_keys_list = []
reference_list =[('None', -1)]

for index, key in enumerate(self.datasets.keys()):
if 'Reference' not in key:
if 'SPECTR' in self.datasets[key].data_type.name:
spectrum_list.append((f'{key}: {self.datasets[key].title}', index))
self.spectrum_keys_list.append(key)
reference_list.append((f'{key}: {self.datasets[key].title}', index))

if set_key in self.spectrum_keys_list:
self.key = set_key
else:
self.key = self.spectrum_keys_list[-1]
self.dataset = self.datasets[self.key]

spec_dim = self.dataset.get_dimensions_by_type(sidpy.DimensionType.SPECTRAL)
self.spec_dim = self.dataset._axes[spec_dim[0]]

Expand Down Expand Up @@ -1285,7 +1317,7 @@ def do_fit(self, value=0):
edges = eels.make_cross_sections(self.edges, np.array(self.energy_scale), beam_kv, eff_beta, self.low_loss)

if self.dataset.data_type == sidpy.DataType.SPECTRAL_IMAGE:
spectrum = self.dataset.view.get_spectrum()
spectrum = self.view.get_spectrum()
else:
spectrum = self.dataset
self.edges = eels.fit_edges2(spectrum, self.energy_scale, edges)
Expand All @@ -1303,6 +1335,76 @@ def do_fit(self, value=0):
self.model = self.edges['model']['spectrum']
self.update()
self.plot()

def do_all_button_click(self, value=0):
if self.sidebar[13,0].value==False:
return

if self.dataset.data_type.name != 'SPECTRAL_IMAGE':
self.do_fit()
return

if 'experiment' in self.dataset.metadata:
exp = self.dataset.metadata['experiment']
if 'convergence_angle' not in exp:
raise ValueError('need a convergence_angle in experiment of metadata dictionary ')
alpha = exp['convergence_angle']
beta = exp['collection_angle']
beam_kv = exp['acceleration_voltage']
else:
raise ValueError('need a experiment parameter in metadata dictionary')

eff_beta = eels.effective_collection_angle(self.energy_scale, alpha, beta, beam_kv)

self.low_loss = None
if self.sidebar[12, 1].value:
for key in self.datasets.keys():
if key != self.key:
if isinstance(self.datasets[key], sidpy.Dataset):
if 'SPECTR' in self.datasets[key].data_type.name:
if self.datasets[key].energy_loss[0] < 0:
self.low_loss = self.datasets[key]/self.datasets[key].sum()

edges = eels.make_cross_sections(self.edges, np.array(self.energy_scale), beam_kv, eff_beta, self.low_loss)

view = self.view
bin_x = view.bin_x
bin_y = view.bin_y

start_x = view.x
start_y = view.y

number_of_edges = 0
for key in self.edges:
if key.isdigit():
number_of_edges += 1

results = np.zeros([int(self.dataset.shape[0]/bin_x), int(self.dataset.shape[1]/bin_y), number_of_edges])
total_spec = int(self.dataset.shape[0]/bin_x)*int(self.dataset.shape[1]/bin_y)
self.sidebar[13,1].max = total_spec
#self.ui.progress.setMaximum(total_spec)
#self.ui.progress.setValue(0)
ind = 0
for x in range(int(self.dataset.shape[0]/bin_x)):
for y in range(int(self.dataset.shape[1]/bin_y)):
ind += 1
self.sidebar[13,1].value = ind
view.x = x*bin_x
view.y = y*bin_y
spectrum = view.get_spectrum()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
edges = eels.fit_edges2(spectrum, self.energy_scale, edges)
for key, edge in edges.items():
if key.isdigit():
# element.append(edge['element'])
results[x, y, int(key)] = edge['areal_density']
edges['spectrum_image_quantification'] = results
self.sidebar[13,1].value = total_spec
view.x = start_x
view.y = start_y
self.sidebar[13,0].value = False


def modify_onset(self, value=-1):
edge_index = self.sidebar[4, 0].value
Expand Down Expand Up @@ -1348,7 +1450,7 @@ def set_action(self):
self.sidebar[2, 0].observe(self.set_fit_area, names='value')

self.sidebar[3, 0].on_click(self.find_elements)
self.sidebar[4, 0].observe(self.update)
self.sidebar[4, 0].observe(self.update, names='value')
self.sidebar[5, 0].observe(self.set_element, names='value')

self.sidebar[7, 0].observe(self.modify_onset, names='value')
Expand All @@ -1357,10 +1459,10 @@ def set_action(self):
self.sidebar[10, 0].observe(self.modify_areal_density, names='value')

self.sidebar[11, 0].on_click(self.do_fit)
self.sidebar[12, 2].observe(self.plot)
self.sidebar[0, 0].observe(self.plot)

self.sidebar[12,0].observe(self.set_y_scale)
self.sidebar[12, 2].observe(self.plot, names='value')
self.sidebar[0, 0].observe(self.plot, names='value')
self.sidebar[12,0].observe(self.set_y_scale, names='value')
self.sidebar[13,0].observe(self.do_all_button_click, names='value')

self.elements_cancel_button.on_click(self.set_figure_pane)
self.elements_auto_button.on_click(self.auto_id)
Expand Down
33 changes: 17 additions & 16 deletions pyTEMlib/eels_dialog_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,10 @@ def __init__(self, dset, spectrum_number=0, figure=None, **kwargs):
self.figure.canvas.toolbar_visible = True

super().__init__(dset, spectrum_number=spectrum_number, figure=self.figure, **kwargs)

try:
self.dataset = self.dset
except:
pass
self.start_cursor = ipywidgets.FloatText(value=0, description='Start:', disabled=False, color='black', layout=ipywidgets.Layout(width='200px'))
self.end_cursor = ipywidgets.FloatText(value=0, description='End:', disabled=False, color='black', layout=ipywidgets.Layout(width='200px'))
self.panel = ipywidgets.VBox([ipywidgets.HBox([ipywidgets.Label('',layout=ipywidgets.Layout(width='100px')), ipywidgets.Label('Cursor:'),
Expand All @@ -583,26 +586,26 @@ def __init__(self, dset, spectrum_number=0, figure=None, **kwargs):
def line_select_callback(self, x_min, x_max):
self.start_cursor.value = np.round(x_min, 3)
self.end_cursor.value = np.round(x_max, 3)
self.start_channel = np.searchsorted(self.dset.energy_loss, self.start_cursor.value)
self.end_channel = np.searchsorted(self.dset.energy_loss, self.end_cursor.value)
self.start_channel = np.searchsorted(self.dataset.energy_loss, self.start_cursor.value)
self.end_channel = np.searchsorted(self.dataset.energy_loss, self.end_cursor.value)

def plot(self, scale=True, additional_spectra=None):

self.energy_scale = self.dset.energy_loss.values
self.dataset = self.dset
self.energy_scale = self.dataset.energy_loss.values
x_limit = self.axis.get_xlim()
y_limit = np.array(self.axis.get_ylim())

self.axis.clear()

self.axis.plot(self.energy_scale, self.dset*self.y_scale, label='spectrum')
self.axis.plot(self.energy_scale, self.dataset*self.y_scale, label='spectrum')

if additional_spectra is not None:
if isinstance(additional_spectra, dict):
for key, spectrum in additional_spectra.items():
self.axis.plot(self.energy_scale, spectrum*self.y_scale, label=key)

self.axis.set_xlabel(self.dset.labels[0])
self.axis.set_ylabel(self.dset.data_descriptor)
self.axis.set_xlabel(self.dataset.labels[0])
self.axis.set_ylabel(self.dataset.data_descriptor)
self.axis.ticklabel_format(style='sci', scilimits=(-2, 3))
if scale:
self.axis.set_ylim(np.array(y_limit)*self.change_y_scale)
Expand All @@ -617,8 +620,8 @@ def plot(self, scale=True, additional_spectra=None):
self.axis.legend()
self.figure.canvas.draw_idle()

class SIPlot(sidpy.viz.dataset_viz.SpectralImageVisualizer):

class SIPlot(sidpy.viz.dataset_viz.SpectralImageVisualizerBase):
def __init__(self, dset, figure=None, horizontal=True, **kwargs):
if figure is None:
with plt.ioff():
Expand All @@ -627,8 +630,8 @@ def __init__(self, dset, figure=None, horizontal=True, **kwargs):
self.figure = figure
self.figure.canvas.toolbar_position = 'right'
self.figure.canvas.toolbar_visible = True

super().__init__(dset, figure= self.figure, horizontal=horizontal, **kwargs)
self.dset = dset
super().__init__(self.dset, figure=self.figure, horizontal=horizontal, **kwargs)

self.start_cursor = ipywidgets.FloatText(value=0, description='Start:', disabled=False, color='black', layout=ipywidgets.Layout(width='200px'))
self.end_cursor = ipywidgets.FloatText(value=0, description='End:', disabled=False, color='black', layout=ipywidgets.Layout(width='200px'))
Expand All @@ -645,8 +648,8 @@ def __init__(self, dset, figure=None, horizontal=True, **kwargs):
def line_select_callback(self, x_min, x_max):
self.start_cursor.value = np.round(x_min, 3)
self.end_cursor.value = np.round(x_max, 3)
self.start_channel = np.searchsorted(self.dset.energy_loss, self.start_cursor.value)
self.end_channel = np.searchsorted(self.dset.energy_loss, self.end_cursor.value)
self.start_channel = np.searchsorted(self.dataset.energy_loss, self.start_cursor.value)
self.end_channel = np.searchsorted(self.dataset.energy_loss, self.end_cursor.value)

def plot(self, scale=True, additional_spectra=None):

Expand Down Expand Up @@ -678,8 +681,6 @@ def plot(self, scale=True, additional_spectra=None):
self.fig.canvas.draw_idle()




def get_periodic_table_widget(energy_scale=None):

if energy_scale is None:
Expand Down
Loading

0 comments on commit 2c29c9c

Please sign in to comment.