Exemplo n.º 1
0
def update_wad(filename, mode, axorder, sporder, **kwargs):

    #sphereac = 999, ctimespac = '120000', bgac = 999, bgactime = '120000', spremac = 999, spremactime = '120000', bgremac = 999, bgremactime = '120000',scantime='120000'



    output = {}


    #Float values

    fmt = '%H%M%S' #assumed format for all times



    output['Sphere activity'] = kwargs['sphereac']
    output['Calibration time sphere'] = kwargs['ctimespac']



    output['Background activity'] = kwargs['bgac']
    output['Background act time'] = kwargs['bgactime']



    output['Sphere rem act'] = kwargs['spremac']
    output['Sphere rem act time'] = kwargs['spremactime']



    output['Bg rem act'] = kwargs['bgremac']
    output['Bg rem act time'] = kwargs['bgremactime']




    ''' With all the activities known we can simply calculate
the activity concentration of the spheres @ scantime and the activity concentration of the background @ scantime
    '''
    th = niqlib.thf18

    # convert all 'string' times to datetime format so we can calculate something

    scantime = datetime.strptime(kwargs['scantime'],fmt)
    ctimespac = datetime.strptime(kwargs['ctimespac'],fmt) 
    bgactime = datetime.strptime(kwargs['bgactime'],fmt) 
    spremactime = datetime.strptime(kwargs['spremactime'],fmt) 
    bgremactime = datetime.strptime(kwargs['bgremactime'],fmt) 



    actspsctime = niqlib.decay(float(kwargs['sphereac']),float((scantime - ctimespac).seconds/60.),th) - niqlib.decay(float(kwargs['spremac']),float((scantime - spremactime).seconds/60.),th) 

    actbgsctime = niqlib.decay(float(kwargs['bgac']),float((scantime - bgactime).seconds/60.),th) - niqlib.decay(float(kwargs['bgremac']),float((scantime - bgremactime).seconds/60.),th) 

    output['Act Spheres (scantime)'] = actspsctime
    output['Act BG (scantime)'] = actbgsctime


    output['Scantime'] = scantime.time()

    print axorder 

    print "1 - loading data"
    iqphantom = simple_nema_phantom(data=filename,mode=mode,order=axorder,orientation=sporder)

    print "z_slice bollen: ",iqphantom.z

    zslicefig = plt.figure()
    ax1 = zslicefig.add_subplot(111)

    plt.xtitle = "Z slice"
    plt.ytitle = "Counts in slice"
    #plt.plot(iqphantom.z_slice())
    #plt.show()


    # images

    output['EARL z_slice'] = iqphantom.z
    output['EARL zprofile'] = zslicefig



    print "2 - registering data"

    print "COR and angle before registration:", iqphantom.cor, iqphantom.angle

    #regpars = iqphantom.register_phantom()
    #print "registration pars:", regpars[0], regpars[1]
   # output['regpars'] = regpars

    print "COR and angle after registration:", iqphantom.cor, iqphantom.angle

    output['cor'] = iqphantom.cor
    output['angle'] = iqphantom.angle

    print "3 - show result: "
    print "zslice", iqphantom.z

    output['EARL halfslice'] = iqphantom.plot_dataspheres(iqphantom.z)

    

    print "4 - sphere statistics: "
    results  = iqphantom.get_stats()
    for key in results.keys():
        print "Sphere: ", key 
        tmpresult = results[key]
        for subkey in tmpresult.keys():
            print '\t', subkey, tmpresult[subkey]
    bgresults  = iqphantom.get_bg_stats()
    for key in bgresults.keys():
        print "BG Sphere: ", key 
        tmpresult = bgresults[key]
        for subkey in tmpresult.keys():
            print '\t', subkey, tmpresult[subkey]
        

    bgaverage = np.average([bgresults[key]['avg'] for key in bgresults.keys()])
    print bgaverage

    meancontrastlist = {}
    maxcontrastlist = {}
    for key in results.keys():
        meancontrastlist[key] = (results[key]['avg'] - bgaverage)/bgaverage
        maxcontrastlist[key] = (results[key]['max'] - bgaverage)/bgaverage

    output['maxcontrast'] = maxcontrastlist
    output['meancontrast'] = meancontrastlist


    print "5 - contrasts: "
    for key in meancontrastlist.keys():
        print "mean:", key, meancontrastlist[key]
        print "max:", key, maxcontrastlist[key]


    print "6 - rc list: "
    
    inputcontrast = (actspsctime - actbgsctime)/ actbgsctime
    meanrclist = {}
    maxrclist = {}

    for key in results.keys():
        meanrclist[key] = meancontrastlist[key]/inputcontrast
        maxrclist[key] = maxcontrastlist[key]/inputcontrast

    for key in meanrclist.keys():
        print "mean: ",key, meanrclist[key]
        print "max: ", key, maxrclist[key]

    output['maxrc'] = maxrclist
    output['meanrc'] = meanrclist


    output['RC-1'] = meanrclist[1]
    output['RC-2'] = meanrclist[2]
    output['RC-3'] = meanrclist[3]
    output['RC-4'] = meanrclist[4]
    output['RC-5'] = meanrclist[5]
    output['RC-6'] = meanrclist[6]

    output['EARL rc mean curve'] =  plot_rclist(meanrclist,'za')
    output['EARL rc max curve'] =  plot_rclist(maxrclist,'za')


    output['status'] = 'processed'

    print "Writing results to wadserver" 



    return output
