|
| 1 | +import contextlib |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import unittest |
| 5 | +from concurrent.futures import Future |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +import numpy as np |
| 9 | + |
| 10 | + |
| 11 | +try: |
| 12 | + from executorlib.standalone.hdf import ( |
| 13 | + load, |
| 14 | + get_output, |
| 15 | + get_runtime, |
| 16 | + get_queue_id, |
| 17 | + get_future_from_cache, |
| 18 | + group_dict, |
| 19 | + ) |
| 20 | + |
| 21 | + import h5py |
| 22 | + import cloudpickle |
| 23 | + |
| 24 | + def dump(file_name: Optional[str], data_dict: dict) -> None: |
| 25 | + """ |
| 26 | + Previous dump function just copied here for backwards compatibility. |
| 27 | + """ |
| 28 | + if file_name is not None: |
| 29 | + file_name_abs = os.path.abspath(file_name) |
| 30 | + os.makedirs(os.path.dirname(file_name_abs), exist_ok=True) |
| 31 | + with h5py.File(file_name_abs, "a") as fname: |
| 32 | + for data_key, data_value in data_dict.items(): |
| 33 | + if data_key in group_dict: |
| 34 | + with contextlib.suppress(ValueError): |
| 35 | + fname.create_dataset( |
| 36 | + name="/" + group_dict[data_key], |
| 37 | + data=np.void(cloudpickle.dumps(data_value)), |
| 38 | + ) |
| 39 | + |
| 40 | + skip_h5py_test = False |
| 41 | +except ImportError: |
| 42 | + skip_h5py_test = True |
| 43 | + |
| 44 | + |
| 45 | +def my_funct(a, b): |
| 46 | + return a + b |
| 47 | + |
| 48 | + |
| 49 | +@unittest.skipIf( |
| 50 | + skip_h5py_test, "h5py is not installed, so the h5io tests are skipped." |
| 51 | +) |
| 52 | +class TestSharedFunctions(unittest.TestCase): |
| 53 | + def test_hdf_mixed(self): |
| 54 | + cache_directory = os.path.abspath("executorlib_cache") |
| 55 | + os.makedirs(cache_directory, exist_ok=True) |
| 56 | + file_name = os.path.join(cache_directory, "test_mixed.h5") |
| 57 | + a = 1 |
| 58 | + b = 2 |
| 59 | + dump( |
| 60 | + file_name=file_name, |
| 61 | + data_dict={"fn": my_funct, "args": [a], "kwargs": {"b": b}}, |
| 62 | + ) |
| 63 | + data_dict = load(file_name=file_name) |
| 64 | + self.assertTrue("fn" in data_dict.keys()) |
| 65 | + self.assertEqual(data_dict["args"], [a]) |
| 66 | + self.assertEqual(data_dict["kwargs"], {"b": b}) |
| 67 | + flag, no_error, output = get_output(file_name=file_name) |
| 68 | + self.assertTrue(get_runtime(file_name=file_name) == 0.0) |
| 69 | + self.assertFalse(no_error) |
| 70 | + self.assertFalse(flag) |
| 71 | + self.assertIsNone(output) |
| 72 | + |
| 73 | + def test_get_future_from_file(self): |
| 74 | + cache_directory = os.path.abspath("executorlib_cache") |
| 75 | + os.makedirs(cache_directory, exist_ok=True) |
| 76 | + file_name = os.path.join(cache_directory, "test_mixed_i.h5") |
| 77 | + a = 1 |
| 78 | + b = 2 |
| 79 | + dump( |
| 80 | + file_name=file_name, |
| 81 | + data_dict={"fn": my_funct, "args": [a], "kwargs": {"b": b}}, |
| 82 | + ) |
| 83 | + future = get_future_from_cache( |
| 84 | + cache_directory=cache_directory, |
| 85 | + cache_key="test_mixed", |
| 86 | + ) |
| 87 | + self.assertTrue(isinstance(future, Future)) |
| 88 | + self.assertFalse(future.done()) |
| 89 | + |
| 90 | + def test_get_output_file_missing(self): |
| 91 | + cache_directory = os.path.abspath("executorlib_cache") |
| 92 | + with self.assertRaises(FileNotFoundError): |
| 93 | + get_output(file_name=os.path.join(cache_directory, "does_not_exist.h5")) |
| 94 | + |
| 95 | + def test_get_future_from_file_missing(self): |
| 96 | + cache_directory = os.path.abspath("executorlib_cache") |
| 97 | + with self.assertRaises(FileNotFoundError): |
| 98 | + get_future_from_cache( |
| 99 | + cache_directory=cache_directory, |
| 100 | + cache_key="does_not_exist", |
| 101 | + ) |
| 102 | + |
| 103 | + def test_hdf_args(self): |
| 104 | + cache_directory = os.path.abspath("executorlib_cache") |
| 105 | + os.makedirs(cache_directory, exist_ok=True) |
| 106 | + file_name = os.path.join(cache_directory, "test_args.h5") |
| 107 | + a = 1 |
| 108 | + b = 2 |
| 109 | + dump(file_name=file_name, data_dict={"fn": my_funct, "args": [a, b]}) |
| 110 | + data_dict = load(file_name=file_name) |
| 111 | + self.assertTrue("fn" in data_dict.keys()) |
| 112 | + self.assertEqual(data_dict["args"], [a, b]) |
| 113 | + self.assertEqual(data_dict["kwargs"], {}) |
| 114 | + flag, no_error, output = get_output(file_name=file_name) |
| 115 | + self.assertTrue(get_runtime(file_name=file_name) == 0.0) |
| 116 | + self.assertFalse(flag) |
| 117 | + self.assertFalse(no_error) |
| 118 | + self.assertIsNone(output) |
| 119 | + |
| 120 | + def test_hdf_kwargs(self): |
| 121 | + cache_directory = os.path.abspath("executorlib_cache") |
| 122 | + os.makedirs(cache_directory, exist_ok=True) |
| 123 | + file_name = os.path.join(cache_directory, "test_kwargs.h5") |
| 124 | + a = 1 |
| 125 | + b = 2 |
| 126 | + dump( |
| 127 | + file_name=file_name, |
| 128 | + data_dict={ |
| 129 | + "fn": my_funct, |
| 130 | + "args": (), |
| 131 | + "kwargs": {"a": a, "b": b}, |
| 132 | + "queue_id": 123, |
| 133 | + "error_log_file": "error.out", |
| 134 | + }, |
| 135 | + ) |
| 136 | + data_dict = load(file_name=file_name) |
| 137 | + self.assertTrue("fn" in data_dict.keys()) |
| 138 | + self.assertEqual(data_dict["args"], ()) |
| 139 | + self.assertEqual(data_dict["kwargs"], {"a": a, "b": b}) |
| 140 | + self.assertEqual(get_queue_id(file_name=file_name), 123) |
| 141 | + flag, no_error, output = get_output(file_name=file_name) |
| 142 | + self.assertTrue(get_runtime(file_name=file_name) == 0.0) |
| 143 | + self.assertFalse(flag) |
| 144 | + self.assertFalse(no_error) |
| 145 | + self.assertIsNone(output) |
| 146 | + |
| 147 | + def test_hdf_missing_funct(self): |
| 148 | + cache_directory = os.path.abspath("executorlib_cache") |
| 149 | + os.makedirs(cache_directory, exist_ok=True) |
| 150 | + file_name = os.path.join(cache_directory, "test_missing_funct.h5") |
| 151 | + dump( |
| 152 | + file_name=file_name, |
| 153 | + data_dict={ |
| 154 | + "queue_id": 123, |
| 155 | + }, |
| 156 | + ) |
| 157 | + with self.assertRaises(TypeError): |
| 158 | + load(file_name=file_name) |
| 159 | + |
| 160 | + def test_hdf_missing_args(self): |
| 161 | + cache_directory = os.path.abspath("executorlib_cache") |
| 162 | + os.makedirs(cache_directory, exist_ok=True) |
| 163 | + file_name = os.path.join(cache_directory, "test_missing_args.h5") |
| 164 | + dump( |
| 165 | + file_name=file_name, |
| 166 | + data_dict={ |
| 167 | + "fn": my_funct, |
| 168 | + }, |
| 169 | + ) |
| 170 | + data_dict = load(file_name=file_name) |
| 171 | + self.assertTrue("fn" in data_dict.keys()) |
| 172 | + self.assertEqual(data_dict["args"], ()) |
| 173 | + flag, no_error, output = get_output(file_name=file_name) |
| 174 | + self.assertTrue(get_runtime(file_name=file_name) == 0.0) |
| 175 | + self.assertFalse(flag) |
| 176 | + self.assertFalse(no_error) |
| 177 | + self.assertIsNone(output) |
| 178 | + |
| 179 | + def test_hdf_queue_id(self): |
| 180 | + cache_directory = os.path.abspath("executorlib_cache") |
| 181 | + os.makedirs(cache_directory, exist_ok=True) |
| 182 | + file_name = os.path.join(cache_directory, "test_queue.h5") |
| 183 | + queue_id = 123 |
| 184 | + dump( |
| 185 | + file_name=file_name, |
| 186 | + data_dict={"queue_id": queue_id}, |
| 187 | + ) |
| 188 | + self.assertEqual(get_queue_id(file_name=file_name), 123) |
| 189 | + flag, no_error, output = get_output(file_name=file_name) |
| 190 | + self.assertTrue(get_runtime(file_name=file_name) == 0.0) |
| 191 | + self.assertFalse(flag) |
| 192 | + self.assertFalse(no_error) |
| 193 | + self.assertIsNone(output) |
| 194 | + |
| 195 | + def test_hdf_error(self): |
| 196 | + cache_directory = os.path.abspath("executorlib_cache") |
| 197 | + os.makedirs(cache_directory, exist_ok=True) |
| 198 | + file_name = os.path.join(cache_directory, "test_error.h5") |
| 199 | + error = ValueError() |
| 200 | + dump( |
| 201 | + file_name=file_name, |
| 202 | + data_dict={"error": error}, |
| 203 | + ) |
| 204 | + flag, no_error, output = get_output(file_name=file_name) |
| 205 | + self.assertTrue(get_runtime(file_name=file_name) == 0.0) |
| 206 | + self.assertTrue(flag) |
| 207 | + self.assertFalse(no_error) |
| 208 | + self.assertTrue(isinstance(output, error.__class__)) |
| 209 | + |
| 210 | + def tearDown(self): |
| 211 | + shutil.rmtree("executorlib_cache", ignore_errors=True) |
0 commit comments