File: test_dosdata.py

package info (click to toggle)
python-ase 3.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (368 lines) | stat: -rw-r--r-- 14,851 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# fmt: off
from collections import OrderedDict
from typing import Any, List, Tuple

import numpy as np
import pytest

from ase.spectrum.dosdata import DOSData, GridDOSData, RawDOSData


class MinimalDOSData(DOSData):
    """Inherit from ABC to test its features"""

    def get_energies(self):
        return NotImplementedError()

    def get_weights(self):
        return NotImplementedError()

    def copy(self):
        return NotImplementedError()


class TestDosData:
    """Test the abstract base class for DOS data"""
    sample_info: List[Tuple[Any, Any]] = [
        (None, {}),
        ({}, {}),
        ({'symbol': 'C', 'index': '2', 'strangekey': 'isallowed'},
         {'symbol': 'C', 'index': '2', 'strangekey': 'isallowed'}),
        ('notadict', TypeError),
        (False, TypeError)]

    @pytest.mark.parametrize('info, expected', sample_info)
    def test_dosdata_init_info(self, info, expected):
        """Check 'info' parameter is handled properly"""
        if isinstance(expected, type) and isinstance(expected(), Exception):
            with pytest.raises(expected):
                dos_data = MinimalDOSData(info=info)
        else:
            dos_data = MinimalDOSData(info=info)
            assert dos_data.info == expected

    @pytest.mark.parametrize('info, expected',
                             [({}, ''),
                              ({'key1': 'value1'}, 'key1: value1'),
                              (OrderedDict([('key1', 'value1'),
                                            ('key2', 'value2')]),
                               'key1: value1; key2: value2'),
                              ({'key1': 'value1', 'label': 'xyz'}, 'xyz'),
                              ({'label': 'xyz'}, 'xyz')])
    def test_label_from_info(self, info, expected):
        assert DOSData.label_from_info(info) == expected


