-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: More intermediate text files, highlights, useful plots
- A subset of modes are identified by index in the config file as "highlights". This spectrum is broadened separately from the total sum. - reference.pdf now just compares fundamentals from both methods: this is a more useful sanity-check - plot.pdf now plots highlighted modes against fundamental and ref spectra from Abins. This seems the most research-relevant output. - Various minor improvements: more control over binning and plot ranges, plotting stylesheet, simpler scripts (but more of them)
- Loading branch information
Showing
6 changed files
with
196 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from pathlib import Path | ||
|
||
from euphonic import ureg, Quantity, Spectrum1D | ||
import numpy as np | ||
from numpy.polynomial import Polynomial | ||
from snakemake.script import snakemake | ||
|
||
bin_config = snakemake.params.bin_config | ||
|
||
|
||
def tosca_broadening(x: Quantity) -> Quantity: | ||
"""Gaussian width parameter for TOSCA as a function of energy""" | ||
|
||
poly = Polynomial([2.5, 5e-3, 1e-7]) | ||
return poly(x.to("1/cm").magnitude) * ureg("1/cm") | ||
|
||
|
||
def get_spectrum(x: list[float], y: list[float]) -> Spectrum1D: | ||
"""Get a binned and broadened spectrum from scattered x/y input""" | ||
bins = np.linspace(bin_config["x_min"], bin_config["x_max"], bin_config["n_bins"]) | ||
bin_width = bins[1] - bins[0] | ||
|
||
hist, _ = np.histogram(x, bins=bins, weights=y, density=False) | ||
hist /= bin_width # Correct intensity scaling for bins | ||
|
||
if snakemake.params.instrument != "TOSCA": | ||
raise Exception("Currently only TOSCA resolution function is available.") | ||
|
||
spectrum = Spectrum1D(ureg.Quantity(bins, "1/cm"), ureg.Quantity(hist, "cm")) | ||
spectrum = spectrum.broaden( | ||
x_width=tosca_broadening, shape="gauss", width_convention="std" | ||
) | ||
return spectrum | ||
|
||
|
||
def read_csv(input_file: Path) -> tuple[np.ndarray, np.ndarray]: | ||
return np.loadtxt( | ||
input_file, delimiter=",", skiprows=1, usecols=(1, 2), unpack=True | ||
) | ||
|
||
|
||
spectrum = get_spectrum(*read_csv(Path(snakemake.input[0]))) | ||
spectrum.to_text_file(snakemake.output[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from itertools import groupby | ||
from operator import itemgetter | ||
from pathlib import Path | ||
|
||
import dataset | ||
from snakemake.script import snakemake | ||
|
||
db_file = Path(snakemake.input[0]) | ||
mode_selection = snakemake.params.selection | ||
|
||
with ( | ||
dataset.connect(f"sqlite:///{db_file.resolve()}") as db, | ||
open(snakemake.output["csv"], "wt") as csv, | ||
open(snakemake.output["txt"], "wt") as txt, | ||
): | ||
print(f"# mode_index,frequency,intensity", file=csv) | ||
print(f"# mode_index frequency intensity", file=txt) | ||
|
||
abins_table = db["abins"] | ||
atoms_table = db["atoms"] | ||
modes_table = db["modes"] | ||
|
||
cross_sections = dict( | ||
map(itemgetter("atom_index", "cross_section"), atoms_table.all()) | ||
) | ||
|
||
frequencies = dict(map(itemgetter("mode_index", "frequency"), modes_table.all())) | ||
|
||
def get_intensity(data_row): | ||
return cross_sections[data_row["atom_index"]] * data_row["weight"] | ||
|
||
for mode_index, group in groupby( | ||
abins_table.find(order_by="mode_index"), key=itemgetter("mode_index") | ||
): | ||
if mode_selection is not None and mode_index not in mode_selection: | ||
# Limit output to selected modes | ||
continue | ||
|
||
frequency = frequencies[mode_index] | ||
intensity = sum(map(get_intensity, group)) | ||
|
||
print(f"{mode_index:d},{frequency:f},{intensity:f}", file=csv) | ||
print(f"{mode_index:4d} {frequency:8.2f} {intensity:7.3f}", file=txt) |