-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_operations.py
More file actions
143 lines (124 loc) · 4.16 KB
/
Copy pathmy_operations.py
File metadata and controls
143 lines (124 loc) · 4.16 KB
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
import sys
import numpy as NP
def reverse(inp, axis=0, ind_range=[-1,-1]):
"""
---------------------------------------------------------------
A generic function to reverse a specific axis or a subset of
indices of this specific axis of a multi-dimensional array. This
works on data up to 8 dimensions.
Input:
inp Multi-dimensional array (up to 8 dimensions)
Keyword Inputs:
axis [scalar, default = 0] The axis along which the array
is to be reversed while preserving the order of the other
axes. 0 <= axis <= 7
ind_range [2-element list] The lower and upper limits of indices
of the axis over which the data is to be reversed.
Output:
The array with its data reversed over a subset or the entirety
of the specified axis.
---------------------------------------------------------------
"""
inp = NP.asarray(inp)
try:
isinstance(inp, NP.ndarray)
# type(inp) is numpy.ndarray
except TypeError:
print 'Unable to convert to Numpy array data type'
sys.exit(1) # Abort execution
shp = NP.shape(inp)
ndim = len(shp)
if ndim > 8:
print "Input data with more than 8 dimensions not supported."
print "Aborted execution in my_operations.reverse()"
sys.exit(1)
if (axis < 0) or (axis >= ndim):
print "Input data does not contain the axis specified."
print "Aborted execution in my_operations.reverse()"
sys.exit(1)
if (ind_range[0] <= -1):
ind_range[0] = 0 # set default to starting index
if (ind_range[1] == -1) or (ind_range[1] >= shp[axis]):
ind_range[1] = shp[axis]-1 # set default to ending index
if shp[axis] == 1:
return inp
revinds = range(ind_range[1],ind_range[0]-1,-1)
if ndim == 1:
return inp[revinds]
elif ndim == 2:
if axis == 0:
return inp[revinds,:]
else:
return inp[:,revinds]
elif ndim == 3:
if axis == 0:
return inp[revinds,:,:]
elif axis == 1:
return inp[:,revinds,:]
else:
return inp[:,:,revinds]
elif ndim == 4:
if axis == 0:
return inp[revinds,:,:,:]
elif axis == 1:
return inp[:,revinds,:,:]
elif axis == 2:
return inp[:,:,revinds,:]
else:
return inp[:,:,:,revinds]
elif ndim == 5:
if axis == 0:
return inp[revinds,:,:,:,:]
elif axis == 1:
return inp[:,revinds,:,:,:]
elif axis == 2:
return inp[:,:,revinds,:,:]
elif axis == 3:
return inp[:,:,:,revinds,:]
else:
return inp[:,:,:,:,revinds]
elif ndim == 6:
if axis == 0:
return inp[revinds,:,:,:,:,:]
elif axis == 1:
return inp[:,revinds,:,:,:,:]
elif axis == 2:
return inp[:,:,revinds,:,:,:]
elif axis == 3:
return inp[:,:,:,revinds,:,:]
elif axis == 4:
return inp[:,:,:,:,revinds,:]
else:
return inp[:,:,:,:,:,revinds]
elif ndim == 7:
if axis == 0:
return inp[revinds,:,:,:,:,:,:]
elif axis == 1:
return inp[:,revinds,:,:,:,:,:]
elif axis == 2:
return inp[:,:,revinds,:,:,:,:]
elif axis == 3:
return inp[:,:,:,revinds,:,:,:]
elif axis == 4:
return inp[:,:,:,:,revinds,:,:]
elif axis == 5:
return inp[:,:,:,:,:,revinds,:]
else:
return inp[:,:,:,:,:,:,revinds]
elif ndim == 8:
if axis == 0:
return inp[revinds,:,:,:,:,:,:,:]
elif axis == 1:
return inp[:,revinds,:,:,:,:,:,:]
elif axis == 2:
return inp[:,:,revinds,:,:,:,:,:]
elif axis == 3:
return inp[:,:,:,revinds,:,:,:,:]
elif axis == 4:
return inp[:,:,:,:,revinds,:,:,:]
elif axis == 5:
return inp[:,:,:,:,:,revinds,:,:]
elif axis == 6:
return inp[:,:,:,:,:,:,revinds,:]
else:
return inp[:,:,:,:,:,:,:,revinds]