✅ Step 2. 악기별 평가 항목 정의 (확장 버전)

악기 주요 평가 요소 제외 또는 무시할 항목 설명 요약
🎤 보컬 pitch, tone, rhythm (onset) tempo_abs, tempo_ratio 절대 템포는 사람마다 달라 무의미할 수 있음
🥁 드럼 onset, tempo pitch, tone 멜로디/음색과 무관함
🎸 기타 pitch, tone, rhythm transpose 조(key)의 이동은 커버에 따라 허용 가능
🎹 베이스 pitch (저음), onset mfcc_cos, mfcc_dtw 등 tone 유사도 항목 음색이 다르고 mfcc 거리 큰 경우 많음

✅ 악기별 가중치 딕셔너리 (자동 평가용)

instrument_weights = {
    "vocals": {
        "pitch_dtw": 0.15, "transpose": 0.1, "pitch_chunk": 0.1,
        "mfcc_norm_euc": 0.15, "mfcc_dtw": 0.15,
        "onset_euc": 0.1, "onset_dtw": 0.1, "onset_match": 0.1,
        "tempo_log": 0.15
    },
    "drums": {
        "onset_euc": 0.2, "onset_dtw": 0.2, "onset_proxy": 0.2,
        "onset_match": 0.2, "tempo_log": 0.2
    },
    "guitar": {
        "pitch_dtw": 0.12, "pitch_chunk": 0.1,
        "mfcc_norm_euc": 0.12, "mfcc_dtw": 0.15,
        "onset_dtw": 0.1, "onset_match": 0.1, "tempo_log": 0.1
    },
    "bass": {
        "pitch_dtw": 0.2, "pitch_chunk": 0.15,
        "mfcc_norm_euc": 0.1,  # 일부 tone 유지
        "onset_proxy": 0.15, "onset_match": 0.15, "tempo_log": 0.15
    }
}


✅ 자동 필터링 전략 (예외 처리 포함)

def filter_scores_for_instrument(score_dict, instrument):
    """
    악기 특성에 따라 제외할 항목은 제거하고 점수 dict 정제
    """
    exclude_by_instrument = {
        "vocals": {"tempo_abs", "tempo_ratio"},
        "drums": {"pitch_dtw", "transpose", "pitch_chunk", "mfcc_cos", "mfcc_dtw", "mfcc_norm_euc"},
        "guitar": {"transpose"},
        "bass": {"mfcc_cos", "mfcc_dtw"}
    }
    exclude_keys = exclude_by_instrument.get(instrument, set())
    return {k: v for k, v in score_dict.items() if k not in exclude_keys}


✅ 자동 적용 전체 흐름

def compute_custom_score_for_instrument(score_dict, instrument):
    # 불필요한 항목 제거
    filtered = filter_scores_for_instrument(score_dict, instrument)
    # 가중치 선택
    weights = instrument_weights.get(instrument, {})
    return compute_weighted_score(filtered, weights)


✅ 예시 사용

for song, song_data in results.items():
    for instrument, data in song_data.items():
        if instrument not in instrument_weights:
            continue
        final_score = compute_custom_score_for_instrument(data['cover']['individual'], instrument)
        print(f"{song} - {instrument}: 최종 점수 {final_score}")


✅ 보너스: 시각화 시 제외 항목 회색 처리도 자동

def plot_score_with_instrument_filter(score_dict, instrument):
    keys = list(score_dict.keys())
    values = [score_dict[k] for k in keys]
    include_keys = filter_scores_for_instrument(score_dict, instrument).keys()

    plt.figure(figsize=(10, 5))
    bars = plt.bar(keys, values, color=[
        'skyblue' if k in include_keys else 'lightgray' for k in keys
    ])
    plt.title(f"{instrument.upper()} 평가 항목 시각화")
    plt.ylim(0, 100)
    for bar in bars:
        yval = bar.get_height()
        plt.text(bar.get_x() + bar.get_width() / 2, yval + 2, f"{yval:.2f}",
                 ha='center', va='bottom')
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.show()