diff options
| author | Terry Truong <terry06890@gmail.com> | 2022-06-17 15:33:40 +1000 |
|---|---|---|
| committer | Terry Truong <terry06890@gmail.com> | 2022-06-17 15:33:40 +1000 |
| commit | 6916e49e449461fbb9534ed51c70db6cf6d66f9f (patch) | |
| tree | e00e75f122ccd79af64372eb6e56044ec215e60f /backend | |
| parent | 27c38b53728818361692f947cb71a7184d0c986b (diff) | |
Make reduced-tree a bit more inclusive
Diffstat (limited to 'backend')
| -rwxr-xr-x | backend/data/genReducedTreeData.py | 44 | ||||
| -rwxr-xr-x | backend/data/trimTree.py | 3 | ||||
| -rwxr-xr-x | backend/server.py | 1 |
3 files changed, 26 insertions, 22 deletions
diff --git a/backend/data/genReducedTreeData.py b/backend/data/genReducedTreeData.py index ba223b0..0991f92 100755 --- a/backend/data/genReducedTreeData.py +++ b/backend/data/genReducedTreeData.py @@ -69,27 +69,9 @@ for name in minimalNames: if name == None: rootName = prevName print(f"New node set has {len(nodeMap)} nodes") -# Remove certain 'chain collapsible' nodes -print("Removing 'chain collapsible' nodes") -namesToRemove = set() -for (name, nodeObj) in nodeMap.items(): - if name not in minimalNames and len(nodeObj["children"]) == 1: - parentName = nodeObj["parent"] - childName = nodeObj["children"][0] - # Connect parent and child - nodeMap[parentName]["children"].remove(name) - nodeMap[parentName]["children"].append(childName) - nodeMap[childName]["parent"] = parentName - # Adjust child pSupport - nodeMap[childName]["pSupport"] &= nodeObj["pSupport"] - # Remember for removal - namesToRemove.add(name) -for name in namesToRemove: - del nodeMap[name] -print(f"New node set has {len(nodeMap)} nodes") # Merge-upward compsite-named nodes print("Merging-upward composite-named nodes") -namesToRemove2 = set() +namesToRemove = set() for (name, nodeObj) in nodeMap.items(): parent = nodeObj["parent"] if parent != None and compNameRegex.fullmatch(name) != None: @@ -100,6 +82,27 @@ for (name, nodeObj) in nodeMap.items(): nodeMap[n]["parent"] = parent nodeMap[n]["pSupport"] &= nodeObj["pSupport"] # Remember for removal + namesToRemove.add(name) +for name in namesToRemove: + del nodeMap[name] +print(f"New node set has {len(nodeMap)} nodes") +# Remove certain 'chain collapsible' nodes +print("Removing 'chain collapsible' nodes") +namesToRemove2 = set() +for (name, nodeObj) in nodeMap.items(): + hasOneChild = len(nodeObj["children"]) == 1 + isOnlyChild = nodeObj["parent"] != None and len(nodeMap[nodeObj["parent"]]["children"]) == 1 + if name not in minimalNames and (hasOneChild or isOnlyChild): + parentName = nodeObj["parent"] + children = nodeObj["children"] + # Connect parent and children + nodeMap[parentName]["children"].remove(name) + nodeMap[parentName]["children"].extend(children) + for n in children: + nodeMap[n]["parent"] = parentName + # Adjust child pSupport + nodeMap[n]["pSupport"] &= nodeObj["pSupport"] + # Remember for removal namesToRemove2.add(name) for name in namesToRemove2: del nodeMap[name] @@ -119,7 +122,8 @@ for (name, nodeObj) in nodeMap.items(): children = [row[0] for row in dbCur.execute("SELECT child FROM edges where node = ?", (name,))] newChildren = [n for n in children if not (n in nodeMap or n in namesToRemove) and - dbCur.execute("SELECT name from node_imgs WHERE name = ?", (n,)).fetchone() != None] + (dbCur.execute("SELECT name from node_imgs WHERE name = ?", (n,)).fetchone() != None or + dbCur.execute("SELECT name from linked_imgs WHERE name = ? AND otol_id2 IS NULL", (n,)).fetchone() != None)] newChildNames = newChildren[:max(0, PREF_NUM_CHILDREN - numChildren)] nodeObj["children"].extend(newChildNames) namesToAdd.extend(newChildNames) diff --git a/backend/data/trimTree.py b/backend/data/trimTree.py index 2642ca0..65e44e4 100755 --- a/backend/data/trimTree.py +++ b/backend/data/trimTree.py @@ -43,6 +43,7 @@ for name in nodesToKeep: parent = row[0] if parent not in nodesToKeep and parent not in ancestors: ancestors.add(parent) + name = parent continue break nodesToKeep.update(ancestors) @@ -50,7 +51,7 @@ print(f"Total of {len(nodesToKeep)} nodes to keep") # Find root node query = "SELECT name FROM nodes LEFT JOIN edges ON nodes.name = edges.child WHERE edges.node IS NULL LIMIT 1" (rootName,) = dbCur.execute(query).fetchone() -print(f"Found root node \"{rootName\"}") +print(f"Found root node '{rootName}'") # Traverse tree, looking for trimmable nodes print("Looking for trimmable nodes") nodeToTipsChg = {} diff --git a/backend/server.py b/backend/server.py index 2212b13..3cc3953 100755 --- a/backend/server.py +++ b/backend/server.py @@ -137,7 +137,6 @@ def lookupNodeInfo(name, useReducedTree): if nodeObj != None: if isinstance(nodeObj["imgName"], str): otolId = nodeObj["imgName"][:-4] # Convert filename excluding .jpg suffix - print(otolId) query = "SELECT images.id, images.src, url, license, artist, credit FROM" \ " nodes INNER JOIN node_imgs ON nodes.name = node_imgs.name" \ " INNER JOIN images ON node_imgs.img_id = images.id AND node_imgs.src = images.src" \ |
