def get_oxygen_to_hif(self, degree=1, render=False):
        """
        Returns coefficients for ultra-hypoxia and hypoxia polynomials to be
        used by cancer cells if the warburg
        switch has *NOT* been activated. At present, only hypoxic threshold is
        changed whereas ultra-hypoxic is left as-is.

        Parameters
        ----------
        degree : int, optional
            Degree of the generated polynomial. Optional, defaults to 1.
        render : bool, optional
            If set to true, will display the generated function

        Returns
        -------
        list
            Coefficients of polynomial for ultra-hypoxia
        list
            Coefficients of polynomial for hypoxia
        """
        points = [
            # Points below 0.02
            (0, 0),
            (self.ultra_hypoxia_threshold, self.max_hif),
            # Points to 0.02
            (self.ultra_hypoxia_threshold, self.max_hif),
            (self.hypoxia_threshold, 1),
        ]

        xs = [p[0] for p in points]
        ys = [p[1] for p in points]

        # Fitting points between 0.02 and 0.2
        xsAscending = [xs[0], xs[1]]
        ysAscending = [ys[0], ys[1]]

        pA = Polynomial.fit(xsAscending, ysAscending, degree)

        # Fitting points between 0.02 and 0
        xsDescending = [xs[2], xs[3]]
        ysDescending = [ys[2], ys[3]]

        pD = Polynomial.fit(xsDescending, ysDescending, degree)

        # Getting data to reproduce polynomials, use of sort:
        # p = Polynomial(coef=[list of coeffs], domain=[xstart, xend])

        if render:
            plt.plot(*pA.linspace(), label="Ascending", color="orange")
            plt.plot(*pD.linspace(), label="Descending", color="green")
            plt.scatter(xs, ys, label="Raw Points")
            plt.legend()
            plt.xlabel("Oxygen Saturation (%)")
            plt.ylabel("Relative HIF Expression Rates")
            plt.title("Oxygen Saturation to Relative HIF Expression Rates")
            plt.show()

        return [round(c, 2)
                for c in list(pA.coef)], [round(c, 2) for c in list(pD.coef)]
예제 #2
0
 def video_efficiency(self, usuario):
     
     x_eff_video_1 = array([70.0, 85.0,  100.0])
     y_eff_video_1 = array([75.0, 85.0,  100.0])                 
     pol_eff_video_1 = Polynomial.fit(x_eff_video_1, y_eff_video_1, 2)
     
     x_eff_video_2 = array([100.0, 150.0, 200.0, 250.0, 300.0])
     y_eff_video_2 = array([100.0, 60.0, 30.0, 10.0, 0.0])                       
     pol_eff_video_2 = Polynomial.fit(x_eff_video_2, y_eff_video_2, 2)
     
     subject_videos = GqlQuery("SELECT * FROM ViewerVideo")
     
     videosAbove70 = 0
     videoPoints = 0
     
     for subject_video in subject_videos:
         
         userVideo = GqlQuery("SELECT * FROM UserVideo WHERE video = :1 AND user = :2", subject_video.video, usuario.user).get()
         
         if(userVideo != None):
             porcentajeVideo = (userVideo.seconds_watched/userVideo.duration)*100
             
             if(porcentajeVideo > 70):
                 videosAbove70 += 1
                 if(porcentajeVideo >= 100):
                     videoPoints += pol_eff_video_2(porcentajeVideo)
                 if(porcentajeVideo < 100):
                     videoPoints += pol_eff_video_1(porcentajeVideo)
                     
     if(videosAbove70 != 0):
         videoPoints = int(videoPoints/videosAbove70)
         
     return videoPoints
def correct_minority_voltage(plot_name, minority_voltage):
    """correct for the slope of the voltage curve
    fit a line to the beginning and end of the minority voltage
    curve. then subtract the line from the curve to remove the slope.
    The special selection of the x values is, because we can't fit the peak,
    we can only fit the more or less linear parts of the input curve.
    """
    global coordinate
    xs = numpy.linspace(0, 239, 240)
    xs = numpy.concatenate((xs, numpy.linspace(310, 399, 90)))
    xs = numpy.concatenate((xs, numpy.linspace(1200, len(minority_voltage) - 1, len(minority_voltage) - 1200)))
    selected_minority_voltage = minority_voltage[0:240] + minority_voltage[310:400] + minority_voltage[1200:]

    corrected = []
    fit = Poly.fit(xs, selected_minority_voltage, 1)
    for i, min_v in enumerate(minority_voltage):
        corrected.append(min_v - fit(i))

    # make a plot for visual inspection of the fit quality
    full_xs = numpy.linspace(0, xs[-1], len(minority_voltage))
    plot = fig.add_subplot(y_max, x_max, coordinate)
    plt.title(plot_name)
    plt.plot(full_xs, fit(full_xs), label="fit")
    plt.plot(full_xs, minority_voltage, label="minority voltage")
    plt.plot(full_xs, corrected, label="corrected minority voltage")
    plt.plot(full_xs, numpy.zeros(len(full_xs)), label="zero reference")
    coordinate += 1

    return corrected
def get_knee_level(mask, percentage_mid=0.5, percentage_side=0.15):

    min_x, max_x = get_top_width(mask)
    possible_indices = np.arange(np.floor(min_x), np.ceil(max_x))

    mid = int(len(possible_indices) / 2)

    num_pts_mid = int(len(possible_indices) * percentage_mid)
    num_pts_side = int(len(possible_indices) * percentage_side)
    lower_mid = int(mid - num_pts_mid / 2)
    upper_mid = int(mid + num_pts_mid / 2)

    x_vals = np.concatenate([
        possible_indices[num_pts_side:lower_mid],
        possible_indices[upper_mid:-num_pts_side]
    ])

    y_vals = np.array([get_min_y_per_x(mask, _x) for _x in x_vals])

    line = Polynomial.fit(x_vals,
                          y_vals,
                          1,
                          domain=[x_vals.min(), x_vals.max()])

    return line(possible_indices), possible_indices
예제 #5
0
def lsf(mask, leave_out_percentage=(0.15, 0.15)):

    if isinstance(leave_out_percentage, (float, int)):
        bottom_percentage = top_percentage = leave_out_percentage
    else:
        top_percentage, bottom_percentage = leave_out_percentage

    contour_points = get_contpt(mask)

    if leave_out_percentage:

        num_pts_top = int(len(contour_points[0]) * top_percentage)
        num_pts_bottom = int(len(contour_points[0]) * bottom_percentage)

        indices = np.argsort(contour_points[0])
        contour_points = (contour_points[0][indices],
                          contour_points[1][indices])

        contour_points_filtered = (
            contour_points[0][num_pts_top:-num_pts_bottom],
            contour_points[1][num_pts_top:-num_pts_bottom],
        )
    else:
        contour_points_filtered = contour_points

    line = Polynomial.fit(
        contour_points_filtered[0],
        contour_points_filtered[1],
        1,
        domain=[contour_points[0].min(), contour_points[0].max()],
    )

    return contour_points[0], line(contour_points[0])
예제 #6
0
def read_plot_power_opt():
    #read new data
    power_within_data = pd.read_csv("power_within_x.csv", delimiter=',')
    x_within = power_within_data['Temp']
    y_within = power_within_data['Power']
    z_within = power_within_data['Error']
    golden_data_power = pd.read_csv("golden_power_opt.csv", delimiter=',')
    x_golden = golden_data_power['Temp']
    y_golden = golden_data_power['Power']
    z_golden = golden_data_power['Error']
    print('series', x_golden, y_golden)
    label2 = '   Error:' + str(error)
    label1 = '  INV : ' + str(inv) + '   Header : ' + str(header)
    label = label2 + label1
    plt.annotate(
        label,
        xy=(x_golden, y_golden),
        arrowprops=dict(facecolor='black', shrink=0.05),
    )
    p = Polynomial.fit(x, y, 3)
    #print('p COEFF',p.coef)
    plt.plot(*p.linspace(), 'bo')
    plt.plot(x, y, color='green', linestyle='dashed', marker='o')
    plt.plot(
        search_points(inv, header)[0],
        search_points(inv, header)[1], 'ro')
    plt.show()
