xobjects 0.5.4__tar.gz → 0.5.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. {xobjects-0.5.4/xobjects.egg-info → xobjects-0.5.6}/PKG-INFO +1 -1
  2. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_buffer.py +7 -5
  3. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_hybrid_class.py +75 -0
  4. xobjects-0.5.4/tests/test_to_json.py → xobjects-0.5.6/tests/test_to_dict.py +4 -4
  5. xobjects-0.5.6/xobjects/_version.py +1 -0
  6. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/array.py +5 -2
  7. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/context_cupy.py +5 -1
  8. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/hybrid_class.py +21 -5
  9. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/ref.py +5 -2
  10. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/struct.py +5 -5
  11. {xobjects-0.5.4 → xobjects-0.5.6/xobjects.egg-info}/PKG-INFO +1 -1
  12. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects.egg-info/SOURCES.txt +1 -1
  13. xobjects-0.5.4/xobjects/_version.py +0 -1
  14. {xobjects-0.5.4 → xobjects-0.5.6}/LICENSE +0 -0
  15. {xobjects-0.5.4 → xobjects-0.5.6}/pyproject.toml +0 -0
  16. {xobjects-0.5.4 → xobjects-0.5.6}/setup.cfg +0 -0
  17. {xobjects-0.5.4 → xobjects-0.5.6}/setup.py +0 -0
  18. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_align.py +0 -0
  19. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_array.py +0 -0
  20. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_capi.py +0 -0
  21. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_chunk.py +0 -0
  22. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_context_opencl.py +0 -0
  23. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_kernel.py +0 -0
  24. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_linked_array.py +0 -0
  25. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_nplike_arrays.py +0 -0
  26. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_ref.py +0 -0
  27. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_scalars.py +0 -0
  28. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_shared_memory.py +0 -0
  29. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_strides.py +0 -0
  30. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_string.py +0 -0
  31. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_struct.py +0 -0
  32. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_typeutils.py +0 -0
  33. {xobjects-0.5.4 → xobjects-0.5.6}/tests/test_unionref.py +0 -0
  34. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/__init__.py +0 -0
  35. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/_patch_pyopencl_array.py +0 -0
  36. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/capi.py +0 -0
  37. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/context.py +0 -0
  38. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/context_cpu.py +0 -0
  39. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/context_pyopencl.py +0 -0
  40. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/general.py +0 -0
  41. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/linkedarray.py +0 -0
  42. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/scalar.py +0 -0
  43. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/specialize_source.py +0 -0
  44. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/string.py +0 -0
  45. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/test_helpers.py +0 -0
  46. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/typeutils.py +0 -0
  47. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects/union.py +0 -0
  48. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects.egg-info/dependency_links.txt +0 -0
  49. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects.egg-info/requires.txt +0 -0
  50. {xobjects-0.5.4 → xobjects-0.5.6}/xobjects.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xobjects
3
- Version: 0.5.4
3
+ Version: 0.5.6
4
4
  Summary: In-memory serialization and code generator for CPU and GPU
5
5
  Home-page: https://xsuite.readthedocs.io/
6
6
  Download-URL: https://pypi.python.org/pypi/xobjects
@@ -95,10 +95,12 @@ def test_free_simple(test_context):
95
95
  ch.free_string(offset)
96
96
  ch.check()
97
97
 
98
+
98
99
  @for_all_test_contexts
99
100
  def test_free(test_context):
100
101
  class CheckFree(xo.Struct):
101
102
  a = xo.Float64
103
+
102
104
  ch = CheckFree(a=5, _context=test_context)
103
105
  assert ch._buffer.capacity == 8
104
106
  assert ch._buffer.chunks == []
@@ -107,17 +109,17 @@ def test_free(test_context):
107
109
  with pytest.raises(ValueError, match="Cannot free outside of buffer"):
108
110
  ch._buffer.free(0, 10)
109
111
  with pytest.raises(ValueError, match="Cannot free outside of buffer"):
