import random


def sim_detections(gt, tpr, fpr):
    """Simulates detection data for a set of ground truth cluster labels and an
    annotator with a specified TPR and FPR.

    Returns an array of with same length as input gt, where 1 indicates the
    simulated annotator detected a cluster and 0 indicates an undetected
    cluster.

    Args:
        gt (array): Array of ground truth cluster labels. 1 indicates a true
            detection and 0 indicates a false detection.
        tpr (float): The true positive rate of the annotator. For a ground
            truth value of 1, it is the probability that the function will
            output 1, indicating that the simulated annotator detected the
            true cluster.
        fpr (float): The false positive rate of the annotator. For a ground
            truth value of 0, it is the probability that the funciton will
            output 1, indicating that the simulated annotator falsely detected
            the cluster.

    Returns:
        array: Array of detected cluster labels. A value of 1 indicates that
            a cluster was detected by the annotator, and 0 indicates that the
            cluster was not detected by the annotator.
    """

    assert tpr >= 0 and tpr <= 1, "TPR must be between 0 and 1"
    assert fpr >= 0 and fpr <= 1, "FPR must be between 0 and 1"

    det_list = []
    for item in gt:
        rand = random.random()
        if item == 1:
            if rand < tpr:
                det_list.append(1)
            else:
                det_list.append(0)
        elif item == 0:
            if rand < fpr:
                det_list.append(1)
            else:
                det_list.append(0)

    return det_list