scripts: Added --squarify-ratio to treemap[d3].py/codemap[d3].py

Might as well. The internal algorithm already supports this.
This commit is contained in:
Christopher Haster
2025-03-11 16:18:49 -05:00
parent f033a55cc5
commit 4df90dfa0a
4 changed files with 68 additions and 28 deletions

View File

@@ -596,8 +596,8 @@ def partition_squarify(children, total, x, y, width, height, *,
height_ = height height_ = height
# note we don't really care about width vs height until # note we don't really care about width vs height until
# actually slicing # actually slicing
ratio = max(bdiv(aspect_ratio[0], aspect_ratio[1]), ratio = max(aspect_ratio[0] / aspect_ratio[1],
bdiv(aspect_ratio[1], aspect_ratio[0])) aspect_ratio[1] / aspect_ratio[0])
while i < len(children): while i < len(children):
# calculate initial aspect ratio # calculate initial aspect ratio
@@ -1046,13 +1046,16 @@ def main(paths, *,
or (args.get('dice_and_slice') and (tile.depth & 1) == 0)): or (args.get('dice_and_slice') and (tile.depth & 1) == 0)):
partition_dice(tile.children, tile.value, partition_dice(tile.children, tile.value,
x__, y__, width__, height__) x__, y__, width__, height__)
elif args.get('squarify'): elif (args.get('squarify')
partition_squarify(tile.children, tile.value, or args.get('squarify_ratio')
x__, y__, width__, height__) or args.get('rectify')):
elif args.get('rectify'):
partition_squarify(tile.children, tile.value, partition_squarify(tile.children, tile.value,
x__, y__, width__, height__, x__, y__, width__, height__,
aspect_ratio=(width_, height_)) aspect_ratio=(args['squarify_ratio'], 1)
if args.get('squarify_ratio')
else (width_, height_)
if args.get('rectify')
else (1, 1))
else: else:
# default to binary partitioning # default to binary partitioning
partition_binary(tile.children, tile.value, partition_binary(tile.children, tile.value,
@@ -1288,6 +1291,13 @@ if __name__ == "__main__":
help="Use the rectify partitioning scheme. This is like " help="Use the rectify partitioning scheme. This is like "
"squarify, but tries to match the aspect ratio of the " "squarify, but tries to match the aspect ratio of the "
"window.") "window.")
parser.add_argument(
'--squarify-ratio',
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.")
parser.add_argument( parser.add_argument(
'--to-scale', '--to-scale',
nargs='?', nargs='?',

View File

@@ -438,8 +438,8 @@ def partition_squarify(children, total, x, y, width, height, *,
height_ = height height_ = height
# note we don't really care about width vs height until # note we don't really care about width vs height until
# actually slicing # actually slicing
ratio = max(bdiv(aspect_ratio[0], aspect_ratio[1]), ratio = max(aspect_ratio[0] / aspect_ratio[1],
bdiv(aspect_ratio[1], aspect_ratio[0])) aspect_ratio[1] / aspect_ratio[0])
while i < len(children): while i < len(children):
# calculate initial aspect ratio # calculate initial aspect ratio
@@ -975,13 +975,16 @@ def main(paths, output, *,
or (args.get('dice_and_slice') and (tile.depth & 1) == 0)): or (args.get('dice_and_slice') and (tile.depth & 1) == 0)):
partition_dice(tile.children, tile.value, partition_dice(tile.children, tile.value,
x__, y__, width__, height__) x__, y__, width__, height__)
elif args.get('squarify'): elif (args.get('squarify')
partition_squarify(tile.children, tile.value, or args.get('squarify_ratio')
x__, y__, width__, height__) or args.get('rectify')):
elif args.get('rectify'):
partition_squarify(tile.children, tile.value, partition_squarify(tile.children, tile.value,
x__, y__, width__, height__, x__, y__, width__, height__,
aspect_ratio=(width_, height_)) aspect_ratio=(args['squarify_ratio'], 1)
if args.get('squarify_ratio')
else (width_, height_)
if args.get('rectify')
else (1, 1))
else: else:
# default to binary partitioning # default to binary partitioning
partition_binary(tile.children, tile.value, partition_binary(tile.children, tile.value,
@@ -2162,6 +2165,13 @@ if __name__ == "__main__":
help="Use the rectify partitioning scheme. This is like " help="Use the rectify partitioning scheme. This is like "
"squarify, but tries to match the aspect ratio of the " "squarify, but tries to match the aspect ratio of the "
"window.") "window.")
parser.add_argument(
'--squarify-ratio',
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.")
parser.add_argument( parser.add_argument(
'--to-scale', '--to-scale',
nargs='?', nargs='?',

View File

@@ -664,8 +664,8 @@ def partition_squarify(children, total, x, y, width, height, *,
height_ = height height_ = height
# note we don't really care about width vs height until # note we don't really care about width vs height until
# actually slicing # actually slicing
ratio = max(bdiv(aspect_ratio[0], aspect_ratio[1]), ratio = max(aspect_ratio[0] / aspect_ratio[1],
bdiv(aspect_ratio[1], aspect_ratio[0])) aspect_ratio[1] / aspect_ratio[0])
while i < len(children): while i < len(children):
# calculate initial aspect ratio # calculate initial aspect ratio
@@ -934,13 +934,16 @@ def main(csv_paths, *,
or (args.get('dice_and_slice') and (tile.depth & 1) == 0)): or (args.get('dice_and_slice') and (tile.depth & 1) == 0)):
partition_dice(tile.children, tile.value, partition_dice(tile.children, tile.value,
x__, y__, width__, height__) x__, y__, width__, height__)
elif args.get('squarify'): elif (args.get('squarify')
partition_squarify(tile.children, tile.value, or args.get('squarify_ratio')
x__, y__, width__, height__) or args.get('rectify')):
elif args.get('rectify'):
partition_squarify(tile.children, tile.value, partition_squarify(tile.children, tile.value,
x__, y__, width__, height__, x__, y__, width__, height__,
aspect_ratio=(width_, height_)) aspect_ratio=(args['squarify_ratio'], 1)
if args.get('squarify_ratio')
else (width_, height_)
if args.get('rectify')
else (1, 1))
else: else:
# default to binary partitioning # default to binary partitioning
partition_binary(tile.children, tile.value, partition_binary(tile.children, tile.value,
@@ -1157,6 +1160,13 @@ if __name__ == "__main__":
help="Use the rectify partitioning scheme. This is like " help="Use the rectify partitioning scheme. This is like "
"squarify, but tries to match the aspect ratio of the " "squarify, but tries to match the aspect ratio of the "
"window.") "window.")
parser.add_argument(
'--squarify-ratio',
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.")
parser.add_argument( parser.add_argument(
'--to-scale', '--to-scale',
nargs='?', nargs='?',

View File

@@ -507,8 +507,8 @@ def partition_squarify(children, total, x, y, width, height, *,
height_ = height height_ = height
# note we don't really care about width vs height until # note we don't really care about width vs height until
# actually slicing # actually slicing
ratio = max(bdiv(aspect_ratio[0], aspect_ratio[1]), ratio = max(aspect_ratio[0] / aspect_ratio[1],
bdiv(aspect_ratio[1], aspect_ratio[0])) aspect_ratio[1] / aspect_ratio[0])
while i < len(children): while i < len(children):
# calculate initial aspect ratio # calculate initial aspect ratio
@@ -750,13 +750,16 @@ def main(csv_paths, output, *,
or (args.get('dice_and_slice') and (tile.depth & 1) == 0)): or (args.get('dice_and_slice') and (tile.depth & 1) == 0)):
partition_dice(tile.children, tile.value, partition_dice(tile.children, tile.value,
x__, y__, width__, height__) x__, y__, width__, height__)
elif args.get('squarify'): elif (args.get('squarify')
partition_squarify(tile.children, tile.value, or args.get('squarify_ratio')
x__, y__, width__, height__) or args.get('rectify')):
elif args.get('rectify'):
partition_squarify(tile.children, tile.value, partition_squarify(tile.children, tile.value,
x__, y__, width__, height__, x__, y__, width__, height__,
aspect_ratio=(width_, height_)) aspect_ratio=(args['squarify_ratio'], 1)
if args.get('squarify_ratio')
else (width_, height_)
if args.get('rectify')
else (1, 1))
else: else:
# default to binary partitioning # default to binary partitioning
partition_binary(tile.children, tile.value, partition_binary(tile.children, tile.value,
@@ -1024,6 +1027,13 @@ if __name__ == "__main__":
help="Use the rectify partitioning scheme. This is like " help="Use the rectify partitioning scheme. This is like "
"squarify, but tries to match the aspect ratio of the " "squarify, but tries to match the aspect ratio of the "
"window.") "window.")
parser.add_argument(
'--squarify-ratio',
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.")
parser.add_argument( parser.add_argument(
'--to-scale', '--to-scale',
nargs='?', nargs='?',