Teichlab / celltypist

A tool for semi-automatic cell type classification
https://www.celltypist.org/
MIT License
278 stars 44 forks source link

Binary cell type decisions lead to np.AxisError in predict_labels_and_prob #17

Closed chbeltz closed 2 years ago

chbeltz commented 2 years ago

When trying to make a binary cell type decision using CellTypist, I encountered an AxisError in line 131 of models.py, caused by scores.argmax(axis=1). When making binary decisions, sklearn's LogsticRegression.decision_function yields not an ndarray of shape (n_samples, n_classes) but of (n_samples,), making axis=1 and argmax in general problematic.

My suggested solution is first checking the number of dimensions in the output of decision_function and if it is equal to 1, as with binary decisions, emulating the output format expected by CellTypist, conserving the difference in confidence scores between the two available classes for every cell. This is implemented in #18.

ChuanXu1 commented 2 years ago

@chbeltz, thx a lot!! will check and let you know soon.

ChuanXu1 commented 2 years ago

@chbeltz , can I ask why you set the scores of the opposite class to all 0. As I understand, the emulated score matrix can be simply stacked by column-binding the -decision_function vector with the decision_function vector.

chbeltz commented 2 years ago

@ChuanXu1 Sure, it could. However I did not see any reason to believe that would be superior. In fact I did not see anything in the docs leading me to lean either way, seeing as the relative difference in certainty between predictions would remain the same.

Is there anything making your version preferable in your opinion?

ChuanXu1 commented 2 years ago

@chbeltz, in a binary case, even with the 'ovr' mode, the classifier outputs the intercept and coefficients for the second class (.classes_[1]). For the first class, the decision score is not output, but it is actually the negative form of the score in the second class. Activated by the sigmoid further, the probability of the first class will be 1 - probability_of_2nd_class. If you turn the opposite to 0, you will get all probabilities of 0.5. Wdyt?

ChuanXu1 commented 2 years ago

@chbeltz, I did not adopt your approach finally, but used the negative scores to make the sum of probabilities in binary cases to 1. Let me know how you think of it? Anyhow, thank you very much for raising the binary cases. cfb3b910e1912f4fb2f0f7f64f8437b2cb78930e

chbeltz commented 2 years ago

Ah, makes sense. Did not think of that. Thanks for the fix.