File size: 2,475 Bytes
0a3edb6 | 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 | import json, sys
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
sys.path.insert(0, "scripts")
from render_png import draw, _icon
from compare import region_flows
from factorion import Channel, str2item, Direction
DELTA = {1: (0, -1), 2: (1, 0), 3: (0, 1), 4: (-1, 0)}
regions = json.load(open(sys.argv[1]))
best = max((r for r in regions if region_flows(r)), key=lambda r: len(r.get("lanes") or []))
r, w, flows, truth = region_flows(best)
truth_lr = {(x, y, {1: "L", 2: "R"}[l]): v for (x, y, l), v in truth.items()}
W, H = r.ent.shape
dens = np.zeros((W, H))
for (x, y, _) in truth_lr:
if 0 <= x < W and 0 <= y < H:
dens[x, y] += 1
from factorion import str2ent
MACH = [str2ent(n).value for n in ('assembling_machine_1', 'stone_furnace', 'steel_furnace', 'electric_furnace', 'inserter', 'long_handed_inserter')]
asm = np.isin(w[Channel.ENTITIES.value], MACH).astype(float)
S_ = int(sys.argv[2]) if len(sys.argv) > 2 else 16
score = lambda x, y: min(dens[x:x + S_, y:y + S_].sum(), 3 * asm[x:x + S_, y:y + S_].sum())
cx, cy = max(((x, y) for x in range(0, max(1, W - S_), 4) for y in range(0, max(1, H - S_), 4)), key=lambda p: score(*p))
crop = (cx, min(W, cx + S_), cy, min(H, cy + S_))
fig, axes = plt.subplots(1, 1, figsize=(15, 15)); axes = [axes]
for ax, title, labels in ((axes[0], "Items on each lane in the save (icon on the lane's side of the belt)", truth_lr),):
draw(w, ax, title, crop)
for (x, y, lane), its in labels.items():
if not (crop[0] <= x < crop[1] and crop[2] <= y < crop[3]):
continue
dx, dy = DELTA.get(int(w[Channel.DIRECTION.value, x, y]), (0, 0))
lx, ly = (dy, -dx) if lane == "L" else (-dy, dx)
px, py = x + 0.5 + 0.24 * lx, y + 0.5 + 0.24 * ly
if len(its) == 1:
item = str2item(next(iter(its)))
img = _icon(item.value) if item else None
if img is not None:
ax.imshow(img, extent=(px - 0.17, px + 0.17, py + 0.17, py - 0.17), zorder=8)
continue
if its or labels is not truth_lr:
ax.text(px, py, "?" if its or True else "", ha="center", va="center", fontsize=7, color="#c0392b", zorder=8, weight="bold")
ax.set_xlim(crop[0], crop[1]); ax.set_ylim(crop[3], crop[2]); ax.set_aspect("equal")
fig.savefig("/home/user/factorion/plots/lane_items_spotcheck.png", dpi=90, bbox_inches="tight")
print("region", best["x"], best["y"], "crop", crop)
|