<commit_before>from scipy.optimize import curve_fit
import numpy as np
from numpy.fft import fft
from scipy.linalg import svd, eig, inv, pinv
from enum import Enum
from auspex.log import logger

def hilbert(signal):
    # construct the Hilbert transform of the signal via the FFT
    # in essense, we just want to set negative frequency components to zero
    spectrum = np.fft.fft(signal)
    n = len(signal)
    midpoint = int(np.ceil(n/2))

    kernel = np.zeros(n)
    kernel[0] = 1
    if n%2 == 0:
        kernel[midpoint] = 1
    kernel[1:midpoint] = 2
    return np.fft.ifft(kernel * spectrum)

def KT_estimation(data, times, order):
    # Find the hilbert transform
    analytic_signal = hilbert(data)
    time_step = times[1]-times[0]

    # Create the Hankel matrix
    N = len(analytic_signal)
    K = order
    M = (N//2)-1
    L = N-M+1
    H = np.zeros((L, M), dtype=np.complex128)
    for ct in range(M):
        H[:,ct] = analytic_signal[ct:ct+L]

    #Try and seperate the signal and noise subspace via the svd
    U,S,V = svd(H, False) # V is not transposed/conjugated in numpy svd

    #Reconstruct the approximate Hankel matrix with the first K singular values
    #Here we can iterate and modify the singular values
    S_k = np.diag(S[:K])

    #Estimate the variance from the rest of the singular values
    varEst = (1/((M-K)*L)) * np.sum(S[K:]**2)
    Sfilt = np.matmul(S_k**2 - L*varEst*np.eye(K), inv(S_k))
    Hbar = np.matmul(np.matmul(U[:,:K], Sfilt), V[:K,:])

    #Reconstruct the data from the averaged anti-diagonals
    cleanedData = np.zeros(N, dtype=np.complex128)
    tmpMat = np.flip(Hbar,1)
    idx = -L+1
    for ct in range(N-1,-1,-1):
        cleanedData[ct] = np.mean(np.diag(tmpMat,idx))
        idx += 1

    #Create a cleaned Hankel matrix
    cleanedH = np.empty_like(H)
    cleanedAnalyticSig = hilbert(cleanedData)
    for ct in range(M):
        cleanedH[:,ct] = cleanedAnalyticSig[ct:ct+L]

    #Compute Q with total least squares
    #U_K1*Q = U_K2
    U = svd(cleanedH, False)[0]
    U_K = U[:,0:K]
    tmpMat = np.hstack((U_K[:-1,:],U_K[1:,:]))
    V = svd(tmpMat, False)[2].T.conj()
    n = np.size(U_K,1)
    V_AB = V[:n,n:]
    V_BB = V[n:,n:]
    Q = np.linalg.lstsq(V_BB.conj().T, -V_AB.conj().T)[0].conj().T

    #Now poles are eigenvalues of Q
    poles, _ = eig(Q)

    #Take the log and return the decay constant and frequency
    freqs = np.zeros(K)
    Tcs   = np.zeros(K)
    for ct in range(K):
        sk = np.log(poles[ct])
        freqs[ct] = np.imag(sk)/(2*np.pi*time_step)
        Tcs[ct] = -1.0/np.real(sk)*time_step

    #Refit the data to get the amplitude
    A = np.zeros((N, K), dtype=np.complex128)
    for ct in range(K):
        A[:,ct] = np.power(poles[ct], range(0,N))

    amps = np.linalg.lstsq(A, cleanedData)[0]

    return freqs, Tcs, amps

def fit_rabi(xdata, ydata):
    """Analyze Rabi amplitude data to find pi-pulse amplitude and phase offset.
        Arguments:
            xdata: ndarray of calibration amplitudes. length should be even.
            ydata: measurement amplitudes
        Returns:
            pi_amp: Fitted amplitude of pi pulsed
            offset: Fitted mixer offset
            fit_pts: Fitted points."""

    def rabi_model(x, *p):
        return p[0] - p[1]*np.cos(2*np.pi*p[2]*(x - p[3]))

    #seed Rabi frequency from largest FFT component
    N = len(ydata)
    yfft = fft(ydata)
    f_max_ind = np.argmax(np.abs(yfft[1:N//2]))
    f_0 = 0.5 * max([1, f_max_ind]) / xdata[-1]
    amp_0 = 0.5*(ydata.max() - ydata.min())
    offset_0 = np.mean(ydata)
    phase_0 = 0
    if ydata[N//2 - 1] > offset_0:
        amp_0 = -amp_0
    popt, _ = curve_fit(rabi_model, xdata, ydata, [offset_0, amp_0, f_0, phase_0])
    f_rabi = np.abs(popt[2])
    pi_amp = 0.5/f_rabi
    offset = popt[3]
    return pi_amp, offset, rabi_model(xdata, *popt)

def fit_ramsey(xdata, ydata, two_freqs = False):
    if two_freqs:
        # Initial KT estimation
        freqs, Tcs, amps = KT_estimation(ydata, xdata, 2)
        p0 = [*freqs, *abs(amps), *Tcs, *np.angle(amps), np.mean(ydata)]
        popt, pcov = curve_fit(ramsey_2f, xdata, ydata, p0 = p0)
        fopt = [popt[0], popt[1]]
    else:
        # Initial KT estimation
        freqs, Tcs, amps = KT_estimation(ydata, xdata, 1)
        p0 = [freqs[0], abs(amps[0]), Tcs[0], np.angle(amps[0]), np.mean(ydata)]
        popt, pcov = curve_fit(ramsey_1f, xdata, ydata, p0 = p0)
        fopt = [popt[0]]
    perr = np.sqrt(np.diag(pcov))
    fopt = popt[:two_freqs+1]
    ferr = perr[:two_freqs+1]
    return fopt, ferr, popt

def ramsey_1f(x, f, A, tau, phi, y0):
    return A*np.exp(-x/tau)*np.cos(2*np.pi*f*x + phi) + y0

def ramsey_2f(x, f1, f2, A1, A2, tau1, tau2, phi1, phi2, y0):
    return ramsey_1f(x, f1, A1, tau1, phi1, y0/2) + ramsey_1f(x, f2, A2, tau2, phi2, y0/2)

def fit_drag(data, DRAG_vec, pulse_vec):
    """Fit calibration curves vs DRAG parameter, for variable number of pulses"""
    num_DRAG = len(DRAG_vec)
    num_seqs = len(pulse_vec)
    xopt_vec = np.zeros(num_seqs)
    perr_vec = np.zeros(num_seqs)
    popt_mat = np.zeros((4, num_seqs))
    data = data.reshape(len(data)//num_DRAG, num_DRAG)
    #first fit sine to lowest n, for the full range
    data_n = data[1, :]
    T0 = 2*(DRAG_vec[np.argmax(data_n)] - DRAG_vec[np.argmin(data_n)]) #rough estimate of period

    p0 = [0, 1, T0, 0]
    popt, pcov = curve_fit(sinf, DRAG_vec, data_n, p0 = p0)
    perr_vec[0] = np.sqrt(np.diag(pcov))[0]
    x_fine = np.linspace(min(DRAG_vec), max(DRAG_vec), 1001)
    xopt_vec[0] = x_fine[np.argmin(sinf(x_fine, *popt))]
    popt_mat[:,0] = popt
    for ct in range(1, len(pulse_vec)):
        #quadratic fit for subsequent steps, narrower range
        data_n = data[ct, :]
        p0 = [1, xopt_vec[ct-1], 0]
        #recenter for next fit
        closest_ind =np.argmin(abs(DRAG_vec - xopt_vec[ct-1]))
        fit_range = int(np.round(0.5*num_DRAG*pulse_vec[0]/pulse_vec[ct]))
        curr_DRAG_vec = DRAG_vec[max(0, closest_ind - fit_range) : min(num_DRAG-1, closest_ind + fit_range)]
        reduced_data_n = data_n[max(0, closest_ind - fit_range) : min(num_DRAG-1, closest_ind + fit_range)]
        #quadratic fit
        popt, pcov = curve_fit(quadf, curr_DRAG_vec, reduced_data_n, p0 = p0)
        perr_vec[ct] = np.sqrt(np.diag(pcov))[0]
        x_fine = np.linspace(min(curr_DRAG_vec), max(curr_DRAG_vec), 1001)
        xopt_vec[ct] = x_fine[np.argmin(quadf(x_fine, *popt))] #why not x0?
        popt_mat[:3,ct] = popt
    return xopt_vec, perr_vec, popt_mat

def sinf(x, f, A, phi, y0):
    return A*np.sin(2*np.pi*f*x + phi) + y0

def quadf(x, A, x0, b):
    return A*(x-x0)**2+b

def fit_photon_number(xdata, ydata, params):
    ''' Fit number of measurement photons before a Ramsey. See McClure et al., Phys. Rev. App. 2016
    input params:
	1 - cavity decay rate kappa (MHz)
	2 - detuning Delta (MHz)
	3 - dispersive shift 2Chi (MHz)
	4 - Ramsey decay time T2* (us)
	5 - exp(-t_meas/T1) (us), only if starting from |1> (to include relaxation during the 1st msm't)
	6 - initial qubit state (0/1)
    '''
    params = [2*np.pi*p for p in params[:3]] + params[3:] # convert to angular frequencies
    def model_0(t, pa, pb):
        return (-np.imag(np.exp(-(1/params[3]+params[1]*1j)*t + (pa-pb*params[2]*(1-np.exp(-((params[0] + params[2]*1j)*t)))/(params[0]+params[2]*1j))*1j)))
    def model(t, pa, pb):
        return  params[4]*model_0(t, pa, pb) + (1-params[4])*model_0(t, pa+np.pi, pb) if params[5] == 1  else model_0(t, pa, pb)
    popt, pcov = curve_fit(model, xdata, ydata, p0 = [0, 1])
    perr = np.sqrt(np.diag(pcov))
    finer_delays = np.linspace(np.min(xdata), np.max(xdata), 4*len(xdata))
    fit_curve = model(finer_delays, *popt)
    return popt[1], perr[1], (finer_delays, fit_curve)

def fit_quad(xdata, ydata):
    popt, pcov = curve_fit(quadf, xdata, ydata, p0 = [1, min(ydata), 0])
    perr = np.sqrt(np.diag(pcov))
    return popt, perr

class CR_cal_type(Enum):
    LENGTH = 1
    PHASE = 2
    AMPLITUDE = 3

def fit_CR(xpoints, data, cal_type):
    """Fit CR calibration curves for variable pulse length, phase, or amplitude"""
    data0 = data[:len(data)//2]
    data1 = data[len(data)//2:]
    x_fine = np.linspace(min(xpoints), max(xpoints), 1001)
    if cal_type == CR_cal_type.LENGTH:
        p0 = [1/(2*xpoints[-1]), 1, np.pi/2, 0]
        popt0, _ = curve_fit(sinf, xpoints, data0, p0 = p0)
        popt1, _ = curve_fit(sinf, xpoints, data1, p0 = p0)
        #find the first zero crossing
        yfit0 = sinf(x_fine[:int(1/abs(popt0[0])/2/(x_fine[1]-x_fine[0]))], *popt0)
        yfit1 = sinf(x_fine[:int(1/abs(popt1[0])/2/(x_fine[1]-x_fine[0]))], *popt1)
        #average between the two qc states, rounded to 10 ns
        xopt = round((x_fine[np.argmin(abs(yfit0))] + x_fine[np.argmin(abs(yfit1))])/2/10e-9)*10e-9
        logger.info('CR length = {} ns'.format(xopt*1e9))
    elif cal_type == CR_cal_type.PHASE:
        p0 = [1/(xpoints[-1]), 1, np.pi, 0]
        popt0, _ = curve_fit(sinf, x_fine, data0, p0 = p0)
        popt1, _ = curve_fit(sinf, x_fine, data1, p0 = p0)
        #find the phase for maximum contrast
        contrast = (sinf(x_fine, *popt0) - sinf(x_fine, *popt1))/2
        logger.info('CR contrast = {}'.format(max(contrast)))
        xopt = x_fine[np.argmax(contrast)] - np.pi
    elif cal_type == CR_cal_type.AMPLITUDE:
        popt0 = np.polyfit(xpoints, data0, 1)
        popt1 = np.polyfit(xpoints, data1, 1)
        yfit0 = popt0[0]*x_fine+popt0[1]
        yfit1 = popt1[0]*x_fine+popt1[1]
        #average between optimum amplitudes
        xopt = -(popt0[1]/popt0[0] + popt1[1]/popt1[0])/2
        logger.info('CR amplitude = {}'.format(xopt))
    return xopt, popt0, popt1
<commit_msg>Fix CRphase<commit_after>from scipy.optimize import curve_fit
import numpy as np
from numpy.fft import fft
from scipy.linalg import svd, eig, inv, pinv
from enum import Enum
from auspex.log import logger

def hilbert(signal):
    # construct the Hilbert transform of the signal via the FFT
    # in essense, we just want to set negative frequency components to zero
    spectrum = np.fft.fft(signal)
    n = len(signal)
    midpoint = int(np.ceil(n/2))

    kernel = np.zeros(n)
    kernel[0] = 1
    if n%2 == 0:
        kernel[midpoint] = 1
    kernel[1:midpoint] = 2
    return np.fft.ifft(kernel * spectrum)

def KT_estimation(data, times, order):
    # Find the hilbert transform
    analytic_signal = hilbert(data)
    time_step = times[1]-times[0]

    # Create the Hankel matrix
    N = len(analytic_signal)
    K = order
    M = (N//2)-1
    L = N-M+1
    H = np.zeros((L, M), dtype=np.complex128)
    for ct in range(M):
        H[:,ct] = analytic_signal[ct:ct+L]

    #Try and seperate the signal and noise subspace via the svd
    U,S,V = svd(H, False) # V is not transposed/conjugated in numpy svd

    #Reconstruct the approximate Hankel matrix with the first K singular values
    #Here we can iterate and modify the singular values
    S_k = np.diag(S[:K])

    #Estimate the variance from the rest of the singular values
    varEst = (1/((M-K)*L)) * np.sum(S[K:]**2)
    Sfilt = np.matmul(S_k**2 - L*varEst*np.eye(K), inv(S_k))
    Hbar = np.matmul(np.matmul(U[:,:K], Sfilt), V[:K,:])

    #Reconstruct the data from the averaged anti-diagonals
    cleanedData = np.zeros(N, dtype=np.complex128)
    tmpMat = np.flip(Hbar,1)
    idx = -L+1
    for ct in range(N-1,-1,-1):
        cleanedData[ct] = np.mean(np.diag(tmpMat,idx))
        idx += 1

    #Create a cleaned Hankel matrix
    cleanedH = np.empty_like(H)
    cleanedAnalyticSig = hilbert(cleanedData)
    for ct in range(M):
        cleanedH[:,ct] = cleanedAnalyticSig[ct:ct+L]

    #Compute Q with total least squares
    #U_K1*Q = U_K2
    U = svd(cleanedH, False)[0]
    U_K = U[:,0:K]
    tmpMat = np.hstack((U_K[:-1,:],U_K[1:,:]))
    V = svd(tmpMat, False)[2].T.conj()
    n = np.size(U_K,1)
    V_AB = V[:n,n:]
    V_BB = V[n:,n:]
    Q = np.linalg.lstsq(V_BB.conj().T, -V_AB.conj().T)[0].conj().T

    #Now poles are eigenvalues of Q
    poles, _ = eig(Q)

    #Take the log and return the decay constant and frequency
    freqs = np.zeros(K)
    Tcs   = np.zeros(K)
    for ct in range(K):
        sk = np.log(poles[ct])
        freqs[ct] = np.imag(sk)/(2*np.pi*time_step)
        Tcs[ct] = -1.0/np.real(sk)*time_step

    #Refit the data to get the amplitude
    A = np.zeros((N, K), dtype=np.complex128)
    for ct in range(K):
        A[:,ct] = np.power(poles[ct], range(0,N))

    amps = np.linalg.lstsq(A, cleanedData)[0]

    return freqs, Tcs, amps

def fit_rabi(xdata, ydata):
    """Analyze Rabi amplitude data to find pi-pulse amplitude and phase offset.
        Arguments:
            xdata: ndarray of calibration amplitudes. length should be even.
            ydata: measurement amplitudes
        Returns:
            pi_amp: Fitted amplitude of pi pulsed
            offset: Fitted mixer offset
            fit_pts: Fitted points."""

    def rabi_model(x, *p):
        return p[0] - p[1]*np.cos(2*np.pi*p[2]*(x - p[3]))

    #seed Rabi frequency from largest FFT component
    N = len(ydata)
    yfft = fft(ydata)
    f_max_ind = np.argmax(np.abs(yfft[1:N//2]))
    f_0 = 0.5 * max([1, f_max_ind]) / xdata[-1]
    amp_0 = 0.5*(ydata.max() - ydata.min())
    offset_0 = np.mean(ydata)
    phase_0 = 0
    if ydata[N//2 - 1] > offset_0:
        amp_0 = -amp_0
    popt, _ = curve_fit(rabi_model, xdata, ydata, [offset_0, amp_0, f_0, phase_0])
    f_rabi = np.abs(popt[2])
    pi_amp = 0.5/f_rabi
    offset = popt[3]
    return pi_amp, offset, rabi_model(xdata, *popt)

def fit_ramsey(xdata, ydata, two_freqs = False):
    if two_freqs:
        # Initial KT estimation
        freqs, Tcs, amps = KT_estimation(ydata, xdata, 2)
        p0 = [*freqs, *abs(amps), *Tcs, *np.angle(amps), np.mean(ydata)]
        popt, pcov = curve_fit(ramsey_2f, xdata, ydata, p0 = p0)
        fopt = [popt[0], popt[1]]
    else:
        # Initial KT estimation
        freqs, Tcs, amps = KT_estimation(ydata, xdata, 1)
        p0 = [freqs[0], abs(amps[0]), Tcs[0], np.angle(amps[0]), np.mean(ydata)]
        popt, pcov = curve_fit(ramsey_1f, xdata, ydata, p0 = p0)
        fopt = [popt[0]]
    perr = np.sqrt(np.diag(pcov))
    fopt = popt[:two_freqs+1]
    ferr = perr[:two_freqs+1]
    return fopt, ferr, popt

def ramsey_1f(x, f, A, tau, phi, y0):
    return A*np.exp(-x/tau)*np.cos(2*np.pi*f*x + phi) + y0

def ramsey_2f(x, f1, f2, A1, A2, tau1, tau2, phi1, phi2, y0):
    return ramsey_1f(x, f1, A1, tau1, phi1, y0/2) + ramsey_1f(x, f2, A2, tau2, phi2, y0/2)

def fit_drag(data, DRAG_vec, pulse_vec):
    """Fit calibration curves vs DRAG parameter, for variable number of pulses"""
    num_DRAG = len(DRAG_vec)
    num_seqs = len(pulse_vec)
    xopt_vec = np.zeros(num_seqs)
    perr_vec = np.zeros(num_seqs)
    popt_mat = np.zeros((4, num_seqs))
    data = data.reshape(len(data)//num_DRAG, num_DRAG)
    #first fit sine to lowest n, for the full range
    data_n = data[1, :]
    T0 = 2*(DRAG_vec[np.argmax(data_n)] - DRAG_vec[np.argmin(data_n)]) #rough estimate of period

    p0 = [0, 1, T0, 0]
    popt, pcov = curve_fit(sinf, DRAG_vec, data_n, p0 = p0)
    perr_vec[0] = np.sqrt(np.diag(pcov))[0]
    x_fine = np.linspace(min(DRAG_vec), max(DRAG_vec), 1001)
    xopt_vec[0] = x_fine[np.argmin(sinf(x_fine, *popt))]
    popt_mat[:,0] = popt
    for ct in range(1, len(pulse_vec)):
        #quadratic fit for subsequent steps, narrower range
        data_n = data[ct, :]
        p0 = [1, xopt_vec[ct-1], 0]
        #recenter for next fit
        closest_ind =np.argmin(abs(DRAG_vec - xopt_vec[ct-1]))
        fit_range = int(np.round(0.5*num_DRAG*pulse_vec[0]/pulse_vec[ct]))
        curr_DRAG_vec = DRAG_vec[max(0, closest_ind - fit_range) : min(num_DRAG-1, closest_ind + fit_range)]
        reduced_data_n = data_n[max(0, closest_ind - fit_range) : min(num_DRAG-1, closest_ind + fit_range)]
        #quadratic fit
        popt, pcov = curve_fit(quadf, curr_DRAG_vec, reduced_data_n, p0 = p0)
        perr_vec[ct] = np.sqrt(np.diag(pcov))[0]
        x_fine = np.linspace(min(curr_DRAG_vec), max(curr_DRAG_vec), 1001)
        xopt_vec[ct] = x_fine[np.argmin(quadf(x_fine, *popt))] #why not x0?
        popt_mat[:3,ct] = popt
    return xopt_vec, perr_vec, popt_mat

def sinf(x, f, A, phi, y0):
    return A*np.sin(2*np.pi*f*x + phi) + y0

def quadf(x, A, x0, b):
    return A*(x-x0)**2+b

def fit_photon_number(xdata, ydata, params):
    ''' Fit number of measurement photons before a Ramsey. See McClure et al., Phys. Rev. App. 2016
    input params:
	1 - cavity decay rate kappa (MHz)
	2 - detuning Delta (MHz)
	3 - dispersive shift 2Chi (MHz)
	4 - Ramsey decay time T2* (us)
	5 - exp(-t_meas/T1) (us), only if starting from |1> (to include relaxation during the 1st msm't)
	6 - initial qubit state (0/1)
    '''
    params = [2*np.pi*p for p in params[:3]] + params[3:] # convert to angular frequencies
    def model_0(t, pa, pb):
        return (-np.imag(np.exp(-(1/params[3]+params[1]*1j)*t + (pa-pb*params[2]*(1-np.exp(-((params[0] + params[2]*1j)*t)))/(params[0]+params[2]*1j))*1j)))
    def model(t, pa, pb):
        return  params[4]*model_0(t, pa, pb) + (1-params[4])*model_0(t, pa+np.pi, pb) if params[5] == 1  else model_0(t, pa, pb)
    popt, pcov = curve_fit(model, xdata, ydata, p0 = [0, 1])
    perr = np.sqrt(np.diag(pcov))
    finer_delays = np.linspace(np.min(xdata), np.max(xdata), 4*len(xdata))
    fit_curve = model(finer_delays, *popt)
    return popt[1], perr[1], (finer_delays, fit_curve)

def fit_quad(xdata, ydata):
    popt, pcov = curve_fit(quadf, xdata, ydata, p0 = [1, min(ydata), 0])
    perr = np.sqrt(np.diag(pcov))
    return popt, perr

class CR_cal_type(Enum):
    LENGTH = 1
    PHASE = 2
    AMPLITUDE = 3

def fit_CR(xpoints, data, cal_type):
    """Fit CR calibration curves for variable pulse length, phase, or amplitude"""
    data0 = data[:len(data)//2]
    data1 = data[len(data)//2:]
    if cal_type == CR_cal_type.LENGTH:
        xpoints = xpoints[0]
        x_fine = np.linspace(min(xpoints), max(xpoints), 1001)
        p0 = [1/(2*xpoints[-1]), 1, np.pi/2, 0]
        popt0, _ = curve_fit(sinf, xpoints, data0, p0 = p0)
        popt1, _ = curve_fit(sinf, xpoints, data1, p0 = p0)
        #find the first zero crossing
        yfit0 = sinf(x_fine[:int(1/abs(popt0[0])/2/(x_fine[1]-x_fine[0]))], *popt0)
        yfit1 = sinf(x_fine[:int(1/abs(popt1[0])/2/(x_fine[1]-x_fine[0]))], *popt1)
        #average between the two qc states, rounded to 10 ns
        xopt = round((x_fine[np.argmin(abs(yfit0))] + x_fine[np.argmin(abs(yfit1))])/2/10e-9)*10e-9
        logger.info('CR length = {} ns'.format(xopt*1e9))
    elif cal_type == CR_cal_type.PHASE:
        xpoints = xpoints[1]
        x_fine = np.linspace(min(xpoints), max(xpoints), 1001)
        p0 = [1/(xpoints[-1]), 1, np.pi, 0]
        popt0, _ = curve_fit(sinf, xpoints, data0, p0 = p0)
        popt1, _ = curve_fit(sinf, xpoints, data1, p0 = p0)
        #find the phase for maximum contrast
        contrast = (sinf(x_fine, *popt0) - sinf(x_fine, *popt1))/2
        logger.info('CR contrast = {}'.format(max(contrast)))
        xopt = x_fine[np.argmax(contrast)] - np.pi
    elif cal_type == CR_cal_type.AMPLITUDE:
        xpoints = xpoints[2]
        x_fine = np.linspace(min(xpoints), max(xpoints), 1001)
        popt0 = np.polyfit(xpoints, data0, 1)
        popt1 = np.polyfit(xpoints, data1, 1)
        yfit0 = popt0[0]*x_fine+popt0[1]
        yfit1 = popt1[0]*x_fine+popt1[1]
        #average between optimum amplitudes
        xopt = -(popt0[1]/popt0[0] + popt1[1]/popt1[0])/2
        logger.info('CR amplitude = {}'.format(xopt))
    return xopt, popt0, popt1
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
'''
    tests.storage.dav.test_main
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: (c) 2014 Markus Unterwaditzer
    :license: MIT, see LICENSE for more details.
'''

import datetime
import os
from textwrap import dedent

import pytest

import requests
import requests.exceptions

from .. import StorageTests
import vdirsyncer.exceptions as exceptions
from vdirsyncer.storage.base import Item
from vdirsyncer.storage.dav import CaldavStorage, CarddavStorage

dav_server = os.environ.get('DAV_SERVER', '').strip() or 'radicale'

def _get_server_mixin(server_name):
    from . import __name__ as base
    x = __import__('{}.servers.{}'.format(base, server_name), fromlist=[''])
    return x.ServerMixin

ServerMixin = _get_server_mixin(dav_server)

VCARD_TEMPLATE = u'''BEGIN:VCARD
VERSION:3.0
FN:Cyrus Daboo
N:Daboo;Cyrus
ADR;TYPE=POSTAL:;2822 Email HQ;Suite 2821;RFCVille;PA;15213;USA
EMAIL;TYPE=INTERNET;TYPE=PREF:cyrus@example.com
NICKNAME:me
NOTE:Example VCard.
ORG:Self Employed
TEL;TYPE=WORK;TYPE=VOICE:412 605 0499
TEL;TYPE=FAX:412 605 0705
URL:http://www.example.com
X-SOMETHING:{r}
END:VCARD'''

TASK_TEMPLATE = u'''BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//dmfs.org//mimedir.icalendar//EN
BEGIN:VTODO
CREATED:20130721T142233Z
DTSTAMP:20130730T074543Z
LAST-MODIFIED;VALUE=DATE-TIME:20140122T151338Z
SEQUENCE:2
SUMMARY:Book: Kowlani - Tödlicher Staub
X-SOMETHING:{r}
END:VTODO
END:VCALENDAR'''

EVENT_TEMPLATE = u'''BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
X-SOMETHING:{r}
END:VEVENT
END:VCALENDAR'''

templates = {
    'VCARD': VCARD_TEMPLATE,
    'VEVENT': EVENT_TEMPLATE,
    'VTODO': TASK_TEMPLATE
}

class DavStorageTests(ServerMixin, StorageTests):
    def test_dav_broken_item(self):
        item = Item(u'HAHA:YES')
        s = self._get_storage()
        try:
            s.upload(item)
        except (exceptions.Error, requests.exceptions.HTTPError):
            pass
        assert not list(s.list())

    @pytest.mark.xfail(dav_server == 'owncloud',
                       reason='See issue #16')
    def test_wrong_etag(self):
        super(DavStorageTests, self).test_wrong_etag()

    @pytest.mark.xfail(dav_server == 'owncloud',
                       reason='See issue #16')
    def test_update_nonexisting(self):
        super(DavStorageTests, self).test_update_nonexisting()

class TestCaldavStorage(DavStorageTests):
    storage_class = CaldavStorage

    item_template = TASK_TEMPLATE

    def test_both_vtodo_and_vevent(self):
        task = self._create_bogus_item(item_template=TASK_TEMPLATE)
        event = self._create_bogus_item(item_template=EVENT_TEMPLATE)
        s = self._get_storage()
        href_etag_task = s.upload(task)
        href_etag_event = s.upload(event)
        assert set(s.list()) == set([
            href_etag_task,
            href_etag_event
        ])

    @pytest.mark.parametrize('item_type', ['VTODO', 'VEVENT'])
    def test_item_types_correctness(self, item_type):
        other_item_type = 'VTODO' if item_type == 'VEVENT' else 'VEVENT'
        kw = self.get_storage_args()
        s = self.storage_class(item_types=(item_type,), **kw)
        try:
            s.upload(self._create_bogus_item(
                item_template=templates[other_item_type]))
            s.upload(self._create_bogus_item(
                item_template=templates[other_item_type]))
        except (exceptions.Error, requests.exceptions.HTTPError):
            pass
        href, etag = \
            s.upload(self._create_bogus_item(
                item_template=templates[item_type]))
        ((href2, etag2),) = s.list()
        assert href2 == href
        assert etag2 == etag

    @pytest.mark.parametrize('item_types', [
        ('VTODO',),
        ('VEVENT',),
        ('VTODO', 'VEVENT'),
        ('VTODO', 'VEVENT', 'VJOURNAL')
    ])
    def test_item_types_performance(self, item_types, monkeypatch):
        kw = self.get_storage_args()
        s = self.storage_class(item_types=item_types, **kw)

        old_list = s._list
        calls = []

        def _list(*a, **kw):
            calls.append(None)
            return old_list(*a, **kw)

        monkeypatch.setattr(s, '_list', _list)

        list(s.list())
        assert len(calls) == len(item_types)

    @pytest.mark.xfail(dav_server == 'radicale',
                       reason='Radicale doesn\'t support timeranges.')
    def test_timerange_correctness(self):
        kw = self.get_storage_args()
        start_date = datetime.datetime(2013, 9, 10)
        end_date = datetime.datetime(2013, 9, 13)
        s = self.storage_class(start_date=start_date, end_date=end_date, **kw)

        too_old_item = self._create_bogus_item(item_template=dedent(u'''
            BEGIN:VCALENDAR
            VERSION:2.0
            PRODID:-//hacksw/handcal//NONSGML v1.0//EN
            BEGIN:VEVENT
            DTSTART:19970714T170000Z
            DTEND:19970715T035959Z
            SUMMARY:Bastille Day Party
            X-SOMETHING:{r}
            END:VEVENT
            END:VCALENDAR
            ''').strip())

        too_new_item = self._create_bogus_item(item_template=dedent(u'''
            BEGIN:VCALENDAR
            VERSION:2.0
            PRODID:-//hacksw/handcal//NONSGML v1.0//EN
            BEGIN:VEVENT
            DTSTART:20150714T170000Z
            DTEND:20150715T035959Z
            SUMMARY:Another Bastille Day Party
            X-SOMETHING:{r}
            END:VEVENT
            END:VCALENDAR
            ''').strip())

        good_item = self._create_bogus_item(item_template=dedent(u'''
            BEGIN:VCALENDAR
            VERSION:2.0
            PRODID:-//hacksw/handcal//NONSGML v1.0//EN
            BEGIN:VEVENT
            DTSTART:20130911T170000Z
            DTEND:20130912T035959Z
            SUMMARY:What's with all these Bastille Day Partys
            X-SOMETHING:{r}
            END:VEVENT
            END:VCALENDAR
            ''').strip())

        s.upload(too_old_item)
        s.upload(too_new_item)
        href, etag = s.upload(good_item)

        assert list(s.list()) == [(href, etag)]

    def test_item_types_passed_as_string(self):
        kw = self.get_storage_args()
        a = self.storage_class(item_types='VTODO,VEVENT', **kw)
        b = self.storage_class(item_types=('VTODO', 'VEVENT'), **kw)
        assert a.item_types == b.item_types == ('VTODO', 'VEVENT')

    def test_invalid_resource(self, monkeypatch):
        calls = []
        args = self.get_storage_args(collection=None)

        def request(session, method, url, data=None, headers=None, auth=None,
                    verify=None):
            assert method == 'OPTIONS'
            assert url == args['url']
            calls.append(None)

            r = requests.Response()
            r.status_code = 200
            r._content = 'Hello World.'
            return r

        monkeypatch.setattr('requests.sessions.Session.request', request)

        with pytest.raises(ValueError):
            self.storage_class(**args)
        assert len(calls) == 1

    def test_empty_get_multi_performance(self, monkeypatch):
        s = self._get_storage()

        def breakdown(*a, **kw):
            raise AssertionError('Expected not to be called.')

        monkeypatch.setattr('requests.sessions.Session.request', breakdown)

        assert list(s.get_multi([])) == []

class TestCarddavStorage(DavStorageTests):
    storage_class = CarddavStorage
    item_template = VCARD_TEMPLATE
<commit_msg>owncloud requires UIDs<commit_after># -*- coding: utf-8 -*-
'''
    tests.storage.dav.test_main
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: (c) 2014 Markus Unterwaditzer
    :license: MIT, see LICENSE for more details.
'''

import datetime
import os
from textwrap import dedent

import pytest

import requests
import requests.exceptions

from .. import StorageTests
import vdirsyncer.exceptions as exceptions
from vdirsyncer.storage.base import Item
from vdirsyncer.storage.dav import CaldavStorage, CarddavStorage

dav_server = os.environ.get('DAV_SERVER', '').strip() or 'radicale'

def _get_server_mixin(server_name):
    from . import __name__ as base
    x = __import__('{}.servers.{}'.format(base, server_name), fromlist=[''])
    return x.ServerMixin

ServerMixin = _get_server_mixin(dav_server)

VCARD_TEMPLATE = u'''BEGIN:VCARD
VERSION:3.0
FN:Cyrus Daboo
N:Daboo;Cyrus
ADR;TYPE=POSTAL:;2822 Email HQ;Suite 2821;RFCVille;PA;15213;USA
EMAIL;TYPE=INTERNET;TYPE=PREF:cyrus@example.com
NICKNAME:me
NOTE:Example VCard.
ORG:Self Employed
TEL;TYPE=WORK;TYPE=VOICE:412 605 0499
TEL;TYPE=FAX:412 605 0705
URL:http://www.example.com
X-SOMETHING:{r}
UID:{r}
END:VCARD'''

TASK_TEMPLATE = u'''BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//dmfs.org//mimedir.icalendar//EN
BEGIN:VTODO
CREATED:20130721T142233Z
DTSTAMP:20130730T074543Z
LAST-MODIFIED;VALUE=DATE-TIME:20140122T151338Z
SEQUENCE:2
SUMMARY:Book: Kowlani - Tödlicher Staub
X-SOMETHING:{r}
UID:{r}
END:VTODO
END:VCALENDAR'''

EVENT_TEMPLATE = u'''BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
X-SOMETHING:{r}
UID:{r}
END:VEVENT
END:VCALENDAR'''

templates = {
    'VCARD': VCARD_TEMPLATE,
    'VEVENT': EVENT_TEMPLATE,
    'VTODO': TASK_TEMPLATE
}

class DavStorageTests(ServerMixin, StorageTests):
    def test_dav_broken_item(self):
        item = Item(u'HAHA:YES')
        s = self._get_storage()
        try:
            s.upload(item)
        except (exceptions.Error, requests.exceptions.HTTPError):
            pass
        assert not list(s.list())

    @pytest.mark.xfail(dav_server == 'owncloud',
                       reason='See issue #16')
    def test_wrong_etag(self):
        super(DavStorageTests, self).test_wrong_etag()

    @pytest.mark.xfail(dav_server == 'owncloud',
                       reason='See issue #16')
    def test_update_nonexisting(self):
        super(DavStorageTests, self).test_update_nonexisting()

class TestCaldavStorage(DavStorageTests):
    storage_class = CaldavStorage

    item_template = TASK_TEMPLATE

    def test_both_vtodo_and_vevent(self):
        task = self._create_bogus_item(item_template=TASK_TEMPLATE)
        event = self._create_bogus_item(item_template=EVENT_TEMPLATE)
        s = self._get_storage()
        href_etag_task = s.upload(task)
        href_etag_event = s.upload(event)
        assert set(s.list()) == set([
            href_etag_task,
            href_etag_event
        ])

    @pytest.mark.parametrize('item_type', ['VTODO', 'VEVENT'])
    def test_item_types_correctness(self, item_type):
        other_item_type = 'VTODO' if item_type == 'VEVENT' else 'VEVENT'
        kw = self.get_storage_args()
        s = self.storage_class(item_types=(item_type,), **kw)
        try:
            s.upload(self._create_bogus_item(
                item_template=templates[other_item_type]))
            s.upload(self._create_bogus_item(
                item_template=templates[other_item_type]))
        except (exceptions.Error, requests.exceptions.HTTPError):
            pass
        href, etag = \
            s.upload(self._create_bogus_item(
                item_template=templates[item_type]))
        ((href2, etag2),) = s.list()
        assert href2 == href
        assert etag2 == etag

    @pytest.mark.parametrize('item_types', [
        ('VTODO',),
        ('VEVENT',),
        ('VTODO', 'VEVENT'),
        ('VTODO', 'VEVENT', 'VJOURNAL')
    ])
    def test_item_types_performance(self, item_types, monkeypatch):
        kw = self.get_storage_args()
        s = self.storage_class(item_types=item_types, **kw)

        old_list = s._list
        calls = []

        def _list(*a, **kw):
            calls.append(None)
            return old_list(*a, **kw)

        monkeypatch.setattr(s, '_list', _list)

        list(s.list())
        assert len(calls) == len(item_types)

    @pytest.mark.xfail(dav_server == 'radicale',
                       reason='Radicale doesn\'t support timeranges.')
    def test_timerange_correctness(self):
        kw = self.get_storage_args()
        start_date = datetime.datetime(2013, 9, 10)
        end_date = datetime.datetime(2013, 9, 13)
        s = self.storage_class(start_date=start_date, end_date=end_date, **kw)

        too_old_item = self._create_bogus_item(item_template=dedent(u'''
            BEGIN:VCALENDAR
            VERSION:2.0
            PRODID:-//hacksw/handcal//NONSGML v1.0//EN
            BEGIN:VEVENT
            DTSTART:19970714T170000Z
            DTEND:19970715T035959Z
            SUMMARY:Bastille Day Party
            X-SOMETHING:{r}
            UID:{r}
            END:VEVENT
            END:VCALENDAR
            ''').strip())

        too_new_item = self._create_bogus_item(item_template=dedent(u'''
            BEGIN:VCALENDAR
            VERSION:2.0
            PRODID:-//hacksw/handcal//NONSGML v1.0//EN
            BEGIN:VEVENT
            DTSTART:20150714T170000Z
            DTEND:20150715T035959Z
            SUMMARY:Another Bastille Day Party
            X-SOMETHING:{r}
            UID:{r}
            END:VEVENT
            END:VCALENDAR
            ''').strip())

        good_item = self._create_bogus_item(item_template=dedent(u'''
            BEGIN:VCALENDAR
            VERSION:2.0
            PRODID:-//hacksw/handcal//NONSGML v1.0//EN
            BEGIN:VEVENT
            DTSTART:20130911T170000Z
            DTEND:20130912T035959Z
            SUMMARY:What's with all these Bastille Day Partys
            X-SOMETHING:{r}
            UID:{r}
            END:VEVENT
            END:VCALENDAR
            ''').strip())

        s.upload(too_old_item)
        s.upload(too_new_item)
        href, etag = s.upload(good_item)

        assert list(s.list()) == [(href, etag)]

    def test_item_types_passed_as_string(self):
        kw = self.get_storage_args()
        a = self.storage_class(item_types='VTODO,VEVENT', **kw)
        b = self.storage_class(item_types=('VTODO', 'VEVENT'), **kw)
        assert a.item_types == b.item_types == ('VTODO', 'VEVENT')

    def test_invalid_resource(self, monkeypatch):
        calls = []
        args = self.get_storage_args(collection=None)

        def request(session, method, url, data=None, headers=None, auth=None,
                    verify=None):
            assert method == 'OPTIONS'
            assert url == args['url']
            calls.append(None)

            r = requests.Response()
            r.status_code = 200
            r._content = 'Hello World.'
            return r

        monkeypatch.setattr('requests.sessions.Session.request', request)

        with pytest.raises(ValueError):
            self.storage_class(**args)
        assert len(calls) == 1

    def test_empty_get_multi_performance(self, monkeypatch):
        s = self._get_storage()

        def breakdown(*a, **kw):
            raise AssertionError('Expected not to be called.')

        monkeypatch.setattr('requests.sessions.Session.request', breakdown)

        assert list(s.get_multi([])) == []

class TestCarddavStorage(DavStorageTests):
    storage_class = CarddavStorage
    item_template = VCARD_TEMPLATE
<|endoftext|>


<commit_before>"""

File: beat_poition.py

Purpose: Defines a class for representing a (measure, beat_in_measure) position.
         Proper interpretation of BeatPosition requires a TimeSignature entity.

"""

class BeatPosition(object):
    """
    Class that represents a measure/beat location.
    """

    def __init__(self, measure_number, beat_number):
        """
        Args:
          measure_number:  An integer representing the measure ordinal
          beat_number: A Fraction representing the beat within the measure.
        """
        self.__measure_number = measure_number
        self.__beat_number = beat_number

    @property
    def measure_number(self):
        return self.__measure_number

    @property
    def beat_number(self):
        return self.__beat_number

    def __lt__(self, other):
        return (self.measure_number < other.measure_number) or \
               (self.measure_number == other.measure_number and self.beat_number < other.beat_number)

    def __eq__(self, other):
        if other is None:
            return False
        return self.measure_number == other.measure_number and self.beat_number == other.beat_number

    def __le__(self, other):
        return self.__lt__(other) or self.__eq__(other)

    def __ne__(self, other):
        if other is None:
            return False
        return self.measure_number != other.measure_number or self.beat_number != other.beat_number

    def __gt__(self, other):
        if other is None:
            return False
        return not self.__le__(other)

    def __ge__(self, other):
        if other is None:
            return False
        return not self.__lt__(other)

    def __hash__(self):
        return hash(str(self))

    def __str__(self):
        return 'BP[{0}, {1}]'.format(self.measure_number, self.beat_number)
<commit_msg>Add beat and beat_fraction properties.<commit_after>"""

File: beat_poition.py

Purpose: Defines a class for representing a (measure, beat_in_measure) position.
         Proper interpretation of BeatPosition requires a TimeSignature entity.

"""

class BeatPosition(object):
    """
    Class that represents a measure/beat location.
    """

    def __init__(self, measure_number, beat_number):
        """
        Args:
          measure_number:  An integer representing the measure ordinal
          beat_number: A Fraction representing the beat within the measure.
        """
        self.__measure_number = measure_number
        self.__beat_number = beat_number

    @property
    def measure_number(self):
        return self.__measure_number

    @property
    def beat_number(self):
        return self.__beat_number

    @property
    def beat(self):
        return int(self.beat_number)

    @property
    def beat_fraction(self):
        return self.beat_number - self.beat

    def __lt__(self, other):
        return (self.measure_number < other.measure_number) or \
               (self.measure_number == other.measure_number and self.beat_number < other.beat_number)

    def __eq__(self, other):
        if other is None:
            return False
        return self.measure_number == other.measure_number and self.beat_number == other.beat_number

    def __le__(self, other):
        return self.__lt__(other) or self.__eq__(other)

    def __ne__(self, other):
        if other is None:
            return False
        return self.measure_number != other.measure_number or self.beat_number != other.beat_number

    def __gt__(self, other):
        if other is None:
            return False
        return not self.__le__(other)

    def __ge__(self, other):
        if other is None:
            return False
        return not self.__lt__(other)

    def __hash__(self):
        return hash(str(self))

    def __str__(self):
        return 'BP[{0}, {1}]'.format(self.measure_number, self.beat_number)
<|endoftext|>


<commit_before># See the License for the specific language governing permissions and
# limitations under the License.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import mock
import numpy as np

from tests import utils

class Test_polynomial_sign(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(poly_surface):
        from bezier import _surface_helpers

        return _surface_helpers.polynomial_sign(poly_surface)

    def _helper(self, bernstein, expected):
        import bezier

        poly_surface = bezier.Surface(bernstein)
        result = self._call_function_under_test(poly_surface)
        self.assertEqual(result, expected)

    def test_positive(self):
        # pylint: disable=no-member
        bernstein = np.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]]).T
        # pylint: enable=no-member
        self._helper(bernstein, 1)

    def test_negative(self):
        # pylint: disable=no-member
        bernstein = np.array([[-1.0, -2.0, -1.0]]).T
        # pylint: enable=no-member
        self._helper(bernstein, -1)

    def test_zero(self):
        bernstein = np.zeros((10, 1))
        self._helper(bernstein, 0)

    def test_mixed(self):
        # pylint: disable=no-member
        bernstein = np.array([[-1.0, 1.0, -1.0]]).T
        # pylint: enable=no-member
        self._helper(bernstein, 0)

    def test_max_iterations(self):
        # pylint: disable=no-member
        bernstein = np.array([[1.0, 2.0, 3.0]]).T
        # pylint: enable=no-member
        with mock.patch('bezier._surface_helpers.MAX_SUBDIVISIONS', new=1):
            self._helper(bernstein, 1)

    def test_no_conclusion(self):
        # pylint: disable=no-member
        bernstein = np.array([[-1.0, 1.0, 2.0]]).T
        # pylint: enable=no-member
        with mock.patch('bezier._surface_helpers.MAX_SUBDIVISIONS', new=0):
            with self.assertRaises(ValueError):
                self._helper(bernstein, None)

class Test__2x2_det(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(mat):
        from bezier import _surface_helpers

        return _surface_helpers._2x2_det(mat)

    def test_integers(self):
        mat = np.array([
            [1.0, 2.0],
            [3.0, 4.0],
        ])
        self.assertEqual(self._call_function_under_test(mat), -2.0)

    def test_better_than_numpy(self):
        mat = np.array([
            [-24.0, 3.0],
            [-27.0, 0.0],
        ]) / 16.0
        actual_det = self._call_function_under_test(mat)
        self.assertEqual(actual_det, 81.0 / 256.0)

        np_det = np.linalg.det(mat)
        self.assertNotEqual(actual_det, np_det)
        self.assertLess(abs(actual_det - np_det), 1e-16)

class Test_quadratic_jacobian_polynomial(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(nodes):
        from bezier import _surface_helpers

        return _surface_helpers.quadratic_jacobian_polynomial(nodes)

    def test_it(self):
        # B(L1, L2, L3) = [L1^2 + L2^2, L2^2 + L3^2]
        nodes = np.array([
            [1.0, 0.0],
            [0.0, 0.0],
            [1.0, 1.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 1.0],
        ])
        bernstein = self._call_function_under_test(nodes)
        self.assertEqual(bernstein.shape, (6, 1))
        expected = np.array([[0.0, 2.0, 0.0, -2.0, 2.0, 0.0]])
        expected = expected.T  # pylint: disable=no-member
        self.assertTrue(np.all(bernstein == expected))

class Test_cubic_jacobian_polynomial(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(nodes):
        from bezier import _surface_helpers

        return _surface_helpers.cubic_jacobian_polynomial(nodes)

    def test_it(self):
        # B(L1, L2, L3) = [L1^3 + L2^3, L2^3 + L3^3]
        nodes = np.array([
            [1.0, 0.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [1.0, 1.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 1.0],
        ])
        bernstein = self._call_function_under_test(nodes)
        shape = (15, 1)
        self.assertEqual(bernstein.shape, shape)
        expected = np.zeros(shape)
        expected[2, 0] = 1.5
        expected[9, 0] = -1.5
        expected[11, 0] = 1.5
        self.assertTrue(np.all(bernstein == expected))

class Test_de_casteljau_one_round(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(nodes, degree, lambda1, lambda2, lambda3):
        from bezier import _surface_helpers

        return _surface_helpers.de_casteljau_one_round(
            nodes, degree, lambda1, lambda2, lambda3)

    def test_linear(self):
        nodes = np.array([
            [0.0, 0.0],
            [1.0, 0.0],
            [0.0, 1.0],
        ])
        s_val, t_val = 0.5, 0.375
        expected = np.array([
            [s_val, t_val],
        ])

        result = self._call_function_under_test(
            nodes, 1, 1.0 - s_val - t_val, s_val, t_val)
        self.assertTrue(np.all(result == expected))

    def test_quadratic(self):
        # Use a fixed seed so the test is deterministic and round
        # the nodes to 8 bits of precision to avoid round-off.
        nodes = utils.get_random_nodes(
            shape=(6, 2), seed=97764, num_bits=8)

        p200, p110, p020, p101, p011, p002 = nodes
        s_val = 0.25
        t_val = 0.125

        q100 = (1.0 - s_val - t_val) * p200 + s_val * p110 + t_val * p101
        q010 = (1.0 - s_val - t_val) * p110 + s_val * p020 + t_val * p011
        q001 = (1.0 - s_val - t_val) * p101 + s_val * p011 + t_val * p002

        expected = np.vstack([q100, q010, q001])
        result = self._call_function_under_test(
            nodes, 2, 1.0 - s_val - t_val, s_val, t_val)
        self.assertTrue(np.all(result == expected))

    def test_cubic(self):
        nodes = np.array([
            [0.0, 0.0],
            [3.25, 1.5],
            [6.5, 1.5],
            [10.0, 0.0],
            [1.5, 3.25],
            [5.0, 5.0],
            [10.0, 5.25],
            [1.5, 6.5],
            [5.25, 10.0],
            [0.0, 10.0],
        ])

        s_val = 0.25
        t_val = 0.375
        lambda1 = 1.0 - s_val - t_val
        transform = np.array([
            [lambda1, s_val, 0., 0., t_val, 0., 0., 0., 0., 0.],
            [0., lambda1, s_val, 0., 0., t_val, 0., 0., 0., 0.],
            [0., 0., lambda1, s_val, 0., 0., t_val, 0., 0., 0.],
            [0., 0., 0., 0., lambda1, s_val, 0., t_val, 0., 0.],
            [0., 0., 0., 0., 0., lambda1, s_val, 0., t_val, 0.],
            [0., 0., 0., 0., 0., 0., 0., lambda1, s_val, t_val],
        ])
        # pylint: disable=no-member
        expected = transform.dot(nodes)
        # pylint: enable=no-member
        result = self._call_function_under_test(
            nodes, 3, lambda1, s_val, t_val)
        self.assertTrue(np.all(result == expected))

class Test_specialize_surface(unittest.TestCase):

    WEIGHTS0 = (1.0, 0.0, 0.0)
    WEIGHTS1 = (0.5, 0.5, 0.0)
    WEIGHTS2 = (0.0, 1.0, 0.0)
    WEIGHTS3 = (0.5, 0.0, 0.5)
    WEIGHTS4 = (0.0, 0.5, 0.5)
    WEIGHTS5 = (0.0, 0.0, 1.0)

    @staticmethod
    def _call_function_under_test(nodes, degree,
                                  weights_a, weights_b, weights_c):
        from bezier import _surface_helpers

        return _surface_helpers.specialize_surface(
            nodes, degree, weights_a, weights_b, weights_c)

    def _helpers(self, degree, all_nodes, inds_a, inds_b, inds_c, inds_d):
        num_nodes = len(inds_a)
        id_mat = np.eye(num_nodes)

        expected_a = self._call_function_under_test(
            id_mat, degree,
            self.WEIGHTS0, self.WEIGHTS1, self.WEIGHTS3)
        expected_b = self._call_function_under_test(
            id_mat, degree,
            self.WEIGHTS4, self.WEIGHTS3, self.WEIGHTS1)
        expected_c = self._call_function_under_test(
            id_mat, degree,
            self.WEIGHTS1, self.WEIGHTS2, self.WEIGHTS4)
        expected_d = self._call_function_under_test(
            id_mat, degree,
            self.WEIGHTS3, self.WEIGHTS4, self.WEIGHTS5)

        self.assertTrue(np.all(all_nodes[inds_a, :] == expected_a))
        self.assertTrue(np.all(all_nodes[inds_b, :] == expected_b))
        self.assertTrue(np.all(all_nodes[inds_c, :] == expected_c))
        self.assertTrue(np.all(all_nodes[inds_d, :] == expected_d))

    def test_known_linear(self):
        from bezier import _surface_helpers

        all_nodes = _surface_helpers.LINEAR_SUBDIVIDE
        self._helpers(1, all_nodes,
                      (0, 1, 3), (4, 3, 1),
                      (1, 2, 4), (3, 4, 5))

    def test_known_quadratic(self):
        from bezier import _surface_helpers

        all_nodes = _surface_helpers.QUADRATIC_SUBDIVIDE
        self._helpers(2, all_nodes,
                      (0, 1, 2, 5, 6, 9),
                      (11, 10, 9, 7, 6, 2),
                      (2, 3, 4, 7, 8, 11),
                      (9, 10, 11, 12, 13, 14))

    def test_known_cubic(self):
        from bezier import _surface_helpers

        all_nodes = _surface_helpers.CUBIC_SUBDIVIDE
        self._helpers(3, all_nodes,
                      (0, 1, 2, 3, 7, 8, 9, 13, 14, 18),
                      (21, 20, 19, 18, 16, 15, 14, 10, 9, 3),
                      (3, 4, 5, 6, 10, 11, 12, 16, 17, 21),
                      (18, 19, 20, 21, 22, 23, 24, 25, 26, 27))
<commit_msg>Adding unit test for _make_transform().<commit_after># See the License for the specific language governing permissions and
# limitations under the License.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import mock
import numpy as np

from tests import utils

class Test_polynomial_sign(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(poly_surface):
        from bezier import _surface_helpers

        return _surface_helpers.polynomial_sign(poly_surface)

    def _helper(self, bernstein, expected):
        import bezier

        poly_surface = bezier.Surface(bernstein)
        result = self._call_function_under_test(poly_surface)
        self.assertEqual(result, expected)

    def test_positive(self):
        # pylint: disable=no-member
        bernstein = np.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]]).T
        # pylint: enable=no-member
        self._helper(bernstein, 1)

    def test_negative(self):
        # pylint: disable=no-member
        bernstein = np.array([[-1.0, -2.0, -1.0]]).T
        # pylint: enable=no-member
        self._helper(bernstein, -1)

    def test_zero(self):
        bernstein = np.zeros((10, 1))
        self._helper(bernstein, 0)

    def test_mixed(self):
        # pylint: disable=no-member
        bernstein = np.array([[-1.0, 1.0, -1.0]]).T
        # pylint: enable=no-member
        self._helper(bernstein, 0)

    def test_max_iterations(self):
        # pylint: disable=no-member
        bernstein = np.array([[1.0, 2.0, 3.0]]).T
        # pylint: enable=no-member
        with mock.patch('bezier._surface_helpers.MAX_SUBDIVISIONS', new=1):
            self._helper(bernstein, 1)

    def test_no_conclusion(self):
        # pylint: disable=no-member
        bernstein = np.array([[-1.0, 1.0, 2.0]]).T
        # pylint: enable=no-member
        with mock.patch('bezier._surface_helpers.MAX_SUBDIVISIONS', new=0):
            with self.assertRaises(ValueError):
                self._helper(bernstein, None)

class Test__2x2_det(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(mat):
        from bezier import _surface_helpers

        return _surface_helpers._2x2_det(mat)

    def test_integers(self):
        mat = np.array([
            [1.0, 2.0],
            [3.0, 4.0],
        ])
        self.assertEqual(self._call_function_under_test(mat), -2.0)

    def test_better_than_numpy(self):
        mat = np.array([
            [-24.0, 3.0],
            [-27.0, 0.0],
        ]) / 16.0
        actual_det = self._call_function_under_test(mat)
        self.assertEqual(actual_det, 81.0 / 256.0)

        np_det = np.linalg.det(mat)
        self.assertNotEqual(actual_det, np_det)
        self.assertLess(abs(actual_det - np_det), 1e-16)

class Test_quadratic_jacobian_polynomial(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(nodes):
        from bezier import _surface_helpers

        return _surface_helpers.quadratic_jacobian_polynomial(nodes)

    def test_it(self):
        # B(L1, L2, L3) = [L1^2 + L2^2, L2^2 + L3^2]
        nodes = np.array([
            [1.0, 0.0],
            [0.0, 0.0],
            [1.0, 1.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 1.0],
        ])
        bernstein = self._call_function_under_test(nodes)
        self.assertEqual(bernstein.shape, (6, 1))
        expected = np.array([[0.0, 2.0, 0.0, -2.0, 2.0, 0.0]])
        expected = expected.T  # pylint: disable=no-member
        self.assertTrue(np.all(bernstein == expected))

class Test_cubic_jacobian_polynomial(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(nodes):
        from bezier import _surface_helpers

        return _surface_helpers.cubic_jacobian_polynomial(nodes)

    def test_it(self):
        # B(L1, L2, L3) = [L1^3 + L2^3, L2^3 + L3^3]
        nodes = np.array([
            [1.0, 0.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [1.0, 1.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 0.0],
            [0.0, 1.0],
        ])
        bernstein = self._call_function_under_test(nodes)
        shape = (15, 1)
        self.assertEqual(bernstein.shape, shape)
        expected = np.zeros(shape)
        expected[2, 0] = 1.5
        expected[9, 0] = -1.5
        expected[11, 0] = 1.5
        self.assertTrue(np.all(bernstein == expected))

class Test_de_casteljau_one_round(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(nodes, degree, lambda1, lambda2, lambda3):
        from bezier import _surface_helpers

        return _surface_helpers.de_casteljau_one_round(
            nodes, degree, lambda1, lambda2, lambda3)

    def test_linear(self):
        nodes = np.array([
            [0.0, 0.0],
            [1.0, 0.0],
            [0.0, 1.0],
        ])
        s_val, t_val = 0.5, 0.375
        expected = np.array([
            [s_val, t_val],
        ])

        result = self._call_function_under_test(
            nodes, 1, 1.0 - s_val - t_val, s_val, t_val)
        self.assertTrue(np.all(result == expected))

    def test_quadratic(self):
        # Use a fixed seed so the test is deterministic and round
        # the nodes to 8 bits of precision to avoid round-off.
        nodes = utils.get_random_nodes(
            shape=(6, 2), seed=97764, num_bits=8)

        p200, p110, p020, p101, p011, p002 = nodes
        s_val = 0.25
        t_val = 0.125

        q100 = (1.0 - s_val - t_val) * p200 + s_val * p110 + t_val * p101
        q010 = (1.0 - s_val - t_val) * p110 + s_val * p020 + t_val * p011
        q001 = (1.0 - s_val - t_val) * p101 + s_val * p011 + t_val * p002

        expected = np.vstack([q100, q010, q001])
        result = self._call_function_under_test(
            nodes, 2, 1.0 - s_val - t_val, s_val, t_val)
        self.assertTrue(np.all(result == expected))

    def test_cubic(self):
        nodes = np.array([
            [0.0, 0.0],
            [3.25, 1.5],
            [6.5, 1.5],
            [10.0, 0.0],
            [1.5, 3.25],
            [5.0, 5.0],
            [10.0, 5.25],
            [1.5, 6.5],
            [5.25, 10.0],
            [0.0, 10.0],
        ])

        s_val = 0.25
        t_val = 0.375
        lambda1 = 1.0 - s_val - t_val
        transform = np.array([
            [lambda1, s_val, 0., 0., t_val, 0., 0., 0., 0., 0.],
            [0., lambda1, s_val, 0., 0., t_val, 0., 0., 0., 0.],
            [0., 0., lambda1, s_val, 0., 0., t_val, 0., 0., 0.],
            [0., 0., 0., 0., lambda1, s_val, 0., t_val, 0., 0.],
            [0., 0., 0., 0., 0., lambda1, s_val, 0., t_val, 0.],
            [0., 0., 0., 0., 0., 0., 0., lambda1, s_val, t_val],
        ])
        # pylint: disable=no-member
        expected = transform.dot(nodes)
        # pylint: enable=no-member
        result = self._call_function_under_test(
            nodes, 3, lambda1, s_val, t_val)
        self.assertTrue(np.all(result == expected))

class Test__make_transform(unittest.TestCase):

    @staticmethod
    def _call_function_under_test(degree, weights_a, weights_b, weights_c):
        from bezier import _surface_helpers

        return _surface_helpers._make_transform(
            degree, weights_a, weights_b, weights_c)

    def _helper(self, degree, weights, expected0, expected1, expected2):
        result = self._call_function_under_test(
            degree, weights[0, :], weights[1, :], weights[2, :])

        self.assertIsInstance(result, dict)
        self.assertEqual(len(result), 3)
        self.assertTrue(np.all(result[0] == expected0))
        self.assertTrue(np.all(result[1] == expected1))
        self.assertTrue(np.all(result[2] == expected2))

    def test_linear(self):
        weights = np.array([
            [1.0, 0.0, 0.0],
            [0.5, 0.5, 0.0],
            [0.5, 0.0, 0.5],
        ])
        expected0 = weights[[0], :]
        expected1 = weights[[1], :]
        expected2 = weights[[2], :]
        self._helper(1, weights, expected0, expected1, expected2)

    def test_quadratic(self):
        weights = np.array([
            [0.0, 0.5, 0.5],
            [0.5, 0.0, 0.5],
            [0.5, 0.5, 0.0],
        ])
        expected0 = np.array([
            [0.0, 0.5, 0.0, 0.5, 0.0, 0.0],
            [0.0, 0.0, 0.5, 0.0, 0.5, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.5, 0.5],
        ])
        expected1 = np.array([
            [0.5, 0.0, 0.0, 0.5, 0.0, 0.0],
            [0.0, 0.5, 0.0, 0.0, 0.5, 0.0],
            [0.0, 0.0, 0.0, 0.5, 0.0, 0.5],
        ])
        expected2 = np.array([
            [0.5, 0.5, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.5, 0.5, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.5, 0.5, 0.0],
        ])
        self._helper(2, weights, expected0, expected1, expected2)

class Test_specialize_surface(unittest.TestCase):

    WEIGHTS0 = (1.0, 0.0, 0.0)
    WEIGHTS1 = (0.5, 0.5, 0.0)
    WEIGHTS2 = (0.0, 1.0, 0.0)
    WEIGHTS3 = (0.5, 0.0, 0.5)
    WEIGHTS4 = (0.0, 0.5, 0.5)
    WEIGHTS5 = (0.0, 0.0, 1.0)

    @staticmethod
    def _call_function_under_test(nodes, degree,
                                  weights_a, weights_b, weights_c):
        from bezier import _surface_helpers

        return _surface_helpers.specialize_surface(
            nodes, degree, weights_a, weights_b, weights_c)

    def _helpers(self, degree, all_nodes, inds_a, inds_b, inds_c, inds_d):
        num_nodes = len(inds_a)
        id_mat = np.eye(num_nodes)

        expected_a = self._call_function_under_test(
            id_mat, degree,
            self.WEIGHTS0, self.WEIGHTS1, self.WEIGHTS3)
        expected_b = self._call_function_under_test(
            id_mat, degree,
            self.WEIGHTS4, self.WEIGHTS3, self.WEIGHTS1)
        expected_c = self._call_function_under_test(
            id_mat, degree,
            self.WEIGHTS1, self.WEIGHTS2, self.WEIGHTS4)
        expected_d = self._call_function_under_test(
            id_mat, degree,
            self.WEIGHTS3, self.WEIGHTS4, self.WEIGHTS5)

        self.assertTrue(np.all(all_nodes[inds_a, :] == expected_a))
        self.assertTrue(np.all(all_nodes[inds_b, :] == expected_b))
        self.assertTrue(np.all(all_nodes[inds_c, :] == expected_c))
        self.assertTrue(np.all(all_nodes[inds_d, :] == expected_d))

    def test_known_linear(self):
        from bezier import _surface_helpers

        all_nodes = _surface_helpers.LINEAR_SUBDIVIDE
        self._helpers(1, all_nodes,
                      (0, 1, 3), (4, 3, 1),
                      (1, 2, 4), (3, 4, 5))

    def test_known_quadratic(self):
        from bezier import _surface_helpers

        all_nodes = _surface_helpers.QUADRATIC_SUBDIVIDE
        self._helpers(2, all_nodes,
                      (0, 1, 2, 5, 6, 9),
                      (11, 10, 9, 7, 6, 2),
                      (2, 3, 4, 7, 8, 11),
                      (9, 10, 11, 12, 13, 14))

    def test_known_cubic(self):
        from bezier import _surface_helpers

        all_nodes = _surface_helpers.CUBIC_SUBDIVIDE
        self._helpers(3, all_nodes,
                      (0, 1, 2, 3, 7, 8, 9, 13, 14, 18),
                      (21, 20, 19, 18, 16, 15, 14, 10, 9, 3),
                      (3, 4, 5, 6, 10, 11, 12, 16, 17, 21),
                      (18, 19, 20, 21, 22, 23, 24, 25, 26, 27))
<|endoftext|>


<commit_before>"""
Test context managers -- i.e.: the `with` statement
"""

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from pymssql import InterfaceError
from .helpers import pymssqlconn

class TestContextManagers(unittest.TestCase):
    def test_pymssql_Connection_with(self):
        with pymssqlconn() as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT @@version AS version")
            self.assertIsNotNone(conn._conn)

        with self.assertRaises(InterfaceError) as context:
            self.assertIsNotNone(conn._conn)

        self.assertEqual(str(context.exception), "Connection is closed.")

    def test_pymssql_Cursor_with(self):
        conn = pymssqlconn()
        with conn.cursor() as cursor:
            cursor.execute("SELECT @@version AS version")
            self.assertIsNotNone(conn._conn)

        self.assertIsNotNone(cursor)

        with self.assertRaises(InterfaceError) as context:
            cursor.execute("SELECT @@version AS version")

        self.assertEqual(str(context.exception), "Cursor is closed.")
<commit_msg>test_context_managers.py: Add blank line for pep8<commit_after>"""
Test context managers -- i.e.: the `with` statement
"""

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from pymssql import InterfaceError
from .helpers import pymssqlconn

class TestContextManagers(unittest.TestCase):
    def test_pymssql_Connection_with(self):
        with pymssqlconn() as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT @@version AS version")
            self.assertIsNotNone(conn._conn)

        with self.assertRaises(InterfaceError) as context:
            self.assertIsNotNone(conn._conn)

        self.assertEqual(str(context.exception), "Connection is closed.")

    def test_pymssql_Cursor_with(self):
        conn = pymssqlconn()
        with conn.cursor() as cursor:
            cursor.execute("SELECT @@version AS version")
            self.assertIsNotNone(conn._conn)

        self.assertIsNotNone(cursor)

        with self.assertRaises(InterfaceError) as context:
            cursor.execute("SELECT @@version AS version")

        self.assertEqual(str(context.exception), "Cursor is closed.")
<|endoftext|>


<commit_before>import io
import os
import tempfile
import types

from six import PY3, PY2

from pywincffi.core import dist
from pywincffi.core.checks import (
    INPUT_CHECK_MAPPINGS, FileType, CheckMapping, Enums,
    input_check)
from pywincffi.dev.testutil import TestCase
from pywincffi.exceptions import InputError

class TestTypeCheckFailure(TestCase):
    """
    Tests for :func:`pywincffi.core.types.input_check`
    """
    def test_type_error(self):
        with self.assertRaises(InputError):
            input_check("foobar", 1, str)

    def test_handle_type_failure(self):
        with self.assertRaises(InputError):
            input_check("", None, Enums.HANDLE)

    def test_not_a_handle(self):
        ffi, _ = dist.load()
        with self.assertRaises(InputError):
            input_check("", ffi.new("void *[2]"), Enums.HANDLE)

class TestEnumMapping(TestCase):
    def setUp(self):
        self.original_mappings = INPUT_CHECK_MAPPINGS.copy()

    def tearDown(self):
        super(TestEnumMapping, self).tearDown()
        INPUT_CHECK_MAPPINGS.clear()
        INPUT_CHECK_MAPPINGS.update(self.original_mappings)

    def test_nullable(self):
        INPUT_CHECK_MAPPINGS.update(
            mapping=CheckMapping(
                kind="pointer",
                cname="void *",
                nullable=True
            )
        )

        ffi, _ = dist.load()
        input_check("", ffi.NULL, "mapping")

    def test_overlapped(self):
        ffi, _ = dist.load()
        input_check("", ffi.new("OVERLAPPED[1]"), Enums.OVERLAPPED)

class TestEnumUTF8(TestCase):
    def test_attribute_error(self):
        with self.assertRaises(InputError):
            input_check("", None, Enums.UTF8)

class TestAllowedValues(TestCase):
    def test_exception_attribute(self):
        try:
            input_check("", 1, allowed_values=(2, ))

        except InputError as error:
            self.assertEqual(error.allowed_values, (2, ))

        else:
            self.fail("InputError not raised")

    def test_valid(self):
        input_check("", 1, allowed_values=(1, ))

    def test_invalid(self):
        with self.assertRaises(InputError):
            input_check("", 1, allowed_values=(2, ))

    def test_invalid_message(self):
        try:
            input_check("foo", 1, allowed_values=(2, ))

        except InputError as error:
            self.assertEqual(
                error.message,
                "Expected value for foo to be in (2,). Got 1 instead."
            )

        else:
            self.fail("InputError not raised")

    def test_assertion(self):
        with self.assertRaises(AssertionError):
            input_check("", None, allowed_values=1)

class TestEnumPyFile(TestCase):
    def test_file_type(self):
        if PY3:
            self.assertIs(FileType, io.IOBase)
        elif PY2:
            # pylint: disable=no-member
            self.assertIs(FileType, types.FileType)
        else:
            self.fail("This is neither Python 2 or 3")

    def test_valid(self):
        fd, path = tempfile.mkstemp()
        self.addCleanup(os.remove, path)

        with os.fdopen(fd, "r") as python_file:
            input_check("", python_file, Enums.PYFILE)

    def test_invalid(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.addCleanup(os.remove, path)

        with self.assertRaises(InputError):
            input_check("", path, Enums.PYFILE)
<commit_msg>adding missing test for nullable state of Enums.OVERLAPPED<commit_after>import io
import os
import tempfile
import types

from six import PY3, PY2

from pywincffi.core import dist
from pywincffi.core.checks import (
    INPUT_CHECK_MAPPINGS, FileType, CheckMapping, Enums,
    input_check)
from pywincffi.dev.testutil import TestCase
from pywincffi.exceptions import InputError

class TestTypeCheckFailure(TestCase):
    """
    Tests for :func:`pywincffi.core.types.input_check`
    """
    def test_type_error(self):
        with self.assertRaises(InputError):
            input_check("foobar", 1, str)

    def test_handle_type_failure(self):
        with self.assertRaises(InputError):
            input_check("", None, Enums.HANDLE)

    def test_not_a_handle(self):
        ffi, _ = dist.load()
        with self.assertRaises(InputError):
            input_check("", ffi.new("void *[2]"), Enums.HANDLE)

class TestEnumMapping(TestCase):
    def setUp(self):
        self.original_mappings = INPUT_CHECK_MAPPINGS.copy()

    def tearDown(self):
        super(TestEnumMapping, self).tearDown()
        INPUT_CHECK_MAPPINGS.clear()
        INPUT_CHECK_MAPPINGS.update(self.original_mappings)

    def test_nullable(self):
        INPUT_CHECK_MAPPINGS.update(
            mapping=CheckMapping(
                kind="pointer",
                cname="void *",
                nullable=True
            )
        )

        ffi, _ = dist.load()
        input_check("", ffi.NULL, "mapping")

    def test_overlapped(self):
        ffi, _ = dist.load()
        input_check("", ffi.new("OVERLAPPED[1]"), Enums.OVERLAPPED)

    def test_overlapped_nullable(self):
        ffi, _ = dist.load()
        input_check("", ffi.NULL, Enums.OVERLAPPED)

class TestEnumUTF8(TestCase):
    def test_attribute_error(self):
        with self.assertRaises(InputError):
            input_check("", None, Enums.UTF8)

class TestAllowedValues(TestCase):
    def test_exception_attribute(self):
        try:
            input_check("", 1, allowed_values=(2, ))

        except InputError as error:
            self.assertEqual(error.allowed_values, (2, ))

        else:
            self.fail("InputError not raised")

    def test_valid(self):
        input_check("", 1, allowed_values=(1, ))

    def test_invalid(self):
        with self.assertRaises(InputError):
            input_check("", 1, allowed_values=(2, ))

    def test_invalid_message(self):
        try:
            input_check("foo", 1, allowed_values=(2, ))

        except InputError as error:
            self.assertEqual(
                error.message,
                "Expected value for foo to be in (2,). Got 1 instead."
            )

        else:
            self.fail("InputError not raised")

    def test_assertion(self):
        with self.assertRaises(AssertionError):
            input_check("", None, allowed_values=1)

class TestEnumPyFile(TestCase):
    def test_file_type(self):
        if PY3:
            self.assertIs(FileType, io.IOBase)
        elif PY2:
            # pylint: disable=no-member
            self.assertIs(FileType, types.FileType)
        else:
            self.fail("This is neither Python 2 or 3")

    def test_valid(self):
        fd, path = tempfile.mkstemp()
        self.addCleanup(os.remove, path)

        with os.fdopen(fd, "r") as python_file:
            input_check("", python_file, Enums.PYFILE)

    def test_invalid(self):
        fd, path = tempfile.mkstemp()
        os.close(fd)
        self.addCleanup(os.remove, path)

        with self.assertRaises(InputError):
            input_check("", path, Enums.PYFILE)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_face_recognition
----------------------------------

Tests for `face_recognition` module.
"""

import unittest
import os
from click.testing import CliRunner

from face_recognition import api
from face_recognition import cli

class Test_face_recognition(unittest.TestCase):

    def test_load_image_file(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
        assert img.shape == (1137, 910, 3)

    def test_load_image_file_32bit(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "32bit.png"))
        assert img.shape == (1200, 626, 3)

    def test_raw_face_locations(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
        detected_faces = api._raw_face_locations(img)

        assert len(detected_faces) == 1
        assert detected_faces[0].top() == 142
        assert detected_faces[0].bottom() == 409

    def test_raw_face_locations_32bit_image(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "32bit.png"))
        detected_faces = api._raw_face_locations(img)

        assert len(detected_faces) == 1
        assert detected_faces[0].top() == 290
        assert detected_faces[0].bottom() == 558

    def test_face_locations(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
        detected_faces = api.face_locations(img)

        assert len(detected_faces) == 1
        assert detected_faces[0] == (142, 617, 409, 349)

    def test_raw_face_landmarks(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
        face_landmarks = api._raw_face_landmarks(img)
        example_landmark = face_landmarks[0].parts()[10]

        assert len(face_landmarks) == 1
        assert face_landmarks[0].num_parts == 68
        assert (example_landmark.x, example_landmark.y) == (552, 399)

    def test_face_landmarks(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
        face_landmarks = api.face_landmarks(img)

        assert set(face_landmarks[0].keys()) == set(['chin', 'left_eyebrow', 'right_eyebrow', 'nose_bridge', 'nose_tip', 'left_eye', 'right_eye', 'top_lip', 'bottom_lip'])
        assert face_landmarks[0]['chin'] == [(369, 220), (372, 254), (378, 289), (384, 322), (395, 353), (414, 382), (437, 407), (464, 424), (495, 428), (527, 420), (552, 399), (576, 372), (594, 344), (604, 314)]

    def test_face_encodings(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
        encodings = api.face_encodings(img)

        assert len(encodings) == 1
        assert len(encodings[0]) == 128

    def test_compare_faces(self):
        img_a1 = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
        img_a2 = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama2.jpg"))
        img_a3 = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama3.jpg"))

        img_b1 = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "biden.jpg"))

        face_encoding_a1 = api.face_encodings(img_a1)[0]
        face_encoding_a2 = api.face_encodings(img_a2)[0]
        face_encoding_a3 = api.face_encodings(img_a3)[0]
        face_encoding_b1 = api.face_encodings(img_b1)[0]

        faces_to_compare = [
            face_encoding_a2,
            face_encoding_a3,
            face_encoding_b1
        ]

        match_results = api.compare_faces(faces_to_compare, face_encoding_a1)
        assert match_results[0] == True
        assert match_results[1] == True
        assert match_results[2] == False

    def test_command_line_interface(self):
        runner = CliRunner()
        help_result = runner.invoke(cli.main, ['--help'])
        assert help_result.exit_code == 0
        assert '--help  Show this message and exit.' in help_result.output
<commit_msg>Refactor Tests (#8)<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_face_recognition
----------------------------------

Tests for `face_recognition` module.
"""

import unittest
import os
from click.testing import CliRunner

from face_recognition import api
from face_recognition import cli

class Test_face_recognition(unittest.TestCase):

    def test_load_image_file(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        self.assertEqual(img.shape, (1137, 910, 3))

    def test_load_image_file_32bit(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', '32bit.png'))
        self.assertEqual(img.shape, (1200, 626, 3))

    def test_raw_face_locations(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        detected_faces = api._raw_face_locations(img)

        self.assertEqual(len(detected_faces), 1)
        self.assertEqual(detected_faces[0].top(), 142)
        self.assertEqual(detected_faces[0].bottom(), 409)

    def test_raw_face_locations_32bit_image(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', '32bit.png'))
        detected_faces = api._raw_face_locations(img)

        self.assertEqual(len(detected_faces), 1)
        self.assertEqual(detected_faces[0].top(), 290)
        self.assertEqual(detected_faces[0].bottom(), 558)

    def test_face_locations(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        detected_faces = api.face_locations(img)

        self.assertEqual(len(detected_faces), 1)
        self.assertEqual(detected_faces[0], (142, 617, 409, 349))

    def test_raw_face_landmarks(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        face_landmarks = api._raw_face_landmarks(img)
        example_landmark = face_landmarks[0].parts()[10]

        self.assertEqual(len(face_landmarks), 1)
        self.assertEqual(face_landmarks[0].num_parts, 68)
        self.assertEqual((example_landmark.x, example_landmark.y), (552, 399))

    def test_face_landmarks(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        face_landmarks = api.face_landmarks(img)

        self.assertEqual(
            set(face_landmarks[0].keys()),
            set(['chin', 'left_eyebrow', 'right_eyebrow', 'nose_bridge',
                 'nose_tip', 'left_eye', 'right_eye', 'top_lip',
                 'bottom_lip']))
        self.assertEqual(
            face_landmarks[0]['chin'],
            [(369, 220), (372, 254), (378, 289), (384, 322), (395, 353),
             (414, 382), (437, 407), (464, 424), (495, 428), (527, 420),
             (552, 399), (576, 372), (594, 344), (604, 314)])

    def test_face_encodings(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        encodings = api.face_encodings(img)

        self.assertEqual(len(encodings), 1)
        self.assertEqual(len(encodings[0]), 128)

    def test_compare_faces(self):
        img_a1 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        img_a2 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama2.jpg'))
        img_a3 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama3.jpg'))

        img_b1 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'biden.jpg'))

        face_encoding_a1 = api.face_encodings(img_a1)[0]
        face_encoding_a2 = api.face_encodings(img_a2)[0]
        face_encoding_a3 = api.face_encodings(img_a3)[0]
        face_encoding_b1 = api.face_encodings(img_b1)[0]

        faces_to_compare = [
            face_encoding_a2,
            face_encoding_a3,
            face_encoding_b1]

        match_results = api.compare_faces(faces_to_compare, face_encoding_a1)
        self.assertIs(match_results[0], True)
        self.assertIs(match_results[1], True)
        self.assertIs(match_results[2], False)

    def test_command_line_interface(self):
        target_string = '--help  Show this message and exit.'
        runner = CliRunner()
        help_result = runner.invoke(cli.main, ['--help'])
        self.assertEqual(help_result.exit_code, 0)
        self.assertTrue(target_string in help_result.output)
<|endoftext|>


<commit_before>from datetime import date
import sqlalchemy as sa
from wtforms_alchemy import DateRange
from wtforms.validators import NumberRange
from tests import ModelFormTestCase

class TestFieldParameters(ModelFormTestCase):
    def test_assigns_labels_from_column_info(self):
        self.init(info={'label': 'Test Column'})
        self.assert_label('test_column', 'Test Column')

    def test_assigns_description_from_column_info(self):
        self.init(info={'description': 'Description'})
        self.assert_description('test_column', 'Description')

    def test_does_not_add_default_value_if_default_is_callable(self):
        self.init(default=lambda: "test")
        self.assert_default('test_column', None)

    def test_assigns_scalar_defaults(self):
        self.init(default=u"test")
        self.assert_default('test_column', "test")

    def test_min_and_max_info_attributes_generate_number_range_validator(self):
        self.init(type_=sa.Integer, info={'min': 1, 'max': 100})
        validator = self.get_validator('test_column', NumberRange)
        assert validator.min == 1
        assert validator.max == 100

    def test_min_and_max_info_attributes_generate_date_range_validator(self):
        self.init(
            type_=sa.Date,
            info={'min': date(1990, 1, 1), 'max': date(2000, 1, 1)}
        )
        validator = self.get_validator('test_column', DateRange)
        assert validator.min == date(1990, 1, 1)
        assert validator.max == date(2000, 1, 1)
<commit_msg>Added tests for custom field class, closed #18<commit_after>from datetime import date
import sqlalchemy as sa
from wtforms import widgets
from wtforms_alchemy import DateRange, ModelForm
from wtforms.fields import StringField
from wtforms.validators import NumberRange
from tests import ModelFormTestCase

class TestFieldParameters(ModelFormTestCase):
    def test_assigns_labels_from_column_info(self):
        self.init(info={'label': 'Test Column'})
        self.assert_label('test_column', 'Test Column')

    def test_assigns_description_from_column_info(self):
        self.init(info={'description': 'Description'})
        self.assert_description('test_column', 'Description')

    def test_does_not_add_default_value_if_default_is_callable(self):
        self.init(default=lambda: "test")
        self.assert_default('test_column', None)

    def test_assigns_scalar_defaults(self):
        self.init(default=u"test")
        self.assert_default('test_column', "test")

    def test_min_and_max_info_attributes_generate_number_range_validator(self):
        self.init(type_=sa.Integer, info={'min': 1, 'max': 100})
        validator = self.get_validator('test_column', NumberRange)
        assert validator.min == 1
        assert validator.max == 100

    def test_min_and_max_info_attributes_generate_date_range_validator(self):
        self.init(
            type_=sa.Date,
            info={'min': date(1990, 1, 1), 'max': date(2000, 1, 1)}
        )
        validator = self.get_validator('test_column', DateRange)
        assert validator.min == date(1990, 1, 1)
        assert validator.max == date(2000, 1, 1)

    def test_uses_custom_field_class(self):
        class InputTest(widgets.Input):
            input_type = 'color'

        class FieldTest(StringField):
            widget = InputTest()

        class ModelTest(self.base):
            __tablename__ = 'model_test'
            query = None
            id = sa.Column(sa.Integer, primary_key=True)
            test_column = sa.Column(
                sa.UnicodeText,
                info={'form_field_class': FieldTest}
            )

        class ModelTestForm(ModelForm):
            class Meta:
                model = ModelTest

        form = ModelTestForm()
        assert 'type="color"' in str(form.test_column)
<|endoftext|>


<commit_before>import sys
if '' not in sys.path:
    sys.path.append('')

import logging
import unittest

from pyactors.generator import GeneratorActor
from pyactors.exceptions import EmptyInboxException

_logger = logging.getLogger('test_generator_actors')

class TestActor(GeneratorActor):
    ''' TestActor
    '''
    def __init__(self, iters=10):
        super(TestActor, self).__init__()
        self.result = 0
        self.iters = iters

    def loop(self):
        for i in range(self.iters):
            if self.processing:
                self.result += i
                yield
            else:
                break
        self.stop()

class SenderActor(GeneratorActor):
    ''' Sender Actor
    '''
    def loop(self):
        for actor in self.find(actor_name='Receiver'):
            actor.send('message from sender')
        self.stop()

class ReceiverActor(GeneratorActor):
    ''' ReceiverActor
    '''
    def __init__(self, name=None):
        super(ReceiverActor, self).__init__(name=name)
        self.message = None

    def loop(self):
        while self.processing:
            try:
                self.message = self.inbox.get()
            except EmptyInboxException:
                self._waiting = True
                yield

            if self.message:
                break
        self.stop()

class GeneratorActorTest(unittest.TestCase):

    def test_actors_run(self):
        ''' test_actors_run
        '''
        actor = TestActor()
        actor.start()
        self.assertEqual(actor.processing, True)
        actor.run()
        self.assertEqual(actor.result, 45)
        self.assertEqual(actor.run_once(), False)
        self.assertEqual(actor.processing, False)
        self.assertEqual(actor.waiting, False)

    def test_actors_stop_in_the_middle(self):
        ''' test_actors_stop_in_the_middle
        '''
        actor = TestActor()
        actor.start()
        self.assertEqual(actor.processing, True)
        for _ in range(5):
            actor.run_once()
        actor.stop()
        self.assertEqual(actor.result, 10)
        self.assertEqual(actor.run_once(), False)
        self.assertEqual(actor.processing, False)
        self.assertEqual(actor.waiting, False)

    def test_actors_processing_with_children(self):
        ''' test_actors_processing_with_children
        '''
        parent = GeneratorActor()
        for _ in range(5):
            parent.add_child(TestActor())
        parent.start()
        parent.run()
        self.assertEqual([child.result for child in parent.children], [45,45,45,45,45])
        self.assertEqual(parent.run_once(), False)
        self.assertEqual(parent.processing, False)
        self.assertEqual(parent.waiting, False)

    def test_actors_processing_with_diff_timelife_children(self):
        ''' test_actors_processing_with_diff_timelife_children
        '''
        parent = GeneratorActor()
        for i in range(5):
            parent.add_child(TestActor(iters=i))
        parent.start()
        parent.run()
        self.assertEqual(set([child.result for child in parent.children]), set([0,0,1,3,6]))
        self.assertEqual(parent.run_once(), False)
        self.assertEqual(parent.processing, False)
        self.assertEqual(parent.waiting, False)

    def test_actors_send_msg_between_actors(self):
        ''' test_actors_send_msg_between_actors
        '''
        parent = GeneratorActor()
        parent.add_child(SenderActor(name='Sender'))
        parent.add_child(ReceiverActor(name='Receiver'))
        parent.start()
        parent.run()
        parent.stop()
        self.assertEqual(
                [actor.message for actor in parent.find(actor_name='Receiver')],
                ['message from sender']
        )

<commit_msg>test cases updated<commit_after>import sys
if '' not in sys.path:
    sys.path.append('')

import logging
import unittest

from pyactors.generator import GeneratorActor
from pyactors.exceptions import EmptyInboxException

_logger = logging.getLogger('test_generator_actors')

class TestActor(GeneratorActor):
    ''' TestActor
    '''
    def __init__(self, iters=10):
        super(TestActor, self).__init__()
        self.result = 0
        self.iters = iters

    def loop(self):
        for i in range(self.iters):
            if self.processing:
                self.result += i
                yield
            else:
                break
        self.stop()

class Sender(GeneratorActor):
    ''' Sender
    '''
    def loop(self):
        for actor in self.find(actor_name='Receiver'):
            actor.send('message from sender')
        self.stop()

class Receiver(GeneratorActor):
    ''' Receiver
    '''
    def __init__(self, name=None):
        super(Receiver, self).__init__(name=name)
        self.message = None

    def loop(self):
        while self.processing:
            try:
                self.message = self.inbox.get()
            except EmptyInboxException:
                self._waiting = True
                yield

            if self.message:
                break
        self.stop()

class GeneratorActorTest(unittest.TestCase):

    def test_actors_run(self):
        ''' test_actors_run
        '''
        actor = TestActor()
        actor.start()
        self.assertEqual(actor.processing, True)
        actor.run()
        self.assertEqual(actor.result, 45)
        self.assertEqual(actor.run_once(), False)
        self.assertEqual(actor.processing, False)
        self.assertEqual(actor.waiting, False)

    def test_actors_stop_in_the_middle(self):
        ''' test_actors_stop_in_the_middle
        '''
        actor = TestActor()
        actor.start()
        self.assertEqual(actor.processing, True)
        for _ in range(5):
            actor.run_once()
        actor.stop()
        self.assertEqual(actor.result, 10)
        self.assertEqual(actor.run_once(), False)
        self.assertEqual(actor.processing, False)
        self.assertEqual(actor.waiting, False)

    def test_actors_processing_with_children(self):
        ''' test_actors_processing_with_children
        '''
        parent = GeneratorActor()
        for _ in range(5):
            parent.add_child(TestActor())
        parent.start()
        parent.run()
        self.assertEqual([child.result for child in parent.children], [45,45,45,45,45])
        self.assertEqual(parent.run_once(), False)
        self.assertEqual(parent.processing, False)
        self.assertEqual(parent.waiting, False)

    def test_actors_processing_with_diff_timelife_children(self):
        ''' test_actors_processing_with_diff_timelife_children
        '''
        parent = GeneratorActor()
        for i in range(5):
            parent.add_child(TestActor(iters=i))
        parent.start()
        parent.run()
        self.assertEqual(set([child.result for child in parent.children]), set([0,0,1,3,6]))
        self.assertEqual(parent.run_once(), False)
        self.assertEqual(parent.processing, False)
        self.assertEqual(parent.waiting, False)

    def test_actors_send_msg_between_actors(self):
        ''' test_actors_send_msg_between_actors
        '''
        parent = GeneratorActor()
        parent.add_child(Sender(name='Sender'))
        parent.add_child(Receiver(name='Receiver'))
        parent.start()
        parent.run()
        self.assertEqual(
                [actor.message for actor in parent.find(actor_name='Receiver')],
                ['message from sender']
        )

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django.db import models
import model_settings as config

MAX_LENGTH_20 = config.MAX_LENGTH_20
MAX_LENGTH_100 = config.MAX_LENGTH_100
MAX_LENGTH_200 = config.MAX_LENGTH_200
MAX_LENGTH_1024 = config.MAX_LENGTH_1024

class PaperEduRaw(models.Model):
    '''
        中国科技论文在线

        从中华科技论文在线抓回的原始数据
        cn & en字段:
            title -- 标题
            authors -- 作者
            locations -- 单位
            abstract -- 摘要
            keywords -- 关键字
        其余字段
            pub_date -- 发布时间
            impressions -- 浏览量
            collections -- 收藏数
            comments -- 评论数
            pdf_download -- pdf下载链接
            author_intro -- 作者简介
            contact -- 通信联系人
            paper_edu_pub_record -- 收录情况中国科技论文在线
            pub_periodical -- 发表期刊
            url -- 抓取页面链接
            created_at 记录创建日期
            updated_at 记录更新日期
            raw_html
    '''
    title_cn = models.CharField(
        verbose_name=u'标题_中',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )

    title_en = models.CharField(
        verbose_name=u'标题_英',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    authors_cn = models.CharField(
        verbose_name=u'作者_中',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    authors_en = models.CharField(
        verbose_name=u'作者_英',
        max_length=MAX_LENGTH_1024,
        blank=True,
        null=True,
    )
    locations_cn = models.CharField(
        verbose_name=u'单位_中',
        max_length=MAX_LENGTH_1024,
        blank=True,
        null=True,
    )
    locations_en = models.CharField(
        verbose_name=u'单位_英',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    abstract_cn = models.TextField(
        verbose_name=u'摘要_中',
        blank=True,
        null=True,
    )
    abstract_en = models.TextField(
        verbose_name=u'摘要_英',
        blank=True,
        null=True,
    )
    keywords_cn = models.CharField(
        verbose_name=u'关键字_中',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    keywords_en = models.CharField(
        verbose_name=u'关键字_英',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    pub_date = models.DateField(
        verbose_name=u'发布日期',
        blank=True,
        null=True,
    )
    impressions = models.IntegerField(
        verbose_name=u'浏览量',
        default=0,
    )
    collections = models.IntegerField(
        verbose_name=u'收藏数',
        default=0,
    )
    comments = models.IntegerField(
        verbose_name=u'评论数',
        default=0,
    )
    pdf_download = models.URLField(
        verbose_name=u'pdf下载链接',
        blank=True,
        null=True,
    )
    author_intro = models.CharField(
        verbose_name=u'作者简介',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    contact = models.CharField(
        verbose_name=u'通信联系人',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    paper_edu_pub_record = models.CharField(
        verbose_name=u'中国科技在线收录情况',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    pub_periodical = models.CharField(
        verbose_name=u'发表期刊',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    url = models.URLField(
        verbose_name=u'抓取页面链接',
        unique=True,
    )
    raw_html = models.TextField(
        verbose_name=u'源代码',
        blank=True,
        null=True,
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

class PaperEduKeyWordCN(models.Model):
    '''中文关键字'''
    word = models.CharField(
        verbose_name=u'中文关键词',
        max_length=MAX_LENGTH_100,
        unique=True,
    )
    raw_data = models.ManyToManyField(
        'paper_edu.PaperEduRaw',
        verbose_name=u'原始抓取数据',
    )
    def __unicode__(self):
        return self.word

class PaperEduKeyWordEN(models.Model):
    '''英文关键字'''
    word = models.CharField(
        verbose_name=u'英文关键词',
        max_length=MAX_LENGTH_100,
        unique=True,
    )
    raw_data = models.ManyToManyField(
        'paper_edu.PaperEduRaw',
        verbose_name=u'原始抓取数据',
    )
    cn_word = models.ManyToManyField(
        'paper_edu.PaperEduKeyWordCN',
        verbose_name=u'中文关键字',
    )
    def __unicode__(self):
        return self.word
<commit_msg>contact字段有的页面像写作文<commit_after># -*- coding: utf-8 -*-
from django.db import models
import model_settings as config

MAX_LENGTH_20 = config.MAX_LENGTH_20
MAX_LENGTH_100 = config.MAX_LENGTH_100
MAX_LENGTH_200 = config.MAX_LENGTH_200
MAX_LENGTH_1024 = config.MAX_LENGTH_1024

class PaperEduRaw(models.Model):
    '''
        中国科技论文在线

        从中华科技论文在线抓回的原始数据
        cn & en字段:
            title -- 标题
            authors -- 作者
            locations -- 单位
            abstract -- 摘要
            keywords -- 关键字
        其余字段
            pub_date -- 发布时间
            impressions -- 浏览量
            collections -- 收藏数
            comments -- 评论数
            pdf_download -- pdf下载链接
            author_intro -- 作者简介
            contact -- 通信联系人
            paper_edu_pub_record -- 收录情况中国科技论文在线
            pub_periodical -- 发表期刊
            url -- 抓取页面链接
            created_at 记录创建日期
            updated_at 记录更新日期
            raw_html
    '''
    title_cn = models.CharField(
        verbose_name=u'标题_中',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )

    title_en = models.CharField(
        verbose_name=u'标题_英',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    authors_cn = models.CharField(
        verbose_name=u'作者_中',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    authors_en = models.CharField(
        verbose_name=u'作者_英',
        max_length=MAX_LENGTH_1024,
        blank=True,
        null=True,
    )
    locations_cn = models.CharField(
        verbose_name=u'单位_中',
        max_length=MAX_LENGTH_1024,
        blank=True,
        null=True,
    )
    locations_en = models.CharField(
        verbose_name=u'单位_英',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    abstract_cn = models.TextField(
        verbose_name=u'摘要_中',
        blank=True,
        null=True,
    )
    abstract_en = models.TextField(
        verbose_name=u'摘要_英',
        blank=True,
        null=True,
    )
    keywords_cn = models.CharField(
        verbose_name=u'关键字_中',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    keywords_en = models.CharField(
        verbose_name=u'关键字_英',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    pub_date = models.DateField(
        verbose_name=u'发布日期',
        blank=True,
        null=True,
    )
    impressions = models.IntegerField(
        verbose_name=u'浏览量',
        default=0,
    )
    collections = models.IntegerField(
        verbose_name=u'收藏数',
        default=0,
    )
    comments = models.IntegerField(
        verbose_name=u'评论数',
        default=0,
    )
    pdf_download = models.URLField(
        verbose_name=u'pdf下载链接',
        blank=True,
        null=True,
    )
    author_intro = models.CharField(
        verbose_name=u'作者简介',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    contact = models.CharField(
        verbose_name=u'通信联系人',
        max_length=MAX_LENGTH_1024,
        blank=True,
        null=True,
    )
    paper_edu_pub_record = models.CharField(
        verbose_name=u'中国科技在线收录情况',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    pub_periodical = models.CharField(
        verbose_name=u'发表期刊',
        max_length=MAX_LENGTH_200,
        blank=True,
        null=True,
    )
    url = models.URLField(
        verbose_name=u'抓取页面链接',
        unique=True,
    )
    raw_html = models.TextField(
        verbose_name=u'源代码',
        blank=True,
        null=True,
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

class PaperEduKeyWordCN(models.Model):
    '''中文关键字'''
    word = models.CharField(
        verbose_name=u'中文关键词',
        max_length=MAX_LENGTH_100,
        unique=True,
    )
    raw_data = models.ManyToManyField(
        'paper_edu.PaperEduRaw',
        verbose_name=u'原始抓取数据',
    )
    def __unicode__(self):
        return self.word

class PaperEduKeyWordEN(models.Model):
    '''英文关键字'''
    word = models.CharField(
        verbose_name=u'英文关键词',
        max_length=MAX_LENGTH_100,
        unique=True,
    )
    raw_data = models.ManyToManyField(
        'paper_edu.PaperEduRaw',
        verbose_name=u'原始抓取数据',
    )
    cn_word = models.ManyToManyField(
        'paper_edu.PaperEduKeyWordCN',
        verbose_name=u'中文关键字',
    )
    def __unicode__(self):
        return self.word
<|endoftext|>