Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample reorg #2

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name = "pypi"
[packages]

[dev-packages]
ipykernel = "*"

[requires]
python_version = "3.11"
Expand Down
367 changes: 365 additions & 2 deletions Pipfile.lock

Large diffs are not rendered by default.

217 changes: 217 additions & 0 deletions dev/dev.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12590,6 +12590,26 @@
"a.x"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7600"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"38*200"
]
},
{
"cell_type": "code",
"execution_count": 37,
Expand Down Expand Up @@ -12672,6 +12692,26 @@
"np.all(d2.x == d3.x), np.all(d2.y == d3.y)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.5"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0.15/0.06"
]
},
{
"cell_type": "code",
"execution_count": 7,
Expand Down Expand Up @@ -13058,6 +13098,183 @@
"ts_1000 = np.linspace(0, 1, 1000)\n",
"data_1000 = np.sin(2 * np.pi * ts_1000)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1.70621755e+09, 1.70621755e+09, 1.70621755e+09, 1.70621757e+09,\n",
" 1.70621759e+09, 1.70621759e+09, 1.70621759e+09])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_tstamps"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.2143950399286514"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.random.uniform(0, 0.5)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[2m2024-03-12T15:28:48.767775Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Loading recording from: ../tests/test_data/2024-01-25_22-19-10_test-f96b6e36/\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.769442Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Loading recording info\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.771087Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Loading calibration data\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.773302Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Loading raw time (ns) files\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.775611Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Loading gaze data\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.777397Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Loading IMU data\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.875818Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Loading scene camera video\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.906987Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Loading eye camera video\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.913535Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Loading events \u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.914691Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Parsing unique events\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"\u001b[2m2024-03-12T15:28:48.915374Z\u001b[0m [\u001b[32m\u001b[1minfo \u001b[0m] \u001b[1mNeonRecording: Finished loading recording.\u001b[0m \u001b[36mfunc_name\u001b[0m=\u001b[35mload\u001b[0m\n",
"{'gaze': array([[ True, True, True, True],\n",
" [ True, True, True, True],\n",
" [ True, True, True, True],\n",
" [ True, True, True, True]])}\n",
"all equal gaze shifted (linear interp): True\n"
]
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"import random\n",
"\n",
"import pupil_labs.neon_recording as nr\n",
"\n",
"rec = nr.load('../tests/test_data/2024-01-25_22-19-10_test-f96b6e36/')\n",
"\n",
"gaze = rec.gaze\n",
"imus = rec.imu\n",
"eye = rec.eye\n",
"scene = rec.scene\n",
"\n",
"tstamps = gaze.ts\n",
"test_tstamps = [tstamps[100], tstamps[150], tstamps[200], tstamps[len(tstamps)//2], tstamps[-400], tstamps[-203], tstamps[-200]]\n",
"for ic in range(len(test_tstamps)):\n",
"\ttest_tstamps[ic] = test_tstamps[ic] + np.random.uniform(0, 0.5)\n",
"\n",
"shuffled_test_tstamps = random.sample(test_tstamps, len(test_tstamps))\n",
"\n",
"gaze_samps_shifted = {\n",
" 'np.interp': [],\n",
" 'np.interp_shuffled': [],\n",
" 'rob.interp': [],\n",
" 'rob.interp_shuffled': [],\n",
"}\n",
"\n",
"# np.interp approach\n",
"gaze_samps_shifted['np.interp'] = gaze.sample(test_tstamps, method=\"linear\")\n",
"gaze_samps_shifted['np.interp_shuffled'] = gaze.sample(shuffled_test_tstamps, method=\"linear\")\n",
"\n",
"# rob approach\n",
"gaze_samps_shifted['rob.interp'] = gaze.sample_rob_interp(test_tstamps)\n",
"gaze_samps_shifted['rob.interp_shuffled'] = gaze.sample_rob_interp(shuffled_test_tstamps)\n",
"\n",
"all_equal_interp_shifted = {\n",
" 'gaze': np.zeros((4, 4), dtype=bool),\n",
"}\n",
"kc = 0\n",
"for k in gaze_samps_shifted:\n",
" jc = 0\n",
" for j in gaze_samps_shifted:\n",
" all_equal_interp_shifted['gaze'][kc, jc] = np.all(gaze_samps_shifted[k] == gaze_samps_shifted[j])\n",
"\n",
" jc += 1\n",
"\n",
" kc += 1\n",
"\n",
"print(all_equal_interp_shifted)\n",
"print('all equal gaze shifted (linear interp):', np.all(all_equal_interp_shifted['gaze']))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"rec.array([(805.25998137, 756.99365861, 1.70621755e+09, 3.55813646),\n",
" (794.2766782 , 765.21001451, 1.70621755e+09, 3.73488307),\n",
" (801.59493555, 710.08808104, 1.70621755e+09, 3.91472697),\n",
" (930.27649807, 823.59277847, 1.70621757e+09, 21.6195159 ),\n",
" (755.91415717, 858.2909549 , 1.70621759e+09, 38.59534764),\n",
" (894.48095481, 1122.28195571, 1.70621759e+09, 39.44808292),\n",
" (884.4642695 , 1098.74650958, 1.70621759e+09, 39.58002234)],\n",
" dtype=[('x', '<f8'), ('y', '<f8'), ('ts', '<f8'), ('ts_rel', '<f8')])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gaze_samps_shifted['np.interp']"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"rec.array([(805.25998137, 756.99365861, 1.70621755e+09, 3.55813646),\n",
" (794.2766782 , 765.21001451, 1.70621755e+09, 3.73488307),\n",
" (801.59493555, 710.08808104, 1.70621755e+09, 3.91472697),\n",
" (930.27649807, 823.59277847, 1.70621757e+09, 21.6195159 ),\n",
" (755.91415717, 858.2909549 , 1.70621759e+09, 38.59534764),\n",
" (894.48095481, 1122.28195571, 1.70621759e+09, 39.44808292),\n",
" (884.4642695 , 1098.74650958, 1.70621759e+09, 39.58002234)],\n",
" dtype=[('x', '<f8'), ('y', '<f8'), ('ts', '<f8'), ('ts_rel', '<f8')])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gaze_samps_shifted['rob.interp']"
]
}
],
"metadata": {
Expand Down
151 changes: 151 additions & 0 deletions examples/compare_test_sample_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import random

import cv2
import numpy as np

import pupil_labs.neon_recording as nr

rec = nr.load('./tests/test_data/2024-01-25_22-19-10_test-f96b6e36/')

gaze = rec.gaze
imus = rec.imu
eye = rec.eye
scene = rec.scene

tstamps = gaze.ts
test_tstamps = [tstamps[0], tstamps[1], tstamps[2], tstamps[len(tstamps)//2], tstamps[-3], tstamps[-2], tstamps[-1]]
shuffled_test_tstamps = random.sample(test_tstamps, len(test_tstamps))

gaze_samps = {
'searchsorted': [],
'searchsorted_shuffled': [],
'broadcast': [],
'broadcast_shuffled': [],
'rob_min': [],
'rob_min_shuffled': []
}
imu_samps = gaze_samps.copy()

# nearest neighbor sampling
# np.searchsorted approach
gaze_samps['searchsorted'] = gaze.sample(test_tstamps)
gaze_samps['searchsorted_shuffled'] = gaze.sample(shuffled_test_tstamps)
imu_samps['searchsorted'] = imus.sample(test_tstamps)
imu_samps['searchsorted_shuffled'] = imus.sample(shuffled_test_tstamps)

# np broadcasting approach
gaze_samps['broadcast'] = gaze.sample_rob_broadcast(test_tstamps)
gaze_samps['broadcast_shuffled'] = gaze.sample_rob_broadcast(shuffled_test_tstamps)
imu_samps['broadcast'] = imus.sample_rob_broadcast(test_tstamps)
imu_samps['broadcast_shuffled'] = imus.sample_rob_broadcast(shuffled_test_tstamps)

# rob min diff approach
gaze_samps['rob_min'] = gaze.sample_rob(test_tstamps)
gaze_samps['rob_min_shuffled'] = gaze.sample_rob(shuffled_test_tstamps)
imu_samps['rob_min'] = imus.sample_rob(test_tstamps)
imu_samps['rob_min_shuffled'] = imus.sample_rob(shuffled_test_tstamps)

print()
print("imu methods are divergent because the test ts land between samples and nearest and insert order interp disagree")
print()

# basic test is that all of these methods should return the same result for each stream
all_equal = {
'gaze': np.zeros((6, 6), dtype=bool),
'imu': np.zeros((6, 6), dtype=bool)
}
kc = 0
for k in gaze_samps:
jc = 0
for j in gaze_samps:
all_equal['gaze'][kc, jc] = np.all(gaze_samps[k] == gaze_samps[j])
all_equal['imu'][kc, jc] = np.all(imu_samps[k] == imu_samps[j])

jc += 1

kc += 1

print(all_equal)
print('all equal gaze:', np.all(all_equal['gaze']))
print('all equal imu:', np.all(all_equal['imu']))

# let's test linear interpolation methods
gaze_samps_linear = {
'np.interp': [],
'np.interp_shuffled': [],
'rob.interp': [],
'rob.interp_shuffled': [],
'rob_min': [],
'rob_min_shuffled': []
}

# np.interp approach
gaze_samps_linear['np.interp'] = gaze.sample(test_tstamps, method="linear")
gaze_samps_linear['np.interp_shuffled'] = gaze.sample(shuffled_test_tstamps, method="linear")

# rob approach
gaze_samps_linear['rob.interp'] = gaze.sample_rob_interp(test_tstamps)
gaze_samps_linear['rob.interp_shuffled'] = gaze.sample_rob_interp(shuffled_test_tstamps)

# when asking linear interp to sample at tstamps that exist in the original data,
# we should get back the same results as min diff, nearest neighbor search on those
# same input tstamps
gaze_samps_linear['rob_min'] = gaze.sample_rob(test_tstamps)
gaze_samps_linear['rob_min_shuffled'] = gaze.sample_rob(shuffled_test_tstamps)

# basic test is that all of these methods should return the same result for each stream
all_equal_interp = {
'gaze': np.zeros((6, 6), dtype=bool),
}
kc = 0
for k in gaze_samps_linear:
jc = 0
for j in gaze_samps_linear:
all_equal_interp['gaze'][kc, jc] = np.all(gaze_samps_linear[k] == gaze_samps_linear[j])

jc += 1

kc += 1

print(all_equal_interp)
print('all equal gaze (linear interp):', np.all(all_equal_interp['gaze']))

# let's see if linearly interpolating between timestamps gives same results between np.interp
# and rob.interp. then, i feel fairly confident that all is working

test_tstamps = [tstamps[100], tstamps[150], tstamps[200], tstamps[len(tstamps)//2], tstamps[-400], tstamps[-203], tstamps[-200]]
for ic in range(len(test_tstamps)):
test_tstamps[ic] = test_tstamps[ic] + np.random.uniform(0, 0.5)

shuffled_test_tstamps = random.sample(test_tstamps, len(test_tstamps))

gaze_samps_shifted = {
'np.interp': [],
'np.interp_shuffled': [],
'rob.interp': [],
'rob.interp_shuffled': [],
}

# np.interp approach
gaze_samps_shifted['np.interp'] = gaze.sample(test_tstamps, method="linear")
gaze_samps_shifted['np.interp_shuffled'] = gaze.sample(shuffled_test_tstamps, method="linear")

# rob approach
gaze_samps_shifted['rob.interp'] = gaze.sample_rob_interp(test_tstamps)
gaze_samps_shifted['rob.interp_shuffled'] = gaze.sample_rob_interp(shuffled_test_tstamps)

all_equal_interp_shifted = {
'gaze': np.zeros((4, 4), dtype=bool),
}
kc = 0
for k in gaze_samps_shifted:
jc = 0
for j in gaze_samps_shifted:
all_equal_interp_shifted['gaze'][kc, jc] = np.all(gaze_samps_shifted[k] == gaze_samps_shifted[j])

jc += 1

kc += 1

print(all_equal_interp_shifted)
print('all equal gaze shifted (linear interp):', np.all(all_equal_interp_shifted['gaze']))
Loading
Loading