Skip to content

Commit d4ed11e

Browse files
committed
updated Path
1 parent ae1a2f6 commit d4ed11e

29 files changed

+367
-778
lines changed

openptv_python/calibration.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Calibration data structures and functions."""
22

33
import copy
4-
import pathlib
4+
from pathlib import Path
55
from typing import Optional
66

77
import numpy as np
@@ -127,7 +127,7 @@ def __init__(self,
127127

128128

129129
@classmethod
130-
def from_file(cls, ori_file: str, add_file: str | None):
130+
def from_file(cls, ori_file: Path, add_file: Path | None):
131131
"""
132132
Read exterior and interior orientation, and if available, parameters for distortion corrections.
133133
@@ -141,7 +141,7 @@ def from_file(cls, ori_file: str, add_file: str | None):
141141
-------
142142
- ext_par, int_par, glass, addp: Calibration object parts without multimedia lookup table.
143143
"""
144-
if not pathlib.Path(ori_file).exists():
144+
if not ori_file.exists():
145145
raise IOError(f"File {ori_file} does not exist")
146146

147147
ret = cls()
@@ -166,6 +166,8 @@ def from_file(cls, ori_file: str, add_file: str | None):
166166
# this is anyhow default
167167
# self.mmlut.data = None # no multimedia data yet
168168

169+
ret.added_par = np.array([0,0,0,0,0,1,0], dtype=np.float64)
170+
169171
# Additional parameters
170172
if add_file is not None:
171173
with open(add_file, "r", encoding="utf-8") as fp:
@@ -179,10 +181,6 @@ def from_file(cls, ori_file: str, add_file: str | None):
179181
else:
180182
# print("no addpar fallback used") # Waits for proper logging.
181183
print("No addpar file found. Using default values for radial distortion")
182-
# ret.added_par.k1 = ret.added_par.k2 = ret.added_par.k3 \
183-
# = ret.added_par.p1 = ret.added_par.p2 = ret.added_par.she = 0.0
184-
# ret.added_par.scx = 1.0
185-
ret.added_par = np.array([0,0,0,0,0,1,0], dtype=np.float64)
186184

187185
return ret
188186
# print(f"Calibration data read from files {ori_file} and {add_file}")
@@ -395,7 +393,7 @@ def write_ori(
395393
ext_par: np.recarray,
396394
int_par: np.recarray,
397395
glass: np.ndarray,
398-
added_par: np.recarray,
396+
added_par: np.ndarray,
399397
filename: str,
400398
add_file: Optional[str],
401399
) -> bool:
@@ -424,7 +422,7 @@ def write_ori(
424422
return success
425423

426424

427-
def read_ori(ori_file: str, add_file: str) -> Calibration:
425+
def read_ori(ori_file: Path, add_file: Path) -> Calibration:
428426
"""
429427
Read exterior and interior orientation, and if available, parameters for distortion corrections.
430428
@@ -495,7 +493,7 @@ def compare_addpar(a1, a2):
495493
return np.array_equal(a1, a2)
496494

497495

498-
def read_calibration(ori_file: str, addpar_file: str) -> Calibration:
496+
def read_calibration(ori_file: Path, addpar_file: Path | None) -> Calibration:
499497
"""Read the orientation file including the added parameters."""
500498
return Calibration().from_file(ori_file, addpar_file)
501499

openptv_python/parameters.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Parameters for OpenPTV-Python."""
2-
import os
32
from dataclasses import asdict, dataclass, field
43
from pathlib import Path
54
from typing import List, Tuple
@@ -146,9 +145,9 @@ def get_last(self):
146145
return self.last
147146

148147
@classmethod
149-
def from_file(cls, filename: str, num_cams: int):
148+
def from_file(cls, filename: Path, num_cams: int):
150149
"""Read sequence parameters from file."""
151-
if not Path(filename).exists():
150+
if not filename.exists():
152151
raise IOError("File {filename} does not exist.")
153152

154153
ret = cls()
@@ -162,7 +161,7 @@ def from_file(cls, filename: str, num_cams: int):
162161
return ret
163162

164163

165-
def read_sequence_par(filename: str, num_cams: int = TR_MAX_CAMS) -> SequencePar:
164+
def read_sequence_par(filename: Path, num_cams: int = TR_MAX_CAMS) -> SequencePar:
166165
"""Read sequence parameters from file and return SequencePar object."""
167166
return SequencePar().from_file(filename, num_cams)
168167

@@ -213,7 +212,7 @@ class TrackPar(Parameters):
213212
# }
214213

215214
@classmethod
216-
def from_file(cls, filename: str):
215+
def from_file(cls, filename: Path):
217216
"""Read tracking parameters from file and return TrackPar object.
218217
219218
Note that the structure has 13 attributes, from which we read only 9
@@ -306,7 +305,7 @@ def get_dny(self):
306305
return self.dny
307306

