| 29 | |
| 30 | |
| 31 | class MPBArray(np.ndarray): |
| 32 | def __new__(cls, input_array, lattice, kpoint=None, bloch_phase=False): |
| 33 | # Input array is an already formed ndarray instance |
| 34 | # We first cast to be our class type |
| 35 | obj = np.asarray(input_array).view(cls) |
| 36 | # add the new properties to the created instance |
| 37 | obj.lattice = lattice |
| 38 | obj.kpoint = kpoint |
| 39 | obj.bloch_phase = bloch_phase |
| 40 | # Finally, we must return the newly created object: |
| 41 | return obj |
| 42 | |
| 43 | def __array_finalize__(self, obj): |
| 44 | # ``self`` is a new object resulting from |
| 45 | # ndarray.__new__(MPBArray, ...), therefore it only has |
| 46 | # attributes that the ndarray.__new__ constructor gave it - |
| 47 | # i.e. those of a standard ndarray. |
| 48 | |
| 49 | # We could have got to the ndarray.__new__ call in 3 ways: |
| 50 | # From an explicit constructor - e.g. MPBArray(lattice): |
| 51 | # obj is None |
| 52 | # (we're in the middle of the MPBArray.__new__ |
| 53 | # constructor, and self.lattice will be set when we return to |
| 54 | # MPBArray.__new__) |
| 55 | if obj is None: |
| 56 | return |
| 57 | |
| 58 | # From view casting - e.g arr.view(MPBArray): |
| 59 | # obj is arr |
| 60 | # (type(obj) can be MPBArray) |
| 61 | # From new-from-template - e.g mpbarr[:3] |
| 62 | # type(obj) is MPBArray |
| 63 | # |
| 64 | # Note that it is here, rather than in the __new__ method, |
| 65 | # that we set the default value for 'lattice', because this |
| 66 | # method sees all creation of default objects - with the |
| 67 | # MPBArray.__new__ constructor, but also with |
| 68 | # arr.view(MPBArray). |
| 69 | self.lattice = getattr(obj, "lattice", None) |
| 70 | self.kpoint = getattr(obj, "kpoint", None) |
| 71 | self.bloch_phase = getattr(obj, "bloch_phase", False) |
| 72 | |
| 73 | |
| 74 | class ModeSolver: |
no outgoing calls
no test coverage detected