-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.py
More file actions
114 lines (93 loc) · 2.7 KB
/
tester.py
File metadata and controls
114 lines (93 loc) · 2.7 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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
from copy import deepcopy
def arrayStrides(array):
return np.array(array.strides)/array.dtype.itemsize
def elementOffset(array):
shape = np.array(array.shape)
strides = arrayStrides(array)
# print "shape: ", shape
# print "strides: ", strides
out = deepcopy(strides)
for i in xrange(len(shape)-1,0,-1):
out[i-1] = strides[i-1] - np.sum(strides[i:]*(shape[i:]-1))
return out
def elementOffsetImplement(array):
shape = np.array(array.shape)
strides = arrayStrides(array)
ndim = len(shape)
tmp = [0] * ndim
for i in xrange(ndim-1, -1, -1):
tmp[i] = strides[i] * (shape[i]-1)
if i < ndim-1:
tmp[i] += tmp[i+1]
out = deepcopy(strides)
for i in xrange(ndim-1, 0, -1):
out[i-1] = strides[i-1] - tmp[i]
return out
# def nextIndex(array, idx):
# idx +=1
# shape = array.shape
# i = array.ndim - 1
# while (i>0) and (idx%shape[i] == 0):
# idx /= shape[i]
# i -= 1
# def indexDimension(array, idx):
# offsets = elementOffset(array)
# strides = arrayStrides(array)
# shape = np.array(array.shape)
# tmp = idx+1
# i = len(shape)-1
# acc = 0
# while (i>0) and (tmp%shape[i] == 0):
# tmp = tmp//shape[i]
# acc += ((shape[i]-1) * strides[i])
# i -= 1
# return strides[i] - acc
def indexDimension(array, idx):
strides = arrayStrides(array)
shape = np.array(array.shape)
idx = idx+1
tmp = 0
for i in xrange(array.ndim-1,-1,-1):
if idx%shape[i] != 0:
break
tmp += ((shape[i]-1) * strides[i])
idx /= shape[i]
return strides[i] - tmp
# tmp = idx+1
# i = len(shape)-1
# acc = 0
# while (i>0) and (tmp%shape[i] == 0):
# tmp = tmp//shape[i]
# acc += ((shape[i]-1) * strides[i])
# i -= 1
# return strides[i] - acc
def unravel(array, idx):
tmp = idx
out = []
for d in array.shape[:-1]:
out.append(tmp//d)
tmp = tmp%d
out.append(tmp)
return tuple(out)
if __name__== "__main__":
shape = (16, 16)
x = np.arange(np.prod(shape)).reshape(shape)
y = x[slice(0,-1,2), slice(0,-1,2)]
print unravel(x[0], 16)
# print y
# print arrayStrides(y)
# print x[slice(0,4,2)]
# print x
# print nextIndex(x[::2,::2],1)
# print elementOffset(x[::2,::2])
# print elementOffsetImplement(x[::2, ::2,::2])
# for i in xrange(10):
# y = indexDimension(x,i+1)
# print "results:", i+1, y
# idx = 15
# print x[::2]
# test = indexDimension(x[::2], idx)
# print x.flatten()[test+idx]