-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorph_array_to_size.py
34 lines (31 loc) · 1.64 KB
/
morph_array_to_size.py
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
import numpy
def morph_array_to_size(from_array, to_size):
from_size = len(from_array)
to_array = numpy.zeros((to_size), dtype=float)
if from_size >= to_size:
# for f in range(from_size):
# to_array[f] = from_array[f]
for f in range(from_size):
starting_to_element = int(to_size / from_size * f)
ending_to_element = int(to_size / from_size * (f + 1))
if starting_to_element == ending_to_element or \
ending_to_element - to_size / from_size * (f + 1) == 0:
to_array[starting_to_element] += from_array[f]
else:
amt1_pct = from_size / to_size * f - int(from_size / to_size * f)
amt2_pct = 1 - amt1_pct
to_array[starting_to_element] += from_array[f] * amt1_pct
to_array[ending_to_element] += from_array[f] * amt2_pct
else:
for t in range(to_size):
starting_from_element = int(from_size / to_size * t)
ending_from_element = int(from_size / to_size * (t + 1))
if starting_from_element == ending_from_element or \
ending_from_element - from_size / to_size * (t + 1) == 0:
to_array[t] += from_array[starting_from_element] * from_size / to_size
else:
amt1_pct = int(from_size / to_size * t) + 1 - from_size / to_size * t
amt2_pct = from_size / to_size * (t + 1) - int(from_size / to_size * (t + 1))
to_array[t] += from_array[starting_from_element] * amt1_pct
to_array[t] += from_array[ending_from_element] * amt2_pct
return to_array