Skip to content

Commit

Permalink
XPS: Scale Experimental Data Instead of Simulated Data
Browse files Browse the repository at this point in the history
Changes the logic for the intensity scaling feature of PR aiidalab#627
to scale the intensities of the experimental data instead of the
simulated data. This approach is intended to greatly speed up the
response time for the intensity scale widget.

This change also uses `numpy` rather than `pandas` to read the
incoming CSV data, in order to make data manipulation more intuitive.
This change strictly enforces the requirement that the CSV file should
have no header line.
  • Loading branch information
PNOGillespie committed Mar 27, 2024
1 parent 30c4988 commit 3719942
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/aiidalab_qe/plugins/xps/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def export_xps_data(outputs):


def xps_spectra_broadening(
points, equivalent_sites_data, gamma=0.3, sigma=0.3, label="", intensity=1.0
points, equivalent_sites_data, gamma=0.3, sigma=0.3, label=""
):
"""Broadening the XPS spectra with Voigt function and return the spectra data"""

Expand All @@ -55,7 +55,7 @@ def xps_spectra_broadening(
)
for site in point:
# Weight for the spectra of every atom
intensity = equivalent_sites_data[site]["multiplicity"] * intensity
intensity = equivalent_sites_data[site]["multiplicity"]
relative_core_level_position = point[site]
y = (
intensity
Expand Down Expand Up @@ -182,7 +182,6 @@ def _update_view(self):
equivalent_sites_data,
gamma=gamma.value,
sigma=sigma.value,
intensity=intensity.value,
)
# only plot the selected spectrum
for site, d in spectra[spectrum_select.value].items():
Expand All @@ -204,7 +203,6 @@ def response(change):
equivalent_sites_data,
gamma=gamma.value,
sigma=sigma.value,
intensity=intensity.value,
)

for site, d in spectra[spectrum_select.value].items():
Expand Down Expand Up @@ -232,13 +230,26 @@ def response(change):
)
self.g.layout.barmode = "overlay"
self.g.layout.xaxis.title = xaxis
self.plot_experimental_data()
datasets = [dataset["name"] for dataset in self.g.data]
if (
self.experimental_data is not None
and "Experimental Data" not in datasets
):
self.plot_experimental_data(intensity=intensity.value)

def response_exp_intensity(change):
new_intensity = intensity.value
y = self.experimental_data[:, 1] * new_intensity
with self.g.batch_update():
for index, dataset in enumerate(self.g.data):
if dataset["name"] == "Experimental Data":
self.g.data[index].y = y

spectra_type.observe(response, names="value")
spectrum_select.observe(response, names="value")
gamma.observe(response, names="value")
sigma.observe(response, names="value")
intensity.observe(response, names="value")
intensity.observe(response_exp_intensity, names="value")
fill.observe(response, names="value")
self.children = [
spectra_type,
Expand All @@ -257,22 +268,27 @@ def response(change):

def _handle_upload(self, change):
"""Process the uploaded experimental data file."""
import pandas as pd
import numpy as np

uploaded_file = next(iter(change.new.values()))
content = uploaded_file["content"]
content_str = content.decode("utf-8")

from io import StringIO

df = pd.read_csv(StringIO(content_str), header=None)

self.experimental_data = df
self.plot_experimental_data()
self.experimental_data = np.loadtxt(
StringIO(content_str), delimiter=",", converters=lambda i: np.float64(i)
)
self.plot_experimental_data(intensity=1)

def plot_experimental_data(self):
def plot_experimental_data(self, intensity):
"""Plot the experimental data alongside the calculated data."""
if self.experimental_data is not None:
x = self.experimental_data[0]
y = self.experimental_data[1]
self.g.add_scatter(x=x, y=y, mode="lines", name="Experimental Data")
x = self.experimental_data[:, 0]
y = self.experimental_data[:, 1]
self.g.add_scatter(
x=x,
y=y * intensity,
mode="lines",
name="Experimental Data",
)

0 comments on commit 3719942

Please sign in to comment.