Skip to content

Commit 326e31e

Browse files
committed
simplified xenium labels indices handling
1 parent b043d12 commit 326e31e

File tree

1 file changed

+40
-54
lines changed

1 file changed

+40
-54
lines changed

src/spatialdata_io/readers/xenium.py

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -377,65 +377,51 @@ def _get_labels_and_indices_mapping(
377377

378378
# build the matching table
379379
version = _parse_version_of_xenium_analyzer(specs)
380+
if mask_index == 0:
381+
# nuclei currently not supported
382+
return labels, None
380383
if version is not None and version < packaging.version.parse("1.3.0"):
381384
# supported in version 1.3.0 and not supported in version 1.0.2; conservatively, let's assume it is not
382385
# supported in versions < 1.3.0
383-
indices_mapping = None
384-
elif mask_index == 0:
385-
# nuclei currently not supported
386-
indices_mapping = None
387-
else:
388-
version = _parse_version_of_xenium_analyzer(specs)
389-
if version is None or version < packaging.version.parse("2.0.0"):
390-
expected_label_index = z["seg_mask_value"][...]
391-
real_label_index = _get_unique_label_values_as_index(labels).values
392-
393-
# background removal
394-
if real_label_index[0] == 0:
395-
real_label_index = real_label_index[1:]
396-
397-
if not np.array_equal(expected_label_index, real_label_index):
398-
raise ValueError(
399-
"The label indices from the labels differ from the ones from the input data. Please report "
400-
f"this issue. Real label indices: {real_label_index}, expected label indices: "
401-
f"{expected_label_index}."
402-
)
403-
cell_id, dataset_suffix = z["cell_id"][...].T
404-
cell_id_str = cell_id_str_from_prefix_suffix_uint32(cell_id, dataset_suffix)
405-
406-
# labels_index is an uint32, so let's cast to np.int64 to avoid the risk of overflow on some systems
407-
indices_mapping = pd.DataFrame(
408-
{
409-
"region": labels_name,
410-
"cell_id": cell_id_str,
411-
"label_index": real_label_index.astype(np.int64),
412-
}
386+
return labels, None
387+
388+
cell_id, dataset_suffix = z["cell_id"][...].T
389+
cell_id_str = cell_id_str_from_prefix_suffix_uint32(cell_id, dataset_suffix)
390+
391+
# this information will probably be available in the `label_id` column for version > 2.0.0 (see public
392+
# release notes mentioned above)
393+
real_label_index = _get_unique_label_values_as_index(labels)
394+
395+
# background removal
396+
if real_label_index[0] == 0:
397+
real_label_index = real_label_index[1:]
398+
399+
if version < packaging.version.parse("2.0.0"):
400+
expected_label_index = z["seg_mask_value"][...]
401+
real_label_index = _get_unique_label_values_as_index(labels).values
402+
403+
if not np.array_equal(expected_label_index, real_label_index):
404+
raise ValueError(
405+
"The label indices from the labels differ from the ones from the input data. Please report "
406+
f"this issue. Real label indices: {real_label_index}, expected label indices: "
407+
f"{expected_label_index}."
413408
)
414-
else:
415-
labels_positional_indices = z["polygon_sets"][mask_index]["cell_index"][...]
416-
if not np.array_equal(labels_positional_indices, np.arange(len(labels_positional_indices))):
417-
raise ValueError(
418-
"The positional indices of the labels do not match the expected range. Please report this "
419-
"issue."
420-
)
421-
# this information will probably be available in the `label_id` column (see public release notes
422-
# mentioned above)
423-
real_label_index = _get_unique_label_values_as_index(labels)
424-
cell_id, dataset_suffix = z["cell_id"][...].T
425-
cell_id_str = cell_id_str_from_prefix_suffix_uint32(cell_id, dataset_suffix)
426-
427-
# background removal
428-
if real_label_index[0] == 0:
429-
real_label_index = real_label_index[1:]
430-
431-
# labels_index is an uint32, so let's cast to np.int64 to avoid the risk of overflow on some systems
432-
indices_mapping = pd.DataFrame(
433-
{
434-
"region": labels_name,
435-
"cell_id": cell_id_str,
436-
"label_index": real_label_index.astype(np.int64),
437-
}
409+
else:
410+
labels_positional_indices = z["polygon_sets"][mask_index]["cell_index"][...]
411+
if not np.array_equal(labels_positional_indices, np.arange(len(labels_positional_indices))):
412+
raise ValueError(
413+
"The positional indices of the labels do not match the expected range. Please report this "
414+
"issue."
438415
)
416+
417+
# labels_index is an uint32, so let's cast to np.int64 to avoid the risk of overflow on some systems
418+
indices_mapping = pd.DataFrame(
419+
{
420+
"region": labels_name,
421+
"cell_id": cell_id_str,
422+
"label_index": real_label_index.astype(np.int64),
423+
}
424+
)
439425
return labels, indices_mapping
440426

441427

0 commit comments

Comments
 (0)