plottracer 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- plottracer/__init__.py +95 -0
- plottracer/__main__.py +6 -0
- plottracer/annotations.py +165 -0
- plottracer/axes.py +1819 -0
- plottracer/calibration.py +162 -0
- plottracer/class_view.py +75 -0
- plottracer/classification.py +278 -0
- plottracer/cli.py +1982 -0
- plottracer/digitize.py +3437 -0
- plottracer/errorbars.py +263 -0
- plottracer/halftone.py +87 -0
- plottracer/interactive.py +74 -0
- plottracer/legends.py +288 -0
- plottracer/level.py +334 -0
- plottracer/masks.py +26 -0
- plottracer/metadata.py +271 -0
- plottracer/panels.py +590 -0
- plottracer/pdf.py +1864 -0
- plottracer/pdf_figures.py +161 -0
- plottracer/plot_objects.py +190 -0
- plottracer/py.typed +0 -0
- plottracer/svg.py +1520 -0
- plottracer-0.2.0.dist-info/METADATA +1852 -0
- plottracer-0.2.0.dist-info/RECORD +28 -0
- plottracer-0.2.0.dist-info/WHEEL +4 -0
- plottracer-0.2.0.dist-info/entry_points.txt +3 -0
- plottracer-0.2.0.dist-info/licenses/COPYING +674 -0
- plottracer-0.2.0.dist-info/licenses/LICENSE +165 -0
plottracer/__init__.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""Recover data from images of plots."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
from .axes import Axes, read_axes
|
|
6
|
+
from .calibration import Calibration, Point, Tick
|
|
7
|
+
from .classification import classify
|
|
8
|
+
from .digitize import (
|
|
9
|
+
STYLES,
|
|
10
|
+
Colour,
|
|
11
|
+
Image,
|
|
12
|
+
MarkerSeries,
|
|
13
|
+
Series,
|
|
14
|
+
Style,
|
|
15
|
+
find_colours,
|
|
16
|
+
find_error_bars,
|
|
17
|
+
find_marker_error_bars,
|
|
18
|
+
find_marker_series,
|
|
19
|
+
find_markers,
|
|
20
|
+
find_series,
|
|
21
|
+
load_image,
|
|
22
|
+
remove_grid,
|
|
23
|
+
trace_curve,
|
|
24
|
+
trace_curves,
|
|
25
|
+
trace_series,
|
|
26
|
+
)
|
|
27
|
+
from .errorbars import ErrorBars
|
|
28
|
+
from .level import Levelling, find_skew, level_image
|
|
29
|
+
from .panels import Panel, read_panels
|
|
30
|
+
from .pdf import (
|
|
31
|
+
Box,
|
|
32
|
+
Drawing,
|
|
33
|
+
find_pdf_colours,
|
|
34
|
+
find_pdf_error_bars,
|
|
35
|
+
find_pdf_markers,
|
|
36
|
+
find_pdf_series,
|
|
37
|
+
load_pdf,
|
|
38
|
+
read_pdf_axes,
|
|
39
|
+
remove_pdf_grid,
|
|
40
|
+
render_pdf,
|
|
41
|
+
trace_pdf_curves,
|
|
42
|
+
trace_pdf_series,
|
|
43
|
+
)
|
|
44
|
+
from .plot_objects import PlotObject
|
|
45
|
+
from .svg import load_svg, render_svg
|
|
46
|
+
|
|
47
|
+
__version__ = version("plottracer")
|
|
48
|
+
|
|
49
|
+
__all__ = [
|
|
50
|
+
"STYLES",
|
|
51
|
+
"Axes",
|
|
52
|
+
"Box",
|
|
53
|
+
"Calibration",
|
|
54
|
+
"Colour",
|
|
55
|
+
"Drawing",
|
|
56
|
+
"ErrorBars",
|
|
57
|
+
"Image",
|
|
58
|
+
"Levelling",
|
|
59
|
+
"MarkerSeries",
|
|
60
|
+
"Panel",
|
|
61
|
+
"PlotObject",
|
|
62
|
+
"Point",
|
|
63
|
+
"Series",
|
|
64
|
+
"Style",
|
|
65
|
+
"Tick",
|
|
66
|
+
"__version__",
|
|
67
|
+
"classify",
|
|
68
|
+
"find_colours",
|
|
69
|
+
"find_error_bars",
|
|
70
|
+
"find_marker_error_bars",
|
|
71
|
+
"find_marker_series",
|
|
72
|
+
"find_markers",
|
|
73
|
+
"find_pdf_colours",
|
|
74
|
+
"find_pdf_error_bars",
|
|
75
|
+
"find_pdf_markers",
|
|
76
|
+
"find_pdf_series",
|
|
77
|
+
"find_series",
|
|
78
|
+
"find_skew",
|
|
79
|
+
"level_image",
|
|
80
|
+
"load_image",
|
|
81
|
+
"load_pdf",
|
|
82
|
+
"load_svg",
|
|
83
|
+
"read_axes",
|
|
84
|
+
"read_panels",
|
|
85
|
+
"read_pdf_axes",
|
|
86
|
+
"remove_grid",
|
|
87
|
+
"remove_pdf_grid",
|
|
88
|
+
"render_pdf",
|
|
89
|
+
"render_svg",
|
|
90
|
+
"trace_curve",
|
|
91
|
+
"trace_curves",
|
|
92
|
+
"trace_pdf_curves",
|
|
93
|
+
"trace_pdf_series",
|
|
94
|
+
"trace_series",
|
|
95
|
+
]
|
plottracer/__main__.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"""Find text annotations and their arrow or bracket shafts inside raster axes."""
|
|
2
|
+
|
|
3
|
+
import itertools
|
|
4
|
+
from collections.abc import Sequence
|
|
5
|
+
|
|
6
|
+
import cv2
|
|
7
|
+
import numpy as np
|
|
8
|
+
import numpy.typing as npt
|
|
9
|
+
|
|
10
|
+
from .axes import _read_text, _Text
|
|
11
|
+
from .calibration import Point
|
|
12
|
+
from .digitize import Image, _Box, _framed_area
|
|
13
|
+
from .masks import Rectangle, painted
|
|
14
|
+
|
|
15
|
+
_CONTRAST = 26
|
|
16
|
+
_MIN_CONFIDENCE = 0.7
|
|
17
|
+
_MIN_LETTERS = 2
|
|
18
|
+
_MIN_WORD_ASPECT = 0.65 # short horizontal labels; vertical caps mimic letters
|
|
19
|
+
_MIN_GLYPH = 5
|
|
20
|
+
_MIN_SHAFT = 20
|
|
21
|
+
_MAX_SHAFT_RADIUS = 2.5
|
|
22
|
+
_END_SHARE = 0.12
|
|
23
|
+
_CAP_WIDTH = 3
|
|
24
|
+
_HEAD_REACH = 12
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _ink(image: Image) -> npt.NDArray[np.uint8]:
|
|
28
|
+
"""Contrast with the dominant paper colour, including on dark backgrounds."""
|
|
29
|
+
paper = np.median(image, axis=(0, 1))
|
|
30
|
+
return (np.max(np.abs(image - paper), axis=2) >= _CONTRAST).astype(np.uint8)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _has_letters(ink: npt.NDArray[np.uint8]) -> bool:
|
|
34
|
+
"""Look for adjacent character-sized components before running optional OCR."""
|
|
35
|
+
_, _, stats, _ = cv2.connectedComponentsWithStats(ink)
|
|
36
|
+
glyphs = sorted(
|
|
37
|
+
(x, y, w, h) for x, y, w, h, _ in stats[1:] if h >= _MIN_GLYPH and w <= 2 * h
|
|
38
|
+
)
|
|
39
|
+
return any(
|
|
40
|
+
0 < x2 - x <= w + h and abs(y2 - y) <= h and h / 2 <= h2 <= 2 * h
|
|
41
|
+
for (x, y, w, h), (x2, y2, _, h2) in itertools.pairwise(glyphs)
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _rectangle(text: _Text) -> Rectangle:
|
|
46
|
+
return (
|
|
47
|
+
text.left - 2,
|
|
48
|
+
text.bottom - 2,
|
|
49
|
+
text.right + 2,
|
|
50
|
+
text.top + 2,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _shaft(
|
|
55
|
+
ys: npt.NDArray[np.intp], xs: npt.NDArray[np.intp], *, labelled: bool
|
|
56
|
+
) -> bool:
|
|
57
|
+
"""A thin straight shaft with a wider head or cap confined to an end."""
|
|
58
|
+
points = np.column_stack((xs, ys)).astype(np.float32)
|
|
59
|
+
vx, vy, cx, cy = cv2.fitLine(points, cv2.DIST_L1, 0, 0.01, 0.01).ravel()
|
|
60
|
+
shifted = points - (cx, cy)
|
|
61
|
+
along = shifted @ np.array((vx, vy))
|
|
62
|
+
signed = shifted @ np.array((-vy, vx))
|
|
63
|
+
across = abs(signed)
|
|
64
|
+
fraction = (along - along.min()) / np.ptp(along)
|
|
65
|
+
middle = (fraction > _END_SHARE) & (fraction < 1 - _END_SHARE)
|
|
66
|
+
radius = np.percentile(across[middle], 99)
|
|
67
|
+
if radius > _MAX_SHAFT_RADIUS or np.max(across[~middle]) <= radius * 2 + 0.7:
|
|
68
|
+
return False
|
|
69
|
+
if labelled:
|
|
70
|
+
return True
|
|
71
|
+
# An isolated bracket has two transverse caps. An isolated arrow has
|
|
72
|
+
# two wings that widen as they retreat from a narrow terminal point.
|
|
73
|
+
caps = []
|
|
74
|
+
for distance in (along - along.min(), along.max() - along):
|
|
75
|
+
tip = signed[distance <= _CAP_WIDTH]
|
|
76
|
+
rear = signed[(distance > _CAP_WIDTH) & (distance <= _HEAD_REACH)]
|
|
77
|
+
tip_width = min(float(tip.max()), float(-tip.min()))
|
|
78
|
+
rear_width = min(float(rear.max()), float(-rear.min()))
|
|
79
|
+
if rear_width > max(3, 2 * radius) and rear_width > 1.75 * max(1, tip_width):
|
|
80
|
+
return True
|
|
81
|
+
caps.append(tip_width > max(4, 2.5 * radius) and tip_width > 2 * rear_width)
|
|
82
|
+
return all(caps)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _text_boxes(
|
|
86
|
+
image: Image, area: _Box, frames: Sequence[_Box], texts: Sequence[_Text] | None
|
|
87
|
+
) -> list[Rectangle]:
|
|
88
|
+
"""Keep credible words wholly inside this plot and outside framed objects."""
|
|
89
|
+
height = image.shape[0]
|
|
90
|
+
roi = image[area.top : area.bottom, area.left : area.right]
|
|
91
|
+
ink = _ink(roi)
|
|
92
|
+
if texts is None:
|
|
93
|
+
if not _has_letters(ink):
|
|
94
|
+
return []
|
|
95
|
+
try:
|
|
96
|
+
texts = _read_text(image)
|
|
97
|
+
except ModuleNotFoundError:
|
|
98
|
+
return []
|
|
99
|
+
boxes = []
|
|
100
|
+
for text in texts:
|
|
101
|
+
if (
|
|
102
|
+
text.right - text.left < _MIN_WORD_ASPECT * (text.top - text.bottom)
|
|
103
|
+
or text.score < _MIN_CONFIDENCE
|
|
104
|
+
or (
|
|
105
|
+
sum(c.isalpha() for c in text.text) < _MIN_LETTERS
|
|
106
|
+
and not (
|
|
107
|
+
any(c.isalpha() for c in text.text)
|
|
108
|
+
and any(c.isdigit() for c in text.text)
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
):
|
|
112
|
+
continue
|
|
113
|
+
box = _rectangle(text)
|
|
114
|
+
left, bottom, right, top = box
|
|
115
|
+
local = _Box(
|
|
116
|
+
round(height - top), round(height - bottom), round(left), round(right)
|
|
117
|
+
)
|
|
118
|
+
region = local.within(area)
|
|
119
|
+
if (
|
|
120
|
+
area.holds(local)
|
|
121
|
+
and not any(frame.holds(region) for frame in frames)
|
|
122
|
+
and np.any(ink[region.top : region.bottom, region.left : region.right])
|
|
123
|
+
):
|
|
124
|
+
boxes.append(box)
|
|
125
|
+
return boxes
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def excluded(
|
|
129
|
+
image: Image,
|
|
130
|
+
origin: Point,
|
|
131
|
+
texts: Sequence[_Text] | None,
|
|
132
|
+
saved: Sequence[Rectangle] | None,
|
|
133
|
+
) -> tuple[Image, list[Rectangle], int]:
|
|
134
|
+
"""Mask plot text, adjacent headed shafts and isolated arrows or brackets."""
|
|
135
|
+
area, frames = _framed_area(image, origin)
|
|
136
|
+
roi = image[area.top : area.bottom, area.left : area.right]
|
|
137
|
+
height = image.shape[0]
|
|
138
|
+
boxes = _text_boxes(image, area, frames, texts) if saved is None else list(saved)
|
|
139
|
+
result = painted(image, boxes)
|
|
140
|
+
plot = result[area.top : area.bottom, area.left : area.right]
|
|
141
|
+
_, labels, stats, _ = cv2.connectedComponentsWithStats(_ink(plot))
|
|
142
|
+
shafts = np.zeros(labels.shape, dtype=np.uint8)
|
|
143
|
+
count = 0
|
|
144
|
+
for index, (x, y, w, h, _) in enumerate(stats[1:], 1):
|
|
145
|
+
if max(w, h) < _MIN_SHAFT:
|
|
146
|
+
continue
|
|
147
|
+
left, top = x + area.left, height - (y + area.top)
|
|
148
|
+
near = any(
|
|
149
|
+
max(x0 - left - w, left - x1, y0 - top, top - h - y1) < y1 - y0
|
|
150
|
+
for x0, y0, x1, y1 in boxes
|
|
151
|
+
)
|
|
152
|
+
if near or 0.13 * max(plot.shape[:2]) <= max(w, h) <= 0.75 * max(
|
|
153
|
+
plot.shape[:2]
|
|
154
|
+
):
|
|
155
|
+
ys, xs = np.nonzero(labels[y : y + h, x : x + w] == index)
|
|
156
|
+
if _shaft(ys, xs, labelled=near):
|
|
157
|
+
shafts[y + ys, x + xs] = 1
|
|
158
|
+
count += 1
|
|
159
|
+
if count:
|
|
160
|
+
edges = cv2.dilate(shafts, np.ones((3, 3), np.uint8))
|
|
161
|
+
result = result.copy()
|
|
162
|
+
result[area.top : area.bottom, area.left : area.right][edges > 0] = np.median(
|
|
163
|
+
roi, axis=(0, 1)
|
|
164
|
+
)
|
|
165
|
+
return result, boxes, count
|