10
10
from scipy .interpolate import interp1d
11
11
12
12
from pymatgen .analysis .structure_matcher import StructureMatcher
13
+ from pymatgen .core import Element
13
14
from pymatgen .core .spectrum import Spectrum
14
15
from pymatgen .symmetry .analyzer import SpacegroupAnalyzer
15
16
16
17
if TYPE_CHECKING :
18
+ from collections .abc import Sequence
17
19
from typing import Literal
18
20
21
+ from pymatgen .core import Structure
22
+
19
23
__author__ = "Chen Zheng, Yiming Chen"
20
24
__copyright__ = "Copyright 2012, The Materials Project"
21
25
__version__ = "3.0"
@@ -42,29 +46,31 @@ class XAS(Spectrum):
42
46
Attributes:
43
47
x (Sequence[float]): The sequence of energies.
44
48
y (Sequence[float]): The sequence of mu(E).
45
- absorbing_element (str): The absorbing element of the spectrum.
49
+ absorbing_element (str or .Element ): The absorbing element of the spectrum.
46
50
edge (str): The edge of the spectrum.
47
51
spectrum_type (str): The type of the spectrum (XANES or EXAFS).
48
52
absorbing_index (int): The absorbing index of the spectrum.
53
+ zero_negative_intensity (bool) : Whether to set unphysical negative intensities to zero
49
54
"""
50
55
51
56
XLABEL = "Energy"
52
57
YLABEL = "Intensity"
53
58
54
59
def __init__ (
55
60
self ,
56
- x ,
57
- y ,
58
- structure ,
59
- absorbing_element ,
60
- edge = "K" ,
61
- spectrum_type = "XANES" ,
62
- absorbing_index = None ,
61
+ x : Sequence ,
62
+ y : Sequence ,
63
+ structure : Structure ,
64
+ absorbing_element : str | Element ,
65
+ edge : str = "K" ,
66
+ spectrum_type : str = "XANES" ,
67
+ absorbing_index : int | None = None ,
68
+ zero_negative_intensity : bool = False ,
63
69
):
64
70
"""Initialize a spectrum object."""
65
71
super ().__init__ (x , y , structure , absorbing_element , edge )
66
72
self .structure = structure
67
- self .absorbing_element = absorbing_element
73
+ self .absorbing_element = Element ( absorbing_element )
68
74
self .edge = edge
69
75
self .spectrum_type = spectrum_type
70
76
self .e0 = self .x [np .argmax (np .gradient (self .y ) / np .gradient (self .x ))]
@@ -75,8 +81,16 @@ def __init__(
75
81
]
76
82
self .absorbing_index = absorbing_index
77
83
# check for empty spectra and negative intensities
78
- if sum (1 for i in self .y if i <= 0 ) / len (self .y ) > 0.05 :
79
- raise ValueError ("Double check the intensities. Most of them are non-positive." )
84
+ neg_intens_mask = self .y < 0.0
85
+ if len (self .y [neg_intens_mask ]) / len (self .y ) > 0.05 :
86
+ warnings .warn (
87
+ "Double check the intensities. More than 5% of them are negative." ,
88
+ UserWarning ,
89
+ stacklevel = 2 ,
90
+ )
91
+ self .zero_negative_intensity = zero_negative_intensity
92
+ if self .zero_negative_intensity :
93
+ self .y [neg_intens_mask ] = 0.0
80
94
81
95
def __str__ (self ):
82
96
return (
0 commit comments