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

fix presence matrix wrong shape #236

Merged
merged 2 commits into from
Mar 2, 2023
Merged

Conversation

ebezzi
Copy link
Member

@ebezzi ebezzi commented Mar 1, 2023

Reverts a change that introduced a bug that led the presence matrix to have an incorrect shape.

Per the cell-census schema specification, the presence matrix needs to have N rows where N is the number of datasets belonging to the experiment. Since this is a sparse matrix, its domain will extend to the total number of datasets (i.e. max(d.soma_joinid for d in datasets)), therefore the shape of the presence matrix (for each experiment) should equal the total number of datasets.

@ebezzi ebezzi requested a review from atolopko-czi March 1, 2023 22:04
@bkmartinjr bkmartinjr self-requested a review March 1, 2023 22:18
Copy link
Collaborator

@atolopko-czi atolopko-czi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes, if only to ensure we're doing the right thing here before approving. My take is that this change is not needed.

@@ -82,6 +82,8 @@ def test_base_builder_creation(
# Presence matrix should exist with the correct dimensions
for exp_name in ["homo_sapiens", "mus_musculus"]:
fdpm = census[CENSUS_DATA_NAME][exp_name].ms[MEASUREMENT_RNA_NAME][FEATURE_DATASET_PRESENCE_MATRIX_NAME]
fdpm_matrix = fdpm.read().coos().concat()
assert fdpm_matrix.shape[0] == 4 # 4 datasets
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each presence matrix should only have rows for the datasets that the experiment it is associated with. So 2 (distinct) datasets in each presence matrix for this test, 4 features each, 8 rows total. See discussion. Let me know if I'm misunderstanding.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this assert is wrong. Andrew has it right as I read the conftest code.

Remember, per Pablo's spec, it is the number of datasets that contribute data to the specific experiment, not the number in the total census (i.e., union of all organisms)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the (edited) description, this wasn't the previous behavior which is what this PR is trying to restore. Since that is not compliant with the SOMA abstract spec, we'll probably want to close this and work towards a more permanent fix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was misunderstanding the issue at hand.

Here are some suggested constraints to assert - ie. what we would expect to be true:

  1. For all experiment's presence matrix, all dimension 0 coordinates (aka the dataset DataFrame's soma_joinid) should be > 0 and <= the max soma_joinid in the datasets table.
  2. for all experiment's presence matrix, the shape[0] == max(datasets soma_joinid) + 1

Important assumption above: the rows in the datasets DataFrame are assigned 0..n_datasets. This is just a convention (they could be anything, but we choose to sequentially assign them starting at zero).

So I would change your assertion to read:

assert fdpm_matrix.shape[0] == max(returned_datasets.soma_joinid) + 1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, to clear up the above comment - the spec is silent on the shape of the presence matrix, and only specifies its contents. I believe that fixing the shape as proposed by this PR is consistent with the spec.

@ebezzi ebezzi requested a review from atolopko-czi March 1, 2023 23:08
@@ -265,7 +265,7 @@ def populate_presence_matrix(self) -> None:
# sanity check
assert len(self.presence) == self.n_datasets

max_dataset_joinid = max(self.presence.keys())
max_dataset_joinid = max(d.soma_joinid for d in datasets)
Copy link
Contributor

@bkmartinjr bkmartinjr Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong - it is going to add datasets which do not exist in the organism's experiment.

Edit: my misunderstanding of the defect being fixed. This change looks correct to me.

Copy link
Contributor

@bkmartinjr bkmartinjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good.

One requests before you land this: [lease document in the PR description the assumed data design, which is that the shape of the presence matrix on the first dimension must match the shape of the datasets dataframe.

Thank you!

Copy link
Contributor

@bkmartinjr bkmartinjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ebezzi ebezzi merged commit 3b5d3b3 into main Mar 2, 2023
@ebezzi ebezzi deleted the ebezzi/fix-presence-matrix-shape branch March 2, 2023 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants