Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quad gating to rushd #14

Open
meson800 opened this issue Nov 17, 2023 · 0 comments
Open

Add quad gating to rushd #14

meson800 opened this issue Nov 17, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@meson800
Copy link
Member

meson800 commented Nov 17, 2023

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:
Image

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:

  1. Makes a scatter or kdeplot using the seaborn function.
  2. Does a groupby (by whatever the hue column is, if it exists).
  3. Calculates the gated percentage by the hue column.
  4. Draws axvline / axhline at 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 decorated

I assigned Emma since it looks like you use this type of gate (and can contribute to rushd!), Kasey because you've added stuff before.

@meson800 meson800 added the enhancement New feature or request label Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants