Coverage for kwave/data.py: 71%

1 from dataclasses import dataclass

2

3 import numpy as np

4

5

6@dataclass

7 class Array(object):

8 """

9 This class is very similar to Numpy.ndarray but there are differences:

10 - It can have 3 elements at max

11 - Its elements can be anything

12 - The elements do not have to be same type,

13 e.g. this is valid: Array([<scalar>, <List>, <Tuple of Tuples>])

14 """

15 data: list

16

17 def __post_init__(self):

18 assert 1 <= len(self) <= 3

19

20 def __len__(self):

21 return len(self.data)

22

23 def __getitem__(self, item):

24 return self.data[item]

25

26 def __setitem__(self, key, value):

27 self.data[key] = value

28

29 @property

30 def x(self):

31 return self[0]

32

33 @x.setter

34 def x(self, val):

35 self[0] = val

36

37 @property

38 def y(self):

39 return self[1] if len(self) >= 2 else np.nan

40

41 @y.setter

42 def y(self, val):

43 assert len(self) >= 2

44 self[1] = val

45

46 @property

47 def z(self):

48 """

49 :return: 3rd dimension element. 0 if not defined

50 """

51 return self[2] if len(self) == 3 else np.nan

52

53 @z.setter

54 def z(self, val):

55 assert len(self) == 3

56 self[2] = val

57

58 def numpy(self):

59 return np.asarray(self.data)

60

61 def assign_dim(self, dim, val):

62 if dim == 1:

63 self.x = val

64 if dim == 2:

65 self.y = val

66 if dim == 3:

67 self.z = val

68

69 def append(self, val):

70 assert len(self.data) <= 2

71 self.data.append(val)