napari-spatialdata icon indicating copy to clipboard operation
napari-spatialdata copied to clipboard

NaN values colored if non-NaN values are only one

Open aeisenbarth opened this issue 1 year ago • 2 comments

~When the table misses some instances or contains NaN values, colors are not mapped. Specifically if:~

  • ~The table contains less instances than the segmentation image in labels~
  • ~Or an observation column or X column contains NaN (because missing values have been filled up).~

Actual behavior:

If an obs/X column contains NaN and the non-NaN values are all the same, all labels are visualized with color, as if it ignores the fact that some are NaN.

Expected behavior:

It does not color values that are NaN.

aeisenbarth avatar Apr 29 '24 17:04 aeisenbarth

Sorry, the initial observation actually is working correctly.

Except in one edge case:

  • If a column's non-NaN values have a single value (all same or NaN), all labels receive the same color (yellow) although the NaN labels should be transparent.

This is specifically the case when n_obs == 2 (so rather a rare case, not a critical issue):

import anndata as ad
import numpy as np
import pandas as pd
import spatialdata as sd
from napari_spatialdata import Interactive

labels = sd.models.Labels2DModel.parse(np.array([[0, 1], [2, 0]]), dims=("y", "x"))
adata = ad.AnnData(obs=pd.DataFrame({"col1": [0.5, np.nan], "region": "region1", "instance_id": [1, 2]}))
table = sd.models.TableModel.parse(adata, region=["region1"], region_key="region", instance_key="instance_id")
sdata = sd.SpatialData(labels={"region1": labels}, table=table)
Interactive(sdata=sdata)
# Expected: obs "col1" is yellow for label 1 (top right) and transparent for label 2 (bottom left)
# Actual: label 1 and label 2 are yellow

image

aeisenbarth avatar Apr 29 '24 18:04 aeisenbarth

Thanks for reporting! Will look into it

melonora avatar May 10 '24 16:05 melonora