Added stretch_array_to_length_with_interpolation

This commit is contained in:
Grant Sanderson 2020-02-05 14:44:16 -08:00
parent 9da5ef4743
commit c34bb7912c

View file

@ -72,27 +72,36 @@ def batch_by_property(items, property_func):
return batch_prop_pairs return batch_prop_pairs
def tuplify(obj): def listify(obj):
if isinstance(obj, str): if isinstance(obj, str):
return (obj,) return [obj]
try: try:
return tuple(obj) return list(obj)
except TypeError: except TypeError:
return (obj,) return [obj]
def stretch_array_to_length(nparray, length): def stretch_array_to_length(nparray, length):
curr_len = len(nparray) curr_len = len(nparray)
if curr_len > length: if curr_len > length:
raise Warning( raise Warning("Trying to stretch array to a length shorter than its own")
"Trying to stretch array to a length shorter than its own") indices = np.arange(0, curr_len, curr_len / length).astype(int)
indices = np.arange(length) / float(length) return nparray[indices]
indices *= curr_len
return nparray[indices.astype('int')]
def stretch_array_to_length_with_interpolation(nparray, length):
curr_len = len(nparray)
cont_indices = np.linspace(0, curr_len - 1, length)
return np.array([
(1 - a) * nparray[lh] + a * nparray[rh]
for ci in cont_indices
for lh, rh, a in [(int(ci), int(np.ceil(ci)), ci % 1)]
])
def make_even(iterable_1, iterable_2): def make_even(iterable_1, iterable_2):
list_1, list_2 = list(iterable_1), list(iterable_2) list_1 = list(iterable_1)
list_2 = list(iterable_2)
length = max(len(list_1), len(list_2)) length = max(len(list_1), len(list_2))
return ( return (
[list_1[(n * len(list_1)) // length] for n in range(length)], [list_1[(n * len(list_1)) // length] for n in range(length)],