import json
import sys
import os

import numpy as np

match_list = json.load(open('data/AMT_test/match_list.json', 'r'))

if __name__ == '__main__':
    task = sys.argv[1]
    pred_dir = f'data/AMT_test/{task}_pred'
    pred_list = [os.path.join(pred_dir, f) for f in os.listdir(pred_dir)]
    # gt_pred = json.load(open(f'data/AMT_test/{task}_pred/condition_normalize_melspec_{task}_preds.json', 'r'))
    gt_pred = json.load(open(f'data/AMT_test/{task}_pred/target_sound_normalize_melspec_{task}_preds.json', 'r'))
    if len(sys.argv) == 3 and sys.argv[2] == 'match':
        gt_pred = [p for i, p in enumerate(gt_pred) if match_list[i]]
    elif len(sys.argv) == 3 and sys.argv[2] == 'mismatch':
        gt_pred = [p for i, p in enumerate(gt_pred) if not match_list[i]]
    gt_pred = np.array(gt_pred)
    for pred_file in pred_list:
        if 'condition' in pred_file or 'target' in pred_file:
            continue
        pred = json.load(open(pred_file, 'r'))
        if len(sys.argv) == 3 and sys.argv[2] == 'match':
            pred = [p for i, p in enumerate(pred) if match_list[i]]
        elif len(sys.argv) == 3 and sys.argv[2] == 'mismatch':
            pred = [p for i, p in enumerate(pred) if not match_list[i]]
        pred = np.array(pred)
        acc = np.sum(pred == gt_pred) / len(pred)
        print(pred_file.split('/')[-1].replace('CondAVTransformer_VNet_randshift_2s_', '').replace(f'_GH_vqgan_normalize_melspec_{task}_preds.json', '').replace(f'_GH_vqgan_no_earlystop_normalize_melspec_{task}_preds.json', '').replace(f'_normalize_melspec_{task}_preds.json', ''), f'{acc:.4f}')


import json
import sys
import os

import numpy as np

match_list = json.load(open('data/AMT_test/match_list.json', 'r'))

if __name__ == '__main__':
    task = sys.argv[1]
    pred_dir = f'data/AMT_test/{task}_pred'
    pred_list = [os.path.join(pred_dir, f) for f in os.listdir(pred_dir)]
    # gt_pred = json.load(open(f'data/AMT_test/{task}_pred/condition_normalize_melspec_{task}_preds.json', 'r'))
    gt_pred = json.load(open(f'data/AMT_test/{task}_pred/target_sound_normalize_melspec_{task}_preds.json', 'r'))
    if len(sys.argv) == 3 and sys.argv[2] == 'match':
        gt_pred = [p for i, p in enumerate(gt_pred) if match_list[i]]
    elif len(sys.argv) == 3 and sys.argv[2] == 'mismatch':
        gt_pred = [p for i, p in enumerate(gt_pred) if not match_list[i]]
    gt_pred = np.array(gt_pred)
    for pred_file in pred_list:
        if 'condition' in pred_file or 'target' in pred_file:
            continue
        pred = json.load(open(pred_file, 'r'))
        if len(sys.argv) == 3 and sys.argv[2] == 'match':
            pred = [p for i, p in enumerate(pred) if match_list[i]]
        elif len(sys.argv) == 3 and sys.argv[2] == 'mismatch':
            pred = [p for i, p in enumerate(pred) if not match_list[i]]
        pred = np.array(pred)
        acc = np.sum(pred == gt_pred) / len(pred)
        print(pred_file.split('/')[-1].replace('CondAVTransformer_VNet_randshift_2s_', '').replace(f'_GH_vqgan_normalize_melspec_{task}_preds.json', '').replace(f'_GH_vqgan_no_earlystop_normalize_melspec_{task}_preds.json', '').replace(f'_normalize_melspec_{task}_preds.json', ''), f'{acc:.4f}')


# gt_pred = json.load(open(f'data/AMT_test/{task}_pred/condition_normalize_melspec_{task}_preds.json', 'r'))