Tweak plots

This commit is contained in:
Matthias Bisping 2023-01-25 19:44:54 +01:00
parent d9d363834a
commit d5e501a05d

View File

@ -8,11 +8,10 @@ from enum import Enum
from functools import lru_cache, partial
from math import sqrt
from pathlib import Path
from typing import Tuple, Union, Iterable, List
from typing import Tuple, Iterable, List
import albumentations as A
import cv2 as cv
import matplotlib
import numpy as np
import pandas as pd
import pytest
@ -28,7 +27,6 @@ from cv_analysis.table_parsing import isolate_vertical_and_horizontal_components
from cv_analysis.utils import star, rconj, conj
from cv_analysis.utils.common import normalize_to_gray_scale
from cv_analysis.utils.conversion import normalize_image_format_to_array, normalize_image_format_to_pil
from cv_analysis.utils.drawing import draw_rectangles
from cv_analysis.utils.merging import merge_related_rectangles
from cv_analysis.utils.postprocessing import remove_overlapping, remove_included
from cv_analysis.utils.spacial import area
@ -37,7 +35,8 @@ random_seed = random.randint(0, 2**32 - 1)
# random_seed = 3896311122
# random_seed = 1986343479
random_seed = 273244862 # empty large table
# random_seed = 273244862 # empty large table
random_seed = 3717442900
rnd = random.Random(random_seed)
logger.info(f"Random seed: {random_seed}")
@ -533,7 +532,7 @@ class Size(Enum):
# MEDIUM = sqrt((100 * 3) ** 2)
# LARGE = sqrt((100 * 10) ** 2)
SMALL = 100
SMALL = 120
MEDIUM = 180
LARGE = 300
@ -650,7 +649,7 @@ class RecursiveRandomTable(RandomContentRectangle):
logger.debug(f"Generating {choice} {size:.0f} {get_size_class(cell).name}")
if choice == "plot":
if choice == "plot" and is_square_like(cell):
return generate_random_plot(cell)
else:
@ -972,7 +971,7 @@ def get_fonts(path: Path = None) -> List[str]:
@lru_cache(maxsize=None)
def get_font_picker(**kwargs):
return RandomFontPicker(**kwargs, return_default_font=True)
return RandomFontPicker(**kwargs, return_default_font=False)
@lru_cache(maxsize=None)
@ -1034,7 +1033,8 @@ class RandomPlot(RandomContentRectangle):
plt_fn = rnd.choice(
[
self.generate_random_line_plot,
self.generate_random_scatter_plot,
self.generate_random_histogram,
self.generate_random_bar_plot,
]
)
elif is_tall(rectangle):
@ -1052,7 +1052,11 @@ class RandomPlot(RandomContentRectangle):
def generate_random_bar_plot(self, rectangle: Rectangle):
x = sorted(np.random.randint(low=1, high=11, size=5))
y = np.random.randint(low=1, high=11, size=5)
self.__generate_random_plot(plt.bar, rectangle, x, y)
bar_fn = partial(
plt.bar,
log=random.choice([True, False]),
)
self.__generate_random_plot(bar_fn, rectangle, x, y)
def generate_random_line_plot(self, rectangle: Rectangle):
f = rnd.choice([np.sin, np.cos, np.tan, np.exp, np.log, np.sqrt, np.square])
@ -1060,7 +1064,11 @@ class RandomPlot(RandomContentRectangle):
x = np.linspace(0, 10, 100)
y = f(x)
self.__generate_random_plot(plt.plot, rectangle, x, y)
plot_fn = partial(
plt.plot,
)
self.__generate_random_plot(plot_fn, rectangle, x, y)
def generate_random_scatter_plot(self, rectangle: Rectangle):
x = np.random.normal(size=100)
@ -1069,28 +1077,43 @@ class RandomPlot(RandomContentRectangle):
def generate_random_histogram(self, rectangle: Rectangle):
x = np.random.normal(size=100)
self.__generate_random_plot(plt.hist, rectangle, x, 10)
hist_fn = partial(
plt.hist,
orientation=random.choice(["horizontal", "vertical"]),
histtype=random.choice(["bar", "barstacked", "step", "stepfilled"]),
log=random.choice([True, False]),
stacked=random.choice([True, False]),
density=random.choice([True, False]),
cumulative=random.choice([True, False]),
)
self.__generate_random_plot(hist_fn, rectangle, x, random.randint(5, 20))
def generate_random_pie_chart(self, rectangle: Rectangle):
x = np.random.uniform(size=10)
n = random.randint(3, 7)
x = np.random.uniform(size=n)
pie_fn = partial(
plt.pie,
labels=[f"Label {i}" for i in range(10)],
autopct="%1.1f%%",
shadow=True,
startangle=90,
pctdistance=0.85,
labeldistance=1.1,
colors=self.cmap(np.linspace(0, 1, 10)),
)
self.__generate_random_plot(pie_fn, rectangle, x, None, plot_kwargs=self.generate_plot_kwargs(keywords=["a"]))
self.__generate_random_plot(
pie_fn,
rectangle,
x,
np.random.uniform(0, 0.1, size=n),
plot_kwargs=self.generate_plot_kwargs(keywords=["a"]),
)
def generate_plot_kwargs(self, keywords=None):
kwargs = {
"color": rnd.choice(self.cmap.colors),
"linestyle": rnd.choice(["-", "--", "-.", ":"]),
"linewidth": rnd.uniform(0.5, 2),
"linewidth": rnd.uniform(1, 4),
}
return kwargs if not keywords else {k: v for k, v in kwargs.items() if k in keywords}