예제 #7
0
파일: __timings.py 프로젝트: UCLHp/gate-pbt
def get_time_per_particle(e_vs_pps_file):
    """Estimate the simulation time per primary particle, in water
    at different energies. Return polynomial fit"""
    # Takes E_vs_PPS data

    energies, time_per_prim = [], []
    for line in open(e_vs_pps_file, "r"):
        values = [float(s) for s in line.strip().split()]
        energies.append(values[0])
        time_per_prim.append(1.0 / values[1])

    plt.scatter(energies, time_per_prim)

    p = Polynomial.fit(energies, time_per_prim, 2)
    plt.plot(*p.linspace())

    ################# checking polynomial fit ##################
    time = []
    for en in energies:
        t = -6.2931E-4 + 1.3926E-5 * en + 1.5251E-8 * en**2
        time.append(t)

    plt.scatter(energies, time, s=15)
    ############################################################

    plt.ylabel("Simulation time per primary")
    plt.xlabel("Energy")
    plt.ylim((0, 0.005))

    ###print(p)
    # correct coefficients
    pnormal = p.convert(domain=(-1, 1))
    print("time per particle polynomial params = {}".format(pnormal))
예제 #8
0
def poly(x, y, plot=False):
    p = Polynomial.fit(y, x, 3)
    if plot:
        plt.scatter(x, y)
        plt.plot(*p.linspace(), lw=3, color='r')
        plt.show()
    return p
예제 #9
0
def xi_csd(d, bins=np.linspace(0,15,151), nbg=0., binned=False):
	from scipy import signal
	from numpy.polynomial import Polynomial
	np.random.seed(seed=42)
	if binned==False:
		actcounts, bins = np.histogram(d, bins=bins)
		counts = actcounts + np.random.poisson(nbg, size=len(d))
	else:counts = d + np.random.poisson(nbg, size=len(d))
	counts = np.maximum(counts - nbg, np.zeros_like(counts))
	centroids = (bins[1:] + bins[:-1]) / 2.
	err = np.sqrt(counts+nbg)#*(counts/actcounts)
	bkg=0
	degree=1
	pp=Polynomial.fit(centroids,counts,degree,w=1./numpy.sqrt(counts+1.))
	tdata= counts/pp(centroids)
	terr = err/pp(centroids)
	px, py= signal.csd(tdata,tdata, fs=1./(centroids[1]-centroids[0]),scaling='spectrum',nperseg=len(centroids))
	py= py.real
	px= 1./px
	py= numpy.sqrt(py*(centroids[-1]-centroids[0]))

	nerrsim= 1000
	ppy_err= numpy.empty((nerrsim,len(px)))
	for ii in range(nerrsim):
		tmock= terr*numpy.random.normal(size=len(centroids))
		ppy_err[ii]= signal.csd(tmock,tmock,
								fs=1./(centroids[1]-centroids[0]),scaling='spectrum',
								nperseg=len(centroids))[1].real
	py_err= numpy.sqrt(numpy.median(ppy_err,axis=0)*(centroids[-1]-centroids[0]))
	#np.save('/Users/hendel/Desktop/pscrosscheck_xi_csd.npy',(d,bins,counts,tdata,terr,px,py,py_err))
	return px,py,py_err
    def get_hif_to_metabolic_rate(self, render=False):
        """
        Returns coefficients for hif to metabolic rate. The domain of the
        function is [0, maxHIF].

        Parameters
        ----------
        render : bool, optional
            If set to true, will display the generated function

        Returns
        -------
        list
            Function coefficients
        """
        points = [(0, self.base_oxygen_metabolic_rate), (self.max_hif, 0)]

        xs = [p[0] for p in points]
        ys = [p[1] for p in points]

        p = Polynomial.fit(xs, ys, 2)
        # Getting data to reproduce polynomials, use of sort:
        # p = Polynomial(coef=[list of coeffs], domain=[xstart, xend])

        if render:
            plt.plot(*p.linspace(), label="Fit", color="orange")
            plt.scatter(xs, ys, label="Raw Points")
            plt.legend()
            plt.xlabel("HIF Expression Rates")
            plt.ylabel("Metabolic Rate")
            plt.title("Metabolic Rates across HIF Expression Rates")
            plt.show()

        return [round(c, 2) for c in list(p.coef)]
