Skip to content

Commit

Permalink
improve test coverage #189
Browse files Browse the repository at this point in the history
  • Loading branch information
hcwinsemius committed Nov 8, 2024
1 parent 2dda236 commit 2bcc3b2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,6 @@
"- Frames-per-second of a video are not as constant as we would like them to be and many cameras (in particular cheap IP cameras) are not good at keeping track of the time evolution per frame. This can cause a lot of variability which has nothing to do with real uncertainty. `pyorc` does read frame-specific timings in case frame rates are not constant.\n",
"- last but not least, remaining uncertainties in the PIV solution can cause variability.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
28 changes: 7 additions & 21 deletions pyorc/service/velocimetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,8 @@ def vmin_vmax_to_norm(opts):
"""
if "vmin" in opts or "vmax" in opts:
if "vmin" in opts:
vmin = opts["vmin"]
del opts["vmin"]
else:
vmin = None
if "vmax" in opts:
vmax = opts["vmax"]
del opts["vmax"]
else:
vmax = None
vmin = opts.pop("vmin", None)
vmax = opts.pop("vmax", None)
norm = Normalize(vmin=vmin, vmax=vmax)
opts["norm"] = norm
return opts
Expand Down Expand Up @@ -93,14 +85,10 @@ def run_func_hash_io(
Wrapper function to run a sub function if either output is not present or input has changed. If check is False then
simply passes everything.
"""
if attrs is None:
attrs = []
if inputs is None:
inputs = []
if configs is None:
configs = []
if outputs is None:
outputs = []
attrs = [] if attrs is None else attrs
inputs = [] if inputs is None else inputs
configs = [] if configs is None else configs
outputs = [] if outputs is None else outputs

def decorator_func(processor_func):
@functools.wraps(processor_func)
Expand Down Expand Up @@ -164,7 +152,6 @@ def wrapper_func(ref, *args, **kwargs):
fn_hash = os.path.join(path_out, f"{os.path.basename(getattr(ref, i))}.hash")
# get hash
hash256 = cli_utils.get_file_hash(getattr(ref, i))
# print(hash256.hexdigest())
with open(fn_hash, "w") as f:
f.write(hash256.hexdigest())
else:
Expand Down Expand Up @@ -382,8 +369,7 @@ def frames(self, **kwargs):
def velocimetry(self, method="get_piv", write=False, **kwargs):
if len(kwargs) > 1:
raise OverflowError(f"Too many arguments under velocimetry, only one allowed, but {len(kwargs)} given.")
if len(kwargs) == 0:
kwargs[method] = {}
kwargs[method] = kwargs.get(method, {}) if len(kwargs) == 0 else kwargs[method]
# get velocimetry results
self.velocimetry_obj = apply_methods(self.da_frames, "frames", logger=self.logger, **kwargs)
m = list(kwargs.keys())[0]
Expand Down
117 changes: 60 additions & 57 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,76 @@
import json
from matplotlib import backend_bases
import matplotlib.pyplot as plt
import os.path

import matplotlib.pyplot as plt
import pytest
import warnings
from click.testing import CliRunner
from matplotlib import backend_bases

import pyorc.service
from pyorc.cli.main import cli
from pyorc.cli.cli_elements import GcpSelect, AoiSelect, StabilizeSelect
from pyorc.cli import cli_utils
from pyorc.cli.cli_elements import AoiSelect, GcpSelect, StabilizeSelect
from pyorc.cli.main import cli
from pyorc.helpers import xyz_transform


def test_cli_info(cli_obj):
result = cli_obj.invoke(cli, ["--info"], echo=True)
assert result.exit_code == 0


def test_cli_license(cli_obj):
result = cli_obj.invoke(cli, ["--license"], echo=True)
assert result.exit_code == 0


def test_cli_cam_config(cli_obj):
result = cli_obj.invoke(
cli, [
'camera-config',
'--help'
],
echo=True
)
result = cli_obj.invoke(cli, ["camera-config", "--help"], echo=True)
assert result.exit_code == 0


