acq2sqlite

Convert db.txt into a sqlite database.

Functions

column_names()

These names match what's used by dcmmeta2tsv.py and 00_build_db.bash CSA first, normal dicom headers, and then filename.

have_pipe_data()

Classes

DBQuery([sql])

Convenient SQL queries for tracking dicom headers/metadata.

class acq2sqlite.DBQuery(sql=None)[source]

Convenient SQL queries for tracking dicom headers/metadata.

This class is a poorly implemented, ad-hoc/bespoke ORM for database defined in schema.sql

CONSTS = ['Project', 'SequenceName', 'iPAT', 'Comments', 'SequenceType', 'PED_major', 'Phase', 'TR', 'TE', 'Matrix', 'PixelResol', 'BWP', 'BWPPE', 'FA', 'TA', 'FoV'][source]

CONSTS is a list of expected aquisition-invarient parameters. The values of these attributes should be the same for every acquisition sharing a Project × SequenceName pair (across all sessions of a Project).

We consider the acquisition to have a Quallity Assurance error when the value of any of these parameters in a single acquisition fails to match the template.

For example TR for task EPI acquisition identified by SequenceName=RewardedAnti in Project=WPC-8620 should always be 1300 ms.

../_images/nonconforming_example.png
check_acq(d)[source]

Is this exact acquisition (time, id, series) already in the database?

Parameters:

d (dict[str, str]) – All parameters of an acquisition

Returns:

True/False if dict params exist

Return type:

bool

dict_to_db_row(d)[source]

insert a dicom header (representative of acquisition) into db

Parameters:

d (dict[str, str])

Return type:

None

is_template(param_id)[source]

Check if param id is the ideal template.

Parameters:

param_id (int)

Return type:

bool

param_rowid(d)[source]
Parameters:

d (dict[str, str]) – dicom headers

Returns:

acq_param (new or existing) rowid identifying unique set of CONSTS

Return type:

int | None

Find or insert the combination of parameters for an acquisition. Using CONSTS, the header parameters that should be invariant across acquisitions of the same name within a study.

>>> db = DBQuery(sqlite3.connect(':memory:'))
>>> with open('schema.sql') as f: _ = [db.sql.execute(c) for c in f.read().split(";")]
...
>>> # db.sql.execute(".read schema.sql")
>>> example = {k: 'x' for k in db.CONSTS}
>>> db.param_rowid(example)
1
>>> db.param_rowid(example)
1
>>> db.param_rowid({**example, 'Project': 'b'})
2
>>> str(db.param_rowid({}))
'None'
search_acq_param(d)[source]

Try to find aca_param row id of CONSTS part of input d

Parameters:

d (dict[str, str]) – dictionary of tag values (keys in CONSTS)

Returns:

rowid of matching (param_id) or None

Return type:

int | None

tsv_to_dict(line)[source]

Read a tsv line into dictionary.

Parameters:

line (str) – tab separated string. likely line from dcmmeta2tsv.py

Returns:

dictionary with taglist.txt names and acquisition values.

Return type:

dict[str, str]

acq2sqlite.column_names()[source]

These names match what’s used by dcmmeta2tsv.py and 00_build_db.bash CSA first, normal dicom headers, and then filename.

Defaults to reading from taglist.txt This provides a language agnostic lookup for columns in schema.sql

AND
  • prepends Phase and iPAT

  • appends filename

These column names should match what is output by ./dcmmeta2tsv.bash or ./dcmmeta2tsv.py

Also see dcmmeta2tsv.read_known_tags()

>>> cn = column_names() # reads taglist.txt
>>> cn[0] # hard coded here
'Phase'
>>> cn[3] # from taglist.xt
'AcqDate'