Example #1
0
def makepolyfit(nosedist, nosetail, degree=DEGREE):
    """ Fit the midline to a polynomial.
    
    args:
        nosedist (dict): dict of numpy arrays of distance from nose to tail
        
        nosetail (dict): dict of numpy arrays of coordinates along midline
    
    kwargs:
        degree (int): degree of polynomial fit
    
    returns:
        dict: dict of coefficients, each of which is m X 2 array for m degrees
    
    """
    coefs = {}
    for k, v in nosetail.iteritems():
        u = nosedist[k]
        try:
            # First, smooth the data
            x, y = zip(*v)
            x = fullmovingavg(x, span=5)
            y = fullmovingavg(y, span=5)

            # Then fit to requested type of polynomial
            cx = polyfit(u, x, degree)
            cy = polyfit(u, y, degree)

            # Return coefficients, converted to regular polynomial basis
            coefs[k] = (cx, cy)
        except:
            pass
    return coefs
Example #2
0
def fit_min_max(args,p,max_iter,proj_list,proj_axis):
	mins = numpy.array([])
	maxs = numpy.array([])
	
	kind = [item for item in args.kind.split(' ')]
	
	for i in xrange(int(args.fmin)+int(args.step),max_iter+1,int(args.step)):
		args.proj = proj_list[p]
		axis = proj_axis[args.proj]
		dat = load_map(args,p,i)
		unit_l, unit_d, unit_t, unit_m = load_units(i, args)

		if kind[p] == 'dens':
			dat *= unit_d	# in g/cc
		if kind[p] in ['vx','vy','vz']:
			dat *= (unit_l/unit_t)/1e5 # in km/s
		if kind[p] in ['stars','dm']:
			dat += 1e-12
		
		if args.logscale:
			mins = numpy.append(mins,numpy.log10(numpy.amin(dat)))
			maxs = numpy.append(maxs,numpy.log10(numpy.amax(dat)))
		else:
			mins = numpy.append(mins,numpy.amin(dat))
			maxs = numpy.append(maxs,numpy.amax(dat))
		
	ii = range(int(args.fmin)+int(args.step),max_iter+1,int(args.step))
	cmin = polyfit(ii,mins,args.poly)	
	cmax = polyfit(ii,maxs,args.poly)

	return p, cmin, cmax
Example #3
0
def smoothenData(data, smoothLength, polyOrder, channelsN):
    """ Function that smooths the data with polynomial and downsamples it.
    
    :param data: list of syllables with samples
    :param smoothLength: sampling points to downsample data to
    :param polyOrder: Order of the polynomial to smooth data with
    :param channelsN: Number of mel frequency channels in data    
    
    :returns newData: smoothend and downsampled data
    """

    newData = []
    for syllable in data:
        newSyllable = []
        for sample in syllable:
            newSample = np.zeros((smoothLength, channelsN))
            size = sample.shape[0]
            xVals = np.arange(1, size + 1)
            interpolCoords = np.linspace(1, size, smoothLength)
            polycoeff = poly.polyfit(list(range(size)), sample, polyOrder)
            sampleSmooth = poly.polyval(list(range(size)), polycoeff)
            for channel_i in range(channelsN):
                f = sp.interpolate.interp1d(xVals, sampleSmooth[channel_i])
                newSample[:, channel_i] = f(interpolCoords)
            newSyllable.append(newSample)
        newData.append(newSyllable)
    return newData
    def work(self, fig=None, ax=None):
        """Draw the polynomial fit on matplotlib figure or axis

        Parameters:
        -----------
        fig: matplotlib figure
        ax: matplotlib axis

        Returns:
        --------
        a tuple with figure and axis objects
        """
        if ax is None:
            if fig is None:
                return fig, ax
            else:
                ax = fig.gca()
        from numpy.polynomial.polynomial import polyfit
        from numpy.polynomial.polynomial import polyval
        x = self.data[self.aes['x']]
        y = self.data[self.aes['y']]
        min_x = min(x)
        max_x = max(x)
        c = polyfit(x, y, self.degree)
        x_ = np.linspace(min_x, max_x, len(x))
        y_ = polyval(x_, c)
        ax.plot(x_, y_, lw=self.lw, c=self.colour)
        return fig, ax
Example #5
0
def get_immigration2(S, starting_age, ending_age, E):
    '''
    Parameters:
        S - Number of age cohorts
        starting age - initial age of cohorts

    Returns:
        im_array - S x 1 array of immigration rates for each
                   age cohort
        child_imm_rate - starting_age x 1 array of immigration
            rates for children
    '''
    imm_rate_condensed1 = get_immigration1(
        S, starting_age, ending_age, pop_2010, pop_2011, E)
    imm_rate_condensed2 = get_immigration1(
        S, starting_age, ending_age, pop_2011, pop_2012, E)
    imm_rate_condensed3 = get_immigration1(
        S, starting_age, ending_age, pop_2012, pop_2013, E)
    im_array = (
        imm_rate_condensed1 + imm_rate_condensed2 + imm_rate_condensed3) / 3.0
    poly_imm = poly.polyfit(np.linspace(
        1, ending_age, ending_age-1), im_array[:-1], deg=18)
    poly_imm_int = poly.polyint(poly_imm)
    child_imm_rate = poly.polyval(np.linspace(
        0, starting_age, E+1), poly_imm_int)
    imm_rate = poly.polyval(np.linspace(
        starting_age, ending_age, S+1), poly_imm_int)
    child_imm_rate = np.diff(child_imm_rate)
    imm_rate = np.diff(imm_rate)
    imm_rate[-1] = 0.0
    return imm_rate, child_imm_rate
Example #6
0
def fit(xcoords_fit, ycoords_fit, dat_fit): #does fit heavy lifting
    x_fit = np.linspace(xcoords_fit[0], xcoords_fit[-1], 50)
    np.concatenate([xcoords_fit, x_fit])
    dat_fit = int(dat_fit)
    coefs = ply.polyfit(xcoords_fit, ycoords_fit, dat_fit, w=np.divide(1, yerrors_no_ul))
    y_fit = ply.polyval(x_fit, coefs)
    return (x_fit, y_fit, coefs)
Example #7
0
def get_fert(S, starting_age, ending_age, E):
    '''
    Parameters:
        S - Number of age cohorts
        starting age - initial age of cohorts

    Returns:
        fert_rate - Sx1 array of fertility rates for each
            age cohort
        children_fertrate  - starting_age x 1 array of zeros, to be
            used in get_omega()
    '''
    # Fit a polynomial to the fertility rates
    poly_fert = poly.polyfit(age_midpoint, fert_data, deg=4)
    fert_rate = integrate(poly_fert, np.linspace(
        starting_age, ending_age, S+1))
    fert_rate /= 2.0
    children_fertrate_int = poly.polyint(poly_fert)
    children_fertrate_int = poly.polyval(np.linspace(
        0, starting_age, E + 1), children_fertrate_int)
    children_fertrate = np.diff(children_fertrate_int)
    children_fertrate /= 2.0
    children_fertrate[children_fertrate < 0] = 0
    children_fertrate[:int(10*S/float(ending_age-starting_age))] = 0
    return fert_rate, children_fertrate
Example #8
0
def polynomial(ny, nx, order=2, nz=None):    
    coeffs = poly.polyfit(nx,ny,order)
    if nz is not None:
        continuum = poly.polyval(nz, coeffs)
    else:
        continuum = poly.polyval(nx, coeffs)
    return continuum
Example #9
0
def poly_interp(xi,yi):
    """
    General polynomial interpolation. 

    Compute the coefficients of the polynomial
    interpolating the points (xi[i],yi[i]) for i = 0,1,2,...,n-1
    where n = len(xi) = len(yi).

    Returns c, an array containing the coefficients of
      p(x) = c[0] + c[1]*x + c[2]*x**2 + ... + c[N-1]*x**(N-1).

    """

    # check inputs and print error message if not valid:

    error_message = "xi and yi should have type numpy.ndarray"
    assert (type(xi) is np.ndarray) and (type(yi) is np.ndarray), error_message

    error_message = "xi and yi should have the same length "
    assert len(xi)==len(yi), error_message

    # Using poly.polyfit with no need to reorder the coefs
    n = len(xi)
    c = poly.polyfit(xi, yi, n-1)

    return c
def solve_polyfit(xarr, yarr, degree, weight, deriv=None):
    # Obtain solution using polyfit
    z = P.polyfit(xarr, yarr, deg=degree, w=weight)

    if deriv is not None:
        z = P.polyder(z, m=deriv)
    return z
Example #11
0
def fit_polynomial(data, ln_xray_property, deg, whatIsFit):
    """Fit a DEG-order polynomial in x, y space.

    numpy.polynomial.polinomial.polyfit() returns coefficients,
    from 0th order first to N-th order last (note that this is
    *opposite* from how np.polyfit behaves!).
    """
    radiuspackage = extrapolate_radius(data)

    r = radiuspackage[0]
    ln_r = radiuspackage[1]
    r_fine = radiuspackage[2]
    ln_r_fine = radiuspackage[4]

    print("Now fitting    |" + "  " + make_number_ordinal(deg) +
          " order polynomial to " + whatIsFit)

    coeffs = poly.polyfit(ln_r, ln_xray_property, deg)

    # polyval() is used to assemble cubic fit:
    # $p(x) = c_0 + c_1 x + c_2 x^2 + c3 x^3$
    # where c_n are the coeffs returned by polyfit()
    ln_fit = poly.polyval(ln_r, coeffs)
    fit = np.exp(ln_fit)

    # Now use these coefficients to extrapolate fit
    # across larger radius

    ln_fit_fine = poly.polyval(ln_r_fine, coeffs)
    fit_fine = np.exp(ln_fit_fine)

    fitpackage = (fit, r, fit_fine, r_fine, coeffs)

    return fitpackage
Example #12
0
 def applyScale(self,scale):
     # extract coordinates
     if "coordinates" in scale:
         coordinates = scale["coordinates"]
     else:
         coordinates = []
     # extract degree
     if "degree" in scale:
         polyfit_degree = scale["degree"]
     else:
         polyfit_degree = 1
     # compute polyfit
     if len(coordinates) > polyfit_degree:
         xv = np.array([e[0] for e in coordinates])
         yv = np.array([e[1] for e in coordinates])
         c,_ = poly.polyfit(xv,yv,polyfit_degree,full=True)
         coefficients = list(c)
         coefficients.reverse()
         # apply polyfit          
         self.beginResetModel()
         for i in range(len(self.coordinates)):
             x = self.coordinates[i][0]
             newT = np.poly1d(coefficients)([x])[0]
             self.coordinates[i][1] = int(round(newT))
         self.endResetModel()
         # update Polyfit and trigger the redraw of the matplotlib graph canvas
         self.computePolyfit()
Example #13
0
def plot_fit_line(ax, x, y):
    from numpy.polynomial.polynomial import polyfit
    t = np.arange(100)
    xy = [(a, b) for (a, b) in zip(x, y) if np.isfinite(a) and np.isfinite(b)]
    x, y = zip(*xy)
    b, m = polyfit(x, y, 1)
    print("y = {} + {} * x".format(b, m))
    ax.plot(t, b + m * t, '-', lw=3, color='k')
 def _fit_poly(self, dataset):
     '''
     Do a lsq fit of an (deg)th degree polynomial to the dataset data and return a
     numpy polynomial object 
     '''
     xs, ys, _ = dataset.data.T
     coefs = polyfit(xs, ys, self.deg)
     return coefs
Example #15
0
def fit_spring_data(datafile):
    import numpy as np
    import numpy.polynomial.polynomial as pf
    data = np.loadtxt(datafile)
    x = data[0]
    y = data[1]
    fit = pf.polyfit(x,y,1)
    return abs(fit[1])
Example #16
0
def residuals(td,ant_s11_file,amp_s_file,Kt,sh,freq,data):
    Eff_sm = efficiency_test(td,ant_s11_file,amp_s_file)
    mean_d = Kt*(data/Eff_sm(freq)-sh)
    minfreq = where(freq<=60)[0][-1]
    maxfreq = where(freq<=90)[0][-1]
    fit_d = poly.polyfit(log10(freq[minfreq:maxfreq]/70.),log10(mean_d[minfreq:maxfreq]),1)
    fit_val = 10**(poly.polyval(log10(freq/70.),fit_d))
    return mean_d[minfreq:maxfreq] - fit_val[minfreq:maxfreq]
Example #17
0
 def residual(par, model, lines):
     lines.meta['refwave'], lines.meta['par'] = allpar(par)
     wguess = lines.fit(np.arange(arc['f'].shape[-1]).reshape(1,1,-1),
                        arc['x'].reshape(-1,1,1))
     mguess = np.interp(wguess, model['w'], model['f'])
     pfit1, extra = poly.polyfit(mguess.flatten(), arc['f'].flatten(),
                                 1, full=True)
     # print('par={}, chi2={}'.format(par, extra[0]))
     aguess = pfit1[0] + pfit1[1]*mguess
     return (arc['f'] - aguess).flatten()
Example #18
0
  def take_turn(self):
    if len(self.xs) < 9000:
      return float(random.randrange(200))

    # do curve fitting
    # in the form A + Bx + Cx^2
    coeffs = poly.polyfit(self.xs, self.ys, 2) # fit a polynomial of degree 2
    func = poly.Polynomial(coeffs) # create the polynomial
    m = opt.minimize(-func, 100)
    return m.x[0]