110
- ch._buffer.free(7,2)
111
- ch._buffer.free(0,4)
112
+ ch._buffer.free(7, 2)
113
+ ch._buffer.free(0, 4)
112
114
  assert len(ch._buffer.chunks) == 1
113
115
  assert ch._buffer.chunks[0].start == 0
114
116
  assert ch._buffer.chunks[0].end == 4
115
- ch._buffer.free(0,4) # Does nothing
116
- ch._buffer.free(2,4) # Increases free chunk
117
+ ch._buffer.free(0, 4) # Does nothing
118
+ ch._buffer.free(2, 4) # Increases free chunk
117
119
  assert len(ch._buffer.chunks) == 1
118
120
  assert ch._buffer.chunks[0].start == 0
119
121
  assert ch._buffer.chunks[0].end == 6
120
- ch._buffer.free(7,1)
122
+ ch._buffer.free(7, 1)
121
123
  assert len(ch._buffer.chunks) == 2
122
124
  assert ch._buffer.chunks[0].start == 0
123
125
  assert ch._buffer.chunks[0].end == 6
@@ -330,3 +330,78 @@ def test_to_json_defaults():
330
330
  assert np.all(b_dict.pop("a") == [1, 2, 3])
331
331
  assert b_dict.pop("d") == 8
332
332
  assert b_dict == {}
333
+
334
+
335
+ def test_to_dict_python_vars():
336
+ class TD(xo.HybridClass):
337
+ _xofields = {
338
+ "_a": xo.Float64[3],
339
+ "_b": xo.Int64,
340
+ }
341
+ _skip_in_to_dict = ["_a", "_b"]
342
+ _store_in_to_dict = ["a", "b", "c"]
343
+
344
+ def __init__(self, **kwargs):
345
+ if "_xobject" in kwargs and kwargs["_xobject"] is not None:
346
+ self.xoinitialize(**kwargs)
347
+ return
348
+ kwargs["_a"] = kwargs.pop("a", [1.0, 2.0, 3.0])
349
+ kwargs["_b"] = kwargs.pop("b", 0)
350
+ self._initialize(**kwargs)
351
+
352
+ def _initialize(self, **kwargs):
353
+ # Need to handle non-xofields manually
354
+ c = kwargs.pop("c", -9)
355
+ super().__init__(**kwargs)
356
+ self._c = c
357
+
358
+ @property
359
+ def a(self):
360
+ return self._a
361
+
362
+ @property
363
+ def b(self):
364
+ return self._b
365
+
366
+ @property
367
+ def c(self):
368
+ return self._c
369
+
370
+ # Verify that to_dict has all fields, including python-only ones,
371
+ # for default initialisation
372
+ td1 = TD()
373
+ td1_dict = td1.to_dict()
374
+ assert td1_dict.pop("__class__") == "TD"
375
+ assert all(td1_dict.pop("a") == [1, 2, 3])
376
+ assert td1_dict.pop("b") == 0
377
+ assert td1_dict.pop("c") == -9
378
+ assert td1_dict == {}
379
+
380
+ # Verify that to_dict has all fields, including python-only ones,
381
+ # for custom initialisation
382
+ td2 = TD(a=[8, 9, 10], b=40, c=20)
383
+ td2_dict = td2.to_dict()
384
+ assert td2_dict.pop("__class__") == "TD"
385
+ assert all(td2_dict.pop("a") == [8, 9, 10])
386
+ assert td2_dict.pop("b") == 40
387
+ assert td2_dict.pop("c") == 20
388
+ assert td2_dict == {}
389
+
390
+ # Verify that from_dict works correctly
391
+ td2_dict = td2.to_dict()
392
+ td3 = TD.from_dict(td2_dict)
393
+ assert all(td3.a == td2.a)
394
+ assert td3.b == td2.b
395
+ assert td3.c == td2.c
396
+
397
+ # Verify that copy works correctly
398
+ td4 = td3.copy()
399
+ assert all(td4.a == td2.a)
400
+ assert td4.b == td2.b
401
+ assert td4.c == td2.c
402
+
403
+ # Verify that move works correctly
404
+ td3.move(_context=xo.ContextCpu(omp_num_threads="auto"))
405
+ assert all(td3.a == td2.a)
406
+ assert td3.b == td2.b
407
+ assert td3.c == td2.c
@@ -1,7 +1,7 @@
1
1
  import xobjects as xo
