コード例 #1
0
def simulate_pcrs_with_probabilities():
    num_experiments = 100
    probabilities = [0.01 * i for i in range(1, 10)]
    num_pcrs = 5
    y = []
    naive_y = []

    for probability in probabilities:
        state_probabilities = {
            'contaminated': probability,
            'defective': probability,
            'good': 1 - probability
        }
        errors = []
        naive_errors = []

        for i in range(num_experiments):
            db = PCRDatabase()
            logic = PCRLogic(db)
            curr_aliquots = [
                Aliquot(reagent, 1, 'Biowares', choose(state_probabilities))
                for reagent in REAGENT_MAP
            ]

            for j in range(num_pcrs):
                false_neg = any(
                    map(lambda aliquot: aliquot.state == 'defective',
                        curr_aliquots))
                false_pos = any(
                    map(lambda aliquot: aliquot.state == 'contaminated',
                        curr_aliquots)) and not false_neg

                if false_neg:
                    pcr = PCR(False, False, curr_aliquots)
                elif false_pos:
                    pcr = PCR(True, True, curr_aliquots)
                else:
                    pcr = PCR(True, False, curr_aliquots)
                db.add_pcr(pcr)

                # Randomly choose new aliquot
                for i in range(len(curr_aliquots)):
                    old_aliquot = curr_aliquots[i]
                    new_aliquot = Aliquot(old_aliquot.reagent,
                                          old_aliquot.id + 1,
                                          old_aliquot.manufacturer,
                                          choose(state_probabilities))
                    aliquot_probabilities = {
                        old_aliquot: 0.8,
                        new_aliquot: 0.2
                    }
                    curr_aliquots[i] = choose(aliquot_probabilities)

            # Compute the defective and contaminated aliquot probabilities
            deductions = logic.make_probabilistic_deductions()
            all_aliquots = db.get_all_aliquots()
            errors.append(rmse(all_aliquots, deductions))

            # Compute naive approach errors
            naive_deductions = dict(deductions)
            for error in naive_deductions:
                prob_list = naive_deductions[error]
                for i in range(len(prob_list)):
                    prob_list[i] = (prob_list[i][0], 1.0)
            naive_errors.append(rmse(all_aliquots, naive_deductions))

            print[(pcr.pos_control_result, pcr.negative_control_result)
                  for pcr in db.pcrs]

        avg_error = sum(errors) / len(errors)
        avg_naive_error = sum(naive_errors) / len(naive_errors)
        y.append(avg_error)
        naive_y.append(avg_naive_error)

    print y
    print naive_y

    pyplot.plot(probabilities, y, label='Bayesian inference')
    pyplot.plot(probabilities, naive_y, label='Naive approach')
    pyplot.legend(loc=2)
    pyplot.xlabel('Probability of reagent being defective or contaminated')
    pyplot.ylabel('RMSE')
    pyplot.title(
        'RMSE as a function of reagent defective/contaminated probability')
    pyplot.show()
コード例 #2
0
def simulate_pcrs_with_heatmap():
    num_pcrs = 10
    state_probabilities = {
        'contaminated': 0.04,
        'defective': 0.04,
        'good': 0.92
    }

    db = PCRDatabase()
    logic = PCRLogic(db)
    curr_aliquots = [
        Aliquot(reagent, 1, 'Biowares', choose(state_probabilities))
        for reagent in REAGENT_MAP
    ]

    for j in range(num_pcrs):
        false_neg = any(
            map(lambda aliquot: aliquot.state == 'defective', curr_aliquots))
        false_pos = any(
            map(lambda aliquot: aliquot.state == 'contaminated',
                curr_aliquots)) and not false_neg

        if false_neg:
            pcr = PCR(False, False, curr_aliquots)
        elif false_pos:
            pcr = PCR(True, True, curr_aliquots)
        else:
            pcr = PCR(True, False, curr_aliquots)
        db.add_pcr(pcr)

        # Randomly choose new aliquot
        for i in range(len(curr_aliquots)):
            old_aliquot = curr_aliquots[i]
            new_aliquot = Aliquot(old_aliquot.reagent, old_aliquot.id + 1,
                                  old_aliquot.manufacturer,
                                  choose(state_probabilities))
            aliquot_probabilities = {old_aliquot: 0.8, new_aliquot: 0.2}
            curr_aliquots[i] = choose(aliquot_probabilities)

    # Compute the defective and contaminated aliquot probabilities
    deductions = logic.make_probabilistic_deductions()
    all_aliquots = db.get_all_aliquots()

    print 'defective', dict(deductions['defective'])
    print 'contaminated', dict(deductions['contaminated'])

    # Create heatmap
    actual_reagent_contaminations = np.array(
        [[0. for _ in range(len(curr_aliquots))] for _ in range(num_pcrs)])
    actual_reagent_defective = np.array(
        [[0. for _ in range(len(curr_aliquots))] for _ in range(num_pcrs)])
    predicted_reagent_contaminations = np.array(
        [[0. for _ in range(len(curr_aliquots))] for _ in range(num_pcrs)])
    predicted_reagent_defective = np.array(
        [[0. for _ in range(len(curr_aliquots))] for _ in range(num_pcrs)])

    for i in range(len(db.pcrs)):
        pcr = db.pcrs[i]
        for j in range(len(pcr.aliquots)):
            aliquot = pcr.aliquots[j]
            actual_reagent_contaminations[i][j] = float(
                aliquot.state == 'contaminated')
            actual_reagent_defective[i][j] = float(
                aliquot.state == 'defective')
            predicted_reagent_defective[i][j] = dict(
                deductions['defective']).get(aliquot, 0.0)
            predicted_reagent_contaminations[i][j] = dict(
                deductions['contaminated']).get(aliquot, 0.0)

    print[(pcr.pos_control_result, pcr.negative_control_result)
          for pcr in db.pcrs]

    # Plot heatmap for actual contaminations
    row_labels = [REAGENT_MAP[aliquot.reagent] for aliquot in curr_aliquots]
    column_labels = [i + 1 for i in range(num_pcrs)]
    plot_heatmap(actual_reagent_contaminations, row_labels, column_labels,
                 matplotlib.cm.Reds, 'Actual contaminated reagents')
    plot_heatmap(predicted_reagent_contaminations, row_labels, column_labels,
                 matplotlib.cm.Reds,
                 'Bayesian probabilities for contaminated reagents')
    plot_heatmap(actual_reagent_defective, row_labels, column_labels,
                 matplotlib.cm.Blues, 'Actual defective reagents')
    plot_heatmap(predicted_reagent_defective, row_labels, column_labels,
                 matplotlib.cm.Blues,
                 'Bayesian probabilities for defective reagents')
    pyplot.show()