def gridsearchplots(inputpath, outputpath, eqcount, stcount, n=20 ):

    gridresults = np.genfromtxt(outputpath + 'Source-effects/grid-search/gridsearch-results.txt',
                                delimiter=',')
    earthquakes = np.genfromtxt(inputpath + 'earth.csv',
                                delimiter=',')
    freqs = np.genfromtxt(outputpath + 'Interpolating/freqs.txt',
                          delimiter=',')
    momentrate = np.genfromtxt(outputpath +
                                'Source-effects/momentrates.txt', delimiter=',')
    momentratepstd = np.genfromtxt(outputpath +
                                'Source-effects/momentratespstd.txt', delimiter=',')
    momentratenstd = np.genfromtxt(outputpath +
                                'Source-effects/momentratesnstd.txt', delimiter=',')
    magnitudes = np.genfromtxt(outputpath +
                               'Source-effects/magnitudesextracted.txt', delimiter=',')
    momentratecalculated = np.zeros([n, eqcount])
    #print gridresults
    #print freqs[2]
    momentratecalculated = [(10 ** (1.5 * (gridresults[z, 4] + 10.7)) / (1 + (f / gridresults[z, 7]) ** gridresults[z, 2])) for z in range(int(eqcount)) for f in np.linspace(0.4, 15, 1000)]
    #print np.shape(momentratecalculated)
    counter = 0
    for i in range(int(eqcount) / 4 + 1):
        if counter == eqcount:
            break
        fig = plt.figure(i)
        for j in range(4):
            if counter == eqcount:
                break
            ax1 = fig.add_subplot(2, 2, j + 1)
            if j == 0 or j == 2:
                ax1.yaxis.set_label_text(r'Moment rate spectrum $(dyne \times Cm)$', size=12,
                                         family='freeserif', color='#000099', )
            ax1 = fig.add_subplot(2, 2, j + 1)
            ax1.xaxis.grid(which='both', ls='-', lw=0.3, color='#c0c0c0', alpha=0.1, zorder=0)
            ax1.yaxis.grid(which='both', ls='-', lw=0.3, color='#c0c0c0', alpha=0.1, zorder=1)
            ax1.set_title(str(int(earthquakes[counter, 1])) + '/' + str(int(earthquakes[counter, 2])) + '/' +
                          str(int(earthquakes[counter, 3])))
            ax1.loglog(freqs, momentrate[:, counter], lw=2, ls='-', color='#FF8000')
            ax1.loglog(freqs, momentratepstd[:, counter], lw=2, ls='--',
                       zorder=2, color='#193300')
            ax1.loglog(freqs, momentratenstd[:, counter], lw=2, ls='--',
                       zorder=2, color='#193300')
            fit = nppoly.polyfit(np.log(freqs), np.log(momentrate[:, counter]), 1)
            ax1.loglog(freqs, np.exp(nppoly.polyval(np.log(freqs), fit)),
               alpha=0.5, color='#994c00', linewidth=2)
            ax1.loglog(np.linspace(0.4, 15, 1000), momentratecalculated[counter*1000: counter*1000 + 1000],
               alpha=0.5, color='#994c00', linewidth=2)
            counter += 1
            # ax1.set_ylim([10**20, 10**26])
            ax1.set_xlim([0, 100])
        plt.tight_layout()
        fig.savefig(outputpath + 'Source-effects/grid-search/source-plot'
                    + str(i) + '.pdf', format='pdf', dpi=200)
        plt.close(fig)
def pathcalculations(outputpath, n=20):
    result = np.genfromtxt(outputpath + 'svd/answer.txt',
                           delimiter=',')
    covd = np.genfromtxt(outputpath + 'svd/Covariance/covd.txt',
                         defaultfmt='.4e', delimiter=',')
    freqs = np.genfromtxt(outputpath + 'Interpolating/freqs.txt',
                          delimiter=',')
    error = np.zeros(n)
    qfactor2 = result[:n]
    for j in range(n):
        s = [np.random.normal(result[j], np.sqrt(covd[j,j])) for x in range(10000)]
        s = np.asarray(s)
        s = 1/s
        error[j] = np.std(s)
    qfactor = 1.0 / qfactor2

    np.savetxt(outputpath + 'Path-effect/q.txt', qfactor,
               fmt='%0.5f', delimiter=',')
    np.savetxt(outputpath + 'Path-effect/covdiagonal.txt', np.sqrt(np.diag(covd)[:]),
               fmt='%0.5f', delimiter=',')
    fit = nppoly.polyfit(np.log(freqs), np.log(qfactor), 1)

    aa = np.exp(fit[0])

    # ----------------------------- plotting -------------------

    fig = plt.figure(1)
    fig.suptitle('Q-factor', fontsize=14, fontweight='bold', family='freeserif')
    ax1 = fig.add_subplot(111)
    ax1.xaxis.grid(which='both', ls='-', lw=0.3, color='#c0c0c0', alpha=0.1, zorder=0)
    ax1.yaxis.grid(which='both', ls='-', lw=0.3, color='#c0c0c0', alpha=0.1, zorder=1)
    ax1.errorbar(freqs, qfactor, error )
    ax1.loglog(freqs, qfactor, color='#202020', linewidth=2,
               linestyle='--', label='Calculated Qs', zorder=4)
    ax1.loglog(freqs, np.exp(nppoly.polyval(np.log(freqs), fit)),
               alpha=0.5, color='#994c00', linewidth=2,
               label=r'Fitted line  $Q_{s}=%0.2f \times f^{%0.1f}$' % (aa, fit[1]), zorder=3)
    # --------------- Plot Properties ------------------
    ax1.xaxis.set_label_text('Frequency(Hz)', size=12, family='freeserif', color='#000099')
    ax1.yaxis.set_label_text('Q-factor(Qs)', size=12, family='freeserif', color='#000099')

    ax1.get_xaxis().tick_bottom()
    ax1.get_yaxis().tick_left()

    # ax1.spines['top'].set_visible(False)
    # ax1.spines['right'].set_visible(False)

    ax1.set_xlim([0.2, 70])
    ax1.set_ylim([20, 2000])
    ax1.legend(loc='upper left', frameon=False, fancybox=True, shadow=True,
               prop={'size': 14, 'family': 'freeserif'})
    # ------------------- End --------------------------
    fig.savefig(outputpath + 'Path-effect/Quality-factor.pdf', format='pdf', dpi=1000)
    plt.close(fig)
    print '%0.2ff^%0.2f' % (np.exp(fit[0]), fit[1])
Example #21
0
def poly_fit(fit_me, xi, xf, weights,order=10, *args, **kwargs):
  # Fit returns coefficients, highest-order first
  pfit = polyfit(xi, fit_me, deg=order,w=weights)

  # Generate poly function
  pfunc = np.poly1d(pfit)
  
  # Evaluate at 'xf'
  fitted = pfunc(xf)
  residual = pfunc(xi) - fit_me
  return (fitted, residual)
Example #22
0
def FindPResistFromIV(I, V, lower_range, upper_range):
    # finds the dynamic R for a given range of the IV curve by fitting for the slope of V vs I.
    #  Most likely use of this is to find the residual resistance (normal) below the transition.
    #
    if len(I) or len(V) < 2:
        PResist = 0
    else:
        coefs = poly.polyfit(V[lower_range:upper_range], I[lower_range:upper_range], 1)
        PResist = 1/coefs[1]  # coefs[1] is the slope = dI/dV, so 1/coefs[1] is dV/dI = R (dynamic)
        
    return PResist
Example #23
0
def FindPResistFromIV(Current, Voltage, lower_range, upper_range):
    # finds the dynamic R for a given range of the IV curve by fitting for the slope of V vs I.
    #  Most likely use of this is to find the residual resistance (normal) below the transition.
    #
    if len(Current[lower_range:upper_range]) or len(Voltage[lower_range:upper_range]) < 2:
        PResist = 0
    else:
        coefs = poly.polyfit(Voltage[lower_range:upper_range], Current[lower_range:upper_range], 1)
        PResist = 1/coefs[1]
        
    return PResist
def iqi(f, xs, tol = 0.1, nMax = 100):
	""" function, initial X values, tolerance, max iterations """
	start = time.time()
	n = 0
	while (abs(f(xs[0])) > tol) & (n < nMax):
		p = nppoly.polyfit(xs, map(f, xs), 3)
		guess = map(abs, nppoly.polyroots(p))[1]
		xs.pop()
		xs.insert(0, guess)
		n += 1
	# zero, iterct, runtime
	return xs[1], n, time.time()-start
Example #25
0
  def final_report(self):
    coeffs = poly.polyfit(self.xs, self.ys, 2) # fit a polynomial of degree 2

    xs = range(0,200)
    ffit = poly.polyval(xs, coeffs)
    plt.figure()
    plt.plot(xs, ffit)
    plt.savefig(self.name + '.png', bbox_inches='tight')
 
    plt.figure()
    plt.plot(self.xs, self.ys, '.')
    plt.savefig(self.name + 'scatter.png', bbox_inches='tight')
Example #26
0
 def test_polyfit(self) :
     def f(x) :
         return x*(x - 1)*(x - 2)
     # Test exceptions
     assert_raises(ValueError, poly.polyfit, [1],    [1],     -1)
     assert_raises(TypeError,  poly.polyfit, [[1]],  [1],      0)
     assert_raises(TypeError,  poly.polyfit, [],     [1],      0)
     assert_raises(TypeError,  poly.polyfit, [1],    [[[1]]],  0)
     assert_raises(TypeError,  poly.polyfit, [1, 2], [1],      0)
     assert_raises(TypeError,  poly.polyfit, [1],    [1, 2],   0)
     # Test fit
     x = np.linspace(0,2)
     y = f(x)
     coef = poly.polyfit(x, y, 3)
     assert_equal(len(coef), 4)
     assert_almost_equal(poly.polyval(x, coef), y)
     coef = poly.polyfit(x, y, 4)
     assert_equal(len(coef), 5)
     assert_almost_equal(poly.polyval(x, coef), y)
     coef2d = poly.polyfit(x, np.array([y,y]).T, 4)
     assert_almost_equal(coef2d, np.array([coef,coef]).T)
Example #27
0
def unidentified_lines(wv_master, xpos_arc, solution, poly_degree_wfit,
                       nlines_arc, nlines_master, times_sigma_inclusion):
    # ---
    # Include unidentified lines by using the prediction of the polynomial fit
    # to the current set of identified lines. The included lines are labelled
    # as type='I'.

    nfit, ifit, xfit, yfit, wfit = select_data_for_fit(wv_master, xpos_arc,
                                                       solution)
    coeff_fit = polynomial.polyfit(xfit, yfit, poly_degree_wfit, w=1 / wfit)
    poly = polynomial.Polynomial(coeff_fit)
    rfit = abs(yfit - poly(xfit))
    sigma_rfit = sigmaG(rfit)

    list_id_already_found = []
    list_funcost_already_found = []
    for i in range(nlines_arc):
        if solution[i]['lineok']:
            list_id_already_found.append(solution[i]['id'])
            list_funcost_already_found.append(solution[i]['funcost'])

    nnewlines = 0
    for i in range(nlines_arc):
        if not solution[i]['lineok']:
            zfit = poly(xpos_arc[i])  # predicted wavelength
            isort = np.searchsorted(wv_master, zfit)
            if isort == 0:
                ifound = 0
                dlambda = wv_master[ifound] - zfit
            elif isort == nlines_master:
                ifound = isort - 1
                dlambda = zfit - wv_master[ifound]
            else:
                dlambda1 = zfit - wv_master[isort - 1]
                dlambda2 = wv_master[isort] - zfit
                if dlambda1 < dlambda2:
                    ifound = isort - 1
                    dlambda = dlambda1
                else:
                    ifound = isort
                    dlambda = dlambda2

            if ifound not in list_id_already_found:  # unused line
                if dlambda < times_sigma_inclusion * sigma_rfit:
                    list_id_already_found.append(ifound)
                    solution[i]['lineok'] = True
                    solution[i]['type'] = 'I'
                    solution[i]['id'] = ifound
                    solution[i]['funcost'] = max(list_funcost_already_found)
                    nnewlines += 1

    return solution
Example #28
0
 def __linfit(self,bottom,test):
     cv = 299792.458
     fit   = pol.polyfit(self.group.lmbd[self.idx[bottom]],self.data[:,bottom].T,2)
     a,b,c = fit[2,:],fit[1,:],fit[0,:]
     lmin  = -b/(2*a)
     bot   = pol.polyval(lmin,fit,tensor=False)
     pred  = pol.polyval(self.group.lmbd[test],fit)
     vel   = cv*(lmin-self.cent)/self.cent
     # Error of fit with extra error term to penalize fits 
     # that gets wildly off center, with extra weight so it *hurts*
     err   = np.sqrt( np.mean( (self.data[:,test]-pred)**2,axis=1) 
                                      + 2*(lmin-self.cent)**2 )
     return vel,bot,err
Example #29
0
    def __init__(self, websocket, user, client):
        self.ser = serial.Serial(settings.port, 9600)
        self.write = writefile.Write(settings.output)
        self.recording = False
        self.websocket = websocket
        self.user = user
        self.client = client

        y = [0, 30, 50, 70, 90]
        x = [700, 659, 645, 580, 535]

        self.coefs = poly.polyfit(x, y, 2)
        logging.debug(self.coefs)
Example #30
0
 def solve (self):
     self.paramnames = ['a%d' % i for i in xrange (self.maxexponent + 1)]
     # Based on my reading of the polyfit() docs, I think w=invsigma**2 is right...
     self.params = npoly.polyfit (self.x, self.data, self.maxexponent,
                                  w=self.invsigma**2)
     self.perror = None # does anything provide this? could farm out to lmmin ...
     self.covar = None
     self.mfunc = lambda x: npoly.polyval (x, self.params)
     self.mdata = self.mfunc (self.x)
     self.resids = self.data - self.mdata
     self.rchisq = (((self.resids * self.invsigma)**2).sum ()
                    / (self.x.size - (self.maxexponent + 1)))
     return self
Example #31
0
def get_object_runner():
    # Pick object model
    obj_name = obj_sampler.next()

    # Is our model texture mapped?
    mapped = False
    try:
        with open(
                os.path.join(obj_root, obj_name, 'models',
                             'model_normalized.mtl'), 'r') as f:
            for line in f:
                if 'map_Kd' in line:
                    mapped = True
                    break
    except FileNotFoundError:
        pass

    if mapped:
        replace = np.random.rand() < mapped_replace_prob
    else:
        replace = np.random.rand() < unmapped_replace_prob

    # Default stuff
    d = {
        "module": "object.ObjectTrajectoryRunner",
        "config": {
            'path': "<args:0>/%s/models/model_normalized.obj" % obj_name,
            'seed': pick_randint(0, 2**31)
        },
    }

    if replace:
        texture = tex_sampler.next()
        d['config']['texture'] = "<args:1>/%s" % texture

    max_tsl_dist = obj_max_tsl_per_frame * n_frames / degree
    max_rot_dist = obj_max_rot_per_frame * n_frames / degree

    # Create control points
    loc_pts = np.zeros((degree + 1, 3))
    rot_pts = np.zeros((degree + 1, 3))
    scl_pts = np.zeros((degree + 1, 3))
    loc_pts[0] = get_vector_in_frustum(obj_min_base, obj_max_base,
                                       obj_min_into, obj_max_into,
                                       cam_min_into)
    rot_pts[0] = np.random.rand(3) * np.pi * 2
    scl_pts[0] = pick_rand(obj_min_scale, obj_max_scale, 3)
    is_static = np.random.rand() < enter_static_prob
    for i in range(1, degree + 1):
        if is_static:
            loc_pts[i] = loc_pts[i - 1]
            rot_pts[i] = rot_pts[i - 1]
            scl_pts[i] = scl_pts[i - 1]
            if np.random.rand() > conti_static_prob:  # Inverted
                is_static = False
        else:
            this_tsl_dist = pick_normal_rand(0, max_tsl_dist, 3)
            loc_pts[i] = get_next_vector_in_frustum(loc_pts[i - 1],
                                                    this_tsl_dist,
                                                    obj_min_base, obj_max_base,
                                                    obj_min_into, obj_max_into,
                                                    cam_min_into)
            rot_pts[i] = rot_pts[i - 1] + pick_normal_rand(0, max_rot_dist, 3)
            scl_pts[i] = scl_pts[i - 1] * pick_rand(
                min_scale_change**(1 / degree), max_scale_change**(1 / degree),
                3)
            if np.random.rand() < enter_static_prob:
                is_static = True

    # Create polynomial
    Xs = np.array([i / degree for i in range(degree + 1)])
    loc_poly = poly.polyfit(Xs, loc_pts, deg=degree).astype(float).tolist()
    rot_poly = poly.polyfit(Xs, rot_pts, deg=degree).astype(float).tolist()
    scl_poly = poly.polyfit(Xs, scl_pts, deg=degree).astype(float).tolist()

    d['config']['poses'] = {
        'location_poly': loc_poly,
        'rotation_poly': rot_poly,
        'scale_poly': scl_poly,
    }

    return d, loc_poly
