Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherry pick fix for baysor 0.7.0 (#133) #140

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions sopa/segmentation/transcripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,21 @@ def _read_one_segmented_patch(

adata.obs.rename(columns={"area": SopaKeys.ORIGINAL_AREA_OBS}, inplace=True)

cells_num = pd.Series(adata.obs_names if id_as_string else adata.obs["CellID"].astype(int), index=adata.obs_names)
cells_ids = pd.Series(adata.obs_names if id_as_string else adata.obs["CellID"].astype(int), index=adata.obs_names)
del adata.obs["CellID"]

with open(polygon_file) as f:
polygons_dict = json.load(f)
polygons_dict = {c["cell"]: c for c in polygons_dict["geometries"]}

cells_num = cells_num[cells_num.map(lambda num: len(polygons_dict[num]["coordinates"][0]) >= min_vertices)]
def _keep_cell(ID: str | int):
if ID not in polygons_dict:
return False
return len(polygons_dict[ID]["coordinates"][0]) >= min_vertices

gdf = gpd.GeoDataFrame(
index=cells_num.index,
geometry=[shape(polygons_dict[cell_num]) for cell_num in cells_num],
)
cells_ids = cells_ids[cells_ids.map(_keep_cell)]

gdf = gpd.GeoDataFrame(index=cells_ids.index, geometry=[shape(polygons_dict[ID]) for ID in cells_ids])

gdf.geometry = gdf.geometry.map(lambda cell: shapes._ensure_polygon(cell))
gdf = gdf[~gdf.geometry.isna()]
Expand Down Expand Up @@ -191,14 +193,16 @@ def _resolve_patches(
)


def copy_segmentation_config(path: Path, config: dict, config_path: str | None):
def copy_segmentation_config(path: Path | str, config: dict, config_path: str | None):
"""Copy the segmentation config to a file.

Args:
path: Where the config will be saved
config: Dictionnary config
config_path: Already existing config file, will be copied if provided
"""
path = Path(path)

if config_path is not None:
import shutil

Expand Down
Loading