From 4df90dfa0a7f0d0eb5b044ca4de2162e0209ea5c Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 11 Mar 2025 16:18:49 -0500 Subject: [PATCH] scripts: Added --squarify-ratio to treemap[d3].py/codemap[d3].py Might as well. The internal algorithm already supports this. --- scripts/codemap.py | 24 +++++++++++++++++------- scripts/codemapd3.py | 24 +++++++++++++++++------- scripts/treemap.py | 24 +++++++++++++++++------- scripts/treemapd3.py | 24 +++++++++++++++++------- 4 files changed, 68 insertions(+), 28 deletions(-) diff --git a/scripts/codemap.py b/scripts/codemap.py index e4c9b034..98fbd495 100755 --- a/scripts/codemap.py +++ b/scripts/codemap.py @@ -596,8 +596,8 @@ 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(bdiv(aspect_ratio[0], aspect_ratio[1]), - bdiv(aspect_ratio[1], aspect_ratio[0])) + ratio = max(aspect_ratio[0] / aspect_ratio[1], + aspect_ratio[1] / aspect_ratio[0]) while i < len(children): # calculate initial aspect ratio @@ -1046,13 +1046,16 @@ def main(paths, *, or (args.get('dice_and_slice') and (tile.depth & 1) == 0)): partition_dice(tile.children, tile.value, x__, y__, width__, height__) - elif args.get('squarify'): - partition_squarify(tile.children, tile.value, - x__, y__, width__, height__) - elif args.get('rectify'): + elif (args.get('squarify') + or args.get('squarify_ratio') + or args.get('rectify')): partition_squarify(tile.children, tile.value, 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: # default to binary partitioning partition_binary(tile.children, tile.value, @@ -1288,6 +1291,13 @@ if __name__ == "__main__": help="Use the rectify partitioning scheme. This is like " "squarify, but tries to match the aspect ratio of the " "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( '--to-scale', nargs='?', diff --git a/scripts/codemapd3.py b/scripts/codemapd3.py index c08f28ae..ef5262d3 100755 --- a/scripts/codemapd3.py +++ b/scripts/codemapd3.py @@ -438,8 +438,8 @@ 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(bdiv(aspect_ratio[0], aspect_ratio[1]), - bdiv(aspect_ratio[1], aspect_ratio[0])) + ratio = max(aspect_ratio[0] / aspect_ratio[1], + aspect_ratio[1] / aspect_ratio[0]) while i < len(children): # calculate initial aspect ratio @@ -975,13 +975,16 @@ def main(paths, output, *, or (args.get('dice_and_slice') and (tile.depth & 1) == 0)): partition_dice(tile.children, tile.value, x__, y__, width__, height__) - elif args.get('squarify'): - partition_squarify(tile.children, tile.value, - x__, y__, width__, height__) - elif args.get('rectify'): + elif (args.get('squarify') + or args.get('squarify_ratio') + or args.get('rectify')): partition_squarify(tile.children, tile.value, 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: # default to binary partitioning partition_binary(tile.children, tile.value, @@ -2162,6 +2165,13 @@ if __name__ == "__main__": help="Use the rectify partitioning scheme. This is like " "squarify, but tries to match the aspect ratio of the " "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( '--to-scale', nargs='?', diff --git a/scripts/treemap.py b/scripts/treemap.py index def804e6..c9fea928 100755 --- a/scripts/treemap.py +++ b/scripts/treemap.py @@ -664,8 +664,8 @@ 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(bdiv(aspect_ratio[0], aspect_ratio[1]), - bdiv(aspect_ratio[1], aspect_ratio[0])) + ratio = max(aspect_ratio[0] / aspect_ratio[1], + aspect_ratio[1] / aspect_ratio[0]) while i < len(children): # calculate initial aspect ratio @@ -934,13 +934,16 @@ def main(csv_paths, *, or (args.get('dice_and_slice') and (tile.depth & 1) == 0)): partition_dice(tile.children, tile.value, x__, y__, width__, height__) - elif args.get('squarify'): - partition_squarify(tile.children, tile.value, - x__, y__, width__, height__) - elif args.get('rectify'): + elif (args.get('squarify') + or args.get('squarify_ratio') + or args.get('rectify')): partition_squarify(tile.children, tile.value, 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: # default to binary partitioning partition_binary(tile.children, tile.value, @@ -1157,6 +1160,13 @@ if __name__ == "__main__": help="Use the rectify partitioning scheme. This is like " "squarify, but tries to match the aspect ratio of the " "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( '--to-scale', nargs='?', diff --git a/scripts/treemapd3.py b/scripts/treemapd3.py index 02cb013d..878002e7 100755 --- a/scripts/treemapd3.py +++ b/scripts/treemapd3.py @@ -507,8 +507,8 @@ 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(bdiv(aspect_ratio[0], aspect_ratio[1]), - bdiv(aspect_ratio[1], aspect_ratio[0])) + ratio = max(aspect_ratio[0] / aspect_ratio[1], + aspect_ratio[1] / aspect_ratio[0]) while i < len(children): # calculate initial aspect ratio @@ -750,13 +750,16 @@ def main(csv_paths, output, *, or (args.get('dice_and_slice') and (tile.depth & 1) == 0)): partition_dice(tile.children, tile.value, x__, y__, width__, height__) - elif args.get('squarify'): - partition_squarify(tile.children, tile.value, - x__, y__, width__, height__) - elif args.get('rectify'): + elif (args.get('squarify') + or args.get('squarify_ratio') + or args.get('rectify')): partition_squarify(tile.children, tile.value, 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: # default to binary partitioning partition_binary(tile.children, tile.value, @@ -1024,6 +1027,13 @@ if __name__ == "__main__": help="Use the rectify partitioning scheme. This is like " "squarify, but tries to match the aspect ratio of the " "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( '--to-scale', nargs='?',