Example #32
0
    def VirialTheorem(self):
        '''This method extracts the kinetic and potential energy, and, using Virial theorem, calculates the value of this theorem, which is then plotted over time'''
        LoadDataEng = np.load(
            'Energy_data_%s_years_%ss_timestep_%s_bodies_%s.npy' %
            ((self.TotalTime / (86400 * 365)), self.deltaT, len(
                self.Planet), self.SolarSystem.PlanetList[0].method))
        ChangeinTotalEnergy = []
        PotentialEnergy = []
        KineticEnergy = []
        TotalEnergy = []
        Time = []
        Timeplot = []
        TotalEnergyplot = []
        ChangeinTotalEnergyplot = []

        #Extraction of data
        for line in LoadDataEng:
            Time.append(line[0])

        for line in LoadDataEng:
            TotalKinetic = line[len(self.Planet) + 1]
            TotalPotential = line[len(self.Planet) + 2]
            PotentialEnergy.append(TotalPotential)
            KineticEnergy.append(TotalKinetic)

        #This is using the Virial theorem to calculation total energy
        for i in range(len(KineticEnergy)):
            TotalEnergy.append(2 * KineticEnergy[i] - PotentialEnergy[i])

        ChangeinTotalEnergyCalc = False
        Answer = str(
            input('Do you want to include Change in Virial Theorem? (Y/N)'))
        if Answer == 'Y':
            ChangeinTotalEnergyCalc = True

        #This fits a line of best fit
        Time_new = np.linspace(Time[0], Time[-1], num=len(Time) * 10)
        coefficients = poly.polyfit(Time, TotalEnergy, 10)
        fit = poly.polyval(Time_new, coefficients)
        plt.plot(Time_new, fit, label='Best fit')

        for l in range(0, len(Time), int(len(Time) / 200)):
            Timeplot.append(Time[l])
            TotalEnergyplot.append(TotalEnergy[l])

        plt.plot(Timeplot,
                 TotalEnergyplot,
                 'o',
                 markersize=2,
                 label='Value of Virial\'s Theorem')
        plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
        plt.xlabel('Time [s]')
        plt.ylabel('Value of Virial\'s Theorem')
        plt.legend(loc=1)
        plt.title(
            'Value of Virial\'s Theorem of %s bodies,  \n %s years, time step of %s seconds, \n Standard Deviation: %s  Mean: %s \n'
            %
            (len(self.Planet), self.TotalTime /
             (86400 * 365), self.deltaT, '{:0.3e}'.format(np.std(TotalEnergy)),
             '{:0.3e}'.format(np.average(TotalEnergy))))
        plt.savefig(
            'Value of Virial\'s Theorem of %s bodies %s years time step of %s seconds.png'
            % (len(self.Planet), self.TotalTime / (86400 * 365), self.deltaT),
            bbox_inches='tight')
        plt.show()

        if ChangeinTotalEnergyCalc == True:

            for j in range(1, len(LoadDataEng)):
                ChangeinTotalEnergy.append(
                    ((TotalEnergy[j] - TotalEnergy[0]) / TotalEnergy[0]) * 100)
            Time.pop(0)
            Time_new = np.linspace(Time[0], Time[-1], num=len(Time) * 10)
            coefficients = poly.polyfit(Time, ChangeinTotalEnergy, 10)
            fit = poly.polyval(Time_new, coefficients)
            plt.plot(Time_new, fit, label='Best fit')

            for l in range(0, len(Time), int(len(Time) / 200)):
                ChangeinTotalEnergyplot.append(ChangeinTotalEnergy[l])

            plt.plot(
                Timeplot,
                ChangeinTotalEnergyplot,
                'o',
                markersize=3,
                label='Percentage change in the value of Virial\'s theorem')
            plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
            plt.xlabel('Time [s]')
            plt.ylabel('Percentage change in value of Virial\'s Theorem')
            plt.legend(loc=1)
            plt.title(
                'Percentage change in the value of Virial\'s Theorem of %s bodies,  \n %s years, time step of %s seconds, \n Standard Deviation: %s  Mean: %s \n'
                % (len(self.Planet), self.TotalTime / (86400 * 365),
                   self.deltaT, '{:0.3e}'.format(np.std(ChangeinTotalEnergy)),
                   '{:0.3e}'.format(np.average(ChangeinTotalEnergy))))
            plt.savefig(
                'Percentage change in the value of Virial\'s Theorem of %s bodies %s years time step of %s seconds.png'
                % (len(self.Planet), self.TotalTime /
                   (86400 * 365), self.deltaT),
                bbox_inches='tight')
            plt.show()
Example #33
0
    def _wb1Reg_(self):
        #WB1
        # Pulling excel file data for WB1 up and changing into a numpy array
        print("\n" + "\n")
        # print('Excel data set in array format:' + '\n' + '\n')

        book = xlrd.open_workbook('PastWinningNumbers_ExcelFormat.xlsx')
        sheets = book.sheets()
        for sheet in sheets:

            data = np.array(
                [[sheet.cell_value(r, c) for c in range(sheet.ncols)]
                 for r in range(sheet.nrows)])
            # print(data)
            data.shape = (49, 7)
            # print(data.shape)
        # Creating a linear regression scatter plot chart for WB1

        lottoSet_df = pd.read_excel(book, index_col=None, na_values=['NA'])

        # print('\n' + '\n' + 'LottoSet_df:')
        # print(lottoSet_df)

        x = np.array(lottoSet_df['WB1'])
        y = np.array(lottoSet_df.index)

        # print('\n' + '\n' + 'WB1:')
        # print(x)
        # print('\n' + '\n' + 'Dates, indexed:')
        # print(y)

        xerr = [1] * 48

        plt.rc('font', family='serif', size=13)
        m, b = polyfit(x, y, 1)
        plt.plot(x, y, 's', color='#0066FF')
        plt.plot(x, m * x + b, 'r-')  #BREAKS ON THIS LINE
        plt.errorbar(x, y, xerr=xerr, yerr=0, linestyle="None", color='black')
        plt.title('Scatter Plot of winning WB1, 2019', fontsize=14)
        plt.xlabel('White Ball 1', fontsize=14)
        plt.ylabel('Index', fontsize=14)
        plt.autoscale(enable=True, axis=u'both', tight=False)
        plt.grid(False)
        plt.xlim(0, 70)
        plt.ylim(0, 50)
        plt.scatter(x, y, 1)
        W1 = plt.show()
        print(W1)

        print('................................')

        # implementing Tensorflow API
        # resources: https://jacobbuckman.com/post/graph-inspection/
        # https://www.tensorflow.org/api_docs/python/tf/Graph
        W1 = tf.Graph()
        with W1.as_default():

            c = tf.constant(30.0)
            assert c.graph is W1

        print('W1 Tensorflow Graph:')
        print(W1)
        # output is the gloable default graph object: <tensorflow.python.framework.ops.Graph object at 0x000001C272F59470>

        print('................................')

        # Attempting to use statsmodels to get summary of WB1 regression

        print('OLS Regression results for White Ball 1:' + '\n' + '\n')

        model = sm.OLS(y, x)
        results = model.fit()
        print(results.summary())
                orientation='potrait',
                format='pdf',
                transparent=False)
    plt.show()


def trsf(x):
    return x / 100.


n = 10
x = np.linspace(xmin, xmax, 100)

# method 1:
# regression using ployfit
c = poly.polyfit(hgt, wgt, n)
y = poly.polyval(x, c)
plot_data_and_fit(hgt, wgt, x, y, "ployfit")

# method 2:
# regression using the Vandermonde matrix and pinv
X = poly.polyvander(hgt, n)
#Pattern Recognition (1) B-IT, Summer Term 2019
c = np.dot(la.pinv(X), wgt)
y = np.dot(poly.polyvander(x, n), c)
plot_data_and_fit(hgt, wgt, x, y, "Vandermonde matrix and pinv")

# method 3:
# regression using the Vandermonde matrix and lstsq
X = poly.polyvander(hgt, n)
c = la.lstsq(X, wgt)[0]
x = np.array([1.0, 2.5, 3.5, 4.0, 1.1, 1.8, 2.2, 3.7])
y = np.array([6.008, 15.722, 27.130, 33.772, 5.257, 9.549, 11.098, 28.828])

#On peut utiliser directement la fonction de fit polynomial
#x,y et le degré du polynome
p = Polynomial.fit(x, y, 8)
print(p)
#Attention ce fit utilise des valeurs shiftées et scalées (stabilité numérique)
#Pour afficher les coefs "normaux", on fait comme suit :
print(p.convert(domain=(-1, 1)))
#Plus directement on pouvait écrire : p = Polynomial.fit(x,y,1,domain=(-1,1))

#Autre manière de procéder
#plt.figure(1)
coefs, r = poly.polyfit(x, y, 2, full=True)
#RSS
print("Coefs:", coefs, "r:", r)
RSS = np.sum((np.polyval(np.polyfit(x, y, 1), x) - y)**2)
print("RSS=", RSS)
ffit = poly.polyval(x, coefs)
#plt.plot(x,y,'o')
#plt.plot(x,ffit)
#plt.plot(x,ffit-y,'*')

plt.figure(2)
plt.plot(*p.linspace())
plt.plot(x, y, 'o')
plt.show()
#plt.figure
#plt.plot(x,y,'o')
Example #36
0
    plt.errorbar(MJD[~idx],
                 RV_HARPS[~idx] * 1000 - np.mean(RV_HARPS[~idx] * 1000),
                 yerr=RV_noise[~idx],
                 fmt=".k",
                 capsize=0,
                 alpha=0.3)
    plt.show()
    # idx2 = (MJD[~idx] > 53986) & (MJD[~idx] < 53990)
    # plt.errorbar(MJD[~idx][idx2], RV_HARPS[~idx][idx2] *1000 - np.mean(RV_HARPS[~idx][idx2] *1000), yerr=RV_noise[~idx][idx2], fmt=".k", capsize=0, alpha=0.3)
    # plt.show()

