Skip to content

Commit

Permalink
fixing mypy inconsistinces
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlib committed Nov 18, 2023
1 parent bf79cab commit 1d7aa50
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 20 deletions.
1 change: 1 addition & 0 deletions openptv_python/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PRIO_DEFAULT = 2
CORRES_NONE = -1

SORTGRID_EPS = 25

TR_UNUSED = -1
TR_BUFSPACE = 4 # 4 frames in the buffer to track
Expand Down
2 changes: 1 addition & 1 deletion openptv_python/correspondences.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def safely_allocate_adjacency_lists(
num_cams: int, target_counts: List[int]
) -> List[List[List[Correspond]]]:
"""Allocate adjacency lists."""
lists = [[[] for _ in range(num_cams)] for _ in range(num_cams)]
lists = [[[float] for _ in range(num_cams)] for _ in range(num_cams)]
error = 0

for c1 in range(num_cams - 1):
Expand Down
11 changes: 7 additions & 4 deletions openptv_python/epi.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def epi_mm(xl, yl, cal1, cal2, mmp, vpar) -> tuple[float, float, float, float]:
_type_: _description_
"""
z_min, z_max = 0, 0
pos, v, X = [0, 0, 0], [0, 0, 0], [0, 0, 0]
# pos, v, X = [0, 0, 0], [0, 0, 0], [0, 0, 0]

pos, v = ray_tracing(xl, yl, cal1, mmp)

Expand All @@ -102,9 +102,12 @@ def epi_mm(xl, yl, cal1, cal2, mmp, vpar) -> tuple[float, float, float, float]:
vpar.z_min_lay[1] - vpar.z_min_lay[0]
) / (vpar.x_lay[1] - vpar.x_lay[0])

z_max = vpar.z_max_lay[0] + (pos[0] - vpar.x_lay[0]) * (
vpar.z_max_lay[1] - vpar.z_max_lay[0]
) / (vpar.x_lay[1] - vpar.x_lay[0])
z_max = float(
vpar.z_max_lay[0]
+ (pos[0] - vpar.x_lay[0])
* (vpar.z_max_lay[1] - vpar.z_max_lay[0])
/ (vpar.x_lay[1] - vpar.x_lay[0])
)

X = move_along_ray(z_min, pos, v)
xmin, ymin = flat_image_coord(X, cal2, mmp)
Expand Down
13 changes: 7 additions & 6 deletions openptv_python/sortgrid.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import List, Tuple
from typing import List

import numpy as np

from openptv_python.calibration import Calibration
from openptv_python.parameters import ControlPar

from .constants import POS_INF, PT_UNUSED
from .constants import POS_INF, PT_UNUSED, SORTGRID_EPS
from .epi import Coord3d
from .imgcoord import img_coord
from .tracking_frame_buf import Target
Expand Down Expand Up @@ -113,13 +113,14 @@ def read_sortgrid_par(filename) -> int:
with open(filename, "r", encoding="utf-8") as fpp:
eps = int(fpp.readline())
except IOError:
print(f"Can't open sortgrid parameter file: {filename} using default value")
# handle error
eps = None
eps = SORTGRID_EPS

return eps


def read_calblock(filename: str) -> Tuple[Coord3d]:
def read_calblock(filename: str) -> List[Coord3d]:
"""
Read the calibration block file into the structure of 3D positions and pointers.
Expand All @@ -146,9 +147,9 @@ def read_calblock(filename: str) -> Tuple[Coord3d]:
coords.append(coord)
except FileNotFoundError:
print(f"Can't open calibration block file: {filename}")
return None
return []
except ValueError:
print(f"Empty or badly formatted file: {filename}")
return None
return []

return coords
56 changes: 48 additions & 8 deletions openptv_python/track.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Tracking algorithm."""
import math
from dataclasses import dataclass, field
from typing import List, Tuple

Expand Down Expand Up @@ -209,14 +208,53 @@ def pos3d_in_bounds(pos, bounds):
)


# def angle_acc(
# start: np.ndarray, pred: np.ndarray, cand: np.ndarray
# ) -> Tuple[float, float]:
# """Calculate the angle between the (1st order) numerical velocity vectors.

# to the predicted next_frame position and to the candidate actual position. The
# angle is calculated in [gon], see [1]. The predicted position is the
# position if the particle continued at current velocity.

# Arguments:
# ---------
# start -- vec3d, the particle start position
# pred -- vec3d, predicted position
# cand -- vec3d, possible actual position

# Returns:
# -------
# angle -- float, the angle between the two velocity vectors, [gon]
# acc -- float, the 1st-order numerical acceleration embodied in the deviation from prediction.
# """
# v0 = pred - start
# v1 = cand - start

# acc = math.dist(v0, v1)
# # acc = np.linalg.norm(v0 - v1)

# if np.all(v0 == -v1):
# angle = 200
# elif np.all(v0 == v1):
# angle = 0
# else:
# angle = float((200.0 / math.pi) * math.acos(
# math.fsum([v0[i] * v1[i] for i in range(3)])
# / (math.dist(start, pred) * math.dist(start, cand)))
# )

# return angle, acc


def angle_acc(
start: np.ndarray, pred: np.ndarray, cand: np.ndarray
) -> Tuple[float, float]:
"""Calculate the angle between the (1st order) numerical velocity vectors.
to the predicted next_frame position and to the candidate actual position. The
angle is calculated in [gon], see [1]. The predicted position is the
position if the particle continued at current velocity.
position if the particle continued at the current velocity.
Arguments:
---------
Expand All @@ -232,20 +270,22 @@ def angle_acc(
v0 = pred - start
v1 = cand - start

acc = math.dist(v0, v1)
# acc = np.linalg.norm(v0 - v1)
acc = np.linalg.norm(v0 - v1)

if np.all(v0 == -v1):
angle = 200
elif np.all(v0 == v1):
angle = 0
else:
angle = (200.0 / math.pi) * math.acos(
math.fsum([v0[i] * v1[i] for i in range(3)])
/ (math.dist(start, pred) * math.dist(start, cand))
dot_product = np.sum(v0 * v1)
norm_start_pred = np.linalg.norm(start - pred)
norm_start_cand = np.linalg.norm(start - cand)

angle = (200.0 / np.pi) * np.arccos(
dot_product / (norm_start_pred * norm_start_cand)
)

return angle, acc
return float(angle), float(acc)


def candsearch_in_pix(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_sortgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

from openptv_python.calibration import read_calibration
from openptv_python.constants import SORTGRID_EPS
from openptv_python.parameters import read_control_par
from openptv_python.sortgrid import (
nearest_neighbour_pix,
Expand Down Expand Up @@ -37,7 +38,7 @@ def test_read_sortgrid_par(self):
eps = read_sortgrid_par(
"tests/testing_fodder/parameters/sortgrid_corrupted.par"
)
self.assertEqual(eps, None)
self.assertEqual(eps, SORTGRID_EPS)

def test_read_calblock(self):
"""Test reading calblock.txt file."""
Expand Down

0 comments on commit 1d7aa50

Please sign in to comment.