• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bethgelab / foolbox / 8139141456

04 Mar 2024 11:03AM UTC coverage: 37.923% (-60.6%) from 98.477%
8139141456

Pull #722

github

web-flow
Merge 5663238db into 17e0e9b31
Pull Request #722: Fix guide compilation

1344 of 3544 relevant lines covered (37.92%)

0.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.7
/foolbox/plot.py
1
from typing import Tuple, Any, Optional
1✔
2
import numpy as np
1✔
3
import eagerpy as ep
1✔
4

5

6
def images(
1✔
7
    images: Any,
8
    *,
9
    n: Optional[int] = None,
10
    data_format: Optional[str] = None,
11
    bounds: Tuple[float, float] = (0, 1),
12
    ncols: Optional[int] = None,
13
    nrows: Optional[int] = None,
14
    figsize: Optional[Tuple[float, float]] = None,
15
    scale: float = 1,
16
    **kwargs: Any,
17
) -> None:
18
    import matplotlib.pyplot as plt
×
19

20
    x: ep.Tensor = ep.astensor(images)
×
21
    if x.ndim != 4:
×
22
        raise ValueError(
×
23
            "expected images to have four dimensions: (N, C, H, W) or (N, H, W, C)"
24
        )
25
    if n is not None:
×
26
        x = x[:n]
×
27
    if data_format is None:
×
28
        channels_first = x.shape[1] == 1 or x.shape[1] == 3
×
29
        channels_last = x.shape[-1] == 1 or x.shape[-1] == 3
×
30
        if channels_first == channels_last:
×
31
            raise ValueError("data_format ambigous, please specify it explicitly")
×
32
    else:
33
        channels_first = data_format == "channels_first"
×
34
        channels_last = data_format == "channels_last"
×
35
        if not channels_first and not channels_last:
×
36
            raise ValueError(
×
37
                "expected data_format to be 'channels_first' or 'channels_last'"
38
            )
39
    assert channels_first != channels_last
×
40
    x_np = x.numpy()
×
41
    if channels_first:
×
42
        x_np = np.transpose(x_np, axes=(0, 2, 3, 1))
×
43
    min_, max_ = bounds
×
44
    x_np = (x_np - min_) / (max_ - min_)
×
45

46
    if nrows is None and ncols is None:
×
47
        nrows = 1
×
48
    if ncols is None:
×
49
        assert nrows is not None
×
50
        ncols = (len(x_np) + nrows - 1) // nrows
×
51
    elif nrows is None:
×
52
        nrows = (len(x_np) + ncols - 1) // ncols
×
53
    if figsize is None:
×
54
        figsize = (ncols * scale, nrows * scale)
×
55
    fig, axes = plt.subplots(
×
56
        ncols=ncols,
57
        nrows=nrows,
58
        figsize=figsize,
59
        squeeze=False,
60
        constrained_layout=True,
61
        **kwargs,
62
    )
63

64
    for row in range(nrows):
×
65
        for col in range(ncols):
×
66
            ax = axes[row][col]
×
67
            ax.set_xticks([])
×
68
            ax.set_yticks([])
×
69
            ax.axis("off")
×
70
            i = row * ncols + col
×
71
            if i < len(x):
×
72
                if x_np.shape[-1] == 1:
×
73
                    ax.imshow(x_np[i][:, :, 0])
×
74
                else:
75
                    ax.imshow(x_np[i])
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc