File: test_rdata.py

package info (click to toggle)
python-rdata 0.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 740 kB
  • sloc: python: 2,388; makefile: 22
file content (698 lines) | stat: -rw-r--r-- 22,583 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
"""Tests of parsing and conversion."""

import itertools
import unittest
from collections import ChainMap
from fractions import Fraction
from types import SimpleNamespace
from typing import Any

import numpy as np
import pandas as pd
import pytest
import xarray

import rdata

TESTDATA_PATH = rdata.TESTDATA_PATH


class SimpleTests(unittest.TestCase):
    """Collection of simple test cases."""

    def test_opened_file(self) -> None:
        """Test that an opened file can be passed to parse_file."""
        with (TESTDATA_PATH / "test_vector.rda").open("rb") as f:
            parsed = rdata.parser.parse_file(f)
            converted = rdata.conversion.convert(parsed)

            assert isinstance(converted, dict)

    def test_opened_string(self) -> None:
        """Test that a string can be passed to parse_file."""
        parsed = rdata.parser.parse_file(
            str(TESTDATA_PATH / "test_vector.rda"),
        )
        converted = rdata.conversion.convert(parsed)

        assert isinstance(converted, dict)

    def test_logical(self) -> None:
        """Test parsing of logical vectors."""
        data = rdata.read_rda(TESTDATA_PATH / "test_logical.rda")

        np.testing.assert_equal(data, {
            "test_logical": np.array([True, True, False, True, False]),
        })

    def test_nullable_logical(self) -> None:
        """Test parsing of logical vectors containing NA."""
        data = rdata.read_rda(TESTDATA_PATH / "test_nullable_logical.rda")

        array = data["test_nullable_logical"]
        np.testing.assert_array_equal(
            array.data,
            np.array([True, False, True]),
        )
        np.testing.assert_array_equal(
            array.mask,
            np.array([False, False, True]),
        )

    def test_nullable_int(self) -> None:
        """Test parsing of integer vectors containing NA."""
        data = rdata.read_rda(TESTDATA_PATH / "test_nullable_int.rda")

        array = data["test_nullable_int"]
        np.testing.assert_array_equal(
            array.data,
            np.array([313, -12, -2**31]),
        )
        np.testing.assert_array_equal(
            array.mask,
            np.array([False, False, True]),
        )

    def test_vector(self) -> None:
        """Test parsing of numerical vectors."""
        data = rdata.read_rda(TESTDATA_PATH / "test_vector.rda")

        np.testing.assert_equal(data, {
            "test_vector": np.array([1.0, 2.0, 3.0]),
        })

    def test_empty_string(self) -> None:
        """Test that the empty string is parsed correctly."""
        data = rdata.read_rda(TESTDATA_PATH / "test_empty_str.rda")

        np.testing.assert_equal(data, {
            "test_empty_str": [""],
        })

    def test_na_string(self) -> None:
        """Test that the NA string is parsed correctly."""
        data = rdata.read_rda(TESTDATA_PATH / "test_na_string.rda")

        np.testing.assert_equal(data, {
            "test_na_string": [None],
        })

    def test_complex(self) -> None:
        """Test that complex numbers can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_complex.rda")

        np.testing.assert_equal(data, {
            "test_complex": np.array([1 + 2j, 2, 0, 1 + 3j, -1j]),
        })

    def test_matrix(self) -> None:
        """Test that a matrix can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_matrix.rda")

        np.testing.assert_equal(data, {
            "test_matrix": np.array([
                [1.0, 2.0, 3.0],
                [4.0, 5.0, 6.0],
            ]),
        })

    def test_named_matrix(self) -> None:
        """Test that a named matrix can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_named_matrix.rda")

        reference = xarray.DataArray(
            [
                [1.0, 2.0, 3.0],
                [4.0, 5.0, 6.0],
            ],
            dims=["dim_0", "dim_1"],
            coords={
                "dim_0": ["dim0_0", "dim0_1"],
                "dim_1": ["dim1_0", "dim1_1", "dim1_2"],
            },
        )

        xarray.testing.assert_identical(
            data["test_named_matrix"],
            reference,
        )

    def test_half_named_matrix(self) -> None:
        """Test that a named matrix with no name for a dim can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_half_named_matrix.rda")

        reference = xarray.DataArray(
            [
                [1.0, 2.0, 3.0],
                [4.0, 5.0, 6.0],
            ],
            dims=["dim_0", "dim_1"],
            coords={
                "dim_0": ["dim0_0", "dim0_1"],
            },
        )

        xarray.testing.assert_identical(
            data["test_half_named_matrix"],
            reference,
        )

    def test_full_named_matrix(self) -> None:
        """Test that a named matrix with dim names can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_full_named_matrix.rda")

        reference = xarray.DataArray(
            [
                [1.0, 2.0, 3.0],
                [4.0, 5.0, 6.0],
            ],
            dims=["my_dim_0", "my_dim_1"],
            coords={
                "my_dim_0": ["dim0_0", "dim0_1"],
                "my_dim_1": ["dim1_0", "dim1_1", "dim1_2"],
            },
        )

        xarray.testing.assert_identical(
            data["test_full_named_matrix"],
            reference,
        )

    def test_full_named_matrix_rds(self) -> None:
        """Test that a named matrix with dim names can be parsed."""
        data = rdata.read_rds(TESTDATA_PATH / "test_full_named_matrix.rds")

        reference = xarray.DataArray(
            [
                [1.0, 2.0, 3.0],
                [4.0, 5.0, 6.0],
            ],
            dims=["my_dim_0", "my_dim_1"],
            coords={
                "my_dim_0": ["dim0_0", "dim0_1"],
                "my_dim_1": ["dim1_0", "dim1_1", "dim1_2"],
            },
        )

        xarray.testing.assert_identical(
            data,
            reference,
        )

    def test_list(self) -> None:
        """Test that list can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_list.rda")

        np.testing.assert_equal(data, {
            "test_list":
                [
                    np.array([1.0]),
                    ["a", "b", "c"],
                    np.array([2.0, 3.0]),
                    ["hi"],
                ],
        })

    @pytest.mark.filterwarnings("ignore:Missing constructor")
    def test_file(self) -> None:
        """Test that external pointers can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_file.rda")

        np.testing.assert_equal(data, {
            "test_file": [5],
        })

    def test_expression(self) -> None:
        """Test that expressions can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_expression.rda")

        np.testing.assert_equal(data, {
            "test_expression": rdata.conversion.RExpression([
                rdata.conversion.RLanguage(
                    ["^", "base", "exponent"],
                    attributes={},
                ),
            ]),
        })

    def test_builtin(self) -> None:
        """Test that builtin functions can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_builtin.rda")

        np.testing.assert_equal(data, {
            "test_builtin": rdata.conversion.RBuiltin(name="abs"),
        })

    def test_minimal_function_uncompiled(self) -> None:
        """Test that a minimal function can be parsed."""
        data = rdata.read_rda(
            TESTDATA_PATH / "test_minimal_function_uncompiled.rda",
        )

        converted_fun = data["test_minimal_function_uncompiled"]

        assert isinstance(
            converted_fun,
            rdata.conversion.RFunction,
        )

        np.testing.assert_equal(converted_fun.environment, ChainMap({}))
        np.testing.assert_equal(converted_fun.formals, None)
        np.testing.assert_equal(converted_fun.body, None)
        np.testing.assert_equal(
            converted_fun.source,
            "test_minimal_function_uncompiled <- function() NULL\n",
        )

    @pytest.mark.filterwarnings("ignore:Missing constructor")
    def test_minimal_function(self) -> None:
        """Test that a minimal function (compiled) can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_minimal_function.rda")

        converted_fun = data["test_minimal_function"]

        assert isinstance(
            converted_fun,
            rdata.conversion.RFunction,
        )

        np.testing.assert_equal(converted_fun.environment, ChainMap({}))
        np.testing.assert_equal(converted_fun.formals, None)

        converted_body = converted_fun.body

        assert isinstance(
            converted_body,
            rdata.conversion.RBytecode,
        )

        np.testing.assert_equal(converted_body.code, np.array([12, 17, 1]))
        np.testing.assert_equal(converted_body.attributes, {})

        np.testing.assert_equal(
            converted_fun.source,
            "test_minimal_function <- function() NULL\n",
        )

    def test_empty_function_uncompiled(self) -> None:
        """Test that a simple function can be parsed."""
        data = rdata.read_rda(
            TESTDATA_PATH / "test_empty_function_uncompiled.rda",
        )

        converted_fun = data["test_empty_function_uncompiled"]

        assert isinstance(
            converted_fun,
            rdata.conversion.RFunction,
        )

        np.testing.assert_equal(converted_fun.environment, ChainMap({}))
        np.testing.assert_equal(converted_fun.formals, None)
        assert isinstance(converted_fun.body, rdata.conversion.RLanguage)
        np.testing.assert_equal(
            converted_fun.source,
            "test_empty_function_uncompiled <- function() {}\n",
        )

    @pytest.mark.filterwarnings("ignore:Missing constructor")
    def test_empty_function(self) -> None:
        """Test that a simple function (compiled) can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_empty_function.rda")

        converted_fun = data["test_empty_function"]

        assert isinstance(
            converted_fun,
            rdata.conversion.RFunction,
        )

        np.testing.assert_equal(converted_fun.environment, ChainMap({}))
        np.testing.assert_equal(converted_fun.formals, None)

        converted_body = converted_fun.body

        assert isinstance(
            converted_body,
            rdata.conversion.RBytecode,
        )

        np.testing.assert_equal(converted_body.code, np.array([12, 17, 1]))
        np.testing.assert_equal(converted_body.attributes, {})

        np.testing.assert_equal(
            converted_fun.source,
            "test_empty_function <- function() {}\n",
        )

    @pytest.mark.filterwarnings("ignore:Missing constructor")
    def test_function(self) -> None:
        """Test that functions can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_function.rda")

        converted_fun = data["test_function"]

        assert isinstance(
            converted_fun,
            rdata.conversion.RFunction,
        )

        np.testing.assert_equal(converted_fun.environment, ChainMap({}))
        np.testing.assert_equal(converted_fun.formals, None)

        converted_body = converted_fun.body

        assert isinstance(
            converted_body,
            rdata.conversion.RBytecode,
        )

        np.testing.assert_equal(
            converted_body.code,
            np.array([12, 23, 1, 34, 4, 38, 2, 1]),
        )
        np.testing.assert_equal(converted_body.attributes, {})

        np.testing.assert_equal(
            converted_fun.source,
            "test_function <- function() {print(\"Hello\")}\n",
        )

    @pytest.mark.filterwarnings("ignore:Missing constructor")
    def test_function_arg(self) -> None:
        """Test that functions can be parsed."""
        data = rdata.read_rda(TESTDATA_PATH / "test_function_arg.rda")

        converted_fun = data["test_function_arg"]

        assert isinstance(
            converted_fun,
            rdata.conversion.RFunction,
        )

        np.testing.assert_equal(converted_fun.environment, ChainMap({}))
        np.testing.assert_equal(converted_fun.formals, {"a": NotImplemented})

        converted_body = converted_fun.body

        assert isinstance(
            converted_body,
            rdata.conversion.RBytecode,
        )

        np.testing.assert_equal(
            converted_body.code,
            np.array([12, 23, 1, 29, 4, 38, 2, 1]),
        )
        np.testing.assert_equal(converted_body.attributes, {})

        np.testing.assert_equal(
            converted_fun.source,
            "test_function_arg <- function(a) {print(a)}\n",
        )

    def test_encodings(self) -> None:
        """Test of differents encodings."""
        with self.assertWarns(
            UserWarning,
            msg="Unknown encoding. Assumed ASCII.",
        ):
            data = rdata.read_rda(
                TESTDATA_PATH / "test_encodings.rda",
            )

            np.testing.assert_equal(data, {
                "test_encoding_utf8": ["eĥoŝanĝo ĉiuĵaŭde"],
                "test_encoding_latin1": ["cañón"],
                "test_encoding_bytes": [b"reba\xf1o"],
                "test_encoding_latin1_implicit": [b"\xcd\xf1igo"],
            })

    def test_encodings_v3(self) -> None:
        """Test encodings in version 3 format."""
        data = rdata.read_rda(TESTDATA_PATH / "test_encodings_v3.rda")

        np.testing.assert_equal(data, {
            "test_encoding_utf8": ["eĥoŝanĝo ĉiuĵaŭde"],
            "test_encoding_latin1": ["cañón"],
            "test_encoding_bytes": [b"reba\xf1o"],
            "test_encoding_latin1_implicit": ["Íñigo"],
        })

    def test_dataframe(self) -> None:
        """Test dataframe conversion."""
        for f in ("test_dataframe.rda", "test_dataframe_v3.rda"):
            with self.subTest(file=f):
                data = rdata.read_rda(TESTDATA_PATH / f)

                pd.testing.assert_frame_equal(
                    data["test_dataframe"],
                    pd.DataFrame(
                        {
                            "class": pd.Categorical(
                                ["a", "b", "b"],
                            ),
                            "value": pd.Series(
                                [1, 2, 3],
                                dtype=pd.Int32Dtype(),
                            ).array,
                        },
                        index=pd.RangeIndex(start=1, stop=4),
                    ),
                )

    def test_dataframe_rds(self) -> None:
        """Test dataframe conversion."""
        for f in ("test_dataframe.rds", "test_dataframe_v3.rds"):
            with self.subTest(file=f):
                data = rdata.read_rds(TESTDATA_PATH / f)

                pd.testing.assert_frame_equal(
                    data,
                    pd.DataFrame(
                        {
                            "class": pd.Categorical(
                                ["a", "b", "b"],
                            ),
                            "value": pd.Series(
                                [1, 2, 3],
                                dtype=pd.Int32Dtype(),
                            ).array,
                        },
                        index=pd.RangeIndex(start=1, stop=4),
                    ),
                )

    def test_dataframe_rownames(self) -> None:
        """Test dataframe conversion."""
        data = rdata.read_rda(TESTDATA_PATH / "test_dataframe_rownames.rda")

        pd.testing.assert_frame_equal(
            data["test_dataframe_rownames"],
            pd.DataFrame(
                {
                    "class": pd.Categorical(
                        ["a", "b", "b"],
                    ),
                    "value": pd.Series(
                        [1, 2, 3],
                        dtype=pd.Int32Dtype(),
                    ).array,
                },
                index=("Madrid", "Frankfurt", "Herzberg am Harz"),
            ),
        )

    def test_ts(self) -> None:
        """Test time series conversion."""
        data = rdata.read_rda(TESTDATA_PATH / "test_ts.rda")

        pd.testing.assert_series_equal(
            data["test_ts"],
            pd.Series({
                2000 + Fraction(2, 12): 1.0,
                2000 + Fraction(3, 12): 2.0,
                2000 + Fraction(4, 12): 3.0,
            }),
        )

    def test_s4(self) -> None:
        """Test parsing of S4 classes."""
        with pytest.warns(UserWarning, match="Missing constructor"):
            data = rdata.read_rda(TESTDATA_PATH / "test_s4.rda")

        np.testing.assert_equal(data, {
            "test_s4": SimpleNamespace(
                age=np.array(28),
                name=["Carlos"],
                **{"class": ["Person"]},
            ),
        })

    def test_environment(self) -> None:
        """Test parsing of environments."""
        parsed = rdata.parser.parse_file(
            TESTDATA_PATH / "test_environment.rda",
        )
        converted = rdata.conversion.convert(parsed)

        dict_env = {"string": ["test"]}
        empty_global_env: dict[str, Any] = {}

        np.testing.assert_equal(converted, {
            "test_environment": ChainMap(dict_env, ChainMap(empty_global_env)),
        })

        global_env = {"global": "test"}

        converted_global = rdata.conversion.convert(
            parsed,
            global_environment=global_env,
        )

        np.testing.assert_equal(converted_global, {
            "test_environment": ChainMap(dict_env, ChainMap(global_env)),
        })

    def test_emptyenv(self) -> None:
        """Test parsing the empty environment."""
        data = rdata.read_rda(TESTDATA_PATH / "test_emptyenv.rda")

        assert data == {
            "test_emptyenv": ChainMap({}),
        }

    def test_list_attrs(self) -> None:
        """Test that lists accept attributes."""
        data = rdata.read_rda(TESTDATA_PATH / "test_list_attrs.rda")

        np.testing.assert_equal(data, {
            "test_list_attrs": [["list"], [5]],
        })

    def test_altrep_compact_intseq(self) -> None:
        """Test alternative representation of sequences of ints."""
        data = rdata.read_rda(TESTDATA_PATH / "test_altrep_compact_intseq.rda")

        np.testing.assert_equal(data, {
            "test_altrep_compact_intseq": np.arange(1000),
        })

    def test_altrep_compact_intseq_asymmetric(self) -> None:
        """
        Test alternative representation of sequences of ints.

        This test an origin different from 0, to reproduce
        issue #29.
        """
        data = rdata.read_rda(
            TESTDATA_PATH / "test_altrep_compact_intseq_asymmetric.rda",
        )

        np.testing.assert_equal(data, {
            "test_altrep_compact_intseq_asymmetric": np.arange(-5, 6),
        })

    def test_altrep_compact_realseq(self) -> None:
        """Test alternative representation of sequences of ints."""
        data = rdata.read_rda(
            TESTDATA_PATH / "test_altrep_compact_realseq.rda",
        )

        np.testing.assert_equal(data, {
            "test_altrep_compact_realseq": np.arange(1000.0),
        })

    def test_altrep_compact_realseq_asymmetric(self) -> None:
        """
        Test alternative representation of sequences of ints.

        This test an origin different from 0, to reproduce
        issue #29.
        """
        data = rdata.read_rda(
            TESTDATA_PATH / "test_altrep_compact_realseq_asymmetric.rda",
        )

        np.testing.assert_equal(data, {
            "test_altrep_compact_realseq_asymmetric": np.arange(-5.0, 6.0),
        })

    def test_altrep_deferred_string(self) -> None:
        """Test alternative representation of deferred strings."""
        data = rdata.read_rda(
            TESTDATA_PATH / "test_altrep_deferred_string.rda",
        )

        np.testing.assert_equal(data, {
            "test_altrep_deferred_string": [
                "1", "2.3", "10000",
                "1e+05", "-10000", "-1e+05",
                "0.001", "1e-04", "1e-05",
            ],
        })

    def test_altrep_wrap_real(self) -> None:
        """Test alternative representation of wrap_real."""
        data = rdata.read_rda(
            TESTDATA_PATH / "test_altrep_wrap_real.rda",
        )

        np.testing.assert_equal(data, {
            "test_altrep_wrap_real": [3],
        })

    def test_altrep_wrap_string(self) -> None:
        """Test alternative representation of wrap_string."""
        data = rdata.read_rda(TESTDATA_PATH / "test_altrep_wrap_string.rda")

        np.testing.assert_equal(data, {
            "test_altrep_wrap_string": ["Hello"],
        })

    def test_altrep_wrap_logical(self) -> None:
        """Test alternative representation of wrap_logical."""
        data = rdata.read_rda(TESTDATA_PATH / "test_altrep_wrap_logical.rda")

        np.testing.assert_equal(data, {
            "test_altrep_wrap_logical": [True],
        })

    def test_ascii(self) -> None:
        """Test ascii files."""
        ref_ma = np.ma.array(  # type: ignore[no-untyped-call]
            data=[True],
            mask=[True],
            fill_value=True,
        )
        ref = [[1.1], [2], [3. + 4.j], ref_ma, ["aä"]]

        for tag, v, ext in itertools.product(
                ("", "win_"),
                (2, 3),
                ("rda", "rds"),
        ):
            f = f"test_ascii_{tag}v{v}.{ext}"
            with self.subTest(file=f):
                parsed = rdata.parser.parse_file(
                    TESTDATA_PATH / f,
                )
                converted = rdata.conversion.convert(parsed)

                if ext == "rda":
                    np.testing.assert_equal(converted, {"data": ref})
                    ma = converted["data"][3]
                else:
                    np.testing.assert_equal(converted, ref)
                    ma = converted[3]

                # Test masked array separately
                np.testing.assert_equal(ma.data, ref_ma.data)
                np.testing.assert_equal(ma.mask, ref_ma.mask)
                np.testing.assert_equal(ma.mask, ref_ma.mask)
                np.testing.assert_equal(ma.get_fill_value(),
                                        ref_ma.get_fill_value())


if __name__ == "__main__":
    unittest.main()