Preview:
import os
import time
import cv2
from baseline import find_baseline
from tangent_point import find_tangent_point
from lower_limb import find_lower_limb

masks_dir = "./app/angle_utils/masks"
output_file = "./app/angle_utils/function_timings.csv"
max_masks = 100


def time_function(func, *args):
    start_time = time.time()
    try:
        func(*args)
    except Exception as e:
        print(f"Error in {func.__name__}: {e}")
    end_time = time.time()
    return end_time - start_time


results = {
    "find_baseline": [],
    "find_lower_limb": [],
    "find_tangent_point": []
}

mask_files = [f for f in os.listdir(masks_dir) if f.endswith(".png")]

for i, mask_file in enumerate(mask_files[:max_masks]):
    mask_path = os.path.join(masks_dir, mask_file)
    mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)

    if mask is None:
        print(f"Unable to read mask {mask_file}")
        continue

    print(f"Processing {i+1}/{min(len(mask_files), max_masks)}: {mask_file}")

    baseline_time = time_function(find_baseline, mask)
    results["find_baseline"].append(baseline_time)

    lower_limb_time = time_function(find_lower_limb, mask, mask_file)
    results["find_lower_limb"].append(lower_limb_time)

    tangent_time = time_function(
        find_tangent_point, mask, mask, mask.shape, (0, 0), None, mask_file
    )
    results["find_tangent_point"].append(tangent_time)

with open(output_file, "w") as f:
    f.write("Function,Average Time (s),Max Time (s),Min Time (s)\n")

    for func_name, times in results.items():
        avg_time = sum(times) / len(times) if times else 0
        max_time = max(times) if times else 0
        min_time = min(times) if times else 0
        f.write(f"{func_name},{avg_time},{max_time},{min_time}\n")

print(f"Timing results saved to {output_file}")
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter