File: readobj_unit_test.py

package info (click to toggle)
python-sigima 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 24,956 kB
  • sloc: python: 33,326; makefile: 3
file content (124 lines) | stat: -rw-r--r-- 3,863 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
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.

"""
I/O test

Testing `sigima` specific formats.
"""

from __future__ import annotations

import numpy as np
import pytest

from sigima.io import read_images, read_signals
from sigima.io.ftlab import FTLabImageFile, imread_ftlabima, sigread_ftlabsig
from sigima.objects import ImageObj
from sigima.tests import guiutils, helpers
from sigima.tests.env import execenv


def read_and_view_objs(
    fname: str | None = None, title: str | None = None
) -> list[ImageObj]:
    """Read and view objects from a file

    Args:
        fname: Name of the file to open.
        title: Title for the view.

    Returns:
        List of ImageObj or SignalObj read from the file.
    """
    if "curve" in fname:
        objs = read_signals(fname)
    else:
        objs = read_images(fname)
    for obj in objs:
        if np.all(np.isnan(obj.data)):
            raise ValueError("Data is all NaNs")
    for obj in objs:
        execenv.print(obj)
    guiutils.view_curves_and_images_if_gui(objs, title=f"{title} - {fname}")
    return objs


@helpers.try_open_test_data("Testing TXT file reader", "*.txt")
def test_open_txt(fname: str | None = None, title: str | None = None) -> None:
    """Testing TXT files."""
    read_and_view_objs(fname, title)


@helpers.try_open_test_data("Testing CSV file reader", "*.csv")
def test_open_csv(fname: str | None = None, title: str | None = None) -> None:
    """Testing CSV files."""
    read_and_view_objs(fname, title)


@helpers.try_open_test_data("Testing FTLab signal file reader", "*.sig")
def test_open_sigdata(fname: str | None = None, title: str | None = None) -> None:
    """Testing FTLab signal files."""
    read_and_view_objs(fname, title)

    # Read the FTLab signal file and compare the data with the reference
    data = sigread_ftlabsig(fname)
    ref = read_signals(fname.replace(".sig", ".npy"))[0]
    helpers.check_array_result(f"{fname}", data, ref.xydata)


@helpers.try_open_test_data("Testing MCA file reader", "*.mca")
def test_open_mca(fname: str | None = None, title: str | None = None) -> None:
    """Testing MCA files."""
    read_and_view_objs(fname, title)


@helpers.try_open_test_data("Testing MAT-File reader", "*.mat")
def test_open_mat(fname: str | None = None, title: str | None = None) -> None:
    """Testing MAT files."""
    read_and_view_objs(fname, title)


@helpers.try_open_test_data("Testing SIF file handler", "*.sif")
def test_open_sif(fname: str | None = None, title: str | None = None) -> None:
    """Testing SIF files."""
    read_and_view_objs(fname, title)


@helpers.try_open_test_data("Testing SCOR-DATA file handler", "*.scor-data")
def test_open_scordata(fname: str | None = None, title: str | None = None) -> None:
    """Testing SCOR-DATA files."""
    read_and_view_objs(fname, title)


@helpers.try_open_test_data("Testing FTLab image file handler", "*.ima")
def test_open_imadata(fname: str | None = None, title: str | None = None) -> None:
    """Testing FTLab image files."""
    read_and_view_objs(fname, title)

    # Read the FTLab image file and show the data
    ftlab_file = FTLabImageFile(fname)
    ftlab_file.read()
    execenv.print(ftlab_file)

    # Read the FTLab image file and compare the data with the reference
    data = imread_ftlabima(fname)
    ref = read_images(fname.replace(".ima", ".npy"))[0]
    helpers.check_array_result(f"{fname}", data, ref.data)


@pytest.mark.gui
def test_read_obj_interactive() -> None:
    """Interactive test for I/O: read and view objects from various formats."""
    guiutils.enable_gui()
    test_open_txt()
    test_open_csv()
    test_open_sigdata()
    test_open_mca()
    test_open_mat()
    test_open_sif()
    test_open_scordata()
    test_open_imadata()


if __name__ == "__main__":
    test_read_obj_interactive()