예제 #11
0
def process_pal5_densdata(options):
    # Read and prep data
    backg= 400.
    data= numpy.loadtxt('data/ibata_fig7b_raw.dat',delimiter=',')
    sindx= numpy.argsort(data[:,0])
    data= data[sindx]
    data_lowerr= numpy.loadtxt('data/ibata_fig7b_rawlowerr.dat',delimiter=',')
    sindx= numpy.argsort(data_lowerr[:,0])
    data_lowerr= data_lowerr[sindx]
    data_uperr= numpy.loadtxt('data/ibata_fig7b_rawuperr.dat',delimiter=',')
    sindx= numpy.argsort(data_uperr[:,0])
    data_uperr= data_uperr[sindx]
    data_err= 0.5*(data_uperr-data_lowerr)
    # CUTS
    indx= (data[:,0] > options.minxi-0.05)*(data[:,0] < options.maxxi)
    data= data[indx]
    data_lowerr= data_lowerr[indx]
    data_uperr= data_uperr[indx]
    data_err= data_err[indx]
    # Compute power spectrum
    tdata= data[:,1]-backg
    pp= Polynomial.fit(data[:,0],tdata,deg=options.polydeg,w=1./data_err[:,1])
    tdata/= pp(data[:,0])
    ll= data[:,0]
    py= signal.csd(tdata,tdata,fs=1./(ll[1]-ll[0]),scaling='spectrum',
                   nperseg=len(ll))[1]
    py= py.real
    # Also compute the bispectrum
    Bspec, Bpx= bispectrum.bispectrum(numpy.vstack((tdata,tdata)).T,
                                      nfft=len(tdata),wind=7,nsamp=1,overlap=0)
    ppyr= numpy.fabs(Bspec[len(Bspec)//2+_BISPECIND,len(Bspec)//2:].real)
    ppyi= numpy.fabs(Bspec[len(Bspec)//2+_BISPECIND,len(Bspec)//2:].imag)
    return (numpy.sqrt(py*(ll[-1]-ll[0])),data_err[:,1]/pp(data[:,0]),
            ppyr,ppyi)
예제 #12
0
파일: func_misc.py 프로젝트: dlebars/SLProj
def tglob_ar6(sce, start_date, ye):
    '''Provides a few time series of temperature consistent with the AR6 
    temperature assessment.
    Assumes normal distribution which is not the case in AR6. Needs to be 
    revised later.
    Export a data array.'''

    if sce == 'ssp585_hpp':
        # hpp scenario from AR6 uses the same temperature as
        sce = 'ssp585'

    N = 50  # Number of time series to generate
    ar6_temp_df = get_ar6_temp()
    df = ar6_temp_df[sce].copy()
    # Assuming a normal distribution (it is not!) the standard devation is:
    df['sigma'] = (df['95pc'] - df['5pc']) / (2 * 1.64)

    NormD = np.random.normal(0, 1, N)
    years = np.arange(start_date, ye + 1)
    paths = np.zeros([N, len(years)])

    for i in range(N):
        p = P.fit(df.index, df['mean'] + NormD[i] * df['sigma'], 3)
        paths[i, :] = p(years)

    ds = xr.DataArray(paths,
                      coords=[np.arange(N), years],
                      dims=['model', 'time'])

    return ds
예제 #13
0
def process_pal5_densdata(options):
    # Read and prep data
    backg = 400.0
    data = numpy.loadtxt("data/ibata_fig7b_raw.dat", delimiter=",")
    sindx = numpy.argsort(data[:, 0])
    data = data[sindx]
    data_lowerr = numpy.loadtxt("data/ibata_fig7b_rawlowerr.dat", delimiter=",")
    sindx = numpy.argsort(data_lowerr[:, 0])
    data_lowerr = data_lowerr[sindx]
    data_uperr = numpy.loadtxt("data/ibata_fig7b_rawuperr.dat", delimiter=",")
    sindx = numpy.argsort(data_uperr[:, 0])
    data_uperr = data_uperr[sindx]
    data_err = 0.5 * (data_uperr - data_lowerr)
    # CUTS
    indx = (data[:, 0] > options.minxi - 0.05) * (data[:, 0] < options.maxxi)
    data = data[indx]
    data_lowerr = data_lowerr[indx]
    data_uperr = data_uperr[indx]
    data_err = data_err[indx]
    # Compute power spectrum
    tdata = data[:, 1] - backg
    pp = Polynomial.fit(data[:, 0], tdata, deg=options.polydeg, w=1.0 / data_err[:, 1])
    tdata /= pp(data[:, 0])
    ll = data[:, 0]
    py = signal.csd(tdata, tdata, fs=1.0 / (ll[1] - ll[0]), scaling="spectrum", nperseg=len(ll))[1]
    py = py.real
    # Also compute the bispectrum
    Bspec, Bpx = bispectrum.bispectrum(numpy.vstack((tdata, tdata)).T, nfft=len(tdata), wind=7, nsamp=1, overlap=0)
    ppyr = numpy.fabs(Bspec[len(Bspec) // 2 + _BISPECIND, len(Bspec) // 2 :].real)
    ppyi = numpy.fabs(Bspec[len(Bspec) // 2 + _BISPECIND, len(Bspec) // 2 :].imag)
    return (numpy.sqrt(py * (ll[-1] - ll[0])), data_err[:, 1] / pp(data[:, 0]), ppyr, ppyi)
예제 #14
0
def process_mock_densdata(options):
    print ("Using mock Pal 5 data from %s" % options.mockfilename)
    # Read and prep data for mocks
    xvid = numpy.loadtxt(options.mockfilename)
    xv = xvid[:, :6]
    xv = xv[numpy.argsort(xvid[:, 6])]
    XYZ = bovy_coords.galcenrect_to_XYZ(xv[:, 0], xv[:, 1], xv[:, 2], Xsun=R0, Zsun=0.025)
    lbd = bovy_coords.XYZ_to_lbd(XYZ[0], XYZ[1], XYZ[2], degree=True)
    radec = bovy_coords.lb_to_radec(lbd[:, 0], lbd[:, 1], degree=True)
    xieta = pal5_util.radec_to_pal5xieta(radec[:, 0], radec[:, 1], degree=True)
    # make sure the progenitor is at (0,0)
    xieta[:, 0] -= numpy.median(xieta[:, 0])
    xieta[:, 1] -= numpy.median(xieta[:, 1])
    h, e = numpy.histogram(xieta[:, 0], range=[0.2, 14.3], bins=141)
    xdata = numpy.arange(0.25, 14.35, 0.1)
    # Compute power spectrum
    tdata = h - 0.0
    pp = Polynomial.fit(xdata, tdata, deg=options.polydeg, w=1.0 / numpy.sqrt(h + 1.0))
    tdata /= pp(xdata)
    ll = xdata
    py = signal.csd(tdata, tdata, fs=1.0 / (ll[1] - ll[0]), scaling="spectrum", nperseg=len(ll))[1]
    py = py.real
    # Also compute the bispectrum
    Bspec, Bpx = bispectrum.bispectrum(numpy.vstack((tdata, tdata)).T, nfft=len(tdata), wind=7, nsamp=1, overlap=0)
    ppyr = numpy.fabs(Bspec[len(Bspec) // 2 + _BISPECIND, len(Bspec) // 2 :].real)
    ppyi = numpy.fabs(Bspec[len(Bspec) // 2 + _BISPECIND, len(Bspec) // 2 :].imag)
    return (numpy.sqrt(py * (ll[-1] - ll[0])), numpy.sqrt(h + 1.0) / pp(xdata), ppyr, ppyi)
예제 #15
0
def read_plot_power_opt():
    #read new data
    power_within_data = pd.read_csv("power_within_x.csv", delimiter=',')
    x_within= power_within_data['Temp']
    y_within = power_within_data['Power']
    z_within =  power_within_data['Error']
    golden_data_power = pd.read_csv("golden_power_opt.csv", delimiter=',')
    x_golden = golden_data_power['Temp']
    y_golden = golden_data_power['Power']
    z_golden =  golden_data_power['Error']
    print('series', x_golden, y_golden)
    plt.figure(figsize=(12,6))
    label2 = '   Error:' + str(error)
    label1 = '  INV : ' + str(inv) + '   Header : ' + str(header)
    label = label2+label1
    plt.annotate(label, xy=(x_golden, y_golden),arrowprops=dict(facecolor='yellow', shrink=0.05),)
    p = Polynomial.fit(x, y, 3)
    #print('p COEFF',p.coef)
    plt.plot(*p.linspace(), 'bo', label='polynomial')
    plt.plot(x, y, 'o')
    plt.xlabel('Temperature')
    plt.ylabel('Power')
    plt.title('Temp Sensor Power Optimization')
    plt.plot(search_points(inv, header)[0], search_points(inv, header)[1], 'ro', label='Other simulated points using the same design parameters')
    plt.legend(loc='upper left', frameon=True)
    plt.show()
예제 #16
0
    def computeDistribution(self, d):
        """
		Function computing the discrete distribution (histogram) of each 
		latent state over the 24h in one hour bin
		"""
        """
		Iterate through hours and compute histogram:
		"""
        count = np.zeros((len(np.arange(18)), 2), dtype=float)
        i = 0
        for t in np.arange(24):
            if t in self.recoveryHours:
                count[i, 0] = t
                count[i, 1] = len(np.where(d[:, d.shape[1] - 1] == t)[0])
                i += 1

        idx_recHours = []
        for i in self.recoveryHours:
            i = float(i)
            idx_recHours.append(np.where(count[:, 0] == i)[0][0])
        count = count[idx_recHours, :]
        """
		Fit curve on histogram : Polynomial fit
		"""
        p = Polynomial.fit(self.recoveryInd, count[:, 1], self.polydeg)
        pConv = p.convert(domain=[-1, 1])

        return count, p
def calc_level_tibia2(mask, contour_pts, lsf, vert_tol=0.025, horiz_tol=0.25):
    vert_tol = vert_tol * (contour_pts[0].max() - contour_pts[0].min())

    intersec = None

    contour_zipped = list(zip(*contour_pts))

    for pt in zip(*lsf):

        if (int(pt[0]), int(pt[1])) in contour_zipped:
            intersec = pt

    # transform horizontal tolerance from relative to absolute
    y_level_pts = np.nonzero(mask[int(intersec[0])])[0]
    horiz_tol = horiz_tol * (y_level_pts.max() - y_level_pts.min())

    valid_pt = ([], [])
    min_y, max_y = intersec[0] - vert_tol, intersec[0] + vert_tol
    min_x, max_x = intersec[1] - horiz_tol, intersec[1] + horiz_tol

    for pt in zip(*contour_pts):
        if min_y <= pt[0] <= max_y and min_x <= pt[1] <= max_x:
            valid_pt[0].append(pt[0])
            valid_pt[1].append(pt[1])

    line = Polynomial.fit(valid_pt[1],
                          valid_pt[0],
                          1,
                          domain=[contour_pts[1].min(), contour_pts[1].max()])

    _, indices_level = get_contpt(mask[int(np.mean(contour_pts[0])):])
    indices_level = np.arange(indices_level.min(), indices_level.max())
    return line(indices_level), indices_level
def parabola_example():
    # print the type
    print("PARABOLA:")
    img=read_image('parabola.png')

    edged=get_all_edge_points(img)

    # manually entered:
    points=np.array([[165, 143], [183, 92], [221, 22], [183, 442], [161, 155]])

    a,b,c,d,e,f=get_conics_coefficients(points)

    # display the coeffienct
    print_coefficents(a,b,c,d,e,f)

    discriminant=b**2-4*a*c

    print("discriminant:",discriminant)

    # should be 0
    assert   abs(discriminant)<EPLISION_DISCRMENANT

    x=np.array([p[0] for p in edged ])
    y=[p[1] for p in edged ]
    p = Polynomial.fit(x, y, 2)
    y_new=p(x)
    plt.scatter(y_new,x,color='black')
    plt.show()
예제 #19
0
def process_mock_densdata(options):
    print("Using mock Pal 5 data from %s" % options.mockfilename)
    # Read and prep data for mocks
    xvid= numpy.loadtxt(options.mockfilename)
    xv= xvid[:,:6]
    xv= xv[numpy.argsort(xvid[:,6])]
    XYZ= bovy_coords.galcenrect_to_XYZ(xv[:,0],xv[:,1],xv[:,2],
                                       Xsun=R0,Zsun=0.025)
    lbd= bovy_coords.XYZ_to_lbd(XYZ[0],XYZ[1],XYZ[2],degree=True)
    radec= bovy_coords.lb_to_radec(lbd[:,0],lbd[:,1],degree=True)
    xieta= pal5_util.radec_to_pal5xieta(radec[:,0],radec[:,1],degree=True)
    # make sure the progenitor is at (0,0)
    xieta[:,0]-= numpy.median(xieta[:,0])
    xieta[:,1]-= numpy.median(xieta[:,1])
    h,e= numpy.histogram(xieta[:,0],range=[0.2,14.3],bins=141)
    xdata= numpy.arange(0.25,14.35,0.1)
    # Compute power spectrum
    tdata= h-0.
    pp= Polynomial.fit(xdata,tdata,deg=options.polydeg,w=1./numpy.sqrt(h+1.))
    tdata/= pp(xdata)
    ll= xdata
    py= signal.csd(tdata,tdata,fs=1./(ll[1]-ll[0]),scaling='spectrum',
                   nperseg=len(ll))[1]
    py= py.real
    # Also compute the bispectrum
    Bspec, Bpx= bispectrum.bispectrum(numpy.vstack((tdata,tdata)).T,
                                      nfft=len(tdata),wind=7,nsamp=1,overlap=0)
    ppyr= numpy.fabs(Bspec[len(Bspec)//2+_BISPECIND,len(Bspec)//2:].real)
    ppyi= numpy.fabs(Bspec[len(Bspec)//2+_BISPECIND,len(Bspec)//2:].imag)
    return (numpy.sqrt(py*(ll[-1]-ll[0])),numpy.sqrt(h+1.)/pp(xdata),
            ppyr,ppyi)
def calc_level_tibia1(contour_pts, percentage_mid=0.5, percentage_side=0.15):
    min_x, max_x = top_width(contour_pts)
    possible_indices = np.arange(np.floor(min_x), np.ceil(max_x))

    mid = int(len(possible_indices) / 2)

    num_pts_mid = int(len(possible_indices) * percentage_mid)
    num_pts_side = int(len(possible_indices) * percentage_side)
    lower_mid = int(mid - num_pts_mid / 2)
    upper_mid = int(mid + num_pts_mid / 2)

    x_vals = np.concatenate([
        possible_indices[num_pts_side:lower_mid],
        possible_indices[upper_mid:-num_pts_side],
    ])

    y_vals = np.array([get_min_y_per_x(contour_pts, _x) for _x in x_vals])

    # actual fitting
    line = Polynomial.fit(x_vals,
                          y_vals,
                          1,
                          domain=[x_vals.min(), x_vals.max()])

    return line(possible_indices), possible_indices
예제 #21
0
    def fit_polynomial(data, deg=2):

        poly = Polynomial.fit(data['alphas'], data['chi2'], deg)
        if np.isnan(np.min(poly.coef)):
            print 'RankWarning in fit'
            return None
        return poly
예제 #22
0
def optimal_portfolio(returns1):
    n = len(returns1)
    returns1 = np.asmatrix(returns1)

    N = 100
    mus = [10**(5.0 * t/N - 1.0) for t in range(N)]

    # Convert to cvxopt matrices
    S = opt.matrix(np.cov(returns1))
    pbar = opt.matrix(np.mean(returns1, axis=1))

    # Create constraint matrices
    G = -opt.matrix(np.eye(n))  # negative n x n identity matrix
    h = opt.matrix(0.0, (n, 1))
    A = opt.matrix(1.0, (1, n))
    b = opt.matrix(1.0)

    # Calculate efficient frontier weights using quadratic programming
    portfolios = [solvers.qp(mu * S, -pbar, G, h, A, b)['x']
                  for mu in mus]
    # CALCULATE RISKS AND RETURNS FOR FRONTIER
    returns1 = [blas.dot(pbar, x) for x in portfolios]
    risks1 = [np.sqrt(blas.dot(x, S * x)) for x in portfolios]
    # CALCULATE THE 2ND DEGREE POLYNOMIAL OF THE FRONTIER CURVE
    m1 = np.polyfit(returns1, risks1, 2)
    p = P.fit(returns1, risks1, 2)
    #x1 = np.sqrt(m1[2] / m1[0])
    # CALCULATE THE OPTIMAL PORTFOLIO
    #wt = solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x']
    #return np.asarray(wt), returns1, risks1
    return returns1, risks1
def plot_dens_data(filename=None, backg=400., color='k',zorder=10,marker='o',
                   poly_deg=1,minxi=0.25,
                   errsim_color='k',errsim_zorder=0,
                   err_color=0.7,err_zorder=0,err=False):
    """Plots the power spectrum of the data"""
    # Read the data
    if filename == None:
        data= numpy.loadtxt('data/ibata_fig7b_raw.dat',delimiter=',')
        data_lowerr= numpy.loadtxt('data/ibata_fig7b_rawlowerr.dat',delimiter=',')
        data_uperr= numpy.loadtxt('data/ibata_fig7b_rawuperr.dat',delimiter=',')
    else:
        data= numpy.loadtxt('data/fakeobs/'+filename,delimiter=',')
        data_lowerr= numpy.loadtxt('data/fakeobs/'+filename+'_lower',delimiter=',')
        data_uperr= numpy.loadtxt('data/fakeobs/'+filename+'_upper',delimiter=',')
    sindx= numpy.argsort(data[:,0])
    data= data[sindx]
    sindx= numpy.argsort(data_lowerr[:,0])
    data_lowerr= data_lowerr[sindx]
    sindx= numpy.argsort(data_uperr[:,0])
    data_uperr= data_uperr[sindx]
    data_err= 0.5*(data_uperr-data_lowerr)
    # CUTS
    if filename == None: indx= (data[:,0] > minxi-0.05)*(data[:,0] < 14.35)
    else: indx= (data[:,0] >0.)*(data[:,0] <14.4)
    data= data[indx]
    data_lowerr= data_lowerr[indx]
    data_uperr= data_uperr[indx]
    data_err= data_err[indx]
    # Compute power spectrum
    tdata= data[:,1]-backg
    pp= Polynomial.fit(data[:,0],tdata,deg=poly_deg,w=1./data_err[:,1])
    tdata/= pp(data[:,0])
    data_err= data_err[:,1]/pp(data[:,0])
    ll= data[:,0]
    px, py= signal.csd(tdata,tdata,
                        fs=1./(ll[1]-ll[0]),scaling=scaling,
                        nperseg=len(ll))
    py= py.real
    px= 1./px
    py= numpy.sqrt(py*(ll[-1]-ll[0]))
    # Perform simulations of the noise to determine the power in the noise
    nerrsim= 1000
    ppy_err= numpy.empty((nerrsim,len(px)))
    for ii in range(nerrsim):
        tmock= data_err*numpy.random.normal(size=len(ll))
        ppy_err[ii]= signal.csd(tmock,tmock,
                                fs=1./(ll[1]-ll[0]),scaling=scaling,
                                nperseg=len(ll))[1].real
    py_err= numpy.sqrt(numpy.median(ppy_err,axis=0)*(ll[-1]-ll[0]))
    pcut= np.nanmedian(ppy_err) # Only trust points above this, then remove noise
    #loglog(px[py>pcut],numpy.sqrt(py[py>pcut]**2.-py_err[py>pcut]**2.),
    #       marker=marker,color=color,zorder=zorder,ls='none')
    yvals = numpy.sqrt(py**2.-py_err**2.)
    loglog(px,yvals,marker=marker,color=color,zorder=zorder,ls='none',label=filename)
    errorbar(px[np.isnan(yvals)],py_err[np.isnan(yvals)],
             yerr=numpy.array([.03+0.*px[np.isnan(yvals)],.03+0.*px[np.isnan(yvals)]]),
             uplims=True,capthick=2.,ls='none',color=color,zorder=zorder)
    loglog(px,py_err,lw=2.,color=errsim_color,zorder=errsim_zorder)
    return None
def process_mock_densdata(options):
    print("Using mock Pal 5 data from %s" % options.mockfilename)
    simn = 2
    dat = np.loadtxt(
        '/Users/hendel/projects/streamgaps/streampepper/data/fakeobs/' +
        options.mockfilename,
        delimiter=',')
    #fix seed for testing
    if 0:
        print('warning: Poisson seed fixed')
        np.random.seed(42)
    h = dat[simn] + np.random.poisson(options.nbg, size=len(dat[simn]))
    h = np.maximum(h - options.nbg, np.zeros_like(h))
    #h,e= np.histogram(xieta[:,0],range=[0.2,14.3],bins=141)
    bins = np.linspace(options.ximin, options.ximax, options.nxi)
    xdata = (bins[1:] + bins[:-1]) / 2.
    # Compute power spectrum
    tdata = h - 0.
    pp = Polynomial.fit(xdata,
                        tdata,
                        deg=options.polydeg,
                        w=1. / np.sqrt(h + 1.))
    tdata = tdata / pp(xdata)
    ll = xdata
    px, py = signal.csd(tdata,
                        tdata,
                        fs=1. / (ll[1] - ll[0]),
                        scaling='spectrum',
                        nperseg=len(ll))
    px = 1. / px
    py = py.real
    py = np.sqrt(py * (ll[-1] - ll[0]))

    #get ps error level
    nerrsim = 1000
    ppy_err = np.empty((nerrsim, len(px)))
    terr = np.sqrt(h + 1. + options.nbg) / pp(xdata)
    for ii in range(nerrsim):
        tmock = terr * np.random.normal(size=len(ll))
        ppy_err[ii] = signal.csd(tmock,
                                 tmock,
                                 fs=1. / (ll[1] - ll[0]),
                                 scaling='spectrum',
                                 nperseg=len(ll))[1].real
    py_err = np.sqrt(np.median(ppy_err, axis=0) * (ll[-1] - ll[0]))

    np.save('/Users/hendel/Desktop/pscrosscheck_abc.npy',
            (dat[simn], bins, h, tdata, terr, px, py, py_err))

    # Also compute the bispectrum
    Bspec, Bpx = bispectrum.bispectrum(np.vstack((tdata, tdata)).T,
                                       nfft=len(tdata),
                                       wind=7,
                                       nsamp=1,
                                       overlap=0)
    ppyr = np.fabs(Bspec[len(Bspec) // 2 + _BISPECIND, len(Bspec) // 2:].real)
    ppyi = np.fabs(Bspec[len(Bspec) // 2 + _BISPECIND, len(Bspec) // 2:].imag)
    return (py, terr, ppyr, ppyi, py_err)
def hyperbola_example():
    # print the type
    print("HYPERBOLA:")
    img=read_image('hyperbola.png')

    edged=get_all_edge_points(img)



    selected_points=np.array([
        [116,170],
        [141,267],
        [137,442],
        [310,399],
        [355,123]
            ])

    a,b,c,d,e,f=get_conics_coefficients(selected_points)

    # display the coeffienct
    print_coefficents(a,b,c,d,e,f)

    discriminant=b**2-4*a*c
    print("discriminant:",discriminant)

    # represents an hyperbola
    assert discriminant>0 and abs(discriminant)>EPLISION_DISCRMENANT

    # fit the data to the hypberbola
    x=np.array([p[0] for p in edged if p[1]<=200])
    y=[p[1] for p in edged if p[1]<=200]
    p = Polynomial.fit(x, y, 2)
    y_new=p(x)
    plt.scatter(y_new,x,color='black')

    x=np.array([p[0] for p in edged if p[1]>200])
    y=[p[1] for p in edged if p[1]>200]
    p = Polynomial.fit(x, y, 2)
    y_new=p(x)
    plt.scatter(y_new,x,color='black')


    plt.show()
예제 #26
0
    def __init__(self, relation, degree):
        N = 100
        xs = np.linspace(min(relation.x), max(relation.x), N)
        polynomial = Polynomial.fit(relation.x, relation.y, deg=degree)
        ys = polynomial(xs)
        Curve.__init__(self, x=xs, y=ys)

        self.connectPoints = True
        self.label = self.latexPolynomial(polynomial.coef)
        self.degree = degree
        self.N = N
예제 #27
0
    def check_stabilization(self):
        """ Check for the stabilization of the focus
        """
        if self._autofocus_iterations > 10:
            p = Poly.fit(np.linspace(0, 9, num=10), self._last_pid_output_values, deg=1)
            slope = p(9) - p(0)
            if np.absolute(slope) < 10:
                self._autofocus_stable = True
            else:
                self._autofocus_stable = False

        return self._autofocus_stable
예제 #28
0
def displayer(plotlist3, title):
    plotlist3 = relative(plotlist3)
    plt.figure(figsize=(12, 4))
    y = plotlist3
    x = [(x / len(plotlist3)) * 100 for x in range(len(plotlist3))]
    plt.plot(x, y, alpha=.5)
    plt.title(title)
    plt.ylabel("Sentiment of dialog relative to mean")
    plt.xlabel("Percent of Movie")
    p = Polynomial.fit(x, y, 30)
    plt.plot(*p.linspace())
    plt.show()
예제 #29
0
 def get_minmax_point(self, values):
     find_min = self.type == 'min' or self.type == 'tmin'
     find_arg = self.type == 'tmin' or self.type == 'tmax'
     pos = np.argmin(values) if find_min else np.argmax(values)
     if pos == 0 or pos == len(values) - 1:
         return pos if find_arg else values[pos]
     poly = Polynomial.fit([pos - 1, pos, pos + 1],
                           [values[pos - 1], values[pos], values[pos + 1]],
                           deg=2)
     s = poly.deriv()
     x = s.roots()[0]
     return x if find_arg else poly(x)
예제 #30
0
    def plot(self):
        hfont = {'fontname':'Sans'}
        figure,axes = plt.subplots(4,1,sharex=False,figsize=(6,10))
        refline=np.ones(list(self.countrydata.values())[0].relgrowthrate.size)*self.declinelevel
        #axes[0].plot(refline,linewidth=3,color='g')
        axes[0].plot(self.relgrowthrate,label="Relative increrase")
        axes[0].plot(self.relgrowthratefive,label='5-day average')
        axes[1].plot(self.active,label='Active cases')
        
        #Do linear fit
        time=np.arange(-self.recovery_time+1,1)
        poly=Polynomial.fit(time, self.growth[-self.recovery_time:],1)
        k=poly.coef[1]
        efficiency=(poly(time)[0]/poly(time)[-1])

        axes[2].plot(time,self.growth[-self.recovery_time:],label='Growth')
        axes[2].plot(time,poly(time),label='Trend, eff=%s' %('{0:.2f}'.format(efficiency)))
        axes[3].plot(time,self.relgrowthrate[-self.recovery_time:],label='Relative increase')
        axes[3].plot(time,self.relgrowthratefive[-self.recovery_time:],label='5-day average')
        axes[1].axvline(self.active.shape[0]-self.recovery_time,linestyle='dashed', color='c',label='Recovery limit')
        axes[0].set_xlabel('Days since Jan 20, 2020', **hfont,fontsize=18);
        axes[0].set_ylabel('Relative increase', **hfont,fontsize=18);
        axes[1].set_xlabel('Days since Jan 20, 2020', **hfont,fontsize=18);
        axes[1].set_ylabel('Active cases\n past %s d' %(self.recovery_time), **hfont,fontsize=18);
        axes[2].set_xlabel('Active period', **hfont,fontsize=18);
        axes[2].set_ylabel('Daily growth\n past %s d' %(self.recovery_time), **hfont,fontsize=18);
        axes[3].set_xlabel('Active period', **hfont,fontsize=18);
        axes[3].set_ylabel('Relative increase\n past %s d' %(self.recovery_time), **hfont,fontsize=18);
        axes[0].legend()
        axes[1].legend()
        axes[2].legend()
        axes[3].legend()
        axes[0].set_xlim(0,self.active.size-1)
        axes[0].set_ylim(0,1)
        axes[1].set_xlim(0,self.active.size-1)
        axes[2].set_xlim(-self.recovery_time+1,0)
        axes[3].set_xlim(-self.recovery_time+1,0)
        axes[3].set_ylim(0,0.4)
        axes[0].grid(True)
        axes[1].grid(True)
        axes[2].grid(True)
        axes[3].grid(True)
        titlestr = self.punchline %(self.name)
        #plt.subplots_adjust(top=0.8,hspace=0.4)
        plt.subplots_adjust(hspace=0.4)
        plt.subplots_adjust(left=0.2)
        plt.suptitle(titlestr,fontsize=20);
        plt.grid(True);
        printstr=self.figurepath+"/Covid19_in_%s.%s" %(self.name,self.figtype)
        plt.show(block=False);
        figure.savefig(printstr, format=self.figtype, dpi=300);
        plt.show(block=False);
예제 #31
0
    def infer(self) -> Tuple[Polynomial, str]:
        best = None
        complexity = None
        best_resid = np.inf

        linear, (resid, *_) = Polynomial.fit(self.x, self.y, 1, full=True)
        if resid < best_resid:
            best = linear
            if linear.coef[1] > self.epsilon:
                complexity = 'n'
            else:
                complexity = '1'

        # m, b = np.polyfit(np.log2(self.x), self.y, 1)
        # yp = np.polyval([m, b], np.log2(self.x))
        # plt.plot(self.x, yp, label=f'O(nlogn) [{m:.2f}log(x) + {b:.2f}]')

        quadradic, (resid, *_) = Polynomial.fit(self.x, self.y, 2, full=True)
        if resid < best_resid and quadradic.coef[2] > self.epsilon:
            best = quadradic
            complexity = 'n²'

        return best, complexity
예제 #32
0
def calc_curve_tangent_slope(x, y, degree=3, unit='radians'):
    poly = Polynomial.fit(x, y, degree)
    c = poly.convert().coef
    c_deriv = [i * cd for i, cd in enumerate(c)]
    c_deriv = c[1:]
    f_deriv = Polynomial(c_deriv)
    tan_slope = f_deriv(x)

    slope = np.arctan(tan_slope)

    if unit == 'degree':
        slope = slope / np.pi * 180.

    return slope
예제 #33
0
 def __init__(self, addr, max_power, power_recv=None):
     Sensor.__init__(self, addr)
     power_expected = array([max_power*i/100 for i in xrange(0, 110, 10)])
     if power_recv is None:
         power_recv = power_expected[:]
     self._poly = Polynomial.fit(power_recv, power_expected, 2)
     #self.least_squares(power_recv, power_expected)
     self._conv = interp1d(*self._poly.linspace())
     self._min_recv = min(power_recv)
     self._max_recv = max(power_recv)
     self._min_pow = min(power_expected)
     self._max_pow = max(power_expected)
     self.recv_read = 0
     self.SEND_DELAY = 2
예제 #34
0
파일: WIAG.py 프로젝트: Zhang-xie/WiGr
def fit_config(x):
    res = Polynomial.fit(np.arange(len(x)), x, 8)
    a0, a1, a2 = res.coef[:3]
    v = np.random.rand()

    def equations(vars):
        D, THETA, k = vars
        return (
            k * v / D**2 - a0,
            a0 * (2 * (v / D) * np.cos(THETA)) - a1,
            -a0 * ((v / D)**2 * (1 - 4 * np.cos(THETA)**2)) - a2,
        )

    D, THETA, k = fsolve(equations, (1, 1, 1))
    return D, THETA, k
예제 #35
0
 def __init__(self, addr, max_power, power_recv=None):
     Sensor.__init__(self, addr)
     power_expected = array(
         [max_power * i / 100 for i in xrange(0, 110, 10)])
     if power_recv is None:
         power_recv = power_expected[:]
     self._poly = Polynomial.fit(power_recv, power_expected, 2)
     #self.least_squares(power_recv, power_expected)
     self._conv = interp1d(*self._poly.linspace())
     self._min_recv = min(power_recv)
     self._max_recv = max(power_recv)
     self._min_pow = min(power_expected)
     self._max_pow = max(power_expected)
     self.recv_read = 0
     self.SEND_DELAY = 2
예제 #36
0
 def exercise_progress(self, usuario):
     
     x_vid = array([0.0, 25.0, 50.0, 75.0, 100.0])
     y_vid = array([0.0, 40.0, 65.0, 85.0, 100.0])
     
     pol_exercise_progress = Polynomial.fit(x_vid, y_vid, 5)
     
     subject_exercises = GqlQuery("SELECT * FROM ViewerExercise")
     
     exercise_progress = 0
     
     for subject_exercise in subject_exercises:
         userExercise = GqlQuery("SELECT * FROM UserExercise WHERE exercise_model = :1 AND user = :2", subject_exercise.exercise, usuario.user).get()
         
         if(userExercise != None):
             if(userExercise._progress == 1.0):
                 exercise_progress += 100
             else:
                 exercise_progress += pol_exercise_progress(userExercise._progress*100)
                 
     return int(exercise_progress/subject_exercises.count())
예제 #37
0
 def video_progress(self, usuario):
     
     x_vid = array([0.0, 50.0, 75.0, 100.0])
     y_vid = array([0.0, 30.0, 55.0, 100.0])
     
     pol_video_progress = Polynomial.fit(x_vid, y_vid, 5)
     
     total_video_progress = 0
     
     subject_videos = GqlQuery("SELECT * FROM ViewerVideo")
     
     for subject_video in subject_videos:
             
         userVideo = GqlQuery("SELECT * FROM UserVideo WHERE video = :1 AND user = :2", subject_video.video, usuario.user).get()
         
         if(userVideo != None):
             porcentajeVideo = (userVideo.seconds_watched/userVideo.duration)*100
             if(porcentajeVideo >= 100):
                 total_video_progress += 100
             else:
                 total_video_progress += pol_video_progress(porcentajeVideo)
                     
     return int(total_video_progress/subject_videos.count())
예제 #38
0
points = np.array([
(0.4,	0.587),
(1,	0.967),
(5,	5.01),
(10,	10.09),
(15,	15.16),
(20, 20.23),
(25,	25.32),
(30,	30.37),
(35,	35.4),
(40,	40.5),
(45,	45.6),
(50,	50.6),
(51,	51.6),
(52,	52.6),
(53,	53.7),
(54,	54.7),
(55,	54.8),
(56,	54.8)
])

# get x and y vectors
x = points[:,0]
y = points[:,1]

p = Polynomial.fit(x, y, 8)

plt.plot(x,y,'o')
plt.plot(*p.linspace())
plt.show()
예제 #39
0
def getCafeExptime(swasp_id, vmag):
    V=np.arange(8,14.0,0.5)
    t=np.array([30,120,300,900,2700])

    snr=defaultdict(list)
    snr[8.]   = np.array([23.381,46.762,73.937,128.063,221.812]) 
    snr[8.5]  = np.array([18.572,37.144,58.731,101.724,176.192])
    snr[9.]   = np.array([15.306,30.612,48.402,83.835,145.206])
    snr[9.5]  = np.array([12.158,24.316,38.447,66.592,115.341])
    snr[10.]  = np.array([9.657,19.315,30.540,52.896,91.619])
    snr[10.5] = np.array([7.671,15.342,24.258,42.017,72.776])
    snr[11.]  = np.array([6.093,12.817,19.269,33.375,57.807])
    snr[11.5] = np.array([4.840,9.680,15.306,26.511,45.918])
    snr[12.]  = np.array([3.844,7.689,12.158,21.058,36.474])
    snr[12.5] = np.array([3.05,6.108,9.657,16.727,28.972])
    snr[13.]  = np.array([2.426,4.852,7.671,13.287,23.014])
    snr[13.5] = np.array([1.923,3.854,6.093,10.554,18.280])

    coeffs_store=defaultdict(list)
    for i in sorted(snr.keys()):
        coeffs=np.polyfit(t,snr[i],2)
        coeffs_store[i]=coeffs
        tn=np.arange(30,3060,60)
        besty=np.polyval(coeffs,tn)
    diff=V-vmag
    n=np.where(abs(diff)==min(abs(diff)))[0][0]
    p=P.fit(t,snr[V[n]],2)
    t1,t2=(p-args.snr).roots()
    if t1<t2:
        predicted=t1.real
    else:
        predicted=t2.real
    print('Predicted exptime {0:.2f}'.format(predicted))
    # round to the nearest 60
    predicted = int(math.ceil(predicted/60))*60
    print('Rounding up to nearest 60s {0:d}'.format(predicted))
    # check for texp>2700, scale to right number of
    # spectra to get required SNR
    if predicted > 2700:
        print('Predicted time > 2700s, looking for good combo of spectra')
        snr_max = snr[V[n]][-1]
        print('SNR_max @ 2700 = {0:.2f}'.format(snr_max))
        n_spectra = (args.snr/snr[V[n]][-1])**2
        print('This needs {0:.2f} spectra @ 2700'. format(n_spectra))
        n_spectra = math.ceil(n_spectra)
        print('Rounding up to next integer n_spectra = {0:d}'.format(n_spectra))
        # now work out the best exptime to use to combine 
        # n_spectra spectra to get the desired args.snr
        target_single_spectra_snr = args.snr/math.sqrt(n_spectra)
        print('Working out the target SNR per spectra...')
        print('To achive SNR_total = {0:d} we need {1:d} spectra of SNR_i = {2:.2f}'.format(args.snr, n_spectra, target_single_spectra_snr))
        snr_range = np.polyval(coeffs_store[V[n]], tn)
        diff_ss_snr = abs(snr_range - target_single_spectra_snr)
        loc_ss_snr = np.where(diff_ss_snr == min(diff_ss_snr))[0][0]
        predicted = tn[loc_ss_snr]
        print('The best exposure time for this combination of spectra is {0:d} x {1:d}s'.format(n_spectra, predicted))
    elif predicted < 0:
        predicted = 60
        n_spectra=1
    else:
        n_spectra=1
    print("{0:s} {1:.2f} {2:d} x {3:.2f}s".format(swasp_id,vmag,n_spectra,predicted))
    return n_spectra, predicted
예제 #40
0
def polfit_residuals(
        x, y, deg, reject=None,
        color='b', size=75,
        xlim=None, ylim=None,
        xlabel=None, ylabel=None, title=None,
        use_r=False,
        debugplot=0):
    """Polynomial fit with display of residuals and additional work with R.

    Parameters
    ----------
    x : 1d numpy array, float
        X coordinates of the data being fitted.
    y : 1d numpy array, float
        Y coordinates of the data being fitted.
    deg : int
        Degree of the fitting polynomial.
    reject : None or 1d numpy array (bool)
        If not None, it must be a boolean array indicating whether a
        particular point is rejected or not (i.e., the rejected points
        are flagged as True in this array). Rejected points are
        displayed but not used in the fit.
    color : single character or 1d numpy array of characters
        Color for all the symbols (single character) or for each
        individual symbol (array of color names with the same length as
        'x' or 'y'). If 'color' is a single character, the rejected
        points are displayed in red color, whereas when 'color' is an
        array of color names, rejected points are displayed with the
        color provided in this array.
    size : int
        Marker size for all the symbols (single character) or for each
        individual symbol (array of integers with the same length as
        'x' or 'y').
    xlim : tuple (floats)
        Plot limits in the X axis.
    ylim : tuple (floats)
        Plot limits in the Y axis.
    xlabel : string
        Character string for label in X axis.
    ylabel : string
        Character string for label in y axis.
    title : string
        Character string for graph title.
    use_r : bool
        If True, the function computes several fits, using R, to
        polynomials of degree deg, deg+1 and deg+2 (when possible).
    debugplot : int
        Determines whether intermediate computations and/or plots
        are displayed:
        00 : no debug, no plots
        01 : no debug, plots without pauses
        02 : no debug, plots with pauses
        10 : debug, no plots
        11 : debug, plots without pauses
        12 : debug, plots with pauses

    Return
    ------
    poly : instance of Polynomial (numpy)
        Result from the polynomial fit using numpy Polynomial. Only
        points not flagged as rejected are employed in the fit.
    yres : 1d numpy array, float
        Residuals from polynomial fit. Note that the residuals are
        computed for all the points, including the rejected ones. In
        this way the dimension of this array is the same as the
        dimensions of the input 'x' and 'y' arrays.

    """

    # protections
    if type(x) is not np.ndarray:
        raise ValueError("x=" + str(x) + " must be a numpy.ndarray")
    elif x.ndim != 1:
        raise ValueError("x.ndim=" + str(x.ndim) + " must be 1")
    if type(y) is not np.ndarray:
        raise ValueError("y=" + str(y) + " must be a numpy.ndarray")
    elif y.ndim != 1:
        raise ValueError("y.ndim=" + str(y.ndim) + " must be 1")
    npoints = x.size
    if npoints != y.size:
        raise ValueError("x.size != y.size")
    if reject is not None:
        if npoints != reject.size:
            raise ValueError("x.size != reject.size")
    if type(deg) not in [np.int, np.int64]:
        raise ValueError("deg=" + str(deg) +
                         " is not a valid integer")

    # select points for fit
    if reject is None:
        xfitted = np.copy(x)
        yfitted = np.copy(y)
        xrejected = None
        yrejected = None
        nfitted = npoints
        nrejected = 0
    else:
        xfitted = x[np.logical_not(reject)]
        yfitted = y[np.logical_not(reject)]
        xrejected = x[reject]
        yrejected = y[reject]
        # update number of points for fit
        nfitted = xfitted.size
        nrejected = sum(reject)

    if deg > nfitted - 1:
        raise ValueError("Insufficient nfitted=" + str(nfitted) +
                         " for deg=" + str(deg))

    # polynomial fits using R
    if use_r:
        from ..rutilities import LinearModelYvsX
        print("\n>>> Total number of points:", nfitted)
        # using orthogonal polynomials
        for delta_deg in [2, 1, 0]:
            deg_eff = deg + delta_deg
            if deg_eff <= nfitted - 1:
                myfit = LinearModelYvsX(x=xfitted, y=yfitted, degree=deg_eff,
                                        raw=False)
                print(">>> Fit with R, using orthogonal polynomials:")
                print(myfit.summary)
                pause_debugplot(debugplot)
        # fit using raw polynomials
        myfit = LinearModelYvsX(x=xfitted, y=yfitted, degree=deg, raw=True)
        print(">>> Fit with R, using raw polynomials:")
        print(myfit.summary)
        pause_debugplot(debugplot)

    # fit with requested degree (and raw polynomials)
    poly = Polynomial.fit(x=xfitted, y=yfitted, deg=deg)
    poly = Polynomial.cast(poly)

    # compute residuals
    yres = y - poly(x)  # of all the points
    yres_fitted = yfitted - poly(xfitted)  # points employed in the fit
    yres_rejected = None
    if nrejected > 0:
        yres_rejected = yrejected - poly(xrejected)  # points rejected

    if debugplot >= 10:
        print(">>> Polynomial fit:\n", poly)

    if debugplot % 10 != 0:
        # define colors, markers and sizes for symbols
        if np.array(color).size == 1:
            mycolor = np.array([color] * npoints)
            if reject is not None:
                mycolor[reject] = 'r'
        elif np.array(color).size == npoints:
            mycolor = np.copy(np.array(color))
        elif np.array(color).shape[0] == npoints:  # assume rgb color
            mycolor = np.copy(np.array(color))
        else:
            raise ValueError("color=" + str(color) +
                             " doesn't have the expected dimension")
        if np.array(size).size == 1:
            mysize = np.repeat([size], npoints)
        elif np.array(size).size == npoints:
            mysize = np.copy(np.array(size))
        else:
            raise ValueError("size=" + str(size) +
                             " doesn't have the expected dimension")

        if reject is None:
            cfitted = np.copy(mycolor)
            crejected = None
            sfitted = np.copy(mysize)
            srejected = None
        else:
            cfitted = mycolor[np.logical_not(reject)]
            crejected = mycolor[reject]
            sfitted = mysize[np.logical_not(reject)]
            srejected = mysize[reject]

        import matplotlib
        matplotlib.use('Qt4Agg')
        import matplotlib.pyplot as plt
        fig = plt.figure()

        # residuals
        ax2 = fig.add_subplot(2, 1, 2)
        if xlabel is None:
            ax2.set_xlabel('x')
        else:
            ax2.set_xlabel(xlabel)
        ax2.set_ylabel('residuals')
        if xlim is None:
            xmin = min(x)
            xmax = max(x)
            dx = xmax - xmin
            xmin -= dx/20
            xmax += dx/20
        else:
            xmin, xmax = xlim
        ax2.set_xlim([xmin, xmax])
        ymin = min(yres_fitted)
        ymax = max(yres_fitted)
        dy = ymax - ymin
        ymin -= dy/20
        ymax += dy/20
        ax2.set_ylim([ymin, ymax])
        ax2.axhline(y=0.0, color="black", linestyle="dashed")
        ax2.scatter(xfitted, yres_fitted, color=cfitted,
                    marker='o',
                    edgecolor='k', s=sfitted)
        if nrejected > 0:
            ax2.scatter(xrejected, yres_rejected,
                        marker='x', s=srejected,
                        color=crejected)

        # original data and polynomial fit
        ax = fig.add_subplot(2, 1, 1, sharex=ax2)
        if ylabel is None:
            ax.set_ylabel('y')
        else:
            ax.set_ylabel(ylabel)
        ax.set_xlim([xmin, xmax])
        if ylim is None:
            ymin = min(y)
            ymax = max(y)
            dy = ymax - ymin
            ymin -= dy/20
            ymax += dy/20
        else:
            ymin, ymax = ylim
        ax.set_ylim([ymin, ymax])
        ax.scatter(xfitted, yfitted,
                   color=cfitted, marker='o', edgecolor='k',
                   s=sfitted, label="fitted data")
        xpol = np.linspace(start=xmin, stop=xmax, num=1000)
        ypol = poly(xpol)
        ax.plot(xpol, ypol, 'c-', label="fit")
        if nrejected > 0:
            ax.scatter(xrejected, yrejected,
                       marker='x', s=srejected, color=crejected,
                       label="rejected")

        # shrink axes and put a legend
        box = ax2.get_position()
        ax2.set_position([box.x0, box.y0,
                          box.width, box.height * 0.92])
        delta_ybox = box.height*0.15
        box = ax.get_position()
        ax.set_position([box.x0, box.y0 - delta_ybox,
                         box.width, box.height * 0.92])
        ax.legend(loc=3, bbox_to_anchor=(0.0, 1.1, 1., 0.07),
                  mode="expand", borderaxespad=0., ncol=4,
                  numpoints=1)

        # graph title
        if title is not None:
            plt.title(title + "\n\n")

        plt.show(block=False)
        plt.pause(0.001)
        pause_debugplot(debugplot)

    # return result
    return poly, yres
A = [3.9, 4.0, 4.1, 4.2]
E = []
for a in A:
    name = 'bulk-fcc-%.1f' % a
    b = a / 2

    bulk = Atoms('Al',
                 cell=[[0, b, b],
                       [b, 0, b],
                       [b, b, 0]],
                 pbc=True)

    k = 4
    calc = GPAW(mode=PW(300),       # cutoff
                kpts=(k, k, k),     # k-points
                txt=name + '.txt')  # output file

    bulk.set_calculator(calc)

    energy = bulk.get_potential_energy()
    calc.write(name + '.gpw')
    E.append(energy)

p = Polynomial.fit(A, E, 3)
a0 = p.deriv(1).roots()[0]
B = p.deriv(2)(a0) * 4 / 9 / a0 / u.J * u.m**3 * 1e-9  # GPa
print((a0, B))

assert abs(a0 - 3.9924) < 0.001
assert abs(B - 87.22) < 0.1