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

Plotting multiple statefiles dynamically. #57

Open
jteijema opened this issue Sep 6, 2023 · 2 comments
Open

Plotting multiple statefiles dynamically. #57

jteijema opened this issue Sep 6, 2023 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@jteijema
Copy link
Member

jteijema commented Sep 6, 2023

The example for the new multiple plots code is as follows:

import matplotlib.pyplot as plt

from asreview import open_state
from asreviewcontrib.insights.plot import plot_recall


fig, ax = plt.subplots()

with open_state("tests/asreview_files/sim_van_de_schoot_2017_1.asreview") as s1:
    with open_state("tests/asreview_files/"
                    "sim_van_de_schoot_2017_logistic.asreview") as s2:
        plot_recall(ax,
                    [s1, s2],
                    legend_values=["Naive Bayes", "Logistic"],
                    legend_kwargs={'loc': 'lower center'})

fig.savefig("docs/example_multiple_lines.png")

However, how would one do this dynamically? open_state() method is returning a '_GeneratorContextManager' object, which implies that it is intended to be used as a context manager with the with statement. However, I can't dynamically open a cascading with statement, and I can't store the state for later using:

  for state_file in states:
      state = open_state(state_file)
      state_objs.append(state)

  plot_recall(ax, state_objs, **plot_kwargs)

Any advise for this implementation? I think I cannot manually manage the lifetime of the state objects and they must be used within a with block, and thus I am forced to call plot_recall separately.

@J535D165
Copy link
Member

J535D165 commented Sep 6, 2023

Interesting question. It would be easier if the state class had an open and close method similar to Python's open. We have .close(), but .open() might be useful as well.

For your application, did you try:

def _iter_states(file_paths):
"""Get a generator of state objects from their filepaths.
Parameters
----------
file_paths : list[Path]
List of filepaths of states.
Yields
------
asreview.state.BaseState
State at given filepath.
"""
for fp in file_paths:
with open_state(fp) as s:
yield s

@PeterLombaers
Copy link
Member

You can copy how the entrypoint does it, indeed using _iter_states. If we actually use _iter_states in one of the examples, we should make it public though. Also, I guess the docstring of the various plot functions could be improved, to say (generator of) asreview.state.SQLiteState instead of (list of) asreview.state.SQLiteState.

@J535D165 J535D165 added the question Further information is requested label Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants