Skip to content

Commit 273acca

Browse files
author
xyi
committed
Merge branch 'main' of https://github.com/ckmah/spatialdata-io into main
2 parents a2f3d09 + f670424 commit 273acca

File tree

1 file changed

+14
-46
lines changed
  • src/spatialdata_io/readers

1 file changed

+14
-46
lines changed

src/spatialdata_io/readers/g4x.py

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import re
44
from pathlib import Path
5-
from typing import Union
65

76
import dask.dataframe as dd
8-
import numpy as np
97
import glymur
8+
import numpy as np
109
import PIL
1110
from anndata.io import read_h5ad
1211
from dask.array.image import imread
@@ -82,25 +81,15 @@ def g4x(
8281
output_path = Path(output_path)
8382

8483
# Determine if input_path is a run directory or a single sample directory
85-
if any(
86-
p.is_dir() and re.match(r"[A-Z][0-9]{2}", p.name) for p in input_path.iterdir()
87-
):
84+
if any(p.is_dir() and re.match(r"[A-Z][0-9]{2}", p.name) for p in input_path.iterdir()):
8885
# Run directory with multiple samples
89-
sample_input_paths = [
90-
p
91-
for p in input_path.iterdir()
92-
if p.is_dir() and re.match(r"[A-Z][0-9]{2}", p.name)
93-
]
86+
sample_input_paths = [p for p in input_path.iterdir() if p.is_dir() and re.match(r"[A-Z][0-9]{2}", p.name)]
9487
logger.debug(f"Found {len(sample_input_paths)} samples.")
9588

9689
if output_path is None:
97-
sample_output_paths = [
98-
input_path / p.name / f"{p.name}.zarr" for p in sample_input_paths
99-
]
90+
sample_output_paths = [input_path / p.name / f"{p.name}.zarr" for p in sample_input_paths]
10091
else:
101-
sample_output_paths = [
102-
output_path / f"{p.name}.zarr" for p in sample_input_paths
103-
]
92+
sample_output_paths = [output_path / f"{p.name}.zarr" for p in sample_input_paths]
10493

10594
sdatas = []
10695
for sample_input_path, sample_output_path in tqdm(
@@ -190,9 +179,7 @@ def g4x_sample(
190179
output_zarr_path = Path(output_zarr_path)
191180
if output_zarr_path.suffix != ".zarr":
192181
logger.error(f"Output path must end with '.zarr'. Got {output_zarr_path}")
193-
raise ValueError(
194-
f"Output path must end with '.zarr'. Got {output_zarr_path}"
195-
)
182+
raise ValueError(f"Output path must end with '.zarr'. Got {output_zarr_path}")
196183

197184
if mode not in ["append", "overwrite"]:
198185
msg = f"Invalid mode '{mode}'. Must be one of: 'append', 'overwrite'"
@@ -431,20 +418,15 @@ def _write_segmentation(
431418
f"shapes/{shapes_nuclei_exp_key}",
432419
]
433420

434-
if mode == "append" and any(
435-
p in sdata.elements_paths_on_disk() for p in elements_paths
436-
):
421+
if mode == "append" and any(p in sdata.elements_paths_on_disk() for p in elements_paths):
437422
logger.debug("Segmentation already exist. Skipping...")
438423
return
439424
elif mode == "overwrite":
440425
logger.debug("Deleting existing segmentation elements")
441426
for el in elements:
442427
if el in sdata:
443428
del sdata[el]
444-
if (
445-
f"labels/{el}" in sdata.elements_paths_on_disk()
446-
or f"shapes/{el}" in sdata.elements_paths_on_disk()
447-
):
429+
if f"labels/{el}" in sdata.elements_paths_on_disk() or f"shapes/{el}" in sdata.elements_paths_on_disk():
448430
sdata.delete_element_from_disk(el)
449431

450432
# Load and process segmentation data
@@ -466,16 +448,12 @@ def _write_segmentation(
466448

467449
# Nuclei shapes
468450
sdata[shapes_nuclei_key] = to_polygons(sdata[nuclei_key]).set_index("label")
469-
sdata[shapes_nuclei_key].geometry = sdata[shapes_nuclei_key].translate(
470-
xoff=offset, yoff=offset
471-
)
451+
sdata[shapes_nuclei_key].geometry = sdata[shapes_nuclei_key].translate(xoff=offset, yoff=offset)
472452
sdata[shapes_nuclei_key].index = sdata[shapes_nuclei_key].index.astype(str)
473453

474454
# Expanded nuclei shapes
475455
sdata[shapes_nuclei_exp_key] = to_polygons(sdata[nuclei_exp_key]).set_index("label")
476-
sdata[shapes_nuclei_exp_key].geometry = sdata[shapes_nuclei_exp_key].translate(
477-
xoff=offset, yoff=offset
478-
)
456+
sdata[shapes_nuclei_exp_key].geometry = sdata[shapes_nuclei_exp_key].translate(xoff=offset, yoff=offset)
479457
sdata[shapes_nuclei_exp_key].index = sdata[shapes_nuclei_exp_key].index.astype(str)
480458

481459
logger.debug("Writing elements")
@@ -520,9 +498,7 @@ def _write_protein_images(
520498
img_list.sort()
521499

522500
if not img_list:
523-
logger.warning(
524-
f"No protein images found matching pattern '{pattern}' in {protein_dir}"
525-
)
501+
logger.warning(f"No protein images found matching pattern '{pattern}' in {protein_dir}")
526502
return
527503
logger.debug(f"Found {len(img_list)} protein images")
528504

@@ -554,9 +530,7 @@ def _write_protein_images(
554530

555531
# Create Image2DModel and write
556532
logger.debug("Converting to Image2DModel")
557-
sdata[G4XKeys.PROTEIN_KEY.v] = Image2DModel.parse(
558-
protein_stack, c_coords=channel_names, **kwargs
559-
)
533+
sdata[G4XKeys.PROTEIN_KEY.v] = Image2DModel.parse(protein_stack, c_coords=channel_names, **kwargs)
560534

561535
logger.debug("Writing protein images")
562536
sdata.write_element(G4XKeys.PROTEIN_KEY.v)
@@ -629,9 +603,7 @@ def _write_transcripts(
629603
logger.debug(f"swap_xy: {swap_xy}, {type(swap_xy)}")
630604
if swap_xy:
631605
logger.debug("Swapping x and y coordinates")
632-
transcripts[[coordinates["x"], coordinates["y"]]] = transcripts[
633-
[coordinates["y"], coordinates["x"]]
634-
]
606+
transcripts[[coordinates["x"], coordinates["y"]]] = transcripts[[coordinates["y"], coordinates["x"]]]
635607

636608
pbar.set_description("Converting to PointsModel")
637609
sdata[G4XKeys.TRANSCRIPTS_KEY.v] = PointsModel.parse(
@@ -708,11 +680,7 @@ def _deep_update(base_dict, update_dict):
708680
Recursively update a dictionary with another dictionary.
709681
"""
710682
for key, value in update_dict.items():
711-
if (
712-
isinstance(value, dict)
713-
and key in base_dict
714-
and isinstance(base_dict[key], dict)
715-
):
683+
if isinstance(value, dict) and key in base_dict and isinstance(base_dict[key], dict):
716684
_deep_update(base_dict[key], value)
717685
else:
718686
base_dict[key] = value

0 commit comments

Comments
 (0)