Exemple #1
0
 def objective(p0, q, Iq, sd, y, Wy, Nr, weighted):
     j1, j2, j3, j4, j5, j6, j7, evi = iftv2(exp(p0[0]), p0[1], q, Iq,
                                             sd, Nr, y, Wy, weighted,
                                             smeared)
     return -evi
Exemple #2
0
 def objective(p0,q,Iq,sd,y,Wy,Nr,weighted):
     j1,j2,j3,j4,j5,j6,j7,evi = iftv2(exp(p0[0]),p0[1],q,Iq,sd,Nr,y,Wy,weighted,smeared)
     return -evi
Exemple #3
0
    def runift(self):
        # obtain parameters for solving P(r)
        self.getiftparams()

        # run fortran wrapper ift from iftreg module
        alpha = exp(self.data.logalpha)
        Dmax = self.data.Dmax
        Nr = self.data.Nr

        if self.data.smeared:
            y = self.y
            Wy = self.Wy
        else:
            y = None
            Wy = None

        q = self.data.q
        Iq = self.data.Iq
        sd = self.data.sd

        qmin_id = self.data.datarange[0]
        qmax_id = self.data.datarange[1]

        q = q[qmin_id:qmax_id]
        Iq = Iq[qmin_id:qmax_id]
        sd = sd[qmin_id:qmax_id]

        # solve once for extrapolation
        Jreg, Ireg, Jreg_extrap, Ireg_extrap, q_full, r, pr, evi = iftv2(
            alpha, Dmax, q, Iq, sd, Nr, y, Wy, self.weighdata,
            self.data.smeared)

        r = linspace(0.0, Dmax, Nr)

        # do resampling to estimate error in P(r)
        Nerr = 100
        Pr_list = zeros((Nerr, Nr))
        # precompute smearing matrix
        if self.data.smeared:
            K = trans(q, r, y=y, Wy=Wy)
            Kunsmeared = trans(q, r)
        else:
            K = trans(q, r)
            Kunsmeared = K * 1.0

        for n in range(Nerr):
            wrkiq = normal(loc=Iq, scale=sd)
            _, _, r, wrkpr, _ = iftv3(K, Kunsmeared, alpha, Dmax, q, wrkiq, sd,
                                      Nr, self.weighdata, self.data.smeared)
            Pr_list[n, :] = wrkpr

        # assign full angular range to data
        self.data.q_full = q_full
        # calculate chi-square for fit
        chi = ((Iq - Jreg)**2 / sd**2).mean()

        # assign solution to saxsdata
        pr, pr_error = Pr_list.mean(axis=0), Pr_list.std(axis=0)
        self.data.r = r
        self.data.pr = pr
        self.data.pr_error = pr_error
        self.data.Jreg = Jreg
        self.data.Ireg = Ireg
        self.data.solved = True

        # calculate real-space Rg, etc.
        dr = r[1] - r[0]
        Rg_sq = 0.5 * (pr * r**2 * dr).sum() / (pr * dr).sum()
        self.data.Rg = sqrt(Rg_sq)

        # Calculate SAXSMoW ...
        dq = q[1] - q[0]
        Iq_q = Ireg_extrap * q_full
        # Integrate using Trapezoid rule
        #Qinv = 0.5*dq * (Iq_q[0] + Iq_q[-1] + 2*(Iq_q[1:-1]).sum())
        # Integrate using Simpsons's rule
        Qinv = simps(y=Iq_q, x=q_full)

        Vc = Ireg_extrap[0] / Qinv

        ramboratio = Vc**2 / self.data.Rg

        # from saxsmow calibration
        # slope, offset  = saxsmow(q.max())

        # using protein density 1.35 g/mL
        # 1.35 g/cm^3 x 10^-3 kg/g x 1 Dalton/1.66e-27kg x 1cm^3/1e24 Angstrom^3
        # is 0.81325 Dalton/Angstrom^3
        #saxsmw = (slope*appVol + offset) * 0.81325e-3
        # Using the numbers from biosis.net (Robert Rambo)
        # ONLY for protein / complexes, will have to be different for nucleic acid / complex
        saxsmw = (ramboratio / 0.1231)**1.0
        saxsmw = saxsmw * 0.001
        chi_str = "Chi^2: {0:5.3f}\nI(0): {1:8.3E}\nM.W.: {2:4.1f}kDa".format(
            chi, Ireg_extrap[0], saxsmw)
        chi_textitem = TextItem(chi_str, color='k')
        chi_textitem.setPos(q.max() * 0.65, Ireg_extrap[0] * 0.9)
        chi_textitem.savex = q.max() * 0.65
        chi_textitem.savey = Ireg_extrap[0] * 0.9

        realRg_textitem = pg.TextItem("Rg_real : {0:7.2f}".format(sqrt(Rg_sq)),
                                      color='k',
                                      anchor=(0, 0))
        realRg_textitem.setPos(r.max() * 0.6, pr.max() * 0.9)
        #realpars_textitem = pg.TextItem()

        self.prplot.clear()
        self.rawplot.clear()

        self.rawplot.addItem(self.Iq_errorbar)
        self.rawplot.plot(q, Iq, pen=self.redpen)
        self.rawplot.plot(q_full, Jreg_extrap, pen=self.blackpen)
        # only plot the unsmeared (Ireg) when data is smeared
        # otherwise the lines will just overlay and looks ugly
        if self.data.smeared:
            self.rawplot.plot(q_full, Ireg_extrap, pen=self.bluepen)
        self.rawplot.addItem(chi_textitem)

        self.prplot.addItem(realRg_textitem)
        # add P(r) error bars
        pr_errorbar = ErrorBarItem(x=r,
                                   y=pr,
                                   height=pr_error,
                                   beam=0,
                                   pen={
                                       'color': '#a9a9a9',
                                       'width': 2
                                   })
        self.prplot.addItem(pr_errorbar)
        self.prplot.plot(r, pr, pen=self.redpen)

        # after solving, plot the unsmeared normalized kratky plot
        # x = q*Rg
        # y = (q*Rg)^2 * I(q)/I(0)
        xc_kratky = q_full * self.data.Rg
        yc_kratky = (xc_kratky)**2 * Jreg_extrap / Jreg_extrap[0]

        # update the raw data with the Real Space Rg
        x_kratky = q * self.data.Rg
        y_kratky = (x_kratky)**2 * Iq / Jreg_extrap[0]
        yerr_kratky = (x_kratky)**2 * sd / Jreg_extrap[0]
        kratky_errorbar_corr = ErrorBarItem(x=x_kratky,y=y_kratky,height=yerr_kratky,\
            beam=0,pen={'color':'#a9a9a9','width':2})
        self.kratkyplot.clear()
        self.kratkyplot.addItem(kratky_errorbar_corr)
        self.kratkyplot.plot(x_kratky, y_kratky, pen=self.redpen)
        self.kratkyplot.setLabel('bottom', 'q x Rg')
        self.kratkyplot.setLabel('left', '(q*Rg)<sup>2</sup> x I(q)/I(0)')

        self.kratkyplot.addLine(x=1.73, pen=self.graydashedpen)
        self.kratkyplot.plot(xc_kratky, yc_kratky, pen=self.blackpen)

        # save file (if user wants to)
        savefile = int(self.saveiftresult.checkState())

        if savefile > 0:
            fn_out = str(self.iftresultout.text())

            # integrate P(r) to 1
            dr = r[1] - r[0]
            prscale = pr.sum() * dr
            pr = pr / prscale
            pr_error = pr_error / prscale
            fhd_out = open(fn_out, "wt")
            for n in range(Nr):
                fhd_out.write("{0:5.2f} {1:6.4E} {2:6.4E}\n".format(
                    r[n], pr[n], pr_error[n]))

            fhd_out.close()

            print("saved to {0}".format(fn_out))
Exemple #4
0
    def runift(self):
        # obtain parameters for solving P(r)
        self.getiftparams()

        # run fortran wrapper ift from iftreg module
        alpha = exp(self.data.logalpha)
        Dmax  = self.data.Dmax
        Nr = self.data.Nr

        if self.data.smeared:
            y  = self.y
            Wy = self.Wy
        else:
            y = None
            Wy = None
            
        q  = self.data.q
        Iq = self.data.Iq
        sd = self.data.sd
        
        qmin_id = self.data.datarange[0]
        qmax_id = self.data.datarange[1]

        q  = q[qmin_id:qmax_id]
        Iq = Iq[qmin_id:qmax_id]
        sd = sd[qmin_id:qmax_id]

        Jreg,Ireg, Jreg_extrap, Ireg_extrap, q_full, r,pr,evi = iftv2(alpha,Dmax,q,Iq,sd,Nr,y,Wy,self.weighdata,self.data.smeared)

        # assign full angular range to data
        self.data.q_full = q_full
        # calculate chi-square for fit
        chi = ((Iq-Jreg)**2/sd**2).mean()

        # assign solution to saxsdata
        self.data.r, self.data.pr = r, pr
        self.data.Jreg = Jreg
        self.data.Ireg = Ireg
        self.data.solved = True

        # calculate real-space Rg, etc.
        dr = r[1]-r[0]
        Rg_sq = 0.5 * (pr * r**2 * dr).sum() / (pr*dr).sum()
        self.data.Rg = sqrt(Rg_sq)

        # Calculate SAXSMoW ...
        dq = q[1]-q[0]
        Iq_q = Ireg_extrap * q_full 
        # Integrate using Trapezoid rule
        #Qinv = 0.5*dq * (Iq_q[0] + Iq_q[-1] + 2*(Iq_q[1:-1]).sum())
        # Integrate using Simpsons's rule
        Qinv  = simps(y=Iq_q,x=q_full)

        Vc = Ireg_extrap[0]/Qinv
        
        ramboratio = Vc**2 / self.data.Rg

        # from saxsmow calibration
        # slope, offset  = saxsmow(q.max())
        
        # using protein density 1.35 g/mL
        # 1.35 g/cm^3 x 10^-3 kg/g x 1 Dalton/1.66e-27kg x 1cm^3/1e24 Angstrom^3
        # is 0.81325 Dalton/Angstrom^3
        #saxsmw = (slope*appVol + offset) * 0.81325e-3 
        # Using the numbers from biosis.net (Robert Rambo)
        # ONLY for protein / complexes, will have to be different for nucleic acid / complex
        saxsmw = (ramboratio/0.1231)**1.0
        saxsmw = saxsmw * 0.001
        chi_str = "Chi^2: {0:5.3f}\nI(0): {1:8.3E}\nM.W.: {2:4.1f}kDa".format(chi,Ireg_extrap[0],saxsmw)
        chi_textitem = TextItem(chi_str, color='k')
        chi_textitem.setPos(q.max()*0.65,Ireg_extrap[0]*0.9)
        chi_textitem.savex = q.max()*0.65
        chi_textitem.savey = Ireg_extrap[0]*0.9

        realRg_textitem = pg.TextItem("Rg_real : {0:7.2f}".format(sqrt(Rg_sq)), color='k', anchor=(0,0))
        realRg_textitem.setPos(r.max()*0.6,pr.max()*0.9)
        #realpars_textitem = pg.TextItem()

        self.prplot.clear()
        self.rawplot.clear()

        self.rawplot.addItem(self.Iq_errorbar)
        self.rawplot.plot(q,Iq,pen=self.redpen)
        self.rawplot.plot(q_full,Jreg_extrap,pen=self.blackpen)
        # only plot the unsmeared (Ireg) when data is smeared
        # otherwise the lines will just overlay and looks ugly
        if self.data.smeared:
            self.rawplot.plot(q_full,Ireg_extrap,pen=self.bluepen)
        self.rawplot.addItem(chi_textitem)

        self.prplot.addItem(realRg_textitem)
        self.prplot.plot(r,pr,pen=self.redpen)

        # after solving, plot the unsmeared normalized kratky plot
        # x = q*Rg
        # y = (q*Rg)^2 * I(q)/I(0)
        xc_kratky = q_full * self.data.Rg 
        yc_kratky = (xc_kratky)**2 * Jreg_extrap/Jreg_extrap[0]
        # update the raw data with the Real Space Rg
        x_kratky = q * self.data.Rg
        y_kratky = (x_kratky)**2 * Iq/Jreg_extrap[0]

        self.kratkyplot.clear()
        self.kratkyplot.plot(x_kratky, y_kratky, pen=self.redpen)
        self.kratkyplot.setLabel('bottom','q x Rg')
        self.kratkyplot.setLabel('left','(q*Rg)<sup>2</sup> x I(q)/I(0)')
        self.kratkyplot.addLine(x=1.73,pen=self.graydashedpen)
        self.kratkyplot.plot(xc_kratky, yc_kratky, pen=self.blackpen)        

        # save file (if user wants to)
        savefile = int(self.saveiftresult.checkState())

        if savefile>0:
            fn_out = str(self.iftresultout.text())
            
            # integrate P(r) to 1
            dr = r[1]-r[0]
            pr = pr/(pr.sum()*dr)

            fhd_out = open(fn_out,"wt")
            for n in range(Nr):
                fhd_out.write("{0:5.2f} {1:6.2E}\n".format(r[n],pr[n]))

            fhd_out.close()

            print "saved to {0}".format(fn_out)