Example #1
0
def test_matching_adaptor():
    from sleap.io.format import read

    read(
        "tests/data/hdf5_format_v1/centered_pair_predictions.h5",
        for_object="labels",
        as_format="*",
    )

    read(
        "tests/data/json_format_v1/centered_pair.json",
        for_object="labels",
        as_format="*",
    )
Example #2
0
def test_madlc():
    labels = read(
        "tests/data/dlc/madlc_testdata.csv",
        for_object="labels",
        as_format="deeplabcut",
    )

    assert labels.skeleton.node_names == ["A", "B", "C"]
    assert len(labels) == 3
    assert len(labels[0]) == 2
    assert len(labels[1]) == 2
    assert len(labels[2]) == 1

    assert_array_equal(labels[0][0].numpy(), [[0, 1], [2, 3], [4, 5]])
    assert_array_equal(labels[0][1].numpy(), [[6, 7], [8, 9], [10, 11]])
    assert_array_equal(labels[1][0].numpy(),
                       [[12, 13], [np.nan, np.nan], [15, 16]])
    assert_array_equal(labels[1][1].numpy(),
                       [[17, 18], [np.nan, np.nan], [20, 21]])
    assert_array_equal(labels[2][0].numpy(), [[22, 23], [24, 25], [26, 27]])
Example #3
0
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)
Example #4
0
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)