File size: 10,822 Bytes
2c3c408 | 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 | import contextlib
import datetime
import glob
import importlib
import os
import random
import shutil
import subprocess
import sys
import tempfile
import traceback
import urllib
import uuid
import numpy as np
import pytest
from numpy.testing import assert_almost_equal, assert_array_equal, assert_equal
from packaging.version import Version
import tiledb
SUPPORTED_DATETIME64_DTYPES = tuple(
np.dtype(f"M8[{res}]") for res in "Y M W D h m s ms us ns".split()
)
def has_pandas():
try:
import pandas as pd
except ImportError:
return False
if Version(pd.__version__) < Version("1.0") or Version(pd.__version__) >= Version(
"3.0.0.dev0"
):
return False
return True
def has_pyarrow():
try:
import pyarrow as pa
if Version(pa.__version__) < Version("1.0"):
return False
except ImportError:
return False
return True
def assert_tail_equal(a, *rest, **kwargs):
"""Assert that all arrays in target equal first array"""
for target in rest:
assert_array_equal(a, target, **kwargs)
def create_vfs_dir(path):
"""Create a directory at the given scheme-prefixed path,
first creating the base bucket if needed."""
split_uri = urllib.parse.urlsplit(path)
scheme = split_uri.scheme
bucket = split_uri.netloc
bucket_uri = scheme + "://" + bucket
vfs = tiledb.VFS()
if not vfs.is_bucket(bucket_uri):
vfs.create_bucket(bucket_uri)
vfs.create_dir(path)
class DiskTestCase:
"""Helper class to store paths and associated allocation frames. This is both
a cleanup step and a test of resource management. Some platforms will
refuse to delete an open file, indicating a potentially leaked resource.
"""
@classmethod
def setup_method(self):
# .lower: because bucket name must be all lowercase
prefix = "tiledb-" + self.__name__.lower()
if hasattr(pytest, "tiledb_vfs") and pytest.tiledb_vfs == "s3":
self.path_scheme = pytest.tiledb_vfs + "://"
self.rootdir = self.path_scheme + prefix + str(random.randint(0, 10e10))
create_vfs_dir(self.rootdir)
else:
self.path_scheme = ""
self.rootdir = tempfile.mkdtemp(prefix=prefix)
self.vfs = tiledb.VFS()
self.pathmap = dict()
@classmethod
def teardown_method(self):
# Remove every directory starting with rootdir
# This is both a clean-up step and an implicit test
# of proper resource deallocation (see notes below)
for dirpath in glob.glob(self.rootdir + "*"):
try:
shutil.rmtree(dirpath)
except OSError as exc:
print(
"test '{}' error deleting '{}'".format(
self.__class__.__name__, dirpath
)
)
print("registered paths and originating functions:")
for path, frame in self.pathmap.items():
print(" '{}' <- '{}'".format(path, frame))
raise exc
def path(self, basename=None, shared=False):
if self.path_scheme:
basename = basename if basename else str(uuid.uuid4())
out = os.path.join(self.rootdir, basename)
self.vfs.create_dir(out)
else:
if basename is not None:
# Note: this must be `is not None` because we need to match empty string
out = os.path.abspath(os.path.join(self.rootdir, basename))
else:
out = tempfile.mkdtemp(dir=self.rootdir)
if os.name == "nt" and shared:
subprocess.run(
f'cmd //c "net share tiledb-shared={out}"', shell=True, check=True
)
# We have had issues in both py and libtiledb in the past
# where files were not released (eg: destructor not called)
# Often this is invisible on POSIX platforms, but will
# cause errors on Windows because two processes cannot access
# the same file at once.
# In order to debug this issue, we save the caller where
# this path was allocated so that we can determine what
# test created an unreleased file
frame = traceback.extract_stack(limit=2)[-2][2]
self.pathmap[out] = frame
return out
def assertRaises(self, *args):
return pytest.raises(*args)
def assertRaisesRegex(self, e, m):
return pytest.raises(e, match=m)
@contextlib.contextmanager
def assertEqual(self, *args):
if not len(args) == 2:
raise Exception("Unexpected input len > 2 to assertEquals")
assert args[0] == args[1]
@contextlib.contextmanager
def assertNotEqual(self, *args):
if not len(args) == 2:
raise Exception("Unexpected input len > 2 to assertEquals")
assert args[0] != args[1]
@contextlib.contextmanager
def assertTrue(self, a, msg=None):
if msg:
assert a, msg
else:
assert a
@contextlib.contextmanager
def assertFalse(self, a):
assert a is False
@contextlib.contextmanager
def assertIsInstance(self, v, t):
assert isinstance(v, t)
@contextlib.contextmanager
def assertSetEqual(self, s1, s2):
assert all(isinstance(x, set) for x in (s1, s2))
assert s1 == s2
@contextlib.contextmanager
def assertIsNone(self, a1):
assert a1 is None
@contextlib.contextmanager
def assertTupleEqual(self, a1, a2):
assert a1 == a2
@contextlib.contextmanager
def assertAlmostEqual(self, a1, a2):
assert_almost_equal(a1, a2)
# exclude whitespace: if we generate unquoted newline then pandas will be confused
_ws_set = set("\n\t\r")
def gen_chr(max, printable=False):
while True:
# TODO we exclude 0x0 here because the key API does not embedded NULL
s = chr(random.randrange(1, max))
if printable and (not s.isprintable()) or (s in _ws_set):
continue
if len(s) > 0:
break
return s
def rand_utf8(size=5, printable=False):
# This is a hack to ensure that all UTF-8 is parseable. It would be nicer to
# exclude invalid surrogate pairs, but this will do.
while True:
try:
v = "".join([gen_chr(0xD007F, printable=printable) for _ in range(0, size)])
return v.encode("UTF-8").decode()
except UnicodeError:
continue
def rand_ascii(size=5, printable=False):
return "".join([gen_chr(127, printable) for _ in range(0, size)])
def rand_ascii_bytes(size=5, printable=False):
return b"".join([gen_chr(127, printable).encode("utf-8") for _ in range(0, size)])
def dtype_max(dtype):
if not np.issubdtype(dtype, np.generic):
raise TypeError("expected numpy dtype!")
if np.issubdtype(dtype, np.floating):
finfo = np.finfo(dtype)
return finfo.max
elif np.issubdtype(dtype, np.integer):
iinfo = np.iinfo(dtype)
return int(iinfo.max)
elif np.issubdtype(dtype, np.datetime64):
return np.datetime64(datetime.datetime.max)
raise f"Unknown dtype for dtype_max '{dtype}'"
def dtype_min(dtype):
if not np.issubdtype(dtype, np.generic):
raise TypeError("expected numpy dtype!")
if np.issubdtype(dtype, np.floating):
finfo = np.finfo(dtype)
return finfo.min
elif np.issubdtype(dtype, np.integer):
iinfo = np.iinfo(dtype)
return int(iinfo.min)
elif np.issubdtype(dtype, np.datetime64):
return np.datetime64(datetime.datetime.min)
raise f"Unknown dtype for dtype_min '{dtype}'"
def rand_datetime64_array(
size, start=None, stop=None, include_extremes=True, dtype=None
):
if not dtype:
dtype = np.dtype("M8[ns]")
# generate randint inbounds on the range of the dtype
units = np.datetime_data(dtype)[0]
intmin, intmax = np.iinfo(np.int64).min, np.iinfo(np.int64).max
if start is None:
start = np.datetime64(intmin + 1, units)
else:
start = np.datetime64(start)
if stop is None:
stop = np.datetime64(intmax, units)
else:
stop = np.datetime64(stop)
arr = np.random.randint(
start.astype(dtype).astype(np.int64),
stop.astype(dtype).astype(np.int64),
size=size,
dtype=np.int64,
)
arr.sort()
arr = arr.astype(dtype)
# enable after fix for core issue: ch 7192
if include_extremes:
arr[0] = start
arr[-1] = stop
return arr
def intspace(start, stop, num=50, dtype=np.int64):
"""
Return evenly spaced values over range ensuring that stop is
always the maximum (will not overflow with int dtype as linspace)
:param start:
:param stop:
:param num:
:param dtype:
:return:
"""
rval = np.zeros(num, dtype=dtype)
step = (stop - start) // num
nextval = start
if np.issubdtype(dtype, np.integer) and step < 1:
raise ValueError(
"Cannot use non-integral step value '{}' for integer dtype!".format(step)
)
for i in range(num):
rval[i] = nextval
nextval += step
rval[-1] = stop
return rval
def assert_unordered_equal(a1, a2, unordered=True):
"""Assert that arrays are equal after sorting if
`unordered==True`"""
if unordered:
a1 = np.sort(a1)
a2 = np.sort(a2)
assert_array_equal(a1, a2)
def assert_subarrays_equal(a, b, ordered=True):
assert_equal(a.shape, b.shape)
if not ordered:
a = np.sort(a)
b = np.sort(b)
for a_el, b_el in zip(a.flat, b.flat):
assert_array_equal(a_el, b_el)
def assert_dict_arrays_equal(d1, d2, ordered=True):
assert d1.keys() == d2.keys(), "Keys not equal"
if ordered:
for k in d1.keys():
assert_array_equal(d1[k], d2[k])
else:
d1_dtypes = [tuple((name, value.dtype)) for name, value in d1.items()]
d1_records = [tuple(values) for values in zip(*d1.values())]
array1 = np.array(d1_records, dtype=d1_dtypes)
d2_dtypes = [tuple((name, value.dtype)) for name, value in d2.items()]
d2_records = [tuple(values) for values in zip(*d2.values())]
array2 = np.array(d2_records, dtype=d2_dtypes)
assert_unordered_equal(array1, array2, True)
def assert_captured(cap, expected):
if sys.platform != "win32":
import ctypes
libc = ctypes.CDLL(None)
libc.fflush(None)
out, err = cap.readouterr()
assert not err
assert expected in out
@pytest.fixture(scope="module", params=["hilbert", "row-major"])
def fx_sparse_cell_order(request):
yield request.param
|