File size: 4,303 Bytes
a1fc554 | 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 | import sys
import os
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.append(ROOT_DIR)
os.chdir(ROOT_DIR)
import numpy as np
import time
from diffusion_policy.common.timestamp_accumulator import (
get_accumulate_timestamp_idxs,
TimestampObsAccumulator,
TimestampActionAccumulator
)
def test_index():
buffer = np.zeros(16)
start_time = 0.0
dt = 1/10
timestamps = np.linspace(0,1,100)
gi = list()
next_global_idx = 0
local_idxs, global_idxs, next_global_idx = get_accumulate_timestamp_idxs(timestamps,
start_time=start_time, dt=dt, next_global_idx=next_global_idx)
assert local_idxs[0] == 0
assert global_idxs[0] == 0
# print(local_idxs)
# print(global_idxs)
# print(timestamps[local_idxs])
buffer[global_idxs] = timestamps[local_idxs]
gi.extend(global_idxs)
timestamps = np.linspace(0.5,1.5,100)
local_idxs, global_idxs, next_global_idx = get_accumulate_timestamp_idxs(timestamps,
start_time=start_time, dt=dt, next_global_idx = next_global_idx)
# print(local_idxs)
# print(global_idxs)
# print(timestamps[local_idxs])
# import pdb; pdb.set_trace()
buffer[global_idxs] = timestamps[local_idxs]
gi.extend(global_idxs)
assert np.all(buffer[1:] > buffer[:-1])
assert np.all(np.array(gi) == np.array(list(range(len(gi)))))
# print(buffer)
# start over
next_global_idx = 0
timestamps = np.linspace(0,1,3)
local_idxs, global_idxs, next_global_idx = get_accumulate_timestamp_idxs(timestamps,
start_time=start_time, dt=dt, next_global_idx = next_global_idx)
assert local_idxs[0] == 0
assert local_idxs[-1] == 2
# print(local_idxs)
# print(global_idxs)
# print(timestamps[local_idxs])
# test numerical error issue
# this becomes a problem when eps <= 1e-7
start_time = time.time()
next_global_idx = 0
timestamps = np.arange(100000) * dt + start_time
local_idxs, global_idxs, next_global_idx = get_accumulate_timestamp_idxs(timestamps,
start_time=start_time, dt=dt, next_global_idx = next_global_idx)
assert local_idxs == global_idxs
# print(local_idxs)
# print(global_idxs)
# print(timestamps[local_idxs])
def test_obs_accumulator():
dt = 1/10
ddt = 1/100
n = 100
d = 6
start_time = time.time()
toa = TimestampObsAccumulator(start_time, dt)
poses = np.arange(n).reshape((n,1))
poses = np.repeat(poses, d, axis=1)
timestamps = np.arange(n) * ddt + start_time
toa.put({
'pose': poses,
'timestamp': timestamps
}, timestamps)
assert np.all(toa.data['pose'][:,0] == np.arange(10)*10)
assert len(toa) == 10
# add the same thing, result shouldn't change
toa.put({
'pose': poses,
'timestamp': timestamps
}, timestamps)
assert np.all(toa.data['pose'][:,0] == np.arange(10)*10)
assert len(toa) == 10
# add lower than desired freuquency to test fill_in
dt = 1/10
ddt = 1/5
n = 10
d = 6
start_time = time.time()
toa = TimestampObsAccumulator(start_time, dt)
poses = np.arange(n).reshape((n,1))
poses = np.repeat(poses, d, axis=1)
timestamps = np.arange(n) * ddt + start_time
toa.put({
'pose': poses,
'timestamp': timestamps
}, timestamps)
assert len(toa) == 1 + (n-1) * 2
timestamps = (np.arange(n) + 2) * ddt + start_time
toa.put({
'pose': poses,
'timestamp': timestamps
}, timestamps)
assert len(toa) == 1 + (n-1) * 2 + 4
def test_action_accumulator():
dt = 1/10
n = 10
d = 6
start_time = time.time()
taa = TimestampActionAccumulator(start_time, dt)
actions = np.arange(n).reshape((n,1))
actions = np.repeat(actions, d, axis=1)
timestamps = np.arange(n) * dt + start_time
taa.put(actions, timestamps)
assert np.all(taa.actions == actions)
assert np.all(taa.timestamps == timestamps)
# add another round
taa.put(actions-5, timestamps-0.5)
assert np.allclose(taa.timestamps, timestamps)
# add another round
taa.put(actions+5, timestamps+0.5)
assert len(taa) == 15
assert np.all(taa.actions[:,0] == np.arange(15))
if __name__ == '__main__':
test_action_accumulator()
|