Esempio n. 1
0
 def fitCurrentNoise(self, *args, **kwargs):
     fMax = 1E5
     noises = tesModel.noiseComponents(*args, **kwargs)
     noiseTotal = np.sum(noises[component] for component in noises.keys())
     betaI, tau, L = TesAdmittance.guessBetaTauL(R0, fmin=10E3, fmax=fMax)
     C = tau*G
     f = TesAdmittance.f
     iFit = f < fMax
     model = lmfit.Model(noiseTotal, independent_vars='f')
     params = model.make_params()
     params['alphaI'].vary = True;  params['alphaI'].value = alphaI;
     params['betaI'].vary  = True;  params['betaI'].value = betaI;
     params['P'].vary      = False; params['P'].value = P0
     params['g_tesb'].vary = True;  params['g_tesb'].value = 0.9*G; params['g_tesb'].min = 0
     params['g_tes1'].vary = True;  params['g_tes1'].value = 0.1*G; params['g_tes1'].min = 0   # Wild guess
     params['Ctes'].vary   = True;  params['Ctes'].value = 0.8*C;  # params['Ctes'].min   = 0
     params['C1'].vary     = True;  params['C1'].value   = 0.2*C;  # params['C1'].min     = 0   # Wild guess
     params['T'].vary      = False; params['T'].value = T0
     params['R'].vary      = False; params['R'].value = R0
     
     result = model.fit(data=noiseTotal[iFit].view(dtype=np.float),
                    f=np.hstack([f[iFit], f[iFit]]),
                    params=params )
     print(result.fit_report())
Esempio n. 2
0
    #axes = thevenin.plot(None)

    ss = SineSweep(pathTf + tfFileName)
    print('Sine sweep comment:', ss.comment)

    metaData = ast.literal_eval(ss.comment)
    Rfb = metaData['Rfb']
    Cfb = metaData['Cfb']
    Vbias = metaData['Vbias']
    Vcoil = metaData['Vcoil']

    print('Sine-sweep amplitudes: SC=', ssSc.amplitude, 'Normal=',
          ssNormal.amplitude, 'Bias=', ss.amplitude)
    #print('Normal sine-sweep amplitude), ssNormal.amplitude,ss.amplitude,np.mean(ss.Vdc), np.std(ss.Vdc))

    tesAdmittance = TesAdmittance(ss, thevenin, Rsq)

    sweeps = IvSweepCollection(pathIv + ivFileName)
    print('IV sweep comment:', sweeps.info.comment)
    sweep = sweeps[1]
    print('IV sweep Rfb=', sweeps.info.pflRfb)
    sweep.findTesBias(tes.Rbias, tes.Rshunt, tes.Rsquid(sweeps.info.pflRfb))
    Tbase = sweep.Tadr
    sweep.applyThermalModel(tes.temperature, Tbase)
    I0, R0, T0, alphaIv = sweep.findAlphaIvCloseTo('Vbias',
                                                   Vbias,
                                                   n=10,
                                                   order=3)
    P0 = I0**2 * R0
    print('From IV sweep: R0=', R0 * 1E3, 'mOhm, I0=', 1E6 * I0, 'uA, P0=',
          P0 * 1E12, ' pW, T0=', T0 * 1E3, 'mK')
