File: intensitydata_io.py

package info (click to toggle)
bornagain 23.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,948 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (39 lines) | stat: -rw-r--r-- 1,318 bytes parent folder | download | duplicates (2)
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
# Functional test: tests of IO operations with the IntensityData object

import unittest, numpy
import bornagain as ba
from bornagain.numpyutil import Arrayf64Converter as dac


class IOTest(unittest.TestCase):
    """
    Test serialization of IntensityData
    """
    def setUp(self):
        self.arr = numpy.array([[0, 1, 2.], [3, 4, 5.]])
        numpy.savetxt('intensitydata.txt', self.arr)
        self.dat = ba.readData2D("intensitydata.txt")

    def test_1(self):
        print()
        print("Test: numpy array -> text file -> datafield -> npArray")
        print("input array: ", self.arr)
        print("read back as datafield: ", self.dat.flatVector())

        arr1 = dac.asNpArray(self.dat.dataArray())
        print("exported to NumPy ", arr1, flush=True)
        self.assertTrue(numpy.array_equal(arr1, self.arr))

    def test_2(self):
        print()
        print("Test: numpy array -> text file -> datafield -> flatVector -> numpy.array")
        print("input array: ", self.arr)
        print("read back as datafield: ", self.dat.flatVector())

        arr2 = numpy.array(self.dat.flatVector())
        arr2.shape = self.arr.shape
        print("converted to NumPy ", arr2, flush=True)
        self.assertTrue(numpy.array_equal(arr2, self.arr))

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