diff options
| author | Terry Truong <terry06890@gmail.com> | 2022-06-19 00:39:38 +1000 |
|---|---|---|
| committer | Terry Truong <terry06890@gmail.com> | 2022-06-19 00:39:38 +1000 |
| commit | 8c8ed28c87e649f163aaa54899f4b00c9fc31224 (patch) | |
| tree | 59ee6849d60c1367d384ae7cc65b21376a0527b0 /backend | |
| parent | d17b9a09cb8082f43329f64a459ea6d62031ff21 (diff) | |
Fix linked_imgs table compound-img representation bug
Diffstat (limited to 'backend')
| -rwxr-xr-x | backend/data/genLinkedImgs.py | 21 | ||||
| -rwxr-xr-x | backend/server.py | 13 |
2 files changed, 18 insertions, 16 deletions
diff --git a/backend/data/genLinkedImgs.py b/backend/data/genLinkedImgs.py index 3f32d16..9fe07a2 100755 --- a/backend/data/genLinkedImgs.py +++ b/backend/data/genLinkedImgs.py @@ -17,8 +17,9 @@ upPropagateCompoundImgs = False # Open db dbCon = sqlite3.connect(dbFile) dbCur = dbCon.cursor() -dbCur.execute("CREATE TABLE linked_imgs (name TEXT PRIMARY KEY, otol_id TEXT, otol_id2 TEXT)") - # Associates a node with one (or two) otol-ids with usable images +dbCur.execute("CREATE TABLE linked_imgs (name TEXT PRIMARY KEY, otol_ids TEXT)") + # Associates a node with one (or two) otol-ids with usable images, + # encoded as 'otolId1' or 'otolId1,otolId2' # Get nodes with images print("Getting nodes with images") resolvedNodes = {} # Will map node names to otol IDs with a usable image @@ -58,7 +59,7 @@ while len(resolvedNodes) > 0: # Check if highest-tips child if (childObjs[0]["name"] == nodeName): # Resolve parent, and continue from it - dbCur.execute("INSERT INTO linked_imgs VALUES (?, ?, ?)", (parent, otolId, None)) + dbCur.execute("INSERT INTO linked_imgs VALUES (?, ?)", (parent, otolId)) del nodesToResolve[parent] processedNodes[parent] = otolId parentToChosenTips[parent] = childObjs[0]["tips"] @@ -75,7 +76,7 @@ while len(resolvedNodes) > 0: childObj = next(c for c in childObjs if c["otolId"] != None) resolvedNodes[name] = childObj["otolId"] parentToChosenTips[name] = childObj["tips"] - dbCur.execute("INSERT INTO linked_imgs VALUES (?, ?, ?)", (name, childObj["otolId"], None)) + dbCur.execute("INSERT INTO linked_imgs VALUES (?, ?)", (name, childObj["otolId"])) nodesToResolve.clear() # Iterate through processed nodes with compound names print("Replacing images for compound-name nodes") @@ -89,18 +90,18 @@ for nodeName in processedNodes.keys(): if match != None: # Replace associated image with subname images (subName1, subName2) = match.group(1,2) - otolIdPair = [None, None] + otolIdPair = ["", ""] if subName1 in processedNodes: otolIdPair[0] = processedNodes[subName1] if subName2 in processedNodes: otolIdPair[1] = processedNodes[subName2] # Use no image if both subimages not found - if otolIdPair[0] == None and otolIdPair[1] == None: + if otolIdPair[0] == "" and otolIdPair[1] == "": dbCur.execute("DELETE FROM linked_imgs WHERE name = ?", (nodeName,)) continue # Add to db - dbCur.execute("UPDATE linked_imgs SET otol_id = ?, otol_id2 = ? WHERE name = ?", - (otolIdPair[0], otolIdPair[1], nodeName)) + dbCur.execute("UPDATE linked_imgs SET otol_ids = ? WHERE name = ?", + (otolIdPair[0] + "," + otolIdPair[1], nodeName)) # Possibly repeat operation upon parent/ancestors if upPropagateCompoundImgs: while True: @@ -112,8 +113,8 @@ for nodeName in processedNodes.keys(): (numTips,) = dbCur.execute("SELECT tips from nodes WHERE name = ?", (nodeName,)).fetchone() if parent in parentToChosenTips and parentToChosenTips[parent] <= numTips: # Replace associated image - dbCur.execute("UPDATE linked_imgs SET otol_id = ?, otol_id2 = ? WHERE name = ?", - (otolIdPair[0], otolIdPair[1], parent)) + dbCur.execute("UPDATE linked_imgs SET otol_ids = ? WHERE name = ?", + (otolIdPair[0] + "," + otolIdPair[1], parent)) nodeName = parent continue break diff --git a/backend/server.py b/backend/server.py index 3cc3953..576a239 100755 --- a/backend/server.py +++ b/backend/server.py @@ -61,15 +61,16 @@ def lookupNodes(names, useReducedTree): nodeObjs[idsToNames[otolId]]["imgName"] = otolId + ".jpg" # Get 'linked' images for unresolved names unresolvedNames = [n for n in nodeObjs if nodeObjs[n]["imgName"] == None] - query = "SELECT name, otol_id, otol_id2 from linked_imgs WHERE name IN ({})" + query = "SELECT name, otol_ids from linked_imgs WHERE name IN ({})" query = query.format(",".join(["?"] * len(unresolvedNames))) - for (name, otolId, otolId2) in cur.execute(query, unresolvedNames): - if otolId2 == None: - nodeObjs[name]["imgName"] = otolId + ".jpg" + for (name, otolIds) in cur.execute(query, unresolvedNames): + if "," not in otolIds: + nodeObjs[name]["imgName"] = otolIds + ".jpg" else: + id1, id2 = otolIds.split(",") nodeObjs[name]["imgName"] = [ - otolId + ".jpg" if otolId != None else None, - otolId2 + ".jpg" if otolId2 != None else None, + id1 + ".jpg" if id1 != "" else None, + id2 + ".jpg" if id2 != "" else None, ] # Get preferred-name info query = f"SELECT name, alt_name FROM names WHERE pref_alt = 1 AND name IN ({queryParamStr})" |
