From 8781fdb2b8c530a6c1531ae9e82221eb062e34fb Mon Sep 17 00:00:00 2001 From: Terry Truong Date: Sun, 29 Jan 2023 11:30:47 +1100 Subject: Adjust backend coding style Add line spacing, section comments, and import consistency --- backend/tol_data/gen_linked_imgs.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'backend/tol_data/gen_linked_imgs.py') diff --git a/backend/tol_data/gen_linked_imgs.py b/backend/tol_data/gen_linked_imgs.py index 7002e92..c9d7aac 100755 --- a/backend/tol_data/gen_linked_imgs.py +++ b/backend/tol_data/gen_linked_imgs.py @@ -5,11 +5,12 @@ Look for nodes without images in the database, and tries to associate them with images from their children """ +import argparse import re import sqlite3 DB_FILE = 'data.db' -# + COMPOUND_NAME_REGEX = re.compile(r'\[(.+) \+ (.+)]') UP_PROPAGATE_COMPOUND_IMGS = False @@ -18,14 +19,14 @@ def genData(dbFile: str) -> None: dbCon = sqlite3.connect(dbFile) dbCur = dbCon.cursor() dbCur.execute('CREATE TABLE linked_imgs (name TEXT PRIMARY KEY, otol_ids TEXT)') - # + print('Getting nodes with images') nodeToUsedId: dict[str, str] = {} # Maps name of node to otol ID of node to use image for query = 'SELECT nodes.name, nodes.id FROM nodes INNER JOIN node_imgs ON nodes.name = node_imgs.name' for name, otolId in dbCur.execute(query): nodeToUsedId[name] = otolId print(f'Found {len(nodeToUsedId)}') - # + print('Getting node depths') nodeToDepth: dict[str, int] = {} maxDepth = 0 @@ -33,6 +34,7 @@ def genData(dbFile: str) -> None: for nodeName in nodeToUsedId.keys(): nodeChain = [nodeName] lastDepth = 0 + # Add ancestors while True: row = dbCur.execute('SELECT parent FROM edges WHERE child = ?', (nodeName,)).fetchone() @@ -45,11 +47,12 @@ def genData(dbFile: str) -> None: if nodeName in nodeToDepth: lastDepth = nodeToDepth[nodeName] break + # Add depths for i in range(len(nodeChain)): nodeToDepth[nodeChain[-i-1]] = i + lastDepth maxDepth = max(maxDepth, lastDepth + len(nodeChain) - 1) - # + print('Finding ancestors to give linked images') depthToNodes: dict[int, list[str]] = {depth: [] for depth in range(maxDepth + 1)} for nodeName, depth in nodeToDepth.items(): @@ -70,12 +73,12 @@ def genData(dbFile: str) -> None: (tips,) = dbCur.execute('SELECT tips FROM nodes WHERE name == ?', (node,)).fetchone() if parent not in parentToCandidate or parentToCandidate[parent][1] < tips: parentToCandidate[parent] = (node, tips) - # + print('Replacing linked-images for compound nodes') for iterNum, node in enumerate(parentToCandidate.keys(), 1): if iterNum % 1e4 == 0: print(f'At iteration {iterNum}') - # + match = COMPOUND_NAME_REGEX.fullmatch(node) if match is not None: # Replace associated image with subname images @@ -85,12 +88,15 @@ def genData(dbFile: str) -> None: otolIdPair[0] = nodeToUsedId[subName1] if subName2 in nodeToUsedId: otolIdPair[1] = nodeToUsedId[subName2] + # Use no image if both subimages not found if otolIdPair[0] == '' and otolIdPair[1] == '': dbCur.execute('DELETE FROM linked_imgs WHERE name = ?', (node,)) continue + # Add to db dbCur.execute('UPDATE linked_imgs SET otol_ids = ? WHERE name = ?', (','.join(otolIdPair), node)) + # Possibly repeat operation upon parent/ancestors if UP_PROPAGATE_COMPOUND_IMGS: while True: @@ -104,14 +110,13 @@ def genData(dbFile: str) -> None: node = parent continue break - # + print('Closing database') dbCon.commit() dbCon.close() if __name__ == '__main__': - import argparse parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.parse_args() - # + genData(DB_FILE) -- cgit v1.2.3