Skip to content

Commit

Permalink
fix: Add some fixes to geojson reading.
Browse files Browse the repository at this point in the history
- data_utils: correct "classification" column remapping by checking if the element is a dict
- qupath_utils: add an argument in read_qupath_annotations to indicate which to column to use in the df to import the polygons instead of assuming it, adding logger for logging messages
  • Loading branch information
loic-lb committed Jul 5, 2024
1 parent e14966c commit f9d41a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/prismtoolbox/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ def read_json_with_geopandas(file_path: str, offset: tuple[int, int] = (0, 0)) -
if not df.is_valid.any():
df.loc[~df.is_valid,:] = df.loc[~df.is_valid, :].buffer(0)
if "classification" in df.columns:
df["classification"] = df["classification"].apply(lambda x: x["name"])
df["classification"] = df["classification"].apply(lambda x: x["name"] if type(x) == dict else x)
return df
9 changes: 6 additions & 3 deletions src/prismtoolbox/utils/qupath_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Optional, Tuple, List, Union
from .data_utils import load_obj_with_json, save_obj_with_json, read_json_with_geopandas

log = logging.getLogger(__name__)

def contoursToPolygons(
contours: List[np.ndarray], merge: Optional[bool] = False
Expand Down Expand Up @@ -49,15 +50,17 @@ def PolygonsToContours(polygons: MultiPolygon):
for poly in polygons.geoms
]

def read_qupath_annotations(path: str, offset: Optional[Tuple[int, int]] = (0, 0), class_name: str = "annotation"):
def read_qupath_annotations(path: str, offset: Optional[Tuple[int, int]] = (0, 0), class_name: str = "annotation",
column_to_select: str = "objectType"):
"""Reads pathologist annotations from a .geojson file.
:param path: path to the .geojson file
:param offset: optional offset to add to each coordinate in the arrays
:param class_name: name of the class to select
:param column_to_select: optional column to select
:return:
"""
df = read_json_with_geopandas(path, offset)
column_to_select = "classification" if "classification" in df.columns else "objectType"
polygons = df.loc[df[column_to_select] == class_name, "geometry"].values
polygons = MultiPolygon(polygons)
if not polygons.is_valid:
Expand Down Expand Up @@ -120,7 +123,7 @@ def export_polygons_to_qupath(
if os.path.exists(path) and append_to_existing_file:
previous_features = load_obj_with_json(path)
if len(previous_features) == 0:
logging.warning(
log.warning(
"The .geojson file does not contain any features, creating new file."
)
else:
Expand Down

0 comments on commit f9d41a9

Please sign in to comment.