308307

309-
def read_track_par(filename: str) -> TrackPar:
308+
def read_track_par(filename: Path) -> TrackPar:
310309
"""Read tracking parameters from file and return TrackPar object."""
311310
return TrackPar().from_file(filename)
312311

@@ -377,7 +376,7 @@ def set_corrmin(self, corrmin: float):
377376
self.corrmin = corrmin
378377

379378
@classmethod
380-
def from_file(cls, filename: str):
379+
def from_file(cls, filename: Path):
381380
"""Read volume parameters from file.
382381
383382
Args:
@@ -401,7 +400,7 @@ def from_file(cls, filename: str):
401400
return cls(x_lay, z_min_lay, z_max_lay, cn, cnx, cny, csumg, eps0, corrmin)
402401

403402

404-
def read_volume_par(filename: str) -> VolumePar:
403+
def read_volume_par(filename: Path) -> VolumePar:
405404
"""Read volume parameters from file and returns volume_par object.
406405
407406
Args:
@@ -497,10 +496,10 @@ def get_tiff_flag(self):
497496
return self.tiff_flag
498497

499498
@classmethod
500-
def from_file(cls, filename: str):
499+
def from_file(cls, filename: Path):
501500
"""Read control parameters from file and return ControlPar object."""
502501
ret = cls()
503-
if not os.path.isfile(filename):
502+
if not filename.exists():
504503
raise FileNotFoundError(f"Could not open file {filename}")
505504

506505
with open(filename, "r", encoding="utf-8") as par_file:
@@ -546,7 +545,7 @@ def to_dict(cls, data):
546545
return control_par_dict
547546

548547

549-
def read_control_par(filename: str) -> ControlPar:
548+
def read_control_par(filename: Path) -> ControlPar:
550549
"""Read control parameters from file and return ControlPar object."""
551550
return ControlPar().from_file(filename)
552551

@@ -608,7 +607,7 @@ class TargetPar(Parameters):
608607
# }
609608