Exemplo n.º 2
0
    def  __init__(self,data,order=['x','y','z'],phantomdim=[20,10],phantomvolume=9500,dose=75,dosecaltime='000000',remdose=0,remdosecaltime='000000'):


        print order
        self.xind = order.index('x')
        self.yind = order.index('y')
        self.zind = order.index('z')
        
        axes = [self.xind,self.yind,self.zind]
        print data
        pixelsize,array,info = niq.loaddata(data)

        print info
        
        self.pixel_size = pixelsize
        print "pixelsize: ", pixelsize

        self.phantomvolume = float(phantomvolume)
        self.dose = float(dose)
        self.dosecaltime = str(dosecaltime)
        self.remdose = float(remdose)
        self.remdosecaltime = str(remdosecaltime)

        
        self.rows = info.info["0028","0010"].value
        self.cols = info.info["0028","0011"].value

        self.vendor = info.info["0008","0070"].value
        
        if self.vendor == 'Philips Medical Systems':
            self.suvfactor = info.info["7053","1009"].value
        else:
            self.suvfactor = 1.0

        self.weight = info.info["0010","1030"].value

        print "Acquisition date",info.info["0008","0022"].value #Date
        acqtime = info.info["0008","0032"].value.split('.')[0] #Acq. Time and we remove the microseconds

        
        
        print "Acquisition time:  ", acqtime
        print "Activity calibrated: %s at %s "%(self.dose,self.dosecaltime)
        print "Remainder activity: %s at %s "%(self.remdose,self.remdosecaltime)

        self.fmt = '%H%M%S'
        self.fmt2 = '%H%M'

        

        t1 = datetime.strptime(acqtime, self.fmt) #acquisition time dcmhdr
        t2 = datetime.strptime(self.dosecaltime, self.fmt) #act. cal time
        t3 = datetime.strptime(self.remdosecaltime, self.fmt)#remaining activity cal time

        # Activity concentration at acquisitiontime:


        deltat0 = float((t1 - t2).seconds/60.) #minutes
        deltatr = float((t2 - t3).seconds/60.) #minutes


        self.currconc = niqlib.calc_act_con(self.phantomvolume,self.dose,deltat0,self.remdose,deltatr)

        print "Net activity at acquisition : %s "%self.currconc