Esempio n. 3
0
def fitTransferFunction(ss, thevenin, Rsq, sweeps, axes=None, fMax=1E5):
    metaData = ast.literal_eval(ss.comment)
    Rfb = metaData['Rfb']
    try:
        Cfb = metaData['Cfb']
    except KeyError:
        Cfb = 0
    Vbias = metaData['Vbias']
    Vcoil = metaData['Vcoil']

    print('Sine-sweep amplitudes: SC=', ssSc.amplitude, 'Normal=',
          ssNormal.amplitude, 'Bias=', ss.amplitude)

    tesAdmittance = TesAdmittance(ss, thevenin, Rsq)

    print('IV sweep comment:', sweeps.info.comment)
    sweep = sweeps[1]
    print('IV sweep Rfb=', sweeps.info.pflRfb)
    sweep.subtractSquidOffset(zeros=[1])
    sweep.findTesBias(tes.Rbias, tes.Rshunt, tes.Rsquid(sweeps.info.pflRfb))
    Tbase = sweep.Tadr
    sweep.applyThermalModel(tes.temperature, Tbase)
    I0, R0, T0, alphaIv = sweep.findAlphaIvCloseTo('Vbias',
                                                   Vbias,
                                                   n=10,
                                                   order=3)
    P0 = I0**2 * R0
    print('From IV sweep: R0=', R0 * 1E3, 'mOhm, I0=', 1E6 * I0, 'uA, P0=',
          P0 * 1E12, ' pW, T0=', T0 * 1E3, 'mK')

    #R,dRdVb = sweep.fitRtesCloseTo('Vbias', Vbias, n=30, order=3)
    beta, tau, L = tesAdmittance.guessBetaTauL(R0,
                                               fLowMax=20,
                                               fmin=30E3,
                                               fmax=120E3)
    print('Parameter guesses:')
    print('beta=', beta)
    print('tau=', tau)
    print('L=', L)

    acAmplitude = ss.amplitude / 20

    f = tesAdmittance.f
    iFit = f < fMax

    model = lmfit.Model(fitFunction, independent_vars='f')
    params = model.make_params()

    params['tau'].value = tau
    params['tau'].min = 0.5 * tau
    params['tau'].max = 3 * tau
    params['beta'].value = beta
    params['L'].value = L
    params['L'].min = 0.5 * L
    params['L'].max = 3 * L
    params['R0'].value = R0
    params['R0'].vary = False
    result = model.fit(data=tesAdmittance.A[iFit].view(dtype=np.float),
                       f=np.hstack([f[iFit], f[iFit]]),
                       params=params)
    print(result.fit_report())
    beta = result.best_values['beta']
    tau = result.best_values['tau']
    L = result.best_values['L']
    R0 = result.best_values['R0']

    Afit = AdmittanceModel_Simple(tesAdmittance.f, tau, L, beta, R0)

    A = tesAdmittance.A

    #Rn = tes.Rnormal
    beta = result.params['beta']
    L = result.params['L']
    tau = result.params['tau']
    G = tes.thermalConductance(Ttes=T0)
    C = tau.value * G
    R0 = result.params['R0']
    alphaTf = L.value * G * T0 / P0
    print(T0, R0, alphaIv, alphaTf, beta, tau, L)

    print('Tau type:', type(tau))
    fitResults = dict(betaI=beta.value,
                      L=L.value,
                      tau=tau.value,
                      R0=R0.value,
                      betaIStd=beta.stderr,
                      LStd=L.stderr,
                      tauStd=tau.stderr,
                      R0Std=R0.stderr,
                      chired=result.redchi,
                      aic=result.aic,
                      bic=result.bic,
                      alphaI=alphaTf)
    bias = dict(Vcoil=Vcoil,
                Vbias=Vbias,
                I0=I0,
                P0=P0,
                T0=T0,
                Tbase=Tbase,
                G=G,
                C=C)
    results = dict()
    results.update(fitResults)
    results.update(bias)
    #results.update(hkDict);

    if axes is None:
        gs = gridspec.GridSpec(2, 2, width_ratios=[3, 1])  # 2x2
        ax1 = mpl.subplot(gs[0, 0])  # 1x3
        ax2 = mpl.subplot(gs[1, 0])  # 1x3
        ax3 = mpl.subplot(gs[0, 1])  # 1x2
        ax4 = mpl.subplot(gs[1, 1])  # 1x2 for text
        axes = (ax1, ax2, ax3, ax4)
    else:
        ax1, ax2, ax3, ax4 = axes

    # Re(Y), Im(Y)
    if plotType == 'admittance':
        y = A
        yfit = Afit
        yReLabel = '$\\operatorname{Re}(Y)$'
        yImLabel = '$\\operatorname{Im}(Y)$'
        ax1.set_ylabel(u'TES admittance $Y(f)$ (S)')
        ax2.set_ylabel('$Y(f)$ residual (S)')
        ax2.set_ylim(-100, +100)
    elif plotType == 'impedance':
        y = 1. / A
        yfit = 1. / Afit
        yReLabel = '$\\operatorname{Re}(Z)$'
        yImLabel = '$\\operatorname{Im}(Z)$'
        ax1.set_ylabel(u'TES impedance $Z(f)$ ($\\Omega$)')
        ax2.set_ylabel('$Z(f)$ residual ($\\Omega$)')
        ax2.set_ylim(-3E-4, +3E-4)

    ax1.semilogx(f, np.real(yfit), '-k', label='fit')
    ax1.semilogx(f, np.real(y), 'ob', ms=2, label=yReLabel)
    ax1.semilogx(f, np.imag(yfit), '-k')
    ax1.semilogx(f, np.imag(y), 'or', ms=2, label=yImLabel)
    # Resdiuals
    residual = y - yfit
    ax2.semilogx(f, np.real(residual), '.r', ms=2)
    ax2.semilogx(f, np.imag(residual), '.b', ms=2)

    ax2.set_xlabel('$f$ (Hz)')
    ax2.grid()

    #ax1.set_ylabel(u'TES admittance $Y(f)$ (S)')
    #ax1.set_xlabel('$f$ (Hz)')
    ax1.legend(loc='best')
    ax1.grid()

    fitText = u'$R_0$ = %.4g m$\\Omega$ (%.2f%% $R_n$)\n' % (1E3 * R0,
                                                             100 * R0 / Rn)
    fitText += u'$P_0$ = %.3f pW\n' % (
        1E12 * P0)  # Units used to be incorrect ("nW") -> fixed 2017-12-13
    fitText += u'$I_0$ = %.3f $\\mu$A\n' % (1E6 * I0)
    fitText += u'$T_0$ = %.3f mK\n' % (1E3 * T0)
    fitText += u'$T_b$ = %.3f mK\n' % (1E3 * Tbase)
    fitText += u'$\\alpha = %.1f \\pm %.1f$\n' % (alphaTf,
                                                  L.stderr / L.value * alphaTf)
    fitText += u'$\\beta_{I}$ = %.3f $\\pm$ %.3f\n' % (beta.value, beta.stderr)
    fitText += u'$\\mathcal{L} = %.3f \\pm %.3f$\n' % (L.value, L.stderr)
    fitText += u'$\\tau = %.3f \\pm %.3f$ ms\n' % (1E3 * tau.value,
                                                   1E3 * tau.stderr)
    fitText += u'$C = %.3f \\pm %.3f$ pJ/K' % (1E12 * C, 1E12 * tau.stderr /
                                               tau.value * C)
    props = dict(boxstyle='square', alpha=0.5, facecolor='w')
    ax4.text(0.02,
             0.92,
             fitText,
             va='top',
             bbox=props,
             transform=ax4.transAxes)
    ax4.axis('off')
    #ax4.axes.get_xaxis().set_visible(False)
    #ax4.axes.get_yaxis().set_visible(False)

    bpTitle = '%.2fmK_Vc%.3f_Vb%.3f' % (1E3 * Tbase, Vcoil, Vbias)

    # Polar
    #            ax3.plot(np.real(A), np.imag(A), '.')
    norm = colors.LogNorm(vmin=f.min(), vmax=f.max())
    ax3.plot(np.real(yfit), np.imag(yfit), '-k', lw=1.0)
    ax3.scatter(np.real(y), np.imag(y), s=4, c=f, cmap=cmap, norm=norm)
    #ax3.set_yticklabels([]); ax3.tick_params(labelbottom='off')
    ax3.yaxis.tick_right()
    ax3.yaxis.set_label_position('right')
    ax3.set_xlabel(yReLabel)
    ax3.set_ylabel(yImLabel)
    #limits = ax1.get_ylim(); ax3.set_ylim(limits); ax3.set_xlim(limits)
    ax3.set_aspect('equal', adjustable='datalim', anchor='SW')
    ax3.grid()
    import time
    dateStr = time.strftime('%Y%m%d', time.localtime(ss.t[0]))
    fullDateStr = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ss.t[0]))
    title = '%s/%s: %s - %s\n' % (cooldown, deviceId, tes.DeviceName,
                                  fullDateStr)
    title += '$V_{coil}$=%+06.4f V, Vbias=%.4fV, AC=%.4f Vp' % (Vcoil, Vbias,
                                                                acAmplitude)
    fig.suptitle(title)
    plotFileName = '%s_TFFit_%s_A%.0fmV_%s' % (deviceId, bpTitle,
                                               1E3 * acAmplitude, dateStr)
    mpl.savefig(plotFileName + '.png')
    #mpl.savefig('Plots\\pdf\\'+plotFileName+'.pdf')
    #mpl.close()
    #mpl.show()

    return results, axes
