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
|
import contextlib
import gc
import numpy as np
from numpy.testing import assert_array_equal
import asdf
from asdf.extension import Converter, Extension
from asdf.testing import helpers
class BlockData:
def __init__(self, payload):
self.payload = payload
class BlockConverter(Converter):
tags = ["asdf://somewhere.org/tags/block_data-1.0.0"]
types = [BlockData]
_return_invalid_keys = False
def to_yaml_tree(self, obj, tag, ctx):
# lookup source for obj
block_index = ctx.find_available_block_index(
lambda: np.ndarray(len(obj.payload), dtype="uint8", buffer=obj.payload),
)
return {
"block_index": block_index,
}
def from_yaml_tree(self, node, tag, ctx):
block_index = node["block_index"]
data = ctx.get_block_data_callback(block_index)()
obj = BlockData(data.tobytes())
return obj
class BlockExtension(Extension):
tags = ["asdf://somewhere.org/tags/block_data-1.0.0"]
converters = [BlockConverter()]
extension_uri = "asdf://somewhere.org/extensions/block_data-1.0.0"
@contextlib.contextmanager
def with_extension(ext_class):
with asdf.config_context() as cfg:
cfg.add_extension(ext_class())
yield
@with_extension(BlockExtension)
def test_roundtrip_block_data():
a = BlockData(b"abcdefg")
b = helpers.roundtrip_object(a)
assert a.payload == b.payload
@with_extension(BlockExtension)
def test_block_converter_block_allocation(tmp_path):
a = BlockData(b"abcdefg")
# make a tree without the BlockData instance to avoid
# the initial validate which will trigger block allocation
af = asdf.AsdfFile({"a": None})
# now assign to the tree item (avoiding validation)
af["a"] = a
# they currently hold storage settings
fn = tmp_path / "test.asdf"
af.write_to(fn)
# if we read a file
with asdf.open(fn, mode="rw") as af:
fn2 = tmp_path / "test2.asdf"
# there should be 1 block
assert len(af._blocks.blocks) == 1
# validate should use that block
af.validate()
assert len(af._blocks.blocks) == 1
# as should write_to
af.write_to(fn2)
assert len(af._blocks.blocks) == 1
# and update
af.update()
assert len(af._blocks.blocks) == 1
class BlockDataCallback:
"""An example object that uses the data callback to read block data"""
def __init__(self, callback):
self.callback = callback
@property
def data(self):
return self.callback()
class BlockDataCallbackConverter(Converter):
tags = ["asdf://somewhere.org/tags/block_data_callback-1.0.0"]
types = [BlockDataCallback]
def to_yaml_tree(self, obj, tag, ctx):
block_index = ctx.find_available_block_index(obj.callback)
return {
"block_index": block_index,
}
def from_yaml_tree(self, node, tag, ctx):
block_index = node["block_index"]
obj = BlockDataCallback(ctx.get_block_data_callback(block_index))
return obj
class BlockDataCallbackExtension(Extension):
tags = ["asdf://somewhere.org/tags/block_data_callback-1.0.0"]
converters = [BlockDataCallbackConverter()]
extension_uri = "asdf://somewhere.org/extensions/block_data_callback-1.0.0"
@with_extension(BlockDataCallbackExtension)
def test_block_data_callback_converter(tmp_path):
# use a callback that every time generates a new array
# this would cause issues for the old block management as the
# id(arr) would change every time
a = BlockDataCallback(lambda: np.zeros(3, dtype="uint8"))
tfn = tmp_path / "tmp.asdf"
asdf.AsdfFile({"obj": a}).write_to(tfn)
with asdf.open(tfn) as af:
assert_array_equal(a.data, af["obj"].data)
# make a tree without the BlockData instance to avoid
# the initial validate which will trigger block allocation
af = asdf.AsdfFile({"a": None})
# now assign to the tree item (avoiding validation)
af["a"] = a
# write_to will use the block
fn1 = tmp_path / "test.asdf"
af.write_to(fn1)
# if we read a file
with asdf.open(fn1, mode="rw") as af:
fn2 = tmp_path / "test2.asdf"
# there should be 1 block
assert len(af._blocks.blocks) == 1
# validate should use that block
af.validate()
assert len(af._blocks.blocks) == 1
# as should write_to
af.write_to(fn2)
assert len(af._blocks.blocks) == 1
# and update
af.update()
assert len(af._blocks.blocks) == 1
# check that data was preserved
for fn in (fn1, fn2):
with asdf.open(fn) as af:
assert_array_equal(af["a"].data, a.data)
@with_extension(BlockDataCallbackExtension)
def test_block_with_callback_removal(tmp_path):
fn1 = tmp_path / "test1.asdf"
fn2 = tmp_path / "test2.asdf"
a = BlockDataCallback(lambda: np.zeros(3, dtype="uint8"))
b = BlockDataCallback(lambda: np.ones(3, dtype="uint8"))
base_af = asdf.AsdfFile({"a": a, "b": b})
base_af.write_to(fn1)
for remove_key, check_key in [("a", "b"), ("b", "a")]:
# check that removing one does not interfere with the other
with asdf.open(fn1) as af:
af[remove_key] = None
af.write_to(fn2)
with asdf.open(fn2) as af:
af[check_key] = b.data
# also test update
# first copy fn1 to fn2
with asdf.open(fn1) as af:
af.write_to(fn2)
with asdf.open(fn2, mode="rw") as af:
af[remove_key] = None
af.update()
af[check_key] = b.data
class MultiBlockData:
def __init__(self, data):
self.data = data
self.keys = []
class MultiBlockConverter(Converter):
tags = ["asdf://somewhere.org/tags/multi_block_data-1.0.0"]
types = [MultiBlockData]
def to_yaml_tree(self, obj, tag, ctx):
if not len(obj.keys):
obj.keys = [ctx.generate_block_key() for _ in obj.data]
indices = [ctx.find_available_block_index(d, k) for d, k in zip(obj.data, obj.keys)]
return {
"indices": indices,
}
def from_yaml_tree(self, node, tag, ctx):
indices = node["indices"]
keys = [ctx.generate_block_key() for _ in indices]
cbs = [ctx.get_block_data_callback(i, k) for i, k in zip(indices, keys)]
obj = MultiBlockData([cb() for cb in cbs])
obj.keys = keys
return obj
class MultiBlockExtension(Extension):
tags = ["asdf://somewhere.org/tags/multi_block_data-1.0.0"]
converters = [MultiBlockConverter()]
extension_uri = "asdf://somewhere.org/extensions/multi_block_data-1.0.0"
@with_extension(MultiBlockExtension)
def test_mutli_block():
a = MultiBlockData([np.arange(3, dtype="uint8") for i in range(3)])
b = helpers.roundtrip_object(a)
assert len(a.data) == len(b.data)
assert [np.testing.assert_array_equal(aa, ab) for aa, ab in zip(a.data, b.data)]
class SharedBlockData:
def __init__(self, callback):
self.callback = callback
@property
def data(self):
return self.callback()
class SharedBlockConverter(Converter):
tags = ["asdf://somewhere.org/tags/shared_block_data-1.0.0"]
types = [SharedBlockData]
_return_invalid_keys = False
def to_yaml_tree(self, obj, tag, ctx):
# lookup source for obj
block_index = ctx.find_available_block_index(
lambda: obj.data,
)
return {
"block_index": block_index,
}
def from_yaml_tree(self, node, tag, ctx):
block_index = node["block_index"]
callback = ctx.get_block_data_callback(block_index)
obj = SharedBlockData(callback)
return obj
class SharedBlockExtension(Extension):
tags = ["asdf://somewhere.org/tags/shared_block_data-1.0.0"]
converters = [SharedBlockConverter()]
extension_uri = "asdf://somewhere.org/extensions/shared_block_data-1.0.0"
@with_extension(SharedBlockExtension)
def test_shared_block_reassignment(tmp_path):
fn = tmp_path / "test.asdf"
arr1 = np.arange(10, dtype="uint8")
arr2 = np.arange(5, dtype="uint8")
a = SharedBlockData(lambda: arr1)
b = SharedBlockData(lambda: arr1)
asdf.AsdfFile({"a": a, "b": b}).write_to(fn)
with asdf.open(fn, mode="rw") as af:
af["b"].callback = lambda: arr2
af.update()
with asdf.open(fn) as af:
np.testing.assert_array_equal(af["a"].data, arr1)
np.testing.assert_array_equal(af["b"].data, arr2)
@with_extension(SharedBlockExtension)
def test_shared_block_obj_removal(tmp_path):
fn = tmp_path / "test.asdf"
arr1 = np.arange(10, dtype="uint8")
a = SharedBlockData(lambda: arr1)
b = SharedBlockData(lambda: arr1)
asdf.AsdfFile({"a": a, "b": b}).write_to(fn)
with asdf.open(fn, mode="rw") as af:
af["b"] = None
del b
gc.collect(2)
af.update()
with asdf.open(fn) as af:
np.testing.assert_array_equal(af["a"].data, arr1)
assert af["b"] is None
|