gammapy.utils.tests.test_table   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 35
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A test_table_row_to_dict() 0 4 1
A table() 0 4 1
A test_table_standardise_units() 0 16 1
A test_table_from_row_data() 0 6 1
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