def analyze(dmax, alpha, n1, n2): global sasrec sasrec = saxs.Sasrec(Iq[n1:n2], dmax, qc=qc, r=None, alpha=alpha, ne=nes) Icinterp = np.interp(sasrec.q, sasrec.qc, np.abs(sasrec.Ic)) res = np.log10(np.abs(sasrec.I)) - np.log10(Icinterp) I_l2.set_data(sasrec.qc[:n2], sasrec.Ic[:n2]) Ires_l1.set_data(sasrec.q, res) P_l2.set_data(sasrec.r, sasrec.P) axrg.set_text("Rg = " + str(round(sasrec.rg, 2)) + " +- " + str(round(sasrec.rgerr, 2))) axI0.set_text("I(0) = " + str(round(sasrec.I0, 2)) + " +- " + str(round(sasrec.I0err, 2))) axVp.set_text("Vp = " + str(round(sasrec.Vp, 2)) + " +- " + str(round(sasrec.Vperr, 2))) axVpmw.set_text("Vp MW = " + str(round(sasrec.mwVp, 2)) + " +- " + str(round(sasrec.mwVperr, 2))) axVcmw.set_text("Vc MW = " + str(round(sasrec.mwVc, 2)) + " +- " + str(round(sasrec.mwVcerr, 2))) axlc.set_text("Lc = " + str(round(sasrec.lc, 2)) + " +- " + str(round(sasrec.lcerr, 2)))
def update(val): dmax = sdmax.val alpha = salpha.val #nes = int(snes.val) global sasrec sasrec = saxs.Sasrec(Iq, dmax, qc=qc, r=None, alpha=alpha, ne=nes) I_l2.set_data(sasrec.qc, sasrec.Ic) res = np.log10(np.abs(sasrec.I)) - np.log10( np.interp(sasrec.q, sasrec.qc, np.abs(sasrec.Ic))) Ires_l1.set_data(sasrec.q, res) P_l2.set_data(sasrec.r, sasrec.P) axrg.set_text("Rg = " + str(round(sasrec.rg, 2)) + " +- " + str(round(sasrec.rgerr, 2))) axI0.set_text("I(0) = " + str(round(sasrec.I0, 2)) + " +- " + str(round(sasrec.I0err, 2))) axVp.set_text("Vp = " + str(round(sasrec.Vp, 2)) + " +- " + str(round(sasrec.Vperr, 2))) axVpmw.set_text("Vp MW = " + str(round(sasrec.mwVp, 2)) + " +- " + str(round(sasrec.mwVperr, 2))) axVcmw.set_text("Vc MW = " + str(round(sasrec.mwVc, 2)) + " +- " + str(round(sasrec.mwVcerr, 2))) axlc.set_text("Lc = " + str(round(sasrec.lc, 2)) + " +- " + str(round(sasrec.lcerr, 2))) #axI.set_ylim([0.9*np.min(sasrec.Ic),1.1*np.max(sasrec.Ic)]) #axP.set_xlim([0,1.1*np.max(sasrec.r)]) fig.canvas.draw_idle()
def analyze(dmax, alpha, n1, n2, extrapolate): global sasrec sasrec = saxs.Sasrec(Iq_orig[n1:n2], dmax, qc=qc, r=r, nr=args.nr, alpha=alpha, ne=nes, extrapolate=extrapolate) Icinterp = np.interp(sasrec.q_data, sasrec.qc, np.abs(sasrec.Ic)) res = sasrec.I_data / sasrec.Ierr_data - Icinterp / sasrec.Ierr_data ridx = np.where((sasrec.q_data < sasrec.qc.max())) I_l1.set_data(sasrec.q_data, sasrec.I_data) I_l2.set_data(sasrec.qc, sasrec.Ic) I_l3.set_data(sasrec.qn, sasrec.In) Ires_l1.set_data(sasrec.q_data[ridx], res[ridx]) P_l2.set_data(sasrec.r, sasrec.P) axI0.set_text("$I(0)$ = %.2e $\pm$ %.2e" % (sasrec.I0, sasrec.I0err)) axrg.set_text("$R_g$ = %.2e $\pm$ %.2e" % (sasrec.rg, sasrec.rgerr)) axrav.set_text("$\overline{r}$ = %.2e $\pm$ %.2e" % (sasrec.avgr, sasrec.avgrerr)) axVp.set_text("$V_p$ = %.2e $\pm$ %.2e" % (sasrec.Vp, sasrec.Vperr)) axVc.set_text("$V_c$ = %.2e $\pm$ %.2e" % (sasrec.Vc, sasrec.Vcerr)) axlc.set_text("$\ell_c$ = %.2e $\pm$ %.2e" % (sasrec.lc, sasrec.lcerr))
def analyze(dmax, alpha, n1, n2): global sasrec sasrec = saxs.Sasrec(Iq[n1:n2], dmax, qc=qc, r=None, alpha=alpha, ne=nes) Icinterp = np.interp(sasrec.q, sasrec.qc, np.abs(sasrec.Ic)) res = np.log10(np.abs(sasrec.I)) - np.log10(Icinterp) I_l2.set_data(sasrec.qc[:n2], sasrec.Ic[:n2]) I_l3.set_data(sasrec.qn, sasrec.In) Ires_l1.set_data(sasrec.q, res) P_l2.set_data(sasrec.r, sasrec.P) axI0.set_text("I(0) = %.2e $\pm$ %.2e" % (sasrec.I0, sasrec.I0err)) axrg.set_text("Rg = %.2e $\pm$ %.2e" % (sasrec.rg, sasrec.rgerr)) axrav.set_text("$\overline{r}$ = %.2e $\pm$ %.2e" % (sasrec.avgr, sasrec.avgrerr)) axVp.set_text("Vp = %.2e $\pm$ %.2e" % (sasrec.Vp, sasrec.Vperr)) axVc.set_text("Vc = %.2e $\pm$ %.2e" % (sasrec.Vc, sasrec.Vcerr)) axlc.set_text("Lc = %.2e $\pm$ %.2e" % (sasrec.lc, sasrec.lcerr))
if args.max_alpha is None: args.max_alpha = 10. q = Iq[:, 0] qmin = np.min(q[0]) dq = q[1] - q[0] nq = int(qmin / dq) qc = np.concatenate(([0.0], np.arange(nq) * dq + (qmin - nq * dq), q)) #Icerr = np.concatenate((np.ones(nq+1)*Iq[0,2],Iq[:,2])) if args.qfile is not None: qc = np.loadtxt(args.qfile, usecols=(0, )) Icerr = np.interp(qc, q, Iq[:, 2]) sasrec = saxs.Sasrec(Iq, D, qc=qc, r=None, alpha=alpha, ne=nes) if args.plot: import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button, RadioButtons #fig, (axI, axP) = plt.subplots(1, 2, figsize=(12,6)) fig = plt.figure(0, figsize=(12, 6)) axI = plt.subplot2grid((3, 2), (0, 0), rowspan=2) axR = plt.subplot2grid((3, 2), (2, 0)) axP = plt.subplot2grid((3, 2), (0, 1), rowspan=3) plt.subplots_adjust(left=0.068, bottom=0.25, right=0.98, top=0.95) I_l1, = axI.plot(sasrec.q, sasrec.I, 'k.') I_l2, = axI.plot(sasrec.qc, sasrec.Ic, 'r-', lw=2) if args.log: axI.semilogy()
for i in range(waittime + 1): sys.stdout.write( "\rTo cancel, press CTRL-C in the next %d seconds. " % (waittime - i)) sys.stdout.flush() time.sleep(1) print() except KeyboardInterrupt: print("Canceling...") exit() #calculate chi2 when alpha=0, to get the best possible chi2 for reference sasrec = saxs.Sasrec(Iq[n1:n2], D, qc=qc, r=r, nr=args.nr, alpha=0.0, extrapolate=args.extrapolate) ideal_chi2 = sasrec.calc_chi2() if args.alpha is None: al = [] chi2 = [] #here, alphas are actually the exponents, since the range can #vary from 10^-10 upwards of 10^20. This should cover nearly all likely values alphas = np.arange(-20, 20.) i = 0 nalphas = len(alphas) for alpha in alphas: i += 1
n1 = 0 else: n1 = args.n1 if args.n2 is None: n2 = len(Iq[:, 0]) else: n2 = args.n2 Iq = Iq[n1:n2] if args.dmax is None: #estimate dmax directly from data D, sasrec = saxs.estimate_dmax(Iq, clean_up=True) else: D = args.dmax sasrec = saxs.Sasrec(Iq, D, alpha=0.0) if args.max_dmax is None: args.max_dmax = 2. * D if args.extrapolate: qe = np.linspace(1.0 * Iq[-1, 0], 3.0 * Iq[-1, 0], 1001) qsph = qe[qe > Iq[-1, 0]] #simulate scattering of sphere of Rg of particle Isph = saxs.sphere(q=qsph, R=3. / 5. * sasrec.rg**0.5) Isph += np.random.normal(Isph * 1e-2, size=len(Isph)) idx = np.where(Isph > 0) qsph = qsph[idx] Isph = Isph[idx] #scale extrapolated intensity to match data Isph *= Iq[-50:, 1].mean() / Isph[:20].mean()