celltypist icon indicating copy to clipboard operation
celltypist copied to clipboard

Update plot.py

Open milescsmith opened this issue 2 years ago • 3 comments

Fixes KeyError: [...] not in index when running celltypist.plot.dotplot()

milescsmith avatar Oct 23 '23 20:10 milescsmith

@milescsmith, can you provide more details on why this change is needed? Thank you!

ChuanXu1 avatar Nov 06 '23 10:11 ChuanXu1

Sure. The problem is that if prediction_order is not passed to celltypist.plot.dotplot() (and thus to celltypist.plot._get_fraction_prob_df()), prediction_order is set to all possible categories in the passed AnnotationResult.predicted_labels; however, the dot_color_df is derived from the sliced pred = predictions.predicted_labels[use_as_prediction], so that by the time you get to dot_color_df = dot_color_df.loc[prediction_order], the values in prediction_order may be larger than the possible values in dot_color_df and thus _get_fraction_prob_df() and dotplot() fail due to a KeyError. The bit of code I added ensures the values match or that prediction_order is a subset.

milescsmith avatar Nov 07 '23 17:11 milescsmith

@milescsmith, I think dot_color_df is not sliced, so their values should be matched. Do you any code to produce the scenario you mentioned?

ChuanXu1 avatar Nov 11 '23 08:11 ChuanXu1