if 0:
    from numpy.polynomial import polynomial as P
    c, stats = P.polyfit(MJD[~idx],
                         RV_HARPS[~idx] * 1000 -
                         np.mean(RV_HARPS[~idx] * 1000),
                         3,
                         full=True,
                         w=1 / (RV_noise[~idx])**2)
    x_fit = np.linspace(min(MJD[~idx] - 1), max(MJD[~idx] + 1), 10000)
    y_fit = P.polyval(x_fit, c)
    plt.errorbar(MJD[~idx],
                 RV_HARPS[~idx] * 1000 - np.mean(RV_HARPS[~idx] * 1000),
                 yerr=RV_noise[~idx],
                 fmt=".k",
                 capsize=0)
    plt.plot(x_fit, y_fit)
    plt.show()

    y_plot = P.polyval(MJD[~idx], c)
    plt.plot(MJD[~idx],
             y_plot - (RV_HARPS[~idx] * 1000 - np.mean(RV_HARPS[~idx] * 1000)),
Example #37
0
			mu_r[l] = tbdata_FDS.field('r_mag')[j] + 2.5 * np.log10(math.pi*axis_ratio[l]*math.pow(R_eff[l],2)) + 2.5 * np.log10(2)
			ERR_mu_r[l] = ERR_m_r[l] + (5 / np.log(10)) * (ERR_R_eff[l] / R_eff[l]) + 2.5 * COV_mr_logRe2
			u[l] = tbdata_FDS.field('u')[j]
			g[l] = tbdata_FDS.field('g')[j]
			r[l] = tbdata_FDS.field('r')[j]
			i[l] = tbdata_FDS.field('i')[j]
			ERR_g = tbdata_FDS.field('g_e')[j]
			ERR_r = tbdata_FDS.field('r_e')[j]
			ERR_i = tbdata_FDS.field('i_e')[j]
			log_mass[l] = 1.15 + 0.70*(g[l]-i[l]) - 0.4*M_r[l] + 0.4*(r[l]-i[l])
			ERR_log_mass[l] = np.sqrt(0.49*(ERR_g**2+ERR_i**2) + 0.16*ERR_m_r[l]**2 + 0.16*(ERR_r**2+ERR_i**2))/(log_mass[l]*np.log(10))
			sersic_index[l] = tbdata_FDS.field('n')[j]

xFit = [g[k]-r[k] for k in range(size) if u[k]-g[k] > 0.0]
yFit = [u[k]-g[k] for k in range(size) if u[k]-g[k] > 0.0]
b, m = polyfit(xFit, yFit, 1)
Fitline = [b + m * xFit[k] for k in range(len(xFit))]
plt.plot(xFit, yFit, '.')
plt.plot(xFit, Fitline, c='grey')

for l in range(size):
	draft = name_list[l]
	name_FCC = 'FCC' + str(draft[0])
	if name_FCC=='FCC37' or name_FCC=='FCC46' or name_FCC=='FCC33' or name_FCC=='FCC29':
			u[l] = g[l] + b + (g[l]-r[l])*m
			plt.plot(g[l]-r[l], u[l]-g[l], 'x')

#writing a csv output
ListDict = []
for l in range(size):
	draft = name_list[l]
    mag_instrument_source=mag_instrument
    rmag_source=rmag
    '''
    #    no values
    #    mag_instrument_bkg=mag_instrument[-1]
    #    rmag_bkg=rmag[-1]

    #    print('Instrument Mag of RefStars',mag_instrument[k][1:])
    #    print('Rmag of RefStars',rmag[1:])
    print()
    #    shift,slope=polyfit(mag_instrument[k][1:],rmag[1:],1)

    #    param,res=polyfit(mag_instrument[k][1:],rmag[1:],1,full=True)
    #    shift,slope=polyfit(mag_instrument[k][1:],rmag[1:],1,w=1/(mag_instrument_err[k][1:]))
    shift, slope = polyfit(mag_instrument[k][1:], rmag[1:], 1)

    #    residual=res[0].tolist()[0]
    #    print('shift =','%.3f' %shift)
    #    print('slope =','%.3f' %slope)
    #    print('residual =', residual)

    rmag_fitting[k] = shift + slope * mag_instrument[k]
    print('... rmag_fitting (Ref.Stars only):', rmag_fitting[k][1:])
    print('... rmag_fitting (Target only):', rmag_fitting[k][0])

    Rmag_targets[k] = rmag_fitting[k][0]

    #    SN=signal_mean/signal_std
    #    SN=bkg_mean/bkg_std ??
    #    ErrorRmag=2.5*log10(1+N/S)=1.086/(S/N)
Example #39
0
def get_omega(S, T, starting_age, ending_age, E, flag_graphs):
    '''
    Inputs:
        S - Number of age cohorts (scalar)
        T - number of time periods in TPI (scalar)
        starting_age - initial age of cohorts (scalar)
        ending_age = ending age of cohorts (scalar)
        E = number of children (scalar)
        flag_graphs = graph variables or not (bool)
    Outputs:
        omega_big = array of all population weights over time ((T+S)x1 array)
        g_n_SS = steady state growth rate (scalar)
        omega_SS = steady state population weights (Sx1 array)
        surv_array = survival rates (Sx1 array)
        rho = mortality rates (Sx1 array)
        g_n_vec = population growth rate over time ((T+S)x1 array)
    '''
    data1 = data
    pop_data = np.array(data1['2010'])
    poly_pop = poly.polyfit(np.linspace(
        0, pop_data.shape[0]-1, pop_data.shape[0]), pop_data, deg=11)
    poly_int_pop = poly.polyint(poly_pop)
    pop_int = poly.polyval(np.linspace(
        starting_age, ending_age, S+1), poly_int_pop)
    new_omega = pop_int[1:]-pop_int[:-1]
    surv_array, children_rate = get_survival(S, starting_age, ending_age, E)
    surv_array[-1] = 0.0
    imm_array, children_im = get_immigration2(S, starting_age, ending_age, E)
    #imm_array *= 0.0
    #children_im *= 0.0
    fert_rate, children_fertrate = get_fert(S, starting_age, ending_age, E)
    cum_surv_rate = np.cumprod(surv_array)
    if flag_graphs:
        rate_graphs(S, starting_age, ending_age, imm_array, fert_rate, surv_array, children_im, children_fertrate, children_rate)
    children_int = poly.polyval(np.linspace(0, starting_age, E + 1), poly_int_pop)
    sum2010 = pop_int[-1] - children_int[0]
    new_omega /= sum2010
    children = np.diff(children_int)
    children /= sum2010
    children = np.tile(children.reshape(1, E), (T + S, 1))
    omega_big = np.tile(new_omega.reshape(1, S), (T + S, 1))
    # Generate the time path for each age group
    for t in xrange(1, T + S):
        # Children are born and then have to wait 20 years to enter the model
        omega_big[t, 0] = children[t-1, -1] * (children_rate[-1] + children_im[-1])
        omega_big[t, 1:] = omega_big[t-1, :-1] * (surv_array[:-1] + imm_array[:-1])
        children[t, 1:] = children[t-1, :-1] * (children_rate[:-1] + children_im[:-1])
        children[t, 0] = (omega_big[t-1, :] * fert_rate).sum(0) + (children[t-1] * children_fertrate).sum(0)
    OMEGA = np.zeros(((S + E), (S + E)))
    OMEGA[0, :] = np.array(list(children_fertrate) + list(fert_rate))
    OMEGA += np.diag(np.array(list(children_rate) + list(surv_array[:-1])) + np.array(list(children_im) + list(imm_array[:-1])), -1)
    eigvalues, eigvectors = np.linalg.eig(OMEGA)
    mask = eigvalues.real != 0
    eigvalues = eigvalues[mask]
    mask2 = eigvalues.imag == 0
    eigvalues = eigvalues[mask2].real
    g_n_SS = eigvalues - 1
    eigvectors = np.abs(eigvectors.T)
    eigvectors = eigvectors[mask]
    omega_SS = eigvectors[mask2].real
    if eigvalues.shape[0] != 1:
        ind = ((abs(omega_SS.T/omega_SS.T.sum(0) - np.array(list(children[-1, :]) + list(omega_big[-1, :])).reshape(S+E, 1)).sum(0))).argmin()
        omega_SS = omega_SS[ind]
        g_n_SS = [g_n_SS[ind]]
    omega_SS = omega_SS[E:]
    omega_SS /= omega_SS.sum()
    # Creating the different ability level bins
    if flag_graphs:
        pop_graphs(S, T, starting_age, ending_age, children, g_n_SS[0], omega_big)
    N_vector = omega_big.sum(1)
    g_n_vec = N_vector[1:] / N_vector[:-1] -1.0
    g_n_vec = np.append(g_n_vec, g_n_SS[0])
    rho = 1.0 - surv_array
  
    imm_rates_mat = np.hstack((
        np.tile(np.reshape(imm_array[:],(S,1)), (1, 120)),
        np.tile(np.reshape(imm_array[:],(S,1)), (1, T+S-120))))

    return omega_big, g_n_SS[0], omega_SS, surv_array, rho, g_n_vec, imm_rates_mat
Example #40
0
def fit_foreground(freqs, data, degree=2):
    # data shape: [nfreq, npix]
    pfit = polyfit(freqs, data, deg=degree)
    return np.swapaxes(polyval(freqs, pfit), 0, 1)
Example #41
0
def poly_fit_timescales(x, y, ey, name=None):
    """
    assert a1 > 0
    """
    thre = 1.5
    a1 = 2
    a2 = -3
    order1 = 2
    order2 = 2
    b = min(y)
    if name == "AT2019dge":
        a1 = 4
        a2 = 0
        order1 = 4
        order2 = 4
    elif name == "iPTF14gqr":
        a1 = 2
        a2 = -3
        order1 = 2
        order2 = 3
        thre = 2
    elif name == "SN2005ek":
        a1 = 2
        a2 = -2
        order2 = 2
        thre = 2.
    elif name == "iPTF16hgs":
        a1 = 2
        a2 = -5
        order2 = 3
        thre = 2.
        b = min(y) - 0.05
    elif name == "SN2010X":
        a1 = 2
        a2 = -3
        order1 = 1
        order2 = 2
        thre = 2
    elif name == "SN2019bkc":
        a1 = 2
        a2 = -3
        order2 = 2
        thre = 3.5
    elif name == "SN2018gep":
        a1 = 0.5
        a2 = -1
        order1 = 3
        order2 = 4
        thre = 1.5
    elif name == "SN2018kzr":
        a2 = -2
        order2 = 2
        thre = 3
    elif name == "PTF09dav":
        a1 = 5
        a2 = -5
        order1 = 3
        order2 = 4
        thre = 2
        ix = x < 20
        x = x[ix]
        y = y[ix]
        ey = ey[ix]
    elif name == "SN2002bj":
        a1 = 2
        a2 = -3
        order1 = 1
        order2 = 3
        thre = 2.5
    elif name == "PTF10iuv":
        a1 = 4
        a2 = -3
        order1 = 2
        order2 = 3
        thre = 1.8
    elif name == "iPTF16asu":
        a1 = 1
        a2 = -2
        order1 = 2
        order2 = 3
        thre = 1.8
        #b = b = min(y)-0.15

    y = y - b
    NSAMPLES = 100

    # cut data points not useful
    #plt.errorbar(x, y, ey, fmt=".k")
    ix = np.any([x <= 0, (x > 0) & (y < (min(y) + thre))], axis=0)
    x = x[ix]
    y = y[ix]
    ey = ey[ix]

    if name not in [
            "SN2005ek", "SN2010X", "SN2019bkc", "OGLE13-079", "SN2018kzr",
            "PTF09dav", "SN2002bj", "iPTF16hgs", "SN2018gep", "iPTF16asu",
            "iPTF14gqr"
    ]:
        # adjust peak epoch
        ix1 = x <= a1
        x1 = x[ix1]
        y1 = y[ix1]
        ey1 = ey[ix1]
        coefs1 = mypoly.polyfit(x1, y1, order1, w=1 / ey1**2)
        xnew1 = np.linspace(x1[0] - 0.5, x1[-1], 1000)
        ynew1 = mypoly.polyval(xnew1, coefs1)
        id_peak = np.argsort(ynew1)[0]
        tpeak = xnew1[id_peak]
        x = x - tpeak

    plt.figure(figsize=(6, 4))
    plt.errorbar(x, y, ey, fmt=".k")

    if name not in [
            "SN2005ek", "SN2010X", "SN2019bkc", "OGLE13-079", "SN2018kzr",
            "PTF09dav", "SN2002bj", "iPTF16hgs"
    ]:
        # peak timescale
        ix1 = x <= a1
        x1 = x[ix1]
        y1 = y[ix1]
        ey1 = ey[ix1]
        p1, C_p1 = np.polyfit(x1, y1, order1, w=1 / ey1**2, cov=True)
        xnew1 = np.linspace(x1[0], x1[-1], 1000)
        ynew1 = np.polyval(p1, xnew1)
        ymax = min(ynew1)
        N1 = len(p1)
        L1 = np.linalg.cholesky(C_p1)
        # you should find np.dot(L1, L1.T) == C_p1
        zprep = np.zeros((NSAMPLES, N1))
        t1s = np.zeros(NSAMPLES)
        limflag1 = 0
        for i in range(NSAMPLES):
            try:
                p1_ = p1 + np.dot(L1, zprep[i])
                ynew1 = np.polyval(p1_, xnew1)
                plt.plot(xnew1, ynew1, color="r", linewidth=0.5)
                ydiff1 = abs(ynew1 - (ymax + 0.75))
                id_rise = np.argsort(ydiff1)[0]
                if limflag1 == 0:
                    if ydiff1[id_rise] > 0.01:
                        riselim = True
                    else:
                        riselim = False
                    limflag1 = 1
                t1s[i] = xnew1[id_rise] * (-1)
            except Exception:
                print(i)
        tau_rise = np.median(t1s)
        tau_rise_unc = np.std(t1s)
        plt.plot(tau_rise * (-1), 0.75, 'ro')
    elif name in ["SN2019bkc", "OGLE13-079", "PTF09dav"]:
        ymax = 0
        ix1 = x <= a1
        x1 = x[ix1]
        y1 = y[ix1]
        func1 = interp1d(x1, y1)
        xnew1 = np.linspace(x1[0] + 0.01, x1[-1] - 0.01, 1000)
        ynew1 = func1(xnew1)
        plt.plot(xnew1, ynew1, color="r", linewidth=0.5)
        ydiff1 = abs(ynew1 - (ymax + 0.75))
        id_rise = np.argsort(ydiff1)[0]
        tau_rise = xnew1[id_rise] * (-1)
        tau_rise_unc = -99
        riselim = False
        plt.plot(tau_rise * (-1), 0.75, 'ro')
    else:
        tau_rise = -99
        tau_rise_unc = -99
        riselim = True
        ymax = 0

    # decline timescale
    ix2 = x >= a2
    x2 = x[ix2]
    y2 = y[ix2]
    ey2 = ey[ix2]
    p2, C_p2 = np.polyfit(x2, y2, order2, w=1 / ey2**2, cov=True)
    xnew2 = np.linspace(x2[0], x2[-1], 1000)
    ynew2 = np.polyval(p2, xnew2)
    N2 = len(p2)
    L2 = np.linalg.cholesky(C_p2)
    # you should find np.dot(L1, L1.T) == C_p1
    zprep = np.zeros((NSAMPLES, N2))
    t2s = np.zeros(NSAMPLES)
    limflag2 = 0
    for i in range(NSAMPLES):
        try:
            p2_ = p2 + np.dot(L2, zprep[i])
            ynew2 = np.polyval(p2_, xnew2)
            plt.plot(xnew2, ynew2, color="b", linewidth=0.5)
            ydiff2 = abs(ynew2 - (ymax + 0.75))
            id_decay = np.argsort(ydiff2)[0]
            if limflag2 == 0:
                if ydiff2[id_decay] > 0.01:
                    decaylim = True
                else:
                    decaylim = False
                limflag2 = 1
            t2s[i] = xnew2[id_decay]
        except Exception:
            print(i)

    tau_decay = np.median(t2s)
    tau_decay_unc = np.std(t2s)

    plt.plot(tau_decay, 0.75, 'bo')

    result = {
        "name": name,
        "Mpeak": ymax + b,
        "tau_rise": tau_rise,
        "tau_rise_unc": tau_rise_unc,
        "tau_rise_lim": riselim,
        "tau_decay": tau_decay,
        "tau_decay_unc": tau_decay_unc,
        "tau_decay_lim": decaylim
    }

    return result
Example #42
0
def plot_acceleration(y, annos, pure_fpath, acc_fpath, k, intervals):
    """
    Plots acceleration graph.

    Parameters
    ----------
    y: list of float - each float represents a median annotation time in the
    dataset.
    annos: int - number of annotators in the dataset.
    pure_fpath: str - path where the scatter plot without acceleration should
    be stored.
    acc_fpath: str - path where the scatter plot with fitted acceleration should
    be stored.
    k: int - degree of the polynomial to be fit to the data in order to
    approximate the acceleration.
    intervals: list of intervals - [[l,u], [l,u]], each entry represents the
    (l)ower and (u)pper bounds of an interval.

    """
    # MD, SU, or something else
    institution = pure_fpath.split("_")[4]
    # Number of tweets to be kept
    try:
        TO_KEEP = int(pure_fpath.split("_")[-7])
    except ValueError:
        # Annotator should be plotted, so his/her name is also part of the file
        # name
        TO_KEEP = int(pure_fpath.split("_")[-8])
    # Plotting
    fig = plt.figure(figsize=(5, 3))
    ax = fig.add_subplot(111)
    s = [10 for n in range(annos)]
    # Use different colors to encode the annotator group of each participant
    x = range(len(y))
    # ax.scatter(x, y, color="silver", s=s, label="Median annotation time")
    ax.scatter(x, y, color="darkgray", s=s)
    # Hide the right and top spines (lines)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    # Title
    # if len(group) > 0:
    #     title = "Median annotation time for {} in {}"\
    #         .format(institution.upper(), group.title())
    # elif len(name) > 0:
    #     title = "Annotation times for {}".format(name.title())
    # else:
    #     title = "Median annotation time for {}".format(institution.upper())
    # plt.title(title)
    # Only show ticks on the left and bottom spines
    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')
    # Set labels of axes
    ax.set_xlabel("i-th annotated tweet")
    ax.set_ylabel("Median annotation time in s")
    # Add legend outside of plot
    legend = ax.legend(loc="best", shadow=True)
    # Limits of axes
    plt.xlim(-1, TO_KEEP + 3)
    plt.ylim(-5, max(y) + 3)
    plt.savefig(pure_fpath, bbox_inches='tight', dpi=600)
    # Intervals for which separate derivatives should be calculated
    # intervals = [[0, 20], [20, 40], [40, 60]]
    # intervals = [[0, 30]]
    # intervals = [[0, 25], [25, 59]]
    # intervals = [[0, 19], [19, 59]]
    COLORS_P = ["black", "black", "black"]
    COLORS_D = ["darkorange", "darkorange", "darkorange"]

    # Use numpy's new polynomial module to fit the line with a polynomial
    # of degree k. Fit it only to the current interval
    # http://stackoverflow.com/questions/18767523/fitting-data-with-numpy

    # Fit polynomial of 3rd degree to the data
    for idx, (l, o) in enumerate(intervals):
        # print "Fit interval {}-{}".format(l, o)
        xp = np.linspace(l, o, 1000)
        # x/y have either 50 values, then 100 NANs or 150 values
        # For polyfit to work, we must simply remove the NANs
        x_cur = np.array(x[l:o])
        y_cur = np.array(y[l:o])
        # Indices where x and y are not NAN
        nan_idx = np.isfinite(x_cur) & np.isfinite(y_cur)
        f = poly.polyfit(x_cur[nan_idx], y_cur[nan_idx], k)
        # print "f=", f
        yp = poly.polyval(xp, f)
        # Plot polynomial
        # Uncomment for k=3
        # ax.plot(xp, yp, "-", color=COLORS_P[idx], linewidth=2,
        #         label="f={:<4.1f}x^3+{:<4.1f}x^2+{:<4.1f}x+{:<4.1f}"
        #         .format(f[3], f[2], f[1], f[0]))
        if idx == 0:
            ax.plot(xp,
                    yp,
                    "-",
                    color=COLORS_P[idx],
                    linewidth=2,
                    label="Fitted polynomial")
        else:
            ax.plot(xp, yp, "-", color=COLORS_P[idx], linewidth=2)
        # Uncomment for k=4
        # ax.plot(xp, yp, "-", color=COLORS_P[idx], linewidth=2,
        #         label="{:<4.1f}x^4+{:<4.1f}x^3+{:<4.1f}x^2+{:<4.1f}x+{:<4.1f}"
        #         .format(f[4], f[3], f[2], f[1], f[0]))

        # http://stackoverflow.com/questions/29634217/get-minimum-points-of-numpy-poly1d-curve
        # Coefficients must be past from highest order to lowest order, the
        # opposite of what polyfit() returns, so reverse it
        p = np.poly1d(f[::-1])
        # print "p", p
        pi = p.deriv()
        # print p
        # print "p'", pi
        pii = p.deriv(2)
        # piii = p.deriv(3)
        # print "p''", pii
        roots = pi.r
        # print "roots", roots
        # f' = 0 yields critical points where slope is 0
        # r_crit = roots[roots.imag == 0].real
        r_crit = roots.real
        # print "crit", r_crit
        # Select from critical points the minima, where f'' > 0
        # compute local minima excluding range boundaries
        # x_min = r_crit[pii(r_crit) > 0]
        x_min = r_crit
        y_min = p(x_min)
        # print "x_min", x_min
        # print "y_min", y_min
        # ax.plot(x_min, y_min, "o", color="red")

        # Add y-offset to first derivative, namely the difference of the
        # computed value and y_min
        # If there are multiple points, choose the one with minimum y-value
        min_idx = np.argmin(y_min)
        if not isinstance(y_min, np.float64):
            y_min = y_min[min_idx]
        # print "min y at pos", min_idx
        # print "corresponding x:", x_min[min_idx]
        if not isinstance(x_min, np.float64):
            x_min = x_min[min_idx]
        # print "difference", y_min - pi(x_min)
        # Add offset to the constant of the equation
        pi.c[2] += y_min - pi(x_min)
        ypi = pi(xp)

        # Don't display first derivative
        # Uncomment for k=3
        # ax.plot(xp, ypi, "--", color="black", linewidth=2,
        #         label="f'={:<4.1f}x^2+{:<4.1f}x+{:<4.1f}"
        #         .format(pi[0], pi[1], pi[2]))
        # Uncomment for k=4
        # ax.plot(xp, ypi, "--", color="black", linewidth=2,
        #         label="{:<4.1f}x^3+{:<4.1f}x^2+{:<4.1f}x+{:<4.1f}"
        #         .format(pi[3], pi[2], pi[1], pi[0]))

        # Plot 2nd derivative
        # x_min will be the same as in pi
        roots = pii.r
        # print "roots", roots
        # f' = 0 yields critical points where slope is 0
        # r_crit = roots[roots.imag == 0].real
        r_crit = roots.real
        # print "crit", r_crit
        # Select from critical points the minima, where f''' > 0
        # compute local minima excluding range boundaries
        # x_min = r_crit[piii(r_crit) > 0]
        y_min = p(x_min)
        # print "x_min", x_min
        # print "y_min", y_min
        # ax.plot(x_min, y_min, "o", color="green")

        # Add y-offset to second derivative, namely the difference of the
        # computed value and y_min
        # If there are multiple points, choose the one with minimum y-value
        min_idx = np.argmin(y_min)
        # print "min y at pos", min_idx
        if not isinstance(y_min, np.float64):
            y_min = y_min[min_idx]
            # print "corresponding x:", x_min[min_idx]
        if not isinstance(x_min, np.float64):
            x_min = x_min[min_idx]
        # print "difference", y_min - pi(x_min)
        # Add offset to the constant of the equation
        pii.c[1] += pi(x_min)
        # print "p'(y_min)", y_min
        ypii = pii(xp)
        # Uncomment for k=3
        # ax.plot(xp, ypii, "-", color=COLORS_D[idx], linewidth=3,
        #         label="f''={:<4.1f}x+{:<4.1f}".format(pii[1], pii[0]))
        if idx == 0:
            ax.plot(xp,
                    ypii,
                    "-",
                    color=COLORS_D[idx],
                    linewidth=2,
                    label="Acceleration")
        else:
            ax.plot(xp, ypii, "-", color=COLORS_D[idx], linewidth=2)
        # Uncomment for k=4
        # ax.plot(xp, ypii, "-", color=COLORS_D[idx], linewidth=2,
        #         label="{:<4.1f}x^2+{:<4.1f}x+{:<4.1f}".format(pii[2], pii[1],
        # pii[0]))
    # Split point
    s = intervals[0][1]
    # Add horizontal line to indicate where we split the intervals
    ax.plot((s, s), (0, ax.get_ylim()[1]), "--", color="red", linewidth=2)
    plt.ylim(0, ax.get_ylim()[1])
    # ax.legend(loc="upper right", shadow=True, bbox_to_anchor=(1, 1.4))
    ax.legend(loc="best", shadow=True, fontsize=FONTSIZE)
    plt.savefig(acc_fpath, bbox_inches='tight', dpi=600)
    plt.close()
Example #43
0
    def plot_pairwise(self, par_names=None, prior_info_csv_file=None, fig_filename='pairwise_correlation.png',
                      figure_size=(10, 10)):
        """ creates pairwise correlation between parameters specified by ids
        :param par_names: (list) names of parameter to display
        :param prior_info_csv_file: (string) filename where parameter prior ranges are located
            (Note: '!' will be replaced with '\n')
        :param fig_filename: (string) filename to save the figure as
        :param figure_size: (tuple) figure size
        """

        # read information about prior distributions
        dict_of_priors = None
        if prior_info_csv_file is not None:
            dict_of_priors = self.get_dict_of_priors(prior_info_csv_file=prior_info_csv_file)

        # if parameter names are not specified, include all parameters
        if par_names is None:
            par_names = self.get_all_parameter_names()

        # find the info of parameters to include in the analysis
        info_of_param_info_to_include = []

        # for each parameter, read sampled parameter values and create the histogram
        for par_name in par_names:

            # get parameter values
            par_values = self.dictOfParamValues[par_name]

            # get info
            title, multiplier, x_range, decimal, form = self.get_title_multiplier_x_range_decimal_format(
                par_name=par_name, dict_of_priors=dict_of_priors)

            # adjust parameter values
            par_values = [v*multiplier for v in par_values]

            # append the info for this parameter
            info_of_param_info_to_include.append(
                ParamInfo(name=par_name,
                          label=title.replace('!', '\n'),
                          values=par_values, value_range=x_range))

        # plot pairwise
        # set default properties of the figure
        plt.rc('font', size=6)  # fontsize of texts
        plt.rc('axes', titlesize=6)  # fontsize of the figure title
        plt.rc('axes', titleweight='semibold')  # fontweight of the figure title

        # plot each panel
        n = len(info_of_param_info_to_include)

        if n == 0:
            raise ValueError('Values of parameters are not provided. '
                             'Make sure the calibration algorithm exports the parameter values.')

        f, axarr = plt.subplots(nrows=n, ncols=n, figsize=figure_size)

        for i in range(n):
            for j in range(n):

                # get the current axis
                ax = axarr[i, j]

                if j == 0:
                    ax.set_ylabel(info_of_param_info_to_include[i].label)
                if i == n-1:
                    ax.set_xlabel(info_of_param_info_to_include[j].label)

                if i == j:
                    # plot histogram
                    Fig.add_histogram_to_ax(
                        ax=ax,
                        data=info_of_param_info_to_include[i].values,
                        x_range=info_of_param_info_to_include[i].range
                    )
                    ax.set_yticklabels([])
                    ax.set_yticks([])

                else:
                    ax.scatter(info_of_param_info_to_include[j].values,
                               info_of_param_info_to_include[i].values,
                               alpha=0.5, s=2)
                    ax.set_xlim(info_of_param_info_to_include[j].range)
                    ax.set_ylim(info_of_param_info_to_include[i].range)
                    # correlation line
                    b, m = polyfit(info_of_param_info_to_include[j].values,
                                   info_of_param_info_to_include[i].values, 1)
                    ax.plot(info_of_param_info_to_include[j].values,
                            b + m * info_of_param_info_to_include[j].values, '-', c='black')
                    corr, p = pearsonr(info_of_param_info_to_include[j].values,
                                       info_of_param_info_to_include[i].values)
                    ax.text(0.95, 0.95, '{0:.2f}'.format(corr), transform=ax.transAxes, fontsize=6,
                            va='top', ha='right')

        f.align_ylabels(axarr[:, 0])
        f.tight_layout()
        output_figure(plt=f, filename=fig_filename, dpi=300)
Example #44
0
    def test_polyfit(self):
        def f(x):
            return x*(x - 1)*(x - 2)

        def f2(x):
            return x**4 + x**2 + 1

        # Test exceptions
        assert_raises(ValueError, poly.polyfit, [1], [1], -1)
        assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
        assert_raises(TypeError, poly.polyfit, [], [1], 0)
        assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
        assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
        assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
        assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
        assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
        assert_raises(ValueError, poly.polyfit, [1], [1], [-1,])
        assert_raises(ValueError, poly.polyfit, [1], [1], [2, -1, 6])
        assert_raises(TypeError, poly.polyfit, [1], [1], [])

        # Test fit
        x = np.linspace(0, 2)
        y = f(x)
        #
        coef3 = poly.polyfit(x, y, 3)
        assert_equal(len(coef3), 4)
        assert_almost_equal(poly.polyval(x, coef3), y)
        coef3 = poly.polyfit(x, y, [0, 1, 2, 3])
        assert_equal(len(coef3), 4)
        assert_almost_equal(poly.polyval(x, coef3), y)
        #
        coef4 = poly.polyfit(x, y, 4)
        assert_equal(len(coef4), 5)
        assert_almost_equal(poly.polyval(x, coef4), y)
        coef4 = poly.polyfit(x, y, [0, 1, 2, 3, 4])
        assert_equal(len(coef4), 5)
        assert_almost_equal(poly.polyval(x, coef4), y)
        #
        coef2d = poly.polyfit(x, np.array([y, y]).T, 3)
        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
        coef2d = poly.polyfit(x, np.array([y, y]).T, [0, 1, 2, 3])
        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
        # test weighting
        w = np.zeros_like(x)
        yw = y.copy()
        w[1::2] = 1
        yw[0::2] = 0
        wcoef3 = poly.polyfit(x, yw, 3, w=w)
        assert_almost_equal(wcoef3, coef3)
        wcoef3 = poly.polyfit(x, yw, [0, 1, 2, 3], w=w)
        assert_almost_equal(wcoef3, coef3)
        #
        wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, 3, w=w)
        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
        wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
        # test scaling with complex values x points whose square
        # is zero when summed.
        x = [1, 1j, -1, -1j]
        assert_almost_equal(poly.polyfit(x, x, 1), [0, 1])
        assert_almost_equal(poly.polyfit(x, x, [0, 1]), [0, 1])
        # test fitting only even Polyendre polynomials
        x = np.linspace(-1, 1)
        y = f2(x)
        coef1 = poly.polyfit(x, y, 4)
        assert_almost_equal(poly.polyval(x, coef1), y)
        coef2 = poly.polyfit(x, y, [0, 2, 4])
        assert_almost_equal(poly.polyval(x, coef2), y)
        assert_almost_equal(coef1, coef2)
