import os
import json
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser


def main(parse_args):

    merged_splits = {}

    for splits_path in parse_args.splits_paths:
        with open(splits_path, 'r') as f:
            splits = json.load(f)
        for split_key in splits:
            if not split_key in merged_splits:
                merged_splits[split_key] = splits[split_key]
            else:
                merged_splits.append(splits[split_key])
    
    merged_splits_path = os.path.join(parse_args.splits_dir, parse_args.merged_splits_filename)
    with open(merged_splits_path, 'w') as f:
        json.dump(merged_splits, f)
    print(f'Finished merging. Saving splits to new splits path {merged_splits_path}')

if __name__ == "__main__":
    
    parser = ArgumentParser()

    parser.add_argument(
        '--splits_dir', type=str, help='path to splits directory',
        required=True
    )   
    parser.add_argument(
        '--splits_paths', nargs='+', help='paths all the splits to be merged for training',
        required=True
    )
    parser.add_argument(
        '--merged_splits_filename', type=str, help='filename of output merged split',
        required=True
    )

    parse_args = parser.parse_args()
    assert len(parse_args.splits_paths) > 1, 'must provide more than one splits'

    for splits_p in parse_args.splits_paths:
        assert os.path.exists(splits_p), f'splits file path doesn"t exist: {splits_p}'

    main(parse_args)
import os
import json
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser


def main(parse_args):

    merged_splits = {}

    for splits_path in parse_args.splits_paths:
        with open(splits_path, 'r') as f:
            splits = json.load(f)
        for split_key in splits:
            if not split_key in merged_splits:
                merged_splits[split_key] = splits[split_key]
            else:
                merged_splits.append(splits[split_key])
    
    merged_splits_path = os.path.join(parse_args.splits_dir, parse_args.merged_splits_filename)
    with open(merged_splits_path, 'w') as f:
        json.dump(merged_splits, f)
    print(f'Finished merging. Saving splits to new splits path {merged_splits_path}')

if __name__ == "__main__":
    
    parser = ArgumentParser()

    parser.add_argument(
        '--splits_dir', type=str, help='path to splits directory',
        required=True
    )   
    parser.add_argument(
        '--splits_paths', nargs='+', help='paths all the splits to be merged for training',
        required=True
    )
    parser.add_argument(
        '--merged_splits_filename', type=str, help='filename of output merged split',
        required=True
    )

    parse_args = parser.parse_args()
    assert len(parse_args.splits_paths) > 1, 'must provide more than one splits'

    for splits_p in parse_args.splits_paths:
        assert os.path.exists(splits_p), f'splits file path doesn"t exist: {splits_p}'

    main(parse_args)