recorder.start(base_folder=folder.parent,
               folder_name=folder.name,
               timestamp=False)

# %%
(
    left_line,
    center_line,
    right_line,
    left_to_track,
    center_to_track,
    right_to_track,
    control_points,
) = track.extract_track_from_image(
    points_spacing=1,
    restrict_extremities=False,
    apply_extra_spacing=False,
)

# %%
# load and clean complete bouts
_bouts = pd.read_hdf(paths.analysis_folder / "behavior" / "saved_data" /
                     f"complete_bouts.h5")
_bouts = _bouts.loc[(_bouts.start_roi == 0) & (_bouts.duration < 8)]
_bouts = _bouts.sort_values("duration")  # .iloc[:5]
print(f"Kept {len(_bouts)} bouts")

bouts = []
for i, bout in track(_bouts.iterrows()):
    bouts.append(LocomotionBout(bout, linearize_to=center_line))
    # ---------------------------------- COMPUTE --------------------------------- #
    K = 11
    points_spacing = 3

    (
        left_line,
        center_line,
        right_line,
        left_to_track,
        center_to_track,
        right_to_track,
        control_points,
    ) = extract_track_from_image(
        points_spacing=points_spacing,
        k=K,
        restrict_extremities=True,
        apply_extra_spacing=True,
    )

    min_len_trace_path, min_len_trace_to_track = fit_best_trace(control_points,
                                                                center_line,
                                                                K,
                                                                angle_cost=0,
                                                                length_cost=1)
    min_ang_trace_path, min_ang_trace_to_track = fit_best_trace(control_points,
                                                                center_line,
                                                                K,
                                                                angle_cost=1,
                                                                length_cost=0)
    mean_trace_path, mean_trace_to_track = fit_best_trace(control_points,
                                                          center_line,