Example #45
0
def createFits(mc, mt, lab):
    '''
    Get the FD and PE information from the raw data of compression and tension process
    Polyfit the FD curve and get the flexibility of the molecule woth given label
    Pull out the features of the compression and tension procedure
    Plot out the relative curves and return feature values
    :param mc: compression data
    :param mt: tension data
    :param lab: label of the relative linker
    :return: _ifYesJumpC_, _ifYesJumpT_, _diffOfCT_, _diffOfTC_, _aveCB_, _aveCF_, _aveTB_, _aveTF_
        _ifYesJumpC_:	The Jump Value* of Compression Curve (If no Jump = 0)
            [like in the second group, the abnormal condition having the jump value]
        _ifYesJumpC_:	The Jump Value of Tension Curve (If no Jump = 0)
            [like in the second group, the abnormal condition having the jump value]
        _diffOfCT_:	The largest value of (Force of Compression - Force of Tension) at the same strain
        _diffOfCT_:	The largest value of (Force of Tension - Force of Compression) at the same strain
        _aveCB_:	The high flexibility of Compression*
        _aveCF_:	The low flexibility of Compression
        _aveTB_:	The high flexibility of Tension
        _aveTF_:	The low flexibility of Tension
        *The Jump value is the largest abnormal flexibility*
        *If there is no obvious flexibility changing _ave*B_ will equal _ave*F_ *
    '''

    mcn = np.array(mc)
    mtn = np.array(mt)

    #Polyfit the compression and tension curve with degree 5
    coeffs = poly.polyfit(mcn[:, 0], mcn[:, -1], 5)
    pfit = poly.Polynomial(coeffs)

    m = np.array(list(map(lambda x, y: [x, y], mcn[:, 0], mcn[:, -1])))

    # Shift the curve to satisfy the rule of lowest energy as 0 and at 0% strain
    eo, PEo = findMin(m, pfit)
    if abs(eo) > 4:
        os.system("touch " + lab + "-min-out-of-range")
