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

ghiggi / ximage / 11564978561

29 Oct 2024 12:29AM UTC coverage: 95.956% (-0.07%) from 96.029%
11564978561

Pull #21

github

web-flow
[pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/pre-commit/pre-commit-hooks: v4.5.0 → v5.0.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.5.0...v5.0.0)
- [github.com/astral-sh/ruff-pre-commit: v0.3.5 → v0.7.1](https://github.com/astral-sh/ruff-pre-commit/compare/v0.3.5...v0.7.1)
- [github.com/psf/black: 24.3.0 → 24.10.0](https://github.com/psf/black/compare/24.3.0...24.10.0)
- [github.com/pre-commit/mirrors-prettier: v3.1.0 → v4.0.0-alpha.8](https://github.com/pre-commit/mirrors-prettier/compare/v3.1.0...v4.0.0-alpha.8)
- [github.com/codespell-project/codespell: v2.2.6 → v2.3.0](https://github.com/codespell-project/codespell/compare/v2.2.6...v2.3.0)
- [github.com/asottile/pyupgrade: v3.15.2 → v3.19.0](https://github.com/asottile/pyupgrade/compare/v3.15.2...v3.19.0)
- [github.com/executablebooks/mdformat: 0.7.17 → 0.7.18](https://github.com/executablebooks/mdformat/compare/0.7.17...0.7.18)
- [github.com/nbQA-dev/nbQA: 1.8.5 → 1.8.7](https://github.com/nbQA-dev/nbQA/compare/1.8.5...1.8.7)
Pull Request #21: [pre-commit.ci] pre-commit autoupdate

783 of 816 relevant lines covered (95.96%)

0.96 hits per line

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

100.0
/ximage/patch/plot2d.py
1
# -----------------------------------------------------------------------------.
2
# MIT License
3

4
# Copyright (c) 2024 ximage developers
5
#
6
# This file is part of ximage.
7

8
# Permission is hereby granted, free of charge, to any person obtaining a copy
9
# of this software and associated documentation files (the "Software"), to deal
10
# in the Software without restriction, including without limitation the rights
11
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
# copies of the Software, and to permit persons to whom the Software is
13
# furnished to do so, subject to the following conditions:
14
#
15
# The above copyright notice and this permission notice shall be included in all
16
# copies or substantial portions of the Software.
17
#
18
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
# SOFTWARE.
25

26
# -----------------------------------------------------------------------------.
27
"""Utility functions to display the patch extraction process around labels."""
28
import matplotlib.pyplot as plt
1✔
29

30

31
def _plot_rectangle(ax, xlim, ylim, edgecolor="red", facecolor="None", **kwargs):
1✔
32
    """Plot rectangles from 2D patch list slices."""
33
    # Extract the start and stop values from the slice
34
    x_start, x_stop = xlim
1✔
35
    y_start, y_stop = ylim
1✔
36
    # Calculate the width and height of the rectangle
37
    width = x_stop - x_start
1✔
38
    height = y_stop - y_start
1✔
39
    # Plot rectangle
40
    rectangle = plt.Rectangle((x_start, y_start), width, height, edgecolor=edgecolor, facecolor=facecolor, **kwargs)
1✔
41
    ax.add_patch(rectangle)
1✔
42
    return ax
1✔
43

44

45
def _plot_xr_isel_dict_rectangle(ax, xr_obj, label_name, isel_dicts, edgecolor="red", facecolor="None", **kwargs):
1✔
46
    """Plot xarray 2D isel_dicts rectangles."""
47
    y, x = list(xr_obj[label_name].dims)
1✔
48
    for isel_dict in isel_dicts:
1✔
49
        xr_subset = xr_obj[label_name].isel(isel_dict)
1✔
50
        _ = _plot_rectangle(
1✔
51
            ax=ax,
52
            xlim=xr_subset[x].data[[0, -1]],
53
            ylim=xr_subset[y].data[[0, -1]],
54
            edgecolor=edgecolor,
55
            facecolor=facecolor,
56
            **kwargs,
57
        )
58

59

60
def _get_nice_extent_isel_dict(patches_isel_dicts, partitions_isel_dicts, shape_dict):
1✔
61
    # Retrieve name of dimensions
62
    y, x = list(patches_isel_dicts[0].keys())
1✔
63
    # Retrieve isel_dicts
64
    isel_dicts = patches_isel_dicts + partitions_isel_dicts
1✔
65
    # Get isel dict covering all isel_dicts
66
    subset_isel_dicts = {}
1✔
67
    for dim in [y, x]:
1✔
68
        min_start = min([isel_dict[dim].start for isel_dict in isel_dicts])
1✔
69
        max_stop = max([isel_dict[dim].stop for isel_dict in isel_dicts])
1✔
70
        # Extend a bit
71
        min_start = max(min_start - 2, 0)
1✔
72
        max_stop = min(max_stop + 2, shape_dict[dim])
1✔
73
        subset_isel_dicts[dim] = slice(min_start, max_stop)
1✔
74
    return subset_isel_dicts
1✔
75

76

77
def plot_label_patch_extraction_areas(xr_obj, label_name, patches_isel_dicts, partitions_isel_dicts, **kwargs):
1✔
78
    """Plot for debugging label patch extraction."""
79
    from ximage.labels.plot_labels import plot_labels
1✔
80

81
    # Get isel dict covering all isel_dicts
82
    shape_dict = {dim: xr_obj[dim].shape[0] for dim in xr_obj[label_name].dims}
1✔
83
    subset_isel_dicts = _get_nice_extent_isel_dict(patches_isel_dicts, partitions_isel_dicts, shape_dict=shape_dict)
1✔
84
    # Subset the label array to plot
85
    label_subset = xr_obj[label_name].isel(subset_isel_dicts)
1✔
86
    # Create figure
87
    fig, ax = plt.subplots()
1✔
88
    # Plot labels
89
    p = plot_labels(label_subset, ax=ax)
1✔
90
    p.axes.set_aspect("equal")
1✔
91
    # Plot partitions rectangles
92
    _ = _plot_xr_isel_dict_rectangle(
1✔
93
        ax=ax,
94
        xr_obj=xr_obj,
95
        label_name=label_name,
96
        isel_dicts=partitions_isel_dicts,
97
        edgecolor="black",
98
        facecolor="None",
99
        **kwargs,
100
    )
101
    # Plot patches rectangles
102
    _ = _plot_xr_isel_dict_rectangle(
1✔
103
        ax=ax,
104
        xr_obj=xr_obj,
105
        label_name=label_name,
106
        isel_dicts=patches_isel_dicts,
107
        edgecolor="red",
108
        facecolor="None",
109
        **kwargs,
110
    )
111
    return fig
1✔
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

© 2025 Coveralls, Inc