-
Notifications
You must be signed in to change notification settings - Fork 5
Constructing spatial neighborhood with custom parameters #126
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
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7d98cd2
Constructing spatial neighborhood with custom parameters
bdac0af
Separate each function into individual file
Qirongmao97 3f68c32
Delete preprocessing/neighbors/neighborhood_construction.py
Qirongmao97 3890170
Delete preprocessing/neighbors/neighborhood_construction.yml
Qirongmao97 4130adf
Update delaunay_triangulation.py
Qirongmao97 36367e9
Update coord_type.py
Qirongmao97 6e77564
Update n_neighs.py
Qirongmao97 d30d4ac
Update n_rings.py
Qirongmao97 ee5533f
Update radius.py
Qirongmao97 235a298
Update and rename n_neighs.py to n_neighbourhood.py
Qirongmao97 ea486da
Update delaunay_triangulation.py
Qirongmao97 0625fdb
Delete preprocessing/neighbors/coord_type.py
Qirongmao97 d87dcf7
Delete preprocessing/neighbors/coord_type.yml
Qirongmao97 e76411a
Update n_neighbourhood.py
Qirongmao97 f8e317b
Update delaunay_triangulation.py
Qirongmao97 db8597d
Update n_rings.py
Qirongmao97 fb659d7
Update radius.py
Qirongmao97 6c551de
Committing changes based on the comments
94528f3
Putting each function to subdirectory
97c6bd2
Removing config file of Delaunay Traingulation
dc59a5b
remove config reading
niklasmueboe 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
3 changes: 3 additions & 0 deletions
3
preprocessing/neighbors/delaunay_traingulation/config/config_1.json
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,3 @@ | ||
| { | ||
| "delaunay":false | ||
| } |
File renamed without changes.
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,3 @@ | ||
| { | ||
| "n_neighs":6 | ||
| } |
106 changes: 106 additions & 0 deletions
106
preprocessing/neighbors/n_neighbourhood/n_neighbourhood.py
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,106 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Author_and_contribution: Niklas Mueller-Boetticher; created script, | ||
| # Author_and_contribution: Qirong Mao; implemented method | ||
|
|
||
| import argparse | ||
|
|
||
| # TODO adjust description | ||
| parser = argparse.ArgumentParser( | ||
| description="Neighbor definition based on numbers of neibourhood (only for generic coordinates)" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-c", "--coordinates", help="Path to coordinates (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-m", "--matrix", help="Path to (transformed) counts (as mtx).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-f", "--features", help="Path to features (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-o", "--observations", help="Path to observations (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument("-d", "--out_dir", help="Output directory.", required=True) | ||
|
|
||
| parser.add_argument( | ||
| "--config", | ||
| help="Optional config file (json) used to pass additional parameters.", | ||
| required=False, | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Output files | ||
| from pathlib import Path | ||
|
|
||
| out_dir = Path(args.out_dir) | ||
|
|
||
| spatial_connectivities_file = out_dir / "spatial_connectivities.mtx" | ||
| ##spatial_distances_file = out_dir / "spatial_distances.mtx" | ||
|
|
||
| # Use these filepaths and inputs ... | ||
| coord_file = args.coordinates | ||
| matrix_file = args.matrix | ||
| feature_file = args.features | ||
| observation_file = args.observations | ||
|
|
||
| ## Loading n_neighs parameter from config_file | ||
|
|
||
| if args.config is not None: | ||
| config_file = args.config | ||
|
|
||
| import json | ||
|
|
||
| with open(config) as f: | ||
| parameters = json.load(f) | ||
|
|
||
| n_neighs = data["n_neighs"] | ||
|
|
||
| # ... or AnnData if you want | ||
| def get_anndata(args): | ||
| # Untested template | ||
| import anndata as ad | ||
| import pandas as pd | ||
| import scipy as sp | ||
|
|
||
| X = sp.io.mmread(args.matrix) | ||
| if sp.sparse.issparse(X): | ||
| X = X.tocsr() | ||
| observations = pd.read_table(args.observations, index_col=0) | ||
| features = pd.read_table(args.features, index_col=0) | ||
| coordinates = ( | ||
| pd.read_table(args.coordinates, index_col=0) | ||
| .loc[observations.index, :] | ||
| .to_numpy() | ||
| ) | ||
|
|
||
| adata = ad.AnnData( | ||
| X=X, obs=observations, var=features, obsm={"spatial": coordinates} | ||
| ) | ||
|
|
||
| return adata | ||
|
|
||
|
|
||
| adata = get_anndata(args) | ||
|
|
||
| ## Your code goes here | ||
| import squidpy as sq | ||
|
|
||
| sq.gr.spatial_neighbors(adata, n_neighs=n_neighs, coord_type="generic") | ||
|
|
||
| neighbors = adata.obsp["spatial_connectivities"].astype(int) | ||
| ##distance = adata.obsp["spatial_distances"].astype(float) | ||
|
|
||
| ## Write output | ||
| import scipy as sp | ||
|
|
||
| out_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| sp.io.mmwrite(spatial_connectivities_file, neighbors) | ||
| ##sp.io.mmwrite(spatial_distances_file, distance) |
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,7 @@ | ||
| channels: | ||
| - conda-forge | ||
| dependencies: | ||
| - python=3.9.18 | ||
| - pip | ||
| - pip: | ||
| - squidpy==1.3.1 |
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,3 @@ | ||
| { | ||
| "n_rings":1 | ||
| } |
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,108 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Author_and_contribution: Niklas Mueller-Boetticher; created script, | ||
| # Author_and_contribution: Qirong Mao; implemented method | ||
|
|
||
|
|
||
| import argparse | ||
|
|
||
| # TODO adjust description | ||
| parser = argparse.ArgumentParser( | ||
| description="Neighbor definition based on number of rings of neighbors (only for grid coordinates)" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-c", "--coordinates", help="Path to coordinates (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-m", "--matrix", help="Path to (transformed) counts (as mtx).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-f", "--features", help="Path to features (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-o", "--observations", help="Path to observations (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument("-d", "--out_dir", help="Output directory.", required=True) | ||
|
|
||
| parser.add_argument( | ||
| "--config", | ||
| help="Optional config file (json) used to pass additional parameters.", | ||
| required=False, | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Output files | ||
| from pathlib import Path | ||
|
|
||
| out_dir = Path(args.out_dir) | ||
|
|
||
| spatial_connectivities_file = out_dir / "spatial_connectivities.mtx" | ||
| ##spatial_distances_file = out_dir / "spatial_distances.mtx" | ||
|
|
||
| # Use these filepaths and inputs ... | ||
| coord_file = args.coordinates | ||
| matrix_file = args.matrix | ||
| feature_file = args.features | ||
| observation_file = args.observations | ||
|
|
||
|
|
||
| ## Loading delaunay parameters from config_file | ||
| if args.config is not None: | ||
| config_file = args.config | ||
|
|
||
| import json | ||
|
|
||
| with open(config_file) as f: | ||
| parameters = json.load(f) | ||
|
|
||
| n_rings = parameters["n_rings"] | ||
|
|
||
|
|
||
| # ... or AnnData if you want | ||
| def get_anndata(args): | ||
| # Untested template | ||
| import anndata as ad | ||
| import pandas as pd | ||
| import scipy as sp | ||
|
|
||
| X = sp.io.mmread(args.matrix) | ||
| if sp.sparse.issparse(X): | ||
| X = X.tocsr() | ||
| observations = pd.read_table(args.observations, index_col=0) | ||
| features = pd.read_table(args.features, index_col=0) | ||
| coordinates = ( | ||
| pd.read_table(args.coordinates, index_col=0) | ||
| .loc[observations.index, :] | ||
| .to_numpy() | ||
| ) | ||
|
|
||
| adata = ad.AnnData( | ||
| X=X, obs=observations, var=features, obsm={"spatial": coordinates} | ||
| ) | ||
|
|
||
| return adata | ||
|
|
||
|
|
||
| adata = get_anndata(args) | ||
|
|
||
| ## Your code goes here | ||
| import squidpy as sq | ||
|
|
||
| sq.gr.spatial_neighbors(adata,n_rings=n_rings, coord_type="grid") | ||
|
|
||
| neighbors = adata.obsp["spatial_connectivities"].astype(int) | ||
| ##distance = adata.obsp["spatial_distances"].astype(float) | ||
|
|
||
| ## Write output | ||
| import scipy as sp | ||
|
|
||
| out_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| sp.io.mmwrite(spatial_connectivities_file, neighbors) | ||
| ##sp.io.mmwrite(spatial_distances_file, distance) |
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,7 @@ | ||
| channels: | ||
| - conda-forge | ||
| dependencies: | ||
| - python=3.9.18 | ||
| - pip | ||
| - pip: | ||
| - squidpy==1.3.1 |
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,3 @@ | ||
| { | ||
| "radius":1 | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Why delaunay: false. I think for this one we won't need a config at all. And delaunay should always be true