[WIP] recursive random table: Add recursive construction

This commit is contained in:
Matthias Bisping 2023-01-18 15:04:04 +01:00
parent 30e6350881
commit 3da613af94

View File

@ -427,6 +427,7 @@ def every_nth(n, iterable):
def generate_random_plot(rectangle: Rectangle) -> ContentRectangle:
block = RandomPlot(*rectangle.coords)
block.content = rectangle.content if isinstance(rectangle, ContentRectangle) else None # TODO: Refactor
block.generate_random_plot(rectangle)
return block
@ -459,7 +460,7 @@ class RecursiveRandomTable(RandomContentRectangle):
def __init__(self, x1, y1, x2, y2, seed=None):
super().__init__(x1, y1, x2, y2, seed=seed)
self.n_columns = max(self.width // 100, 1)
self.n_rows = max(self.height // 17, 1)
self.n_rows = max(self.height // random.randint(17, 100), 1)
self.cell_size = (self.width // self.n_columns, self.height // self.n_rows)
self.content = Image.new("RGBA", (self.width, self.height), (255, 255, 255, 255))
@ -476,8 +477,20 @@ class RecursiveRandomTable(RandomContentRectangle):
for cell in self.generate_table():
self.draw_single_cell_borders(cell)
inner_region = shrink_rectangle(cell, 0.1)
cell.content = generate_random_text_block(inner_region, n_sentences=1).content
choice = random.choice(["text", "plot", "recurse", "plain_table"])
if choice == "text":
cell.content = generate_random_text_block(inner_region, n_sentences=1).content
elif choice == "plot":
cell.content = generate_random_plot(inner_region).content
elif choice == "recurse":
cell.content = generate_recursive_random_table(inner_region).content
elif choice == "plain_table":
cell.content = generate_random_table(inner_region).content
assert cell.content.mode == "RGBA"
yield cell
def draw_cell_borders(self, cells: List[ContentRectangle]):
@ -708,8 +721,7 @@ 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)
image = self.__generate_random_plot(plt.bar, rectangle, x, y)
self.content = image
self.__generate_random_plot(plt.bar, rectangle, x, y)
def generate_random_line_plot(self, rectangle: Rectangle):
f = random.choice([np.sin, np.cos, np.tan, np.exp, np.log, np.sqrt, np.square])
@ -717,26 +729,20 @@ class RandomPlot(RandomContentRectangle):
x = np.linspace(0, 10, 100)
y = f(x)
image = self.__generate_random_plot(plt.plot, rectangle, x, y)
self.content = image
self.__generate_random_plot(plt.plot, rectangle, x, y)
def generate_random_scatter_plot(self, rectangle: Rectangle):
x = np.random.normal(size=100)
y = np.random.normal(size=100)
image = self.__generate_random_plot(plt.scatter, rectangle, x, y)
self.content = image
self.__generate_random_plot(plt.scatter, rectangle, x, y)
def generate_random_histogram(self, rectangle: Rectangle):
x = np.random.normal(size=100)
image = self.__generate_random_plot(plt.hist, rectangle, x, 10)
self.content = image
self.__generate_random_plot(plt.hist, rectangle, x, 10)
def generate_random_pie_chart(self, rectangle: Rectangle):
x = np.random.uniform(size=10)
image = self.__generate_random_plot(
plt.pie, rectangle, x, None, plot_kwargs=self.generate_plot_kwargs(keywords=["a"])
)
self.content = image
self.__generate_random_plot(plt.pie, rectangle, x, None, plot_kwargs=self.generate_plot_kwargs(keywords=["a"]))
def generate_plot_kwargs(self, keywords=None):
@ -779,7 +785,8 @@ class RandomPlot(RandomContentRectangle):
maybe() and (ax.spines["top"].set_visible(False) or ax.spines["right"].set_visible(False))
image = dump_plt_to_image(rectangle)
return image
self.content = image if not self.content else superimpose_texture_with_transparency(self.content, image)
def maybe():