Exemplo n.º 3
0
def update_wad(filename, mode, axorder, sporder, **kwargs):

    output = {}


    #Float results

    volume = kwargs['volume']
    output['volume'] = volume
    output['length'] = kwargs['dimensions'][0]
    output['radius'] = kwargs['dimensions'][1]
    output['activity'] = kwargs['bgac']

    output['caltime'] = kwargs['bgactime']
    output['remaindoer'] = kwargs['bgremac']
    output['remtime'] = kwargs['bgremactime']

    output['deltat0'] = 999999
    output['deltatr'] = 999999
    output['scanto'] = 999999

    output['maxsuv'] =999999
    output['avgsuv'] =999999
    output['minsuv'] =999999


    
    th = niqlib.thf18
    fmt = '%H%M%S' #assumed format for all times

    # convert all 'string' times to datetime format so we can calculate something

    scantime = datetime.strptime(kwargs['scantime'],fmt)
    ctimespac = datetime.strptime(kwargs['ctimespac'],fmt) 
    bgactime = datetime.strptime(kwargs['bgactime'],fmt) 
    spremactime = datetime.strptime(kwargs['spremactime'],fmt) 
    bgremactime = datetime.strptime(kwargs['bgremactime'],fmt) 

    actbgsctime = niqlib.decay(float(kwargs['bgac']),float((scantime - bgactime).seconds/60.),th) - niqlib.decay(float(kwargs['bgremac']),float((scantime - bgremactime).seconds/60.),th) 

    output['Act BG (scantime)'] = actbgsctime

    output['Scantime'] = scantime.time()



    print "Writing results to wadserver" 


    print "1 - loading data"
    phantom = uniform_phantom(data=filename, order=axorder,phantomdim=kwargs['dimensions'],phantomvolume=kwargs['volume'],dose=kwargs['bgac'],dosecaltime=kwargs['bgactime'],remdose=kwargs['bgremac'],remdosecaltime=kwargs['bgremactime'])

    print phantom.z
    output['z_slice'] = phantom.z

    zslicefig = plt.figure()
    ax1 = zslicefig.add_subplot(111)

    plt.xtitle = "Z slice"
    plt.ytitle = "Counts in slice"
    plt.plot(phantom.z_slice())

    #plt.show()

    output['EARL zprofile'] = zslicefig
    
    print "2 - info from header"
    """We need to calculate the activity concentration in Bq/ml from the
volume of the phantom and the administered activity.
     """

    # suv =  c(t) / (A(t) / bodyweight)
    # 
    # In the case of suv phantom:
    # suv = c(t) / (A(t) / 9.293), where A in MBq and c in kBq/cc
    # 
    # dicom[7053,1000] converts to SUV

    # For Philips specific:
    # There are 2 ways to calculate the suv
    # 1 - multiply pixel value with tag 7053,1000 and make sure the correct weight was used (or correct)
    # 2 - multiply pix. val. with tag 7053, 1009 (rescale slope) which converts to bq/cc and correct for decay
    

    suv = lambda x:1000*x*float(phantom.suvfactor)/phantom.currconc #option 2

 
    print "3 - show result: "

    sid  = phantom.z
    output['EARL histogram']=phantom.plot_img_hist(sid, bins=256,showfig=False)





    for k in [sid - 6, sid - 4, sid, sid + 4, sid + 6]:
        tmpfig, tmpunif,tmpstd = phantom.slice_uniformity(k,showfig=False)
        print "uniformity: ", k, tmpunif, tmpstd, actbgsctime, suv(tmpunif)
        output['SUV slice %s'%str(k - sid)] =  suv(tmpunif)
        output['COV slice %s'%str(k - sid)] =  tmpstd/tmpunif


    output['EARL halfslice'] =  phantom.plot_dataspheres(sid) #phantom.visualize_slices(sid)

    

    print "4 - sphere statistics: "
    

    bgresults  = phantom.get_bg_stats()

    meansuvlist = []
    maxsuvlist = []

    for key in bgresults.keys():
        print "BG Sphere: ", key 
        tmpresult = bgresults[key]
        meansuvlist.append([key,tmpresult['avg'],tmpresult['avg']/phantom.currconc])
        maxsuvlist.append([key,tmpresult['max'],tmpresult['max']/phantom.currconc])
        for subkey in tmpresult.keys():
            print '\t', subkey, tmpresult[subkey]


    print "SUVmean: ",meansuvlist
    print "SUVmax: ",maxsuvlist

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(zip(*meansuvlist)[2])
    #plt.show()
    


    
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(zip(*maxsuvlist)[2])
    output['EARL suvcurve'] = fig

    #plt.show()
    

    



    output['procstat'] = 'processed'
    output['status'] = 'processed'



    return output
