From 1d7aa50b4106395493944b8ea3b9fdfbd084094f Mon Sep 17 00:00:00 2001 From: Alex Liberzon Date: Sat, 18 Nov 2023 12:01:03 +0200 Subject: [PATCH] fixing mypy inconsistinces --- openptv_python/constants.py | 1 + openptv_python/correspondences.py | 2 +- openptv_python/epi.py | 11 +++--- openptv_python/sortgrid.py | 13 +++---- openptv_python/track.py | 56 ++++++++++++++++++++++++++----- tests/test_sortgrid.py | 3 +- 6 files changed, 66 insertions(+), 20 deletions(-) diff --git a/openptv_python/constants.py b/openptv_python/constants.py index 2ea51a1..286ac50 100644 --- a/openptv_python/constants.py +++ b/openptv_python/constants.py @@ -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 diff --git a/openptv_python/correspondences.py b/openptv_python/correspondences.py index 8801de8..6729123 100644 --- a/openptv_python/correspondences.py +++ b/openptv_python/correspondences.py @@ -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): diff --git a/openptv_python/epi.py b/openptv_python/epi.py index 8277514..a779188 100644 --- a/openptv_python/epi.py +++ b/openptv_python/epi.py @@ -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) @@ -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) diff --git a/openptv_python/sortgrid.py b/openptv_python/sortgrid.py index 00f843d..b24dda4 100644 --- a/openptv_python/sortgrid.py +++ b/openptv_python/sortgrid.py @@ -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 @@ -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. @@ -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 diff --git a/openptv_python/track.py b/openptv_python/track.py index a181422..8fa2cc4 100644 --- a/openptv_python/track.py +++ b/openptv_python/track.py @@ -1,5 +1,4 @@ """Tracking algorithm.""" -import math from dataclasses import dataclass, field from typing import List, Tuple @@ -209,6 +208,45 @@ 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]: @@ -216,7 +254,7 @@ def angle_acc( 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: --------- @@ -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( diff --git a/tests/test_sortgrid.py b/tests/test_sortgrid.py index d62da98..d77751e 100644 --- a/tests/test_sortgrid.py +++ b/tests/test_sortgrid.py @@ -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, @@ -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."""