2
2
 
3
3
 
4
- def test_to_json():
4
+ def test_to_dict():
5
5
  class A(xo.Struct):
6
6
  a = xo.Float64[:]
7
7
  b = xo.Int64
@@ -11,14 +11,14 @@ def test_to_json():
11
11
 
12
12
  x = A(a=[2, 3], b=1)
13
13
  u = Uref(x)
14
- v = Uref(*u._to_json())
14
+ v = Uref(*u._to_dict())
15
15
 
16
16
  assert v.get().a[0] == 2
17
17
  assert v.get().a[1] == 3
18
18
  assert v.get().b == 1
19
19
 
20
20
 
21
- def test_to_json_array():
21
+ def test_to_dict_array():
22
22
  class A(xo.Struct):
23
23
  a = xo.Float64[:]
24
24
 
@@ -35,7 +35,7 @@ def test_to_json_array():
35
35
  a[1] = A(a=[3])
36
36
  a[5] = B(c=2, d=1)
37
37
 
38
- b = AUref(a._to_json())
38
+ b = AUref(a._to_dict())
39
39
 
40
40
  assert b[1].a[0] == 3
41
41
  assert b[5].d == 1
@@ -0,0 +1 @@
1
+ __version__ = "0.5.6"
@@ -690,10 +690,13 @@ class Array(metaclass=MetaArray):
690
690
  return [cls._itemtype]
691
691
 
692
692
  def _to_json(self):
693
+ raise NameError("`_to_json` has been removed. Use `_to_dict` instead.")
694
+
695
+ def _to_dict(self):
693
696
  out = []
694
697
  for v in self: # TODO does not support multidimensional arrays
695
- if hasattr(v, "_to_json"):
696
- vdata = v._to_json()
698
+ if hasattr(v, "_to_dict"):
699
+ vdata = v._to_dict()
697
700
  else:
698
701
  vdata = v
699
702
  if self._has_refs and v is not None:
@@ -457,7 +457,11 @@ class ContextCupy(XContext):
457
457
 
458
458
  extra_include_paths = self.get_installed_c_source_paths()
459
459
  include_flags = [f"-I{path}" for path in extra_include_paths]
460
- extra_compile_args = (*extra_compile_args, *include_flags, "-DXO_CONTEXT_CUDA")
460
+ extra_compile_args = (
461
+ *extra_compile_args,
462
+ *include_flags,
463
+ "-DXO_CONTEXT_CUDA",
464
+ )
461
465
 
