Skip to content

Commit

Permalink
Add prepend_col_name for tags (#47)
Browse files Browse the repository at this point in the history
* add prepend_col_name

* add test

* add changelog entry
  • Loading branch information
bdpedigo authored Aug 26, 2024
1 parent e214a5c commit d6fd57e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This project attempts to follow [Semantic Versioning](https://semver.org) and uses [Keep-a-Changelog formatting](https://keepachangelog.com/en/1.0.0/). But I make mistakes sometimes.

## [3.4.0] - 2024-08-08

### Added

- **SegmentProperties** New option `prepend_col_name` in `SegmentProperties.from_dataframe` will prepend the column name to the tag values.

## [3.3.7] - 2024-07-24

### Added
Expand Down
13 changes: 11 additions & 2 deletions src/nglui/segmentprops/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ def _tag_descriptions(tags, tag_descriptions):
return [tag_descriptions.get(tag, tag) for tag in tags]


def _make_tag_map(df, value_columns, bool_columns, allow_disambiguation):
def _make_tag_map(df, value_columns, bool_columns, allow_disambiguation, prepend_col_name):
if prepend_col_name:
for col in value_columns:
df[col] = df[col].apply(lambda x: f"{col}:{x}")
unique_tags = {}
for col in value_columns:
col_tags = df[col].unique()
Expand Down Expand Up @@ -295,6 +298,7 @@ def _make_tag_property(
tag_descriptions,
name="tags",
allow_disambiguation=True,
prepend_col_name=False,
):
if value_columns is None:
value_columns = []
Expand All @@ -305,6 +309,7 @@ def _make_tag_property(
value_columns,
bool_columns,
allow_disambiguation,
prepend_col_name,
)
tag_values = _generate_tag_values(tag_df, value_columns, bool_columns, tag_map)
return TagProperty(
Expand Down Expand Up @@ -412,6 +417,7 @@ def from_dataframe(
allow_disambiguation: bool = True,
label_separator: str = "_",
label_format_map: Optional[str] = None,
prepend_col_name: bool = False,
):
"""Generate a segment property object from a pandas dataframe based on column
Expand Down Expand Up @@ -450,7 +456,9 @@ def from_dataframe(
via the "format" function, replacing the column names in `{..}` with the values.
For example, "{cell_class}: {cell_type}_{region}" would pluck values from the columns
"cell_class", "cell_type", and "region". Label columns will be ignored and the format string is not validated.
prepend_col_name : bool, optional
If True, will prepend the column name to tag values, by default False.
Returns
-------
SegmentProperties
Expand Down Expand Up @@ -506,6 +514,7 @@ def from_dataframe(
tag_bool_cols,
tag_descriptions,
allow_disambiguation=allow_disambiguation,
prepend_col_name=prepend_col_name,
)
return cls(ids, **properties)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_segment_props.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ def test_categorical_props(test_categorical_df):
assert p_dict["inline"]["properties"][2]["data_type"] == "int32"


def test_prepend_col_name(test_categorical_df):
props = SegmentProperties.from_dataframe(
test_categorical_df,
id_col="seg_id",
label_col="seg_id",
number_cols=["number_int", "number_float"],
tag_value_cols="cell_type",
tag_bool_cols=["tag_a", "tag_b"],
prepend_col_name=True,
)

assert len(props) == 100
p_dict = props.to_dict()
assert "cell_type:ct_a" == p_dict["inline"]["properties"][1]["tags"][2]


def test_segment_props_nulls(test_null_df):
props = SegmentProperties.from_dataframe(
test_null_df,
Expand Down

0 comments on commit d6fd57e

Please sign in to comment.