-
Notifications
You must be signed in to change notification settings - Fork 0
Description
A common flow task is quad gating. I think this example image is probably tool-generated and not done in code, but it is a good example:

Unlike that image, we can also make it more useful if we can show gating per colored condition. However, making a plot with annotations like this is typically at least slightly annoying since it involves a subsequent pandas groupby. We can make a helper function that does a couple things:
- Makes a scatter or kdeplot using the seaborn function.
- Does a groupby (by whatever the
huecolumn is, if it exists). - Calculates the gated percentage by the hue column.
- Draws
axvline/axhlineat certain gate values and adds annotations in the corners. If there are multiple hues, add multicolored text so you can see e.g. the black population has 25% in this quadrant and the purple has 45%.
Implementation tips
The easiest way to do this is probably as a decorator that captures arguments. I think a clean way to implement this would be something that looks like this type of call:
rd.flow.quad_gated(sns.kdeplot)(data=df, x='mRuby2-A', y='mGL-A', x_gate=500, y_gate=2500, hue='condition', log_scale=True)How do we implement such a thing? A decorator is a function that takes a function and returns a function that does what you want. It probably looks like:
# In rd.flow
def quad_gated(sns_f):
def decorated(*args, **kwargs):
# Args is the list of positional arguments, kwargs is the keyword arguments.
# Start by pulling out the "x_gate" and "y_gate" kwargs
x_gate = kwargs['x_gate']
y_gate = kwargs['y_gate']
# strip these keywords out so the seaborn function doesn't get confused
del kwargs['x_gate']
del kwargs['y_gate']
# Extract the dataframe / x col / y col / hue col variables
df = args[0] if len(args) > 0 else kwargs['data']
x_col = kwargs['x']
y_col = kwargs['y']
hue_col = kwargs['hue'] if 'hue' in kwargs else None
# call the seaborn function
g = sns_f(*args, **kwargs)
# g is now the matplotlib Axes object
# TODO: do the groupby gating on df to get percentages
# TODO: pull out the hue mapping from the g object
# TODO: add axvline, axhline, and annotations in the corners
return decoratedI assigned Emma since it looks like you use this type of gate (and can contribute to rushd!), Kasey because you've added stuff before.