Skip to content

Commit

Permalink
better polygon crop
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinblampey committed Sep 12, 2023
1 parent c4d29c8 commit c9a8f74
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions sopa/utils/polygon_crop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from pathlib import Path

import click
Expand All @@ -24,38 +23,31 @@ def __init__(self, ax):
plt.show()

def onselect(self, vertices):
self.vertices = vertices
self.vertices = np.array(vertices)
print(f"Selected polygon with {len(self.vertices)} vertices.")

def disconnect(self):
self.poly.disconnect_events()


def _save_polygon(path: str, coords: np.ndarray | list):
if isinstance(coords, np.ndarray):
coords = coords.tolist()
with open(path, "w") as f:
json.dump(coords, f, indent=4)


def xarr_selector(
image_path: str,
output_path: str,
channels: list[str],
scale_factor: float = 10,
margin_ratio: float = 0.2,
margin_ratio: float = 0.1,
):
import xarray as xr

from .image import resize

image = xr.open_zarr(image_path)["image"]
image = xr.open_zarr(image_path)["image"].transpose("y", "x", "c")

if len(channels):
assert (
len(channels) in VALID_N_CHANNELS
), f"Number of channels provided must be in: {', '.join(VALID_N_CHANNELS)}"
image = image.sel(c=channels).transpose("y", "x", "c")
image = image.sel(c=channels)

image = resize(image, scale_factor).compute()

Expand All @@ -68,7 +60,7 @@ def xarr_selector(

selector = _Selector(ax)

_save_polygon(output_path, np.array(selector.vertices) * scale_factor)
np.savetxt(output_path, selector.vertices * scale_factor)


def cells_selector(
Expand All @@ -87,7 +79,7 @@ def cells_selector(

selector = _Selector(ax)

_save_polygon(output_path, selector.vertices)
np.savetxt(output_path, selector.vertices)


@click.command()
Expand Down

0 comments on commit c9a8f74

Please sign in to comment.