Repository: aeye-lab/pymovements
Pull request #661 (2024-01-12 12:50:18)

chore: improve exception messages in GazeDataFrame.transform()


Patch:
diff --git a/src/pymovements/gaze/gaze_dataframe.py b/src/pymovements/gaze/gaze_dataframe.py
index 336f9a8..358c2df 100644
--- a/src/pymovements/gaze/gaze_dataframe.py
+++ b/src/pymovements/gaze/gaze_dataframe.py
@@ -335,27 +335,29 @@ class GazeDataFrame:
                 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. '
+                            "Neither is 'position' in the dataframe columns, "
+                            'nor is a position column explicitly 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 '
+                            'to run transformations in pixel units, you can do so by using '
                             f"{transform_method.__name__}(position_column='pixel'). "
-                            f'Available dataframe columns are {self.frame.columns}',
+                            f'Available dataframe columns are: {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. '
-                        f'Available dataframe columns are {self.frame.columns}',
+                        "Neither is 'position' in the dataframe columns, "
+                        'nor is a position column explicitly specified. '
+                        'You can specify the position column via: '
+                        f'{transform_method.__name__}(position_column="your_position_column"). '
+                        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. '
+                        "Neither is 'pixel' in the dataframe columns, "
+                        'nor is a pixel column explicitly 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}',
+                        f'Available dataframe columns are: {self.frame.columns}',
                     )
 
             if self.trial_columns is None:


Test patch:
diff --git a/tests/unit/gaze/gaze_transform_test.py b/tests/unit/gaze/gaze_transform_test.py
index 1f763fa..4343738 100644
--- a/tests/unit/gaze/gaze_transform_test.py
+++ b/tests/unit/gaze/gaze_transform_test.py
@@ -795,10 +795,11 @@ def test_gaze_dataframe_pix2deg_creates_position_column(data, experiment, pixel_
             },
             pl.exceptions.ColumnNotFoundError,
             (
-                "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']"
+                "Neither is 'pixel' in the dataframe columns, "
+                'nor is a pixel column explicitly 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',
         ),
@@ -930,13 +931,14 @@ def test_gaze_dataframe_pos2acc_creates_acceleration_column(data, experiment, po
             },
             pl.exceptions.ColumnNotFoundError,
             (
-                "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']"
+                "Neither is 'position' in the dataframe columns, "
+                'nor is a position column explicitly specified. '
+                "Since the dataframe has a 'pixel' column, "
+                'consider running pix2deg() before pos2acc(). If you want to run transformations '
+                "in pixel units, you can do so by using pos2acc(position_column='pixel'). "
+                "Available dataframe columns are: ['pixel']"
             ),
-            id='no_position_column',
+            id='no_position_column_but_has_pixel_column',
         ),
         pytest.param(
             {
@@ -946,9 +948,11 @@ def test_gaze_dataframe_pos2acc_creates_acceleration_column(data, experiment, po
             },
             pl.exceptions.ColumnNotFoundError,
             (
-                "Neither 'position' is in the columns of the dataframe: ['acceleration'] nor "
-                'is the position column specified. Available dataframe columns are '
-                "['acceleration']"
+                "Neither is 'position' in the dataframe columns, "
+                'nor is a position column explicitly specified. '
+                'You can specify the position column via: '
+                'pos2acc(position_column="your_position_column"). '
+                "Available dataframe columns are: ['acceleration']"
             ),
             id='no_position_column',
         ),
@@ -1028,13 +1032,14 @@ def test_gaze_dataframe_pos2vel_creates_velocity_column(data, experiment, positi
             },
             pl.exceptions.ColumnNotFoundError,
             (
-                "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']"
+                "Neither is 'position' in the dataframe columns, "
+                'nor is a position column explicitly specified. '
+                "Since the dataframe has a 'pixel' column, "
+                'consider running pix2deg() before pos2vel(). If you want to run transformations '
+                "in pixel units, you can do so by using pos2vel(position_column='pixel'). "
+                "Available dataframe columns are: ['pixel']"
             ),
-            id='no_position_column',
+            id='no_position_column_but_has_pixel_column',
         ),
         pytest.param(
             {
@@ -1044,9 +1049,11 @@ def test_gaze_dataframe_pos2vel_creates_velocity_column(data, experiment, positi
             },
             pl.exceptions.ColumnNotFoundError,
             (
-                "Neither 'position' is in the columns of the dataframe: ['acceleration'] nor "
-                'is the position column specified. Available dataframe columns are '
-                "['acceleration']"
+                "Neither is 'position' in the dataframe columns, "
+                'nor is a position column explicitly specified. '
+                'You can specify the position column via: '
+                'pos2vel(position_column="your_position_column"). '
+                "Available dataframe columns are: ['acceleration']"
             ),
             id='no_position_column',
         ),