scripts: maps: Fixed some aspect ratio issues, limited scope

Replacing -R/--aspect-ratio, --to-ratio now calculates the width/height
_before_ adding decoration such as headers, stack info, etc.

I toying around with generalizing -R/--aspect-ratio to include
decorations, but when Wolfram Alpha spit this mess for the post-header
formula:

      header*r - sqrt(4*v*r + padding^2*r)
  w = ------------------------------------
                        2

I decided maybe a generalized -R/--aspect-ratio is a _bit_ too
complicated for what are supposed to be small standalone Python
scripts...

---

Also fixed the scaling formula, which should've taken the sqrt _after_
multiplying by the aspect ratio:

  w = sqrt(v*r)

I only noticed while trying to solve for the more complicated
post-decoration formula, the difference is pretty minor.
This commit is contained in:
Christopher Haster
2025-04-11 00:53:12 -05:00
parent cb5cbb9241
commit 61ce23ce7e
7 changed files with 170 additions and 161 deletions
+33 -38
View File
@@ -501,7 +501,7 @@ def partition_dice(children, total, x, y, width, height):
y_ += t.height
def partition_squarify(children, total, x, y, width, height, *,
aspect_ratio=(1,1)):
aspect_ratio=1/1):
# this algorithm is described here:
# https://www.win.tue.nl/~vanwijk/stm.pdf
i = 0
@@ -512,8 +512,7 @@ def partition_squarify(children, total, x, y, width, height, *,
height_ = height
# note we don't really care about width vs height until
# actually slicing
ratio = max(aspect_ratio[0] / aspect_ratio[1],
aspect_ratio[1] / aspect_ratio[0])
ratio = max(aspect_ratio, 1/aspect_ratio)
while i < len(children):
# calculate initial aspect ratio
@@ -637,7 +636,7 @@ def main(paths, output, *,
mode_callees=False,
mode_callers=False,
to_scale=None,
aspect_ratio=(1,1),
to_ratio=1/1,
title=None,
padding=1,
no_label=False,
@@ -993,36 +992,31 @@ def main(paths, output, *,
else totals.get('frame', 0) if not nil_frames
else totals.get('ctx', 0))
if total_value:
# don't include header/stack in scale
width__ = width_
height__ = height_
if not no_header:
height__ -= mt.ceil(FONT_SIZE * 1.3)
if not no_stack:
width__ *= (1 - stack_ratio)
# scale width only
if height is not None:
if not no_stack:
width_ = mt.ceil(((total_value * to_scale) / height_)
# add space for stack
/ (1 - stack_ratio))
else:
width_ = mt.ceil((total_value * to_scale) / height_)
width__ = mt.ceil((total_value * to_scale) / max(height__, 1))
# scale height only
elif width is not None:
if not no_stack:
height_ = mt.ceil((total_value * to_scale)
# carve out space for stack
/ (width_ * (1 - stack_ratio)))
else:
height_ = mt.ceil((total_value * to_scale) / width_)
height__ = mt.ceil((total_value * to_scale) / max(width__, 1))
# scale based on aspect-ratio
else:
if not no_stack:
width_ = mt.ceil(mt.sqrt(total_value * to_scale)
* (aspect_ratio[0] / aspect_ratio[1])
# add space for stack
/ (1 - stack_ratio))
height_ = mt.ceil((total_value * to_scale)
# carve out space for stack
/ (width_ * (1 - stack_ratio)))
else:
width_ = mt.ceil(mt.sqrt(total_value * to_scale)
* (aspect_ratio[0] / aspect_ratio[1]))
height_ = mt.ceil((total_value * to_scale) / width_)
width__ = mt.ceil(mt.sqrt(total_value * to_scale * to_ratio))
height__ = mt.ceil((total_value * to_scale) / max(width__, 1))
if not no_stack:
width__ /= (1 - stack_ratio)
if not no_header:
height__ += mt.ceil(FONT_SIZE * 1.3)
width_ = width__
height_ = height__
# our general purpose partition function
def partition(tile, **args):
@@ -1073,11 +1067,12 @@ def main(paths, output, *,
or args.get('rectify')):
partition_squarify(tile.children, tile.value,
x__, y__, width__, height__,
aspect_ratio=(args['squarify_ratio'], 1)
if args.get('squarify_ratio')
else (width_, height_)
if args.get('rectify')
else (1, 1))
aspect_ratio=(
args['squarify_ratio']
if args.get('squarify_ratio')
else width_/height_
if args.get('rectify')
else 1/1))
else:
# default to binary partitioning
partition_binary(tile.children, tile.value,
@@ -2278,8 +2273,8 @@ if __name__ == "__main__":
type=lambda x: (
(lambda a, b: a / b)(*(float(v) for v in x.split(':', 1)))
if ':' in x else float(x)),
help="Specify an explicit ratio for the squarify algorithm. "
"Implies --squarify.")
help="Specify an explicit aspect ratio for the squarify "
"algorithm. Implies --squarify.")
parser.add_argument(
'--to-scale',
nargs='?',
@@ -2290,10 +2285,10 @@ if __name__ == "__main__":
help="Scale the resulting treemap such that 1 pixel ~= 1/scale "
"units. Defaults to scale=1. ")
parser.add_argument(
'-R', '--aspect-ratio',
'--to-ratio',
type=lambda x: (
tuple(float(v) for v in x.split(':', 1))
if ':' in x else (float(x), 1)),
(lambda a, b: a / b)(*(float(v) for v in x.split(':', 1)))
if ':' in x else float(x)),
help="Aspect ratio to use with --to-scale. Defaults to 1:1.")
parser.add_argument(
'-t', '--tiny',