generated from openproblems-bio/task_template
-
Notifications
You must be signed in to change notification settings - Fork 1
Reimplement emd #48
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
Merged
Merged
Reimplement emd #48
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7fdb265
wip new emd
ghar1821 a88f01c
new script
ghar1821 72019dc
tidy up
ghar1821 c64caa1
Update CHANGELOG.md
ghar1821 443ea1c
Merge branch 'main' into reimplement_emd
ghar1821 11bdc12
Update CHANGELOG.md
ghar1821 6fbe665
rename metrics
ghar1821 51aaffe
wip
ghar1821 e645a9c
Trigger Build
ghar1821 56ca58c
Merge branch 'main' into reimplement_emd
ghar1821 ceab1e6
Update config.vsh.yaml
ghar1821 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import anndata as ad | ||
import numpy as np | ||
import pandas as pd | ||
from scipy.stats import wasserstein_distance | ||
|
||
|
||
def compute_emd( | ||
integrated_ct: ad.AnnData, | ||
validation_ct: ad.AnnData, | ||
markers_to_assess: list | ||
) -> pd.DataFrame: | ||
""" | ||
Calculate EMD metric | ||
|
||
Args: | ||
integrated_ct (ad.AnnData): batch integrated data | ||
validation_ct (ad.AnnData): validation data | ||
markers_to_assess (list): list of markers to compute EMD for | ||
|
||
Returns: | ||
pd.DataFrame: 1 row data frame where a column is a marker. Value is the EMD. | ||
""" | ||
|
||
|
||
emd_vals = {} | ||
|
||
for marker in markers_to_assess: | ||
# marker = markers_to_assess[0] | ||
|
||
mexp_integrated = np.array(integrated_ct[:, marker].layers["integrated"]).flatten() | ||
mexp_validation = np.array(validation_ct[:, marker].layers["preprocessed"]).flatten() | ||
|
||
i_values, i_weights = bin_array(mexp_integrated) | ||
v_values, v_weights = bin_array(mexp_validation) | ||
|
||
# i_values (and v_values) are the explicit support (set of all possible bin values) | ||
# of the probability distribution i_weights (and v_weights). | ||
emd = wasserstein_distance(i_values, v_values, i_weights, v_weights) | ||
emd_vals[marker] = [emd] | ||
|
||
emd_df = pd.DataFrame.from_dict(emd_vals) | ||
|
||
return emd_df | ||
|
||
|
||
def bin_array(values): | ||
""" | ||
Bin values into probability distribution. | ||
|
||
Args: | ||
values (list): values to bin | ||
|
||
Returns: | ||
list: Bin indices - centre of the bin. | ||
list: Probability distribution of the input values. | ||
""" | ||
|
||
# 2000 bins, the 0.0000001 is to avoid the left edge being included in the bin | ||
# (Mainly impacting 0 values) | ||
# range is set to -100 to 100 with the assumption that the range of values for each marker | ||
# will not exceed this | ||
bin_edges = np.arange(-100, 100.1, 0.1)+0.0000001 | ||
|
||
# using histogram retains the physical meaning of distances between bins, | ||
# such that moving mass from bin [-5.0, -4.9) to [-4.9, -4.8) has lower cost than | ||
# moving it to [99.9, 100.0) | ||
counts_per_bin, _ = np.histogram(values, bins=bin_edges) | ||
|
||
# this converts distribution of absolute marker values to probability distribution. | ||
# it allows subsequent EMD comparison between datasets of different sizes (number of cells). | ||
bin_probabilities = counts_per_bin / np.sum(counts_per_bin) | ||
|
||
# if bin_edges = [0,1,2,3,4,5] | ||
# bin_edges[:-1] will give you [0,1,2,3,4] | ||
# bin_edges[1:] will give you [1,2,3,4,5] | ||
# sum will sum each element up and divide by 2 will give you the centre of the bin | ||
# so for the 1st bin, (0+1)/2 = 0.5 | ||
bin_indices = (bin_edges[:-1] + bin_edges[1:]) / 2 | ||
|
||
# the 1st return value is the bin indices | ||
return bin_indices, bin_probabilities |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LuLeom I expanded the return values to 4. So now we have mean and max calculated across all cell types, then mean and max across donors (see EMD Mean DN). See comment on the implementation in script.py file. I'll explain there.