Exemplo n.º 1
0
def plot_sample_mean_and_sd_maps(selected_patients):
    dataDirectory = r"../Data/OnlyProstateResults/AllFields"
    patients_who_recur, patients_who_dont_recur = separate_by_recurrence(selected_patients)
    # (meanMap1, varMap, stdMap) = load_local_field_recurrence(selected_patients, dataDirectory)

    (meanMap1, varMap1, stdMap1) = make_average_field(patients_who_recur, dataDirectory)

    meanMap1.to_csv("../outputResults/recurrence_mean_map.csv", header=None, index=False)
    stdMap1.to_csv("../outputResults/recurrence_std_map.csv", header=None, index=False)

    plot_heat_map(meanMap1, -1, 1, 'mean map - patients_who_recur')
    plot_heat_map(varMap1, 0, 1, 'variance map - patients_who_recur')
    plot_heat_map(stdMap1, 0, 1, 'standard deviation map - patients_who_recur')

    (meanMap2, varMap2, stdMap2) = make_average_field(patients_who_dont_recur, dataDirectory)
    plot_heat_map(meanMap2, -1, 1, 'mean map - patients_who_dont_recur')
    plot_heat_map(varMap2, 0, 1, 'variance map - patients_who_dont_recur')
    plot_heat_map(stdMap2, 0, 1, 'standard deviation map - patients_who_dont_recur')

    meanMap2.to_csv("../outputResults/no_recurrence_mean_map.csv", header=None, index=False)
    stdMap2.to_csv("../outputResults/no_recurrence_std_map.csv", header=None, index=False)

    plot_heat_map(meanMap1 - meanMap2, -0.3, 0.3, 'Difference in mean map')
    # Var[X-Y] = Var[X]+Var[Y]
    # Standard deviation is the square root of the variance
    plot_heat_map(np.sqrt(varMap1 + varMap2), 0, 1.5, 'Difference in std map')
    (meanMap1 - meanMap2).to_csv("../outputResults/mean_difference_map.csv", header=None, index=False)
    np.sqrt(varMap1 + varMap2).to_csv("../outputResults/std_difference_map.csv", header=None, index=False)
Exemplo n.º 2
0
def print_volume_difference_details(patient_database):
    """
    :param patient_database: is a dataframe that contains the patients global variables
    :return: prints volume difference statistics for collectiver patient, then patients with recurrence and patients /
    without
    """
    print(patient_database["volumeContourDifference"].describe())
    patients_who_recur, patients_who_dont_recur = separate_by_recurrence(patient_database)
    print(patients_who_recur["volumeContourDifference"].describe())
    print(patients_who_dont_recur["volumeContourDifference"].describe())
def plot_neat_scatter(data, save_name):

    fit = np.polyfit(data["volumeContour"], data["volumeContourAuto"], 1)
    fit_fn = np.poly1d(fit)

    # Fitting the graph
    #    fig = plt.figure()
    x = np.linspace(10, 190, 1000)  # Plot straight line
    y = x
    # ax = plt.gca()
    # ax.set_facecolor('white')
    # plt.grid()

    plt.style.use('seaborn-ticks')
    plt.rcParams['font.family'] = 'times'
    plt.rcParams['axes.labelweight'] = 'bold'
    plt.rcParams['axes.labelsize'] = 12
    plt.rcParams['legend.fontsize'] = 12

    rec, n_rec = separate_by_recurrence(data)

    plt.scatter(rec["volumeContour"],
                rec["volumeContourAuto"],
                c='#ff0000',
                alpha=0.5,
                label='Recurrence')
    plt.scatter(n_rec["volumeContour"],
                n_rec["volumeContourAuto"],
                c='#00ff00',
                alpha=0.5,
                label='No Recurrence')
    # plt.scatter(censored["volumeContour"], censored["volumeContourAuto"], c='#0000ff', alpha=0.5, label='Censored')

    plt.plot(x,
             y,
             linestyle='dashed',
             color='k',
             label='Line of Equal Volumes')  # y = x line

    # plt.plot(x,x,'yo', AllPatients["volumeContour"], fit_fn(AllPatients["volumeContour"]), '--k') # linear fit
    plt.figure(1)
    plt.xlim(0, 200)
    plt.ylim(0, 200)
    plt.xlabel('Manual-contour volume [mm$^3$]')
    plt.ylabel('Auto-contour volume [mm$^3$]')
    plt.legend(loc="upper left", frameon=True, framealpha=1)
    plt.grid(True)
    # ax.set_xticks()
    # ax.set_yticks()
    # # ax.grid()
    # # ax.axis('equal')
    plt.savefig(save_name, bbox_inches='tight')
    plt.show()
def mann_whitney_test_statistic(selected_patients):
    """
    Conduct a mann whitney test SUM rank test

    :param selected_patients: dataframe of all patients
    :return: maps of the statistic and the p-values
    """

    # Tag patients with recurrence:1 and non-recurrence:0
    patients_who_recur, patients_who_dont_recur = separate_by_recurrence(
        selected_patients)
    rec_fieldMaps, _ = stack_local_fields(patients_who_recur, 1)
    nonrec_fieldMaps, _ = stack_local_fields(patients_who_dont_recur, 0)
    stat_map, p_map = mann_whitney_u_test(rec_fieldMaps, nonrec_fieldMaps)
    return stat_map, p_map
def global_statistical_analysis(selected_patients):
    '''
    A function that will carry out the statistical analysis of the global data: The dice coefficient and volume
    difference for patients with and without cancer recurrence.

    :param selected_patients: A dataframe of all patients
    '''

    # Split the patients by recurrence
    patients_who_recur, patients_who_dont_recur = separate_by_recurrence(
        selected_patients)
    print('#rec: %.f, #n_rec: %.f' %
          (patients_who_recur.shape[0], patients_who_dont_recur.shape[0]))

    # Test the relationship between volume difference and recurrence
    global_p, lower_p, upper_p = non_parametric_permutation_test(
        patients_who_recur["volumeContourDifference"],
        patients_who_dont_recur["volumeContourDifference"])
    print(
        'Vdiff: p_value(rec=/=n_rec): %.6f p_value(rec<n_rec): %.6f p_value(rec>n_rec): %.6f'
        % (global_p, lower_p, upper_p))

    # Test the relationship between dice coefficient and recurrence
    global_p, lower_p, upper_p = non_parametric_permutation_test(
        patients_who_recur["DSC"], patients_who_dont_recur["DSC"])
    print(
        'DSC: p_value(rec=/=n_rec): %.6f p_value(rec<n_rec): %.6f p_value(rec>n_rec): %.6f'
        % (global_p, lower_p, upper_p))

    # Test the relationship between ratio of volumes and recurrence: V_man/V_auto
    global_p, lower_p, upper_p = non_parametric_permutation_test(
        patients_who_recur["volumeRatio"],
        patients_who_dont_recur["volumeRatio"])
    print(
        'VRatio: p_value(rec=/=n_rec): %.6f p_value(rec<n_rec): %.6f p_value(rec>n_rec): %.6f'
        % (global_p, lower_p, upper_p))

    # Test the relationship between mean dose diff and recurrence: V_man/V_auto
    global_p, lower_p, upper_p = non_parametric_permutation_test(
        patients_who_recur["meanDoseDiff"],
        patients_who_dont_recur["meanDoseDiff"])
    print(
        'VRatio: p_value(rec=/=n_rec): %.6f p_value(rec<n_rec): %.6f p_value(rec>n_rec): %.6f'
        % (global_p, lower_p, upper_p))
def pymining_t_test(selected_patients):
    # Tag patients with recurrence:1 and non-recurrence:0
    patients_who_recur, patients_who_dont_recur = separate_by_recurrence(
        selected_patients)
    (rec_fieldMaps,
     rec_label_array) = stack_local_fields(patients_who_recur, 1)
    (non_rec_field_maps,
     non_rec_label_array) = stack_local_fields(patients_who_dont_recur, 0)

    # Concatenate the two
    totalPatients = np.concatenate((rec_fieldMaps, non_rec_field_maps),
                                   axis=-1)
    labels = np.concatenate((rec_label_array, non_rec_label_array))

    # Now use pymining to get DSC cuts global p value. It should be similar to that from scipy
    global_neg_pvalue, global_pos_pvalue, neg_tthresh, pos_tthresh = pm.permutationTest(
        totalPatients, labels, 1000)
    t_value_map = pm.imagesTTest(totalPatients,
                                 labels)  # no longer.[0] element

    return global_neg_pvalue, global_pos_pvalue, neg_tthresh, pos_tthresh, t_value_map