Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

""" 

Define what a scene is 

""" 

import numpy as np 

 

 

# 

# Define constants 

# 

__spherical_indeces__ = {'elevation': 0, 

'azimuth': 1, 

'radius': 2} 

__cartesian_indeces__ = {'x': 0, 

'y': 1, 

'z': 2} 

__ibpc_indeces__ = {'elevation': 0, 

'azimuth': 1, 

'channel': 2, 

'component': 3} 

__obpc_indeces__ = {'ommatidia': 0, 

'channel': 1, 

'component': 2} 

__eye_indeces__ = {'elevation': 0, 

'azimuth': 1, 

'component': 2} 

__ommadia_indeces__ = {'ommatidia': 0, 

'component': 1} 

 

 

def spherical_indeces(): 

return {'elevation': 0, 

'azimuth': 1, 

'radius': 2} 

 

 

def cartesian_indeces(): 

return {'x': 0, 

'y': 1, 

'z': 2} 

 

 

def check_scene(scene): 

if is_ibpc(scene): 

# print("normal") 

if not is_numeric_array(scene): 

raise TypeError('scene is of non numeric type') 

if not ~np.any(np.isnan(scene)): 

raise ValueError('scene contains nans') 

if not len(scene.shape) == 4: 

raise Exception('scene has wrong shape, must have 4 dimensions') 

if not (scene.shape[1] > 0): 

raise Exception('scenes first dimension is empty') 

if not (scene.shape[0] > 0): 

raise Exception('scenes second dimension is empty') 

if not (scene.shape[2] == 4): 

raise Exception('3rd dimension of scene must be four') 

if not (scene.shape[3] == 1): 

raise Exception('4rd dimension of scene must be one') 

# assert ~(np.any(np.isNone(scene))) 

return True 

 

elif is_obpc(scene): 

if not is_numeric_array(scene): 

raise TypeError('scene is of non numeric type') 

if not ~np.any(np.isnan(scene)): 

raise ValueError('scene contains nans') 

if not len(scene.shape) == 3: 

raise Exception('scene has wrong shape, must have 4 dimensions') 

if not ~(scene.shape[1] <= 0): 

raise Exception('scenes first dimension is empty') 

if not ~(scene.shape[0] <= 0): 

raise Exception('scenes second dimension is empty') 

if not (scene.shape[2] == 4): 

raise Exception('3rd dimension of scene must be four') 

# assert ~(np.any(np.isNone(scene))) 

return True 

 

 

def check_viewing_direction(viewing_direction): 

if not is_numeric_array(viewing_direction): 

raise TypeError('viewing direction is of non numeric type') 

if not ~np.any(np.isnan(viewing_direction)): 

raise ValueError('viewing direction contains nans') 

if len(viewing_direction.shape) < 3: 

raise Exception('viewing direction must have at least 3 dimensions') 

if not (viewing_direction.shape[1] > 0): 

raise Exception('viewing direction has empty second dimension') 

if not (viewing_direction.shape[0] > 0): 

raise Exception('viewing direction has empty first dimension') 

if not (viewing_direction.shape[-1] == 2): 

raise Exception(' last dimension of viewing direction must equal 2') 

return True 

 

 

def is_numeric_array(array): 

"""Checks if the dtype of the array is numeric. 

 

Booleans, unsigned integer, signed integer, floats and complex are 

considered numeric. 

 

:param array : `numpy.ndarray`-like The array to check. 

:returns: True if it is a recognized numerical and False \ 

if object or string. 

:rtype:bool 

""" 

numerical_dtype_kinds = {'b', # boolean 

'u', # unsigned integer 

'i', # signed integer 

'f', # floats 

'c'} # complex 

try: 

return array.dtype.kind in numerical_dtype_kinds 

except AttributeError: 

# in case it's not a numpy array it will probably have no dtype. 

return np.asarray(array).dtype.kind in numerical_dtype_kinds 

 

 

def is_ibpc(place_code): 

"""Test if a place code is image based 

 

:param place_code: a place-code 

:returns: True if image based place-code 

:rtype: bool 

 

""" 

toreturn = isinstance(place_code, np.ndarray) 

toreturn = toreturn and (len(place_code.shape) == 

len(__ibpc_indeces__)) 

return toreturn 

 

 

def is_obpc(place_code): 

"""Test if a place code is ommatidia based 

 

:param place_code: a place-code 

:returns: True if ommatidia based place-code 

:rtype: bool 

 

""" 

toreturn = isinstance(place_code, np.ndarray) 

toreturn = toreturn and (len(place_code.shape) == 

len(__obpc_indeces__)) 

return toreturn 

 

 

def ibs_to_obs(scene, eye_map): 

"""Convert an image based scene to an ommatidium based scene. 

 

:param scene: The scene to be converted 

:param eye_map: The eye_map to use 

:returns: (obs_scene,ommatidia_map) 

:rtype: (np.ndarray,np.ndarray) 

""" 

assert is_ibpc(scene),\ 

'scene should be an ibs scene' 

assert isinstance(eye_map, np.ndarray), 'eye_map should be a numpy array' 

assert len(eye_map.shape) == len(__eye_indeces__),\ 

'eye_map should have {} dimensions to be an ibs scene'.format( 

len(__eye_indeces__)) 

for index_name in ['elevation', 'azimuth']: 

index = __ibpc_indeces__[index_name] 

assert eye_map.shape[index] == scene.shape[index],\ 

'eye_map and scene should have the same number of {}'.format( 

index_name) 

obs_size = (scene.shape[__ibpc_indeces__['elevation']] * 

scene.shape[__ibpc_indeces__['azimuth']], 

scene.shape[__ibpc_indeces__['channel']], 

scene.shape[__ibpc_indeces__['component']]) 

obs_scene = scene.reshape(obs_size) 

omm_size = (eye_map.shape[__ibpc_indeces__['elevation']] * 

eye_map.shape[__ibpc_indeces__['azimuth']], 

eye_map.shape[__ibpc_indeces__['component']]) 

ommatidia_map = eye_map.reshape(omm_size) 

return (obs_scene, ommatidia_map)