def _first_and_last(title, y_label, key, indicator, ticks=None, with_wilcoxon=False, patients=None): firsts, lasts, common, p = first_and_last_data(key, indicator, patients) fig, ax = plt.subplots() ax.boxplot([firsts, lasts], labels=['First Training', 'Last Training']) ax.set_title(title) ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) ax.set_axisbelow(True) ax.set_xlabel('Session') ax.set_ylabel(y_label) if ticks is not None: ax.set_yticks(ticks) if with_wilcoxon: wx = mpatches.Patch(color='red', label="Wilcoxon p-value={}".format(p)) plt.legend(handles=[wx]) plt.show()
def plot_patient_donning_all_s_first_last_10_14_log_regression(): title = 'Patient Donning' key = 'Donning' indicator = 'total' sessions = [10, 14] firsts, lasts, common, p = first_and_last_data(key, indicator) patient_donnings = [p.data[key] for p in PATIENTS] session_dict = {} for s in sessions: session_dict[s] = [] for s in session_dict: for donnings in patient_donnings: session_dict[s].extend( [d['total'] for d in donnings if d['session'] == s]) data = [firsts, lasts, session_dict[10], session_dict[14]] fig, ax = plt.subplots() x = [2, 6, 10, 14] ax.set_xticks(x) ax.boxplot(data, positions=x, labels=['First Training', 'Last Training', '10', '14']) ax.set_title(title) ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) ax.set_axisbelow(True) ax.set_xlabel('Session') ax.set_ylabel('Time (minutes)') y_ticks = np.arange(0, 65, 5) ax.set_yticks(y_ticks[1:]) # logarithmic regression y = [np.median(t) for t in data] def func(t, a, b): return a + b * np.log(t) import scipy.optimize popt, pcov = scipy.optimize.curve_fit(func, x, y) log_a = popt[0] log_a_round = round(log_a, 3) log_b = popt[1] log_b_round = round(log_b, 3) trialX = np.linspace(x[0], x[-1], 20) ylog = func(trialX, *popt) # r2 # https://stackoverflow.com/questions/19189362/getting-the-r-squared-value-using-curve-fit residuals = y - func(x, *popt) ss_res = np.sum(residuals**2) ss_tot = np.sum((y - np.mean(y))**2) log_r_squared = 1 - (ss_res / ss_tot) log_r_squared_round = round(log_r_squared, 3) plt.plot(trialX, ylog, 'r-', ls='--') # legend wilcoxson_text = "Wilcoxon p-value={}".format(p) # wx = mpatches.Patch(color='blue', label=wilcoxson_text) log = mpatches.Patch(color='red', label="y=a+bln(x)\na={}, b={}\n$r^2={}$".format( log_a_round, log_b_round, log_r_squared_round)) plt.legend(handles=[log]) ax.annotate(wilcoxson_text, xy=(0.2, 0.75), xytext=(0.2, 0.85), xycoords='axes fraction', fontsize=10, ha='center', va='bottom', bbox=dict(boxstyle='square', fc='white'), arrowprops=dict(arrowstyle='-[, widthB=5.5, lengthB=1', lw=1.0)) plt.show()
def plot_patient_and_ot_donning_all_s_1_5_10_14_averaged(): title = 'Donning - OTs and Carers' p_key = 'Donning' p_indicator = 'total' sessions = [10, 14] p_firsts, p_lasts, _p_common, p_p = first_and_last_data(p_key, p_indicator) ot_key = 'donning' ot_indicator = 'minutes' ot_firsts, ot_lasts, _ot_common, _ot_p = first_and_last_data( ot_key, ot_indicator, OTS) patient_donnings = [p.data[p_key] for p in PATIENTS] session_dict = {} for s in sessions: session_dict[s] = [] for s in session_dict: for donnings in patient_donnings: session_dict[s].extend( [d['total'] for d in donnings if d['session'] == s]) data = [[ot_firsts, p_firsts], [ot_lasts, p_lasts], [session_dict[10]], [session_dict[14]]] print(data) fig, ax = plt.subplots() ot_color = 'lightgreen' patient_color = 'lightblue' x_ticks = [] patient_x_ticks = [] patient_x_values = [1, 4.43, 10, 14] position = 0 for pair in data: if len(pair) == 2: bp = ax.boxplot(pair, positions=[position + 1, position + 2], widths=0.6, patch_artist=True) bp['boxes'][0].set_facecolor(ot_color) bp['boxes'][1].set_facecolor(patient_color) patient_x_ticks.append(position + 2) x_ticks.append(position + 1.5) else: bp = ax.boxplot(pair, positions=[position + 1], widths=0.6, patch_artist=True) bp['boxes'][0].set_facecolor(patient_color) patient_x_ticks.append(position + 1) x_ticks.append(position + 1) position = position + 3 ax.set_ylabel('Donning Time (minutes)') ax.set_title(title) ax.set_xticks(x_ticks) ax.set_xticklabels(('First Training', 'Last Training', '10', '14')) ot_label = mpatches.Patch(color=ot_color, label="OT") patient_label = mpatches.Patch(color=patient_color, label="Carers") #ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) ax.set_axisbelow(True) # logarithmic regression x = patient_x_ticks patient_data = [p_firsts, p_lasts, session_dict[10], session_dict[14]] y = [np.median(t) for t in patient_data] def func(t, a, b): return a + b * np.log(t) import scipy.optimize popt, pcov = scipy.optimize.curve_fit(func, patient_x_values, y) log_a = popt[0] log_a_round = round(log_a, 3) log_b = popt[1] log_b_round = round(log_b, 3) trialX = np.linspace(x[0], x[-1], 20) ylog = func(trialX, *popt) # r2 # https://stackoverflow.com/questions/19189362/getting-the-r-squared-value-using-curve-fit residuals = y - func(x, *popt) ss_res = np.sum(residuals**2) ss_tot = np.sum((y - np.mean(y))**2) log_r_squared = 1 - (ss_res / ss_tot) log_r_squared_round = round(log_r_squared, 3) #plt.plot(trialX, ylog, 'y-', ls='--') # legend wilcoxson_text = "p-value={}".format(p_p) log = mpatches.Patch(linestyle='--', facecolor='yellow', label="y=a+bln(x)\na={}, b={}\n$r^2={}$".format( log_a_round, log_b_round, log_r_squared_round)) plt.legend(handles=[log, ot_label, patient_label]) ax.annotate(wilcoxson_text, xy=(0.3, 0.75), xytext=(0.3, 0.90), xycoords='axes fraction', fontsize=10, ha='center', va='bottom', bbox=dict(boxstyle='square', fc='lightblue'), arrowprops=dict(arrowstyle='-[, widthB=5.5, lengthB=1', lw=1.0)) plt.show()
def plot_carer_combined(): fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(WIDTH_17_CENTIMERES_IN_INCHES, HEIGHT_9_CENTIMERES_IN_INCHES)) # ------------------------------------------------------------------ # --------------------plot_carer_workload -------------------------- # ------------------------------------------------------------------ x = [] y = [] for id in range(2, 10): c_name = "c" + str(id) carer = [c for c in CARERS if c.name == c_name][0] carer_data = carer.data['NASA_TLX'] p_name = "p" + str(id) patient = [p for p in PATIENTS if p.name == p_name][0] patient_data = patient.data['Donning'] for c_ix, c_session in enumerate(carer_data): p_session = patient_data[c_ix] donning = p_session['total'] demand = c_session['mental_demand'] if np.isnan(donning) or np.isnan(demand): continue else: y.append(donning) x.append(demand) colors = (0, 0, 0) area = np.pi * 3 ax1.scatter(x, y, s=area, c=colors, alpha=0.5) ax1.set_xlabel('Mental demand') ax1.set_ylabel('Donning') # ------------------------------------------------------------------ # -- plot_patient_and_ot_donning_all_s_1_5_10_14_averaged_no_log --- # ------------------------------------------------------------------ title = 'Donning - OTs and Carers' p_key = 'Donning' p_indicator = 'total' sessions = [10, 14] p_firsts, p_lasts, _p_common, p_p = first_and_last_data(p_key, p_indicator) ot_key = 'donning' ot_indicator = 'minutes' ot_firsts, ot_lasts, _ot_common, _ot_p = first_and_last_data( ot_key, ot_indicator, OTS) patient_donnings = [p.data[p_key] for p in PATIENTS] session_dict = {} for s in sessions: session_dict[s] = [] for s in session_dict: for donnings in patient_donnings: session_dict[s].extend( [d['total'] for d in donnings if d['session'] == s]) data = [[ot_firsts, p_firsts], [ot_lasts, p_lasts], [session_dict[10]], [session_dict[14]]] ot_color = 'lightgreen' patient_color = 'lightblue' x_ticks = [] patient_x_ticks = [] position = 0 for pair in data: if len(pair) == 2: bp = ax2.boxplot(pair, positions=[position + 1, position + 2], widths=0.6, patch_artist=True) bp['boxes'][0].set_facecolor(ot_color) bp['boxes'][1].set_facecolor(patient_color) patient_x_ticks.append(position + 2) x_ticks.append(position + 1.5) else: bp = ax2.boxplot(pair, positions=[position + 1], widths=0.6, patch_artist=True) bp['boxes'][0].set_facecolor(patient_color) patient_x_ticks.append(position + 1) x_ticks.append(position + 1) position = position + 3 ax2.set_ylabel('Donning Time (minutes)') ax2.set_title(title) ax2.set_xticks(x_ticks) ax2.set_xticklabels(('First', 'Last', '10', '14')) ot_label = mpatches.Patch(color=ot_color, label="OT") patient_label = mpatches.Patch(color=patient_color, label="Carers") ax2.set_axisbelow(True) # legend wilcoxson_text = "p-value={}".format(p_p) plt.legend(handles=[ot_label, patient_label]) ax2.annotate(wilcoxson_text, xy=(0.3, 0.75), xytext=(0.3, 0.90), xycoords='axes fraction', fontsize=10, ha='center', va='bottom', bbox=dict(boxstyle='square', fc='lightblue'), arrowprops=dict(arrowstyle='-[, widthB=2.5, lengthB=1', lw=1.0)) # ------------------------------------------------------------------ plt.show()
def plot_patient_and_ot_donning_all_s_1_5_10_14_averaged_no_log(): title = 'Donning - OTs and Carers' p_key = 'Donning' p_indicator = 'total' sessions = [10, 14] p_firsts, p_lasts, _p_common, p_p = first_and_last_data(p_key, p_indicator) ot_key = 'donning' ot_indicator = 'minutes' ot_firsts, ot_lasts, _ot_common, _ot_p = first_and_last_data( ot_key, ot_indicator, OTS) patient_donnings = [p.data[p_key] for p in PATIENTS] session_dict = {} for s in sessions: session_dict[s] = [] for s in session_dict: for donnings in patient_donnings: session_dict[s].extend( [d['total'] for d in donnings if d['session'] == s]) data = [[ot_firsts, p_firsts], [ot_lasts, p_lasts], [session_dict[10]], [session_dict[14]]] print(data) fig, ax = plt.subplots() ot_color = 'lightgreen' patient_color = 'lightblue' x_ticks = [] patient_x_ticks = [] position = 0 for pair in data: if len(pair) == 2: bp = ax.boxplot(pair, positions=[position + 1, position + 2], widths=0.6, patch_artist=True) bp['boxes'][0].set_facecolor(ot_color) bp['boxes'][1].set_facecolor(patient_color) patient_x_ticks.append(position + 2) x_ticks.append(position + 1.5) else: bp = ax.boxplot(pair, positions=[position + 1], widths=0.6, patch_artist=True) bp['boxes'][0].set_facecolor(patient_color) patient_x_ticks.append(position + 1) x_ticks.append(position + 1) position = position + 3 ax.set_ylabel('Donning Time (minutes)') ax.set_title(title) ax.set_xticks(x_ticks) ax.set_xticklabels(('First Training', 'Last Training', '10', '14')) ot_label = mpatches.Patch(color=ot_color, label="OT") patient_label = mpatches.Patch(color=patient_color, label="Carers") ax.set_axisbelow(True) # legend wilcoxson_text = "p-value={}".format(p_p) plt.legend(handles=[ot_label, patient_label]) ax.annotate(wilcoxson_text, xy=(0.3, 0.75), xytext=(0.3, 0.90), xycoords='axes fraction', fontsize=10, ha='center', va='bottom', bbox=dict(boxstyle='square', fc='lightblue'), arrowprops=dict(arrowstyle='-[, widthB=5.5, lengthB=1', lw=1.0)) plt.show()