#        return "Error: PE min outside range"
    mcn[:, 0] -= eo
    mtn[:, 0] -= eo
    mcn[:, -1] -= PEo
    mtn[:, -1] -= PEo

    # Plot FD and PE curve after shifted and save the shifted data points
    des = open(lab + "-FD-compression-polyfit.d", "w+")
    for e in mcn:
        des.write(
            str(e[0]) + "\t" + str(e[2]) + "\t" + str(e[3]) + "\t" +
            str(e[4]) + "\n")
    des.close()
    des = open(lab + "-FD-tension-polyfit.d", "w+")
    for e in mtn:
        des.write(
            str(e[0]) + "\t" + str(e[2]) + "\t" + str(e[3]) + "\t" +
            str(e[4]) + "\n")
    des.close()
    plt.figure(figsize=(8.5, 12))
    plt.subplot(211)
    plt.plot(mcn[:, 0],
             mcn[:, 2],
             label='Normal Force - Compression',
             linewidth=0.5)
    plt.plot(mcn[:, 0],
             mcn[:, 1],
             label='Axial Force - Compression',
             linewidth=0.5)
    plt.plot(mtn[:, 0],
             mtn[:, 2],
             label='Normal Force - Tension',
             linewidth=0.5)
    plt.plot(mtn[:, 0],
             mtn[:, 1],
             label='Axial Force - Tension',
             linewidth=0.5)
    plt.legend()
    plt.xlabel('Strain (%)', family='DejaVu Sans', fontsize=10, color='black')
    plt.ylabel('Force (Kcal/mol/A)',
               family='DejaVu Sans',
               fontsize=10,
               color='black')
    plt.xticks(family='DejaVu Sans', fontsize=10, color='black')
    plt.yticks(family='DejaVu Sans', fontsize=10, color='black')
    plt.subplot(212)
    plt.plot(mcn[:, 0],
             mcn[:, -1],
             label='Potential Energy - Compression',
             linewidth=0.5)
    plt.plot(mcn[:, 0],
             mcn[:, -1],
             label='Potential Energy - Tension',
             linewidth=0.5)
    plt.legend()
    plt.xlabel('Strain (%)', family='DejaVu Sans', fontsize=10, color='black')
    plt.ylabel('Potential Energy (Kcal/mol)',
               family='DejaVu Sans',
               fontsize=10,
               color='black')
    plt.xticks(family='DejaVu Sans', fontsize=10, color='black')
    plt.yticks(family='DejaVu Sans', fontsize=10, color='black')
    plt.title('')
    plt.savefig(lab + "-FD-PE-curve.jpeg")
    plt.close()

    # Get the spline function of FD curve for further evaluation and plot the fitted curve
    fdFunctionC = inter.UnivariateSpline(mcn[:, 0], mcn[:, 2], s=200)
    fdFunctionT = inter.UnivariateSpline(mtn[:, 0], mtn[:, 2], s=200)
    plt.figure()
    plt.plot(mcn[:, 0],
             fdFunctionC(mcn[:, 0]),
             label='Normal Force - Compression',
             linewidth=0.5)
    plt.plot(mtn[:, 0],
             fdFunctionT(mtn[:, 0]),
             label='Normal Force - Tensi',
             linewidth=0.5)
    plt.savefig(lab + "-fitting.jpeg")
    plt.close()

    # Calculate the flexibility of the molecule with 'flexibility = PresentLength*Delta(Force)/Delta(Length)'
    # plot the flexibility information and save the data
    flexibilityC = []
    flexibilityT = []
    des = open(lab + "-FD-Flexibility.d", "w+")
    des.write("strain\tcompression\ttension\n")
    _from = min(m[:, 0])
    _to = max(m[:, 0])
    step = (_to - _from) / 500
    xPoints = [x * step + _from for x in range(500 + 1)]
    deltaX = 0.0001

    # Get the differences of tension and compression process and evaluate the jump abnormal situation
    _ifYesJumpC_ = 0
    _ifYesJumpT_ = 0
    _diffOfCT_ = 0
    _diffOfTC_ = 0
    for e in xPoints:
        flexibilityC.append([
            e, (fdFunctionC(e + deltaX) - fdFunctionC(e)) / deltaX *
            ((100 + e) / 100.0)
        ])
        flexibilityT.append([
            e, (fdFunctionT(e + deltaX) - fdFunctionT(e)) / deltaX *
            ((100 + e) / 100.0)
        ])

        if flexibilityC[-1][1] < -10 and flexibilityC[-1][
                1] < _ifYesJumpC_:  #The Threshold was set randomly, in need of more trials.
            _ifYesJumpC_ = flexibilityC[-1][1]
        if flexibilityT[-1][1] < -10 and flexibilityT[-1][
                1] < _ifYesJumpT_:  # The Threshold was set randomly, in need of more trials.
            _ifYesJumpT_ = flexibilityT[-1][1]
        if fdFunctionC(e) - fdFunctionT(e) > _diffOfCT_:
            _diffOfCT_ = fdFunctionC(e) - fdFunctionT(e)
        if fdFunctionT(e) - fdFunctionC(e) > _diffOfTC_:
            _diffOfTC_ = fdFunctionT(e) - fdFunctionC(e)

        des.write(
            str(e) + '\t' + str(flexibilityC[-1][1]) + '\t' +
            str(flexibilityT[-1][1]) + '\n')

    # Calculate the average of flexibility before and after bulk
    _flexibilityValueC_ = []
    _flexibilityValueT_ = []
    _aveCF_ = flexibilityC[0][1]
    _aveTF_ = flexibilityT[0][1]
    _aveCB_ = flexibilityC[-1][1]
    _aveTB_ = flexibilityT[-1][1]
    _countCF_ = 1
    _countTF_ = 1
    _countCB_ = 1
    _countTB_ = 1
    for i in range(len(flexibilityC) - 1):
        if abs(
                flexibilityC[i + 1][1] - (_aveCF_ / _countCF_)
        ) < 3.5:  #The Threshold was set randomly, in need of more trials.
            _aveCF_ += flexibilityC[i + 1][1]
            _countCF_ += 1
        if abs(
                flexibilityC[-i - 2][1] - (_aveCB_ / _countCB_)
        ) < 3.5:  #The Threshold was set randomly, in need of more trials.
            _aveCB_ += flexibilityC[-i - 2][1]
            _countCB_ += 1
        if abs(
                flexibilityT[i + 1][1] - (_aveTF_ / _countTF_)
        ) < 3.5:  #The Threshold was set randomly, in need of more trials.
            _aveTF_ += flexibilityT[i + 1][1]
            _countTF_ += 1
        if abs(
                flexibilityT[-i - 2][1] - (_aveTB_ / _countTB_)
        ) < 3.5:  #The Threshold was set randomly, in need of more trials.
            _aveTB_ += flexibilityT[-i - 2][1]
            _countTB_ += 1
    _aveCB_ = _aveCB_ / _countCB_
    _aveCF_ = _aveCF_ / _countCF_
    _aveTB_ = _aveTB_ / _countTB_
    _aveTF_ = _aveTF_ / _countTF_

    flexibilityT = np.array(flexibilityT)
    flexibilityC = np.array(flexibilityC)

    plt.figure(figsize=(8.5, 5))
    plt.plot(flexibilityC[:, 0],
             flexibilityC[:, 1],
             label='Flexibility - Compression',
             linewidth=0.5)
    plt.plot(flexibilityT[:, 0],
             flexibilityT[:, 1],
             label='Flexibility - Tension',
             linewidth=0.5)
    plt.legend()
    plt.xlabel('Strain (%)', family='DejaVu Sans', fontsize=10, color='black')
    plt.ylabel('Flexibility (Kcal/mol/A)',
               family='DejaVu Sans',
               fontsize=10,
               color='black')
    plt.xticks(family='DejaVu Sans', fontsize=10, color='black')
    plt.yticks(family='DejaVu Sans', fontsize=10, color='black')
    plt.savefig(lab + "-Flexibility-curve.jpeg")
    plt.close()

    f = open(lab + "-features.txt", "w")
    for i in (_ifYesJumpC_, _ifYesJumpT_, _diffOfCT_, _diffOfTC_, _aveCB_,
              _aveCF_, _aveTB_, _aveTF_):
        f.write(str(i) + " ")
    f.close()

    return [
        _ifYesJumpC_, _ifYesJumpT_, _diffOfCT_, _diffOfTC_, _aveCB_, _aveCF_,
        _aveTB_, _aveTF_
    ]
def main():
    """Main routine"""
    print 'Script started'
    # Load the profiles
    runs = ['equilibrium', 'aggradation']
    for run in runs:
        # Choose source path
        if run=='equilibrium':
            os.chdir(eq_in_path)            
        else:
            os.chdir(ag_in_path)
        # Get the pickles
        # first sonar
        f = run + '_sonars.pickle'
        sonars = load_sonars(f)
        # then general parameters
        f1 = os.path.join(gp_source, run, run + '_global_stats_summary.pickle')
        gp = load_sonars(f1)
        # Create a storage dictionary
        d = {}
        probability = {}
        # Choose the output path
        if run=='equilibrium':
            os.chdir(eq_out_path)
            pickle_path = eq_in_path
        else:
            os.chdir(ag_out_path)
            pickle_path = ag_in_path
        # Aggregate the data by flowrate then feedrate instead of by
        # filename. This removes the date field stored in the `sonars`
        # dictionary. Start by creating a new dictionary
        data = {}  # Different for equilibrium and aggradation runs.
        # New dictionary as dictionary key to store fluctuation data
        fluctuations = {} #Gets rewritten at every run. Is this expected?

        for run in sorted(sonars, key=NumericalSort):
            # Give sensible names to the data
            t = sonars[run]['RunSeconds'] / (60. * 60.)
            T = sonars[run]['ContinuousSec'] / (60. * 60.)
            probe_1 = sonars[run]['Probe_1']
            probe_2 = sonars[run]['Probe_2']
            probe_3 = sonars[run]['Probe_3']
            probe_4 = sonars[run]['Probe_4']
            probe_5 = sonars[run]['Probe_5']
            probe_6 = sonars[run]['Probe_6']
            
            # Get the feedrate information for the run we are working on
            feedrate = run.split('-')[0]
            flowrate = run.split('-')[2][:2]
            runtype = run.split('-')[-1]

            # Store non-detrended data
            data.setdefault(flowrate, {}).setdefault(feedrate, {})
            data[flowrate][feedrate] = {'t':t, 'T': T, 'probe_1':probe_1,
                                        'probe_2':probe_2, 'probe_3':probe_3,
                                        'probe_4':probe_4, 'probe_5':probe_5,
                                        'probe_6':probe_6}  
            # Store detrended data
            fluctuations.setdefault(flowrate, {}).setdefault(feedrate, {})

            # Detrending procedure, given instantanues values in data dict.
            for key, value in data[flowrate][feedrate].items():
                # Ensure that time starts at zero
                if key == 't':
                    fluctuations[flowrate][feedrate].setdefault('t', t - t[0])
                elif key == 'T':
                    fluctuations[flowrate][feedrate].setdefault('T', T - T[0])
                else:
                    # Find the trend. Assume very low slope for equilibrium
                    # This allows for the same procedure in aggradation
                    # index of np.nan values in value-array:
                    idx = np.isfinite(value)
                    b_bed_fit, m_bed_fit = P.polyfit((T-T[0])[idx], value[idx],
                                                     1)
                    bed_fit = (T-T[0]) * m_bed_fit + b_bed_fit
                    bed_equation = r'$\eta = {:.4f}x + {:.3f}$'.format(m_bed_fit, b_bed_fit)
                    print bed_equation
                    mu = np.nanmean(value)
                    # Detrend to get fluctuations around the mean.
                    # Uncomment next line for fluctuation around zero
                    #fluctuations[feedrate][key] = value - mu
                    # Uncomment next time for fluctuation around a trend.
                    fluctuations[flowrate][feedrate][key] = value - bed_fit
            # Compute the histogram of the fluctuations
            histograms = {}
            histograms.setdefault(flowrate, {}).setdefault(feedrate, {})
            # A container to collect all values computed by this plotter
            d.setdefault(flowrate, {}).setdefault(feedrate, {})
            probability.setdefault(flowrate, {}).setdefault(feedrate, {})
            for key, value in fluctuations[flowrate][feedrate].items():
                if key == 't':
                    d[flowrate][feedrate].setdefault('t', t-t[0])
                elif key == 'T':
                    d[flowrate][feedrate].setdefault('T', T-T[0])                    
                else:
                    # Establish bounds for the bins and number of bins. This
                    # comes out to about 5 mm per bin.
                    idx = np.isfinite(value)
                    bins = np.linspace(-50, 50, num=21)
                    hist, bin_edges = np.histogram(value, bins=bins,
                                                   density=False )
                    n = np.nansum(hist)
                    pe = hist / (n + 1.)
                    Pe = 1 - pe.cumsum()
                    # Zero out values outside of the bin-bounds. This shouldn't
                    # be necessary after the sonar data is cleaned. We live
                    # with it for now. EDIT: Sonar data has been cleaned.
                    value[value[idx]<-50], value[value[idx]>50] = 0., 0. 
                    pdb.set_trace()
                    variance = np.nansum( value ** 2. ) / ( n - 1. )
                    sigma = np.sqrt(variance)
                    y = ( bin_edges[0:-1] + bin_edges[1:] ) / 2.
                    histograms[flowrate][feedrate][key] = {'hist':hist,
                                                           'bin_edges':bin_edges,
                                                           'pe':pe, 'Pe':Pe,
                                                           'variance':variance,
                                                           'sigma':sigma,
                                                           'n':n, 'y':y}
                    d[flowrate][feedrate].setdefault(key, {})
                    d[flowrate][feedrate][key] = histograms[flowrate][feedrate][key]

        #     plot_timeseries(fluctuations[flowrate][feedrate],
        #                     histograms[flowrate][feedrate], run,
        #                     'fluctuations')    
        #     # Exits the loop for all feedrates
        # plot_sigma(gp, d)
        # plot_pdf_comparison(d, runtype)


    print 'Script completed successfully'
    return