610609
@classmethod
611-
def from_file(cls, filename: str):
610+
def from_file(cls, filename: Path):
612611
"""Read target parameters from file and returns target_par object.
613612
614613
Reads target recognition parameters from a legacy detect_plate.par file,
@@ -673,7 +672,7 @@ def get_min_sum_grey(self):
673672
"""Return the sum grey bounds."""
674673
return self.sumg_min
675674

676-
def read_target_par(filename: str) -> TargetPar:
675+
def read_target_par(filename: Path) -> TargetPar:
677676
"""Read target parameters from file and returns target_par object."""
678677
tpar = TargetPar()
679678
return tpar.from_file(filename)
@@ -712,7 +711,7 @@ class OrientPar(Parameters):
712711
interfflag: int = 0
713712

714713
@classmethod
715-
def from_file(cls, filename: str):
714+
def from_file(cls, filename: Path):
716715
"""Read orientation parameters from file and returns orient_par object."""
717716
ret = cls()
718717
try:
@@ -777,7 +776,7 @@ def from_file(cls, file_path: str, num_cams: int):
777776
return cls(fixp_name, img_name, img_ori0, tiff_flag, pair_flag, chfield)
778777

779778

780-
def read_cal_ori_parameters(file_path: str, num_cams: int) -> CalibrationPar:
779+
def read_cal_ori_parameters(file_path: Path, num_cams: int) -> CalibrationPar:
781780
"""Read from cal_ori.par file."""
782781
with open(file_path, 'r', encoding="utf-8") as file:
783782
fixp_name = file.readline().strip()
@@ -799,7 +798,7 @@ class MultiPlanesPar(Parameters):
799798
filename: list = field(default_factory=list)
800799

801800
@classmethod
802-
def from_file(cls, file_path: str):
801+
def from_file(cls, file_path: Path):
803802
"""Read from multiplanes.par file."""
804803
with open(file_path, 'r', encoding="utf-8") as file:
805804
num_planes = int(file.readline().strip())
@@ -814,14 +813,14 @@ class ExaminePar(Parameters):
814813
combine_flag: bool = False
815814

816815
@classmethod
817-
def from_file(cls, file_path: str):
816+
def from_file(cls, file_path: Path):
818817
"""Read from examine.par file."""
819818
with open(file_path, 'r', encoding="utf-8") as file:
820819
examine_flag = bool(int(file.readline().strip()))
821820
combine_flag = bool(int(file.readline().strip()))
822821
return cls(examine_flag, combine_flag)
823822

824-
def read_examine_par(file_path: str) -> ExaminePar:
823+
def read_examine_par(file_path: Path) -> ExaminePar:
825824
"""Read from examine.par file."""
826825
with open(file_path, 'r', encoding="utf-8") as file:
827826
examine_flag = bool(int(file.readline().strip()))
@@ -835,14 +834,14 @@ class PftVersionPar(Parameters):
835834
existing_target_flag: bool = False
836835

837836
@classmethod
838-
def from_file(cls, file_path: str):
837+
def from_file(cls, file_path: Path):
839838
"""Read from pft_version.par file."""
840839
with open(file_path, 'r', encoding="utf-8") as file:
841840
pft_version = bool(int(file.readline().strip()))
842841
return cls(pft_version)
843842

844843
@classmethod
845-
def write(cls, file_path: str):
844+
def write(cls, file_path: Path):
846845
"""Write to pft_version.par file."""
847846
with open(file_path, 'w', encoding="utf-8") as file:
848847
file.write(f"{cls.existing_target_flag}\n")

openptv_python/sortgrid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from typing import List
23

34
import numpy as np
@@ -119,7 +120,7 @@ def read_sortgrid_par(filename) -> int:
119120
return eps
120121

121122

122-
def read_calblock(filename: str) -> np.recarray: #List[Coord3d]:
123+
def read_calblock(filename: Path) -> np.recarray: #List[Coord3d]:
123124
"""
124125
Read the calibration block file into the structure of 3D positions and pointers.
125126

openptv_python/tracking_frame_buf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tracking frame buffer."""
22
from collections import deque
33
from dataclasses import dataclass, field
4+
from pathlib import Path
45
from typing import Deque, List, Tuple
56

67
import numpy as np
@@ -172,10 +173,11 @@ def read_targets(file_base: str, frame_num: int) -> List[Target]:
172173

173174
if frame_num > 0:
174175
# filename = f"{file_base}{frame_num:04d}_targets"
175-
filename = file_base % frame_num + '_targets'
176+
fname = file_base % frame_num + '_targets'
176177
else:
177-
filename = f"{file_base}_targets"
178+
fname = f"{file_base}_targets"
178179

180+
filename = Path(fname)
179181
print(f" filename: {filename}")
180182

181183
try:

openptv_python/tracking_run.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tracking run module."""
22
import math
33
from dataclasses import dataclass
4+
from pathlib import Path
45
from typing import List
56

67
from openptv_python.calibration import Calibration
@@ -97,10 +98,10 @@ def __init__(
9798

9899

99100
def tr_new(
100-
seq_par_fname: str,
101-
tpar_fname: str,
102-
vpar_fname: str,
103-
cpar_fname: str,
101+
seq_par_fname: Path,
102+
tpar_fname: Path,
103+
vpar_fname: Path,
104+
cpar_fname: Path,
104105
buf_len: int,
105106
max_targets: int,
106107
corres_file_base: str,

tests/gen_track_data.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
testing. It starts from (0,0,0) and moves in a straight line on the x axis,
55
at a slow velocity.
66
"""
7+
from pathlib import Path
78
from typing import List
89

910
import numpy as np
@@ -21,16 +22,16 @@
2122
part_traject[:, 0] = np.r_[:num_frames] * velocity
2223

2324
# Find targets on each camera.
24-
cpar = ControlPar(num_cams=3).from_file("testing_fodder/track/parameters/control_newpart.par")
25+
cpar = ControlPar(num_cams=3).from_file(Path("tests/testing_fodder/track/parameters/control_newpart.par"))
2526

2627
targs: List[List[List[float]]] = [
2728
[[0.0, 0.0] for _ in range(num_frames)] for _ in range(num_cams)
2829
]
2930

3031
for cam in range(num_cams):
3132
cal = Calibration().from_file(
32-
f"testing_fodder/cal/sym_cam{cam+1}.tif.ori",
33-
"testing_fodder/cal/cam1.tif.addpar",
33+
Path(f"tests/testing_fodder/cal/sym_cam{cam+1}.tif.ori"),
34+
Path("tests/testing_fodder/cal/cam1.tif.addpar"),
3435
)
3536
# check this out
3637
for frame in range(num_frames):
@@ -41,7 +42,7 @@
4142
for frame in range(num_frames):
4243
# write 3D positions:
4344
with open(
44-
f"testing_fodder/track/res_orig/particles.{frame+1}", "w", encoding="utf-8"
45+
f"tests/testing_fodder/track/res_orig/particles.{frame+1}", "w", encoding="utf-8"
4546
) as outfile:
4647
# Note correspondence to the single target in each frame.
4748
outfile.writelines(
@@ -63,7 +64,7 @@
6364
# write associated targets from all cameras:
6465
for cam in range(num_cams):
6566
with open(
66-
f"testing_fodder/track/newpart/cam{cam+1}.{frame+1:04d}_targets",
67+
f"tests/testing_fodder/track/newpart/cam{cam+1}.{frame+1:04d}_targets",
6768
"w",
6869
encoding="utf-8",
6970
) as outfile:

tests/test_burgers.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def read_all_calibration(num_cams: int = 4) -> list[Calibration]:
5858
for cam in range(num_cams):
5959
ori_name = ori_tmpl % (cam + 1)
6060
added_name = added_tmpl % (cam + 1)
61-
calib.append(read_calibration(ori_name, added_name))
61+
calib.append(read_calibration( Path(ori_name), Path(added_name) ))
6262

6363
return calib
6464

@@ -71,6 +71,7 @@ def test_burgers(self):
7171
current_directory = Path.cwd()
7272
print(f"working from {current_directory}")
7373
directory = Path("tests/testing_fodder/burgers")
74+
parameters_path = (directory / "parameters").resolve(strict=True)
7475

7576
os.chdir(directory)
7677

@@ -86,7 +87,7 @@ def test_burgers(self):
8687
copy_directory("res_orig/", "res/")
8788
copy_directory("img_orig/", "img/")
8889

89-
cpar = read_control_par("parameters/ptv.par")
90+
cpar = read_control_par(parameters_path / "ptv.par")
9091
self.assertIsNotNone(cpar)
9192

9293
calib = read_all_calibration(cpar.num_cams)
@@ -95,10 +96,10 @@ def test_burgers(self):
9596
print("Test Burgers vortex case")
9697

9798
run = tr_new(
98-
"parameters/sequence.par",
99-
"parameters/track.par",
100-
"parameters/criteria.par",
101-
"parameters/ptv.par",
99+
parameters_path / "sequence.par",
100+
parameters_path / "track.par",
101+
parameters_path / "criteria.par",
102+
parameters_path / "ptv.par",
102103
4,
103104
20000,
104105
"res/rt_is",
@@ -124,10 +125,10 @@ def test_burgers(self):
124125
run.nlinks, 17, f"Was expecting nlinks == 17 but found {run.nlinks}")
125126

126127
run = tr_new(
127-
"parameters/sequence.par",
128-
"parameters/track.par",
129-
"parameters/criteria.par",
130-
"parameters/ptv.par",
128+
parameters_path / "sequence.par",
129+
parameters_path / "track.par",
130+
parameters_path / "criteria.par",
131+
parameters_path / "ptv.par",
131132
4,
132133
20000,
133134
"res/rt_is",

0 commit comments

Comments
 (0)