-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ dcmmeta2tsv.py: slower but CSA capiable replacment for dicom_hinfo
- Loading branch information
Showing
4 changed files
with
115 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Give a tab separated metadata value line per dicom file. | ||
""" | ||
import os | ||
import sys | ||
import re | ||
import pydicom | ||
#import warnings | ||
#warnings.filterwarnings("ignore", module="nibabel.nicom.csareader") | ||
import nibabel.nicom.csareader as csareader | ||
|
||
def tagpair_to_hex(csv_str): | ||
""" | ||
move our text files has tags like "0051,1017" | ||
to pydicom indexe like (0x51,0x1017) | ||
""" | ||
return tuple(hex(int(x,16)) for x in csv_str.split(",")) | ||
|
||
def read_known_tags(tagfile="taglist.txt"): | ||
""" | ||
read in tsv file like with header name,tag,desc. | ||
skip comments and header | ||
""" | ||
with open(tagfile,'r') as f: | ||
tags = [dict(zip(["name","tag","desc"],line.split("\t"))) | ||
for line in f.readlines() | ||
if not re.search("^name|^#", line)] | ||
return tags | ||
|
||
if __name__ == "__main__": | ||
tags = read_known_tags() | ||
for i in range(len(tags)): | ||
tags[i]['tag'] = tagpair_to_hex(tags[i]['tag']) | ||
|
||
for dcm_path in sys.argv[1:]: | ||
if not os.path.isfile(dcm_path): | ||
raise Exception("Bad command line argument: '{dcm_path}' DNE") | ||
dcm = pydicom.dcmread(dcm_path) | ||
meta = [dcm[tag_d['tag']].value for tag_d in tags] | ||
|
||
csa_str = dcm[(0x0029,0x1010)].value | ||
csa_tr = csareader.read(csa_str) | ||
pedp = csa_tr['tags']['PhaseEncodingDirectionPositive']['items'] | ||
pedp = pedp[0] if pedp else "null" | ||
ipat = csa_tr['tags']['ImaPATModeText']['items'] | ||
ipat = ipat[0] if ipat else "null" | ||
# order here matches 00_build_db.bash | ||
csa_tags = [pedp, ipat] | ||
# NB. arrays are '[x, y, z]' instead of ' x y z ' or 'x/y' | ||
# like in dicom_hdr (00_build_db.bash) | ||
all_tags =[str(x) for x in csa_tags + meta] + [dcm_path] | ||
print("\t".join(all_tags)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name tag desc | ||
AcqTime 0008,0032 Acquisition Time like 145446.685000 | ||
AcqDate 0008,0022 like 20241004 | ||
SeriesNumber 0020,0011 REL Series Number | ||
SubID 0010,0010 patient name | ||
#iPAT 0051,1011 PATModeText (private field); not implemented, use CSA value ucPAT | ||
Comments 0020,4000 REL Image Comments//Unaliased MB3/PE4/LB SENSE1 | ||
Operator 0008,1070 | ||
Project 0008,1030 ID Study Description//Brain^wpc-8620 | ||
SequenceName 0008,103e series descripton | ||
SequenceType 0018,0024 ACQ Sequence Name | ||
PED_major 0018,1312 ACQ Phase Encoding Direction, ROW or COL | ||
TR 0018,0080 | ||
TE 0018,0081 | ||
Matrix 0018,1310 ACQ Acquisition Matrix | ||
PixelResol 0028,0030 IMG Pixel Spacing//2.2978723049164\2.2978723049164 | ||
#https://neurostars.org/t/how-is-bandwidthperpixelphaseencode-calculated/26526 (0021,1153) | ||
BWP 0018,0095 ACQ Pixel Bandwidth (?) also unimplemented? need CSA value? | ||
BWPPE 0019,1028 in matlab S.BandwidthPerPixelPhaseEncode; | ||
FA 0018,1314 | ||
TA 0051,100a | ||
FoV 0051,100c eg FoV 1617*1727; but actually cocaluated from matrix and spacing? |