-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Integrate geopandas for reading qupath annotations
qupath_utils: - pyproject: include geopandas as new dependency - data_utils: new function to read json with geopandas, reformat docs of others functions - qupath_utils: ensure contoursToPolygons returns valid polygons, modify read_qupath_annotations to read annotations with geopandas, enable saving as feature collections in export_polygons_to_qupath, and use built-in function from shapely to create box in patchesToPolygons
- Loading branch information
Showing
3 changed files
with
79 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,90 @@ | ||
from __future__ import annotations | ||
|
||
import pickle | ||
import json | ||
import h5py | ||
import geopandas as gpd | ||
import numpy as np | ||
from typing import Tuple, Any | ||
|
||
|
||
def save_obj_with_pickle(obj: object, file_path: str) -> None: | ||
""" | ||
Save an object to a file using pickle. | ||
:param obj: a pickeable object | ||
:param file_path: path to the file | ||
"""Save an object to a file using pickle. | ||
Args: | ||
obj: A pickeable object. | ||
file_path: The path to the file. | ||
""" | ||
with open(file_path, "wb") as f: | ||
pickle.dump(obj, f) | ||
|
||
|
||
def save_obj_with_json(obj: object, file_path: str) -> None: | ||
""" | ||
Save an object to a file using json. | ||
:param obj: a json object | ||
:param file_path: path to the file | ||
"""Save an object to a file using json. | ||
Args: | ||
obj: A json object. | ||
file_path: The path to the file. | ||
""" | ||
with open(file_path, "w") as f: | ||
json.dump(obj, f) | ||
|
||
|
||
def load_obj_with_pickle(file_path: str) -> Any: | ||
""" | ||
Load an object from a file using pickle. | ||
:param file_path: path to a pickle file | ||
:return: a pickeable object from the file | ||
"""Load an object from a file using pickle. | ||
Args: | ||
file_path: The path to the pickle file. | ||
Returns: | ||
A pickeable object from the file. | ||
""" | ||
with open(file_path, "rb") as f: | ||
return pickle.load(f) | ||
|
||
|
||
def load_obj_with_json(file_path: str) -> Any: | ||
""" | ||
Load an object from a file using json. | ||
:param file_path: path to a json file | ||
:return: a json object from the file | ||
"""Load an object from a file using json. | ||
Args: | ||
file_path: The path to the json file. | ||
Returns: | ||
A json object from the file. | ||
""" | ||
with open(file_path, "r") as f: | ||
return json.load(f) | ||
|
||
|
||
def read_h5_file(file_path: str, key: str) -> Tuple[np.ndarray, dict]: | ||
""" | ||
Read an object from a h5 file. | ||
:param file_path: path to a h5 file | ||
:param key: key to the object in the h5 file | ||
:return: an object from the h5 file | ||
"""Read an object from a h5 file. | ||
Args: | ||
file_path: The path to the h5 file. | ||
key: The key to select the dataset in the h5 file. | ||
Returns: | ||
A dataset from the h5 file. | ||
""" | ||
with h5py.File(file_path, "r") as f: | ||
object = f[key][()] | ||
attrs = {key: value for key, value in f[key].attrs.items()} | ||
return object, attrs | ||
|
||
def read_json_with_geopandas(file_path: str, offset: tuple[int, int] = (0, 0)) -> gpd.GeoDataFrame: | ||
"""Read a json file with geopandas. | ||
Args: | ||
file_path: The path to a json file. | ||
Returns: | ||
A GeoDataFrame object from the json file. | ||
""" | ||
data = load_obj_with_json(file_path) | ||
df = gpd.GeoDataFrame.from_features(data) | ||
df.translate(xoff=offset[0], yoff=offset[1]) | ||
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"]) | ||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters