Skip to content

Commit

Permalink
raise helpful error message for transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
SiQube committed Sep 12, 2023
1 parent 2f7b54c commit e0646e6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/pymovements/gaze/gaze_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,30 @@ def transform(
_check_n_components(self.n_components)
kwargs['n_components'] = self.n_components

if transform_method.__name__ in {'pos2vel', 'pos2acc'}:
if 'position' not in self.frame.columns and 'position_column' not in kwargs:
if 'pixel' in self.frame.columns:
raise pl.exceptions.ColumnNotFoundError(
"Neither 'position' is in the columns of the dataframe: "
f'{self.frame.columns} nor is the position column specified. '
"Since the dataframe has a 'pixel' column, consider running "
f'pix2deg() before {transform_method.__name__}(). If you want '
'to calculate pixel transformations, you can do so by using '
f"{transform_method.__name__}(position_column='pixel').",
)
raise pl.exceptions.ColumnNotFoundError(
"Neither 'position' is in the columns of the dataframe: "
f'{self.frame.columns} nor is the position column specified.',
)
if transform_method.__name__ in {'pix2deg'}:
if 'pixel' not in self.frame.columns and 'pixel_column' not in kwargs:
raise pl.exceptions.ColumnNotFoundError(
"Neither 'position' is in the columns of the dataframe: "
f'{self.frame.columns} nor is the pixel column specified. '
'You can specify the pixel column via: '
f'{transform_method.__name__}(pixel_column=name_of_your_pixel_column).',
)

if self.trial_columns is None:
self.frame = self.frame.with_columns(transform_method(**kwargs))
else:
Expand Down
43 changes: 37 additions & 6 deletions tests/gaze/gaze_transform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,9 @@ def test_gaze_dataframe_pix2deg_creates_position_column(data, experiment, pixel_
},
pl.exceptions.ColumnNotFoundError,
(
'pixel\n\nError originated just after this operation:\nDF ["acceleration"]; '
'PROJECT */1 COLUMNS; SELECTION: "None"'
"Neither 'position' is in the columns of the dataframe: ['acceleration'] "
'nor is the pixel column specified. You can specify the pixel column via: '
'pix2deg(pixel_column=name_of_your_pixel_column).'
),
id='no_pixel_column',
),
Expand Down Expand Up @@ -609,8 +610,23 @@ def test_gaze_dataframe_pos2acc_creates_acceleration_column(data, experiment, po
},
pl.exceptions.ColumnNotFoundError,
(
'position\n\nError originated just after this operation:\nDF ["pixel"]; '
'PROJECT */1 COLUMNS; SELECTION: "None"'
"Neither 'position' is in the columns of the dataframe: ['pixel'] nor is the "
"position column specified. Since the dataframe has a 'pixel' column, "
'consider running pix2deg() before pos2acc(). If you want to calculate pixel '
"transformations, you can do so by using pos2acc(position_column='pixel')."
),
id='no_position_column',
),
pytest.param(
{
'data': pl.from_dict({'x': [0.1], 'y': [0.2]}),
'experiment': pm.Experiment(1024, 768, 38, 30, 60, 'center', 1000),
'acceleration_columns': ['x', 'y'],
},
pl.exceptions.ColumnNotFoundError,
(
"Neither 'position' is in the columns of the dataframe: ['acceleration'] nor "
'is the position column specified.'
),
id='no_position_column',
),
Expand Down Expand Up @@ -690,8 +706,23 @@ def test_gaze_dataframe_pos2vel_creates_velocity_column(data, experiment, positi
},
pl.exceptions.ColumnNotFoundError,
(
'position\n\nError originated just after this operation:\nDF ["pixel"]; '
'PROJECT */1 COLUMNS; SELECTION: "None"'
"Neither 'position' is in the columns of the dataframe: ['pixel'] nor is the "
"position column specified. Since the dataframe has a 'pixel' column, "
'consider running pix2deg() before pos2vel(). If you want to calculate pixel '
"transformations, you can do so by using pos2vel(position_column='pixel')."
),
id='no_position_column',
),
pytest.param(
{
'data': pl.from_dict({'x': [0.1], 'y': [0.2]}),
'experiment': pm.Experiment(1024, 768, 38, 30, 60, 'center', 1000),
'acceleration_columns': ['x', 'y'],
},
pl.exceptions.ColumnNotFoundError,
(
"Neither 'position' is in the columns of the dataframe: ['acceleration'] nor "
'is the position column specified.'
),
id='no_position_column',
),
Expand Down

0 comments on commit e0646e6

Please sign in to comment.