55# %% auto 0
66__all__ = ['merge_two_dicts' , 'unpack_and_add' , 'print_greeting' , 'get_varname' , 'get_unique_categories' , 'get_params' ,
77 'get_kwargs' , 'get_color_palette' , 'initialize_fig' , 'get_plot_groups' , 'add_counts_to_ticks' ,
8- 'extract_contrast_plotting_ticks' , 'set_xaxis_ticks_and_lims' , 'show_legend' ,
9- 'Gardner_Altman_Plot_Aesthetic_Adjustments' , 'Cumming_Plot_Aesthetic_Adjustments' , 'Redraw_Spines' ,
10- 'extract_group_summaries' ]
8+ 'extract_contrast_plotting_ticks' , 'set_xaxis_ticks_and_lims' , 'show_legend' , 'gardner_altman_adjustments' ,
9+ 'draw_zeroline' , 'redraw_independent_spines' , 'redraw_dependent_spines' , 'extract_group_summaries' ]
1110
1211# %% ../nbs/API/misc_tools.ipynb 4
1312import datetime as dt
@@ -140,6 +139,9 @@ def get_params(
140139
141140 if show_delta2 and show_mini_meta : raise ValueError ("`show_delta2` and `show_mini_meta` cannot be True at the same time." )
142141
142+ # Horizontal
143+ horizontal = plot_kwargs ["horizontal" ]
144+
143145 # Disable Gardner-Altman plotting if any of the idxs comprise of more than
144146 # two groups or if it is a delta-delta plot.
145147 float_contrast = plot_kwargs ["float_contrast" ]
@@ -149,7 +151,7 @@ def get_params(
149151 if effect_size in ["cliffs_delta" ]:
150152 float_contrast = False
151153
152- if show_delta2 or show_mini_meta :
154+ if show_delta2 or show_mini_meta or horizontal :
153155 float_contrast = False
154156
155157 if not is_paired :
@@ -167,11 +169,6 @@ def get_params(
167169 if err_color is None :
168170 err_color = "black"
169171
170- # Horizontal
171- horizontal = plot_kwargs ["horizontal" ]
172- if horizontal :
173- float_contrast = False
174-
175172 # Contrast Axes kwargs
176173 halfviolin_alpha = plot_kwargs ["halfviolin_alpha" ]
177174 ci_type = plot_kwargs ["ci_type" ]
@@ -1327,7 +1324,7 @@ def show_legend(
13271324 for line in leg .get_lines ():
13281325 line .set_linewidth (3.0 )
13291326
1330- def Gardner_Altman_Plot_Aesthetic_Adjustments (
1327+ def gardner_altman_adjustments (
13311328 effect_size_type : str ,
13321329 plot_data : pd .DataFrame ,
13331330 xvar : str ,
@@ -1382,8 +1379,7 @@ def Gardner_Altman_Plot_Aesthetic_Adjustments(
13821379 _compute_hedges_correction_factor ,
13831380 )
13841381
1385- og_ylim_raw = rawdata_axes .get_ylim ()
1386- og_xlim_raw = rawdata_axes .get_xlim ()
1382+ og_xlim_raw , og_ylim_raw = rawdata_axes .get_xlim (), rawdata_axes .get_ylim ()
13871383
13881384 # Normalize ylims and despine the floating contrast axes.
13891385 # Check that the effect size is within the swarm ylims.
@@ -1530,173 +1526,106 @@ def Gardner_Altman_Plot_Aesthetic_Adjustments(
15301526 ** redraw_axes_kwargs
15311527 )
15321528
1533-
1534- def Cumming_Plot_Aesthetic_Adjustments (
1535- contrast_axes : axes .Axes ,
1536- reflines_kwargs : dict ,
1537- is_paired : bool ,
1538- show_pairs : bool ,
1539- two_col_sankey : bool ,
1540- idx : list ,
1541- ticks_to_start_twocol_sankey : list ,
1542- proportional : bool ,
1543- ticks_to_skip : list ,
1544- temp_idx : list ,
1545- rawdata_axes : axes .Axes ,
1546- redraw_axes_kwargs : dict ,
1547- ticks_to_skip_contrast : list ,
1548- show_delta2 : bool ,
1549- show_mini_meta : bool ,
1550- horizontal : bool ,
1551- skip_redraw_lines : bool
1529+ def draw_zeroline (
1530+ ax : axes .Axes ,
1531+ horizontal : bool ,
1532+ reflines_kwargs : dict
15521533 ):
1553-
1554- """
1555- Aesthetic adjustments for the Cumming plot.
1556-
1557- Parameters
1558- ----------
1559- contrast_axes : object (Axes)
1560- The contrast axes.
1561- reflines_kwargs : dict
1562- Kwargs passed to the reference lines.
1563- is_paired : bool
1564- A boolean flag to determine if the plot is for paired data.
1565- show_pairs : bool
1566- A boolean flag to determine if the plot will show the paired data.
1567- two_col_sankey : bool
1568- A boolean flag to determine if the plot will show a two-column sankey diagram.
1569- idx : list
1570- A list of tuples containing the group names.
1571- ticks_to_start_twocol_sankey : list
1572- TBC.
1573- proportional : bool
1574- A boolean flag to determine if the plot is for proportional data.
1575- ticks_to_skip : list
1576- A list of ticks to skip.
1577- temp_idx : list
1578- A list of tuples containing the group names.
1579- rawdata_axes : object (Axes)
1580- The raw data axes.
1581- redraw_axes_kwargs : dict
1582- Kwargs passed to the redraw axes.
1583- ticks_to_skip_contrast : list
1584- A list of ticks to skip.
1585- show_delta2 : bool
1586- A boolean flag to determine if the plot will have a delta-delta effect size.
1587- show_mini_meta : bool
1588- A boolean flag to determine if the plot will have a mini-meta effect size.
1589- horizontal : bool
1590- A boolean flag to determine if the plot is for horizontal plotting.
1591- skip_redraw_lines : bool
1592- A boolean flag to skip adding spines back if True (for gridkey purposes).
1593- """
1594-
15951534 # If 0 lies within the ylim of the contrast axes, draw a zero reference line.
1596- contrast_axes_lim = contrast_axes .get_xlim () if horizontal else contrast_axes .get_ylim ()
1597- method = contrast_axes .axvline if horizontal else contrast_axes .axhline
1535+ ax_lim = ax .get_xlim () if horizontal else ax .get_ylim ()
1536+ method = ax .axvline if horizontal else ax .axhline
15981537
1599- if contrast_axes_lim [0 ] < contrast_axes_lim [1 ]:
1600- contrast_lim_low , contrast_lim_high = contrast_axes_lim
1538+ if ax_lim [0 ] < ax_lim [1 ]:
1539+ contrast_lim_low , contrast_lim_high = ax_lim
16011540 else :
1602- contrast_lim_high , contrast_lim_low = contrast_axes_lim
1541+ contrast_lim_high , contrast_lim_low = ax_lim
16031542
16041543 if contrast_lim_low < 0 < contrast_lim_high :
16051544 method (0 , ** reflines_kwargs )
16061545
1607- # Add axes spine lines to link the relevant groups in the plot. (re-add as we removed spines)
1608- if not skip_redraw_lines :
1609- if horizontal :
1610- if two_col_sankey :
1611- rightend_ticks = np .array ([len (i ) - 2 for i in idx ]) + np .array (ticks_to_start_twocol_sankey )
1612- starting_ticks = ticks_to_start_twocol_sankey .copy ()
1613- else :
1614- if is_paired == "baseline" and show_pairs :
1615- if proportional and is_paired is not None :
1616- rightend_ticks = np .array ([len (i ) - 1 for i in idx ]) + np .array (ticks_to_skip )
1617- else :
1618- rightend_ticks = np .array ([len (i ) - 1 for i in temp_idx ]) + np .array (ticks_to_skip )
1619- else :
1620- rightend_ticks = np .array ([len (i ) - 1 for i in idx ]) + np .array (ticks_to_skip )
1621- starting_ticks = ticks_to_skip .copy ()
1622-
1623- for ax in [rawdata_axes ]:
1624- sns .despine (ax = ax , left = True )
1625- xlim , ylim = ax .get_xlim (), ax .get_ylim ()
1626- redraw_axes_kwargs ["x" ] = xlim [0 ]
1627- for k , start_tick in enumerate (starting_ticks ):
1628- end_tick = rightend_ticks [k ]
1629- ax .vlines (
1630- ymin = start_tick ,
1631- ymax = end_tick ,
1632- ** redraw_axes_kwargs
1633- )
1634- ax .set_xlim (xlim )
1635- ax .set_ylim (ylim )
1636- del redraw_axes_kwargs ["x" ]
1637-
1638- # Remove y ticks and labels from the contrast axes.
1639- sns .despine (ax = contrast_axes , left = True )
1640- contrast_axes .set_yticks ([])
1641- contrast_axes .set_yticklabels ([])
1642-
1643- else : # Add spine lines to link the relevant groups in the plot. (re-add as we removed spines) - Vertical plots
1644- if two_col_sankey :
1645- rightend_ticks_raw = rightend_ticks_contrast = np .array ([len (i ) - 2 for i in idx ]) + np .array (ticks_to_start_twocol_sankey )
1646- starting_ticks_raw = starting_ticks_contrast = ticks_to_start_twocol_sankey
1546+ def redraw_independent_spines (
1547+ rawdata_axes : axes .Axes ,
1548+ contrast_axes : axes .Axes ,
1549+ horizontal : bool ,
1550+ two_col_sankey : bool ,
1551+ ticks_to_start_twocol_sankey : list ,
1552+ idx : list ,
1553+ is_paired : str ,
1554+ show_pairs : bool ,
1555+ proportional : bool ,
1556+ ticks_to_skip : list ,
1557+ temp_idx : list ,
1558+ ticks_to_skip_contrast : list ,
1559+ extra_delta : bool ,
1560+ redraw_axes_kwargs : dict
1561+ ):
1562+ # Extract the ticks
1563+ if two_col_sankey :
1564+ rightend_ticks_raw = rightend_ticks_contrast = np .array ([len (i ) - 2 for i in idx ]) + np .array (ticks_to_start_twocol_sankey )
1565+ starting_ticks_raw = starting_ticks_contrast = ticks_to_start_twocol_sankey
1566+ else :
1567+ if is_paired == "baseline" and show_pairs :
1568+ if proportional and is_paired is not None :
1569+ rightend_ticks_raw = rightend_ticks_contrast = np .array ([len (i ) - 1 for i in idx ]) + np .array (ticks_to_skip )
16471570 else :
1648- if is_paired == "baseline" and show_pairs :
1649- if proportional and is_paired is not None :
1650- rightend_ticks_raw = rightend_ticks_contrast = np .array ([len (i ) - 1 for i in idx ]) + np .array (ticks_to_skip )
1651- else :
1652- rightend_ticks_raw = np .array ([len (i ) - 1 for i in temp_idx ]) + np .array (ticks_to_skip )
1653- temp_length = [(len (i ) - 1 ) * 2 - 1 for i in idx ] if proportional else [(len (i ) - 1 ) for i in idx ]
1654- rightend_ticks_contrast = np .array (temp_length ) + np .array (ticks_to_skip_contrast )
1655- starting_ticks_raw , starting_ticks_contrast = ticks_to_skip , ticks_to_skip_contrast
1656- else :
1657- rightend_ticks_raw = rightend_ticks_contrast = np .array ([len (i ) - 1 for i in idx ]) + np .array (ticks_to_skip )
1658- starting_ticks_raw = starting_ticks_contrast = ticks_to_skip
1659-
1660- for ax , starting_ticks_current , rightend_ticks_current in zip (
1661- [rawdata_axes , contrast_axes ],
1662- [starting_ticks_raw , starting_ticks_contrast ],
1663- [rightend_ticks_raw , rightend_ticks_contrast ],
1664- ):
1665- sns .despine (ax = ax , bottom = True )
1666- xlim , ylim = ax .get_xlim (), ax .get_ylim ()
1667- redraw_axes_kwargs ["y" ] = ylim [0 ]
1668- for k , start_tick in enumerate (starting_ticks_current ):
1669- end_tick = rightend_ticks_current [k ]
1670- ax .hlines (
1671- xmin = start_tick ,
1672- xmax = end_tick ,
1673- ** redraw_axes_kwargs
1674- )
1675- ax .set_xlim (xlim )
1676- ax .set_ylim (ylim )
1677- del redraw_axes_kwargs ["y" ]
1678-
1679- # Add x-spine line for delta2/mini meta.
1680- if (show_delta2 or show_mini_meta ) and not horizontal and not skip_redraw_lines :
1681- ylim = contrast_axes .get_ylim ()
1682- redraw_axes_kwargs ["y" ] = ylim [0 ]
1683- x_ticks = contrast_axes .get_xticks ()
1684- contrast_axes .hlines (xmin = x_ticks [- 2 ], xmax = x_ticks [- 1 ], ** redraw_axes_kwargs )
1685- del redraw_axes_kwargs ["y" ]
1686-
1687- # Modify the ylims of the axes to flip the plot (so it shows the plots from top to bottom)
1571+ rightend_ticks_raw = np .array ([len (i ) - 1 for i in temp_idx ]) + np .array (ticks_to_skip )
1572+ temp_length = [(len (i ) - 1 ) * 2 - 1 for i in idx ] if proportional else [(len (i ) - 1 ) for i in idx ]
1573+ rightend_ticks_contrast = np .array (temp_length ) + np .array (ticks_to_skip_contrast )
1574+ starting_ticks_raw , starting_ticks_contrast = ticks_to_skip , ticks_to_skip_contrast
1575+ else :
1576+ rightend_ticks_raw = rightend_ticks_contrast = np .array ([len (i ) - 1 for i in idx ]) + np .array (ticks_to_skip )
1577+ starting_ticks_raw = starting_ticks_contrast = ticks_to_skip
1578+
1579+ # Plot the spines
16881580 if horizontal :
1689- if not proportional or (proportional and show_pairs ):
1690- swarm_ylim , contrast_ylim = rawdata_axes .get_ylim (), contrast_axes .get_ylim ()
1691- rawdata_axes .set_ylim (swarm_ylim [1 ], swarm_ylim [0 ])
1692- contrast_axes .set_ylim (contrast_ylim [1 ], contrast_ylim [0 ])
1693- # Modify the ylim to reduce whitespace in specific plots.
1694- if show_delta2 or show_mini_meta or (proportional and show_pairs ):
1695- swarm_ylim , contrast_ylim = rawdata_axes .get_ylim (), contrast_axes .get_ylim ()
1696- rawdata_axes .set_ylim (swarm_ylim [0 ]- 0.5 , swarm_ylim [1 ])
1697- contrast_axes .set_ylim (contrast_ylim [0 ]- 0.5 , contrast_ylim [1 ])
1581+ sns .despine (ax = rawdata_axes , left = True )
1582+ xlim , ylim = rawdata_axes .get_xlim (), rawdata_axes .get_ylim ()
1583+ redraw_axes_kwargs ["x" ] = xlim [0 ]
1584+ for k , start_tick in enumerate (starting_ticks_raw ):
1585+ end_tick = rightend_ticks_raw [k ]
1586+ rawdata_axes .vlines (
1587+ ymin = start_tick ,
1588+ ymax = end_tick ,
1589+ ** redraw_axes_kwargs
1590+ )
1591+ rawdata_axes .set_xlim (xlim )
1592+ rawdata_axes .set_ylim (ylim )
1593+ del redraw_axes_kwargs ["x" ]
1594+
1595+ # Remove y ticks and labels from the contrast axes.
1596+ sns .despine (ax = contrast_axes , left = True )
1597+ contrast_axes .set_yticks ([])
1598+ contrast_axes .set_yticklabels ([])
1599+
1600+ else :
1601+ for ax , starting_ticks_current , rightend_ticks_current in zip (
1602+ [rawdata_axes , contrast_axes ],
1603+ [starting_ticks_raw , starting_ticks_contrast ],
1604+ [rightend_ticks_raw , rightend_ticks_contrast ],
1605+ ):
1606+ sns .despine (ax = ax , bottom = True )
1607+ xlim , ylim = ax .get_xlim (), ax .get_ylim ()
1608+ redraw_axes_kwargs ["y" ] = ylim [0 ]
1609+ for k , start_tick in enumerate (starting_ticks_current ):
1610+ end_tick = rightend_ticks_current [k ]
1611+ ax .hlines (
1612+ xmin = start_tick ,
1613+ xmax = end_tick ,
1614+ ** redraw_axes_kwargs
1615+ )
1616+ ax .set_xlim (xlim )
1617+ ax .set_ylim (ylim )
1618+ del redraw_axes_kwargs ["y" ]
1619+
1620+ # Add x-spine line for delta2/mini meta.
1621+ if extra_delta :
1622+ ylim = contrast_axes .get_ylim ()
1623+ redraw_axes_kwargs ["y" ] = ylim [0 ]
1624+ x_ticks = contrast_axes .get_xticks ()
1625+ contrast_axes .hlines (xmin = x_ticks [- 2 ], xmax = x_ticks [- 1 ], ** redraw_axes_kwargs )
1626+ del redraw_axes_kwargs ["y" ]
16981627
1699- def Redraw_Spines (
1628+ def redraw_dependent_spines (
17001629 rawdata_axes : axes .Axes ,
17011630 contrast_axes : axes .Axes ,
17021631 redraw_axes_kwargs : dict ,
0 commit comments