|
| 1 | +from __future__ import absolute_import, division, print_function |
| 2 | +import sys |
| 3 | + |
| 4 | +def run(args): |
| 5 | + assert len(args) == 0 |
| 6 | + try: |
| 7 | + import numpy as np |
| 8 | + except ImportError: |
| 9 | + print("numpy not available, skipping") |
| 10 | + print("OK") |
| 11 | + return |
| 12 | + from scitbx.array_family import flex |
| 13 | + |
| 14 | + exercise_original_reproducer(np, flex) |
| 15 | + exercise_element_setitem(np, flex) |
| 16 | + exercise_element_iadd(np, flex) |
| 17 | + exercise_array_iadd(np, flex) |
| 18 | + exercise_construction_from_numpy_scalars(np, flex) |
| 19 | + exercise_value_preservation(np, flex) |
| 20 | + print("OK") |
| 21 | + |
| 22 | +def exercise_original_reproducer(np, flex): |
| 23 | + """Reproducer from https://github.com/cctbx/cctbx_project/issues/1084""" |
| 24 | + arr = flex.double(10) |
| 25 | + arr[0] += np.array([1,2,3], dtype=np.float32)[0] |
| 26 | + assert arr[0] == 1.0 |
| 27 | + |
| 28 | +def exercise_element_setitem(np, flex): |
| 29 | + """Test arr[i] = numpy_scalar for various type combinations.""" |
| 30 | + # float scalars -> flex.double |
| 31 | + a = flex.double(1) |
| 32 | + for val in [np.float32(3.5), np.float64(3.5)]: |
| 33 | + a[0] = val |
| 34 | + assert a[0] == 3.5, (a[0], type(val)) |
| 35 | + # integer scalars -> flex.double (implicit widening) |
| 36 | + for val in [np.int32(7), np.int64(7)]: |
| 37 | + a[0] = val |
| 38 | + assert a[0] == 7.0, (a[0], type(val)) |
| 39 | + # float scalars -> flex.float |
| 40 | + b = flex.float(1) |
| 41 | + for val in [np.float32(2.5), np.float64(2.5)]: |
| 42 | + b[0] = val |
| 43 | + assert abs(b[0] - 2.5) < 1e-6, (b[0], type(val)) |
| 44 | + # integer scalars -> flex.int |
| 45 | + c = flex.int(1) |
| 46 | + for val in [np.int32(42), np.int64(42)]: |
| 47 | + c[0] = val |
| 48 | + assert c[0] == 42, (c[0], type(val)) |
| 49 | + |
| 50 | +def exercise_element_iadd(np, flex): |
| 51 | + """Test arr[i] += numpy_scalar for various type combinations.""" |
| 52 | + a = flex.double([10.0]) |
| 53 | + a[0] += np.float32(2.5) |
| 54 | + assert a[0] == 12.5 |
| 55 | + a[0] += np.float64(1.0) |
| 56 | + assert a[0] == 13.5 |
| 57 | + a[0] += np.int32(1) |
| 58 | + assert a[0] == 14.5 |
| 59 | + a[0] += np.int64(1) |
| 60 | + assert a[0] == 15.5 |
| 61 | + |
| 62 | + b = flex.int([10]) |
| 63 | + b[0] += np.int32(5) |
| 64 | + assert b[0] == 15 |
| 65 | + b[0] += np.int64(3) |
| 66 | + assert b[0] == 18 |
| 67 | + |
| 68 | +def exercise_array_iadd(np, flex): |
| 69 | + """Test arr += numpy_scalar (whole-array operations).""" |
| 70 | + a = flex.double([1.0, 2.0, 3.0]) |
| 71 | + a += np.float32(10.0) |
| 72 | + assert list(a) == [11.0, 12.0, 13.0] |
| 73 | + a += np.float64(1.0) |
| 74 | + assert list(a) == [12.0, 13.0, 14.0] |
| 75 | + a += np.int32(1) |
| 76 | + assert list(a) == [13.0, 14.0, 15.0] |
| 77 | + |
| 78 | + b = flex.int([1, 2, 3]) |
| 79 | + b += np.int32(10) |
| 80 | + assert list(b) == [11, 12, 13] |
| 81 | + b += np.int64(1) |
| 82 | + assert list(b) == [12, 13, 14] |
| 83 | + |
| 84 | +def exercise_construction_from_numpy_scalars(np, flex): |
| 85 | + """Test constructing flex arrays from lists containing numpy scalars.""" |
| 86 | + a = flex.double([np.float32(1.0), np.float64(2.0), np.float32(3.0)]) |
| 87 | + assert list(a) == [1.0, 2.0, 3.0] |
| 88 | + b = flex.int([np.int32(1), np.int32(2), np.int32(3)]) |
| 89 | + assert list(b) == [1, 2, 3] |
| 90 | + |
| 91 | +def exercise_value_preservation(np, flex): |
| 92 | + """Test that values are preserved accurately through conversion.""" |
| 93 | + # float32 has ~7 decimal digits of precision |
| 94 | + a = flex.double(1) |
| 95 | + a[0] = np.float32(1.23456789) |
| 96 | + # float32 truncates, so check against float32 precision |
| 97 | + assert abs(a[0] - float(np.float32(1.23456789))) < 1e-10 |
| 98 | + |
| 99 | + # float64 should be exact for representable values |
| 100 | + a[0] = np.float64(1.234567890123456) |
| 101 | + assert a[0] == 1.234567890123456 |
| 102 | + |
| 103 | + # Large integers |
| 104 | + b = flex.int(1) |
| 105 | + b[0] = np.int32(2147483647) # INT32_MAX |
| 106 | + assert b[0] == 2147483647 |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + run(args=sys.argv[1:]) |
0 commit comments