File: test_prov_utils.py

package info (click to toggle)
python-sidpy 0.12.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,988 kB
  • sloc: python: 11,456; makefile: 17
file content (445 lines) | stat: -rw-r--r-- 20,024 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
# -*- coding: utf-8 -*-
"""
Created on Tue Nov  3 15:07:16 2017

@author: Suhas Somnath
"""
from __future__ import division, print_function, unicode_literals, absolute_import
import unittest
import os
import sys
import h5py
import numpy as np

sys.path.append("../../sidpy/")
from sidpy.hdf import prov_utils

from .test_hdf_utils import TestHDFUtilsBase
from . import data_utils


if sys.version_info.major == 3:
    unicode = str

class TestAssignGroupIndex(TestHDFUtilsBase):

    def test_existing(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_group = h5_f['/Raw_Measurement']
            ret_val = prov_utils.assign_group_index(h5_group, 'source_main-Fitter')
            self.assertEqual(ret_val, 'source_main-Fitter_002')

    def test_new(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_group = h5_f['/Raw_Measurement']
            ret_val = prov_utils.assign_group_index(h5_group, 'blah_')
            self.assertEqual(ret_val, 'blah_000')

    def test_invalid_dtypes(self):
        with self.assertRaises(TypeError):
            _ = prov_utils.assign_group_index("not a dataset", 'blah_')

        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_group = h5_f['/Raw_Measurement']
            with self.assertRaises(TypeError):
                _ = prov_utils.assign_group_index(h5_group, 1.24)


class TestCreateIndexedGroup(unittest.TestCase):

    def test_first_group(self):
        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            h5_group = prov_utils.create_indexed_group(h5_f, 'Hello')
            self.assertIsInstance(h5_group, h5py.Group)
            self.assertEqual(h5_group.name, '/Hello_000')
            self.assertEqual(h5_group.parent, h5_f)
            data_utils.verify_book_keeping_attrs(self, h5_group)

            h5_sub_group = prov_utils.create_indexed_group(h5_group, 'Test')
            self.assertIsInstance(h5_sub_group, h5py.Group)
            self.assertEqual(h5_sub_group.name, '/Hello_000/Test_000')
            self.assertEqual(h5_sub_group.parent, h5_group)
            data_utils.verify_book_keeping_attrs(self, h5_sub_group)
        os.remove(file_path)

    def test_second(self):
        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            h5_group_1 = prov_utils.create_indexed_group(h5_f, 'Hello')
            self.assertIsInstance(h5_group_1, h5py.Group)
            self.assertEqual(h5_group_1.name, '/Hello_000')
            self.assertEqual(h5_group_1.parent, h5_f)
            data_utils.verify_book_keeping_attrs(self, h5_group_1)

            h5_group_2 = prov_utils.create_indexed_group(h5_f, 'Hello')
            self.assertIsInstance(h5_group_2, h5py.Group)
            self.assertEqual(h5_group_2.name, '/Hello_001')
            self.assertEqual(h5_group_2.parent, h5_f)
            data_utils.verify_book_keeping_attrs(self, h5_group_2)
        os.remove(file_path)

    def test_w_suffix_(self):
        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            h5_group = prov_utils.create_indexed_group(h5_f, 'Hello_')
            self.assertIsInstance(h5_group, h5py.Group)
            self.assertEqual(h5_group.name, '/Hello_000')
            self.assertEqual(h5_group.parent, h5_f)
            data_utils.verify_book_keeping_attrs(self, h5_group)
        os.remove(file_path)

    def test_empty_base_name(self):
        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            with self.assertRaises(ValueError):
                _ = prov_utils.create_indexed_group(h5_f, '    ')
        os.remove(file_path)

    def test_create_indexed_group_invalid_types(self):
        with self.assertRaises(TypeError):
            _ = prov_utils.create_indexed_group(np.arange(4), "fddfd")

        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            with self.assertRaises(TypeError):
                _ = prov_utils.create_indexed_group(h5_f, 1.2343)
        os.remove(file_path)


class TestCreateResultsGroup(unittest.TestCase):

    def test_first(self):
        self.helper_first()

    def test_dash_in_name(self):
        self.helper_first(add_dash_to_name=True)

    def helper_first(self, add_dash_to_name=False):
        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            h5_dset = h5_f.create_dataset('Main', data=[1, 2, 3])
            if add_dash_to_name:
                h5_group = prov_utils.create_results_group(h5_dset, 'Some-Tool')
                tool_name = 'Some_Tool'
            else:
                tool_name = 'Tool'
                h5_group = prov_utils.create_results_group(h5_dset, tool_name)
            self.assertIsInstance(h5_group, h5py.Group)
            self.assertEqual(h5_group.name, '/Main-' + tool_name + '_000')
            self.assertEqual(h5_group.parent, h5_f)
            data_utils.verify_book_keeping_attrs(self, h5_group)

            h5_dset = h5_group.create_dataset('Main_Dataset', data=[1, 2, 3])
            h5_sub_group = prov_utils.create_results_group(h5_dset, 'SHO_Fit')
            self.assertIsInstance(h5_sub_group, h5py.Group)
            self.assertEqual(h5_sub_group.name, '/Main-' + tool_name + '_000/Main_Dataset-SHO_Fit_000')
            self.assertEqual(h5_sub_group.parent, h5_group)
            data_utils.verify_book_keeping_attrs(self, h5_sub_group)
        os.remove(file_path)

    def test_second(self):
        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            h5_dset = h5_f.create_dataset('Main', data=[1, 2, 3])
            h5_group = prov_utils.create_results_group(h5_dset, 'Tool')
            self.assertIsInstance(h5_group, h5py.Group)
            self.assertEqual(h5_group.name, '/Main-Tool_000')
            self.assertEqual(h5_group.parent, h5_f)
            data_utils.verify_book_keeping_attrs(self, h5_group)

            h5_sub_group = prov_utils.create_results_group(h5_dset, 'Tool')
            self.assertIsInstance(h5_sub_group, h5py.Group)
            self.assertEqual(h5_sub_group.name, '/Main-Tool_001')
            self.assertEqual(h5_sub_group.parent, h5_f)
            data_utils.verify_book_keeping_attrs(self, h5_sub_group)
        os.remove(file_path)

    def test_empty_tool_name(self):
        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            h5_dset = h5_f.create_dataset('Main', data=[1, 2, 3])
            with self.assertRaises(ValueError):
                _ = prov_utils.create_results_group(h5_dset, '   ')
        os.remove(file_path)

    def test_invalid_types(self):
        with self.assertRaises(TypeError):
            _ = prov_utils.create_results_group("not a dataset", 'Tool')

        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            with self.assertRaises(TypeError):
                _ = prov_utils.create_results_group(h5_f, 'Tool')

            h5_dset = h5_f.create_dataset('Main', data=[1, 2, 3])
            with self.assertRaises(TypeError):
                _ = prov_utils.create_results_group(h5_dset, 'Tool',
                                                   h5_parent_group='not_group')

        os.remove(file_path)
        
    def test_different_file(self):
        file_path = 'test.h5'
        new_path = 'new.h5'
        data_utils.delete_existing_file(file_path)
        data_utils.delete_existing_file(new_path)

        with h5py.File(file_path, mode='w') as h5_f:
            h5_dset = h5_f.create_dataset('Main', data=[1, 2, 3])
            # Ensuring that index is calculated at destination, not source:
            _ = h5_f.create_group('Main-Tool_000')

            with h5py.File(new_path, mode='w') as h5_f_new:
                _ = h5_f_new.create_group('Main-Tool_000')

                h5_group = prov_utils.create_results_group(h5_dset, 'Tool',
                                                          h5_parent_group=h5_f_new)

                self.assertIsInstance(h5_group, h5py.Group)
                self.assertEqual(h5_group.name, '/Main-Tool_001')
                self.assertEqual(h5_group.parent, h5_f_new)
                self.assertNotEqual(h5_dset.file, h5_group.file)
                data_utils.verify_book_keeping_attrs(self, h5_group)

        os.remove(file_path)
        os.remove(new_path)


class TestFindResultsGroup(TestHDFUtilsBase):

    def test_legal(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            expected_groups = [
                h5_f['/Raw_Measurement/source_main-Fitter_000'],
                h5_f['/Raw_Measurement/source_main-Fitter_001']]
            ret_val = prov_utils.find_results_groups(h5_main, 'Fitter')
            self.assertEqual(set(ret_val), set(expected_groups))

    def test_no_dset(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            with self.assertRaises(TypeError):
                _ = prov_utils.find_results_groups(h5_f, 'Fitter')

    def test_not_string(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            with self.assertRaises(TypeError):
                _ = prov_utils.find_results_groups(h5_main, np.arange(5))

    def test_no_such_tool(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            ret_val = prov_utils.find_results_groups(h5_main, 'Blah')
            self.assertEqual(len(ret_val), 0)

    def test_results_in_diff_file(self):
        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)

        new_path = 'new.h5'
        data_utils.delete_existing_file(new_path)

        with h5py.File(file_path, mode='w') as h5_f:
            h5_main = h5_f.create_dataset('Main', data=[1, 2, 3])
            with h5py.File(new_path, mode='w') as h5_f_2:
                grp_1 = h5_f_2.create_group('Main-Tool_000')
                grp_2 = h5_f_2.create_group('Main-Tool_001')
                grps = prov_utils.find_results_groups(h5_main, 'Tool',
                                                     h5_parent_group=h5_f_2)
                self.assertEqual(set([grp_1, grp_2]), set(grps))

        os.remove(file_path)
        os.remove(new_path)

    def test_results_in_diff_file_invalid_type(self):
        file_path = 'test.h5'
        data_utils.delete_existing_file(file_path)
        with h5py.File(file_path, mode='w') as h5_f:
            h5_main = h5_f.create_dataset('Main', data=[1, 2, 3])
            with self.assertRaises(TypeError):
                _ = prov_utils.find_results_groups(h5_main, 'Tool',
                                                  h5_parent_group=h5_main)

        os.remove(file_path)
        
    
class TestCheckForMatchingAttrs(TestHDFUtilsBase):

    def test_dset_no_attrs(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            self.assertTrue(prov_utils.check_for_matching_attrs(h5_main, new_parms=None))

    def test_dset_matching_attrs(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            attrs = {'units': 'A', 'quantity':'Current'}
            self.assertTrue(prov_utils.check_for_matching_attrs(h5_main, new_parms=attrs))

    def test_dset_one_mismatched_attrs(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            attrs = {'units': 'A', 'blah': 'meh'}
            self.assertFalse(prov_utils.check_for_matching_attrs(h5_main, new_parms=attrs))

    def test_grp(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main-Fitter_000']
            attrs = {'att_1': 'string_val', 'att_2': 1.2345,
                     'att_3': [1, 2, 3, 4], 'att_4': ['str_1', 'str_2', 'str_3']}
            self.assertTrue(prov_utils.check_for_matching_attrs(h5_main, new_parms=attrs))

    def test_grp_mismatched_types_01(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main-Fitter_000']
            attrs = {'att_4': 'string_val'}
            self.assertFalse(prov_utils.check_for_matching_attrs(h5_main, new_parms=attrs))

    def test_grp_mismatched_types_02(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main-Fitter_000']
            attrs = {'att_1': ['str_1', 'str_2', 'str_3']}
            self.assertFalse(prov_utils.check_for_matching_attrs(h5_main, new_parms=attrs))

    def test_grp_mismatched_types_03(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main-Fitter_000']
            attrs = {'att_4': [1, 4.234, 'str_3']}
            self.assertFalse(prov_utils.check_for_matching_attrs(h5_main, new_parms=attrs))

    def test_grp_mismatched_types_04(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main-Fitter_000']
            attrs = {'att_4': [1, 4.234, 45]}
            self.assertFalse(prov_utils.check_for_matching_attrs(h5_main, new_parms=attrs))


class TestCheckForOld(TestHDFUtilsBase):

    def test_invalid_types(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            with self.assertRaises(TypeError):
                _ = prov_utils.check_for_old("h5_main", "blah")

            with self.assertRaises(TypeError):
                _ = prov_utils.check_for_old(np.arange(4), "blah")

            with self.assertRaises(TypeError):
                _ = prov_utils.check_for_old(h5_main, 1.234)

            with self.assertRaises(TypeError):
                _ = prov_utils.check_for_old(h5_main, 'Fitter',
                                            new_parms="not_a_dictionary")

            with self.assertRaises(TypeError):
                _ = prov_utils.check_for_old(h5_main, 'Fitter',
                                            target_dset=1.234)

    def test_valid_target_dset(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            attrs = {'units': ['V'], 'labels': ['Bias']}
            dset_name = 'Spectroscopic_Indices'
            groups = prov_utils.check_for_old(h5_main, 'Fitter',
                                             new_parms=attrs,
                                             target_dset=dset_name,
                                             verbose=False)
            groups = set(groups)
            self.assertEqual(groups, set([h5_f['/Raw_Measurement/source_main-Fitter_000/'],
                                          h5_f['/Raw_Measurement/source_main-Fitter_001/']]))

    def test_invalid_target_dset(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            attrs = {'att_1': 'string_val', 'att_2': 1.2345,
                     'att_3': [1, 2, 3, 4], 'att_4': ['str_1', 'str_2',
                                                      'str_3']}
            ret = prov_utils.check_for_old(h5_main, 'Fitter', new_parms=attrs,
                                          target_dset='Does_not_exist')
            self.assertEqual(ret, [])

    def test_exact_match(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            attrs = {'att_1': 'string_val', 'att_2': 1.2345,
                     'att_3': [1, 2, 3, 4], 'att_4': ['str_1', 'str_2', 'str_3']}
            [h5_ret_grp] = prov_utils.check_for_old(h5_main, 'Fitter',
                                                   new_parms=attrs,
                                                   target_dset=None)
            self.assertEqual(h5_ret_grp, h5_f['/Raw_Measurement/source_main-Fitter_000'])

    def test_subset_but_match(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            attrs = {'att_2': 1.2345,
                     'att_3': [1, 2, 3, 4], 'att_4': ['str_1', 'str_2', 'str_3']}
            [h5_ret_grp] = prov_utils.check_for_old(h5_main, 'Fitter',
                                                   new_parms=attrs,
                                                   target_dset=None)
            self.assertEqual(h5_ret_grp, h5_f['/Raw_Measurement/source_main-Fitter_000'])

    def test_exact_match_02(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            attrs = {'att_1': 'other_string_val', 'att_2': 5.4321,
                     'att_3': [4, 1, 3], 'att_4': ['s', 'str_2', 'str_3']}
            [h5_ret_grp] = prov_utils.check_for_old(h5_main, 'Fitter',
                                                   new_parms=attrs,
                                                   target_dset=None)
            self.assertEqual(h5_ret_grp, h5_f['/Raw_Measurement/source_main-Fitter_001'])

    def test_fail_01(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            attrs = {'att_1': [4, 1, 3], 'att_2': ['s', 'str_2', 'str_3'],
                     'att_3': 'other_string_val', 'att_4': 5.4321}
            ret_val = prov_utils.check_for_old(h5_main, 'Fitter',
                                              new_parms=attrs, target_dset=None)
            self.assertIsInstance(ret_val, list)
            self.assertEqual(len(ret_val), 0)

    def test_fail_02(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_main = h5_f['/Raw_Measurement/source_main']
            attrs = {'att_x': [4, 1, 3], 'att_z': ['s', 'str_2', 'str_3'],
                     'att_y': 'other_string_val', 'att_4': 5.4321}
            ret_val = prov_utils.check_for_old(h5_main, 'Fitter',
                                              new_parms=attrs, target_dset=None)
            self.assertIsInstance(ret_val, list)
            self.assertEqual(len(ret_val), 0)


class TestGetSourceDataset(TestHDFUtilsBase):

    def test_legal(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            h5_groups = [h5_f['/Raw_Measurement/source_main-Fitter_000'],
                        h5_f['/Raw_Measurement/source_main-Fitter_001']]
            h5_main = h5_f['/Raw_Measurement/source_main']
            for h5_grp in h5_groups:
                self.assertEqual(h5_main, prov_utils.get_source_dataset(h5_grp))

    def test_invalid_type(self):
        with self.assertRaises(TypeError):
            _ = prov_utils.get_source_dataset('/Raw_Measurement/Misc')

    def test_illegal(self):
        with h5py.File(data_utils.std_beps_path, mode='r') as h5_f:
            with self.assertRaises(ValueError):
                _ = prov_utils.get_source_dataset(h5_f['/Raw_Measurement/Misc'])


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