Skip to content

Commit

Permalink
Refactor and clean up code for code coverage #193
Browse files Browse the repository at this point in the history
  • Loading branch information
hcwinsemius committed Dec 10, 2024
1 parent 7552a8e commit ccb8571
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
16 changes: 3 additions & 13 deletions pyorc/cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ def get_ms_gftt(cap, start_frame=0, end_frame=None, n_pts=None, split=2, mask=No
"""
# set end_frame to last if not defined
if end_frame is None:
end_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

end_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) if end_frame is None else end_frame
m = np.eye(3)[0:2]
# m2 = np.eye(3)[0:2]
ms = []
Expand Down Expand Up @@ -566,7 +564,6 @@ def calibrate_camera(
idx = np.array(errs) < tolerance
obj_pts = list(np.array(obj_pts)[idx])
img_pts = list(np.array(img_pts)[idx])
print(len(img_pts))
# do calibration
ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(obj_pts, img_pts, frame_size, None, None)
errs = []
Expand Down Expand Up @@ -954,21 +951,14 @@ def get_time_frames(cap, start_frame, end_frame, lazy=True, fps=None, **kwargs):
n = start_frame
time = []
frame_number = []
if lazy:
frames = None
else:
# already collect the frames
frames = []
frames = None if lazy else []
while ret:
if n > end_frame:
break
if not lazy:
frames.append(img)
t1 = cap.get(cv2.CAP_PROP_POS_MSEC)
if fps is not None:
time.append(n * 1000.0 / fps)
else:
time.append(t1)
time.append(n * 1000.0 / fps) if fps is not None else time.append(t1)
frame_number.append(n)
n += 1
ret, img = get_frame(cap, **kwargs) # read frame 1 + ...
Expand Down
11 changes: 2 additions & 9 deletions pyorc/velocimetry/openpiv.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,6 @@ def piv(
return v_x * res_x, v_y * res_y, s2n, corr


def get_piv_size(**kwargs):
"""Get column, row coordinates of center of PIV windows."""
return openpiv.pyprocess.get_coordinates(**kwargs)


def extended_search_area_piv(
frame_a: np.ndarray,
frame_b: np.ndarray,
Expand Down Expand Up @@ -270,8 +265,7 @@ def extended_search_area_piv(
overlap = [overlap, overlap]

# check the inputs for validity
if search_area_size is None:
search_area_size = window_size
search_area_size = window_size if search_area_size is None else search_area_size

if overlap[0] >= window_size[0] or overlap[1] >= window_size[1]:
raise ValueError("Overlap has to be smaller than the window_size")
Expand Down Expand Up @@ -321,15 +315,14 @@ def extended_search_area_piv(
u, v = openpiv.pyprocess.correlation_to_displacement(corr, n_rows, n_cols, subpixel_method=subpixel_method)

# return output depending if user wanted sig2noise information
sig2noise = np.zeros_like(u) * np.nan
if sig2noise_method is not None:
if use_vectorized == True:
sig2noise = openpiv.pyprocess.vectorized_sig2noise_ratio(
corr, sig2noise_method=sig2noise_method, width=width
)
else:
sig2noise = openpiv.pyprocess.sig2noise_ratio(corr, sig2noise_method=sig2noise_method, width=width)
else:
sig2noise = np.zeros_like(u) * np.nan

sig2noise = sig2noise.reshape(n_rows, n_cols)
# extended code for exporting the maximum found value for corr
Expand Down
9 changes: 9 additions & 0 deletions tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def test_normalize(frames, samples, request):
), f'dtype of result is {frames_norm[0, 0, 0].values.dtype}, expected "uint8"'


def test_smooth(frames_grayscale):
frames_smooth = frames_grayscale.frames.smooth()
assert frames_smooth.shape == frames_grayscale.shape
assert (
frames_smooth[0, 0, 0].values.dtype == "float32"
), f'dtype of result is {frames_smooth[0, 0, 0].values.dtype}, expected "float32"'
assert np.allclose(frames_smooth.values.flatten()[-4:], [158.125, 153.5, 151.375, 151.0])


def test_edge_detect(frames_proj):
frames_edge = frames_proj.frames.edge_detect()
assert frames_edge.shape == frames_proj.shape
Expand Down

0 comments on commit ccb8571

Please sign in to comment.