462
466
  module = cupy.RawModule(
463
467
  code=specialized_source, options=extra_compile_args
@@ -40,7 +40,6 @@ class _FieldOfDressed:
40
40
  if self.isnplikearray:
41
41
  self.__get__(container=container)[:] = value
42
42
  elif hasattr(value, "_xobject"): # value is a dressed xobject
43
-
44
43
  # Copy xobject data from value inside self._xobject
45
44
  # (unless same memory area or Ref and same buffer,
46
45
  # in the latter case reference mechanism is used)
@@ -340,10 +339,18 @@ class HybridClass(metaclass=MetaHybridClass):
340
339
  if _context is None and _buffer is None:
341
340
  _context = self._xobject._buffer.context
342
341
  # This makes a copy of the xobject
343
- xobject = self._XoStruct(
342
+ new_xobject = self._XoStruct(
344
343
  self._xobject, _context=_context, _buffer=_buffer, _offset=_offset
345
344
  )
346
- return self.__class__(_xobject=xobject)
345
+ new = self.__class__.__new__(self.__class__)
346
+ new.__dict__.update(self.__dict__)
347
+ for kk, vv in new.__dict__.items():
348
+ if kk == "_xobject":
349
+ continue
350
+ if hasattr(vv, "copy"):
351
+ new.__dict__[kk] = vv.copy()
352
+ new._xobject = new_xobject
353
+ return new
347
354
 
348
355
  @property
349
356
  def _buffer(self):
@@ -382,14 +389,17 @@ class HybridClass(metaclass=MetaHybridClass):
382
389
  return self._xobject.compile_kernels(*args, **kwargs)
383
390
 
384
391
  def __repr__(self):
385
-
386
392
  if hasattr(self, "_repr_fields"):
387
393
  fnames = self._repr_fields
388
394
  else:
389
395
  fnames = []
390
396
  if hasattr(self, "_add_to_repr"):
391
397
  fnames += self._add_to_repr
392
- fnames += [fname for fname in self._fields]
398
+ fnames += [
399
+ fname
400
+ for fname in self._xofields.keys()
401
+ if not fname.startswith("_")
402
+ ]
393
403
  if hasattr(self, "_skip_in_repr"):
394
404
  fnames = [ff for ff in fnames if ff not in self._skip_in_repr]
395
405
 
@@ -398,6 +408,12 @@ class HybridClass(metaclass=MetaHybridClass):
398
408
  vv = getattr(self, fname)
399
409
  if isinstance(vv, float):
400
410
  vvrepr = f"{vv:.3g}"
411
+ elif isinstance(vv, np.ndarray):
412
+ vvrepr = repr(vv)
413
+ # kill "array("
414
+ vvrepr = vvrepr.split("(", 1)[1]
415
+ # kill trailing ")"
416
+ vvrepr = vvrepr.rsplit(")", 1)[0]
401
417
  else:
402
418
  vvrepr = repr(vv)
403
419
  args.append(f"{fname}={vvrepr}")
@@ -294,10 +294,13 @@ class UnionRef(metaclass=MetaUnionRef):
294
294
  return cls._reftypes
295
295
 
296
296
  def _to_json(self):
297
+ raise NameError("`_to_json` has been removed. Use `_to_dict` instead.")
298
+
299
+ def _to_dict(self):
297
300
  v = self.get()
298
301
  classname = v.__class__.__name__
299
- if hasattr(v, "_to_json"):
300
- v = v._to_json()
302
+ if hasattr(v, "_to_dict"):
303
+ v = v._to_dict()
301
304
  return (classname, v)
302
305
 
303
306
 
@@ -366,15 +366,15 @@ class Struct(metaclass=MetaStruct):
366
366
  foffset = offset + cls._fields[index].offset
367
367
  Int64._to_buffer(buffer, foffset, data_offset)
368
368
 
369
- def _to_dict(self):
370
- return {field.name: field.__get__(self) for field in self._fields}
371
-
372
369
  def _to_json(self):
370
+ raise NameError("`_to_json` has been removed. Use `_to_dict` instead.")
371
+
372
+ def _to_dict(self):
373
373
  out = {}
374
374
  for field in self._fields:
375
375
  v = field.__get__(self)
376
- if hasattr(v, "_to_json"):
377
- v = v._to_json()
376
+ if hasattr(v, "_to_dict"):
377
+ v = v._to_dict()
378
378
  out[field.name] = v
379
379
  return out
380
380
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xobjects
3
- Version: 0.5.4
3
+ Version: 0.5.6
4
4
  Summary: In-memory serialization and code generator for CPU and GPU
5
5
  Home-page: https://xsuite.readthedocs.io/
6
6
  Download-URL: https://pypi.python.org/pypi/xobjects
@@ -18,7 +18,7 @@ tests/test_shared_memory.py
18
18
  tests/test_strides.py
19
19
  tests/test_string.py
20
20
  tests/test_struct.py
21
- tests/test_to_json.py
21
+ tests/test_to_dict.py
22
22
  tests/test_typeutils.py
23
23
  tests/test_unionref.py
24
24
  xobjects/__init__.py
@@ -1 +0,0 @@
1
- __version__ = "0.5.4"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes