From b44b4235bf007df26c9e3a1c90fc8f9020145ceb Mon Sep 17 00:00:00 2001 From: WillForan Date: Thu, 24 Oct 2024 22:47:57 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20rename=20to=20acq2sqlite.p?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01_txt_to_sqlite.bash | 1 + 01_txt_to_sqlite.py => acq2sqlite.py | 72 +++++++++++++++------------- sphinx/index.rst | 2 +- 3 files changed, 42 insertions(+), 33 deletions(-) create mode 100755 01_txt_to_sqlite.bash rename 01_txt_to_sqlite.py => acq2sqlite.py (61%) diff --git a/01_txt_to_sqlite.bash b/01_txt_to_sqlite.bash new file mode 100755 index 00000000..326e3dfd --- /dev/null +++ b/01_txt_to_sqlite.bash @@ -0,0 +1 @@ +./acq2sqlite.py diff --git a/01_txt_to_sqlite.py b/acq2sqlite.py similarity index 61% rename from 01_txt_to_sqlite.py rename to acq2sqlite.py index 7583f7b7..aa804c1a 100755 --- a/01_txt_to_sqlite.py +++ b/acq2sqlite.py @@ -27,7 +27,6 @@ def column_names(): COLNAMES = column_names() -sql = sqlite3.connect("db.sqlite") # see schema.sql ### SQL queries # These are the header values (now sql columns) that should be consistant for an acquistion ('SequenceName') in a specific study ('Project') @@ -57,7 +56,7 @@ def column_names(): # otherwise we'll need to create a new row consts_ins_string = ",".join(CONSTS) -val_quests = ",".join(["?" for k in CONSTS]) +val_quests = ",".join(["?" for _ in CONSTS]) sql_cmd = f"INSERT INTO acq_param({consts_ins_string}) VALUES({val_quests});" ## we'll do the same thing for the acquisition paramaters (e.g. time and series number) that change very time -- only add if not already in the DB @@ -72,33 +71,42 @@ def column_names(): acq_insert = f"INSERT INTO acq({','.join(acq_insert_columns)}) VALUES({','.join(['?' for _ in acq_insert_columns])});" -with open("db.txt", "r") as f: - while line := f.readline(): - vals = line.split("\t") - d = dict(zip(COLNAMES, vals)) - # order here needs to match find_acq. - acq_search_vals = (d["AcqTime"], d["AcqDate"], d["SubID"], d["SeriesNumber"]) - cur = sql.execute(find_acq, acq_search_vals) - acq = cur.fetchone() - if acq: - print(f"have acq {acq[0]} {acq_search_vals}") - continue - - val_array = [d[k] for k in CONSTS] - print(f"searching: {val_array}") - cur = sql.execute(find_cmd, val_array) - res = cur.fetchone() - if res: - rowid = res[0] - print(f"seq repeated: found exiting {rowid}") - else: - cur = sql.execute(sql_cmd, val_array) - rowid = cur.lastrowid - print(f"new seq: created {rowid}") - ### - d["param_id"] = rowid - acq_insert_vals = [d[k] for k in acq_insert_columns] - cur = sql.execute(acq_insert, acq_insert_vals) - print(f"new acq: created {cur.lastrowid}") - -sql.commit() +def dict_to_db_row(d, sql): + """ + insert a dicom header (representive of acquistion) into db + """ + # order here needs to match find_acq. + acq_search_vals = (d["AcqTime"], d["AcqDate"], d["SubID"], d["SeriesNumber"]) + cur = sql.execute(find_acq, acq_search_vals) + acq = cur.fetchone() + if acq: + print(f"have acq {acq[0]} {acq_search_vals}") + return + + val_array = [d[k] for k in CONSTS] + print(f"searching: {val_array}") + cur = sql.execute(find_cmd, val_array) + res = cur.fetchone() + if res: + rowid = res[0] + print(f"seq repeated: found exiting {rowid}") + else: + cur = sql.execute(sql_cmd, val_array) + rowid = cur.lastrowid + print(f"new seq: created {rowid}") + ### + d["param_id"] = rowid + acq_insert_vals = [d[k] for k in acq_insert_columns] + cur = sql.execute(acq_insert, acq_insert_vals) + print(f"new acq: created {cur.lastrowid}") + + +if __name__ == "__main__": + sql = sqlite3.connect("db.sqlite") # see schema.sql + with open("db.txt", "r") as f: + while line := f.readline(): + vals = line.split("\t") + d = dict(zip(COLNAMES, vals)) + dict_to_db_row(d, sql) + + sql.commit() diff --git a/sphinx/index.rst b/sphinx/index.rst index a8a7d4a9..29384e9b 100644 --- a/sphinx/index.rst +++ b/sphinx/index.rst @@ -18,4 +18,4 @@ Code to parse dicoms into a template database and alert on non-conforming sequen change_header dcmmeta2tsv - 01_txt_to_sqlite + acq2sqlite