Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Raise helpful missing column error message for transforms (#542) #542

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/pymovements/gaze/gaze_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,33 @@ 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. '
dkrako marked this conversation as resolved.
Show resolved Hide resolved
"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'). "
f'Available dataframe columns are {self.frame.columns}',
)
raise pl.exceptions.ColumnNotFoundError(
"Neither 'position' is in the columns of the dataframe: "
dkrako marked this conversation as resolved.
Show resolved Hide resolved
f'{self.frame.columns} nor is the position column specified. '
f'Available dataframe columns are {self.frame.columns}',
)
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"). '
f'Available dataframe columns are {self.frame.columns}',
)

if self.trial_columns is None:
self.frame = self.frame.with_columns(transform_method(**kwargs))
else:
Expand Down
48 changes: 42 additions & 6 deletions tests/gaze/gaze_transform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,10 @@ 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"). Available dataframe '
"columns are ['acceleration']"
),
id='no_pixel_column',
),
Expand Down Expand Up @@ -609,8 +611,25 @@ 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'). "
"Available dataframe columns are ['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. Available dataframe columns are '
"['acceleration']"
),
id='no_position_column',
),
Expand Down Expand Up @@ -690,8 +709,25 @@ 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'). "
"Available dataframe columns are ['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. Available dataframe columns are '
"['acceleration']"
),
id='no_position_column',
),
Expand Down