Skip to content

Commit 5a91301

Browse files
committed
fix file I/O
1 parent 3180b67 commit 5a91301

3 files changed

Lines changed: 157 additions & 164 deletions

File tree

niftypet/nipet/aux_sig.py

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,50 @@
99

1010

1111
def constants_h5(pthfn):
12-
# open the HDF5 file
13-
f = h5py.File(pthfn, 'r')
14-
# coincidence event mode
15-
cncdmd = f['HeaderData']['AcqParameters']['EDCATParameters']['coinOutputMode'][0]
16-
if cncdmd == 802:
17-
# bytes per event in this mode:
18-
bpe = 6
19-
log.info("list-mode data in NOMINAL mode (6 bytes per event)")
20-
elif cncdmd == 803:
21-
bpe = 16
22-
log.error(
23-
"list-mode data in CALIBRATION mode (16 bytes per event) not currently supported")
24-
elif cncdmd == 805:
25-
bpe = 8
26-
log.error("the ist-mode data in ENERGY mode (8 bytes per event) not currently supported")
27-
else:
28-
bpe = 0
29-
log.error("list-mode data in UNKNOWN mode")
30-
31-
# toff: scan start time marker (used as offset)
32-
CntH5 = {
33-
'toff': f['HeaderData']['AcqStats']['frameStartCoincTStamp'][0],
34-
'Deff': f['HeaderData']['SystemGeometry']['effectiveRingDiameter'][0],
35-
'TFOV': f['HeaderData']['AcqParameters']['EDCATParameters']['transAxialFOV'][0],
36-
'cpitch': f['HeaderData']['SystemGeometry']['interCrystalPitch'][0],
37-
'bpitch': f['HeaderData']['SystemGeometry']['interBlockPitch'][0],
38-
'exLOR': f['HeaderData']['AcqParameters']['RxScanParameters']['extraRsForTFOV'][0],
39-
'axCB': f['HeaderData']['SystemGeometry']['axialCrystalsPerBlock'][0],
40-
'axBU': f['HeaderData']['SystemGeometry']['axialBlocksPerUnit'][0],
41-
'axUM': f['HeaderData']['SystemGeometry']['axialUnitsPerModule'][0],
42-
'axMno': f['HeaderData']['SystemGeometry']['axialModulesPerSystem'][0],
43-
'txCB': f['HeaderData']['SystemGeometry']['radialCrystalsPerBlock'][0],
44-
'txBU': f['HeaderData']['SystemGeometry']['radialBlocksPerUnit'][0],
45-
'txUM': f['HeaderData']['SystemGeometry']['radialUnitsPerModule'][0],
46-
'txMno': f['HeaderData']['SystemGeometry']['radialModulesPerSystem'][0],
47-
'MRD': f['HeaderData']['AcqParameters']['BackEndAcqFilters']['maxRingDiff'][0],
48-
'tau0': f['HeaderData']['AcqParameters']['EDCATParameters']['negCoincidenceWindow'][0],
49-
'tau1': f['HeaderData']['AcqParameters']['EDCATParameters']['posCoincidenceWindow'][0],
50-
'tauP': f['HeaderData']['AcqParameters']['EDCATParameters']['coincTimingPrecision'][0],
51-
'TOFC': f['HeaderData']['AcqParameters']['RxScanParameters']['tofCompressionFactor'][0],
52-
'LLD': f['HeaderData']['AcqParameters']['EDCATParameters']['lower_energy_limit'][0],
53-
'ULD': f['HeaderData']['AcqParameters']['EDCATParameters']['upper_energy_limit'][0],
54-
'BPE': bpe}
55-
56-
f.close()
12+
with h5py.File(pthfn, 'r') as f:
13+
# coincidence event mode
14+
cncdmd = f['HeaderData']['AcqParameters']['EDCATParameters']['coinOutputMode'][0]
15+
if cncdmd == 802:
16+
# bytes per event in this mode:
17+
bpe = 6
18+
log.info("list-mode data in NOMINAL mode (6 bytes per event)")
19+
elif cncdmd == 803:
20+
bpe = 16
21+
log.error(
22+
"list-mode data in CALIBRATION mode (16 bytes per event) not currently supported")
23+
elif cncdmd == 805:
24+
bpe = 8
25+
log.error(
26+
"the ist-mode data in ENERGY mode (8 bytes per event) not currently supported")
27+
else:
28+
bpe = 0
29+
log.error("list-mode data in UNKNOWN mode")
30+
31+
# toff: scan start time marker (used as offset)
32+
CntH5 = {
33+
'toff': f['HeaderData']['AcqStats']['frameStartCoincTStamp'][0],
34+
'Deff': f['HeaderData']['SystemGeometry']['effectiveRingDiameter'][0],
35+
'TFOV': f['HeaderData']['AcqParameters']['EDCATParameters']['transAxialFOV'][0],
36+
'cpitch': f['HeaderData']['SystemGeometry']['interCrystalPitch'][0],
37+
'bpitch': f['HeaderData']['SystemGeometry']['interBlockPitch'][0],
38+
'exLOR': f['HeaderData']['AcqParameters']['RxScanParameters']['extraRsForTFOV'][0],
39+
'axCB': f['HeaderData']['SystemGeometry']['axialCrystalsPerBlock'][0],
40+
'axBU': f['HeaderData']['SystemGeometry']['axialBlocksPerUnit'][0],
41+
'axUM': f['HeaderData']['SystemGeometry']['axialUnitsPerModule'][0],
42+
'axMno': f['HeaderData']['SystemGeometry']['axialModulesPerSystem'][0],
43+
'txCB': f['HeaderData']['SystemGeometry']['radialCrystalsPerBlock'][0],
44+
'txBU': f['HeaderData']['SystemGeometry']['radialBlocksPerUnit'][0],
45+
'txUM': f['HeaderData']['SystemGeometry']['radialUnitsPerModule'][0],
46+
'txMno': f['HeaderData']['SystemGeometry']['radialModulesPerSystem'][0],
47+
'MRD': f['HeaderData']['AcqParameters']['BackEndAcqFilters']['maxRingDiff'][0],
48+
'tau0': f['HeaderData']['AcqParameters']['EDCATParameters']['negCoincidenceWindow'][0],
49+
'tau1': f['HeaderData']['AcqParameters']['EDCATParameters']['posCoincidenceWindow'][0],
50+
'tauP': f['HeaderData']['AcqParameters']['EDCATParameters']['coincTimingPrecision'][0],
51+
'TOFC': f['HeaderData']['AcqParameters']['RxScanParameters']['tofCompressionFactor']
52+
[0],
53+
'LLD': f['HeaderData']['AcqParameters']['EDCATParameters']['lower_energy_limit'][0],
54+
'ULD': f['HeaderData']['AcqParameters']['EDCATParameters']['upper_energy_limit'][0],
55+
'BPE': bpe}
5756
return CntH5
5857

5958

niftypet/nipet/img/mmrimg.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,8 @@ def getinterfile_off(fmu, Cnt, Offst=OFFSET_DEFAULT):
155155
accounting for image offset (does slow interpolation).
156156
'''
157157
# pead the image file
158-
f = open(fmu, 'rb')
159-
mu = np.fromfile(f, np.float32)
160-
f.close()
158+
with open(fmu, 'rb') as f:
159+
mu = np.fromfile(f, np.float32)
161160

162161
# save_im(mur, Cnt, os.path.dirname(fmu) + '/mur.nii')
163162
# -------------------------------------------------------------------------
@@ -179,9 +178,8 @@ def getinterfile_off(fmu, Cnt, Offst=OFFSET_DEFAULT):
179178
def getinterfile(fim, Cnt):
180179
'''Return the floating point image file in an array from an Interfile file.'''
181180
# pead the image file
182-
f = open(fim, 'rb')
183-
im = np.fromfile(f, np.float32)
184-
f.close()
181+
with open(fim, 'rb') as f:
182+
im = np.fromfile(f, np.float32)
185183

186184
# pumber of voxels
187185
nvx = im.shape[0]
@@ -1016,19 +1014,17 @@ def hmu_offset(hdr):
10161014

10171015
def rd_hmu(fh):
10181016
# --read hdr file--
1019-
f = open(fh, 'r')
1020-
hdr = f.read()
1021-
f.close()
1017+
with open(fh, 'r') as f:
1018+
hdr = f.read()
10221019
# -----------------
10231020
# pegular expression to find the file name
10241021
p = re.compile(r'(?<=:=)\s*\w*[.]\w*')
10251022
i0 = hdr.find('!name of data file')
10261023
i1 = i0 + hdr[i0:].find('\n')
10271024
fbin = p.findall(hdr[i0:i1])[0]
10281025
# --read img file--
1029-
f = open(os.path.join(os.path.dirname(fh), fbin.strip()), 'rb')
1030-
im = np.fromfile(f, np.float32)
1031-
f.close()
1026+
with open(os.path.join(os.path.dirname(fh), fbin.strip()), 'rb') as f:
1027+
im = np.fromfile(f, np.float32)
10321028
# -----------------
10331029
return hdr, im
10341030

0 commit comments

Comments
 (0)