Commit 34ceb735 authored by William Clarke's avatar William Clarke
Browse files

Enabled tryAndFixLastMdh to handle broken files. Added test.

parent d7c9fa73
mapVBVD/_version.py export-subst
*.dat filter=lfs diff=lfs merge=lfs -text
......@@ -5,3 +5,5 @@ build
dist
*.egg-info
__pycache__
.DS_Store
......@@ -77,7 +77,10 @@ def loop_mdh_read( fid, version, Nscans, scan, measOffset, measLength,print_prog
try:
#read everything and cut out the mdh
data_u8 = np.fromfile(fid, dtype=np.uint8, count=int(ulDMALength))
if data_u8.size < int(ulDMALength):
raise EOFError
data_u8 = data_u8[mdhStart:]
except EOFError:
import warnings
warningString = f'\nAn unexpected read error occurred at this byte offset: {cPos} ({cPos/1024**3} GiB)\n'
......@@ -445,9 +448,9 @@ def mapVBVD(filename,quiet=False):
if isEOF:
#recover from read error
#for keys in currTwixObj:
# currTwixObj[keys].tryAndFixLastMdh()
print('tryAndFixLastMdh not yet implemented')
for keys in currTwixObj:
if keys != 'hdr':
currTwixObj[keys].tryAndFixLastMdh()
else:
for keys in currTwixObj:
if keys != 'hdr':
......
......@@ -2,6 +2,7 @@ from dataclasses import dataclass,field
import numpy as np
import copy
import time
import logging
class twix_map_obj:
def __init__(self, dataType,fname,version,rstraj):
......@@ -177,11 +178,45 @@ class twix_map_obj:
self.freeParam = mdh.aushFreePara[useScan].astype(float)
self.memPos = filePos[useScan]
def tryAndFixLastMdh(self):
isLastAcqGood = False
cnt = 0
while not isLastAcqGood and self.NAcq > 0 and cnt < 100:
try:
self.clean()
self.unsorted(self.NAcq)
isLastAcqGood = True
except Exception as e:
logging.exception(f'An error occurred whilst trying to fix last MDH. NAcq = {self.NAcq:.0f}')
self.isBrokenFile = True
self.NAcq -= 1
cnt += 1
def clean(self):
#Cut mdh data to actual size. Maybe we rejected acquisitions at the end
#due to read errors.
# WTC not implemented - nothing to test with
if self.NAcq==0:
return
fields = ['NCol', 'NCha',
'Lin', 'Par', 'Sli', 'Ave', 'Phs', 'Eco', 'Rep',
'Set', 'Seg', 'Ida', 'Idb', 'Idc', 'Idd', 'Ide',
'centerCol' , 'centerLin', 'centerPar', 'cutOff',
'coilSelect' , 'ROoffcenter', 'timeSinceRF', 'IsReflected',
'scancounter', 'timestamp', 'pmutime', 'IsRawDataCorrect',
'slicePos' , 'iceParam', 'freeParam', 'memPos']
nack = self.NAcq
idx = np.arange(0,nack-1)
for f in fields:
curr = getattr(self,f)
if curr.shape[0] > nack: # rarely
print('Here')
setattr(self, f, curr[idx]) # 1st dim: samples, 2nd dim acquisitions
self.NLin = np.max(self.Lin)+1 #+1 so that size isn't 0
self.NPar = np.max(self.Par)+1
......@@ -352,7 +387,7 @@ class twix_map_obj:
def unsorted(self,ival=None):
# returns the unsorted data [NCol,NCha,#samples in acq. order]
if ival:
mem = self.memPos(ival)
mem = np.atleast_1d(self.memPos[ival-1])
else:
mem = self.memPos
out = self.readData(mem)
......@@ -464,15 +499,16 @@ class twix_map_obj:
return fid
def readData(self,mem,cIxToTarg=None,cIxToRaw=None,selRange=None,selRangeSz=None,outSize=None):
mem = mem.astype(int)
if outSize is None:
if selRange is None:
selRange = [np.arange(0,self.dataSize()[0]).astype(int),np.arange(0,self.dataSize()[1]).astype(int)]#[slice(None,None,None),slice(None,None,None)]
else:
selRange[0] = np.arange(0,self.dataSize()[0]).astype(int) #slice(None,None,None)
selRange[1] = np.arange(0,self.dataSize()[0]).astype(int) #slice(None,None,None)
outSize = np.concatenate((self.dataSize()[0:2],mem.shape)).astype(int)
outSize = np.append(self.dataSize()[0:2],mem.size).astype(int)
selRangeSz = outSize
cIxToTarg = np.arange(0,selRangeSz[2])
cIxToRaw = cIxToTarg
......
......@@ -4,6 +4,7 @@ from mapVBVD import mapVBVD
test_data_vb = op.join(op.dirname(__file__),'test_data','meas_MID311_STEAM_wref1_FID115674.dat')
test_data_ve = op.join(op.dirname(__file__),'test_data','meas_MID00305_FID74175_VOI_slaser_wref1.dat')
test_data_vb_broken = op.join(op.dirname(__file__),'test_data','meas_MID111_sLaser_broken_FID4873.dat')
def test_vb():
......@@ -28,4 +29,16 @@ def test_ve():
keys = twixObj[1].search_header_for_keys(('sTXSPEC', 'asNucleusInfo'),top_lvl='MeasYaps')
assert ('sTXSPEC', 'asNucleusInfo', '0', 'tNucleus') in keys['MeasYaps']
key_value = twixObj[1].search_header_for_val('MeasYaps',('sTXSPEC', 'asNucleusInfo', '0', 'tNucleus'))
assert key_value[0] == '"1H"'
def test_vb_broken():
twixObj = mapVBVD(test_data_vb_broken,quiet=True)
assert np.allclose(twixObj.image.fullSize,[4096,32,1,1,1,1,1,1,1,97,1,1,1,1,1,1])
assert np.allclose(twixObj.image.sqzSize(),[4096,32,97])
keys = twixObj.search_header_for_keys(('sTXSPEC', 'asNucleusInfo'),top_lvl='MeasYaps')
assert ('sTXSPEC', 'asNucleusInfo', '0', 'tNucleus') in keys['MeasYaps']
key_value = twixObj.search_header_for_val('MeasYaps',('sTXSPEC', 'asNucleusInfo', '0', 'tNucleus'))
assert key_value[0] == '"1H"'
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment