Skip to content

Commit

Permalink
refactor region id selection and mask label construction in r2d.mask
Browse files Browse the repository at this point in the history
  • Loading branch information
ctuguinay committed Apr 1, 2024
1 parent c8f4da8 commit 68bad89
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions echoregions/regions2d/regions2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ def select_region(
if isinstance(region_id, (float, int, str)):
region_id = [region_id]
elif isinstance(region_id, list):
if len(region_id) == 0:
raise ValueError("region_id list is empty. Cannot be empty.")
for value in region_id:
if not isinstance(value, (float, int, str)):
raise TypeError(
Expand Down Expand Up @@ -483,36 +485,9 @@ def mask(
region_contours : pd.DataFrame
DataFrame containing region_id, depth, and time.
"""
if isinstance(region_id, list):
if len(region_id) == 0:
raise ValueError("region_id list is empty. Cannot be empty.")
mask_label_region_ids = region_id
elif region_id is None:
# Extract all region_id values
mask_label_region_ids = self.data.region_id.astype(int).to_list()
else:
raise TypeError(
f"region_id must be of type list. Currently is of type {type(region_id)}"
)

if mask_labels is None:
# Create mask_labels with each region_id as a key and values starting from 0
mask_labels = {key: idx for idx, key in enumerate(mask_label_region_ids)}

# Check that region_id and mask_labels are of the same size
if len(set(mask_label_region_ids) - set(mask_labels.keys())) > 0:
raise ValueError(
"Each region_id' must be a key in 'mask_labels'. "
"If you would prefer 0 based indexing as values for mask_labels, leave "
"mask_labels as None."
)

# Dataframe containing region information.
region_df = self.select_region(region_id, region_class)

# Select only important columns
region_df = region_df[["region_id", "time", "depth"]]

# Filter for rows with depth values within self min and self max depth and
# for rows that have positive depth.
region_df = region_df[
Expand All @@ -529,6 +504,23 @@ def mask(
"between min_depth and max_depth."
)
else:
# Grab subset region ids
subset_region_ids = region_df.region_id.astype(int).to_list()

if mask_labels is None:
# Create mask_labels with each subset_region_ids as a key and values starting from 0
mask_labels = {key: idx for idx, key in enumerate(subset_region_ids)}

# Check that subset_region_ids and mask_labels are of the same size
if len(set(subset_region_ids) - set(mask_labels.keys())) > 0:
raise ValueError(

Check warning on line 516 in echoregions/regions2d/regions2d.py

View check run for this annotation

Codecov / codecov/patch

echoregions/regions2d/regions2d.py#L516

Added line #L516 was not covered by tests
"Each value in subset_region_ids must be a key in 'mask_labels'. "
"If you would prefer 0 based indexing as values for mask_labels, leave "
"mask_labels as None."
)
# Select only important columns
region_df = region_df[["region_id", "time", "depth"]]

# Organize the regions in a format for region mask.
df = region_df.explode(["time", "depth"])

Expand Down

0 comments on commit 68bad89

Please sign in to comment.