From f41f3d56cefa492d36805c3c26a5925e6d72b163 Mon Sep 17 00:00:00 2001 From: maiyishan Date: Sat, 3 Jun 2023 16:26:03 +0800 Subject: [PATCH] delta points on contrast axis WIP --- dabest/_classes.py | 2 ++ dabest/plotter.py | 66 ++++++++++++++++++++++++++++++++++---- nbs/API/class.ipynb | 2 ++ nbs/API/plotter.ipynb | 74 +++++++++++++++++++++++++++++++++++++++---- 4 files changed, 132 insertions(+), 12 deletions(-) diff --git a/dabest/_classes.py b/dabest/_classes.py index bc711681..a3d80121 100644 --- a/dabest/_classes.py +++ b/dabest/_classes.py @@ -2450,6 +2450,8 @@ def plot(self, color_col=None, es_sf = 2, es_fontsize = 10, + contrast_show_deltas = True, + gridkey_rows=None, gridkey_merge_pairs = False, gridkey_show_Ns = True, diff --git a/dabest/plotter.py b/dabest/plotter.py index 81d35c48..8b269002 100644 --- a/dabest/plotter.py +++ b/dabest/plotter.py @@ -179,7 +179,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs): plot_kwargs["legend_kwargs"]) -################################################### GRIDKEY WIP - extracting kwargs +################################################### GRIDKEY WIP - extracting arguments gridkey_rows = plot_kwargs["gridkey_rows"] gridkey_merge_pairs = plot_kwargs["gridkey_merge_pairs"] @@ -190,7 +190,7 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs): gridkey_show_Ns = False gridkey_show_es = False -################################################### END GRIDKEY WIP - extracting kwargs +################################################### END GRIDKEY WIP - extracting arguments # Group summaries kwargs. gs_default = {'mean_sd', 'median_quartiles', None} @@ -322,8 +322,6 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs): h_space_cummings = 0.1 else: h_space_cummings = 0.3 - - ##### TESTING SOME SHIT ###################### END GRIDKEY HSPACE ALTERATION @@ -453,9 +451,62 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs): slopegraph_kwargs['color'] = plot_palette_raw[color_key] slopegraph_kwargs['label'] = color_key - rawdata_axes.plot(x_points, y_points, **slopegraph_kwargs) + rawdata_axes.plot(x_points, y_points, **slopegraph_kwargs) + x_start = x_start + grp_count + + ##################### DELTA PTS ON CONTRAST PLOT WIP + + contrast_show_deltas = plot_kwargs["contrast_show_deltas"] + + if is_paired == None: + contrast_show_deltas = False + + if contrast_show_deltas == True: + + trans = plt.gca().transData + + delta_plot_data_temp = plot_data.copy() + delta_id_col = dabest_obj.id_col + if color_col != None: + delta_plot_data = delta_plot_data_temp[[xvar, yvar, delta_id_col, color_col]] + deltapts_args = {"hue" : color_col, + "palette" : plot_palette_raw, + "marker" : "^", + "alpha" : 0.5} + + else: + delta_plot_data = delta_plot_data_temp[[xvar, yvar, delta_id_col]] + deltapts_args = {"color" : "k", + "marker" : "^", + "alpha" : 0.5} + + final_deltas = pd.DataFrame() + for i in idx: + for j in i: + if i.index(j) != 0: + temp_df_exp = delta_plot_data[delta_plot_data[xvar].str.contains(j)].reset_index(drop=True) + if is_paired == "baseline": + temp_df_cont = delta_plot_data[delta_plot_data[xvar].str.contains(i[0])].reset_index(drop=True) + elif is_paired == "sequential": + temp_df_cont = delta_plot_data[delta_plot_data[xvar].str.contains(i[i.index(j) - 1])].reset_index(drop=True) + delta_df = temp_df_exp.copy() + delta_df[yvar] = temp_df_exp[yvar] - temp_df_cont[yvar] + final_deltas = pd.concat([final_deltas, delta_df]) + + + # Plot the raw data as a swarmplot. + deltapts_plot = sns.swarmplot(data=final_deltas, x=xvar, y=yvar, + ax=contrast_axes, + order=all_plot_groups, + zorder=2, + **deltapts_args) + contrast_axes.legend().set_visible(False) + + ##################### DELTA PTS ON CONTRAST PLOT END + + # Set the tick labels, because the slopegraph plotting doesn't. rawdata_axes.set_xticks(np.arange(0, len(temp_all_plot_groups))) rawdata_axes.set_xticklabels(temp_all_plot_groups) @@ -1293,7 +1344,10 @@ def EffectSizeDataFramePlotter(EffectSizeDataFrame, **plot_kwargs): contrast_axes.get_xaxis().set_visible(False) ####################################################### END GRIDKEY MAIN CODE WIP - + + + + # Make sure no stray ticks appear! rawdata_axes.xaxis.set_ticks_position('bottom') rawdata_axes.yaxis.set_ticks_position('left') diff --git a/nbs/API/class.ipynb b/nbs/API/class.ipynb index 7b2264a7..3dd1f25f 100644 --- a/nbs/API/class.ipynb +++ b/nbs/API/class.ipynb @@ -3262,6 +3262,8 @@ " es_sf = 2,\n", " es_fontsize = 10,\n", " \n", + " contrast_show_deltas = True,\n", + " \n", " gridkey_rows=None,\n", " gridkey_merge_pairs = False,\n", " gridkey_show_Ns = True,\n", diff --git a/nbs/API/plotter.ipynb b/nbs/API/plotter.ipynb index bec71d6b..63b2f9b7 100644 --- a/nbs/API/plotter.ipynb +++ b/nbs/API/plotter.ipynb @@ -229,7 +229,7 @@ " plot_kwargs[\"legend_kwargs\"])\n", " \n", " \n", - "################################################### GRIDKEY WIP - extracting kwargs \n", + "################################################### GRIDKEY WIP - extracting arguments \n", " \n", " gridkey_rows = plot_kwargs[\"gridkey_rows\"]\n", " gridkey_merge_pairs = plot_kwargs[\"gridkey_merge_pairs\"]\n", @@ -240,7 +240,7 @@ " gridkey_show_Ns = False\n", " gridkey_show_es = False\n", " \n", - "################################################### END GRIDKEY WIP - extracting kwargs\n", + "################################################### END GRIDKEY WIP - extracting arguments\n", "\n", " # Group summaries kwargs.\n", " gs_default = {'mean_sd', 'median_quartiles', None}\n", @@ -372,8 +372,6 @@ " h_space_cummings = 0.1\n", " else:\n", " h_space_cummings = 0.3\n", - " \n", - " ##### TESTING SOME SHIT\n", " \n", " \n", "###################### END GRIDKEY HSPACE ALTERATION \n", @@ -503,9 +501,62 @@ " slopegraph_kwargs['color'] = plot_palette_raw[color_key]\n", " slopegraph_kwargs['label'] = color_key\n", "\n", - " rawdata_axes.plot(x_points, y_points, **slopegraph_kwargs)\n", + " rawdata_axes.plot(x_points, y_points, **slopegraph_kwargs) \n", + "\n", " \n", " x_start = x_start + grp_count\n", + " \n", + " ##################### DELTA PTS ON CONTRAST PLOT WIP \n", + "\n", + " contrast_show_deltas = plot_kwargs[\"contrast_show_deltas\"]\n", + " \n", + " if is_paired == None:\n", + " contrast_show_deltas = False\n", + " \n", + " if contrast_show_deltas == True:\n", + " \n", + " trans = plt.gca().transData\n", + " \n", + " delta_plot_data_temp = plot_data.copy()\n", + " delta_id_col = dabest_obj.id_col\n", + " if color_col != None:\n", + " delta_plot_data = delta_plot_data_temp[[xvar, yvar, delta_id_col, color_col]]\n", + " deltapts_args = {\"hue\" : color_col, \n", + " \"palette\" : plot_palette_raw,\n", + " \"marker\" : \"^\",\n", + " \"alpha\" : 0.5}\n", + " \n", + " else:\n", + " delta_plot_data = delta_plot_data_temp[[xvar, yvar, delta_id_col]]\n", + " deltapts_args = {\"color\" : \"k\",\n", + " \"marker\" : \"^\",\n", + " \"alpha\" : 0.5}\n", + " \n", + " final_deltas = pd.DataFrame()\n", + " for i in idx:\n", + " for j in i:\n", + " if i.index(j) != 0:\n", + " temp_df_exp = delta_plot_data[delta_plot_data[xvar].str.contains(j)].reset_index(drop=True)\n", + " if is_paired == \"baseline\":\n", + " temp_df_cont = delta_plot_data[delta_plot_data[xvar].str.contains(i[0])].reset_index(drop=True)\n", + " elif is_paired == \"sequential\":\n", + " temp_df_cont = delta_plot_data[delta_plot_data[xvar].str.contains(i[i.index(j) - 1])].reset_index(drop=True)\n", + " delta_df = temp_df_exp.copy()\n", + " delta_df[yvar] = temp_df_exp[yvar] - temp_df_cont[yvar]\n", + " final_deltas = pd.concat([final_deltas, delta_df]) \n", + " \n", + " \n", + " # Plot the raw data as a swarmplot.\n", + " deltapts_plot = sns.swarmplot(data=final_deltas, x=xvar, y=yvar,\n", + " ax=contrast_axes,\n", + " order=all_plot_groups, \n", + " zorder=2,\n", + " **deltapts_args)\n", + " contrast_axes.legend().set_visible(False)\n", + " \n", + " ##################### DELTA PTS ON CONTRAST PLOT END\n", + "\n", + " \n", " # Set the tick labels, because the slopegraph plotting doesn't.\n", " rawdata_axes.set_xticks(np.arange(0, len(temp_all_plot_groups)))\n", " rawdata_axes.set_xticklabels(temp_all_plot_groups)\n", @@ -1343,7 +1394,10 @@ " contrast_axes.get_xaxis().set_visible(False)\n", " \n", " ####################################################### END GRIDKEY MAIN CODE WIP \n", - " \n", + " \n", + " \n", + " \n", + " \n", " # Make sure no stray ticks appear!\n", " rawdata_axes.xaxis.set_ticks_position('bottom')\n", " rawdata_axes.yaxis.set_ticks_position('left')\n", @@ -1358,6 +1412,14 @@ " # Return the figure.\n", " return fig" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7355251f", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {