def test_analysis_hdf5(tmpdir, centered_pair_predictions): from sleap.info.write_tracking_h5 import main as write_analysis filename = os.path.join(tmpdir, "analysis.h5") video = centered_pair_predictions.videos[0] write_analysis(centered_pair_predictions, output_path=filename, all_frames=True) labels = read( filename, for_object="labels", as_format="analysis", video=video, ) assert len(labels) == len(centered_pair_predictions) assert len(labels.tracks) == len(centered_pair_predictions.tracks) assert len(labels.all_instances) == len( centered_pair_predictions.all_instances)
def write(cls, filename: str, source_object: Labels): from sleap.info.write_tracking_h5 import main as write_analysis write_analysis(source_object, output_path=filename, all_frames=True)
def main(): parser = argparse.ArgumentParser() parser.add_argument("input_path", help="Path to input file.") parser.add_argument( "-o", "--output", default="", help="Path to output file (optional)." ) parser.add_argument( "--format", default="slp", help="Output format. Default ('slp') is SLEAP dataset; " "'analysis' results in analysis.h5 file; " "'h5' or 'json' results in SLEAP dataset " "with specified file format.", ) parser.add_argument( "--video", default="", help="Path to video (if needed for conversion)." ) args = parser.parse_args() video_callback = Labels.make_video_callback([os.path.dirname(args.input_path)]) try: labels = Labels.load_file(args.input_path, video_search=video_callback) except TypeError: print("Input file isn't SLEAP dataset so attempting other importers...") from sleap.io.format import read video_path = args.video if args.video else None labels = read( args.input_path, for_object="labels", as_format="*", video_search=video_callback, video=video_path, ) if args.format == "analysis": from sleap.info.write_tracking_h5 import main as write_analysis if args.output: output_path = args.output else: output_path = args.input_path output_path = re.sub("(\.json(\.zip)?|\.h5|\.slp)$", "", output_path) output_path = output_path + ".analysis.h5" write_analysis(labels, output_path=output_path, all_frames=True) elif args.output: print(f"Output SLEAP dataset: {args.output}") Labels.save_file(labels, args.output) elif args.format in ("slp", "h5", "json"): output_path = f"{args.input_path}.{args.format}" print(f"Output SLEAP dataset: {output_path}") Labels.save_file(labels, output_path) else: print("You didn't specify how to convert the file.") print(args)