From 2afecda611284d4429be8b1a9915e516988f4fd3 Mon Sep 17 00:00:00 2001 From: "Daniel G. Krakowczyk" Date: Fri, 26 May 2023 12:04:47 +0200 Subject: [PATCH] refactor(events): Rename position property to location (#423) --- src/pymovements/events/__init__.py | 2 +- src/pymovements/events/event_properties.py | 60 +++++++++++----------- tests/dataset/dataset_test.py | 8 +-- tests/events/event_properties_test.py | 22 ++++---- 4 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/pymovements/events/__init__.py b/src/pymovements/events/__init__.py index 9151a9da0..2bae2beed 100644 --- a/src/pymovements/events/__init__.py +++ b/src/pymovements/events/__init__.py @@ -58,7 +58,7 @@ pymovements.events.event_properties.dispersion pymovements.events.event_properties.disposition pymovements.events.event_properties.peak_velocity - pymovements.events.event_properties.position + pymovements.events.event_properties.location """ from pymovements.events.detection.idt import idt from pymovements.events.detection.ivt import ivt diff --git a/src/pymovements/events/event_properties.py b/src/pymovements/events/event_properties.py index 19212f669..b61fd826e 100644 --- a/src/pymovements/events/event_properties.py +++ b/src/pymovements/events/event_properties.py @@ -135,41 +135,13 @@ def duration() -> pl.Expr: @register_event_property -def peak_velocity( - *, - velocity_column: str = 'velocity', - n_components: int = 2, -) -> pl.Expr: - """Peak velocity of an event. - - Parameters - ---------- - velocity_column - The column name of the velocity tuples. - n_components: - Number of positional components. Usually these are the two components yaw and pitch. - - Raises - ------ - ValueError - If number of components is not 2. - """ - _check_has_two_componenents(n_components) - - x_velocity = pl.col(velocity_column).arr.get(0) - y_velocity = pl.col(velocity_column).arr.get(1) - - return (x_velocity.pow(2) + y_velocity.pow(2)).sqrt().max() - - -@register_event_property -def position( +def location( method: str = 'mean', *, position_column: str = 'position', n_components: int = 2, ) -> pl.Expr: - """Centroid position of an event. + """Location of an event. Parameters ---------- @@ -212,6 +184,34 @@ def position( return pl.concat_list(component_expressions).first() +@register_event_property +def peak_velocity( + *, + velocity_column: str = 'velocity', + n_components: int = 2, +) -> pl.Expr: + """Peak velocity of an event. + + Parameters + ---------- + velocity_column + The column name of the velocity tuples. + n_components: + Number of positional components. Usually these are the two components yaw and pitch. + + Raises + ------ + ValueError + If number of components is not 2. + """ + _check_has_two_componenents(n_components) + + x_velocity = pl.col(velocity_column).arr.get(0) + y_velocity = pl.col(velocity_column).arr.get(1) + + return (x_velocity.pow(2) + y_velocity.pow(2)).sqrt().max() + + def _check_has_two_componenents(n_components: int) -> None: """Check that number of componenents is two.""" if n_components != 2: diff --git a/tests/dataset/dataset_test.py b/tests/dataset/dataset_test.py index f40899bbf..41880e577 100644 --- a/tests/dataset/dataset_test.py +++ b/tests/dataset/dataset_test.py @@ -1188,12 +1188,12 @@ def test_event_dataframe_add_property_has_expected_height(dataset_configuration, id='single_event_peak_velocity', ), pytest.param( - {'event_properties': 'position'}, + {'event_properties': 'location'}, { 'subject_id': pl.Int64, **pm.events.EventDataFrame._minimal_schema, 'duration': pl.Int64, - 'position': pl.List(pl.Float64), + 'location': pl.List(pl.Float64), }, id='single_event_position', ), @@ -1222,8 +1222,8 @@ def test_event_dataframe_add_property_has_expected_schema( id='single_event_peak_velocity', ), pytest.param( - {'event_properties': 'position'}, - ['position'], + {'event_properties': 'location'}, + ['location'], id='single_event_position', ), ], diff --git a/tests/events/event_properties_test.py b/tests/events/event_properties_test.py index c97ce5229..5e45e9d21 100644 --- a/tests/events/event_properties_test.py +++ b/tests/events/event_properties_test.py @@ -38,7 +38,7 @@ id='peak_velocity_not_2_components_raise_value_error', ), pytest.param( - event_properties.position, + event_properties.location, {'method': 'foo'}, ValueError, ('method', 'foo', 'not', 'supported', 'mean', 'median'), @@ -352,41 +352,41 @@ def test_property_exceptions(event_property, init_kwargs, input_df, exception, m id='amplitude_two_samples_xy_move', ), pytest.param( - event_properties.position, + event_properties.location, {'method': 'mean'}, pl.DataFrame( {'position': [[0, 0], [1, 0]]}, schema={'position': pl.List(pl.Float64)}, ), pl.DataFrame( - {'position': [[0.5, 0]]}, - schema={'position': pl.List(pl.Float64)}, + {'location': [[0.5, 0]]}, + schema={'location': pl.List(pl.Float64)}, ), id='position_two_samples_mean', ), pytest.param( - event_properties.position, + event_properties.location, {'method': 'mean'}, pl.DataFrame( {'position': [[0, 0], [0, 1], [0, 3]]}, schema={'position': pl.List(pl.Float64)}, ), pl.DataFrame( - {'position': [[0, 1.3333333333333333]]}, - schema={'position': pl.List(pl.Float64)}, + {'location': [[0, 1.3333333333333333]]}, + schema={'location': pl.List(pl.Float64)}, ), id='position_three_samples_mean', ), pytest.param( - event_properties.position, + event_properties.location, {'method': 'median'}, pl.DataFrame( {'position': [[0, 0], [2, 1], [3, 3]]}, schema={'position': pl.List(pl.Float64)}, ), pl.DataFrame( - {'position': [[2, 1]]}, - schema={'position': pl.List(pl.Float64)}, + {'location': [[2, 1]]}, + schema={'location': pl.List(pl.Float64)}, ), id='position_three_samples_median', ), @@ -407,7 +407,7 @@ def test_property_has_expected_result(event_property, init_kwargs, input_df, exp pytest.param(event_properties.dispersion, 'dispersion', id='dispersion'), pytest.param(event_properties.amplitude, 'amplitude', id='amplitude'), pytest.param(event_properties.disposition, 'disposition', id='disposition'), - pytest.param(event_properties.position, 'position', id='position'), + pytest.param(event_properties.location, 'location', id='location'), ], ) def test_property_registered(property_function, property_function_name):