1
|
|
|
# Licensed under a 3-clause BSD style license - see LICENSE.rst |
2
|
|
|
import pytest |
3
|
|
|
from numpy.testing import assert_allclose |
4
|
|
|
import astropy.units as u |
5
|
|
|
from astropy.table import Column, Table |
6
|
|
|
from gammapy.utils.table import ( |
7
|
|
|
table_from_row_data, |
8
|
|
|
table_row_to_dict, |
9
|
|
|
table_standardise_units_copy, |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def test_table_standardise_units(): |
14
|
|
|
table = Table( |
15
|
|
|
[ |
16
|
|
|
Column([1], "a", unit="ph cm-2 s-1"), |
17
|
|
|
Column([1], "b", unit="ct cm-2 s-1"), |
18
|
|
|
Column([1], "c", unit="cm-2 s-1"), |
19
|
|
|
Column([1], "d"), |
20
|
|
|
] |
21
|
|
|
) |
22
|
|
|
|
23
|
|
|
table = table_standardise_units_copy(table) |
24
|
|
|
|
25
|
|
|
assert table["a"].unit == "cm-2 s-1" |
26
|
|
|
assert table["b"].unit == "cm-2 s-1" |
27
|
|
|
assert table["c"].unit == "cm-2 s-1" |
28
|
|
|
assert table["d"].unit is None |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
@pytest.fixture() |
32
|
|
|
def table(): |
33
|
|
|
return Table( |
34
|
|
|
[Column([1, 2], "a"), Column([1, 2] * u.m, "b"), Column(["x", "yy"], "c")] |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def test_table_row_to_dict(table): |
39
|
|
|
actual = table_row_to_dict(table[1]) |
40
|
|
|
expected = {"a": 2, "b": 2 * u.m, "c": "yy"} |
41
|
|
|
assert actual == expected |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def test_table_from_row_data(): |
45
|
|
|
rows = [dict(a=1, b=1 * u.m, c="x"), dict(a=2, b=2 * u.km, c="yy")] |
46
|
|
|
table = table_from_row_data(rows) |
47
|
|
|
assert isinstance(table, Table) |
48
|
|
|
assert table["b"].unit == "m" |
49
|
|
|
assert_allclose(table["b"].data, [1, 2000]) |
50
|
|
|
|