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

Split aoi dataframe by chosen criteria #879

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
18 changes: 18 additions & 0 deletions src/pymovements/stimulus/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ def __init__(
self.end_y_column = end_y_column
self.page_column = page_column

def split_aois_by(
dkrako marked this conversation as resolved.
Show resolved Hide resolved
self,
by: str,
) -> list[TextStimulus]:
"""Split the AOI df.

Parameters
----------
by: str
Splitting criteria.

Returns
-------
list[TextStimulus]
A list of TextStimulus objects.
"""
return self.aois.partition_by(by=by, as_dict=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must have missed this in my review two weeks ago.

this line returns a list of polars.DataFrame not a list of TextStimulus objects.

You should probably do something like this:

return [
    TextStimulus(
        aois=df,
        aoi_column=self.aoi_column,
        width_column=self.width_column,
        ...,  # remaining fields
        page_column=self.page_column,
    )
    for df in self.aois.partition_by(by=by, as_dict=False)
]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SiQube I am confused why mypy didn't complain here, as I would expect a type mismatch. I am clueless. Do you have an explanation? I am a bit worried that there's something wrong in our CI.



def from_file(
aoi_path: str | Path,
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/stimulus/text_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,34 @@ def test_text_stimulus_unsupported_format():
expected = 'unsupported file format ".pickle".Supported formats are: '\
'[\'.csv\', \'.ias\', \'.tsv\', \'.txt\']'
assert msg == expected


@pytest.mark.parametrize(
('aoi_file', 'custom_read_kwargs'),
[
pytest.param(
'tests/files/toy_text_1_1_aoi.csv',
None,
id='toy_text_1_1_aoi',
),
pytest.param(
Path('tests/files/toy_text_1_1_aoi.csv'),
{'separator': ','},
id='toy_text_1_1_aoi',
Copy link
Contributor

@dkrako dkrako Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid having the same id for different test parametrizations.

you could use something like: toy_text_1_1_aoi_sep

),
],
)
def test_text_stimulus_splitting(aoi_file, custom_read_kwargs):
aois_df = pm.stimulus.text.from_file(
aoi_file,
aoi_column='char',
start_x_column='top_left_x',
start_y_column='top_left_y',
width_column='width',
height_column='height',
page_column='page',
custom_read_kwargs=custom_read_kwargs,
)

aois_df = aois_df.split_aois_by(by='line_idx')
assert len(aois_df) == 2
dkrako marked this conversation as resolved.
Show resolved Hide resolved
Loading