-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuv_plotter.py
More file actions
90 lines (66 loc) · 2.65 KB
/
uv_plotter.py
File metadata and controls
90 lines (66 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# %% Imports and load files
import math
from pathlib import Path
import cclib
import matplotlib.pyplot as plot
from cclib.parser.utils import convertor
def extract(logfile):
"""Extract excited state energies and oscillator strength from logfile
Fill excited_states with tuples containing wavelength in nm and oscillator strength.
"""
excited_states = list()
parser = cclib.io.ccopen(logfile)
data = parser.parse()
for wv, os in zip(data.etenergies, data.etoscs):
wv = convertor(wv, "wavenumber", "nm")
excited_states.append((wv, os))
return excited_states
def generate(excited_states, plt_range, grid_size, sigma, normalization=True):
"""Generate gaussian functions for each excitation energy, and sum them all
- Generate a range of wavelengths to use to generate the spectrum
- At each point, compute the absorbance including the contribution of all excitations
- Normalize according to the max absorbance if necessary
- Return a 2D line ready to be plotted.
"""
# Generate the range of wavelengths
grid = range(plt_range[0], plot_range[1], grid_size)
# Generate data for each point
uv_spectrum = list()
for point in grid:
fit = 0.0
for (wv, os) in excited_states:
fit += os * math.e ** (-0.5 * ((point - wv) ** 2) / (sigma ** 2))
uv_spectrum += [(point, fit)]
# Normalize if requested
if normalization:
max_energy = max(point[1] for point in uv_spectrum) # Retrieve max energy
uv_spectrum = [(wavelength, energy / max_energy) for (wavelength, energy) in uv_spectrum]
# Return final spectrum line
return uv_spectrum
# %%
# List of files to plot
files_root = Path('data/UV/')
files = list()
for file in files_root.iterdir():
if file.is_file() and file.suffix == ".log":
files.append(file)
broadening_constant = 50 # Broadening for Gaussians
plot_range = [200, 800] # Range of spectrum to display in nm
plot_grid = 5 # Grid precision (distance between two generated points)
for file in files:
# Extract data and generate the line to plot
data = extract(file.as_posix())
uv_spectrum = generate(data, plot_range, plot_grid, broadening_constant, normalization=True)
# Reformat data
wavelengths = [point[0] for point in uv_spectrum]
absorbances = [point[1] for point in uv_spectrum]
# Setup the plot
fig, ax = plot.subplots()
ax.set_xlabel('Energy (nm)')
ax.set_ylabel('Normalized absorbance')
ax.set_title(file.stem)
ax.plot(wavelengths, absorbances)
fig.show()
# Save image
img_file = Path(files_root, file.stem + ".png")
fig.savefig(img_file, dpi=300)