File: api.py

package info (click to toggle)
python-matplotlib-venn 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 340 kB
  • sloc: python: 1,514; makefile: 8
file content (71 lines) | stat: -rw-r--r-- 2,254 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Specification of the "layout algorithm" interface.

A layout algorithm is a method for mapping from subset sizes to circle centers, radii and label locations.

Copyright 2024, Konstantin Tretyakov.
http://kt.era.ee/

Licensed under MIT license.
"""

from typing import Any, Dict, Sequence, Optional
from abc import ABC, abstractmethod
from matplotlib_venn._math import Point2D

SubsetSizes = Sequence[float]  # .. of length 3 for venn2 and length 7 for venn3

# Failures that may be reported from the layout algorithm.
class LayoutException(Exception):
    pass

class LabelLayout:
    """Text label position in the diagram.

    Given via coordinates and a set of keyword arguments (e.g. "ha" or "va").
    """

    def __init__(self, position: Point2D, kwargs: Dict[str, Any]):
        self.position = position
        self.kwargs = kwargs


class VennLayout:
    """The circle layout specification for a Venn diagram."""

    # Centers of the (2 / 3) circles (in the Axes coordinates).
    # centers: Sequence[Point2D]
    # # Radii of the circles.
    # radii: Sequence[float]
    # # Layout information of set labels. If labels are missing, then None.
    # set_labels_layout: Optional[Sequence[LabelLayout]] = None

    def __init__(
        self,
        centers: Sequence[Point2D],
        radii: Sequence[float],
        set_labels_layout: Optional[Sequence[LabelLayout]] = None,
    ):
        self.centers = centers
        self.radii = radii
        self.set_labels_layout = set_labels_layout


class VennLayoutAlgorithm(ABC):
    """Interface for a Venn layout algorithm."""

    @abstractmethod
    def __call__(
        self,
        subsets: SubsetSizes,
        set_labels: Optional[Sequence[str]] = None,
    ) -> VennLayout:
        """Lay out the Venn circles, returning the diagram layout specification as VennLayout.
        Args:
            subsets: A tuple with 3 (for venn2) or 7 (for venn3) numbers, denoting the sizes of the
            Venn diagram regions in the following order:
                for venn2: (10, 01, 11)
                for venn3: (100, 010, 110, 001, 101, 011, 111).
            set_labels: Optional tuple of set labels. If None, resulting layout provides no label information.
        """
        pass