-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upload importance scores via cggnn command (#204)
* created cg-gnn extraction script * added a quick test for cggnn extract * hotfix importance saving * Use updated pytorch image for Python 3.11 support, cggnn extract test passes. * Use python3.11 directly in test case usage of pip, now that python3.11 is specially installed. * Same fix applied to second test. * undo cggnn docker change * added pheno, removed multistudy to FME * logic hotfix * fix study-substudy references * fix test to account for phenotype columns * make phenotype dict key consistent * Update usage of extractor to omit study reference. * remove phenotypes from continuous dataframes * fix pheno neg expression match * split extract stratification, symbols in col names * a little formatting on FME * explore classes for cggnn extraction * fix FME test * Make test more diagnosable. * cggnn extract clarity refactors * update providers for new feature column names * add cg-gnn to toml checking * adjust squidpy clustering * actually handling this without a try except is better * any typo * handle malformed squidpy returns more gracefully * cggnn extract docstring * add direct importance upload script * Change dataframe to bool values to permit "all" & "all" syntax. * Fix accidentally booleanization of pixel position columns. * Make operator order precedence explicit, booleanization and negation. * Resolve race condition on import ("circular" import) of StudyAccess. --------- Co-authored-by: James Mathews <[email protected]>
- Loading branch information
1 parent
970d06d
commit dd93f8d
Showing
16 changed files
with
128 additions
and
160 deletions.
There are no files selected for viewing
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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,44 @@ | ||
"""Upload importance score output from a cg-gnn instance to the local db.""" | ||
|
||
from argparse import ArgumentParser | ||
|
||
from pandas import read_csv | ||
|
||
from spatialprofilingtoolbox.db.database_connection import DatabaseConnectionMaker | ||
from spatialprofilingtoolbox.db.importance_score_transcriber import transcribe_importance | ||
|
||
|
||
def parse_arguments(): | ||
"""Process command line arguments.""" | ||
parser = ArgumentParser( | ||
prog='spt cggnn upload-importances', | ||
description='Save cell importance scores as defined by cggnn to the database.' | ||
) | ||
parser.add_argument( | ||
'--spt_db_config_location', | ||
type=str, | ||
help='File location for SPT DB config file.', | ||
required=True | ||
) | ||
parser.add_argument( | ||
'--importances_csv_path', | ||
type=str, | ||
help='File location for the importances CSV.', | ||
required=True | ||
) | ||
parser.add_argument( | ||
'--cohort_stratifier', | ||
type=str, | ||
help='Name of the classification cohort variable the GNN was trained on.', | ||
default='', | ||
required=False | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
if __name__ == "__main__": | ||
args = parse_arguments() | ||
df = read_csv(args.importances_csv_path, index_col=0) | ||
connection = DatabaseConnectionMaker(args.spt_db_config_location).get_connection() | ||
transcribe_importance(df, connection, cohort_stratifier=args.cohort_stratifier) | ||
connection.close() |
This file contains 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 @@ | ||
"""Convenience classes for accessing various data in the database.""" | ||
|
||
from spatialprofilingtoolbox.db.accessors.cggnn import CGGNNAccess | ||
from spatialprofilingtoolbox.db.accessors.fractions_and_associations import FractionsAccess | ||
from spatialprofilingtoolbox.db.accessors.phenotypes import PhenotypesAccess | ||
from spatialprofilingtoolbox.db.accessors.study import StudyAccess | ||
from spatialprofilingtoolbox.db.accessors.umap import UMAPAccess |
This file contains 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,36 @@ | ||
"""Convenience access of cg-gnn metrics.""" | ||
|
||
from spatialprofilingtoolbox import get_feature_description | ||
from spatialprofilingtoolbox.db.accessors.study import StudyAccess | ||
from spatialprofilingtoolbox.db.database_connection import SimpleReadOnlyProvider | ||
from spatialprofilingtoolbox.db.exchange_data_formats.metrics import CGGNNImportanceRank | ||
|
||
|
||
class CGGNNAccess(SimpleReadOnlyProvider): | ||
"""Access to cg-gnn features from database.""" | ||
|
||
def get_metrics(self, study: str) -> list[CGGNNImportanceRank]: | ||
"""Get cg-gnn metrics for this study. | ||
Returns | ||
------- | ||
list[CGGNNImportanceRank] | ||
List of (histological structure ID, importance rank) tuples. | ||
""" | ||
components = StudyAccess(self.cursor).get_study_components(study) | ||
self.cursor.execute(f''' | ||
SELECT | ||
qfv.subject, | ||
qfv.value | ||
FROM quantitative_feature_value qfv | ||
JOIN feature_specification fs | ||
ON fs.identifier=qfv.feature | ||
WHERE fs.derivation_method='{get_feature_description("gnn importance score")}' | ||
AND fs.study='{components.analysis}' | ||
; | ||
''') | ||
rows = self.cursor.fetchall() | ||
return [CGGNNImportanceRank( | ||
histological_structure_id=int(row[0]), | ||
rank=int(row[1]) | ||
) for row in rows] |
This file contains 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
2 changes: 1 addition & 1 deletion
2
spatialprofilingtoolbox/db/phenotypes.py → ...ofilingtoolbox/db/accessors/phenotypes.py
This file contains 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
File renamed without changes.
File renamed without changes.
This file contains 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 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 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 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 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 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