Skip to content

Commit 4c0620c

Browse files
jteijemaJ535D165
andauthored
Perfect line in recall plot (#65)
Co-authored-by: Jonathan de Bruin <[email protected]>
1 parent ccb8783 commit 4c0620c

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ result. The plot is saved using the matplotlib API.
287287

288288
```python
289289
import matplotlib.pyplot as plt
290-
291290
from asreview import open_state
291+
292292
from asreviewcontrib.insights.plot import plot_recall
293293

294294
with open_state("example.asreview") as s:
@@ -309,8 +309,8 @@ It's straightforward to customize the plots if you are familiar with
309309

310310
```python
311311
import matplotlib.pyplot as plt
312-
313312
from asreview import open_state
313+
314314
from asreviewcontrib.insights.plot import plot_wss
315315

316316
with open_state("example.asreview") as s:
@@ -332,8 +332,8 @@ knowledge is excluded from the plot.
332332

333333
```python
334334
import matplotlib.pyplot as plt
335-
336335
from asreview import open_state
336+
337337
from asreviewcontrib.insights.plot import plot_wss
338338

339339
with open_state("example.asreview") as s:
@@ -350,8 +350,8 @@ change this behavior. The arguments are identical for each plot function.
350350

351351
```python
352352
import matplotlib.pyplot as plt
353-
354353
from asreview import open_state
354+
355355
from asreviewcontrib.insights.plot import plot_wss
356356

357357
with open_state("example.asreview") as s:
@@ -362,7 +362,30 @@ with open_state("example.asreview") as s:
362362
fig.savefig("example_absolute_axis.png")
363363
```
364364

365-
![Recall with absolute axes](https://github.com/asreview/asreview-insights/blob/main/docs/example_absolute_axes.png)
365+
![Recall with absolute
366+
axes](https://github.com/asreview/asreview-insights/blob/main/docs/example_absolute_axes.png)
367+
368+
#### Example: Adjusting the Random and Perfect curves
369+
370+
By default, each plot will have a curve representing perfect performance, and a
371+
curve representing random sampling performance. Both curves can be removed from
372+
the graph.
373+
374+
```python
375+
import matplotlib.pyplot as plt
376+
from asreview import open_state
377+
378+
from asreviewcontrib.insights.plot import plot_recall
379+
380+
with open_state("example.asreview") as s:
381+
382+
fig, ax = plt.subplots()
383+
plot_recall(ax, s, show_random=False, show_perfect=False)
384+
385+
fig.savefig("example_without_curves.png")
386+
```
387+
388+
![Recall with absolute axes](https://github.com/asreview/asreview-insights/blob/main/docs/example_without_curves.png)
366389

367390

368391
#### Example: Legend for multiple curves in one plot

asreviewcontrib/insights/plot.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def plot_recall(
1313
x_absolute=False,
1414
y_absolute=False,
1515
show_random=True,
16+
show_perfect=True,
1617
show_legend=True,
1718
legend_values=None,
1819
legend_kwargs=None,
@@ -34,6 +35,8 @@ def plot_recall(
3435
If False, the fraction of all included records found is on the y-axis.
3536
show_random: bool
3637
Show the random curve in the plot.
38+
show_perfect: bool
39+
Show the perfect curve in the plot.
3740
show_legend: bool
3841
If state_obj contains multiple states, show a legend in the plot.
3942
legend_values: list[str]
@@ -61,6 +64,7 @@ def plot_recall(
6164
x_absolute=x_absolute,
6265
y_absolute=y_absolute,
6366
show_random=show_random,
67+
show_perfect=show_perfect,
6468
show_legend=show_legend,
6569
legend_values=legend_values,
6670
legend_kwargs=legend_kwargs,
@@ -237,6 +241,7 @@ def _plot_recall(
237241
x_absolute=False,
238242
y_absolute=False,
239243
show_random=True,
244+
show_perfect=True,
240245
show_legend=True,
241246
legend_values=None,
242247
legend_kwargs=None,
@@ -258,7 +263,10 @@ def _plot_recall(
258263
ax = _add_recall_info(ax, labels, x_absolute, y_absolute)
259264

260265
if show_random:
261-
ax = _add_random_curve(ax, labels, x_absolute, y_absolute)
266+
ax = _add_random_curve(ax, labels, x_absolute, y_absolute)
267+
268+
if show_perfect:
269+
ax = _add_perfect_curve(ax, labels, x_absolute, y_absolute)
262270

263271
if show_legend:
264272
if legend_kwargs is None:
@@ -398,6 +406,33 @@ def _add_random_curve(ax, labels, x_absolute, y_absolute):
398406
return ax
399407

400408

409+
def _add_perfect_curve(ax, labels, x_absolute, y_absolute):
410+
"""Add a perfect curve to a plot using step-wise increments.
411+
412+
Returns
413+
-------
414+
plt.axes.Axes
415+
Axes with perfect curve added.
416+
"""
417+
# get total amount of positive labels
418+
if isinstance(labels[0], list):
419+
n_pos_docs = max(sum(label_set) for label_set in labels)
420+
n_docs = max(len(label_set) for label_set in labels)
421+
else:
422+
n_pos_docs = sum(labels)
423+
n_docs = len(labels)
424+
425+
# Create x and y arrays for step plot
426+
x = np.arange(0, n_pos_docs + 1) if x_absolute else np.arange(0, n_pos_docs + 1) / n_docs # noqa: E501
427+
y = np.arange(0, n_pos_docs + 1) if y_absolute else np.arange(0, n_pos_docs + 1) / n_pos_docs # noqa: E501
428+
429+
# Plot the stepwise perfect curve
430+
ax.step(x, y, color="grey", where="post")
431+
432+
return ax
433+
434+
435+
401436
def _add_wss_curve(ax, labels, x_absolute=False, y_absolute=False, legend_label=None):
402437
x, y = _wss_values(labels, x_absolute=x_absolute, y_absolute=y_absolute)
403438
ax.step(x, y, where="post", label=legend_label)

docs/example_without_curves.png

15.1 KB
Loading

docs/example_without_curves.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import matplotlib.pyplot as plt
2+
from asreview import open_state
3+
4+
from asreviewcontrib.insights.plot import plot_recall
5+
6+
with open_state("tests/asreview_files/sim_van_de_schoot_2017_logistic.asreview") as s:
7+
fig, ax = plt.subplots()
8+
plot_recall(ax, s, show_random=False, show_perfect=False)
9+
10+
fig.savefig("docs/example_without_curves.png")

0 commit comments

Comments
 (0)