Skip to content

Commit

Permalink
♻️ rename to acq2sqlite.py
Browse files Browse the repository at this point in the history
  • Loading branch information
WillForan committed Oct 25, 2024
1 parent a8b263e commit b44b423
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
1 change: 1 addition & 0 deletions 01_txt_to_sqlite.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./acq2sqlite.py
72 changes: 40 additions & 32 deletions 01_txt_to_sqlite.py → acq2sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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()
2 changes: 1 addition & 1 deletion sphinx/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b44b423

Please sign in to comment.