Skip to content

Commit

Permalink
Props to df (#35)
Browse files Browse the repository at this point in the history
* add to dataframe method

* update changelog
  • Loading branch information
ceesem authored Jul 11, 2024
1 parent 4a1e6f4 commit a204e24
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 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.2.1] - 2024-07-11

### Added

- **SegmentProperties** : Added a `.to_dataframe()` method for segment properties to convert them back to a dataframe.

## [3.2.0] - 2024-07-10

### Added
Expand Down
27 changes: 27 additions & 0 deletions src/nglui/segmentprops/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ def sort_tag_arrays(x: list) -> list:
return [sorted(y) for y in x]


def is_null_value(value):
if value is None:
return True
elif pd.isna(value):
return True
elif value == "":
return True
else:
return False


@attrs.define
class InlineProperties:
ids = attrs.field(type=list[int], converter=list_of_strings, kw_only=True)
Expand Down Expand Up @@ -288,6 +299,22 @@ def to_dict(self):
self._property_list(),
)

def to_dataframe(self):
"Converts the segment properties to a pandas dataframe"
df_dict = {"ids": self.ids}
for prop in self._property_list():
if (
isinstance(prop, LabelProperty)
or isinstance(prop, DescriptionProperty)
or isinstance(prop, StringProperty)
or isinstance(prop, NumberProperty)
):
df_dict[prop.id] = prop.values
elif isinstance(prop, TagProperty):
for ii, tag in enumerate(prop.tags):
df_dict[tag] = [ii in tags for tags in prop.values]
return pd.DataFrame(df_dict)

@classmethod
def from_dataframe(
cls,
Expand Down
82 changes: 82 additions & 0 deletions tests/test_segment_props.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,81 @@ def test_df():
)


@pytest.fixture
def test_segprops():
return {
"@type": "neuroglancer_segment_properties",
"inline": {
"ids": ["44", "70", "77", "38", "68", "2", "19", "41", "67", "3"],
"properties": [
{
"id": "label",
"type": "label",
"values": [
"44",
"70",
"77",
"38",
"68",
"2",
"19",
"41",
"67",
"3",
],
},
{
"id": "tags",
"type": "tags",
"tags": ["ct_a", "ct_b", "ct_c", "tag_a", "tag_b"],
"tag_descriptions": [
"ct_a",
"ct_b",
"ct_c",
"The first tag",
"The second tag",
],
"values": [
[1, 4],
[2, 4],
[2, 4],
[1, 4],
[2, 4],
[0, 4],
[0, 4],
[1, 4],
[2, 4],
[0, 4],
],
},
{
"id": "number_int",
"type": "number",
"values": [344, 370, 377, 338, 368, 302, 319, 341, 367, 303],
"data_type": "int32",
},
{
"id": "number_float",
"type": "number",
"values": [
344.1000061035156,
370.1000061035156,
377.1000061035156,
338.1000061035156,
368.1000061035156,
302.1000061035156,
319.1000061035156,
341.1000061035156,
367.1000061035156,
303.1000061035156,
],
"data_type": "float32",
},
],
},
}


def test_segment_props(test_df):
props = SegmentProperties.from_dataframe(
test_df,
Expand All @@ -38,3 +113,10 @@ def test_segment_props(test_df):

rh_props = SegmentProperties.from_dict(p_dict)
assert len(rh_props) == 100


def test_property_conversion(test_segprops):
props = SegmentProperties.from_dict(test_segprops)
assert len(props) == 10
prop_df = props.to_dataframe()
assert len(prop_df.columns) == 9

0 comments on commit a204e24

Please sign in to comment.