def test_cli_cam_config_video(cli_obj, vid_file, gcps_src, gcps_dst, lens_position, corners, cli_cam_config_output):
result = cli_obj.invoke(
cli, [
'camera-config',
'-V',
cli,
[
"camera-config",
"-V",
vid_file,
# '--src',
# json.dumps(gcps_src),
'--dst',
"--dst",
json.dumps(gcps_dst),
'--crs',
'32735',
'--z_0',
'1182.2',
'--h_ref',
'0.0',
'--lens_position',
"--crs",
"32735",
"--z_0",
"1182.2",
"--h_ref",
"0.0",
"--lens_position",
json.dumps(lens_position),
'--resolution',
'0.03',
'--window_size',
'25',
'--corners',
"--resolution",
"0.03",
"--window_size",
"25",
"--corners",
json.dumps(corners),
'-vvv',
"-vvv",
cli_cam_config_output,

],
echo=True
echo=True,
)
# assert result.exit_code == 0
assert result.exit_code == 0


def test_cli_velocimetry(cli_obj, vid_file, cam_config_fn, cli_recipe_fn, cli_output_dir):
# ensure we are in the right folder
print(f"current file is: {os.path.dirname(__file__)}")
os.chdir(os.path.dirname(__file__))
result = cli_obj.invoke(
cli, [
'velocimetry',
'-V',
vid_file,
'-c',
cam_config_fn,
'-r',
cli_recipe_fn,
'-v',
cli_output_dir,
'-u'
],
echo=True
cli,
["velocimetry", "-V", vid_file, "-c", cam_config_fn, "-r", cli_recipe_fn, "-v", cli_output_dir, "-u"],
echo=True,
)
import time

time.sleep(1)
assert result.exit_code == 0

Expand All @@ -85,24 +80,21 @@ def test_cli_velocimetry(cli_obj, vid_file, cam_config_fn, cli_recipe_fn, cli_ou
[
"recipe",
"recipe_geojson",
]
],
)
def test_service_video(recipe_, vid_file, cam_config, cli_prefix, cli_output_dir, request):
recipe_ = request.getfixturevalue(recipe_)
# ensure we are in the right folder
print(f"current file is: {os.path.dirname(__file__)}")
os.chdir(os.path.dirname(__file__))
pyorc.service.velocity_flow(
recipe=recipe_,
videofile=vid_file,
cameraconfig=cam_config.to_dict(),
prefix=cli_prefix,
output=cli_output_dir
recipe=recipe_, videofile=vid_file, cameraconfig=cam_config.to_dict(), prefix=cli_prefix, output=cli_output_dir
)


def test_gcps_interact(gcps_dst, frame_rgb):
import matplotlib.pyplot as plt

# convert dst to
crs = 32735
# crs = None
Expand All @@ -115,7 +107,10 @@ def test_gcps_interact(gcps_dst, frame_rgb):
# plt.show(block=True)
# make a click event for testing callbacks
event = backend_bases.MouseEvent(
name="click", canvas=selector.fig.canvas, x=5, y=5,
name="click",
canvas=selector.fig.canvas,
x=5,
y=5,
)
selector.on_press(event)
selector.on_move(event)
Expand All @@ -125,6 +120,7 @@ def test_gcps_interact(gcps_dst, frame_rgb):
selector.on_release(event)
plt.close("all")


def test_aoi_interact(frame_rgb, cam_config_without_aoi):
# convert dst to
# del cam_config_without_aoi.crs
Expand All @@ -138,7 +134,10 @@ def test_aoi_interact(frame_rgb, cam_config_without_aoi):
# plt.show(block=True)
# make a click event for testing callbacks
event = backend_bases.MouseEvent(
name="click", canvas=selector.fig.canvas, x=5, y=5,
name="click",
canvas=selector.fig.canvas,
x=5,
y=5,
)
selector.on_press(event)
selector.on_move(event)
Expand All @@ -152,7 +151,10 @@ def test_aoi_interact(frame_rgb, cam_config_without_aoi):
def test_stabilize_interact(frame_rgb):
selector = StabilizeSelect(frame_rgb)
event = backend_bases.MouseEvent(
name="click", canvas=selector.fig.canvas, x=5, y=5,
name="click",
canvas=selector.fig.canvas,
x=5,
y=5,
)

selector.on_press(event)
Expand All @@ -167,7 +169,8 @@ def test_stabilize_interact(frame_rgb):
# plt.show(block=True)
plt.close("all")


def test_read_shape(gcps_fn):
coords, wkt = cli_utils.read_shape(gcps_fn)
assert(isinstance(wkt, str))
assert(isinstance(coords, list))
assert isinstance(wkt, str)
assert isinstance(coords, list)

0 comments on commit 2bcc3b2

Please sign in to comment.