Skip to content

Commit

Permalink
Fix cell index handling in proximity calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmymathews committed Sep 25, 2023
1 parent b7b48f9 commit 2058def
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
11 changes: 11 additions & 0 deletions spatialprofilingtoolbox/ondemand/providers/proximity_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def _create_ball_trees(self) -> None:
}
for study_name, _data_arrays in self.data_arrays.items()
}
self.index_lookups = {
study_name: {
sample_identifier: tuple(df.index.to_list())
for sample_identifier, df in _data_arrays.items()
}
for study_name, _data_arrays in self.data_arrays.items()
}

@classmethod
def get_or_create_feature_specification(
Expand Down Expand Up @@ -136,6 +143,7 @@ def have_feature_computed(self, feature_specification: str) -> None:
radius,
self.get_cells(sample_identifier, study_name),
self._get_tree(sample_identifier, study_name),
self._get_index_lookup(sample_identifier, study_name),
)
message = 'Computed one feature value of %s: %s, %s'
logger.debug(message, feature_specification, sample_identifier, value)
Expand All @@ -148,3 +156,6 @@ def have_feature_computed(self, feature_specification: str) -> None:

def _get_tree(self, sample_identifier: str, study_name: str) -> BallTree:
return self.trees[study_name][sample_identifier]

def _get_index_lookup(self, sample_identifier: str, study_name: str) -> tuple[int, ...]:
return self.index_lookups[study_name][sample_identifier]
7 changes: 4 additions & 3 deletions spatialprofilingtoolbox/workflow/common/proximity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def compute_proximity_metric_for_signature_pair(
signature2: PhenotypeCriteria,
radius: float,
cells: DataFrame,
tree: BallTree
tree: BallTree,
index_lookup: tuple[int, ...],
) -> float | None:
cells = cells.rename({
column: (column[2:] if (column.startswith('C ') or column.startswith('P ')) else column)
Expand All @@ -38,8 +39,8 @@ def compute_proximity_metric_for_signature_pair(
return_distance=False,
)
counts = [
sum(mask2[index] for index in list(indices))
for indices in within_radius_indices_list
sum(mask2[index_lookup[integer_index]] for integer_index in list(integer_indices))
for integer_indices in within_radius_indices_list
]
count = sum(counts) - sum(logical_and(mask1, mask2))
return count / source_count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def calculate_proximity(self):
r,
cells,
self.tree,
tuple(cells.index.to_list()),
) for f1, f2, r in self.get_cases(all_features)
}
self.write_table(proximity_metrics)
Expand Down

0 comments on commit 2058def

Please sign in to comment.