Skip to content

Commit

Permalink
added decay correction to the CT-based alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
pjmark committed Jan 14, 2024
1 parent 0b5054a commit ccbcc28
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
80 changes: 70 additions & 10 deletions amypet/align_brkdyn_ct.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def align_break_petct(niidat, cts, Cnt, qcpth=None, refpetidx=None, use_stored=F
used as reference instead.
'''


# > output path from the input dictionary

Check failure on line 43 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L43

E303 too many blank lines (2)
Raw output
amypet/align_brkdyn_ct.py:43:5: E303 too many blank lines (2)
opth = niidat['outpath'].parent

Expand All @@ -55,11 +56,16 @@ def align_break_petct(niidat, cts, Cnt, qcpth=None, refpetidx=None, use_stored=F
outdct = np.load(fout, allow_pickle=True)
outdct = outdct.item()
return outdct
else:
# > output dictionary
outdct = {}

# > number of frames for break dynamic acquisitions A and B
nfrmA = len(niidat['series'][0])
nfrmB = len(niidat['series'][1])

outdct['nfrm_t'] = (nfrmA, nfrmB)

# > sort out the correct index for PET reference frame
if refpetidx is not None:
if any([refpetidx[0]<-nfrmA, refpetidx[1]<-nfrmB, refpetidx[0]>=nfrmA, refpetidx[1]>=nfrmB]):

Check failure on line 71 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L71

E225 missing whitespace around operator
Raw output
amypet/align_brkdyn_ct.py:71:29: E225 missing whitespace around operator

Check failure on line 71 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L71

E225 missing whitespace around operator
Raw output
amypet/align_brkdyn_ct.py:71:50: E225 missing whitespace around operator

Check failure on line 71 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L71

E225 missing whitespace around operator
Raw output
amypet/align_brkdyn_ct.py:71:71: E225 missing whitespace around operator

Check failure on line 71 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L71

E225 missing whitespace around operator
Raw output
amypet/align_brkdyn_ct.py:71:92: E225 missing whitespace around operator

Check failure on line 71 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L71

E501 line too long (101 > 99 characters)
Raw output
amypet/align_brkdyn_ct.py:71:100: E501 line too long (101 > 99 characters)
Expand All @@ -72,6 +78,38 @@ def align_break_petct(niidat, cts, Cnt, qcpth=None, refpetidx=None, use_stored=F
refpetidx[1] += nfrmB


# > DECAY CORRECTION (if requested)

Check failure on line 81 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L81

E303 too many blank lines (2)
Raw output
amypet/align_brkdyn_ct.py:81:5: E303 too many blank lines (2)
if Cnt['align']['decay_corr']:

Check failure on line 83 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L83

W293 blank line contains whitespace
Raw output
amypet/align_brkdyn_ct.py:83:1: W293 blank line contains whitespace
# > get the start time of each series for decay correction if requested
ts = [sri['time'][0] for sri in niidat['descr']]
# > index the earliest ref time
i_tref = np.argmin(ts)
idxs = list(range(len(ts)))
idxs.pop(i_tref)
i_tsrs = idxs
# > time difference
td = [ts[i] - ts[i_tref] for i in i_tsrs]
if len(td) > 1:
raise ValueError(
'currently only one dynamic break is allowed - detected more than one')
else:
td = td[0]

# > what tracer / radionuclide is used?
istp = 'F18' * (niidat['tracer'] in Cnt['tracer']['f18']) + 'C11' * (niidat['tracer'] in Cnt['tracer']['c11'])

Check failure on line 100 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L100

E501 line too long (118 > 99 characters)
Raw output
amypet/align_brkdyn_ct.py:100:100: E501 line too long (118 > 99 characters)

# > decay constant using half-life
lmbd = np.log(2) / nimpa.resources.riLUT[istp]['thalf']

# > decay correction factor
dcycrr = np.exp(lmbd * td)
else:
dcycrr = 1.

Check failure on line 109 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L109

W293 blank line contains whitespace
Raw output
amypet/align_brkdyn_ct.py:109:1: W293 blank line contains whitespace
outdct['dcycrr'] = dcycrr


# > reference images based on CT

Check failure on line 113 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L113

E303 too many blank lines (2)
Raw output
amypet/align_brkdyn_ct.py:113:5: E303 too many blank lines (2)
fctref = [None, None]
fpetref = [None, None]
Expand Down Expand Up @@ -235,9 +273,22 @@ def align_break_petct(niidat, cts, Cnt, qcpth=None, refpetidx=None, use_stored=F

for fi, f in enumerate(algn_frm[1]['faligned']):
print(f)
fcopy = algnFpth/Path(f).name
shutil.copyfile(f, fcopy)
faligned.append(fcopy)

Check failure on line 276 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L276

W293 blank line contains whitespace
Raw output
amypet/align_brkdyn_ct.py:276:1: W293 blank line contains whitespace
# > correction for decay
if Cnt['align']['decay_corr']:
fdcrr = algnFpth/(Path(f).name.split('.nii')[0]+'_dcrr.nii.gz')
imd = nimpa.getnii(f, output='all')
nimpa.array2nii(
imd['im']*dcycrr,
imd['affine'],
fdcrr,
trnsp=imd['transpose'],
flip=imd['flip'])
faligned.append(fdcrr)
else:
fcopy = algnFpth/Path(f).name
shutil.copyfile(f, fcopy)
faligned.append(fcopy)

faff = affsF/(Path(f).name.split('.nii')[0]+'_affine.txt')
shutil.copyfile(algn_frm[1]['affines'][fi], faff)
Expand Down Expand Up @@ -287,11 +338,23 @@ def align_break_petct(niidat, cts, Cnt, qcpth=None, refpetidx=None, use_stored=F

for fi, f in enumerate(algn_frm[i_acq]['faligned']):
print(f)
#fnii_org.append(niidat['series'][i_acq][keys2[fi]]['fnii'])

fcopy = algnFpth/Path(f).name
shutil.copyfile(f, fcopy)
faligned.append(fcopy)
# > correction for decay
if Cnt['align']['decay_corr']:
fdcrr = algnFpth/(Path(f).name.split('.nii')[0]+'_dcrr.nii.gz')
imd = nimpa.getnii(f, output='all')
nimpa.array2nii(
imd['im']*dcycrr,
imd['affine'],
fdcrr,
trnsp=imd['transpose'],
flip=imd['flip'])
faligned.append(fdcrr)
else:
fcopy = algnFpth/Path(f).name
shutil.copyfile(f, fcopy)
faligned.append(fcopy)


faff = affsF/(Path(f).name.split('.nii')[0]+'_affine.txt')

Check failure on line 359 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L359

E303 too many blank lines (2)
Raw output
amypet/align_brkdyn_ct.py:359:13: E303 too many blank lines (2)
shutil.copyfile(algn_frm[i_acq]['affines'][fi], faff)
Expand Down Expand Up @@ -335,9 +398,6 @@ def align_break_petct(niidat, cts, Cnt, qcpth=None, refpetidx=None, use_stored=F
#--------------------------------------------

Check failure on line 398 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L398

E265 block comment should start with '# '
Raw output
amypet/align_brkdyn_ct.py:398:5: E265 block comment should start with '# '



# > output dictionary
outdct = {}
outdct['align_acq'] = algn_frm

Check failure on line 401 in amypet/align_brkdyn_ct.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] amypet/align_brkdyn_ct.py#L401

E303 too many blank lines (2)
Raw output
amypet/align_brkdyn_ct.py:401:5: E303 too many blank lines (2)
outdct['ctref'] = fctref
outdct['petref'] = fpetref
Expand Down
3 changes: 2 additions & 1 deletion amypet/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pib = ["pib"]
flute = ["flt", "flut", "flute", "flutemetamol"]
fbb = ["fbb", "florbetaben"]
fbp = ["fbp", "florbetapir"]
mk6240 = ["mk6240", "mk-6240"]

[regpars]
fwhm_t1_mni = 3
Expand Down Expand Up @@ -55,5 +56,5 @@ fbb = [5400, 6600, 1200]
fbp = [3000, 3600, 600]

[tracer]
f18 = ['fbb', 'fbp', 'flute', 'mk-6240']
f18 = ['fbb', 'fbp', 'flute', 'mk6240']
c11 = ['pib']

0 comments on commit ccbcc28

Please sign in to comment.