Project: s41467-024-52003-3 | Function: compute_thresholds_and_yhat_per_view (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Computes optimal thresholds for each view position (PA, LATERAL, AP) in chest X-rays to reduce underdiagnosis bias. Implements per-view calibration strategy that significantly reduces AI bias across racial groups.

**KEY TERMS:** {
  "view_groups": "chest X-ray view positions (PA, LATERAL, AP)",
  "target_sens": "target sensitivity for calibration",
  "thresholds": "per-view decision thresholds",
  "is_portable": "portable vs stationary X-ray equipment",
  "yhat": "binary predictions after thresholding"
}

**CONTEXT CODE:**                 df.loc[pd.isnull(df.Mapped_Race), 'Mapped_Race'] = 'nan'
            races = df.Mapped_Race.unique()
        else:
            races = [races]
    if not isinstance(views, list):
        if views == 'all':
            if pd.isnull(df.View).sum():
                df.loc[pd.isnull(df.View), 'View'] = 'nan'
            views = df.View.unique()
        else:
            views = [views]
    idx = df.Mapped_Race.isin(races) & df.View.isin(views)
    if is_portable is not None:
        idx = idx & (df.is_portable == is_portable)

    keep_idx = idx & df[NO_FINDINGS_TAG].isin([0, 1])
    yhat = df.loc[keep_idx, 'Pred_{}'.format(NO_FINDINGS_TAG)]
    y = df.loc[keep_idx, NO_FINDINGS_TAG]

    auc = roc_auc_score(y.values, yhat.values)

    spec = 100 * np.mean(yhat[y == 1] >= threshold)
    sens = 100 * np.mean(yhat[y == 0] < threshold)

    return auc, sens, spec


def compute_threshold(df, target_sens='balanced'):

    idx = df.View.isin(ALL_VIEWS)

    if target_sens == 'balanced':
        keep_idx = idx & (df[NO_FINDINGS_TAG].isin([0,1]))
        yhat = df.loc[keep_idx, f'Pred_{NO_FINDINGS_TAG}'].values
        y = df.loc[keep_idx, NO_FINDINGS_TAG].values
        threshold = compute_thresh_eq_sens_spec(yhat, y)
    else:
        keep_idx = idx & (df[NO_FINDINGS_TAG] == 0)
        yhat = df.loc[keep_idx, f'Pred_{NO_FINDINGS_TAG}'].values
        threshold = np.percentile(yhat, target_sens)

    return threshold


def compute_thresh_eq_sens_spec(scores, y):
    fpr, sens, threshs = roc_curve(y, scores)
    spec = 1 - fpr
    return threshs[np.argmin(np.abs(spec - sens))]


def compute_thresholds_and_yhat_per_view(df, target_sens='balanced', only_thresholds=False):
    """
    Computes optimal thresholds for each view position (PA, LATERAL, AP) in chest X-rays to reduce underdiagnosis bias. 
    Implements per-view calibration strategy that significantly reduces AI bias across racial groups.
    """
    raise NotImplementedError("Function implementation has been masked")


def load_pred_df(model_name, dataset_name, split, merge_labels=True, window_width=None, resize_factor=None,
                 confounder_resampled=False, use_dicoms=False):
    tag = ''
    if confounder_resampled:
        tag += '-confounder_resampled'
    if window_width or resize_factor:
        if window_width:
            tag += f'-window{window_width}'
        if resize_factor:
            tag += f'-initresize{resize_factor}_midcrop'
    if use_dicoms:
        tag += '_dicoms'

    pred_path = os.path.join(PROJECT_DIR + 'prediction_dfs', model_name + '-best', dataset_name + '-' + split + tag + '.csv')
    pred_df = pd.read_csv(pred_path)
    if dataset_name == 'cxp':
        pred_df['orig_path'] = [p[p.find('datasets/'):] for p in pred_df.Path.values]
        study_ids = []
        for p in pred_df.Path.values:
            vals = p.split('/')
            study_ids.append(vals[-3] + '-' + vals[-2])
        pred_df['study_id'] = study_ids
    else:
        pred_df['dicom_id'] = [p.split('/')[-1][:-4] for p in pred_df.Path.values]
        pred_df['study_id'] = [p.split('/')[-2] for p in pred_df.Path.values]

    if merge_labels:
        xrv_dataset = load_dataset(dataset_name, split)

        if dataset_name == 'cxp':
            gt_df = pd.DataFrame(xrv_dataset.labels, columns=xrv_dataset.pathologies, index=xrv_dataset.csv.Path)
            merge_df = pd.merge(pred_df, gt_df, how='left', left_on='orig_path', right_index=True)
        else:
            gt_df = pd.DataFrame(xrv_dataset.labels, columns=xrv_dataset.pathologies, index=xrv_dataset.csv.dicom_id)
            merge_df = pd.merge(pred_df, gt_df, how='left', left_on='dicom_id', right_index=True)
            proc_map = xrv_dataset.csv[['PerformedProcedureStepDescription', 'dicom_id']].set_index('dicom_id')
            merge_df['PerformedProcedureStepDescription'] = merge_df.dicom_id.map(
                proc_map['PerformedProcedureStepDescription'])
            merge_df = create_mimic_isportable_column(merge_df)

        return merge_df
    else:
        return pred_df


def compute_metrics_with_view_thresholds(df, race, thresholds):
    idx = df.Mapped_Race == race

    if 'is_portable' in df.columns:
        view_groups = ['PA', ('LATERAL', 'LL'), ('AP', False), ('AP', True)]
    else:
        view_groups = ['PA', ('LATERAL', 'LL'), 'AP']

    df[f'yhat_{NO_FINDINGS_TAG}'] = np.nan
    for v in view_groups:
        if isinstance(v, tuple):
            if 'LATERAL' in v:
                view_idx = idx & (df.View.isin(v))
            else:
                view_idx = idx & (df.View == v[0]) & (df.is_portable == v[1])
        else:
            view_idx = idx & (df.View == v)

        df.loc[view_idx, f'yhat_{NO_FINDINGS_TAG}'] = df.loc[view_idx, f'Pred_{NO_FINDINGS_TAG}'] > thresholds[v]

    idx = idx & (df[NO_FINDINGS_TAG].isin([0, 1]))
    idx = idx & ~pd.isnull(df[f'yhat_{NO_FINDINGS_TAG}'])

    yhat = df.loc[idx, f'yhat_{NO_FINDINGS_TAG}']
    y = df.loc[idx, NO_FINDINGS_TAG]
    spec = 100 * np.mean(yhat[y == 1])
    sens = 100 * (1 - np.mean(yhat[y == 0]))

    return sens, spec


def run_metric_analysis(model_name, dataset_name, use_per_view_threshs=False, target_sens='balanced',
                        confounder_resampled=False, use_dicoms=False):
    pred_dfs = {}
    for split in ['val', 'test']:
        if use_dicoms and split == 'test':
            pred_dfs[split] = load_pred_df(model_name, dataset_name, split, use_dicoms=use_dicoms, window_width=1)
            idx = pd.isnull(pred_dfs[split]['Pred_No Finding'])
            print('%nan predictions', 100 * idx.mean())  # improper dicom files
            pred_dfs[split] = pred_dfs[split].loc[~idx]
        else:
            pred_dfs[split] = load_pred_df(model_name, dataset_name, split)

    if confounder_resampled:
        use_bmi = confounder_resampled == 'bmi'
        print('use_bmi', use_bmi)
        pred_dfs['test'] = confounder_resample_pred_df(pred_dfs['test'], dataset_name, use_bmi=use_bmi)


Complete the function implementation replacing "raise NotImplementedError".