Exemplo n.º 4
0
    def  __init__(self,data,order=['x','y','z'],phantomdim=[20,10],phantomvolume=9500,dose=75,dosecaltime='000000',remdose=0,remdosecaltime='000000'):


        print order
        self.xind = order.index('x')
        self.yind = order.index('y')
        self.zind = order.index('z')
        
        axes = [self.xind,self.yind,self.zind]
        print data
        pixelsize,array,info = niq.loaddata(data)

        print info
        
        self.pixel_size = pixelsize
        print "pixelsize: ", pixelsize

        self.phantomvolume = float(phantomvolume)
        self.dose = float(dose)
        self.dosecaltime = str(dosecaltime)
        self.remdose = float(remdose)
        self.remdosecaltime = str(remdosecaltime)

        
        self.rows = info.info["0028","0010"].value
        self.cols = info.info["0028","0011"].value

        self.vendor = info.info["0008","0070"].value
        
        if self.vendor == 'Philips Medical Systems':
            self.suvfactor = info.info["7053","1009"].value
        else:
            self.suvfactor = 1.0

        self.weight = info.info["0010","1030"].value

        print "Acquisition date",info.info["0008","0022"].value #Date
        acqtime = info.info["0008","0032"].value.split('.')[0] #Acq. Time and we remove the microseconds

        
        
        print "Acquisition time:  ", acqtime
        print "Activity calibrated: %s at %s "%(self.dose,self.dosecaltime)
        print "Remainder activity: %s at %s "%(self.remdose,self.remdosecaltime)

        self.fmt = '%H%M%S'
        self.fmt2 = '%H%M'

        

        t1 = datetime.strptime(acqtime, self.fmt) #acquisition time dcmhdr
        t2 = datetime.strptime(self.dosecaltime, self.fmt) #act. cal time
        t3 = datetime.strptime(self.remdosecaltime, self.fmt)#remaining activity cal time

        # Activity concentration at acquisitiontime:


        deltat0 = float((t1 - t2).seconds/60.) #minutes
        deltatr = float((t2 - t3).seconds/60.) #minutes


        self.currconc = niqlib.calc_act_con(self.phantomvolume,self.dose,deltat0,self.remdose,deltatr)

        print "Net activity at acquisition : %s "%self.currconc
        


#float(indata.info["0028","0030"].value[0])

        self.bgradius = 4

        



        self.data = np.array(array)

        print type(self.data)
        print type(self.data[0])

        tmpshape  = np.shape(self.data)

        print tmpshape
        
        self.ddimz = tmpshape[self.zind]
        self.ddimy = tmpshape[self.yind]
        self.ddimx = tmpshape[self.xind]

        self.maxdata = self.data.max()
        self.volume = np.zeros(np.shape(data))

#        self.mode = mode
        self.z = self.z_init()


        self.thrdata = copy.deepcopy(self.data)

        lowind = (self.thrdata < np.max(self.thrdata[self.z,:,:])*0.2)
        self.thrdata[lowind] = 0.0
        highind = (self.thrdata >= np.max(self.thrdata[self.z,:,:])*0.2)
        self.thrdata[highind] = 10

        self.bgdata = copy.deepcopy(self.data)

        self.roughcom = ndimage.measurements.center_of_mass(self.data[self.z,:,:])

        print "rough com location:", self.roughcom

        self.cor = self.roughcom
        self.bgspheres = {}

        self.set_bg_spheres()