-
Hi |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @mmmahdiii, You can merge two existing projects using the File > Merge Into Project... option. Training over multiple slp files is not supported at this time. When merging, I would recommend saving the project under a new name via Save As. Let me know if this helps! Thanks, |
Beta Was this translation helpful? Give feedback.
-
Use this "definitely not for production" code at your own discretion. Merging usually requires user-input for how to handle merging conflicts, but this code only allows the users to specify one method for handling ALL merge conflicts (without first knowing what those conflicts might be). Always save to a different filename (rather than overwriting a pre-existing project)! I've minimally tested and it seems to work. @mmmahdiii Let me know how it goes! import os
import sleap
from sleap import Skeleton, Labels
from sleap.gui.app import main as sleap_label
USE_BASE_STRING = "Use base, discard conflicting new instances"
USE_NEW_STRING = "Use new, discard conflicting base instances"
USE_NEITHER_STRING = "Discard all conflicting instances"
CLEAN_STRING = "Accept clean merge"
MERGE_METHOD = USE_BASE_STRING # TODO: change this to your preferred merge method
# TODO: change these to your own files (add more/less if you want)
ds1 = os.environ["ds-dmc"] # This is the base labels
ds2 = os.environ["ds-mouse"]
ds3 = os.environ["ds-remora"]
filenames = [ds2, ds3] # Probably easiest to directly add subsequent filenames here
out_filename = "merged.slp" # TODO: change this to your preferred output filename
base_labels = sleap.load_file(ds1)
def finishMerge(base_labels, extra_base, extra_new, merge_method):
"""Finishes merge process, possibly resolving conflicts.
This is connected to `accepted` signal.
Args:
None.
Raises:
ValueError: If no valid merge method was selected in dialog.
Returns:
None.
"""
if merge_method == USE_BASE_STRING:
Labels.finish_complex_merge(base_labels, extra_base)
elif merge_method == USE_NEW_STRING:
Labels.finish_complex_merge(base_labels, extra_new)
elif merge_method in (USE_NEITHER_STRING, CLEAN_STRING):
Labels.finish_complex_merge(base_labels, [])
else:
raise ValueError("No valid merge method selected.")
# Taken from sleap.gui.commands.MergeProject ------------------------------------------
for filename in filenames:
print(f"\nMerging {filename} into base labels...")
gui_video_callback = Labels.make_video_callback(
search_paths=[os.path.dirname(filename)]
)
new_labels: Labels = Labels.load_file(filename, video_search=gui_video_callback)
# Taken from sleap.gui.commands.MergeDialog >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
if base_labels.skeleton.node_names != new_labels.skeleton.node_names:
# Warn about mismatching skeletons
base_nodes = base_labels.skeleton.node_names
merge_nodes = new_labels.skeleton.node_names
missing_nodes = [node for node in base_nodes if node not in merge_nodes]
new_nodes = [node for node in merge_nodes if node not in base_nodes]
merged, extra_base, extra_new = Labels.complex_merge_between(
base_labels, new_labels
)
merge_total = 0
merge_frames = 0
for vid_frame_list in merged.values():
# number of frames for this video
merge_frames += len(vid_frame_list.keys())
# number of instances across frames for this video
merge_total += sum((map(len, vid_frame_list.values())))
merged_text = f"Cleanly merged {merge_total} instances"
if merge_total:
merged_text += f" across {merge_frames} frames"
merged_text += "."
print(merged_text)
if not extra_base:
conflict_text = "There are no conflicts."
else:
conflict_text = (
"WARNING: Merge conflicts inspect merged project for correctness!"
)
print(conflict_text)
if extra_base:
merge_method = MERGE_METHOD
else:
merge_method = CLEAN_STRING
print(merge_method)
finishMerge(base_labels, extra_base, extra_new, merge_method)
print(f"... finished merging {filename} into base labels.")
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# ---------------------------------------------------------------------------------
Labels.save_file(base_labels, out_filename)
sleap_label([out_filename])
print("Just a nook to latch my breakpoint onto for debugging.") |
Beta Was this translation helpful? Give feedback.
Use this "definitely not for production" code at your own discretion. Merging usually requires user-input for how to handle merging conflicts, but this code only allows the users to specify one method for handling ALL merge conflicts (without first knowing what those conflicts might be). Always save to a different filename (rather than overwriting a pre-existing project)!
I've minimally tested and it seems to work. @mmmahdiii Let me know how it goes!