import re
import ipywidgets as widgets
import pandas as pd
from pyccapt.calibration.data_tools import data_tools
# Stores values of the currently selected element in the dropdown.
elementDict = {}
# Stores values of the currently selected charge in the dropdown.
chargeDict = {}
# Stores values of the currently selected element with charge in the dropdown.
elementWithChargeDict = {}
[docs]
def onClickAdd(b, variables):
"""
Callback function for the ADD button click event.
Adds the selected element in the dropdown to a list.
Args:
variables (object): Object of the Variables class.
Returns:
None
"""
if 'element' in elementWithChargeDict:
elementMass = elementWithChargeDict['element']
if elementMass not in variables.list_material:
variables.list_material.append(elementMass)
selectedElement = elementDict['element']
charge = chargeDict['charge']
variables.charge.append(charge)
element = re.sub("\d+", "", selectedElement)
element = re.sub("[\(\[].*?[\)\]]", "", element)
variables.element.append(element)
isotope = int(re.findall("\d+", selectedElement)[0])
variables.isotope.append(isotope)
print("Updated List: ", variables.list_material)
print("Updated element List: ", variables.element)
print("Updated isotope List: ", variables.isotope)
print("Updated charge List: ", variables.charge)
else:
print("Please select the charge before adding", end='\r')
[docs]
def onClickDelete(b, variables):
"""
Callback function for the DELETE button click event.
Deletes the selected element in the dropdown from the list.
Args:
variables (object): Variables object.
Returns:
None
"""
if 'element' in elementWithChargeDict:
elementMass = elementWithChargeDict['element']
if elementMass in variables.list_material:
variables.list_material.remove(elementMass)
variables.element.pop()
variables.isotope.pop()
variables.charge.pop()
print("Updated List: ", variables.list_material)
print("Updated element List: ", variables.element)
print("Updated isotope List: ", variables.isotope)
print("Updated charge List: ", variables.charge)
else:
print("Nothing Deleted. Choose carefully (Enter the right combination of element and charge)", end='\r')
else:
print("Please select the element with the right combination of charge to efficiently delete", end='\r')
[docs]
def onClickReset(b, variables):
"""
Callback function for the RESET button click event.
Clears the list and deletes all the elements from it.
Args:
variables (object): Variables object.
Returns:
None
"""
variables.list_material.clear()
variables.element.clear()
variables.isotope.clear()
variables.charge.clear()
print("Updated List: ", variables.list_material)
print("Updated element List: ", variables.element)
print("Updated isotope List: ", variables.isotope)
print("Updated charge List: ", variables.charge)
[docs]
def on_change(change):
"""
Callback function for observing changes in the dropdown widget.
Updates the selected element and its corresponding weight/mass based on the dropdown selection.
"""
if change['type'] == 'change' and change['name'] == 'value':
print("Mass of selected element: %s" % change['new'], ''.ljust(40), end='\r')
elementWithChargeDict.clear()
print("Now please select the appropriate charge", ''.ljust(40), end='\r')
elementDict['element'] = change['new']
compute_element_isotope_values_according_to_selected_charge()
[docs]
def on_change_ions_selection(change):
"""
Callback function for observing changes in the dropdown widget for ions selection.
Updates the selected element and its corresponding weight/mass based on the dropdown selection.
"""
if change['type'] == 'change' and change['name'] == 'value':
print("Mass of selected element: %s" % change['new'], ''.ljust(40), end='\r')
elementWithChargeDict.clear()
print("Now please select the appropriate charge", ''.ljust(40), end='\r')
elementDict['element'] = change['new']
compute_element_isotope_values_according_to_selected_charge(mode='ions_selection')
[docs]
def on_change_charge(change):
"""
Callback function for observing changes in the dropdown widget for charge selection.
Updates the selected charge value.
"""
if change['type'] == 'change' and change['name'] == 'value':
print("Selected charge: %s" % change['new'], ''.ljust(40), end='\r')
updatedCharge = change['new']
chargeDict['charge'] = updatedCharge
compute_element_isotope_values_according_to_selected_charge()
[docs]
def on_change_charge_ions_selection(change):
"""
Callback function for observing changes in the dropdown widget for ions selection and charge.
Updates the selected charge value.
"""
if change['type'] == 'change' and change['name'] == 'value':
print("Selected charge: %s" % change['new'], ''.ljust(40), end='\r')
updatedCharge = change['new']
chargeDict['charge'] = updatedCharge
compute_element_isotope_values_according_to_selected_charge(mode='ions_selection')
[docs]
def compute_element_isotope_values_according_to_selected_charge(mode='calibration'):
"""
Compute the element and isotope values based on the selected charge.
Args:
mode (str): Computation mode. Defaults to 'calibration'.
"""
selectedElement = elementDict['element']
charge = chargeDict['charge']
if mode == 'calibration':
elem = re.findall('\[(.*?)\]', selectedElement)
elementWithCharge = round(float(elem[0]) / int(charge), 2)
elementWithChargeDict['element'] = elementWithCharge
elif mode == 'ions_selection':
elementWithCharge = selectedElement + '(' + str(charge) + '+)'
elementWithChargeDict['element'] = elementWithCharge
[docs]
def dataset_instrument_specification_selection():
"""
Create and return the dataset TDC selection widgets.
Returns:
tdc (object): Dropdown widget for selecting data mode.
pulse_mode (object): Dropdown widget for selecting pulse mode.
flightPathLength (object): FloatText widget for flight path length.
t0 (object): FloatText widget for t0.
max_mc (object): FloatText widget for maximum mc.
det_diam (object): FloatText widget for detector diameter.
"""
flightPathLength = widgets.FloatText(
value='110', placeholder='Flight path length', description='Flight path length:', disabled=False
)
det_diam = widgets.FloatText(value='80', placeholder='Detector diameter', description='Detector diameter:', disabled=False)
t0 = widgets.FloatText(value='38', placeholder='T_0 of the instrument', description='t0:', disabled=False)
max_mc = widgets.FloatText(value='400', placeholder='Maximum possible mc', description='Max mc:', disabled=False)
tdc = widgets.Dropdown(
options=['pyccapt', 'leap_epos', 'leap_pos', 'leap_apt', 'ato_v6'],
value='pyccapt',
description='Data mode:',
)
pulse_mode = widgets.Dropdown(
options=['voltage', 'laser'],
value='voltage',
description='Pulse mode:',
)
return tdc, pulse_mode, flightPathLength, t0, max_mc, det_diam
[docs]
def load_tdc_raw_selection():
"""Create the dropdown for opting into raw ``/tdc`` loading.
The widget has labels (``True``, ``False``) and resolves to ``bool`` values.
Pass the widget's ``.value`` to ``helper_data_loader.load_data`` via the
``load_tdc_raw`` argument. When True, the loader also reads the ``/tdc``
group and links it to the ``/dld`` group via shared ``event_group_id`` so
raw rows can later be saved alongside calibrated dld output.
"""
return widgets.Dropdown(
options=[('False', False), ('True', True)],
value=False,
description='Load raw tdc:',
)
[docs]
def density_field_selection():
"""
Create and return the element dropdown widget for density field selection.
Returns:
element (object): Dropdown widget for selecting an element.
"""
try:
TableFile = '../../../files/field_density_table.h5'
dataframe = pd.read_hdf(TableFile, mode='r')
except Exception as e:
try:
TableFile = './pyccapt/files/field_density_table.h5'
dataframe = pd.read_hdf(TableFile, mode='r')
except Exception as e:
print("Error: ", e)
elementsList = dataframe['element'].tolist()
elementDensityList = dataframe['atom_density'].tolist()
elementFieldList = dataframe['field_evaporation'].tolist()
# Each option is (label, value). Label starts with the element name so the
# built-in Dropdown type-ahead lets the user search by typing the symbol
# (e.g. press "F" then "e" to jump to Fe). Value is (name, density, field);
# consumers read .value[1] for density and .value[2] for field.
options = sorted(
(
(
"{} - Density({}) - FieldEva({})".format(name, density, field),
(name, density, field),
)
for name, density, field in zip(elementsList, elementDensityList, elementFieldList)
),
key=lambda opt: opt[0].lower(),
)
element = widgets.Dropdown(options=options, description='Element')
return element