Esempio n. 1
0
def vsm(
    datain,
    mumaps,
    em,
    hst,
    rsinos,
    scanner_params,
    prcnt_scl=0.1,
    emmsk=False,
    return_uninterp=False,
    return_ssrb=False,
    return_mask=False,
):
    '''
    Voxel-driven scatter modelling (VSM).
    Obtain a scatter sinogram using the mu-maps (hardware and object mu-maps)
    an estimate of emission image, the prompt measured sinogram, an
    estimate of the randoms sinogram and a normalisation sinogram.
    Input:
    - datain:       Contains the data used for scatter-specific detector
                    normalisation.  May also include the non-corrected
                    emission image used for masking, when requested.
    - mumaps:       A tuple of hardware and object mu-maps (in this order).
    - em:           An estimate of the emission image.
    - hst:          Dictionary containing the histogrammed measured data into
                    sinograms.
    - rsinos:       Randoms sinogram (3D).  Needed for proper scaling of
                    scatter to the prompt data.
    - scanner_params: Scanner specific parameters.
    - prcnt_scl:    Ratio of the maximum scatter intensities below which the
                    scatter is not used for fitting it to the tails of prompt
                    data.  Default is 10%.
    - emmsk:        When 'True' it will use uncorrected emission image for
                    masking the sources (voxels) of photons to be used in the
                    scatter modelling.
    '''
    log = logging.getLogger(__name__)

    muh, muo = mumaps

    #-constants, transaxial and axial LUTs are extracted
    Cnt = scanner_params['Cnt']
    txLUT = scanner_params['txLUT']
    axLUT = scanner_params['axLUT']

    if emmsk and not os.path.isfile(datain['em_nocrr']):
        log.info(
            'reconstruction of emission data without scatter and attenuation correction for mask generation'
        )
        recnac = mmrrec.osemone(datain,
                                mumaps,
                                hst,
                                scanner_params,
                                recmod=0,
                                itr=3,
                                fwhm=2.0,
                                store_img=True)
        datain['em_nocrr'] = recnac.fpet

    #-get the normalisation components
    nrmcmp, nhdr = mmrnorm.get_components(datain, Cnt)

    #-smooth for defining the sino scatter only regions
    mu_sctonly = ndi.filters.gaussian_filter(mmrimg.convert2dev(muo, Cnt),
                                             fwhm2sig(0.42, Cnt),
                                             mode='mirror')

    if Cnt['SPN'] == 1:
        snno = Cnt['NSN1']
        snno_ = Cnt['NSN64']
        ssrlut = axLUT['sn1_ssrb']
        saxnrm = nrmcmp['sax_f1']
    elif Cnt['SPN'] == 11:
        snno = Cnt['NSN11']
        snno_ = snno
        ssrlut = axLUT['sn11_ssrb']
        saxnrm = nrmcmp['sax_f11']

    #LUTs for scatter
    sctLUT = get_sctLUT(Cnt)

    #-smooth before down-sampling mu-map and emission image
    muim = ndi.filters.gaussian_filter(muo + muh,
                                       fwhm2sig(0.42, Cnt),
                                       mode='mirror')
    muim = ndi.interpolation.zoom(muim, Cnt['SCTSCLMU'],
                                  order=3)  #(0.499, 0.5, 0.5)

    emim = ndi.filters.gaussian_filter(em, fwhm2sig(0.42, Cnt), mode='mirror')
    emim = ndi.interpolation.zoom(emim, Cnt['SCTSCLEM'],
                                  order=3)  #(0.34, 0.33, 0.33)
    #emim = ndi.interpolation.zoom( emim, (0.499, 0.5, 0.5), order=3 )

    #-smooth the mu-map for mask creation.  the mask contains voxels for which attenuation ray LUT is found.
    smomu = ndi.filters.gaussian_filter(muim,
                                        fwhm2sig(0.84, Cnt),
                                        mode='mirror')
    mumsk = np.int8(smomu > 0.003)

    #CORE SCATTER ESTIMATION
    NSCRS, NSRNG = 64, 8
    sctout = {
        'xsxu':
        np.zeros((NSCRS, NSCRS / 2),
                 dtype=np.int8),  #one when xs>xu, otherwise zero
        'bin_indx':
        np.zeros((NSCRS, NSCRS / 2), dtype=np.int32),
        'sct_val':
        np.zeros((Cnt['TOFBINN'], NSRNG, NSCRS, NSRNG, NSCRS / 2),
                 dtype=np.float32),
        'sct_3d':
        np.zeros((Cnt['TOFBINN'], snno_, NSCRS, NSCRS / 2), dtype=np.float32)
    }

    #<<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>>
    petsct.scatter(sctout, muim, mumsk, emim, sctLUT, txLUT, axLUT, Cnt)
    #<<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>>
    sct3d = sctout['sct_3d']
    sctind = sctout['bin_indx']

    log.debug('total scatter sum:%r' % np.sum(sct3d))
    if np.sum(sct3d) < 1e-04:
        sss = np.zeros((snno, Cnt['NSANGLES'], Cnt['NSBINS']),
                       dtype=np.float32)
        amsksn = np.zeros((snno, Cnt['NSANGLES'], Cnt['NSBINS']),
                          dtype=np.float32)
        sssr = np.zeros((Cnt['NSEG0'], Cnt['NSANGLES'], Cnt['NSBINS']),
                        dtype=np.float32)
        return sss, sssr, amsksn

    #> get SSR for randoms from span-1 or span-11
    rssr = np.zeros((Cnt['NSEG0'], Cnt['NSANGLES'], Cnt['NSBINS']),
                    dtype=np.float32)
    for i in range(snno):
        rssr[ssrlut[i], :, :] += rsinos[i, :, :]

    #ATTENUATION FRACTIONS for scatter only regions, and NORMALISATION for all SCATTER
    #<<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>>
    currentspan = Cnt['SPN']
    Cnt['SPN'] = 1
    atto = np.zeros((txLUT['Naw'], Cnt['NSN1']), dtype=np.float32)
    petprj.fprj(atto, mu_sctonly, txLUT, axLUT, np.array([-1], dtype=np.int32),
                Cnt, 1)
    atto = mmraux.putgaps(atto, txLUT, Cnt)
    #--------------------------------------------------------------
    #get norm components setting the geometry and axial to ones as they are accounted for differently
    nrmcmp['geo'][:] = 1
    nrmcmp['axe1'][:] = 1
    #get sino with no gaps
    nrmg = np.zeros((txLUT['Naw'], Cnt['NSN1']), dtype=np.float32)
    mmr_auxe.norm(nrmg, nrmcmp, hst['buckets'], axLUT, txLUT['aw2ali'], Cnt)
    nrm = mmraux.putgaps(nrmg, txLUT, Cnt)
    #--------------------------------------------------------------

    #get attenuation + norm in (span-11) and SSR
    attossr = np.zeros((Cnt['NSEG0'], Cnt['NSANGLES'], Cnt['NSBINS']),
                       dtype=np.float32)
    nrmsssr = np.zeros((Cnt['NSEG0'], Cnt['NSANGLES'], Cnt['NSBINS']),
                       dtype=np.float32)
    for i in range(Cnt['NSN1']):
        si = axLUT['sn1_ssrb'][i]
        attossr[si, :, :] += atto[i, :, :] / float(axLUT['sn1_ssrno'][si])
        nrmsssr[si, :, :] += nrm[i, :, :] / float(axLUT['sn1_ssrno'][si])
    if currentspan == 11:
        Cnt['SPN'] = 11
        nrmg = np.zeros((txLUT['Naw'], snno), dtype=np.float32)
        mmr_auxe.norm(nrmg, nrmcmp, hst['buckets'], axLUT, txLUT['aw2ali'],
                      Cnt)
        nrm = mmraux.putgaps(nrmg, txLUT, Cnt)
    #--------------------------------------------------------------

    #get the mask for the object from uncorrected emission image
    if emmsk and os.path.isfile(datain['em_nocrr']):
        nim = nib.load(datain['em_nocrr'])
        A = nim.get_sform()
        eim = np.float32(nim.get_data())
        eim = eim[:, ::-1, ::-1]
        eim = np.transpose(eim, (2, 1, 0))

        em_sctonly = ndi.filters.gaussian_filter(eim,
                                                 fwhm2sig(.6, Cnt),
                                                 mode='mirror')
        msk = np.float32(em_sctonly > 0.07 * np.max(em_sctonly))
        msk = ndi.filters.gaussian_filter(msk,
                                          fwhm2sig(.6, Cnt),
                                          mode='mirror')
        msk = np.float32(msk > 0.01)
        msksn = mmrprj.frwd_prj(msk, txLUT, axLUT, Cnt)

        mssr = mmraux.sino2ssr(msksn, axLUT, Cnt)
        mssr = mssr > 0
    else:
        mssr = np.zeros((Cnt['NSEG0'], Cnt['NSANGLES'], Cnt['NSBINS']),
                        dtype=np.bool)

    #<<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>><<+>>

    #--------------------------------------------------------------------------------------------
    # get scatter sinos for TOF or non-TOF
    if Cnt['TOFBINN'] > 1:
        ssn = np.zeros((Cnt['TOFBINN'], snno, Cnt['NSANGLES'], Cnt['NSBINS']),
                       dtype=np.float64)
        sssr = np.zeros(
            (Cnt['TOFBINN'], Cnt['NSEG0'], Cnt['NSANGLES'], Cnt['NSBINS']),
            dtype=np.float32)
        tmp2d = np.zeros((Cnt['NSANGLES'] * Cnt['NSBINS']), dtype=np.float64)
        log.info('interpolate each scatter sino...')
        for k in range(Cnt['TOFBINN']):
            log.info('doing TOF bin k = %d' % k)
            for i in range(snno):
                tmp2d[:] = 0
                for ti in range(len(sctind)):
                    tmp2d[sctind[ti]] += sct3d[k, i, ti]
                #interpolate estimated scatter
                ssn[k, i, :, :] = get_sctinterp(
                    np.reshape(tmp2d, (Cnt['NSANGLES'], Cnt['NSBINS'])),
                    sctind, Cnt)
                sssr[k, ssrlut[i], :, :] += ssn[k, i, :, :]
            log.info('TOF bin #%d' % k)
    elif Cnt['TOFBINN'] == 1:
        ssn = np.zeros((snno, Cnt['NSANGLES'], Cnt['NSBINS']),
                       dtype=np.float32)
        sssr = np.zeros((Cnt['NSEG0'], Cnt['NSANGLES'], Cnt['NSBINS']),
                        dtype=np.float32)
        tmp2d = np.zeros((Cnt['NSANGLES'] * Cnt['NSBINS']), dtype=np.float32)
        log.info('scatter sinogram interpolation...')
        for i in trange(snno,
                        desc="interpolating",
                        unit="sinogram",
                        leave=log.getEffectiveLevel() < logging.INFO):
            tmp2d[:] = 0
            for ti in range(len(sctind)):
                tmp2d[sctind[ti]] += sct3d[0, i, ti]
            #interpolate estimated scatter
            ssn[i, :, :] = get_sctinterp(
                np.reshape(tmp2d, (Cnt['NSANGLES'], Cnt['NSBINS'])), sctind,
                Cnt)
            sssr[ssrlut[i], :, :] += ssn[i, :, :]
    #--------------------------------------------------------------------------------------------

    #=== scale scatter for ssr and non-TOF===
    #mask
    rmsk = (txLUT['msino'] > 0).T
    rmsk.shape = (1, Cnt['NSANGLES'], Cnt['NSBINS'])
    rmsk = np.repeat(rmsk, Cnt['NSEG0'], axis=0)
    amsksn = np.logical_and(attossr >= 0.999, rmsk) * ~mssr
    #scaling factors for ssr
    scl_ssr = np.zeros((Cnt['NSEG0']), dtype=np.float32)
    for sni in range(Cnt['NSEG0']):
        # region of choice for scaling
        thrshld = prcnt_scl * np.max(sssr[sni, :, :])
        amsksn[sni, :, :] *= (sssr[sni, :, :] > thrshld)
        amsk = amsksn[sni, :, :]
        #normalised estimated scatter
        mssn = sssr[sni, :, :] * nrmsssr[sni, :, :]
        mssn[np.invert(amsk)] = 0
        #vectorised masked sino
        vssn = mssn[amsk]
        vpsn = hst['pssr'][sni, amsk] - rssr[sni, amsk]
        scl_ssr[sni] = np.sum(vpsn) / np.sum(mssn)
        #ssr output
        sssr[sni, :, :] *= nrmsssr[sni, :, :] * scl_ssr[sni]

    #=== scale scatter for the proper sino ===
    sss = np.zeros((snno, Cnt['NSANGLES'], Cnt['NSBINS']), dtype=np.float32)
    for i in range(snno):
        sss[i, :, :] = ssn[i, :, :] * scl_ssr[ssrlut[i]] * saxnrm[i] * nrm[
            i, :, :]

    out = {}

    if return_uninterp:
        out['uninterp'] = sct3d
        out['indexes'] = sctind

    if return_ssrb:
        out['ssrb'] = sssr

    if return_mask:
        out['mask'] = amsksn

    if not out:
        return sss
    else:
        out['sino'] = sss
        return out
Esempio n. 2
0
def frwd_prj(im,
             scanner_params,
             isub=np.array([-1], dtype=np.int32),
             dev_out=False,
             attenuation=False):
    ''' Calculate forward projection (a set of sinograms) for the provided input image.
        Arguments:
        im -- input image (can be emission or mu-map image).
        scanner_params -- dictionary of all scanner parameters, containing scanner constants,
            transaxial and axial look up tables (LUT).
        isub -- array of transaxial indices of all sinograms (angles x bins) used for subsets.
            when the first element is negative, all transaxial bins are used (as in pure EM-ML).
        dev_out -- if True, output sinogram is in the device form, i.e., with two dimensions
            (# bins/angles, # sinograms) instead of default three (# sinograms, # bins, # angles).
        attenuation -- controls whether emission or LOR attenuation probability sinogram
            is calculated; the default is False, meaning emission sinogram; for attenuation
            calculations (attenuation=True), the exponential of the negative of the integrated
            mu-values along LOR path is taken at the end.
    '''
    log = logging.getLogger(__name__)

    # Get particular scanner parameters: Constants, transaxial and axial LUTs
    Cnt = scanner_params['Cnt']
    txLUT = scanner_params['txLUT']
    axLUT = scanner_params['axLUT']

    #>choose between attenuation forward projection (mu-map is the input)
    #>or the default for emission image forward projection
    if attenuation:
        att = 1
    else:
        att = 0

    if Cnt['SPN'] == 1:
        # number of rings calculated for the given ring range (optionally we can use only part of the axial FOV)
        NRNG_c = Cnt['RNG_END'] - Cnt['RNG_STRT']
        # number of sinos in span-1
        nsinos = NRNG_c**2
        # correct for the max. ring difference in the full axial extent (don't use ring range (1,63) as for this case no correction)
        if NRNG_c == 64:
            nsinos -= 12
    elif Cnt['SPN'] == 11:
        nsinos = Cnt['NSN11']
    elif Cnt['SPN'] == 0:
        nsinos = Cnt['NSEG0']

    if im.shape[0] == Cnt['SO_IMZ'] and im.shape[1] == Cnt[
            'SO_IMY'] and im.shape[2] == Cnt['SO_IMX']:
        ims = mmrimg.convert2dev(im, Cnt)
    elif im.shape[0] == Cnt['SZ_IMX'] and im.shape[1] == Cnt[
            'SZ_IMY'] and im.shape[2] == Cnt['SZ_IMZ']:
        ims = im
    elif im.shape[0] == Cnt['rSO_IMZ'] and im.shape[1] == Cnt[
            'SO_IMY'] and im.shape[2] == Cnt['SO_IMX']:
        ims = mmrimg.convert2dev(im, Cnt)
    elif im.shape[0] == Cnt['SZ_IMX'] and im.shape[1] == Cnt[
            'SZ_IMY'] and im.shape[2] == Cnt['rSZ_IMZ']:
        ims = im
    else:
        log.error(
            'wrong image size;  it has to be one of these: (z,y,x) = (127,344,344) or (y,x,z) = (320,320,128)'
        )

    log.debug('number of sinos:%d' % nsinos)

    #predefine the sinogram.  if subsets are used then only preallocate those bins which will be used.
    if isub[0] < 0:
        sinog = np.zeros((txLUT['Naw'], nsinos), dtype=np.float32)
    else:
        sinog = np.zeros((len(isub), nsinos), dtype=np.float32)

    # --------------------
    petprj.fprj(sinog, ims, txLUT, axLUT, isub, Cnt, att)
    # --------------------
    # get the sinogram bins in a proper sinogram
    sino = np.zeros((txLUT['Naw'], nsinos), dtype=np.float32)
    if isub[0] >= 0: sino[isub, :] = sinog
    else: sino = sinog

    # put the gaps back to form displayable sinogram
    if not dev_out:
        sino = mmraux.putgaps(sino, txLUT, Cnt)

    return sino