class TestRawDosData:
    """Test the raw DOS data container"""
    @pytest.fixture()
    def sparse_dos(self):
        return RawDOSData([1.2, 3.4, 5.], [3., 2.1, 0.],
                          info={'symbol': 'H', 'number': '1', 'food': 'egg'})

    @pytest.fixture()
    def another_sparse_dos(self):
        return RawDOSData([8., 2., 2., 5.], [1., 1., 1., 1.],
                          info={'symbol': 'H', 'number': '2'})

    def test_init(self):
        with pytest.raises(ValueError):
            RawDOSData([1, 2, 3], [4, 5], info={'symbol': 'H'})

    def test_access(self, sparse_dos):
        assert sparse_dos.info == {'symbol': 'H', 'number': '1', 'food': 'egg'}
        assert np.allclose(sparse_dos.get_energies(), [1.2, 3.4, 5.])
        assert np.allclose(sparse_dos.get_weights(), [3., 2.1, 0.])

    def test_copy(self, sparse_dos):
        copy_dos = sparse_dos.copy()
        assert copy_dos.info == sparse_dos.info
        sparse_dos.info['symbol'] = 'X'
        assert sparse_dos.info['symbol'] == 'X'
        assert copy_dos.info['symbol'] == 'H'

    @pytest.mark.parametrize('other', [True, 1, 0.5, 'string'])
    def test_equality_wrongtype(self, sparse_dos, other):
        assert not sparse_dos._almost_equals(other)

    equality_data = [(((1., 2.), (3., 4.), {'symbol': 'H'}),
                      ((1., 2.), (3., 4.), {'symbol': 'H'}),
                      True),
                     (((1., 3.), (3., 4.), {'symbol': 'H'}),
                      ((1., 2.), (3., 4.), {'symbol': 'H'}),
                      False),
                     (((1., 2.), (3., 5.), {'symbol': 'H'}),
                      ((1., 2.), (3., 4.), {'symbol': 'H'}),
                      False),
                     (((1., 3.), (3., 5.), {'symbol': 'H'}),
                      ((1., 2.), (3., 4.), {'symbol': 'H'}),
                      False),
                     (((1., 2.), (3., 4.), {'symbol': 'H'}),
                      ((1., 2.), (3., 4.), {'symbol': 'He'}),
                      False),
                     (((1., 3.), (3., 4.), {'symbol': 'H'}),
                      ((1., 2.), (3., 4.), {'symbol': 'He'}),
                      False)]

    @pytest.mark.parametrize('data_1, data_2, isequal', equality_data)
    def test_equality(self, data_1, data_2, isequal):
        assert (RawDOSData(*data_1[:2], info=data_1[2])
                ._almost_equals(RawDOSData(*data_2[:2], info=data_2[2]))
                ) == isequal

    def test_addition(self, sparse_dos, another_sparse_dos):
        summed_dos = sparse_dos + another_sparse_dos
        assert summed_dos.info == {'symbol': 'H'}
        assert np.allclose(summed_dos.get_energies(),
                           [1.2, 3.4, 5., 8., 2., 2., 5.])
        assert np.allclose(summed_dos.get_weights(),
                           [3., 2.1, 0., 1., 1., 1., 1.])

    sampling_data_args_results = [
        # Special case: peak max at width 1
        ([[0.], [1.]],
         [[0.], {'width': 1}],
         [1. / (np.sqrt(2. * np.pi))]),
        # Peak max with different width, position
        ([[1.], [2.]],
         [[1.], {'width': 0.5}],
         [2. / (np.sqrt(2. * np.pi) * 0.5)]),
        # Peak max for two simultaneous deltas
        ([[1., 1.], [2., 1.]],
         [[1.], {'width': 1}],
         [3. / (np.sqrt(2. * np.pi))]),
        # Compare with theoretical half-maximum
        ([[0.], [1.]],
         [[np.sqrt(2 * np.log(2)) * 3],
          {'width': 3}],
         [0.5 / (np.sqrt(2 * np.pi) * 3)]),
        # And a case with multiple values, generated
        # using the ASE code (not benchmarked)
        ([[1.2, 3.4, 5], [3., 2.1, 0.]],
         [[1., 1.5, 2., 2.4], {'width': 2}],
         [0.79932418, 0.85848101, 0.88027184, 0.8695055])]

    @pytest.mark.parametrize('data, args, result',
                             sampling_data_args_results)
    def test_sampling(self, data, args, result):
        dos = RawDOSData(data[0], data[1])
        weights = dos._sample(*args[:-1], **args[-1])
        assert np.allclose(weights, result)

        with pytest.raises(ValueError):
            dos._sample([1], smearing="Gauss's spherical cousin")

    def test_sampling_error(self, sparse_dos):
        with pytest.raises(ValueError):
            sparse_dos._sample([1, 2, 3], width=0.)
        with pytest.raises(ValueError):
            sparse_dos._sample([1, 2, 3], width=-1)

    def test_sample_grid(self, sparse_dos):
        min_dos = sparse_dos.sample_grid(10, xmax=5, padding=3, width=0.1)
        assert min_dos.get_energies()[0] == pytest.approx(1.2 - 3 * 0.1)

        max_dos = sparse_dos.sample_grid(10, xmin=0, padding=2, width=0.2)
        assert max_dos.get_energies()[-1] == pytest.approx(5 + 2 * 0.2)

        default_dos = sparse_dos.sample_grid(10)
        assert np.allclose(default_dos.get_energies(),
                           np.linspace(0.9, 5.3, 10))
        dos0 = sparse_dos._sample(np.linspace(0.9, 5.3, 10))
        assert np.allclose(default_dos.get_weights(),
                           dos0)

    # Comparing plot outputs is hard, so we
    # - inspect the line values
    # - check that a line styling parameter is correctly passed through mplargs
    # - set a kwarg from self.sample() to check broadening args are recognised
    linewidths = [1, 5, None]

    @pytest.mark.usefixtures("figure")
    @pytest.mark.parametrize('linewidth', linewidths)
    def test_plot(self, sparse_dos, figure, linewidth):
        if linewidth is None:
            mplargs = None
        else:
            mplargs = {'linewidth': linewidth}

        ax = figure.add_subplot(111)
        ax_out = sparse_dos.plot(npts=5, ax=ax, mplargs=mplargs,
                                 smearing='Gauss')
        assert ax_out == ax

        line_data = ax.lines[0].get_data()
        assert np.allclose(line_data[0], np.linspace(0.9, 5.3, 5))
        assert np.allclose(line_data[1],
                           [1.32955452e-01, 1.51568133e-13,
                            9.30688167e-02, 1.06097693e-13, 3.41173568e-78])

    @pytest.mark.usefixtures("figure")
    @pytest.mark.parametrize('linewidth', linewidths)
    def test_plot_deltas(self, sparse_dos, figure, linewidth):
        if linewidth is None:
            mplargs = None
        else:
            mplargs = {'linewidth': linewidth}

        ax = figure.add_subplot(111)
        ax_out = sparse_dos.plot_deltas(ax=ax, mplargs=mplargs)
        assert ax_out == ax
        assert np.allclose(list(map(lambda x: x.vertices,
                                    ax.get_children()[0].get_paths())),
                           [[[1.2, 0.], [1.2, 3.]],
                            [[3.4, 0.], [3.4, 2.1]],
                            [[5., 0.], [5., 0.]]])


