Repository navigation
Leiden restrict_to parameter - #586
Conversation
Merge with master
Fixes scanpy import error due to missing __init__.py
|
Would you mind adding a test? |
|
Great! Please create helper functions abstracting away identical code blocks in the two functions. E.g. those lines are completely identical to the ones in And the same might apply to the other code block. |
|
I added helper functions. I am working on the tests. I am unsure why it is failing on Travis. |
|
I added tests for both louvain and leiden with restrict parameter. Please review the test code to be sure it is clear and working. |
flying-sheep
left a comment
There was a problem hiding this comment.
Looking great! The test seems effective. There are some minor stylistic changes, but no content ones.
ivirshup
left a comment
There was a problem hiding this comment.
Just a few pointers on tests
| print('Common categories') | ||
| print(common_cat) | ||
| ## Only new categories | ||
| assert len(common_cat) == 0 |
There was a problem hiding this comment.
An example of pytest printing values when assert fails. If this assertion fails pytest should print common_cat on it's own, so there's no need for the print statements above it.
|
|
||
| # Old category should not be present | ||
| categories = new_partition.unique() | ||
| assert c not in categories |
There was a problem hiding this comment.
Isn't this case covered by the assert len(common_cat) == 0 statement?
|
@flying-sheep @ivirshup As you looked through this in detail already, one of you should merge this when ready, ok? |
|
Only one thing that I would ask for: please don't put things in utils when it's not absolutely necessary. Here, it would be very natural to add a Thank you for moving the helper functions there. Or, foreseeing that |
|
@falexwolf I added |
|
I've got one minor comment left (one last redundant print statement), but otherwise I'm good. |
|
Looks good to me! Thanks @fbrundu. |
|
we should update the tutorials and notebooks to use |
|
Yes, we should as soon as many people report seemless installation of the leiden package. I'm still using louvain as I never had any problems with it, but I agree that we should migrate when leiden is stable, mature and easily-installable. |
|
Has anyone compared the two for speed? I guess Leiden has an extra step and therefore might be a bit slower. If they both scale well to >1 million cells, then I agree... otherwise the issues of louvain shouldn't be as bad for KNN-graphs as for more graphs with more heavy tailed degree distributions. |
|
I looked at it a while ago (for one test dataset, probably), and got the impression that @LuckyMD When you say heavy tailed, are you thinking of the unweighted KNN graph case or both? |
|
They'll both be affected by the resolution limit, which might be what you're referring to. This is a well-described problem for Modularity with the configuration null model that it only optimally detects communities within a certain size range relative to the size of the network. For me heavy-tailed networks are PPIs.. KNNs are a lot more regular than that. I'm not sure what a weighted KNN graph would be... are you talking about the PhenoGraph approach? |
|
Yeah, I was pretty much talking about the resolution limit. PhenoGraph would be an example of a weighted KNN, but so would the |
|
I wasn't aware we use a weighted matrix for clustering as default. Are the weights in the |
|
Sort of. I believe weights are between 0 and 1, where the edge to the nearest neighbor has weight=1, and the k-th+ neighbor has weight=0. I'm not quite sure how the weights are scaled within that, but I'm pretty sure it's not rank based. Leland Mcinnes has explained it much better than I can in his explanations of UMAP. It's discussed in the docs starting with the part on Riemannian geometry, but is also covered in his talks or the UMAP paper. |
|
Thanks for the link. This is definitely easier to understand than the paper ;). |
|
If we use the |
|
We didn't use the weights in Louvain (https://github.com/theislab/scanpy/blob/297d6246ccfbf398f771cee1bd4b81b57fc27c76/scanpy/tools/_louvain.py#L31)? Why did you decide to change the default in Leiden
|
|
@falexwolf I recall that image in the figure, thanks! I wasn't aware that the embedding was the difficult part. I would also like to know what the benefit of clustering the weighted graph is in practice.... It definitely makes the clustering procedure more difficult to explain in one sentence in the methods section of a paper... |
|
I was also investigating how I think it'd be pretty different from clustering on the embedding, because the embedding has constraints based on things like minimum distance two points can be from each other, and the number of dimensions it's embedded in. On the binarized KNN-graph, I think we've actually talked about this before (#240). I personally think using a weighted graph makes more sense. For example, say you have a cell type of which occurs 15 times in your dataset, but you've set k to 30. With a binarized graph there will be a less clear signal that this is a distinct cell-type. From a slightly more empirical/ anecdotal perspective, on a couple datasets I tested, total degree of the generated graph was sub-linear (looked log-ish) w.r.t. To me, this suggested a stable representation of the dataset was being found. As a connected point, in my experience clustering results seems fairly robust to |
|
I'm not sure I agree with your interpretation of your total degree plot. To me, increasing I wonder if the observation that you find smaller clusters better in the weighted case is robust. That would suggest that weights can counteract resolution limit issues, which would be very interesting... |
|
Sorry about the delay, I've been working on some writing about this stuff (though from a different perspective). I'm not sure I've got some plots for the effect of resolution and number of neighbors on the size of clusters which are found. This is for the 10x example dataset with 10k pbmcs using the v3 chemistry. What I've done is build the networks at 5 different values of k, four times each (different random seeds). For each of those networks, I ran clustering at 50 different resolutions ( Overall, pretty similar. Now, the minimum cluster sizes (color scales are different, but you'll see why): This looks to me like using the weighted graph allows identifying small clusters even at low resolutions. The cluster of 25 cells looks like megakaryocytes, and are being detected at pretty much every clustering (996 out of 1000) using the weighted graph. |
|
This looks a lot more convincing... It's a bit hard to read the second last plot though... The black parts are also clusters around size 2-10, no? Or am I misreading the scale? Do you have a version with a few more annotations on the colour bar? How often are megakaryocytes detected as a separate cluster in the unweighted case? It looks like unweighted case is definitely worse for higher resolutions with >10 neighbours though. And coming back to the |
|
I took about 20 minutes on it, but couldn't figure out how to add more annotations. I've got interactive versions with hover over, but log scale is bugged in those libraries... I believe the bins that are the darkest shade in the minimum cluster size for the unweighted graph actually correspond to a minimum cluster size of 1 cell. Megakaryocytes were detected as a distinct cluster every time that k was 10 in the unweighted case, but no other times. I think that when we make a call on "this is a kind of cell" from unsupervised clustering, those results should be robust. That is, if there's strong signal in the data and your clustering algorithm can pick up that signal, good clusters shouldn't change much if you vary the parameters a little. If you can pick any parameters from a wide range and get results that are pretty consistent, that seems like good data and a good method to me. |
|
I follow your argumentation on "good clusters". However, I also like the concept that putting k=35 means you make it harder to detect clusters of size < 35, as you 'over-connect' those clusters in a way. The weighted case is less interpretable in that way. However, here it clearly outperforms the unweighted case. I am still a little on the fence (due to interpretability), but I'd be okay with weighting I think. |
Leiden restrict_to parameter





Added restrict_to parameter to leiden by using louvain code as template.
Tests are not yet provided.
A simple example of execution and checks: