Skip to content

Commit

Permalink
added two more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlib committed Nov 18, 2023
1 parent b54a2c3 commit a9fbc8b
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 34 deletions.
7 changes: 4 additions & 3 deletions openptv_python/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,8 @@ def assess_new_position(
for cam in range(run.cpar.num_cams):
# Convert 3D search position to 2D pixel coordinates
pixel = point_to_pixel(pos, run.cal[cam], run.cpar)
print(f"pos {pos}")
print(f"pixel {pixel}")
# print(f"pos {pos}")
# print(f"pixel {pixel}")

# Nearest neighbor search
num_cands = candsearch_in_pix_rest(
Expand Down Expand Up @@ -792,6 +792,7 @@ def trackcorr_c_loop(run_info, step):

# 3D-position
X[1] = vec_copy(curr_path_inf.x)
# print(f"X[1] {X[1]}")

# use information from previous to locate new search position
# and to calculate values for search area
Expand Down Expand Up @@ -853,7 +854,7 @@ def trackcorr_c_loop(run_info, step):
# print(f"X[4] {X[4]}")

diff_pos = vec_subt(X[4], X[3])
# print(f"inside kk loop {kk}")
# print(f"inside kk loop {kk}")
# print(f"diff_pos {diff_pos}")

if pos3d_in_bounds(diff_pos, tpar):
Expand Down
48 changes: 26 additions & 22 deletions openptv_python/tracking_frame_buf.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ def read_targets(file_base: str, frame_num: int) -> List[Target]:
else:
filename = f"{file_base}_targets"

# print(f" filename: {filename}")

try:
with open(filename, "r", encoding="utf-8") as file:
num_targets = int(file.readline().strip())
Expand Down Expand Up @@ -199,6 +201,7 @@ def read_targets(file_base: str, frame_num: int) -> List[Target]:
print(f"Can't open ascii file: {filename}")
raise err

# print(f" read {len(buffer)} targets from {filename}")
return buffer


Expand Down Expand Up @@ -578,26 +581,26 @@ def __init__(
self.prio_file_base = prio_file_base
self.target_file_base = target_file_base

def write_frame_from_start(self, frame_num):
"""Write a frame to disk and advance the buffer."""
# Write the frame to disk
frame = self.buf[0] # first frame
cor_buf = frame.correspond
path_buf = frame.path_info
num_parts = frame.num_parts

write_path_frame(
cor_buf,
path_buf,
num_parts,
self.corres_file_base,
self.linkage_file_base,
self.prio_file_base,
frame_num,
)

# Advance the buffer
self.buf.appendleft(Frame(self.num_cams, MAX_TARGETS))
# def write_frame_from_start(self, frame_num):
# """Write a frame to disk and advance the buffer."""
# # Write the frame to disk
# frame = self.buf[0] # first frame
# cor_buf = frame.correspond
# path_buf = frame.path_info
# num_parts = frame.num_parts

# write_path_frame(
# cor_buf,
# path_buf,
# num_parts,
# self.corres_file_base,
# self.linkage_file_base,
# self.prio_file_base,
# frame_num,
# )

# # Advance the buffer
# # self.buf.appendleft(Frame(self.num_cams, MAX_TARGETS))

def read_frame_at_end(self, frame_num: int, read_links: bool = False) -> None:
"""Read a frame from the disk and add it to the end of the buffer."""
Expand Down Expand Up @@ -650,7 +653,7 @@ def disk_read_frame_at_end(self, frame_num: int, read_links: bool):
frame_num,
)

def disk_write_frame_from_start(self, frame_num: int):
def write_frame_from_start(self, frame_num: int):
"""Write the frame to the first position in the ring.
Arguments:
Expand All @@ -663,6 +666,7 @@ def disk_write_frame_from_start(self, frame_num: int):
True on success, false on failure.
"""
frame = self.buf[0] # always the first one on the left

return frame.write(
self.corres_file_base,
self.linkage_file_base,
Expand Down Expand Up @@ -701,7 +705,7 @@ def read_path_frame(
"""
fname = f"{corres_file_base}.{frame_num}"
print(fname)
# print(fname)

try:
filein = open(fname, "r", encoding="utf-8")
Expand Down
182 changes: 176 additions & 6 deletions tests/test_tracking_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@
import unittest
from pathlib import Path

import numpy as np

from openptv_python.calibration import Calibration, read_calibration
from openptv_python.parameters import (
read_control_par,
)
from openptv_python.track import (
track_forward_start,
trackback_c,
trackcorr_c_finish,
trackcorr_c_loop,
)
from openptv_python.tracking_run import (
tr_new,
)
from openptv_python.vec_utils import vec_norm

EPS = 1e-9

Expand Down Expand Up @@ -92,6 +96,14 @@ def test_trackcorr_no_add(self):
os.chdir(directory)

print(os.path.abspath(os.curdir))
print(Path.cwd())

if Path("res/").exists():
remove_directory("res/")

if Path("img/").exists():
remove_directory("img/")


copy_directory("res_orig/", "res/")
copy_directory("img_orig/", "img/")
Expand All @@ -116,17 +128,85 @@ def test_trackcorr_no_add(self):
10000.0,
)
run.tpar.add = 0

# print(f"run.tpar = {run.tpar}")
print(f"run.seq_par.first = {run.seq_par.first} run.seq_par.last = {run.seq_par.last}")

track_forward_start(run)
trackcorr_c_loop(run, run.seq_par.first)

for step in range(run.seq_par.first + 1, run.seq_par.last):
# print(f"step = {step}")
trackcorr_c_loop(run, step)
print(f"run.npart = {run.npart}")
print(f"run.nlinks = {run.nlinks}")
# print(f"run.npart = {run.npart}")
# print(f"run.nlinks = {run.nlinks}")

trackcorr_c_finish(run, run.seq_par.last)


range_val = run.seq_par.last - run.seq_par.first
npart = run.npart / range_val
nlinks = run.nlinks / range_val

self.assertAlmostEqual(npart, 0.9, delta=EPS)
self.assertAlmostEqual(nlinks, 0.8, delta=EPS)

remove_directory("res/")
remove_directory("img/")

os.chdir(current_directory)

def test_trackcorr_add(self):
"""Test tracking without adding particles."""
# import os

current_directory = Path.cwd()
print(f"working from {current_directory}")
directory = Path("tests/testing_fodder/track")

os.chdir(directory)

print(os.path.abspath(os.curdir))
print(Path.cwd())

if Path("res/").exists():
remove_directory("res/")

if Path("img/").exists():
remove_directory("img/")


copy_directory("res_orig/", "res/")
copy_directory("img_orig/", "img/")

print("----------------------------")
print("Test tracking multiple files 2 cameras, 1 particle")
cpar = read_control_par("parameters/ptv.par")

calib = read_all_calibration(cpar.num_cams)
# calib.append(Calibration())

run = tr_new(
"parameters/sequence.par",
"parameters/track.par",
"parameters/criteria.par",
"parameters/ptv.par",
4,
20000,
"res/rt_is",
"res/ptv_is",
"res/added",
calib,
10000.0,
)

run.seq_par.first = 10240
run.seq_par.last = 10250
run.tpar.add = 1

track_forward_start(run)
trackcorr_c_loop(run, run.seq_par.first)

for step in range(run.seq_par.first + 1, run.seq_par.last):
trackcorr_c_loop(run, step)

trackcorr_c_finish(run, run.seq_par.last)

Expand All @@ -137,11 +217,101 @@ def test_trackcorr_no_add(self):
npart = run.npart / range_val
nlinks = run.nlinks / range_val

self.assertAlmostEqual(npart, 1.0, delta=EPS)
self.assertAlmostEqual(nlinks, 1.0, delta=EPS)
self.assertTrue(
abs(npart - 1.0) < EPS,
f"Was expecting npart == 208/210 but found {npart}"
)
self.assertTrue(
abs(nlinks - 0.7) < EPS,
f"Was expecting nlinks == 328/210 but found {nlinks}"
)


os.chdir(current_directory)

class TestTrackback(unittest.TestCase):

def test_trackback(self):

current_directory = Path.cwd()
print(f"working from {current_directory}")
directory = Path("tests/testing_fodder/track")

os.chdir(directory)

print(os.path.abspath(os.curdir))
print(Path.cwd())

if Path("res/").exists():
remove_directory("res/")

if Path("img/").exists():
remove_directory("img/")


copy_directory("res_orig/", "res/")
copy_directory("img_orig/", "img/")

print("----------------------------")
print("Test tracking multiple files 2 cameras, 1 particle")
cpar = read_control_par("parameters/ptv.par")

calib = read_all_calibration(cpar.num_cams)
# calib.append(Calibration())

run = tr_new(
"parameters/sequence.par",
"parameters/track.par",
"parameters/criteria.par",
"parameters/ptv.par",
4,
20000,
"res/rt_is",
"res/ptv_is",
"res/added",
calib,
10000.0,
)

run.seq_par.first = 10240
run.seq_par.last = 10250
run.tpar.add = 1

track_forward_start(run)
trackcorr_c_loop(run, run.seq_par.first)

for step in range(run.seq_par.first + 1, run.seq_par.last):
trackcorr_c_loop(run, step)

trackcorr_c_finish(run, run.seq_par.last)


run.tpar.dvxmin = run.tpar.dvymin = run.tpar.dvzmin = -50
run.tpar.dvxmax = run.tpar.dvymax = run.tpar.dvzmax = 50


run.lmax = vec_norm(
np.r_[
(run.tpar.dvxmin - run.tpar.dvxmax),
(run.tpar.dvymin - run.tpar.dvymax),
(run.tpar.dvzmin - run.tpar.dvzmax)
]
)

nlinks = trackback_c(run)

print(f"nlinks = {nlinks}")

# Uncomment the following lines to add assertions
self.assertTrue(
abs(nlinks - 1.0) < EPS,
f"Was expecting nlinks to be 1.0 but found {nlinks}"
)

remove_directory("res/")
remove_directory("img/")

os.chdir(current_directory)

if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions tests/test_trans_cam_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_back_trans_point(self):
# test_cal = Calibration(test_Ex, test_I, test_G, test_addp)

#
test_mm = MultimediaPar(1, 1.0, (1.49, 0.0, 0.0), (5.0, 0.0, 0.0), 1.33)
test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [5.0, 0.0, 0.0], 1.33)

Ex_t = Exterior()

Expand Down Expand Up @@ -81,7 +81,7 @@ def test_trans_cam_point(self):
# ap_52 test_addp = {0., 0., 0., 0., 0., 1., 0.};
# Calibration test_cal = {test_Ex, test_I, test_G, test_addp};

test_mm = MultimediaPar(1, 1.0, (1.49, 0.0, 0.0), (5.0, 0.0, 0.0), 1.33)
test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [5.0, 0.0, 0.0], 1.33)

Ex_t = Exterior()
pos_t, cross_p, cross_c = trans_cam_point(test_Ex, test_mm, test_G, pos, Ex_t)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_vec_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_vec_cmp():
vec1 = np.array([1.0, 2.0, 3.0])
vec2 = np.array([1.0, 2.0, 3.0])
assert vec_cmp(vec1, vec2, 1e-4)
vec3 = [4.0, 5.0, 6.0]
vec3 = np.array([4.0, 5.0, 6.0])
assert not vec_cmp(vec1, vec3, 1e-4)


Expand Down

0 comments on commit a9fbc8b

Please sign in to comment.