-
Notifications
You must be signed in to change notification settings - Fork 54
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
Feature: Identify object "families" using segmentation data #398
Open
freemansw1
wants to merge
5
commits into
tobac-project:RC_v1.5.x
Choose a base branch
from
freemansw1:family_id_15x
base: RC_v1.5.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
654c813
add initial family code to tobac
freemansw1 429ee42
add family finding
freemansw1 043e5d1
formatting and documentation
freemansw1 af77224
Merge remote-tracking branch 'upstream/RC_v1.5.x' into family_id_15x
freemansw1 95063e7
updates to decorator call
freemansw1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||||
---|---|---|---|---|---|---|
|
@@ -4,9 +4,13 @@ | |||||
|
||||||
import copy | ||||||
import logging | ||||||
from typing import Union | ||||||
|
||||||
import pandas as pd | ||||||
import skimage | ||||||
|
||||||
from . import internal as internal_utils | ||||||
from . import decorators | ||||||
import numpy as np | ||||||
import sklearn | ||||||
import sklearn.neighbors | ||||||
|
@@ -922,3 +926,88 @@ | |||||
ds["ProjectionCoordinateSystem"] = Projection | ||||||
|
||||||
return ds | ||||||
|
||||||
|
||||||
@decorators.iris_to_xarray | ||||||
def identify_feature_families( | ||||||
feature_df: pd.DataFrame, | ||||||
in_segmentation: xr.DataArray, | ||||||
return_grid: bool = False, | ||||||
family_column_name: str = "feature_family_id", | ||||||
unsegmented_point_values: int = 0, | ||||||
below_threshold_values: int = -1, | ||||||
) -> Union[tuple[pd.DataFrame, xr.DataArray], pd.DataFrame]: | ||||||
""" | ||||||
Function to identify families/storm systems by identifying where segmentation touches. | ||||||
At a given time, segmentation areas are considered part of the same family if they | ||||||
touch at any point. | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
feature_df: pd.DataFrame | ||||||
Input feature dataframe | ||||||
in_segmentation: xr.DataArray | ||||||
Input segmentation | ||||||
return_grid: bool | ||||||
Whether to return the segmentation grid showing families | ||||||
family_column_name: str | ||||||
The name in the output dataframe of the family ID | ||||||
unsegmented_point_values: int | ||||||
The value in the input segmentation for unsegmented but above threshold points | ||||||
below_threshold_values: int | ||||||
The value in the input segmentation for below threshold points | ||||||
|
||||||
Returns | ||||||
------- | ||||||
pd.DataFrame and xr.DataArray or pd.DataFrame | ||||||
Input dataframe with family IDs associated with each feature | ||||||
if return_grid is True, the segmentation grid showing families is | ||||||
also returned. | ||||||
|
||||||
""" | ||||||
|
||||||
# we need to label the data, but we currently label using skimage label, not dask label. | ||||||
|
||||||
# 3D should be 4-D (time, then 3 spatial). | ||||||
# 2D should be 3-D (time, then 2 spatial) | ||||||
is_3D = len(in_segmentation.shape) == 4 | ||||||
seg_family_dict = dict() | ||||||
out_families = copy.deepcopy(in_segmentation) | ||||||
|
||||||
for time_index in range(in_segmentation.shape[0]): | ||||||
in_arr = np.array(in_segmentation.values[time_index]) | ||||||
|
||||||
segmented_arr = np.logical_and( | ||||||
in_arr != unsegmented_point_values, in_arr != below_threshold_values | ||||||
) | ||||||
# These are our families | ||||||
family_labeled_data = skimage.measure.label( | ||||||
segmented_arr, | ||||||
) | ||||||
|
||||||
# now we need to note feature->family relationship in the dataframe. | ||||||
segmentation_props = skimage.measure.regionprops(in_arr) | ||||||
|
||||||
# associate feature ID -> family ID | ||||||
for seg_area in segmentation_props: | ||||||
if is_3D: | ||||||
seg_family = family_labeled_data[ | ||||||
seg_area.coords[0, 0], seg_area.coords[0, 1], seg_area.cords[0, 2] | ||||||
] | ||||||
else: | ||||||
seg_family = family_labeled_data[ | ||||||
seg_area.coords[0, 0], seg_area.coords[0, 1] | ||||||
] | ||||||
seg_family_dict[seg_area.label] = seg_family | ||||||
|
||||||
out_families[time_index] = segmented_arr | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Currently output grid is all ones for segmented area. |
||||||
|
||||||
family_series = pd.Series(seg_family_dict, name=family_column_name) | ||||||
feature_series = pd.Series({x: x for x in seg_family_dict.keys()}, name="feature") | ||||||
family_df = pd.concat([family_series, feature_series], axis=1) | ||||||
out_df = feature_df.merge(family_df, on="feature", how="inner") | ||||||
|
||||||
if return_grid: | ||||||
return out_df, out_families | ||||||
else: | ||||||
return out_df | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should offset the value of these labels by the previous highest feature family so that the families are unique over time