Esempio n. 4
0
def fitTransferFunction(ss, thevenin, Rsq, sweeps, axes=None, fMax=1E5):
    metaData = eval(ss.comment)
    Rfb = metaData['Rfb']
    try:
        Cfb = metaData['Cfb']
    except KeyError:
        Cfb = 0
    Vbias = metaData['Vbias']
    Vcoil = metaData['Vcoil']

    print('Sine-sweep amplitudes: SC=', ssSc.amplitude, 'Normal=',
          ssNormal.amplitude, 'Bias=', ss.amplitude)

    tesAdmittance = TesAdmittance(ss, thevenin, Rsq)

    print('IV sweep comment:', sweeps.info.comment)
    sweep = sweeps[1]
    sweep.subtractSquidOffset(zeros=[1])
    print('IV sweep Rfb=', sweeps.info.pflRfb)
    sweep.findTesBias(tes.Rbias, tes.Rshunt, tes.Rsquid(sweeps.info.pflRfb))
    Tbase = sweep.Tadr
    sweep.applyThermalModel(tes.temperature, Tbase)
    I0, R0, T0, alphaIv = sweep.findAlphaIvCloseTo('Vbias',
                                                   Vbias,
                                                   n=10,
                                                   order=3)
    P0 = I0**2 * R0
    print('From IV sweep: R0=', R0 * 1E3, 'mOhm, I0=', 1E6 * I0, 'uA, P0=',
          P0 * 1E12, ' pW, T0=', T0 * 1E3, 'mK')

    #R,dRdVb = sweep.fitRtesCloseTo('Vbias', Vbias, n=30, order=3)
    betaI, tau, L = tesAdmittance.guessBetaTauL(R0, fmin=10E3, fmax=100E3)

    G = tes.thermalConductance(Ttes=T0)
    alphaI = L * G * T0 / P0
    C = tau * G

    print('Parameter guesses:')
    print('beta=', betaI)
    print('tau=', tau)
    print('L=', L)
    print('G=', G)
    print('C=', C)
    print('alphaI=', alphaI)

    acAmplitude = ss.amplitude / 20

    f = tesAdmittance.f
    iFit = f < fMax

    model = lmfit.Model(fitFunction, independent_vars='f')
    params = model.make_params()

    params['alphaI'].vary = True
    params['alphaI'].value = alphaI
    params['alphaI'].min = 0
    params['betaI'].vary = True
    params['betaI'].value = betaI
    params['betaI'].min = 0
    params['P'].vary = False
    params['P'].value = P0
    params['P'].min = 0
    params['g_tes1'].vary = True
    params['g_tes1'].value = 0.8 * G
    params['g_tes1'].min = 0
    params['g_tes2'].vary = True
    params['g_tes2'].value = 0.1 * G
    params['g_tes2'].min = 0
    params['g_2b'].vary = True
    params['g_2b'].value = 0.1 * G
    params['g_2b'].min = 0
    params['Ctes'].vary = True
    params['Ctes'].value = 0.8 * C
    params['Ctes'].min = 0
    params['C1'].vary = True
    params['C1'].value = 0.1 * C
    params['C1'].min = 0  # Wild guess
    params['C2'].vary = True
    params['C2'].value = 0.1 * C
    params['C2'].min = 0
    params['T'].vary = False
    params['T'].value = T0
    params['T2'].vary = False
    params['T2'].value = T0
    params['R'].vary = False
    params['R'].value = R0
    params['betaThermal'].vary = False
    params['betaThermal'].value = 2.26

    result = model.fit(data=tesAdmittance.A[iFit].view(dtype=np.float),
                       f=np.hstack([f[iFit], f[iFit]]),
                       params=params)
    print(result.fit_report())

    alphaI = result.best_values['alphaI']
    betaI = result.best_values['betaI']
    P = result.best_values['P']
    g_tes1 = result.best_values['g_tes1']
    g_tes2 = result.best_values['g_tes2']
    g_2b = result.best_values['g_2b']
    Ctes = result.best_values['Ctes']
    C1 = result.best_values['C1']
    C2 = result.best_values['C2']
    T = result.best_values['T']
    T2 = result.best_values['T2']
    R = result.best_values['R']
    betaThermal = result.best_values['betaThermal']
    omega = 2 * np.pi * tesAdmittance.f

    Zfit = tesModel.impedance(alphaI, betaI, P, g_tes1, g_tes2, g_2b, Ctes, C1,
                              C2, T, T2, R, betaThermal, omega)

    Afit = 1. / Zfit

    A = tesAdmittance.A

    if axes is None:
        gs = gridspec.GridSpec(2, 2, width_ratios=[3, 1])  # 2x2
        ax1 = mpl.subplot(gs[0, 0])  # 1x3
        ax2 = mpl.subplot(gs[1, 0])  # 1x3
        ax3 = mpl.subplot(gs[0, 1])  # 1x2
        ax4 = mpl.subplot(gs[1, 1])  # 1x2 for text
        axes = (ax1, ax2, ax3, ax4)
    else:
        ax1, ax2, ax3, ax4 = axes

    if plotType == 'admittance':
        y = A
        yfit = Afit
        yReLabel = '$\\operatorname{Re}(Y)$'
        yImLabel = '$\\operatorname{Im}(Y)$'
        ax1.set_ylabel(u'TES admittance $Y(f)$ (S)')
        ax2.set_ylabel('$Y(f)$ residual (S)')
        ax2.set_ylim(-100, +100)
    elif plotType == 'impedance':
        y = 1. / A
        yfit = 1. / Afit
        yReLabel = '$\\operatorname{Re}(Z)$'
        yImLabel = '$\\operatorname{Im}(Z)$'
        ax1.set_ylabel(u'TES impedance $Z(f)$ ($\\Omega$)')
        ax2.set_ylabel('$Z(f)$ residual ($\\Omega$)')
        ax2.set_ylim(-3E-4, +3E-4)

    ax1.semilogx(f, np.real(yfit), '-k', label='fit')
    ax1.semilogx(f, np.real(y), 'ob', ms=2, label=yReLabel)
    ax1.semilogx(f, np.imag(yfit), '-k')
    ax1.semilogx(f, np.imag(y), 'or', ms=2, label=yImLabel)
    # Residuals
    residual = y - yfit
    ax2.semilogx(f, np.real(residual), '.r', ms=2)
    ax2.semilogx(f, np.imag(residual), '.b', ms=2)

    ax2.set_xlabel('$f$ (Hz)')
    ax2.grid()

    #ax1.set_ylabel(u'TES admittance $Y(f)$ (S)')
    #ax1.set_xlabel('$f$ (Hz)')
    ax1.legend(loc='best')
    ax1.grid()

    fitText = u'$R_0$ = %.4g m$\\Omega$ (%.2f%% $R_n$)\n' % (1E3 * R0,
                                                             100 * R0 / Rn)
    fitText += u'$P_0$ = %.3f pW\n' % (
        1E12 * P0)  # Units used to be incorrect ("nW") -> fixed 2017-12-13
    fitText += u'$I_0$ = %.3f $\\mu$A\n' % (1E6 * I0)
    fitText += u'$T_0$ = %.3f mK\n' % (1E3 * T0)
    fitText += u'$T_b$ = %.3f mK\n' % (1E3 * Tbase)
    #fitText += u'$\\alphaI = %.1f \\pm %.1f$\n' % (alphaI, alphaI.stderr)
    #fitText += u'$\\beta_{I}$ = %.3f $\\pm$ %.3f\n' % (betaI.value, betaI.stderr)
    #fitText += u'$\\mathcal{L} = %.3f \\pm %.3f$\n' % (L.value, L.stderr)
    #fitText += u'$\\tau = %.3f \\pm %.3f$ ms\n' % (1E3*tau.value, 1E3*tau.stderr)
    #fitText += u'$C = %.3f \\pm %.3f$ pJ/K' % (1E12*C, 1E12*tau.stderr/tau.value*C)
    #fitText += u'$C1 = %.3f \\pm %.3f$ pJ/K' % (1E12*C1, 1E12*tau.stderr/tau.value*C1)
    props = dict(boxstyle='square', alpha=0.5, facecolor='w')
    ax4.text(0.02,
             0.92,
             fitText,
             va='top',
             bbox=props,
             transform=ax4.transAxes)
    ax4.axis('off')
    #ax4.axes.get_xaxis().set_visible(False)
    #ax4.axes.get_yaxis().set_visible(False)

    bpTitle = '%.2fmK_Vc%.3f_Vb%.3f' % (1E3 * Tbase, Vcoil, Vbias)

    # Polar
    #            ax3.plot(np.real(A), np.imag(A), '.')
    norm = colors.LogNorm(vmin=f.min(), vmax=f.max())
    ax3.plot(np.real(yfit), np.imag(yfit), '-k', lw=1.0)
    ax3.scatter(np.real(y), np.imag(y), s=4, c=f, cmap=cmap, norm=norm)
    #ax3.set_yticklabels([]); ax3.tick_params(labelbottom='off')
    ax3.yaxis.tick_right()
    ax3.yaxis.set_label_position('right')
    ax3.set_xlabel(yReLabel)
    ax3.set_ylabel(yImLabel)
    #limits = ax1.get_ylim(); ax3.set_ylim(limits); ax3.set_xlim(limits)
    ax3.set_aspect('equal', adjustable='datalim', anchor='SW')
    ax3.grid()
    import time
    dateStr = time.strftime('%Y%m%d', time.localtime(ss.t[0]))
    fullDateStr = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ss.t[0]))
    title = '%s/%s: %s - %s\n' % (cooldown, deviceId, tes.DeviceName,
                                  fullDateStr)
    title += '$V_{coil}$=%+06.4f V, Vbias=%.4fV, AC=%.4f Vp' % (Vcoil, Vbias,
                                                                acAmplitude)
    fig.suptitle(title)

    #    plotFileName = '%s_TFFit_%s_A%.0fmV_%s' % (deviceId, bpTitle, 1E3*acAmplitude, dateStr)
    #    #mpl.savefig(plotFileName+'.png')
    #    pdf = PdfPages(outputPath+plotFileName+'.pdf')
    #    pdf.savefig(fig)
    #    pdf.close()

    mpl.show()

    return result, axes