class TestGridDosData:
    """Test the grid DOS data container"""

    def test_init(self):
        # energies and weights must be equal lengths
        with pytest.raises(ValueError):
            GridDOSData(np.linspace(0, 10, 11), np.zeros(10))

        # energies must be evenly spaced
        with pytest.raises(ValueError):
            GridDOSData(np.linspace(0, 10, 11)**2, np.zeros(11))

    @pytest.fixture()
    def dense_dos(self):
        x = np.linspace(0., 10., 11)
        y = np.sin(x / 10)
        return GridDOSData(x, y, info={'symbol': 'C', 'orbital': '2s',
                                       'day': 'Tue'})

    @pytest.fixture()
    def denser_dos(self):
        x = np.linspace(0., 10., 21)
        y = np.sin(x / 10)
        return GridDOSData(x, y)

    @pytest.fixture()
    def another_dense_dos(self):
        x = np.linspace(0., 10., 11)
        y = np.sin(x / 10) * 2
        return GridDOSData(x, y, info={'symbol': 'C', 'orbital': '2p',
                                       'month': 'Feb'})

    def test_access(self, dense_dos):
        assert dense_dos.info == {'symbol': 'C', 'orbital': '2s', 'day': 'Tue'}
        assert len(dense_dos.get_energies()) == 11
        assert dense_dos.get_energies()[-2] == pytest.approx(9.)
        assert dense_dos.get_weights()[-1] == pytest.approx(np.sin(1))

    def test_copy(self, dense_dos):
        copy_dos = dense_dos.copy()
        assert copy_dos.info == dense_dos.info
        dense_dos.info['symbol'] = 'X'
        assert dense_dos.info['symbol'] == 'X'
        assert copy_dos.info['symbol'] == 'C'

    def test_addition(self, dense_dos, another_dense_dos):
        sum_dos = dense_dos + another_dense_dos
        assert np.allclose(sum_dos.get_energies(), dense_dos.get_energies())
        assert np.allclose(sum_dos.get_weights(), dense_dos.get_weights() * 3)
        assert sum_dos.info == {'symbol': 'C'}

        with pytest.raises(ValueError):
            dense_dos + GridDOSData(dense_dos.get_energies() + 1.,
                                    dense_dos.get_weights())
        with pytest.raises(ValueError):
            dense_dos + GridDOSData(dense_dos.get_energies()[1:],
                                    dense_dos.get_weights()[1:])

    def test_check_spacing(self, dense_dos):
        """Check a warning is logged when width < 2 * grid spacing"""
        # In the sample data, grid spacing is 1.0
        dense_dos._sample([1], width=2.1)

        with pytest.warns(UserWarning, match="The broadening width is small"):
            dense_dos._sample([1], width=1.9)

    def test_resampling_consistency(self, dense_dos, denser_dos):
        """Check that resampled spectra are independent of the original density

        Compare resampling of sample function on two different grids to the
        same new grid with broadening. We accept a 5% difference because the
        initial shape is slightly different; what we are checking for is a
        factor 2 difference from "double-counting" the extra data points.
        """
        sampling_params = dict(npts=500, xmin=0, xmax=10, width=4)

        from_dense = dense_dos.sample_grid(**sampling_params)
        from_denser = denser_dos.sample_grid(**sampling_params)

        assert np.allclose(from_dense.get_energies(),
                           from_denser.get_energies())
        assert np.allclose(from_dense.get_weights(),
                           from_denser.get_weights(),
                           rtol=0.05, atol=0.01)

    linewidths = [1, 5, None]

    @pytest.mark.usefixtures("figure")
    @pytest.mark.parametrize('linewidth', linewidths)
    def test_plot(self, dense_dos, figure, linewidth):
        if linewidth is None:
            mplargs = None
        else:
            mplargs = {'linewidth': linewidth}

        ax = figure.add_subplot(111)
        ax_out = dense_dos.plot(ax=ax, mplargs=mplargs,
                                smearing='Gauss')
        assert ax_out == ax

        line_data = ax.lines[0].get_data()

        # With default settings, plot data should be unchanged from input data;
        # this is a special feature of "grid" data to avoid repeated broadening
        assert np.allclose(line_data[0], np.linspace(0., 10., 11))
        assert np.allclose(line_data[1], np.sin(np.linspace(0., 1., 11)))

    @pytest.mark.usefixtures("figure")
    def test_plot_broad_dos(self, dense_dos, figure):
        # Check that setting a grid/broadening does not blow up and reproduces
        # previous results; this result has not been rigorously checked but at
        # least it should not _change_ unexpectedly
        ax = figure.add_subplot(111)
        _ = dense_dos.plot(ax=ax, npts=10, xmin=0, xmax=9,
                           width=4, smearing='Gauss')
        line_data = ax.lines[0].get_data()
        assert np.allclose(line_data[0], range(10))
        assert np.allclose(line_data[1],
                           [0.14659725, 0.19285644, 0.24345501, 0.29505574,
                            0.34335948, 0.38356488, 0.41104823, 0.42216901,
                            0.41503382, 0.39000808])

    smearing_args = [(dict(npts=0, width=None), (0, None)),
                     (dict(npts=10, width=None, default_width=5.), (10, 5.)),
                     (dict(npts=0, width=0.5, default_npts=100), (100, 0.5)),
                     (dict(npts=10, width=0.5), (10, 0.5))]

    @pytest.mark.parametrize('inputs, expected', smearing_args)
    def test_smearing_args_interpreter(self, inputs, expected):
        assert GridDOSData._interpret_smearing_args(**inputs) == expected


class TestMultiDosData:
    """Test interaction between DOS data objects"""
    @pytest.fixture()
    def sparse_dos(self):
        return RawDOSData([1.2, 3.4, 5.], [3., 2.1, 0.],
                          info={'symbol': 'H', 'number': '1', 'food': 'egg'})

    @pytest.fixture()
    def dense_dos(self):
        x = np.linspace(0., 10., 11)
        y = np.sin(x / 10)
        return GridDOSData(x, y, info={'symbol': 'C', 'orbital': '2s',
                                       'day': 'Tue'})

    def test_addition(self, sparse_dos, dense_dos):
        with pytest.raises(TypeError):
            sparse_dos + dense_dos
        with pytest.raises(TypeError):
            dense_dos + sparse_dos