Example #47
0
def plot_nTtau_time(dataset='Webster',
                    add_ITER=False,
                    make_fit=True,
                    fname_plot=''):
    #{{{
    """
    Plot n*T*tau as a function of time for a chosen dataset.

    Parameters
    ----------
    dataset: str
        Possible values are 'Webster', 'Ikeda'.
    add_ITER: bool, optional
        If set, an ITER datapoint is added.
    make_fit: bool, optional
        Is set to true as default value, will perform a fit to the datapoints
        (excluding ITER).
    fname_plot: str, optional
        If set, plot is written into a file.

    Returns
    -------
    """

    # get the data and scale it where necessary
    data = get_dataset(dataset=dataset)
    name_vals = extract_data(data, 'name')
    year_vals = extract_data(data, 'year')
    nTtau_vals = extract_data(data, 'nTtau')

    # prepare plot
    fig = plt.figure(figsize=(8, 6))  # (width, height) in inches
    ax1 = fig.add_subplot(1, 1, 1)  # rows, cols, plotnumber

    # optionally, perform and plot a linear fit to log(nTtau) dataset
    # do this without the ITER datapoint
    if make_fit:
        # perform a linear fit to the log(nTtau) data
        x_new = np.linspace(np.min(year_vals), np.max(year_vals), 100)
        coefs = poly.polyfit(year_vals, np.log10(nTtau_vals), 1)
        fit = poly.polyval(x_new, coefs)
        # calculate the doubling time
        t_double = np.log10(2) / coefs[1]
        print('fit performed to y  =  a0 + a1*x, a0={}, a1={}'.format(
            coefs[0], coefs[1]))
        print('    where  y  --> log10(nTtau)')
        print('           a0 --> log10(nTtau)_0')
        print('           a1 --> log10(a)')
        print('           x  --> t')
        print('    and  (nTtau) = (nTtau)_0 * a^t ')
        print('         --> exponential growth')
        print('    doubling time: 2*y_0 = y0*a^t ==> 2=a^t')
        print(
            '                                  ==> t=log(2)/log(a)={}'.format(
                t_double))
        print('                   --> {} years'.format(
            np.round(t_double, decimals=1)))

        # plot fit to data
        ax1.plot(x_new, 10**(fit))
        # annotate fit
        ax1.annotate('{0:3.1f} years doubling rate (fit to data)'.format(
            np.round(t_double, decimals=1)),
                     xy=(x_new[20], 10**(fit[20])),
                     xytext=(1976, .6 * nTtau_vals[1]),
                     arrowprops=dict(arrowstyle="->", shrinkA=0, color='0.2'),
                     ha='left',
                     va='top')

    # optionally, add ITER point
    if add_ITER:
        nTtau_vals = np.append(nTtau_vals, 4e21 * 1e-19)
        year_vals = np.append(year_vals, 2035)
        name_vals = np.append(name_vals, 'ITER')

    # plot nTtau as a function time dataset
    ax1.plot(year_vals, nTtau_vals, 'o', markersize=10)

    # plot format stuff
    ax1.set_xlabel("Year", fontsize=16)
    ax1.set_ylabel(r"$nT_i\tau_E$ in ($10^{19}\,$keVm$^{-3}$s)", fontsize=16)
    ax1.set_ylim(1e-3, 1e3)
    ax1.set_yscale('log')
    ax1.xaxis.set_major_formatter(FormatStrFormatter('%4d'))
    ax1.tick_params(axis='both',
                    which='both',
                    direction='in',
                    labelsize=14,
                    top='on',
                    right='on')

    # add annotations to datapoints in a very unefficient (annoying) way
    # changing the DPI of the image requires to adjust the pixel-offset values
    # this is realized by the factor dpi_scale, 1 corresponds to dpi=100
    if len(fname_plot) == 0:
        dpi_scale = 1
    else:
        dpi_scale = 5
    # loop through dataset
    for ii in range(name_vals.shape[0]):
        # define list for cases which cannot be annotated automatically
        extra_labels = [
            'Alcator A', 'Alcator C', 'JT60U', 'JT-60U', 'JET', 'ITER'
        ]
        # automatic annotation
        if name_vals[ii] not in extra_labels:
            if add_ITER:
                ax1.annotate(
                    name_vals[ii],
                    xy=(year_vals[ii], nTtau_vals[ii]),
                    xytext=(10 * dpi_scale, -5 * dpi_scale),
                    textcoords='offset pixels',
                )
            else:
                ax1.annotate(
                    name_vals[ii],
                    xy=(year_vals[ii], nTtau_vals[ii]),
                    xytext=(10 * dpi_scale, -5 * dpi_scale),
                    textcoords='offset pixels',
                )
        # handles the above defined extra cases
        elif name_vals[ii] == 'Alcator A' or name_vals[ii] == 'Alcator C':
            if add_ITER:
                ax1.annotate(
                    name_vals[ii],
                    xy=(year_vals[ii], nTtau_vals[ii]),
                    xytext=(-85 * dpi_scale, -6 * dpi_scale),
                    textcoords='offset pixels',
                )
            else:
                ax1.annotate(
                    name_vals[ii],
                    xy=(year_vals[ii], nTtau_vals[ii]),
                    xytext=(-90 * dpi_scale, -6 * dpi_scale),
                    textcoords='offset pixels',
                )
        elif (name_vals[ii] == 'JT60U') or (name_vals[ii] == 'JT-60U'):
            # write left of symbol
            if (year_vals[ii] < 1990) or (year_vals[ii] > 1991
                                          and year_vals[ii] < 1995):
                if add_ITER:
                    ax1.annotate(
                        name_vals[ii],
                        xy=(year_vals[ii], nTtau_vals[ii]),
                        xytext=(-65 * dpi_scale, -6 * dpi_scale),
                        textcoords='offset pixels',
                    )
                else:
                    ax1.annotate(
                        name_vals[ii],
                        xy=(year_vals[ii], nTtau_vals[ii]),
                        xytext=(-64 * dpi_scale, -6 * dpi_scale),
                        textcoords='offset pixels',
                    )
            # label above symbol
            elif year_vals[ii] > 1995:
                ax1.annotate(
                    name_vals[ii],
                    xy=(year_vals[ii], nTtau_vals[ii]),
                    xytext=(-20 * dpi_scale, 7 * dpi_scale),
                    textcoords='offset pixels',
                )
            # default case (label right of symbol)
            else:
                if add_ITER:
                    ax1.annotate(
                        name_vals[ii],
                        xy=(year_vals[ii], nTtau_vals[ii]),
                        xytext=(12 * dpi_scale, -5 * dpi_scale),
                        textcoords='offset pixels',
                    )
                else:
                    ax1.annotate(
                        name_vals[ii],
                        xy=(year_vals[ii], nTtau_vals[ii]),
                        xytext=(10 * dpi_scale, -5 * dpi_scale),
                        textcoords='offset pixels',
                    )
        elif name_vals[ii] == 'JET':
            if year_vals[ii] > 1995:
                ax1.annotate(
                    name_vals[ii],
                    xy=(year_vals[ii], nTtau_vals[ii]),
                    xytext=(-6 * dpi_scale, 9 * dpi_scale),
                    textcoords='offset pixels',
                )
            else:
                if add_ITER:
                    ax1.annotate(
                        name_vals[ii],
                        xy=(year_vals[ii], nTtau_vals[ii]),
                        xytext=(12 * dpi_scale, -5 * dpi_scale),
                        textcoords='offset pixels',
                    )
                else:
                    ax1.annotate(
                        name_vals[ii],
                        xy=(year_vals[ii], nTtau_vals[ii]),
                        xytext=(10 * dpi_scale, -5 * dpi_scale),
                        textcoords='offset pixels',
                    )
        elif name_vals[ii] == 'ITER':
            ax1.annotate(
                name_vals[ii],
                xy=(year_vals[ii], nTtau_vals[ii]),
                xytext=(-48 * dpi_scale, -6 * dpi_scale),
                textcoords='offset pixels',
            )

    fig.text(.703, .885, credit_str, fontsize=7)

    make_plot(fname_plot)
Example #48
0
def xr_rm_poly(ds, order, dim='time'):
    """Returns xarray object with nth-order fit removed.

    Args:
        ds (xarray object): Time series to be detrended.
        order (int): Order of polynomial fit to be removed.
        dim (optional str): Dimension over which to remove the polynomial fit.

    Returns:
        xarray object with polynomial fit removed.
    """
    check_xarray(ds)  # this could be a decorator I think?

    if dim not in ds.dims:
        raise KeyError(f"Input dim, '{dim}', was not found in the ds; "
                       f"found only the following dims: {list(ds.dims)}.")

    # handle both datasets and dataarray
    if isinstance(ds, xr.Dataset):
        da = ds.to_array()
        return_ds = True
    else:
        da = ds.copy()
        return_ds = False

    da_dims_orig = list(da.dims)  # orig -> original
    if len(da_dims_orig) > 1:
        # want independent axis to be the leading dimension
        da_dims_swap = da_dims_orig.copy()  # copy to prevent contamination

        # https://stackoverflow.com/questions/1014523/
        # simple-syntax-for-bringing-a-list-element-to-the-front-in-python
        da_dims_swap.insert(0, da_dims_swap.pop(da_dims_swap.index(dim)))
        da = da.transpose(*da_dims_swap)

        # hide other dims into a single dim
        da = da.stack({'other_dims': da_dims_swap[1:]})
        dims_swapped = True
    else:
        dims_swapped = False

    # NaNs will make the polyfit fail--interpolate any NaNs in
    # the provided dim to prevent poor fit, while other dims' NaNs
    # will be filled with 0s; however, all NaNs will be replaced
    # in the final output
    nan_locs = np.isnan(da.values)

    # any(nan_locs.sum(axis=0)) fails if not 2D
    if nan_locs.ndim == 1:
        nan_locs = nan_locs.reshape(len(nan_locs), 1)
        nan_reshaped = True
    else:
        nan_reshaped = False

    # check if there's any NaNs in the provided dim because
    # interpolate_na is computationally expensive to run regardless of NaNs
    if any(nan_locs.sum(axis=0)) > 0:
        if any(nan_locs[0, :]):
            # [np.nan, 1, 2], no first value to interpolate from; back fill
            da = da.bfill(dim)
        elif any(nan_locs[-1, :]):
            # [0, 1, np.nan], no last value to interpolate from; forward fill
            da = da.ffill(dim)
        else:  # [0, np.nan, 2], can interpolate
            da = da.interpolate_na(dim)

    # this handles the other axes; doesn't matter since it won't affect the fit
    da = da.fillna(0)

    # the actual operation of detrending
    y = da.values
    x = np.arange(0, len(y), 1)
    coefs = poly.polyfit(x, y, order)
    fit = poly.polyval(x, coefs)
    y_dt = y - fit.transpose()  # dt -> detrended
    da.data[:] = y_dt

    # replace back the filled NaNs (keep values where not NaN)
    if nan_reshaped:
        nan_locs = nan_locs[:, 0]
    da = da.where(~nan_locs)

    if dims_swapped:
        # revert the other dimensions to its original form and ordering
        da = da.unstack('other_dims').transpose(*da_dims_orig)

    if return_ds:
        # revert back into a dataset
        return xr.merge(
            da.sel(variable=var).rename(var).drop('variable')
            for var in da['variable'].values)
    else:
        return da
def CCurve(L, W, T, data, group, freq):

    # Arrays
    time, dis, forceSignal, force, segments, stress, strain = [], [], [], [], [], [], []
    area = W * T

    for row in data:
        time.append(row[1])
        dis.append(row[4])
        force.append(row[3])
        segments.append(row[5])
        stress.append(row[3] / area)
        strain.append(((row[4])) / L)

    # What data do we want to work with
    xac = time
    yac = stress
    zac = strain
    # plotting the main values
    xxac = xac[len(xac) - 150:len(xac)]
    yyac = yac[len(yac) - 150:len(yac)]
    zzac = zac[len(zac) - 150:len(zac)]

    mean_stress_ac = (min(stress) + max(stress)) / 2
    mean_strain_ac = (min(strain) + max(strain)) / 2
    amp_stress_ac = (min(stress) - max(stress)) / 2
    amp_strain_ac = (min(strain) - max(strain)) / 2

    # Normalizing data to range 0-1
    x, y, z = [], [], []
    for i in range(0, len(time)):
        x.append((time[i] - min(time)) / (max(time) - min(time)))
    for i in range(0, len(force)):
        y.append((force[i] - min(force)) / (max(force) - min(force)))
    for i in range(0, len(dis)):
        z.append((dis[i] - min(dis)) / (max(dis) - min(dis)))

    # plotting the main values
    xx = x[len(x) - 150:len(x)]
    yy = y[len(y) - 150:len(y)]
    zz = z[len(z) - 150:len(z)]

    # Calculating the phase angle difference between stress and strain
    seg1 = segments.index(
        min(segments) +
        tweak1)  # Tweek the numbers for better position on the curve
    seg2 = segments.index(
        min(segments) +
        tweak2)  # Tweek the numbers for better position on the curve

    xxx = x[seg1:seg2]
    yyy = y[seg1:seg2]
    zzz = z[seg1:seg2]

    mean_stress = (min(yyy) + max(yyy)) / 2
    mean_strain = (min(zzz) + max(zzz)) / 2

    xb = np.asarray(xx)
    yb = np.asarray(yy)

    coeff, stats = P.polyfit(xb, yb, 9, full=True)
    fitpoly = P.Polynomial(coeff)

    fitfunc = lambda x, a, b: a * np.sin(b * x)
    p, pcov = curve_fit(fitfunc, xb, yb, p0=[1.0, 1.0])

    yi = []
    for i in range(0, len(xx)):
        yi.append(p[0] * np.sin(p[1] * xx[i] * 15 * 3) + .5)

    plt.plot(xb, yb, color='r')
    plt.plot(xx, yi, color='b')

    plt.show()
Example #50
0
def _np_rm_poly(x, y, order):
    coefs = poly.polyfit(x, y, order)
    fit = poly.polyval(x, coefs)
    return y - fit
#-------------------------------------------------------------------------------
# Compute the iterative approximation
#-------------------------------------------------------------------------------
for iter_i in range(max_iter):

    #--- Initial or iterative parametrization
    if iter_i == 0:
        #u = uniform_param(P)
        #u = chordlength_param(P)
        u = centripetal_param(P)
    else:
        u = iterative_param(P, u, fxcoeff, fycoeff, fzcoeff, ax[3])

    #--- Compute polynomial approximations and get their coefficients
    fxcoeff = pl.polyfit(u, P[:, 0], polydeg, w=w)
    fycoeff = pl.polyfit(u, P[:, 1], polydeg, w=w)
    fzcoeff = pl.polyfit(u, P[:, 2], polydeg, w=w)

    #--- Calculate function values f(u)=(fx(u),fy(u),fz(u))
    f_u[:, 0] = pl.polyval(u, fxcoeff)
    f_u[:, 1] = pl.polyval(u, fycoeff)
    f_u[:, 2] = pl.polyval(u, fzcoeff)

    #--- Calculate fine values for ploting
    f_uu[:, 0] = pl.polyval(uu, fxcoeff)
    f_uu[:, 1] = pl.polyval(uu, fycoeff)
    f_uu[:, 2] = pl.polyval(uu, fzcoeff)

    #--- Print plots
    hp = ax[0].plot(f_uu[:, 0],
Example #52
0
def polyfit(x, y, deg, rcond=None, full=False, w=None):
    from numpy.polynomial.polynomial import polyfit
    return polyfit(x, y, deg, rcond, full, w)
    #    print(bg_subtracted_err[k])
    err_count_per_img[k] = np.sqrt(sum(bg_subtracted_err[k]**2) / n_refstar)
    print('... measurement error after background subtraction (counts):',
          '%.4f' % err_count_per_img[k])
    err_mag_instrument_per_img[k] = np.sqrt(
        sum(mag_instrument_err[k]**2) / n_refstar)
    print('... magnitude error after background subtraction (mag):',
          '%.4f' % err_mag_instrument_per_img[k])

    print('Instrument Mag of FU_Ori', mag_instrument[k][0])
    #    print('Instrument Mag of RefStars',mag_instrument[k][1:])
    print()

    #    shift,slope=polyfit(mag_instrument[k][1:],rmag[1:],1)
    shift, slope = polyfit(mag_instrument[k][1:],
                           rmag[1:],
                           1,
                           w=1 / (mag_instrument_err[k][1:]))
    #    shift,slope=polyfit(mag_instrument[k][1:],rmag[1:],1)
    param, res = polyfit(mag_instrument[k][1:],
                         rmag[1:],
                         1,
                         w=1 / (mag_instrument_err[k][1:]),
                         full=True)
    #    param,res=polyfit(mag_instrument[k][1:],rmag[1:],1,full=True)
    res_least_sq_fit = res[0]
    print('... residual least squre fitting', res_least_sq_fit)

    rmag_fitting[k] = shift + slope * mag_instrument[k]
    print('... rmag_fitting (Ref.Stars only):', rmag_fitting[k][1:])
    print('... rmag_fitting (Target only):', rmag_fitting[k][0])
def weighted_local_regression(xpts, ypts, dists_from_p0, weighting_H):
    weights = np.exp(-dists_from_p0**2 / (0.5 * weighting_H**2)) 
    # weights will be squared, hence factor of 0.5
    return polynomial.polyfit(xpts, ypts, deg = 1, w = weights)
ax.annotate(compass_df['Borough'].iloc[32],
            (compass_df['Count'].iloc[32], compass_df['Income'].iloc[32]))  # Westminster

ax.annotate(compass_df['Borough'].iloc[19],
            (compass_df['Count'].iloc[19], compass_df['Income'].iloc[19]))


# ax1 = count_df_no_outliers.plot.scatter(x='Count', y='Income')
# ax1 = ax1.set_ylim(bottom=0)

# ax2 = count_df.plot.scatter(x='Count', y='Income')

x = compass_df['Count'].astype('float')
y = compass_df['Income'].astype('float')

b, m = polyfit(x, y, 1)
plt.plot(x, b + m * x, '-')

ax.legend()

count_df_no_outliers = count_df

count_df_no_outliers['Income'] = count_df_no_outliers['Income'][count_df_no_outliers['Income'].between(
    count_df_no_outliers['Income'].quantile(.15), count_df_no_outliers['Income'].quantile(.85))]

count_df_no_outliers = count_df_no_outliers.dropna()
# print(count_df_no_outliers)

# ax1 = count_df_no_outliers.plot.scatter(x='Count', y='Income')
# ax1 = ax1.set_ylim(bottom=0)
# # ax2 = count_df.plot.bar(x='Count', y='Income')
Example #56
0
def polynom_best_fit(xData, yData):
    global polynom_params
    AICS = []
    BICS = []
    polynom_params = []
    global best
    for i in [1, 2, 3, 4, 5]:
        params = []

        params = poly.polyfit(xData, yData, i, w=weight)
        #Finds the fitting parameters for the iterable.
        if len(params) == 2:
            model_p1 = p1(xData, *params)
            if (flag_BIC == 1):
                BICS.append(compute_BIC(yData, model_p1, 2))
            if (flag_AIC == 1):
                AICS.append(compute_AIC(yData, model_p1, 2))
        if len(params) == 3:
            model_p2 = p2(xData, *params)
            if (flag_BIC == 1):
                BICS.append(compute_BIC(yData, model_p1, 3))
            if (flag_AIC == 1):
                AICS.append(compute_AIC(yData, model_p1, 3))
        if len(params) == 4:
            model_p3 = p3(xData, *params)
            if (flag_BIC == 1):
                BICS.append(compute_BIC(yData, model_p1, 4))
            if (flag_AIC == 1):
                AICS.append(compute_AIC(yData, model_p1, 4))
        if len(params) == 5:
            model_p4 = p4(xData, *params)
            if (flag_BIC == 1):
                BICS.append(compute_BIC(yData, model_p1, 5))
            if (flag_AIC == 1):
                AICS.append(compute_AIC(yData, model_p1, 5))
        if len(params) == 6:
            model_p5 = p5(xData, *params)
            if (flag_BIC == 1):
                BICS.append(compute_BIC(yData, model_p1, 6))
            if (flag_AIC == 1):
                AICS.append(compute_AIC(yData, model_p1, 6))
        else:
            continue
        #Computes the AIC/BIC of each polynomial fit by looking at the length of the 'params' array.
        #Note that the dimension of a vector containing the parameters for a polynomial is always n+1 where n is the
        #highest degree of the polynomial, so they can be uniquely selected this way.
        def AIC_BIC_choose():
            if (flag_BIC == 1):
                best = np.where(BICS == min(BICS))
                return best
            if (flag_AIC == 1):
                best = np.where(AICS == min(AICS))
                return best

        best = AIC_BIC_choose()
        #Finds the lowest AIC or BIC of the data.
        best_model = []
        if best[0][0] == 0:
            if (flag_BIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 1, w=weight)))
                print('First degree best fit')
                print('with BIC =', min(BICS))

                return min(BICS)
            if (flag_AIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 1, w=weight)))
                print('First degree best fit')
                print('with AIC =', min(AICS))

                return min(AICS)
        if best[0][0] == 1:
            if (flag_BIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 2, w=weight)))
                print('Second degree best fit')
                print('with BIC =', min(BICS))

                return min(BICS)
            if (flag_AIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 2, w=weight)))
                print('Second degree best fit')
                print('with AIC =', min(AICS))

                return min(AICS)

        if best[0][0] == 2:
            if (flag_BIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 3, w=weight)))
                print('Third degree best fit')
                print('with BIC =', min(BICS))

                return min(BICS)
            if (flag_AIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 3, w=weight)))
                print('Third degree best fit')
                print('with AIC =', min(AICS))

                return min(AICS)
        if best[0][0] == 3:
            if (flag_BIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 4, w=weight)))
                print('Fourth degree best fit')
                print('with BIC =', min(BICS))

                return min(BICS)
            if (flag_AIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 4, w=weight)))
                print('Fourth degree best fit')
                print('with AIC =', min(AICS))

                return min(AICS)
        if best[0][0] == 4:
            if (flag_BIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 5, w=weight)))
                print('Fifth degree best fit')
                print('with BIC =', min(BICS))

                return min(BICS)
            if (flag_AIC == 1):
                polynom_params.append(
                    tuple(poly.polyfit(xData, yData, 5, w=weight)))
                print('Fifth degree best fit')
                print('with AIC =', min(AICS))

                return min(AICS)
Example #57
0
def regressAllJointPositions(order):
    global _trajectorySteering
    global _trajectoryShoulder0Right
    global _trajectoryShoulder1Right
    global _trajectoryShoulder2Right
    global _trajectoryShoulder0Left
    global _trajectoryShoulder1Left
    global _trajectoryShoulder2Left
    global _trajectoryElbow0Right
    global _trajectoryElbow1Right
    global _trajectoryElbow0Left
    global _trajectoryElbow1Left
    global _trajectoryWrist0Right
    global _trajectoryWrist1Right
    global _trajectoryWrist0Left
    global _trajectoryWrist1Left

    global _regressedShoulder0Right
    global _regressedShoulder1Right
    global _regressedShoulder2Right
    global _regressedShoulder0Left
    global _regressedShoulder1Left
    global _regressedShoulder2Left
    global _regressedElbow0Right
    global _regressedElbow1Right
    global _regressedElbow0Left
    global _regressedElbow1Left
    global _regressedWrist0Right
    global _regressedWrist1Right
    global _regressedWrist0Left
    global _regressedWrist1Left

    _regressedShoulder0Right = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryShoulder0Right, order))
    _regressedShoulder1Right = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryShoulder1Right, order))
    _regressedShoulder2Right = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryShoulder2Right, order))
    _regressedElbow0Right = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryElbow0Right, order))
    _regressedElbow1Right = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryElbow1Right, order))
    _regressedWrist0Right = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryWrist0Right, order))
    _regressedWrist1Right = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryWrist1Right, order))

    _regressedShoulder0Left = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryShoulder0Left, order))
    _regressedShoulder1Left = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryShoulder1Left, order))
    _regressedShoulder2Left = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryShoulder2Left, order))
    _regressedElbow0Left = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryElbow0Left, order))
    _regressedElbow1Left = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryElbow1Left, order))
    _regressedWrist0Left = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryWrist0Left, order))
    _regressedWrist1Left = poly.Polynomial(
        poly.polyfit(_trajectorySteering, _trajectoryWrist1Left, order))

    return 1
Example #58
0
def saveRegressionToFile(filename, order):
    regressionCoefficientsDict = {
        JOINT_SHOULDER_AXIS0_RIGHT:
        poly.polyfit(_trajectorySteering, _trajectoryShoulder0Right,
                     order).tolist(),
        JOINT_SHOULDER_AXIS1_RIGHT:
        poly.polyfit(_trajectorySteering, _trajectoryShoulder1Right,
                     order).tolist(),
        JOINT_SHOULDER_AXIS2_RIGHT:
        poly.polyfit(_trajectorySteering, _trajectoryShoulder2Right,
                     order).tolist(),
        JOINT_ELBOW_ROT0_RIGHT:
        poly.polyfit(_trajectorySteering, _trajectoryElbow0Right,
                     order).tolist(),
        JOINT_ELBOW_ROT1_RIGHT:
        poly.polyfit(_trajectorySteering, _trajectoryElbow1Right,
                     order).tolist(),
        JOINT_WRIST_0_RIGHT:
        poly.polyfit(_trajectorySteering, _trajectoryWrist0Right,
                     order).tolist(),
        JOINT_WRIST_1_RIGHT:
        poly.polyfit(_trajectorySteering, _trajectoryWrist1Right,
                     order).tolist(),
        JOINT_SHOULDER_AXIS0_LEFT:
        poly.polyfit(_trajectorySteering, _trajectoryShoulder0Left,
                     order).tolist(),
        JOINT_SHOULDER_AXIS1_LEFT:
        poly.polyfit(_trajectorySteering, _trajectoryShoulder1Left,
                     order).tolist(),
        JOINT_SHOULDER_AXIS2_LEFT:
        poly.polyfit(_trajectorySteering, _trajectoryShoulder2Left,
                     order).tolist(),
        JOINT_ELBOW_ROT0_LEFT:
        poly.polyfit(_trajectorySteering, _trajectoryElbow0Left,
                     order).tolist(),
        JOINT_ELBOW_ROT1_LEFT:
        poly.polyfit(_trajectorySteering, _trajectoryElbow1Left,
                     order).tolist(),
        JOINT_WRIST_0_LEFT:
        poly.polyfit(_trajectorySteering, _trajectoryWrist0Left,
                     order).tolist(),
        JOINT_WRIST_1_LEFT:
        poly.polyfit(_trajectorySteering, _trajectoryWrist1Left,
                     order).tolist()
    }

    with open(filename, "w") as write_file:
        json.dump(regressionCoefficientsDict,
                  write_file,
                  indent=4,
                  sort_keys=True)
        print("Saved regression coefficients in file ", filename)
Example #59
0
hand = loaddata(filename, cutoff)
q2 = hand[0]
ge = hand[1]
dge = hand[2]
wge = 1 / dge
print("Loaded ", len(q2), " points from", filename, ".")

filename = "Carlson.csv"
carlson = loaddata(filename, cutoff)
nq2 = carlson[0]
nge = carlson[1]
ndge = carlson[2]
nwge = 1 / ndge
print("Loaded ", len(nq2), " points from", filename, ".")

p2, p2stat = poly.polyfit(q2, ge, 2, full=True, w=wge)
np2, np2stat = poly.polyfit(nq2, nge, 2, full=True, w=nwge)

ffit2 = poly.Polynomial(p2)
nffit2 = poly.Polynomial(np2)

#
# Do The Hand Paper Fits
#

popt1, pcov1 = curve_fit(func1, q2, ge, sigma=dge)
print("1st Fit Parameters", popt1)
#Derived Chi Squared Value For This Model
chi_squared = np.sum(((func1(q2, *popt1) - ge) / dge)**2)
reduced_chi_squared = chi_squared / (len(q2) - len(popt1))
print("with a chi2 of {0:.3f} ".format(chi_squared))
Example #60
0
input.close()

x = []
y = []
x_filter = []
y_filter = []

for score in decoy_dic:
    x.append(score)
    frac = float(decoy_dic[score][0] / decoy_dic[score][1])
    y.append(frac)
    if 6 < score < 10:
        x_filter.append(score)
        y_filter.append(frac)

coefs = poly.polyfit(np.array(x_filter), np.array(y_filter), 1)
print("coefficients", coefs)
fit = poly.polyval(x, coefs)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y, label="Real data", s=1)
ax.plot(x, fit, label="Polynomial with order=1", color='C1')
ax.legend()
plt.xlabel('-log10(SpecEValue)')
plt.ylabel('Gamma (class specific decoy hits / total decoy hits)')
fig.savefig("fitcurve.png")

input2 = open(input_file, 'r')

for line in input2: