forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbinsIOmoduleTest.py
121 lines (88 loc) · 4.73 KB
/
AbinsIOmoduleTest.py
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
# -*- coding: utf-8 -*-# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
import unittest
import numpy as np
from abins import IO, test_helpers
class IOTest(unittest.TestCase):
def tearDown(self):
test_helpers.remove_output_files(list_of_names=["Cars", "temphgfrt"])
@staticmethod
def _save_stuff():
saver = IO(input_filename="Cars.foo", group_name="Volksvagen")
# add some attributes
saver.add_attribute("Fuel", 100)
saver.add_attribute("Speed", 200)
# add some datasets
saver.add_data("Passengers", np.array([4]))
saver.add_data("FireExtinguishers", np.array([2]))
# add some mode complex data sets
wheels = [{"Winter": False, "Punctured": False, "Brand": "Mercedes", "Age": 2},
{"Winter": False, "Punctured": False, "Brand": "Mercedes", "Age": 3},
{"Winter": False, "Punctured": False, "Brand": "Mercedes", "Age": 5},
{"Winter": False, "Punctured": True, "Brand": "Mercedes", "Age": 7}]
chairs = {"AdjustableHeadrests": True, "ExtraPadding": True}
saver.add_data("wheels", wheels)
saver.add_data("chairs", chairs)
# save attributes and datasets
saver.save()
def _save_wrong_attribute(self):
poor_saver = IO(input_filename="BadCars.foo", group_name="Volksvagen")
poor_saver.add_attribute("BadPassengers", np.array([4]))
self.assertRaises(ValueError, poor_saver.save)
def _save_wrong_dataset(self):
poor_saver = IO(input_filename="BadCars.foo", group_name="Volksvagen")
poor_saver.add_data("BadPassengers", 4)
self.assertRaises(ValueError, poor_saver.save)
def _wrong_filename(self):
self.assertRaises(ValueError, IO, input_filename=1, group_name="goodgroup")
def _wrong_groupname(self):
self.assertRaises(ValueError, IO, input_filename="goodfile", group_name=1)
def _wrong_file(self):
poor_loader = IO(input_filename="bumCars", group_name="nice_group")
self.assertRaises(IOError, poor_loader.load, list_of_attributes="one_attribute")
def _loading_attributes(self):
data = self.loader.load(list_of_attributes=["Fuel", "Speed"])
attr_data = data["attributes"]
self.assertEqual(100, attr_data["Fuel"])
self.assertEqual(200, attr_data["Speed"])
self.assertRaises(ValueError, self.loader.load, list_of_attributes=["NiceFuel"])
self.assertRaises(ValueError, self.loader.load, list_of_attributes=1)
self.assertRaises(ValueError, self.loader.load, list_of_attributes=[1, "Speed"])
def _loading_datasets(self):
data = self.loader.load(list_of_datasets=["Passengers", "FireExtinguishers"])
self.assertEqual(np.array([4]), data["datasets"]["Passengers"])
self.assertEqual(np.array([2]), data["datasets"]["FireExtinguishers"])
self.assertRaises(ValueError, self.loader.load, list_of_datasets=["NicePassengers"])
self.assertRaises(ValueError, self.loader.load, list_of_datasets=1)
self.assertRaises(ValueError, self.loader.load, list_of_datasets=[1, "Passengers"])
def _loading_structured_datasets(self):
"""
Loads more complicated data from the hdf file.
"""
data = self.loader.load(list_of_datasets=["wheels", "chairs"])
self.assertEqual([{"Winter": False, "Punctured": False, "Brand": "Mercedes", "Age": 2},
{"Winter": False, "Punctured": False, "Brand": "Mercedes", "Age": 3},
{"Winter": False, "Punctured": False, "Brand": "Mercedes", "Age": 5},
{"Winter": False, "Punctured": True, "Brand": "Mercedes", "Age": 7}],
data["datasets"]["wheels"])
self.assertEqual({"AdjustableHeadrests": True, "ExtraPadding": True},
data["datasets"]["chairs"])
self.assertRaises(ValueError, self.loader.load, list_of_datasets=["WrongDataSet"])
self.assertRaises(ValueError, self.loader.load, list_of_datasets=1)
def runTest(self):
self._save_stuff()
self._save_wrong_attribute()
self._save_wrong_dataset()
self.loader = IO(input_filename="Cars.foo", group_name="Volksvagen")
self._wrong_filename()
self._wrong_groupname()
self._wrong_file()
self._loading_attributes()
self._loading_datasets()
self._loading_structured_datasets()
if __name__ == '__main__':
unittest.main()