Skip to content

Commit fa1a925

Browse files
authored
fixed bugs for xenium explorer 3+.
all small bugs are fixed regarding the vertices and the zarr format is now compatible with xenium explorer v3+.
1 parent b7b51ed commit fa1a925

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/segger/validation/xenium_explorer.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ def get_flatten_version(polygon_vertices: List[List[Tuple[float, float]]], max_v
2525
np.ndarray: Padded or truncated list of polygon vertices.
2626
"""
2727
flattened = []
28-
29-
if isinstance(vertices, np.ndarray):
30-
vertices = vertices.tolist()
31-
3228
for vertices in polygon_vertices:
29+
if len(vertices) < 3:
30+
pass
31+
if isinstance(vertices, np.ndarray):
32+
vertices = vertices.tolist()
33+
3334
if len(vertices) > max_value:
3435
flattened.append(vertices[:max_value])
3536
else:
36-
flattened.append(vertices + [(0.0, 0.0)] * (max_value - len(vertices)))
37+
flattened.append(vertices + [vertices[0]] * (max_value - len(vertices)))
3738
return np.array(flattened, dtype=np.float32)
3839

3940

@@ -67,6 +68,7 @@ def seg2explorer(
6768
"""
6869
source_path = Path(source_path)
6970
storage = Path(output_dir)
71+
storage.mkdir(parents=True, exist_ok=True)
7072

7173
cell_id2old_id: Dict[int, Any] = {}
7274
cell_id: List[int] = []
@@ -127,7 +129,7 @@ def seg2explorer(
127129
seg_mask_value.append(uint_cell_id)
128130

129131
cell_polygon_vertices = get_flatten_version(polygon_vertices[0], max_value=128)
130-
nucl_polygon_vertices = get_flatten_version(polygon_vertices[1], max_value=128)
132+
# nucl_polygon_vertices = get_flatten_version(polygon_vertices[1], max_value=20)
131133

132134
cells = {
133135
"cell_id": np.array(
@@ -141,19 +143,33 @@ def seg2explorer(
141143
],
142144
dtype=np.int32,
143145
),
144-
"polygon_vertices": np.array(
145-
[nucl_polygon_vertices, cell_polygon_vertices], dtype=np.float32
146-
),
146+
# "polygon_vertices": np.array(
147+
# [nucl_polygon_vertices, cell_polygon_vertices], dtype=np.float32
148+
# ),
147149
"seg_mask_value": np.array(seg_mask_value, dtype=np.int32),
148150
}
149151

150152
source_zarr_store = ZipStore(source_path / "cells.zarr.zip", mode="r") # added this line
151153
existing_store = zarr.open(source_zarr_store, mode="r")
152154
new_store = zarr.open(storage / f"{cells_filename}.zarr.zip", mode="w")
153-
new_store["cell_id"] = cells["cell_id"]
154-
new_store["polygon_num_vertices"] = cells["polygon_num_vertices"]
155-
new_store["polygon_vertices"] = cells["polygon_vertices"]
156-
new_store["seg_mask_value"] = cells["seg_mask_value"]
155+
156+
# Create polygon_sets group with the new structure
157+
polygon_group = new_store.create_group("polygon_sets")
158+
159+
# Process cell polygons (set 1)
160+
# cell_polygons = cells["polygon_vertices"][1] # Cell polygons are at index 1
161+
cell_num_vertices = cells["polygon_num_vertices"][1] # Cell vertex counts
162+
163+
# Reshape cell polygons to (n_cells, 50) format
164+
n_cells = cell_polygon_vertices.shape[0]
165+
cell_vertices_flat = cell_polygon_vertices.reshape(n_cells, -1)[:, :257] # Take first 50 values
166+
167+
set1 = polygon_group.create_group("1")
168+
set1["cell_index"] = np.arange(1, n_cells + 1, dtype=np.uint32) # 1-based indexing
169+
set1["method"] = np.ones(n_cells, dtype=np.uint32) # All method=1
170+
set1["num_vertices"] = np.array(cell_num_vertices, dtype=np.int32)
171+
set1["vertices"] = cell_vertices_flat.astype(np.float32)
172+
157173
new_store.attrs.update(existing_store.attrs)
158174
new_store.attrs["number_cells"] = len(cells["cell_id"])
159175
new_store.store.close()
@@ -440,8 +456,8 @@ def generate_experiment_file(
440456
with open(template_path) as f:
441457
experiment = json.load(f)
442458

443-
experiment["images"].pop("morphology_filepath")
444-
experiment["images"].pop("morphology_focus_filepath")
459+
# experiment["images"].pop("morphology_filepath")
460+
# experiment["images"].pop("morphology_focus_filepath")
445461

446462
experiment["xenium_explorer_files"][
447463
"cells_zarr_filepath"

0 commit comments

Comments
 (0)