/
array.py
1192 lines (957 loc) · 36.7 KB
/
array.py
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
array
=====
Defines a shallow wrapper around numpy.ndarray for extra functionality like unit-tracking.
For most purposes, the differences between numpy.ndarray and
array.SimArray are not important. However, when units are specified
(by setting the ``units`` attribute), the behaviour is slightly
different. In particular,
* it becomes impossible to add or subtract arrays with incompatible dimensions
>>> SimArray([1,2], "Mpc") + SimArray([1,2], "Msol"))
ValueError
* addition or subtraction causes auto-unit conversion. For example
>>> SimArray([1,2], "Mpc") + SimArray([1,2], "kpc")
SimArray([1.001, 1.002], "Mpc")
* Note that in this context the left value takes precedence in
specifying the return units, so that reversing the order of the
operation here would return results in kpc.
* If only one of the arrays specifies a Unit, no checking occurs and
the unit of the returned array is assumed to be the same as the one
specified input unit.
* Powers to single integer or rational powers will maintain unit
tracking. Powers to float or other powers will not be able to do
so.
>>> SimArray([1,2],"Msol Mpc**-3")**2
SimArray([1, 4], 'Msol**2 Mpc**-6')
>>> SimArray([1,2],"Msol Mpc**-3")**(1,3)
SimArray([ 1.,1.26], 'Msol**1/3 Mpc**-1')
Syntax above mirrors syntax in units module, where a length-two tuple
can represent a rational number, in this case one third.
>>> SimArray([1.,2], "Msol Mpc**-3")**0.333
SimArray([ 1.,1.26]) # Lost track of units
*Getting the array in specified units*
--------------------------------------
Given an array, you can convert it in-place into units of your
own chosing:
>>> x = SimArray([1,2], "Msol")
>>> x.convert_units('kg')
>>> print x
SimArray([ 1.99e+30, 3.98e+30], 'kg')
Or you can leave the original array alone and get a *copy* in
different units, correctly converted:
>>> x = SimArray([1,2], "Msol")
>>> print x.in_units("kg")
SimArray([ 1.99e+30, 3.98e+30], 'kg')
>>> print x
SimArray([1,2], "Msol")
If the SimArray was created by a SimSnap (which is most likely), it
has a pointer into the SimSnap's properties so that the cosmological
context is automatically fetched. For example, comoving -> physical
conversions are correctly achieved:
>>> f = pynbody.load("fname")
>>> f['pos']
SimArray([[ 0.05419805, -0.0646539 , -0.15700017],
[ 0.05169899, -0.06193341, -0.14475258],
[ 0.05672406, -0.06384531, -0.15909944],
...,
[ 0.0723075 , -0.07650762, -0.07657281],
[ 0.07166634, -0.07453796, -0.08020873],
[ 0.07165282, -0.07468577, -0.08020587]], '2.86e+04 kpc a')
>>> f['pos'].convert_units('kpc')
>>> f['pos']
SimArray([[ 1548.51403101, -1847.2525312 , -4485.71463308],
[ 1477.1124212 , -1769.52421398, -4135.78377699],
[ 1620.68592366, -1824.15000686, -4545.69387564],
...,
[ 2065.9264273 , -2185.92982874, -2187.79225915],
[ 2047.60759667, -2129.6537339 , -2291.6758134 ],
[ 2047.2214441 , -2133.87693163, -2291.59406997]], 'kpc')
*Specifying rules for ufunc's*
------------------------------
In general, it's not possible to infer what the output units from a given
ufunc should be. While numpy built-in ufuncs should be handled OK, other
ufuncs will need their output units defined (otherwise a numpy.ndarray
will be returned instead of our custom type.)
To do this, decorate a function with SimArray.ufunc_rule(ufunc). The function
you define should take the same number of parameters as the ufunc. These will
be the input parameters of the ufunc. You should return the correct units for
the output, or raise units.UnitsException (in the latter case, the return
array will be made into a numpy.ndarray.)
For example, here is the code for the correct addition/subtraction
handler:
.. code-block:: python
@SimArray.ufunc_rule(np.add)
@SimArray.ufunc_rule(np.subtract)
def _consistent_units(a,b) :
# This will be called whenever the standard numpy ufuncs np.add
# or np.subtract are called with parameters a,b.
# You should always be ready for the inputs to have no units.
a_units = a.units if hasattr(a, 'units') else None
b_units = b.units if hasattr(b, 'units') else None
# Now do the logic. If we're adding incompatible units,
# we want just to get a plain numpy array out. If we only
# know the units of one of the arrays, we assume the output
# is in those units.
if a_units is not None and b_units is not None :
if a_units==b_units :
return a_units
else :
raise units.UnitsException("Incompatible units")
elif a_units is not None :
return a_units
else :
return b_units
"""
import atexit
import fractions
import functools
import os
import weakref
from functools import reduce
import numpy as np
from . import units as units
_units = units
class SimArray(np.ndarray):
"""
Defines a shallow wrapper around numpy.ndarray for extra
functionality like unit-tracking.
"""
_ufunc_registry = {}
@property
def ancestor(self):
"""Provides the basemost SimArray that an IndexedSimArray is based on."""
return self
@property
def derived(self):
if self.sim and self.name:
return self.sim.is_derived_array(self.name, getattr(self, 'family', None))
else:
return False
@derived.setter
def derived(self, value):
if value:
raise ValueError("Can only unlink an array. Delete an array to force a rederivation if this is the intended effect.")
if self.derived:
self.sim.unlink_array(self.name)
def __reduce__(self):
T = np.ndarray.__reduce__(self)
T = (
T[0], T[1], (self.units, T[2][0], T[2][1], T[2][2], T[2][3], T[2][4]))
return T
def __setstate__(self, args):
self._units = args[0]
self.sim = None
self._name = None
np.ndarray.__setstate__(self, args[1:])
def __new__(subtype, data, units=None, sim=None, **kwargs):
new = np.array(data, **kwargs).view(subtype)
if hasattr(data, 'units') and hasattr(data, 'sim') and units is None and sim is None:
units = data.units
sim = data.sim
if hasattr(data, 'family'):
new.family = data.family
if isinstance(units, str):
units = _units.Unit(units)
new._units = units
# Always associate a SimArray with the top-level snapshot.
# Otherwise we run into problems with how the reference should
# behave: we don't want to lose the link to the simulation by
# storing a weakref to a SubSnap that might be deconstructed,
# but we also wouldn't want to store a strong ref to a SubSnap
# since that would keep the entire simulation alive even if
# deleted.
#
# So, set the sim attribute to the top-level snapshot and use
# the normal weak-reference system.
if sim is not None:
new.sim = sim.ancestor
# will generate a weakref automatically
new._name = None
return new
def __array_finalize__(self, obj):
if obj is None:
return
elif obj is not self and hasattr(obj, 'units'):
self._units = obj.units
self._sim = obj._sim
self._name = obj._name
if hasattr(obj, 'family'):
self.family = obj.family
else:
self._units = None
self._sim = lambda: None
self._name = None
def __array_wrap__(self, array, context=None):
if context is None:
n_array = array.view(SimArray)
return n_array
try:
ufunc = context[0]
output_units = SimArray._ufunc_registry[ufunc](*context[1])
n_array = array.view(SimArray)
n_array.units = output_units
n_array.sim = self.sim
n_array._name = self._name
return n_array
except (KeyError, units.UnitsException):
return array
@staticmethod
def ufunc_rule(for_ufunc):
def x(fn):
SimArray._ufunc_registry[for_ufunc] = fn
return fn
return x
@property
def units(self):
if hasattr(self.base, 'units'):
return self.base.units
else:
if self._units is None:
return _units.no_unit
else:
return self._units
@units.setter
def units(self, u):
if not isinstance(u, units.UnitBase) and u is not None:
u = units.Unit(u)
if hasattr(self.base, 'units'):
self.base.units = u
else:
if hasattr(u, "_no_unit"):
self._units = None
else:
self._units = u
@property
def name(self):
if hasattr(self.base, 'name'):
return self.base.name
return self._name
@property
def sim(self):
if hasattr(self.base, 'sim'):
base_sim = self.base.sim
else:
base_sim = self._sim()
if self.family is not None and base_sim is not None:
return base_sim[self.family]
else:
return base_sim
@sim.setter
def sim(self, s):
if hasattr(self.base, 'sim'):
self.base.sim = s
else:
if s is not None:
self._sim = weakref.ref(s)
else:
self._sim = lambda: None
@property
def family(self):
try:
return self._family
except AttributeError:
return None
@family.setter
def family(self, fam):
self._family = fam
def __mul__(self, rhs):
if isinstance(rhs, _units.UnitBase):
x = self.copy()
x.units = x.units * rhs
return x
else:
return np.ndarray.__mul__(self, rhs)
def __div__(self, rhs):
if isinstance(rhs, _units.UnitBase):
x = self.copy()
x.units = x.units / rhs
return x
else:
return np.ndarray.__div__(self, rhs)
def __truediv__(self, rhs):
if isinstance(rhs, _units.UnitBase):
x = self.copy()
x.units = x.units / rhs
return x
else:
return np.ndarray.__truediv__(self, rhs)
def __imul__(self, rhs):
if isinstance(rhs, _units.UnitBase):
self.units *= rhs
else:
np.ndarray.__imul__(self, rhs)
try:
self.units *= rhs.units
except AttributeError:
pass
return self
def __idiv__(self, rhs):
if isinstance(rhs, _units.UnitBase):
self.units /= rhs
else:
np.ndarray.__idiv__(self, rhs)
try:
self.units /= rhs.units
except AttributeError:
pass
return self
def __itruediv__(self, rhs):
if isinstance(rhs, _units.UnitBase):
self.units /= rhs
else:
np.ndarray.__itruediv__(self, rhs)
try:
self.units /= rhs.units
except AttributeError:
pass
return self
def conversion_context(self):
if self.sim is not None:
return self.sim.conversion_context()
else:
return {}
def _generic_add(self, x, add_op=np.add):
if hasattr(x, 'units') and not hasattr(self.units, "_no_unit") and not hasattr(x.units, "_no_unit"):
# Check unit compatibility
try:
context = x.conversion_context()
except AttributeError:
context = {}
# Our own contextual information overrides x's
context.update(self.conversion_context())
try:
cr = x.units.ratio(self.units,
**context)
except units.UnitsException:
raise ValueError("Incompatible physical dimensions {!r} and {!r}, context {!r}".format(
str(self.units), str(x.units), str(self.conversion_context())))
if cr == 1.0:
r = add_op(self, x)
else:
b = np.multiply(x, cr)
if hasattr(b, 'units'):
b.units = None
if not np.can_cast(b.dtype,self.dtype):
b = np.asarray(b, dtype=x.dtype)
r = add_op(self, b)
return r
elif units.is_unit(x):
x = x.in_units(self.units, **self.conversion_context())
r = add_op(self, x)
return r
else:
r = add_op(self, x)
return r
def __add__(self, x):
if isinstance(x, _units.UnitBase):
return x + self
else:
return self._generic_add(x)
def __sub__(self, x):
if isinstance(x, _units.UnitBase):
return (-x + self).in_units(self.units)
else:
return self._generic_add(x, np.subtract)
def __iadd__(self, x):
self._generic_add(x, np.ndarray.__iadd__)
return self
def __isub__(self, x):
self._generic_add(x, np.ndarray.__isub__)
return self
def __pow__(self, x):
numerical_x = x
if isinstance(x, tuple):
x = fractions.Fraction(x[0], x[1])
numerical_x = float(x)
elif isinstance(x, fractions.Fraction):
numerical_x = float(x)
# The following magic circumvents our normal unit-assignment
# code which couldn't cope with the numerical version of x
# in the case of fractions. All this is necessary to make the
# magic tuple->fraction conversions work seamlessly.
r = np.asarray(np.power(self.view(np.ndarray), numerical_x)).view(SimArray)
# Recent numpy versions can take 1-element arrays and return
# scalars, in which case we now have a floating point number :(
if type(r) is not SimArray:
return r
if self.units is not None and (
isinstance(x, fractions.Fraction) or
isinstance(x, int)):
r.sim = self.sim
r.units = self.units ** x
else:
r.units = None
r.sim = self.sim
return r
def __repr__(self):
x = np.ndarray.__repr__(self)
if not hasattr(self.units, "_no_unit"):
return x[:-1] + ", '" + str(self.units) + "')"
else:
return x
def __setitem__(self, item, to):
if hasattr(to, "in_units") and not hasattr(self.units, "_no_unit") and not hasattr(to.units, "_no_unit"):
np.ndarray.__setitem__(self, item, to.in_units(self.units))
else:
np.ndarray.__setitem__(self, item, to)
def __setslice__(self, a, b, to):
self.__setitem__(slice(a, b), to)
def abs(self, *args, **kwargs):
x = np.abs(self, *args, **kwargs)
if hasattr(x, 'units') and self.units is not None:
x.units = self.units
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def cumsum(self, axis=None, dtype=None, out=None):
x = np.ndarray.cumsum(self, axis, dtype, out)
if hasattr(x, 'units') and self.units is not None:
x.units = self.units
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def prod(self, axis=None, dtype=None, out=None):
x = np.ndarray.prod(self, axis, dtype, out)
if hasattr(x, 'units') and axis is not None and self.units is not None:
x.units = self.units ** self.shape[axis]
if hasattr(x, 'units') and axis is None and self.units is not None:
x.units = self.units
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def sum(self, *args, **kwargs):
x = np.ndarray.sum(self, *args, **kwargs)
if hasattr(x, 'units') and self.units is not None:
x.units = self.units
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def mean(self, *args, **kwargs):
x = np.ndarray.mean(self, *args, **kwargs)
if hasattr(x, 'units') and self.units is not None:
x.units = self.units
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def mean_by_mass(self, *args, **kwargs):
return self.sim.mean_by_mass(self.name)
def max(self, *args, **kwargs):
x = np.ndarray.max(self, *args, **kwargs)
if hasattr(x, 'units') and self.units is not None:
x.units = self.units
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def min(self, *args, **kwargs):
x = np.ndarray.min(self, *args, **kwargs)
if hasattr(x, 'units') and self.units is not None:
x.units = self.units
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def ptp(self, *args, **kwargs):
x = np.ndarray.ptp(self, *args, **kwargs)
if hasattr(x, 'units') and self.units is not None:
x.units = self.units
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def std(self, *args, **kwargs):
x = np.ndarray.std(self, *args, **kwargs)
if hasattr(x, 'units') and self.units is not None:
x.units = self.units
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def var(self, *args, **kwargs):
x = np.ndarray.var(self, *args, **kwargs)
if hasattr(x, 'units') and self.units is not None:
x.units = self.units ** 2
if hasattr(x, 'sim') and self.sim is not None:
x.sim = self.sim
return x
def set_units_like(self, new_unit):
"""Set the units for this array by performing dimensional analysis
on the supplied unit and referring to the units of the original
file"""
if self.sim is not None:
self.units = self.sim.infer_original_units(new_unit)
else:
raise RuntimeError("No link to SimSnap")
def set_default_units(self, quiet=False):
"""Set the units for this array by performing dimensional analysis
on the default dimensions for the array."""
if self.sim is not None:
try:
self.units = self.sim._default_units_for(self.name)
except (KeyError, units.UnitsException):
if not quiet:
raise
else:
raise RuntimeError("No link to SimSnap")
def in_original_units(self):
"""Retun a copy of this array expressed in the units
specified in the parameter file."""
return self.in_units(self.sim.infer_original_units(self.units))
def in_units(self, new_unit, **context_overrides):
"""Return a copy of this array expressed relative to an alternative
unit."""
context = self.conversion_context()
context.update(context_overrides)
if self.units is not None:
r = self * self.units.ratio(new_unit,
**context)
r.units = new_unit
return r
else:
raise ValueError("Units of array unknown")
def convert_units(self, new_unit):
"""Convert units of this array in-place. Note that if
this is a sub-view, the entire base array will be converted."""
if self.base is not None and hasattr(self.base, 'units'):
self.base.convert_units(new_unit)
else:
self *= self.units.ratio(new_unit,
**(self.conversion_context()))
self.units = new_unit
def write(self, **kwargs):
"""
Write this array to disk according to the standard method
associated with its base file. This is equivalent to calling
>>> sim.gas.write_array('array')
in the case of writing out the array 'array' for the gas
particle family. See the description of
:func:`pynbody.snapshot.SimSnap.write_array` for options.
"""
if self.sim and self.name:
self.sim.write_array(self.name, fam=self.family, **kwargs)
else:
raise RuntimeError("No link to SimSnap")
def __del__(self):
"""Clean up disk if this was made from a named
shared array"""
if getattr(self, '_shared_del', False):
_shared_array_unlink(self)
# Set up the correct comparison functions
def _unit_aware_comparison(ar, other, comparison_op=None):
# guaranteed to be called with ar a SimArray instance
if units.is_unit_like(other):
if units.has_units(ar):
# either other is a unit, or an array with a unit If
# it's an array with a unit that matches our own, we
# want to fall straight through to the comparison
# operation. If it's an array with a unit that doesn't
# match ours, OR it's a plain unit, we want to
# convert first.
if units.is_unit(other) or other.units != ar.units:
other = other.in_units(ar.units)
else:
raise units.UnitsException("One side of a comparison has units and the other side does not")
return comparison_op(ar, other)
for f in np.ndarray.__lt__, np.ndarray.__le__, np.ndarray.__eq__, \
np.ndarray.__ne__, np.ndarray.__gt__, np.ndarray.__ge__:
# N.B. cannot use functools.partial because it doesn't implement the descriptor
# protocol
@functools.wraps(f, assigned=("__name__", "__doc__"))
def wrapper_function(self, other, comparison_op=f):
return _unit_aware_comparison(self, other, comparison_op=comparison_op)
setattr(SimArray, f.__name__, wrapper_function)
# Now add dirty bit setters to all the operations which are known
# to modify the numpy array
def _dirty_fn(w):
def q(a, *y, **kw):
if a.sim is not None and a.name is not None:
a.sim._dirty(a.name)
if kw != {}:
return w(a, *y, **kw)
else:
return w(a, *y)
q.__name__ = w.__name__
return q
_dirty_fns = ['__setitem__', '__setslice__',
'__irshift__',
'__imod__',
'__iand__',
'__ifloordiv__',
'__ilshift__',
'__imul__',
'__ior__',
'__ixor__',
'__isub__',
'__invert__',
'__iadd__',
'__itruediv__',
'__idiv__',
'__ipow__']
for x in _dirty_fns:
setattr(SimArray, x, _dirty_fn(getattr(SimArray, x)))
_u = SimArray.ufunc_rule
def _get_units_or_none(*a):
if len(a) == 1:
if hasattr(a[0], "units"):
return a[0].units
else:
return None
else:
r = []
for x in a:
if hasattr(x, "units"):
r.append(x.units)
else:
r.append(None)
return r
#
# Now we have the rules for unit outputs after numpy built-in ufuncs
#
# Note if these raise UnitsException, a standard numpy array is returned
# from the ufunc to indicate the units can't be calculated. That means
# ufuncs can do 'non-physical' things, but then return ndarrays instead
# of SimArrays.
@_u(np.sqrt)
def _sqrt_units(a):
if a.units is not None:
return a.units ** (1, 2)
else:
return None
@_u(np.multiply)
def _mul_units(a, b):
a_units, b_units = _get_units_or_none(a, b)
if a_units is not None and b_units is not None:
return a_units * b_units
elif a_units is not None:
return a_units
else:
return b_units
@_u(np.divide)
@_u(np.true_divide)
def _div_units(a, b):
a_units, b_units = _get_units_or_none(a, b)
if a_units is not None and b_units is not None:
return a_units / b_units
elif a_units is not None:
return a_units
else:
return 1 / b_units
@_u(np.add)
@_u(np.subtract)
def _consistent_units(a, b):
a_units, b_units = _get_units_or_none(a, b)
if a_units is not None and b_units is not None:
if a_units == b_units:
return a_units
else:
raise units.UnitsException("Incompatible units")
elif a_units is not None:
return a_units
else:
return b_units
@_u(np.power)
def _pow_units(a, b):
a_units = _get_units_or_none(a)
if a_units is not None:
if not isinstance(b, int) and not isinstance(b, units.Fraction):
raise units.UnitsException("Can't track units")
return a_units ** b
else:
return None
@_u(np.arctan)
@_u(np.arctan2)
@_u(np.arcsin)
@_u(np.arccos)
@_u(np.arcsinh)
@_u(np.arccosh)
@_u(np.arctanh)
@_u(np.sin)
@_u(np.tan)
@_u(np.cos)
@_u(np.sinh)
@_u(np.tanh)
@_u(np.cosh)
def _trig_units(*a):
return 1
@_u(np.greater)
@_u(np.greater_equal)
@_u(np.less)
@_u(np.less_equal)
@_u(np.equal)
@_u(np.not_equal)
def _comparison_units(*a):
return None
class IndexedSimArray:
@property
def derived(self):
return self.base.derived
@property
def ancestor(self):
return self.base.ancestor
def __init__(self, array, ptr):
self.base = array
self._ptr = ptr
def __array__(self, dtype=None):
return np.asanyarray(self.base[self._ptr], dtype=dtype)
def _reexpress_index(self, index):
if isinstance(index, tuple) or (isinstance(index, list) and len(index) > 0 and hasattr(index[0], '__len__')):
return (self._ptr[index[0]],) + tuple(index[1:])
else:
return self._ptr[index]
def __getitem__(self, item):
return self.base[self._reexpress_index(item)]
def __setitem__(self, item, to):
self.base[self._reexpress_index(item)] = to
def __getslice__(self, a, b):
return self.__getitem__(slice(a, b))
def __setslice__(self, a, b, to):
self.__setitem__(slice(a, b), to)
def __repr__(self):
return self.__array__().__repr__() # Could be optimized
def __str__(self):
return self.__array__().__str__() # Could be optimized
def __len__(self):
return len(self._ptr)
def __reduce__(self):
return SimArray(self).__reduce__()
@property
def shape(self):
x = [len(self._ptr)]
x += self.base.shape[1:]
return tuple(x)
@property
def ndim(self):
return self.base.ndim
@property
def units(self):
return self.base.units
@units.setter
def units(self, u):
self.base.units = u
@property
def sim(self):
if self.base.sim is not None:
return self.base.sim[self._ptr]
else:
return None
@sim.setter
def sim(self, s):
self.base.sim = s
@property
def dtype(self):
return self.base.dtype
def conversion_context(self):
return self.base.conversion_context()
def set_units_like(self, new_unit):
self.base.set_units_like(new_unit)
def in_units(self, new_unit, **context_overrides):
return IndexedSimArray(self.base.in_units(new_unit, **context_overrides), self._ptr)
def convert_units(self, new_unit):
self.base.convert_units(new_unit)
def write(self, **kwargs):
self.base.write(**kwargs)
def prod(self):
return np.array(self).prod()
# The IndexedSimArray class is now supplemented by wrapping all the
# standard numpy methods with a generated function which extracts an
# array realization of the subview before calling the underlying
# method.
def _wrap_fn(w):
def q(s, *y, **kw):
# AP: I Don't understand why the following condition should be necessary,
# but it seems required on McMaster setup (Py 2.6.5, NP 1.4.1)
if kw != {}:
return w(SimArray(s), *y, **kw)
else:
return w(SimArray(s), *y)
q.__name__ = w.__name__
return q
# functions we definitely want to wrap, even though there's an existing
# implementation
_override = "__eq__", "__ne__", "__gt__", "__ge__", "__lt__", "__le__"
for x in set(np.ndarray.__dict__).union(SimArray.__dict__):
w = getattr(SimArray, x)
if 'array' not in x and ((not hasattr(IndexedSimArray, x)) or x in _override) and hasattr(w, '__call__'):
setattr(IndexedSimArray, x, _wrap_fn(w))
############################################################
# SUPPORT FOR SHARING ARRAYS BETWEEN PROCESSES
############################################################
try:
import ctypes
import functools
import mmap
import multiprocessing
import multiprocessing.sharedctypes
import os
import random
import tempfile
import time
import posix_ipc
_all_shared_arrays = []
except ImportError:
posix_ipc = None
def _array_factory(dims, dtype, zeros, shared):
"""Create an array of dimensions *dims* with the given numpy *dtype*.
If *zeros* is True, the returned array is guaranteed zeroed. If *shared*
is True, the returned array uses shared memory so can be efficiently
shared across processes."""
global _all_shared_arrays