예제 #1
0
파일: eos.py 프로젝트: fraserphysics/metfie
    def isentrope(self, S):
        if self.S == S:
            return
        self.S = S

        # Make array of v values.  To be independent variable for ode
        # and splines.
        dv = (self.v_max - self.v_min)/1000
        v = np.arange(self.v_min-dv, self.v_max+1.5*dv, dv)
        # Allocate array for dependent variables
        PE = np.empty((2,len(v)))
        
        # Find initial state variables for ode
        v_1 = 1/self.rhoCJ
        E_1 = self.E_0*np.exp((S-self.S_0)/self.cv)
        P_1 = self.Ev2P(E_1, v_1)
        i_1 = np.searchsorted(v,[v_1])[0] # v[i_1-1] < v_1 < v[i_1]

        self.ode.set_initial_value([P_1,E_1],v_1)
        for i in range(i_1,len(v)):
            self.ode.integrate(v[i])
            PE[:,i] = self.ode.y
        self.ode.set_initial_value([P_1,E_1],v_1)
        for i in range(i_1-1,-1,-1):
            self.ode.integrate(v[i])
            PE[:,i] = self.ode.y
        from scipy.interpolate import InterpolatedUnivariateSpline as spline
        self.isentrope_v2E = spline(v,PE[1],k=3,s=0)
        self.isentrope_E2v = spline(PE[1,-1::-1],v)
        self.isentrope_P2v = spline(PE[0,-1::-1],v)
        return
예제 #2
0
def draw_CV_on_window_center():
    x_ecg = np.array([-0.415263158, -0.336315789, -0.257368421, -0.178421053, -0.099473684, -0.035957895, -0.020526316, -0.015784211, 0.0, 0.036094737, 0.059157895, 0.069473684, 0.103842105, 0.188842105, 0.249368421, 0.373315789, 0.442473684, 0.468421053, 0.531842105, 0.576526316, 0.584736842, 0.648421053])
    y_ecg = np.array([-1414, -1738, -333, -1846, -981, 2585, 11990, 14476, 22367, 16746, 5720, -4333, -10386, -2819, -3143, -766, 3342, 3882, 1504, -1954, -2170, -2063])
    x_ecg_new = np.linspace(np.min(x_ecg), np.max(x_ecg), len(x_ecg) * 5)
    y_ecg_new = spline(x_ecg, y_ecg, x_ecg_new)

    x_CV = np.array(np.linspace(-0.475, 0.525, 41))
    # y_CV = [0.8811, 0.8757, 0.8649, 0.7838, 0.6595, 0.5297, 0.6054, 0.6270, 0.6378, 0.6270, 0.6486, 0.6378, 0.6378, 0.6216, 0.5892, 0.6324, 0.7027, 0.7243, 0.7351, 0.7351, 0.7081, 0.7135, 0.6270, 0.6270, 0.6486, 0.6486, 0.6378, 0.6216, 0.6000, 0.6270, 0.7892, 0.8270, 0.8378, 0.8054, 0.7405, 0.7297, 0.6541, 0.6811, 0.8378, 0.8595]
    y_CV = np.array([0.7135, 0.627, 0.627, 0.6486, 0.6486, 0.6378, 0.6216, 0.6, 0.627, 0.7892, 0.827, 0.8378, 0.8054, 0.7405, 0.7297, 0.6541, 0.6811, 0.8378, 0.8595, 0.8811, 0.8757, 0.8649, 0.7838, 0.6595, 0.5297, 0.6054, 0.627, 0.6378, 0.627, 0.6486, 0.6378, 0.6378, 0.6216, 0.5892, 0.6324, 0.7027, 0.7243, 0.7351, 0.7351, 0.7081, 0.7135])
    x_CV_new = np.linspace(np.min(x_CV), np.max(x_CV), len(x_CV) * 5)
    y_CV_new = spline(x_CV, y_CV, x_CV_new)

    fig, ax1 = plt.subplots()
    ax1.plot(x_ecg / 6, y_ecg / float(0.8 * np.max(y_ecg)), 'k-', linewidth=2.)
    ax1.set_xlabel(u'Время, сек')
    ax1.set_ylabel(u'Интенсивность', color='k')
    for tl in ax1.get_yticklabels():
        tl.set_color('k')

    ax2 = ax1.twinx()
    # ax2.plot(x_CV_new / 6, y_CV_new, 'r-', linewidth=2.)
    ax2.plot(x_CV / 6, y_CV, 'r-', linewidth=2.)
    ax2.plot(x_CV / 6, y_CV, 'r^')
    plt.ylim([0.1, 1.0])
    ax2.set_ylabel(u'Оценка 5х5-fold кросс-валидации', color='r')
    for tl in ax2.get_yticklabels():
        tl.set_color('r')
    plt.grid(True)
    # plt.show()
    plt.savefig(r'..\..\logs\CV_on_window_center.png')
예제 #3
0
파일: fast.py 프로젝트: savila/pyfast
def prepare_mf(mpart, grid, mf_kwargs):
    M = np.linspace(np.log10(mpart), np.log10(grid.max()), 2000)
    mf_obj = MassFunction(M=M, **mf_kwargs)

    mf = mf_obj.dndm
    m_outside_range = mf_obj.mltm[0] + mf_obj.mgtm[-1]

    cumfunc = cumtrapz(10 ** M * mf, M, initial=0) * np.log(10)

    cdf = spline(M, cumfunc, k=3)
    icdf = spline(cumfunc, M, k=3)

    if MAKE_PLOTS:
        plt.clf()
        plt.plot(M, cumfunc)

        plt.plot(M, cdf(M))
        plt.savefig("cumfunc.pdf")

        plt.clf()
        mcumfunc = cumtrapz(10 ** (2 * M) * mf, dx=M[1] - M[0], initial=1e-20) * np.log(10)
        plt.plot(M, mcumfunc)
        plt.savefig("mcumfunc.pdf")

        # How much mass is above 10**12.5
        minvcumfunc = cumtrapz(10 ** (2 * M[::-1]) * mf[::-1], dx=M[1] - M[0]) * np.log(10)
        minvcumfunc = np.concatenate((np.array([minvcumfunc[0]]), minvcumfunc))
        minvcumfunc /= minvcumfunc[-1]
        plt.clf()
        plt.plot(M, minvcumfunc[::-1])
        plt.yscale('log')
        plt.grid(True)
        plt.savefig("minvcumfunc.pdf")

    return cdf, icdf, M, mf, m_outside_range
def plot_word_graph():
	global score_true,score_false
	accuracies = []
	n_pure = []
	data_size = []
	fig, ax = plt.subplots()
	axes = [ax, ax.twinx()]
	for thr in range(1500,2500):
		print thr
		mixed = []
		pure  = []
		threshold = float(thr)/1
		for similiarity_index in score_true:
			if similiarity_index > thr:
				pure.append(1)
			else:
				mixed.append(0)
		for similiarity_index in score_false:
			if similiarity_index > thr:
				pure.append(0)
			else:
				mixed.append(1)
		accuracies.append(float(pure.count(1)*100)/len(pure))
		n_pure.append(pure.count(1))
		data_size.append(len(pure))

	base = np.array([float(x)/1 for x in range(1500,2500)])
	thr = np.linspace(base.min(),base.max(),2000)
	accuracies_smooth = spline(base,accuracies,thr)
	n_pure_smooth = spline(base,n_pure,thr)
	data_size_smooth = spline(base,data_size,thr)
	axes[1].plot(thr,accuracies_smooth,'r')
	axes[0].plot(thr,n_pure_smooth,'b')
	axes[0].plot(thr,data_size_smooth,'g')
	plt.show()
예제 #5
0
파일: hmf.py 프로젝트: tbs1980/hmf
    def _upper_ngtm(self, M, mass_function, cut):
        """Calculate the mass function above given range of `M` in order to integrate"""
        ### WE CALCULATE THE MASS FUNCTION ABOVE THE COMPUTED RANGE ###
        # mass_function is logged already (not log10 though)
        m_upper = np.linspace(np.log(M[-1]), np.log(10 ** 18), 500)
        if cut:  # since its been cut, the best we can do is a power law
            mf_func = spline(np.log(M), mass_function, k=1)
            mf = mf_func(m_upper)
        else:
            # We try to calculate the hmf as far as we can normally
            new_pert = copy.deepcopy(self)
            new_pert.update(M=np.log10(np.exp(m_upper)))
            mf = np.log(np.exp(m_upper) * new_pert.dndm)

            if np.isnan(mf[-1]):  # Then we couldn't get up all the way, so have to do linear ext.
                if np.isnan(mf[1]):  # Then the whole extension is nan and we have to use the original (start at 1 because 1 val won't work either)
                    mf_func = spline(np.log(M), mass_function, k=1)
                    mf = mf_func(m_upper)
                else:
                    mfslice = mf[np.logical_not(np.isnan(mf))]
                    m_nan = m_upper[np.isnan(mf)]
                    m_true = m_upper[np.logical_not(np.isnan(mf))]
                    mf_func = spline(m_true, mfslice, k=1)
                    mf[len(mfslice):] = mf_func(m_nan)
        return m_upper, mf
예제 #6
0
파일: eam.py 프로젝트: conwayje/ase-python
    def read_adp_data(self, data, d):
        """reads in the extra data fro the adp format"""

        self.d_data = np.zeros([self.Nelements, self.Nelements, self.nr])
        # should be non symetrical combinations of 2
        for i in range(self.Nelements):
            for j in range(i, self.Nelements):
                self.d_data[i, j] = data[d:d + self.nr]
                d += self.nr
                
        self.q_data = np.zeros([self.Nelements, self.Nelements, self.nr])
        # should be non symetrical combinations of 2
        for i in range(self.Nelements):
            for j in range(i, self.Nelements):
                self.q_data[i, j] = data[d:d + self.nr]
                d += self.nr
    
        self.d = np.zeros([self.Nelements, self.Nelements], object)
        self.d_d = np.zeros([self.Nelements, self.Nelements], object)
        self.q = np.zeros([self.Nelements, self.Nelements], object)
        self.d_q = np.zeros([self.Nelements, self.Nelements], object)
    
        for i in range(self.Nelements):
            for j in range(i, self.Nelements):
                self.d[i, j] = spline(self.r[1:], self.d_data[i, j][1:], k=3)
                self.d_d[i, j] = self.deriv(self.d[i, j])
                self.q[i, j] = spline(self.r[1:], self.q_data[i, j][1:], k=3)
                self.d_q[i, j] = self.deriv(self.q[i, j])

                # make symmetrical
                if j != i:
                    self.d[j, i] = self.d[i, j]
                    self.d_d[j, i] = self.d_d[i, j]
                    self.q[j, i] = self.q[i, j]
                    self.d_q[j, i] = self.d_q[i, j]
예제 #7
0
파일: cpuinfo.py 프로젝트: bigzz/moniter
def animate(cpu):
    x = range(50)
    cpuinfo_g = cpuinfo()

    y_load_l = cpuinfo_g.load_sum_l
    y_load_b = cpuinfo_g.load_sum_b
    y_freq_l = cpuinfo_g.freq_l
    y_freq_b = cpuinfo_g.freq_b

    x_sm = np.array(x)
    y_sm_ll = np.array(y_load_l)
    y_sm_lb = np.array(y_load_b)
    y_sm_fl = np.array(y_freq_l)
    y_sm_fb = np.array(y_freq_b)

    x_smooth = np.linspace(x_sm.min(), x_sm.max(), 200)
    y_smooth_load_l = spline(x, y_sm_ll, x_smooth)
    y_smooth_load_b = spline(x, y_sm_lb, x_smooth)

    y_smooth_freq_l = spline(x, y_sm_fl, x_smooth)
    y_smooth_freq_b = spline(x, y_sm_fb, x_smooth)


    cpulock.acquire()
    load_sum_l.set_data(x_smooth, y_smooth_load_l)
    load_sum_b.set_data(x_smooth, y_smooth_load_b)

    freq_l.set_data(x_smooth, y_smooth_freq_l)
    freq_b.set_data(x_smooth, y_smooth_freq_b)
    cpulock.release()

    return load_sum_l, load_sum_b, freq_l, freq_b
예제 #8
0
    def set_splines(self):
        # this section turns the file data into three functions (and
        # derivative functions) that define the potential
        self.embedded_energy = np.empty(self.Nelements, object)
        self.electron_density = np.empty(self.Nelements, object)
        self.d_embedded_energy = np.empty(self.Nelements, object)
        self.d_electron_density = np.empty(self.Nelements, object)

        for i in range(self.Nelements):
            self.embedded_energy[i] = spline(self.rho,
                                             self.embedded_data[i], k=3)
            self.electron_density[i] = spline(self.r,
                                              self.density_data[i], k=3)
            self.d_embedded_energy[i] = self.deriv(self.embedded_energy[i])
            self.d_electron_density[i] = self.deriv(self.electron_density[i])

        self.phi = np.empty([self.Nelements, self.Nelements], object)
        self.d_phi = np.empty([self.Nelements, self.Nelements], object)

        # ignore the first point of the phi data because it is forced
        # to go through zero due to the r*phi format in alloy and adp
        for i in range(self.Nelements):
            for j in range(i, self.Nelements):
                self.phi[i, j] = spline(
                    self.r[1:],
                    self.rphi_data[i, j][1:] / self.r[1:], k=3)

                self.d_phi[i, j] = self.deriv(self.phi[i, j])

                if j != i:
                    self.phi[j, i] = self.phi[i, j]
                    self.d_phi[j, i] = self.d_phi[i, j]
예제 #9
0
파일: hmf.py 프로젝트: duducosmos/hmf
    def _lower_ngtm(self, M, mass_function, cut):
        ### WE CALCULATE THE MASS FUNCTION BELOW THE COMPUTED RANGE ###
        # mass_function is logged already (not log10 though)
        m_lower = np.linspace(np.log(10 ** 3), np.log(M[0]), 500)
        if cut:  #since its been cut, the best we can do is a power law
            mf_func = spline(np.log(M), mass_function, k=1)
            mf = mf_func(m_lower)
        else:
            #We try to calculate the hmf as far as we can normally
            sigma_0 = tools.mass_variance(np.exp(m_lower), self._power_0, self.lnkh, self.cosmo_params['mean_dens'])
            sigma = sigma_0 * self.growth
            dlnsdlnm = tools.dlnsdlnm(np.exp(m_lower), sigma_0, self._power_0, self.lnkh, self.cosmo_params['mean_dens'])
            n_eff = tools.n_eff(dlnsdlnm)
            fsigma = fits(m_lower, n_eff, self.mf_fit, sigma, self.cosmo_params['delta_c'],
                          self.z, self.delta_halo, self.cosmo_params, self.user_fit, cut_fit=True).nufnu()()
            #fsigma = nufnu()
            dndm = fsigma * self.cosmo_params['mean_dens'] * np.abs(dlnsdlnm) / np.exp(m_lower) ** 2
            mf = np.log(np.exp(m_lower) * dndm)

            if np.isnan(mf[0]):  #Then we couldn't go down all the way, so have to do linear ext.
                mfslice = mf[np.logical_not(np.isnan(mf))]
                m_nan = m_lower[np.isnan(mf)]
                m_true = m_lower[np.logical_not(np.isnan(mf))]
                mf_func = spline(m_true, mfslice, k=1)
                mf[:len(mfslice)] = mf_func(m_nan)
        return m_lower, mf
예제 #10
0
    def __init__(self, **model_parameters):
        super(Tinker08, self).__init__(**model_parameters)


        if self.delta_halo not in self.delta_virs:
            A_array = np.array([self.params["A_%s" % d] for d in self.delta_virs])
            a_array = np.array([self.params["a_%s" % d] for d in self.delta_virs])
            b_array = np.array([self.params["b_%s" % d] for d in self.delta_virs])
            c_array = np.array([self.params["c_%s" % d] for d in self.delta_virs])

            A_func = spline(self.delta_virs, A_array)
            a_func = spline(self.delta_virs, a_array)
            b_func = spline(self.delta_virs, b_array)
            c_func = spline(self.delta_virs, c_array)

            A_0 = A_func(self.delta_halo)
            a_0 = a_func(self.delta_halo)
            b_0 = b_func(self.delta_halo)
            c_0 = c_func(self.delta_halo)
        else:
            A_0 = self.params["A_%s" % (int(self.delta_halo))]
            a_0 = self.params["a_%s" % (int(self.delta_halo))]
            b_0 = self.params["b_%s" % (int(self.delta_halo))]
            c_0 = self.params["c_%s" % (int(self.delta_halo))]


        self.A = A_0 * (1 + self.z) ** (-self.params["A_exp"])
        self.a = a_0 * (1 + self.z) ** (-self.params["a_exp"])
        alpha = 10 ** (-(0.75 / np.log10(self.delta_halo / 75)) ** 1.2)
        self.b = b_0 * (1 + self.z) ** (-alpha)
        self.c = c_0
def semi_supervised_test2(df, labeled_points, step):
    # for i in range(6):
    total_points = 600
    feature, label = seperate_feature_label(df)
    accuracy_for_supervise = []
    accuracy_for_semi_supervise = []
    x = []
    indices = np.arange(len(feature))
    label_indices = balanced_sample_maker(feature, label, labeled_points / len(label))
    unlabeled_indices = np.delete(indices, np.array(label_indices))
    # print(unlabeled_indices.size)
    # print(unlabeled_indices.size)
    rng = np.random.RandomState(0)
    rng.shuffle(unlabeled_indices)
    indices = np.concatenate((label_indices, unlabeled_indices[:total_points]))
    # n_total_samples = len(indices)

    for i in range(80):
        x.append(total_points)
        unlabeled_index = np.arange(total_points)[labeled_points:]
        # print(unlabeled_index.size)
        X = feature.iloc[indices]
        y = label.iloc[indices]
        y_train = np.copy(y)
        y_train[unlabeled_index] = -1
        # supervised learning
        classifier = KNeighborsClassifier(n_neighbors=6)
        classifier.fit(X.iloc[:labeled_points], y.iloc[:labeled_points])
        y_pred = classifier.predict(X.iloc[labeled_points:])
        true_labels = y.iloc[unlabeled_index]
        # print(confusion_matrix(true_labels,y_pred))
        print("%d labeled & %d unlabeled (%d total)"
              % (labeled_points, total_points - labeled_points, total_points))
        accuracy_for_supervise.append(accuracy_score(true_labels, y_pred))

        lp_model = label_propagation.LabelSpreading(gamma=1, kernel='knn', max_iter=300, n_neighbors=6)
        lp_model.fit(X, y_train)
        predicted_labels = lp_model.transduction_[unlabeled_index]
        # print('Iteration %i %s' % (i, 70 * '_'))
        accuracy_for_semi_supervise.append(accuracy_score(true_labels, predicted_labels))
        print('Semi-supervised learning:', accuracy_score(true_labels, predicted_labels))

        total_points += step  # print(unlabeled_indices[(total_points-50):total_points])
        indices = np.concatenate((indices, unlabeled_indices[(total_points - step):total_points]))

    x_sm = np.array(x)
    y_sm = np.array(accuracy_for_supervise)
    y1_sm = np.array(accuracy_for_semi_supervise)
    x_smooth = np.linspace(x_sm.min(), x_sm.max(), 200)
    y_smooth = spline(x, y_sm, x_smooth)
    y1_smooth = spline(x, y1_sm, x_smooth)
    sup, = plt.plot(x_smooth, y_smooth, label='Supervised learning with kNN')
    semi_l, = plt.plot(x_smooth, y1_smooth, label='Semi-supervised learning using Label Propagation')
    # plt.legend(handles=[sup, semi_l])
    plt.xlabel('Total samples')
    plt.ylabel('Accuracy')
    plt.title('Semi-supervised learning for labeled ' + str(labeled_points) + ' samples ')
    plt.show()
    return accuracy_score(true_labels, predicted_labels)
예제 #12
0
파일: setups.py 프로젝트: yidapa/lfdft
    def __init__(self, symbol):
        """Atomic Setup (Pseudopotential data)
        data read from data/*_pp.dat files.
        TODO: expand documentation & cleanup
        """
        self.lref = 1

        fname = 'data/' + symbol.lower() + '_pp.dat'                
        datafile = pkg_resources.resource_stream(__name__, fname)
        self.symbol = symbol
        self.fname = fname
        
        # python doesn't like 1D-2 formating (convert to 1E-2)
        t = maketrans('D', 'E')
        data = [l.translate(t).split() for l in datafile]
        
        line=0
        self.n = int(data[line][0])
        self.dx = float(data[line][1])
        self.lmax = int(data[line][2])
        self.q = float(data[line][3])
        
        line=1 
        self.rl = np.array([float(r) for r in data[line]]) 
        self.radius = max(self.rl)
        
        m = self.n + 3
        v = np.array([[float(x) for x in line] for line in data[2:m]]).T
        u = np.array([[float(x) for x in line] for line in data[m::]]).T
        
        x = np.arange(self.n+1) * self.dx

        # create splines
        self.v_spline = {l:spline(x, v[l+1], s=0)
                         for l in xrange(self.lmax+1)}
        self.u_spline = {l:spline(x, u[l+1], s=0)
                         for l in xrange(self.lmax+1)}

        self.vnl_spline = {}
        self.psi_spline = {}

        psi = np.zeros_like(x)
        vnl = np.zeros_like(x)
            
        for l in xrange(self.lmax+1):
            psi[1::] = u[l+1,1::]/(x[1::]**(l+1)*np.sqrt((2*l+1)/(4*np.pi)))
            vnl[1::] = (v[l+1,1::]-v[self.lref+1, 1::])*psi[1::]
            psi[0] = psi[1]
            vnl[0] = vnl[1]

            self.psi_spline[l] = spline(x,psi,s=0)
            self.vnl_spline[l] = spline(x,vnl,s=0)
            

        self.L = [l for l in xrange(self.lmax+1) if l != self.lref]        
        self.LM = list(itertools.chain.from_iterable(
                range(l**2, (l+1)**2) for l in self.L))
        self.uVu = self.calculate_uVu()
예제 #13
0
    def _p(self, K, c):
        """
        The reduced dimensionless fourier-transform of the profile

        This function should not need to be called by the user in general.

        Parameters
        ----------
        K : float or array_like
            The unit-less wavenumber k*r_s

        c : float or array_like
            The concentration

        .. note :: This should be replaced by an analytic function if possible
        """
        c = np.atleast_1d(c)
        if K.ndim < 2:
            if len(K)!=len(c):
                K = np.atleast_2d(K).T # should be len(rs) x len(k)
            else:
                K = np.atleast_2d(K)
        minsteps = 100

        # if len(c)>50:
        #     C = np.linspace(c.min(),c.max(),50)
        # else:
        #     C = c
        #
        if K.size > 100:
            kk = np.logspace(np.log10(K.min()),np.log10(K.max()),100)
        else:
            kk = np.sort(np.flatten(K))

        res = np.zeros_like(K)
        intermediate_res = np.zeros((len(kk),len(c)))

        for ik, kappa in enumerate(kk):
            smallest_period = np.pi / kappa
            dx = smallest_period / 5

            nsteps = max(int(np.ceil(c.max() / dx)),minsteps)

            x, dx = np.linspace(0, c.max(), nsteps, retstep=True)
            spl = spline(x, x*self._f(x)*np.sin(kappa*x)/kappa)

            intg = spl.antiderivative()

            intermediate_res[ik,:] = intg(c) - intg(0)

        for ic, cc in enumerate(c):
            #print intermediate_res.shape, kk.shape, res.shape
            # For high K, intermediate_res can be negative, so we mask that.
            mask = intermediate_res[:,ic]>0
            spl = spline(np.log(kk[mask]),np.log(intermediate_res[mask,ic]),k=1)
            res[:,ic] = np.exp(spl(np.log(K[:,ic])))

        return res
예제 #14
0
def HighPassFilter(data, vel, width=5, linearize=False):
    """
    Function to apply a high-pass filter to data.
      Data must be in an xypoint container, and have linear wavelength spacing
      vel is the width of the features you want to remove, in velocity space (in cm/s)
      width is how long it takes the filter to cut off, in units of wavenumber
    """

    if linearize:
        original_data = data.copy()
        datafcn = spline(data.x, data.y, k=3)
        errorfcn = spline(data.x, data.err, k=3)
        contfcn = spline(data.x, data.cont, k=3)
        linear = DataStructures.xypoint(data.x.size)
        linear.x = np.linspace(data.x[0], data.x[-1], linear.size())
        linear.y = datafcn(linear.x)
        linear.err = errorfcn(linear.x)
        linear.cont = contfcn(linear.x)
        data = linear

    # Figure out cutoff frequency from the velocity.
    featuresize = 2 * data.x.mean() * vel / constants.c.cgs.value  # vel MUST be given in units of cm
    dlam = data.x[1] - data.x[0]  # data.x MUST have constant x-spacing
    Npix = featuresize / dlam

    nsamples = data.size()
    sample_rate = 1.0 / dlam
    nyq_rate = sample_rate / 2.0  # The Nyquist rate of the signal.
    width /= nyq_rate
    cutoff_hz = min(1.0 / featuresize, nyq_rate - width * nyq_rate / 2.0)  # Cutoff frequency of the filter

    # The desired attenuation in the stop band, in dB.
    ripple_db = 60.0

    # Compute the order and Kaiser parameter for the FIR filter.
    N, beta = kaiserord(ripple_db, width)
    if N % 2 == 0:
        N += 1

    # Use firwin with a Kaiser window to create a lowpass FIR filter.
    taps = firwin(N, cutoff_hz / nyq_rate, window=('kaiser', beta), pass_zero=False)

    # Extend data to prevent edge effects
    y = np.r_[data.y[::-1], data.y, data.y[::-1]]

    # Use lfilter to filter data with the FIR filter.
    smoothed_y = lfilter(taps, 1.0, y)

    # The phase delay of the filtered signal.
    delay = 0.5 * (N - 1) / sample_rate
    delay_idx = np.searchsorted(data.x, data.x[0] + delay)
    smoothed_y = smoothed_y[data.size() + delay_idx:-data.size() + delay_idx]
    if linearize:
        fcn = spline(data.x, smoothed_y)
        return fcn(original_data.x)
    else:
        return smoothed_y
예제 #15
0
    def projected_corr_gal(self):
        """
        Projected correlation function w(r_p).

        From Beutler 2011, eq 6.

        To integrate perform a substitution y = x - r_p.
        """
        lnr = np.log(self.r)
        lnxi = np.log(self.corr_gal)

        p = np.zeros(len(self.r))

        # Calculate correlation to higher scales for better integration
        if self.proj_limit is None:
            rlim = max(80.0, 5 * self.rmax)
        else:
            rlim = self.proj_limit

        print "RLIM, RMAX", rlim, self.rmax
        if rlim > self.rmax:
            upper_h = deepcopy(self)
            dr = self.r[1] / self.r[0]
            upper_h.update(rmin=self.rmax * dr, rmax=rlim, rnum=20)

            fit = spline(np.concatenate((self.r, upper_h.r)),
                         np.concatenate((self.corr_gal, upper_h.corr_gal)), k=3)  # [self.corr_gal > 0] maybe?
            print "FIT: ", fit(0.1)
        else:
            fit = spline(self.r, self.corr_gal, k=3)  # [self.corr_gal > 0] maybe?
            print "fit: ", fit(0.1)
        f_peak = 0.01
        a = 0

        for i, rp in enumerate(self.r):
            if a != 1.3 and i < len(self.r) - 1:
                # Get slope at rp (== index of power law at rp)
                ydiff = (lnxi[i + 1] - lnxi[i]) / (lnr[i + 1] - lnr[i])
                # if the slope is flatter than 1.3, it will converge faster, but to make sure, we cut at 1.3
                a = max(1.3, -ydiff)
                theta = self._get_theta(a)

            min_y = theta * f_peak ** 2 * rp

            # Get the upper limit for this rp
            lim = np.log10(rlim - rp)

            # Set the y vector for this rp
            y = np.logspace(np.log10(min_y), lim, 1000)

            # Integrate
            integ_corr = fit(y + rp)
            integrand = integ_corr / np.sqrt((y + 2 * rp) * y)
            p[i] = intg.simps(integrand, y) * 2

        return p
예제 #16
0
    def __init__(self, x, y, xref, yref,weights=None, kx=None,ky=None):
        if weights==None:
            weights = np.ones(x.shape)
        if kx == None:
            kx = np.linspace(x.min(), x.max())
        if ky == None:
            ky = np.linspace(y.min(), y.max())

        self.spline_x = spline(x,y,xref, tx=kx,ty=ky,w=weights)
        self.spline_y = spline(x,y,yref, tx=kx,ty=ky,w=weights)
def semi_supervised_test1(df, labeled_points, total_points):
    #split the data according to the classes, generate labelled , unlabelled mark for each and reshuffle.
    
        feature, label = seperate_feature_label(df)
        accuracy_for_supervise =[]
        accuracy_for_semi_supervise =[]
        x = []
        indices = np.arange(len(feature))
        label_indices = balanced_sample_maker(feature, label, labeled_points/len(label))
        unlabeled_indices = np.delete(indices, np.array(label_indices))
        rng = np.random.RandomState(0)
        rng.shuffle(unlabeled_indices)
        indices = np.concatenate((label_indices, unlabeled_indices[:total_points]))
        n_total_samples = len(indices)
        for i in range(100):
            unlabeled_indices=np.arange(n_total_samples)[labeled_points:]
            X = feature.iloc[indices]
            y = label.iloc[indices]
            y_train = np.copy(y)
            y_train[unlabeled_indices] = -1
            classifier = KNeighborsClassifier(n_neighbors=6)
            classifier.fit(X.iloc[:labeled_points], y.iloc[:labeled_points])

            y_pred = classifier.predict(X.iloc[labeled_points:])
            # y_all = pd.concat(y.iloc[:labeled_points], y_pred)
            true_labels = y.iloc[unlabeled_indices]
            # print(confusion_matrix(true_labels,y_pred))
            # print("%d labeled & %d unlabeled (%d total)"
                  # % (labeled_points, n_total_samples - labeled_points, n_total_samples))
            accuracy_for_supervise.append(accuracy_score(true_labels, y_pred))
            
            lp_model = label_propagation.LabelSpreading(gamma=0.25, kernel='knn', max_iter=300, n_neighbors=6)
            lp_model.fit(X, y_train)
            predicted_labels = lp_model.transduction_[unlabeled_indices]
            # print('Iteration %i %s' % (i, 70 * '_'))
            accuracy_for_semi_supervise.append(accuracy_score(true_labels, predicted_labels))
            x.append(labeled_points)
            # print(confusion_matrix(true_labels, predicted_labels))
            print('Semi-supervised learning:', accuracy_score(true_labels, predicted_labels))
            labeled_points += 5
        x_sm = np.array(x)
        y_sm = np.array(accuracy_for_supervise)
        y1_sm = np.array(accuracy_for_semi_supervise)
        x_smooth = np.linspace(x_sm.min(), x_sm.max(), 200)
        y_smooth = spline(x, y_sm, x_smooth)
        y1_smooth = spline(x, y1_sm, x_smooth)
        plt.ylim(0.4, 0.9)
        sup, = plt.plot(x_sm,y_sm,'b-',label='kNN')
        semi, = plt.plot(x_sm,y1_sm,'r-',label='Semi-supervised learning')
        plt.legend(handles=[sup, semi],frameon=False,loc=4,fontsize=20)
        plt.xlabel('Labeled points',fontsize=20)
        plt.ylabel('Accuracy',fontsize=20)
        plt.xticks(fontsize=15)  
        plt.yticks(fontsize=20) 
        plt.title('Semi-supervised learning with Random Selection',fontsize=20)
예제 #18
0
def CombineXYpoints(xypts, snr=None, xspacing=None, numpoints=None, interp_order=3):
    """
    Function to combine a list of xypoints into a single
      xypoint. Useful for combining several orders/chips
      or for coadding spectra

    Warning! This function is basically un-tested! 

      ***Optional keywords***
      snr: the spectra will be weighted by the signal-to-noise ratio
           before adding
      xspacing: the x-spacing in the final array
      numpoints: the number of points in the final array. If neither
                 numpoints nor xspacing is given, the x-spacing in the
                 final array will be determined by averaging the spacing
                 in each of the xypoints.
      interp_order: the interpolation order. Default is cubic
    """

    if snr is None or type(snr) != list:
        snr = [1.0] * len(xypts)

    # Find the maximum range of the x data:
    first = np.min([o.x[0] for o in xypts])
    last = np.max([o.x[-1] for o in xypts])
    avg_spacing = np.mean([(o.x[-1] - o.x[0]) / float(o.size() - 1) for o in xypts])

    if xspacing is None and numpoints is None:
        xspacing = avg_spacing
    if numpoints is None:
        if xspacing is None:
            xspacing = avg_spacing
        numpoints = (last - first) / xspacing
    x = np.linspace(first, last, numpoints)

    full_array = DataStructures.xypoint(x=x, y=np.zeros(x.size), err=np.zeros(x.size))
    numvals = np.zeros(x.size, dtype=np.float)  # The number of arrays each x point is in
    normalization = 0.0
    for xypt in xypts:
        #interpolator = ErrorPropagationSpline(xypt.x, xypt.y / xypt.cont, xypt.err / xypt.cont, k=interp_order)
        interpolator = spline(xypt.x, xypt.y/xypt.cont, k=interp_order)
        err_interpolator = spline(xypt.x, xypt.err/xypt.cont, k=interp_order)
        left = np.searchsorted(full_array.x, xypt.x[0])
        right = np.searchsorted(full_array.x, xypt.x[-1], side='right')
        if right < xypt.size():
            right += 1
        numvals[left:right] += 1.0
        val, err = interpolator(full_array.x[left:right]), err_interpolator(full_array.x[left:right])
        full_array.y[left:right] += val
        full_array.err[left:right] += err ** 2

    full_array.err = np.sqrt(full_array.err)
    full_array.y[numvals > 0] /= numvals[numvals > 0]
    return full_array
예제 #19
0
    def __init__(self, **model_parameters):
        super(Tinker10, self).__init__(**model_parameters)


        if self.delta_halo not in self.delta_virs:
            beta_array = np.array([self.params["beta_%s" % d] for d in self.delta_virs])
            gamma_array = np.array([self.params["gamma_%s" % d] for d in self.delta_virs])
            phi_array = np.array([self.params["phi_%s" % d] for d in self.delta_virs])
            eta_array = np.array([self.params["eta_%s" % d] for d in self.delta_virs])

            beta_func = spline(self.delta_virs, beta_array)
            gamma_func = spline(self.delta_virs, gamma_array)
            phi_func = spline(self.delta_virs, phi_array)
            eta_func = spline(self.delta_virs, eta_array)

            beta_0 = beta_func(self.delta_halo)
            gamma_0 = gamma_func(self.delta_halo)
            phi_0 = phi_func(self.delta_halo)
            eta_0 = eta_func(self.delta_halo)
        else:
            beta_0 = self.params['beta_%s' % (int(self.delta_halo))]
            gamma_0 = self.params['gamma_%s' % (int(self.delta_halo))]
            phi_0 = self.params['phi_%s' % (int(self.delta_halo))]
            eta_0 = self.params['eta_%s' % (int(self.delta_halo))]

        self.beta = beta_0 * (1 + min(self.z, self.params["max_z"])) ** self.params["beta_exp"]
        self.phi = phi_0 * (1 + min(self.z, self.params["max_z"])) ** self.params['phi_exp']
        self.eta = eta_0 * (1 + min(self.z, self.params["max_z"])) ** self.params['eta_exp']
        self.gamma = gamma_0 * (1 + min(self.z, self.params["max_z"])) ** self.params['gamma_exp']

        # # The normalisation only works with specific conditions
        # gamma > 0
        if self.gamma <= 0:
            if self.terminate:
                raise ValueError("gamma must be > 0, got " + str(self.gamma))
            else:
                self.gamma = 1e-3
        # eta >-0.5
        if self.eta <= -0.5:
            if self.terminate:
                raise ValueError("eta must be > -0.5, got " + str(self.eta))
            else:
                self.eta = -0.499
        # eta-phi >-0.5
        if self.eta - self.phi <= -0.5:
            if self.terminate:
                raise ValueError("eta-phi must be >-0.5, got " + str(self.eta - self.phi))
            else:
                self.phi = self.eta + 0.499
        if self.beta <= 0:
            if self.terminate:
                raise ValueError("beta must be > 0, got " + str(self.beta))
            else:
                self.beta = 1e-3
def astropy_smooth(data, vel, linearize=False, kernel=convolution.Gaussian1DKernel, **kern_args):
    """
    Smooth using a gaussian filter, using astropy.

    Parameters:
    ===========
    - data:          kglib.utils.DataStructures.xypoint instance
                     The data to smooth.

    - vel:           float
                     The velocity scale to smooth out.
                     Can either by an astropy quantity or a float in km/s

    - linearize:     boolean
                     If True, we will put the data in a constant
                     log-wavelength spacing grid before smoothing.
                     The output has the same spacing as the input
                     regardless of this variable.

    - kernel:        astropy.convolution kernel
                     The astropy kernel to use. The default is the
                     Gaussian1DKernel.

    - kern_args:     Additional kernel arguments beyond width

    Returns:
    ========
    A smoothed version of the data, on the same wavelength grid as the data
    """

    if linearize:
        original_data = data.copy()
        datafcn = spline(data.x, data.y, k=3)
        linear = DataStructures.xypoint(data.x.size)
        linear.x = np.logspace(np.log10(data.x[0]), np.log10(data.x[-1]), linear.size())
        linear.y = datafcn(linear.x)
        data = linear

    # Figure out feature size in pixels
    if not isinstance(vel, u.quantity.Quantity):
        vel *= u.km / u.second

    featuresize = (vel / constants.c).decompose().value
    dlam = np.log(data.x[1] / data.x[0])
    Npix = featuresize / dlam

    # Make kernel and smooth
    kern = kernel(Npix, **kern_args)
    smoothed = convolution.convolve(data.y, kern, boundary='extend')

    if linearize:
        fcn = spline(data.x, smoothed)
        return fcn(original_data.x)
    return smoothed
예제 #21
0
def metal_isoch(clust_isoch, clust_isoch_params, zx_pol, zy_pol, m_rang):
    '''
    Selects those isochrone sequences located inside the metallicity range given.
    '''
    
    # Store in arrays the isoch sequences and params for clusters inside the
    # metallicty range being processed.
    clust_isoch_met, clust_isoch_params_met, iso_ages = [], [], []
    for indx,iso_param in enumerate(clust_isoch_params):
        if m_rang[0] <= iso_param[3] <= m_rang[1]:
            iso_ages.append(iso_param[2])
            clust_isoch_met.append(clust_isoch[indx])
            clust_isoch_params_met.append(iso_param)
            
    # Check for duplicated ages.
    
    # Interpolate a single isochrone from those with the same age.
    
    # Replace all the isochrones of same age with the newly interpolated one.
    
    # Interpolate points close to the final ZAMS so as to smooth the section
    # were they intersect.
    clust_isoch_met_z = []
    for isoch in clust_isoch_met:
        
        # Iterate though ZAMS points ordered in increasing order in y.
        x3, y3 = [], []
        for indx,y1_i in enumerate(zy_pol):
            if 0. <(y1_i-isoch[1][-1])<=0.4 and zx_pol[indx] > isoch[0][-1]:
                y3 = [y1_i]
                x3 = [zx_pol[indx]]
                break

        sx = np.array(list(isoch[0])+x3)
        sy = np.array(list(isoch[1])+y3)
        t  = np.arange(sx.size,dtype=float)
        t /= t[-1]
        N  = np.linspace(0,1,1000)
        SX = spline(t,sx,N,order=2)
        SY = spline(t,sy,N,order=2)
        
        # Append interpolated isochrone.
        clust_isoch_met_z.append([SX.tolist(), SY.tolist()])
    
    # Sort all lists according to age.
    if iso_ages:
        iso_ages_s, clust_isoch_met_s, clust_isoch_params_met_s = \
        map(list, zip(*sorted(zip(iso_ages, clust_isoch_met_z,
                                  clust_isoch_params_met), reverse=True)))
    else:
        iso_ages_s, clust_isoch_met_s, clust_isoch_params_met_s = [], [], []
        
    return iso_ages_s, clust_isoch_met_s, clust_isoch_params_met_s
def semi_supervised_test2(df ,labeled_points, step):
        total_points = labeled_points+50
        feature ,label = seperate_feature_label(df)
        accuracy_for_supervise=[]
        accuracy_for_semi_supervise=[]
        x=[]
        indices = np.arange(len(feature))
        label_indices = balanced_sample_maker(feature,label,labeled_points/len(label))
        unlabeled_indices=np.delete(indices,np.array(label_indices))
        rng = np.random.RandomState(0)
        rng.shuffle(unlabeled_indices)
        indices = np.concatenate((label_indices,unlabeled_indices[:total_points]))
        # (len(df.index)-labeled_points-50)
        # print(len((df.index)))
        for i in range(int(len((df.index)-labeled_points-50)/step)):
            x.append(total_points)
            unlabeled_index = np.arange(total_points)[labeled_points:]
            X = feature.iloc[indices]
            y = label.iloc[indices]
            print(len(y.index))
            print(unlabeled_index)
            y_train = np.copy(y)
            y_train[unlabeled_index] = -1
            classifier = KNeighborsClassifier(n_neighbors=6)
            classifier.fit(X.iloc[:labeled_points],y.iloc[:labeled_points])
            y_pred = classifier.predict(X.iloc[labeled_points:])
            true_labels = y.iloc[unlabeled_index]
            accuracy_for_supervise.append(accuracy_score(true_labels,y_pred))
            lp_model = label_propagation.LabelSpreading(gamma=0.25, kernel='knn',max_iter=300, n_neighbors=6)
            lp_model.fit(X, y_train)
            predicted_labels = lp_model.transduction_[unlabeled_index]
            accuracy_for_semi_supervise.append(accuracy_score(true_labels, predicted_labels))
            total_points += step
            indices = np.concatenate((indices,unlabeled_indices[(total_points-step):total_points]))
        # print('accuracy gain average with %d labels:%d'%(labeled_points, np.average(accuracy_for_semi_supervise)-np.average(accuracy_for_semi_supervise)))
        x_sm = np.array(x)
        # print(x_sm)
        y_sm = np.array(accuracy_for_supervise)
        # print(y_sm)
        y1_sm=np.array(accuracy_for_semi_supervise)
        x_smooth = np.linspace(x_sm.min(), x_sm.max(), 200)
        y_smooth = spline(x, y_sm, x_smooth)
        y1_smooth = spline(x, y1_sm, x_smooth)
        sup, = plt.plot(x_sm,y_smooth,'b-',label='kNN')
        semi, = plt.plot(x_sm,y1_smooth,'r-',label='Label Propagation')
        plt.legend(handles=[sup, semi],frameon=False,loc=4)
        plt.ylim(0.4, 1)  
        plt.xlabel('Total samples',fontsize=20)
        plt.xticks(fontsize=15)  
        plt.yticks(fontsize=20)  
        plt.ylabel('Accuracy',fontsize=20)
        plt.title('Learning curve with '+str(labeled_points)+'labels',fontsize=20)
        return accuracy_score(true_labels, predicted_labels)
예제 #23
0
def makeSimpleGraph():
  baseFile = open('betterFile3.txt',mode = 'r')
  lines = []

  for line in baseFile:
    group = line.split()
    lines.append(group)


  lines = [line for line in lines if len(line)==8]

  gen = str(20)

  platinumPairs = [((line[0]=='Pt')*int(line[2])+(line[1]=='Pt')*(85-int(line[2])),int(line[7])) for line in lines if line[6]==gen]
  silverPairs = [((line[0]=='Ag')*int(line[2])+(line[1]=='Ag')*(85-int(line[2])),int(line[7])) for line in lines if line[6]==gen]
  nickelPairs = [((line[0]=='Ni')*int(line[2])+(line[1]=='Ni')*(85-int(line[2])),int(line[7])) for line in lines if line[6]==gen]
  copperPairs = [((line[0]=='Cu')*int(line[2])+(line[1]=='Cu')*(85-int(line[2])),int(line[7])) for line in lines if line[6]==gen]

  platY = []
  silverY = []
  nickelY = []
  copperY = []


  for k in range(LENGTH):
    platY.append(sum( piece[0] for piece in platinumPairs if piece[1]==k )/78.75 )
    silverY.append(sum( piece[0] for piece in silverPairs if piece[1]==k )/78.75 )
    nickelY.append(sum( piece[0] for piece in nickelPairs if piece[1]==k )/78.75 )
    copperY.append(sum( piece[0] for piece in copperPairs if piece[1]==k )/78.75 )

  xRange = range(LENGTH)

  xNew = np.linspace(min(xRange),max(xRange),20)
  power_smooth1 = spline(xRange,platY,xNew)
  power_smooth2 = spline(xRange,silverY,xNew)
  power_smooth3 = spline(xRange,nickelY,xNew)
  power_smooth4 = spline(xRange,copperY,xNew)


  plt.title("% Composition per Alpha Value")

  #plt.plot(xRange,platY,color='b',label = "Pt")
  #plt.plot(xRange,silverY,color='r',label= "Ag")
  #plt.plot(xRange,nickelY,color='g',label="Ni")
  #plt.plot(xRange,copperY,color='y',label="Cu")
  plt.plot(xNew,power_smooth1,color='b',label="Pt")
  plt.plot(xNew,power_smooth2,color='r',label="Ag")
  plt.plot(xNew,power_smooth3,color='g',label="Ni")
  plt.plot(xNew,power_smooth4,color='y',label="Cu")
  plt.xlabel("Alpha Value")
  plt.ylabel("Percent Composition")
  plt.legend(loc="center left",bbox_to_anchor=(1,0.5))
  plt.show()
예제 #24
0
파일: fkp.py 프로젝트: rainwoodman/nbodykit
 def nbar(self, val):
     """
     Set the number density as a function of redshift, :math:`n(z)`
     
     This should be either:
         
         1.  a string, specifying the name of the file to read :math:`n(z)` from; 
             the file should contain two columns, specifying ``(z, nz)``
         2.  a float, specifying the constant :math:`n(z)` value to use
     """
     if val is not None:
         
         # input is an existing file
         if isinstance(val, six.string_types) and os.path.exists(val):
                 
             # read from the file
             try:
                 d = numpy.loadtxt(val)
                 
                 # columns are z, n(z)
                 if d.shape[1] == 2:
                     self._nbar = spline(d[:,0], d[:,1])
             
                 # columns are z_min, z_max, z_cen, n(z)
                 elif d.shape[1] == 4:
                     self._nbar = spline(d[:,-2], d[:,-1])
                 
                 # wrong number of columns
                 else:
                     raise
             except:
                 raise ValueError("n(z) file should specify either: [z, n(z)] or [z_min, z_max, z_cen, n(z)]")
         
             # redshift is required for the n(z) spline
             self._nbar.need_redshift = True
         
         # input is a float, specifying the constant nbar
         elif isinstance(val, six.string_types) and is_float(val) or numpy.isscalar(val):
             val = float(val)
             
             def constant_nbar(redshift):
                 nbar = numpy.array(val)
                 nbar = numpy.lib.stride_tricks.as_strided(nbar, (len(redshift), nbar.size), (0, nbar.itemsize))
                 return nbar.squeeze()
             self._nbar = constant_nbar # return a constant n(z)
             self._nbar.need_redshift = False
         else:
             msg = ("error setting ``nbar`` parameter from input value; should "
                    " be either the name of a file, or a float value if n(z) is constant")
             raise TypeError(msg)
     else:
         self._nbar = None
def plot_fig(x_label, y_label):
    new1_x = np.linspace(min(plot1_x), max(plot1_x), 1000)
    new1_y = spline(plot1_x, plot1_y, new1_x)
    new2_x = np.linspace(min(plot2_x), max(plot2_x), 1000)
    new2_y = spline(plot2_x, plot2_y, new2_x)
    plt.plot(new1_x, new1_y, 'b', label='Test Accuracy')
    plt.plot(new2_x, new2_y, 'g', label='Train Accuracy')
    # plt.plot(plot1_x, plot1_y, 'b', label='Test Accuracy')
    # plt.plot(plot2_x, plot2_y, 'g', label='Train Accuracy')
    plt.legend(loc=0)
    plt.xlabel(x_label)
    plt.ylabel(y_label)
    plt.show()
def IterativeLowPass(data, vel, numiter=100, lowreject=3, highreject=3, width=5, linearize=False):
    """
    An iterative version of LowPassFilter.
    It will ignore outliers in the low pass filter. New parameters that are
    not described in the docstring fro LowPassFilter are:

    Parameters:
    ===========
    - numiter:       integer
                     The maximum number of iterations to take

    - lowreject:     integer
                     How many sigma below the current filtered curve do we
                     count as bad and ignore in the next iteration?

    - highreject:    integer
                     How many sigma above the current filtered curve do we
                     count as bad and ignore in the next iteration?
    """

    datacopy = data.copy()
    if linearize:
        datafcn = spline(datacopy.x, datacopy.y, k=3)
        errorfcn = spline(datacopy.x, datacopy.err, k=1)
        contfcn = spline(datacopy.x, datacopy.cont, k=1)
        linear = DataStructures.xypoint(datacopy.x.size)
        linear.x = np.linspace(datacopy.x[0], datacopy.x[-1], linear.size())
        linear.y = datafcn(linear.x)
        linear.err = errorfcn(linear.x)
        linear.cont = contfcn(linear.x)
        datacopy = linear.copy()

    done = False
    iter = 0
    datacopy.cont = FittingUtilities.Continuum(datacopy.x, datacopy.y, fitorder=9, lowreject=2.5, highreject=5)
    while not done and iter < numiter:
        done = True
        iter += 1
        smoothed = LowPassFilter(datacopy, vel, width=width)
        residuals = datacopy.y / smoothed
        mean = np.mean(residuals)
        std = np.std(residuals)
        badpoints = np.where(np.logical_or((residuals - mean) < -lowreject * std, residuals - mean > highreject * std))[
            0]
        if badpoints.size > 0:
            done = False
            datacopy.y[badpoints] = smoothed[badpoints]
    if linearize:
        return linear.x, smoothed
    else:
        return smoothed
예제 #27
0
def betamax(_gwylmo,n=10,plt=False,opt=True,verbose=False):
    from scipy.interpolate import interp1d as spline
    from scipy.optimize import minimize
    from numpy import pi,linspace,argmax,array
    dphi_list = pi*linspace(-1,1,n)
    dpsi,val = [],[]
    for dphi in dphi_list:
        [dpsi_,val_] = alphamax(_gwylmo,dphi,plt=False,n=n)
        dpsi.append( dpsi_ )
        val.append( val_ )

    dphis = linspace(min(dphi_list),max(dphi_list),1e3)
    vals = spline( dphi_list, val, kind='cubic' )( dphis )
    dpsi_s = spline( dphi_list, dpsi, kind='cubic' )( dphis )

    action = lambda x: -sum( vectorize( _gwylmo, x[0], x[1] ) )
    dphi_opt_guess = dphis[argmax(vals)]
    dpsi_opt_guess = dpsi_s[argmax(vals)]
    if opt:
        K = minimize( action, [dphi_opt_guess,dpsi_opt_guess] )
        # print K
        dphi_opt,dpsi_opt = K.x
        val_max = -K.fun
    else:
        dphi_opt = dphi_opt_guess
        dpsi_opt = dpsi_opt_guess
        val_max = vals.max()

    if plt:
        # Setup plotting backend
        import matplotlib as mpl
        from mpl_toolkits.mplot3d import axes3d
        mpl.rcParams['lines.linewidth'] = 0.8
        mpl.rcParams['font.family'] = 'serif'
        mpl.rcParams['font.size'] = 12
        mpl.rcParams['axes.labelsize'] = 20
        mpl.rcParams['axes.titlesize'] = 20
        from matplotlib.pyplot import plot,xlabel,title
        plot( dphi_list, val, linewidth=4, alpha=0.1, color='k' )
        plot( dphi_opt, val_max, 'or', alpha=0.5 )
        plot( dphis, vals )
        xlabel(r'$\phi$')
        title(val_max)

    if verbose:
        print 'dphi_opt = ' + str(dphi_opt)
        print 'dpsi_opt = ' + str(dpsi_opt)
        print 'val_max = ' + str(val_max)

    return dphi_opt,dpsi_opt
예제 #28
0
def makeReferenceRLHistogram(alnRatios, refLengths, outfile, format, quantile):
    """
    X-axis: (unique) reference length 
    Y-axis: count
    """
    fig = plt.figure(dpi=300, figsize=(10, 6))
    ax = fig.add_subplot(111)
    ax.set_title("Aligned Reference Length Distribution (qCov>=80%)")

    fullPass = alnRatios[alnRatios['IsFullPass']&(alnRatios['rCov']>=.8)]
    fullLength = alnRatios[alnRatios['IsFullLength']&(alnRatios['rCov']>=.8)]
    
    if quantile is not None:
        refLengths = refLengths[refLengths < mstats.mquantiles(refLengths, [quantile])[0]]
    # plot all references first
    bins = 50
    y,binEdges = n.histogram(refLengths, bins=bins)
    bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
    xnew = n.linspace(bincenters.min(), bincenters.max(), 100)
    ysmooth = spline(bincenters, y, xnew)
    # normalize by hand
    ysmooth = ysmooth*1./sum(ysmooth)
    ax.plot(xnew, ysmooth, '-', label="All References")
    max_y = max(ysmooth)

    for l, label in zip((fullPass, fullLength), ("Aligned full-pass subreads", "Aligned " + SeenName + " subreads")):
        alnRefLength = dict(zip(l['RefID'], l['RefLength']))
        if len(alnRefLength) == 0:
            continue
        alnRefLength = n.array(alnRefLength.values())
        if quantile is not None:
            alnRefLength = alnRefLength[alnRefLength < mstats.mquantiles(alnRefLength, [quantile])[0]]
        bins = (max(alnRefLength)-min(alnRefLength))/100 + 1
        y,binEdges = n.histogram(alnRefLength, bins=bins)
        bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
        xnew = n.linspace(bincenters.min(), bincenters.max(), 300)
        ysmooth = spline(bincenters, y, xnew)
        # normalize by hand
        ysmooth = ysmooth*1./sum(ysmooth)
        ax.plot(xnew, ysmooth, '-', label=label)
        max_y = max(max_y, max(ysmooth))
        #num, bins, patches = ax.hist(alnRefLength, bins=50, histtype='step', label=label, normed=True)
        #max_y = max(max_y, max(num))

    ax.set_ylim(0, max_y * 1.1)
    ax.legend(loc='upper center', prop={'size': 'small'})
    ax.set_xlabel("Reference Length")
    ax.set_ylabel("Fraction")
    fig.savefig(outfile, format=format)
예제 #29
0
def xCoordinates(sobel_img):
    num_rows = float(len(sobel_img)) # get number of x values

    # sum along y axis
    vert_sum = np.sum(sobel_img,axis=0)
    # make it an average value (divide by # of x values)
    vert_sum = np.divide(vert_sum,num_rows)

    x = np.arange(0,len(vert_sum)) # for graphing
    xnew = np.arange(0,len(vert_sum),50) # for smoothing
    #smooth
    y_smooth = spline(x, vert_sum, xnew)

    #make a sin curve 1/3 of the width of image
    img_width, img_height = sobel_img.shape
    z = np.arange(0,int(img_width/3),1)
    def f(x):
        return np.sin(x/90)*-15 + 25

    f = [f(i) for i in z] # make sine into an array

    # convolve sine and the vertical sum
    y_conv = np.convolve(vert_sum, f,'same')

    # detect local minima
    mins = (np.diff(np.sign(np.diff(y_conv))) > 0).nonzero()[0] + 1

    return mins
예제 #30
0
def yCoordinates(sobel_img):
    num_col = float(len(sobel_img[0])) #number of y values
    # sum along x axis
    horiz_sum = np.sum(sobel_img, axis=1)
    #average value
    horiz_sum = np.divide(horiz_sum, num_col)

    y = np.arange(0, len(horiz_sum))
    ynew = np.arange(0, len(horiz_sum))
    x_smooth = spline(y, horiz_sum, ynew)

    #make a sin curve 1/3 of the height
    img_width, img_height = sobel_img.shape
    z = np.arange(0,int(img_height/3),1)
    def f(x):
        return np.sin(x/90)*-15 + 25

    f = [f(i) for i in z] # make sine into an array

    # convolve sine and the vertical sum
    y_conv = np.convolve(horiz_sum, f,'same')

    # detect local minima
    mins = (np.diff(np.sign(np.diff(y_conv))) > 0).nonzero()[0] + 1

    return mins
예제 #31
0
def simula4():

    #parametros
    numero_ciclos= 1
    tempo_simulacao = 1000000
    u=[]#lista para plotar
    valor=[]#lista para plotar

    ##TODO: melhores nomes para as variáveis

    for w in range(1,20):
        u_servidor = round(0.5*(w+1), 2)#lambda do problema
        media=0
        for q in range(numero_ciclos):
            fila = 0
            i = 0
            j = 0
            servidor = 0
            tempo_proximo = 0
            entrada = 0  ##tempo até a próxima entrada
            tempo_proximo = 0  ##tempo até terminar a execução no servidor
            ##variáveis para calcular a média
            tempo = 0
            esperanca = 0
            fim = 0

            while (1):

                if(tempo == tempo_simulacao):
                    esperanca = esperanca / tempo
                    #print('a número médio de pessoas no sistema é: ' + str(esperanca))
                    media= media + esperanca/numero_ciclos
                    break
                ##if(i == numero_de_aleatorios and j == numero_de_aleatorios):
                ##    esperanca = esperanca / tempo
                ##    #print('a número médio de pessoas no sistema é: ' + str(esperanca))
                ##    media= media + esperanca/numero_ciclos
                ##    break

                while(entrada == 0):
                    uniforme = np.random.uniform(5,15)
                    entrada = int(uniforme) ##Gera uma nova entrada exponencial
                    resto += uniforme - entrada
                    if(resto > 1):
                        entrada += 1
                    fila+= 1
                    i += 1

                entrada -= 1

                ## while nescessário caso caia em um tempo = 0 novamente
                resto = 0
                while(tempo_proximo == 0 and fila > 0):
                    servidor = 1
                    fila -= 1
                    aleatorio = np.random.random_sample()
                    if(aleatorio > 0.9):
                        fila +=1
                    exponencial = np.random.exponential(1/u_servidor)
                    tempo_proximo = int(exponencial)
                    resto += exponencial - tempo_proximo
                    if(resto > 1):
                        tempo_proximo += 1
                    j += 1
                if(fila == 0 and tempo_proximo == 0):
                        servidor = 0
                else:
                        tempo_proximo -= 1

                ##coisas inuteis para parar quando já tudo processado
                ##nunca será executável pois para depois da ultima remessa da entrada
                if(fim == 0):
                    ##print('fila = ' + str(fila))
                    ##print('servidor = ' + str(servidor))
                    pass
                else:
                    break
                if(tempo == tempo_simulacao):
                    fim = 1

                esperanca += fila + servidor ##soma o número de clientes no sistema
                tempo += 1  ##adiciona mais um no tempo

        ##executando a função main
        print("Media das medias: ", media)
        u.append(u_servidor)
        valor.append(media)

    plt.figure(figsize=(8, 6), dpi=100)
    x_smooth = np.linspace(min(u), max(u), 10000)
    y_smooth = spline (u, valor, x_smooth)
    plt.plot(x_smooth,y_smooth,'-b', label="Curva Amortizada")
    plt.plot(u, valor, 'bo', label="Pontos Da Curva")
    #plt.plot(lamb, valor2, '-ro', label="Curva Teste")
    plt.axis([0, 1.1*(max(u)), 0, 1.2*(max(valor))])
    plt.suptitle('Cenário-3', fontsize=20)
    plt.xlabel('Lambda', fontsize=15)
    plt.ylabel('Média', fontsize=15)
    plt.legend(loc=1, prop={'size':10})
    plt.show()
ax = plt.subplot(111,zorder=8)

ax.plot(rVector[:-4]/TOF, density[:-4]/scale, 'ko',markersize=5.5,zorder=1,clip_on=False)


x_all = rVectorAll/TOF
x_pts = rVector/TOF
x_to_0 = np.append(0,x_pts)

fermi_to_0 = np.append(density_fermi[0],density_fermi)
thermal_to_0 = np.append(density_thermal[0],density_thermal)

x_plot = np.linspace(min(x_all),max(x_all),1000)

y_fermi = spline(x_to_0, fermi_to_0, x_plot)
y_thermal = spline(x_to_0, thermal_to_0, x_plot)

h2 = plt.plot(x_plot, y_fermi/scale, 'b',label='Fermi-Dirac',linewidth=1.5,zorder=3)
h1 = plt.plot(x_plot, y_thermal/scale, 'r',label='Thermal',linewidth=1.5,zorder=2)

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.xlabel('Velocity (mm/s)')
plt.ylabel('Column density (a.u.)')
ax.tick_params(axis='y', which='major', pad=12)

legend = plt.legend(frameon=False, fontsize=12,numpoints=1,loc=3)
plt.ylim(0,1)
plt.xlim(0, V_MAX)
예제 #33
0
def plot_dynamic_spectra(spectra_wavelengths, spectra, parameters):
    """
    Plot the model spectra vs the observed spectra (interpolated)
    """

    ##### prepare a spectrum array
    spectra_array = np.zeros([100, len(spectra_wavelengths)])

    for phase in spectra.keys():

        for spectrum in spectra[phase].keys():

            spectra_array[phase, :] = spectra[phase][spectrum]

    spectra_dict = spectra
    n_phases = len(spectra_dict.keys())

    list_phases = []

    for count, ph in enumerate(spectra_dict.keys()):
        list_phases.append(ph)

    list_phases.sort()
    array_phases = 0.01 * np.array(list_phases)
    n_phases = len(array_phases)
    spectra_temp = np.zeros((len(spectra_wavelengths), n_phases))
    triple_spectra = np.zeros((len(spectra_wavelengths), 3 * n_phases))

    triple_phases = np.zeros(3 * n_phases)
    for i in range(3):
        triple_phases[i * n_phases:(i + 1) * n_phases] = array_phases + (i - 1)

    for count, key in enumerate(list_phases):
        mean_data = np.zeros(len(spectra_wavelengths))
        for spectr in spectra_dict[key]:

            mean_data += spectra_dict[key][spectr]

        mean_data /= len(spectra_dict[key])
        spectra_temp[:, count] = mean_data
        triple_spectra[:, count] = mean_data
        triple_spectra[:, count + n_phases] = mean_data
        triple_spectra[:, count + 2 * n_phases] = mean_data

    interpolated_data = np.zeros((len(spectra_wavelengths), 100))
    phases = np.arange(0, 1.0, 0.01)

    from scipy.interpolate import interp1d
    from scipy.interpolate import interp2d
    from scipy.interpolate import spline
    import matplotlib.gridspec as gridspec

    ###### observed spectra ########################################################

    colormap = plt.cm.seismic

    gs1 = gridspec.GridSpec(1, 1)
    my_dpi = 100
    fig = plt.figure(figsize=(1200 / my_dpi, 600 / my_dpi), dpi=my_dpi)
    axes = fig.add_subplot(gs1[0])
    for w, wave in enumerate(spectra_wavelengths):
        interp_wave = spline(triple_phases,
                             triple_spectra[w, :],
                             phases,
                             order=1,
                             kind='smoothest')
        interpolated_data[w, :] = interp_wave
        central_wavelength = parameters['OTHER']['central_wavelength']


    axes.imshow(interpolated_data.T, cmap=colormap, \
            extent=((spectra_wavelengths[0] - central_wavelength)/\
                            central_wavelength*3.e5, (spectra_wavelengths[-1] - central_wavelength)/\
                            central_wavelength*3.e5, 1, 0),\
                            origin='upper', vmin=0.2, vmax=1.8, aspect='auto')
    # plt.suptitle("Observations vs model")
    axes.set_xlabel('RV (km/s)', fontsize=18)
    axes.set_ylabel('Phase', fontsize=18)

    plt.show()
예제 #34
0
file_ = 'output_es_mac300.txt'
with open(file_, 'r') as f:
    for line in f:
        listt_3.append(line)

X1 = list(map(int, listt_3[0].strip().split(' ')))
Y1 = list(map(int, listt_3[1].strip().split(' ')))
firstDeadNodeRound = findFirstDeadNodeRound(X1, Y1)
lastDeadNodeRound = findLastDeadNodeRound(X1, Y1)
roundAxis = roundAxis[0:firstDeadNodeRound //
                      2] + roundAxis[firstDeadNodeRound // 2:6000:100]
numberOfDeadNodesAxis = numberOfDeadNodesAxis[
    0:firstDeadNodeRound // 2] + numberOfDeadNodesAxis[firstDeadNodeRound //
                                                       2:6000:100]
roundAxis = np.asarray(roundAxis)
numberOfDeadNodesAxis = np.asarray(numberOfDeadNodesAxis)
roundAxis_new = np.linspace(roundAxis.min(), roundAxis.max(), 60000)
numberOfDeadNodesAxis_smooth = spline(roundAxis, numberOfDeadNodesAxis,
                                      roundAxis_new)
plt.text(firstDeadNodeRound - 100, -10, firstDeadNodeRound)
plt.text(lastDeadNodeRound - 100, 305, lastDeadNodeRound)
plt.plot(roundAxis_new, numberOfDeadNodesAxis_smooth, 'r', label='ES-MAC')
plt.xlabel('Number of rounds')
plt.ylabel('Number of Dead Nodes')
plt.title(
    'Graph between number of Dead nodes and number of rounds\n(100x100 grid)')
plt.legend()
plt.show()
if (show_graphics == 1):
    done()
예제 #35
0
def plotTrajectories(
        energy,
        K_t,
        koff,
        outputDir="/Users/jameskomianos/Code/stochfilaments/data-PROD3/data-NM/output/",
        saveDir="/Users/jameskomianos/Desktop/"):

    #predicted steady state
    lo_ss = energy * common.kT * (
        1.0 / (1.0 + (v / v_b) * math.exp(-energy))) / (2.0 * 10.0 * K_t)
    print "Predicted steady-state overlap = ", lo_ss

    #mpl.rcParams['axes.color_cycle'] = ['y','k','darkgreen']
    #mpl.rcParams['axes.color_cycle'] = ['b', 'g', 'r', 'c']
    mpl.rcParams['axes.color_cycle'] = [
        'darkorange', 'darkorange', 'blueviolet', 'blueviolet', 'brown',
        'brown', 'darkblue'
    ]

    pvals = []
    perrs = []

    k_minus = koff

    runtime = 200.0
    numIntervals = 200.0

    l_intervals = pickle.load(
        open(
            outputDir + "/intervalsl-e%0.0f-Kt%0.3f-koff%0.4f.p" %
            (energy, K_t, k_minus), "rb"))

    if (len(l_intervals) != 0):

        intervalTime = runtime / numIntervals

        t = []
        l = []
        devs = []
        errplus = []
        errminus = []

        intervalTime = 0
        for i in xrange(0, len(l_intervals)):

            intervalTime += runtime / numIntervals

            lset = l_intervals[i]

            lval = np.mean(lset)
            l.append(lval)
            t.append(intervalTime)

            dev = np.std(lset) / math.sqrt(250.0)
            devs.append(dev)

            errplus.append(lval + dev)
            errminus.append(lval - dev)

            logl = [math.log(x) for x in l if x > 0]
            logt = [math.log(x) for x in t if x > 0]
            logdev = []

            index = 0
            for lval in l:
                logdev.append(0.434 * devs[index] / lval)
                index += 1

    #truncate for relevance
    #semi-log is needed for t~tlab
    index = 0
    logl_semil = []
    t_semil = []

    logl_ll = []
    logt_ll = []

    flag = "LIN_COLLECT"
    initSlope = 0.0
    tautrans = 0.0

    for tval in t:

        if (flag == "EXP_COLLECT" and tval > tautrans * 2):

            logl_semil.append(math.log(l[index]))
            t_semil.append(t[index])

        if (flag == "LIN_COLLECT"):
            logl_ll.append(logl[index])
            logt_ll.append(logt[index])

            if (len(logl_ll) > 2):
                slope, intercept, r_value, p_value, std_err = stats.linregress(
                    logt_ll, logl_ll)
                if (len(logl_ll) == 3):
                    initSlope = slope
                elif (abs(slope - initSlope) / initSlope >= 0.10):
                    tautrans = tval
                    flag = "EXP_COLLECT"

        index += 1

    lo_last = l[index - 1]

    print "Transition time = ", tautrans, " s"

    #plot trajectory
    plt.figure(8, figsize=(3.5, 2.5))

    #smooth out
    that = np.linspace(min(t), max(t), 10000)
    lhat = spline(t, l, that)

    p = plt.plot(that, lhat, markersize=2, linewidth=2.0)
    color = p[0].get_color()

    plt.fill_between(t, errplus, errminus, color=color, alpha=0.3)

    plt.xlabel(r'$\mathsf{Time\/ (s)}$', fontsize=12)
    plt.ylabel(r'$\mathsf{\langle l_o \rangle\/(nm)}$', fontsize=12)
    plt.savefig(saveDir + '/traj.pdf', format='pdf')
    plt.xlim((0, 100))

    #plot log(l) vs log(t)
    plt.figure(10, figsize=(3.5, 3))
    plt.errorbar(logt, logl, fmt='o', yerr=logdev)  #, mfc='white', zorder=1)
    plt.xlabel(r'$\mathsf{log(t)}$', fontsize=12)
    plt.ylabel(r'$\mathsf{log(\langle l_o \rangle)}$', fontsize=12)
    plt.savefig(saveDir + '/trajlog.pdf', format='pdf')

    #regression
    print
    slope, intercept, r_value, p_value, std_err = stats.linregress(
        logt_ll, logl_ll)
    print "Log-log plot..."
    print "Observed alpha_l = ", math.exp(intercept)
    print "r^2 = ", r_value * r_value
    print "std_err = ", std_err
    print "v = ", slope

    #plot log(l) vs t
    plt.figure(9, figsize=(3, 3))

    plt.errorbar(t, logl, fmt='o-', yerr=logdev)
    plt.xlabel(r'$\mathsf{Time\/ (s)}$', fontsize=12)
    plt.ylabel(r'$\mathsf{log(\langle l_o \rangle)}$', fontsize=12)
    plt.savefig(saveDir + '/trajsemilog.pdf', format='pdf')

    slope, intercept, r_value, p_value, std_err = stats.linregress(
        t_semil, logl_semil)
    print
    print "Semi-log plot..."
    print "Observed alpha = ", math.exp(intercept)
    print "r^2 = ", r_value * r_value
    print "std_err = ", std_err
    print "u = ", slope
예제 #36
0
"""
"""

fp = open("GRU.pickle", 'rb')
y_gru = np.array(cPickle.load(fp))

fp = open("LSTM.pickle", 'rb')
y_lstm = np.array(cPickle.load(fp))

fp = open("BiRNN.pickle", 'rb')
y_birnn = np.array(cPickle.load(fp))

x = np.array([i for i in range(len(y_gru))])

x_smooth = np.linspace(x.min(), x.max(), 300)
y_gru_smooth = spline(x, y_gru, x_smooth)
y_lstm_smooth = spline(x, y_lstm, x_smooth)
y_birnn_smooth = spline(x, y_birnn, x_smooth)

plt.figure()
plt.plot(x_smooth, y_gru_smooth, ':')
plt.plot(x_smooth, y_lstm_smooth, '--')
plt.plot(x_smooth, y_birnn_smooth, '-')
plt.grid(alpha=0.5)
plt.legend(['GRU', 'LSTM', 'BiRNN'], loc='upper right')
plt.show()

fp = open("BiRNN_slot3.pickle", 'rb')
y_birnn3 = np.array(cPickle.load(fp))

x = np.array([i for i in range(len(y_birnn3))])
예제 #37
0
                    maxLength = len(x)
            for x in band_energies:
                for y in range(maxLength - len(x)):
                    x.append(x[-1])
            assert (npoint) == len(idx)
            band_energies = asarray(band_energies)
            # Now perform spline interpolation on band structure, if requested
            if should_spline == True:
                xvals_smooth = np.linspace(
                    xvals.min(), xvals.max(), spline_factor *
                    len(xvals))  # Interpolated x axis for spline smoothing
                new_band_energies = []
                for b in range(band_energies.shape[1]
                               ):  # Spline every band, one by one
                    new_band_energies.append(
                        spline(xvals, band_energies[:, b], xvals_smooth))
                band_energies = asarray(new_band_energies).transpose(
                )  # recombine the bands back into the original data format
                xvals_plot = xvals_smooth  # and use the interpolated x axis for plotting
            else:
                xvals_plot = xvals  # Use the "raw" x axis for plotting

            for b in range(band_energies.shape[1]):
                # Make sure legend entry only appears once
                if b == 0 and iband == 1:
                    if max_spin_channel[i] > 1 and spin == 1:
                        ax_bands.plot(xvals_plot,
                                      band_energies[:, b],
                                      color=color_array[i],
                                      label=plotName[i] + " up")
                    elif max_spin_channel[i] > 1 and spin == 2:
linear_ins_value2 = interp1d(x, y2)
print('当x为6,7时,对应的线性插值y1的值为:', linear_ins_value1([6, 7]))
print('当x为6,7时,对应的线性插值y2的值为:', linear_ins_value2([6, 7]))

# 拉格朗日插值
from scipy.interpolate import lagrange

lg_ins_value1 = lagrange(x, y1)
lg_ins_value2 = lagrange(x, y2)
print('当x为6,7时,对应的拉格朗日插值y1的值为:', lg_ins_value1([6, 7]))
print('当x为6,7时,对应的拉格朗日插值y2的值为:', lg_ins_value2([6, 7]))

# 样条插值--推荐用BSpline替换
from scipy.interpolate import spline

spline_ins_value1 = spline(x, y1, xnew=np.array([6, 7]))
spline_ins_value2 = spline(x, y2, xnew=np.array([6, 7]))
print('当x为6,7时,对应的样条插值y1的值为:', spline_ins_value1)
print('当x为6,7时,对应的样条插值y2的值为:', spline_ins_value2)


# 5.2.3检测与处理异常值:3σ原则和箱线图
# ①3σ原则:局限性只能针对正态分布或者近似正态分布的数据有效
def out_range(ser1):
    bool_ind = (ser1.mean() - 3 * ser1.std() >
                ser1) | (ser1.mean() + 3 * ser1.var() < ser1)
    index = np.arange(ser1.shape[0])[bool_ind]
    outrange = ser1.iloc[index]
    return outrange

def smooth(Time,Y):
    f = interpolate.spline(Time, Y,Time)
    return f(Time)
예제 #40
0
# data_P = [0, 0, 0, 0, 0, 0, 1, 1, 0, 3, 2, 5, 8, 7, 6, 4, 1, 0, 0, 0]
# data_P = [0, 0, 0, 0, 1, 1, 10, 7, 26, 27, 53, 52, 50, 39, 28, 17, 10, 9, 3, 1]
# data_P = [0, 0, 0, 0, 0, 4, 6, 15, 31, 20, 42, 43, 44, 50, 40, 15, 14, 3, 3, 4]

wsum_P = sum(data_P)
data_P[:] = [float(weight) / wsum_P for weight in data_P]
wsum_N = sum(data_N)
data_N[:] = [float(weight) / wsum_N for weight in data_N]

###############################################################################
# Line Plot
###############################################################################

# bincenters = 0.5 * (bins[1:] + bins[:-1])
newcenters = np.linspace(min(bincenters), max(bincenters), 300)
smooth_P = spline(bincenters, data_P, newcenters)
smooth_N = spline(bincenters, data_N, newcenters)
plt.plot(newcenters, smooth_P, '-', color='#1f77b4', label="True Matches")
plt.plot(newcenters, smooth_N, '-', color='#ff7f0e', label="True Non-Matches")
plt.xlabel("Similarity")
plt.ylabel("Amount")
plt.ylim(0, max(max(smooth_P), max(smooth_N)) + 0.02)
plt.title("True Ground Truth Distribution (ncvoter)")
plt.legend(loc="best")

###############################################################################
# Histogram
###############################################################################

# fig = plt.figure(dpi=None, facecolor="white")
# ax = fig.add_subplot(111)
예제 #41
0
# Display data with HDIs of posterior predictions.

plt.figure()
# Plot data values:
y_lim = [np.min(y_HDI_lim), np.max(y_HDI_lim)]
plt.plot(x, y, 'k.')
plt.xlim(x_lim)
plt.ylim(y_lim)
plt.xlabel('X (height in inches)')
plt.ylabel('Y (weight in pounds)')
plt.title('Data with 95% HDI & Mean of Posterior Predictions')
# Superimpose posterior predicted 95% HDIs:
y_post_pred_ave = np.average(y_post_pred, axis=1)
#Book version of the HDI representation
#plt.errorbar(x_post_pred,y_post_pred_ave,
#             yerr=[abs(y_HDI_lim[:,0]-y_post_pred_ave),
#                   abs(y_HDI_lim[:,1]-y_post_pred_ave)], fmt='.')

#Smoothed version of the HDI representation
x_new = np.linspace(x_post_pred.min(), x_post_pred.max(), 200)
y_HDI_lim_smooth = spline(x_post_pred, y_HDI_lim, x_new)
plt.plot(x_post_pred, y_post_pred_ave)
plt.fill_between(x_new,
                 y_HDI_lim_smooth[:, 0],
                 y_HDI_lim_smooth[:, 1],
                 alpha=0.3)

plt.savefig('Figure_16.6.png')

plt.show()
예제 #42
0
roundAxis_1 = list(map(int, listt_1[0].strip().split(' ')))
numberOfDeadNodesAxis_1 = list(map(int, listt_1[1].strip().split(' ')))
firstDeadNodeRound_1 = findFirstDeadNodeRound(roundAxis_1,
                                              numberOfDeadNodesAxis_1)
lastDeadNodeRound_1 = findLastDeadNodeRound(roundAxis_1,
                                            numberOfDeadNodesAxis_1)
roundAxis_1 = roundAxis_1[0:firstDeadNodeRound_1 //
                          2] + roundAxis_1[firstDeadNodeRound_1 // 2:6000:60]
numberOfDeadNodesAxis_1 = numberOfDeadNodesAxis_1[
    0:firstDeadNodeRound_1 //
    2] + numberOfDeadNodesAxis_1[firstDeadNodeRound_1 // 2:6000:60]
roundAxis_1 = np.asarray(roundAxis_1)
numberOfDeadNodesAxis_1 = np.asarray(numberOfDeadNodesAxis_1)
roundAxis_new_1 = np.linspace(roundAxis_1.min(), roundAxis_1.max(), 60000)
numberOfDeadNodesAxis_smooth_1 = spline(roundAxis_1, numberOfDeadNodesAxis_1,
                                        roundAxis_new_1)
plt.text(firstDeadNodeRound_1 - 350, 10, firstDeadNodeRound_1)
plt.text(lastDeadNodeRound_1, 305, lastDeadNodeRound_1)
plt.plot(roundAxis_new_1, numberOfDeadNodesAxis_smooth_1, 'b', label='BMA MAC')

roundAxis_2 = list(map(int, listt_2[0].strip().split(' ')))
numberOfDeadNodesAxis_2 = list(map(int, listt_2[1].strip().split(' ')))
firstDeadNodeRound_2 = findFirstDeadNodeRound(roundAxis_2,
                                              numberOfDeadNodesAxis_2)
lastDeadNodeRound_2 = findLastDeadNodeRound(roundAxis_2,
                                            numberOfDeadNodesAxis_2)
roundAxis_2 = roundAxis_2[0:firstDeadNodeRound_2 //
                          2:10] + roundAxis_2[firstDeadNodeRound_2 //
                                              2:6000:75]
numberOfDeadNodesAxis_2 = numberOfDeadNodesAxis_2[
    0:firstDeadNodeRound_2 //
예제 #43
0
               [51069435]])
# print(s)
x = solve(m, s)
t = np.array([[11321], [12223], [15707], [14070], [12152], [11502]])
y = solve(m, s1)

for i in range(0, 6):
    y[i] = trans(str(y[i][0]))
# print(x)
print(y)

plt.ylabel('时段内的平均等待时间/分钟', size=15)
plt.xlabel('时间段', size=15)
print(m.dot(t))
for i in range(0, 6):
    print(s1[i][0] / sum(m[i]), end=',')

xnew = np.linspace(
    2, 22,
    300)  # 300 represents number of points to make between T.min and T.max

y = spline(v, y, xnew)

plt.plot(xnew, y, 'purple')

# plt.plot(v,y,'purple')
plt.xlim(0, 24, 4)
plt.ylim(0, 200)
plt.xticks(range(0, 28, 4))

plt.show()
예제 #44
0
def make_segments(count, thresh, blur, image, path, newpath,
                  actual_contours_path, segment_path):
    # print image.shape
    # fo = open("processing.txt", "a")
    # print path
    coordinates_of_segments = {}
    temp_coordinates = []
    ret, th = cv2.threshold(blur, thresh, 255, cv2.THRESH_BINARY)

    # individual = 'croppedSegments/'
    xpath = newpath
    imsave(newpath + '/thresholded_image.jpg', th)
    filename = list(path.split('/'))
    filename = filename[-1]
    filename = list(filename.split('\\'))
    filename = filename[-1]
    # print filename
    imsave(thresholded_path + filename, th)

    cnts, hierarchy = cv2.findContours(th, 1, 2)

    thresholded_image = cv2.cv.LoadImage(newpath + '/thresholded_image.jpg')
    pathses = newpath + "/size.txt"
    # delete_file(newpath + '/thresholded_image.jpg')
    actual_image = cv2.imread(path)
    file_name = []
    file_name = path.split("\\")
    file = file_name[-1]
    coordinates_of_segments[file] = []
    # print file,thresh
    loaded_image = cv2.cv.LoadImage(path)
    rect_image = actual_image
    contour_list = []
    mask = np.ones(image.shape[:2], dtype="uint8") * 255

    # loop over the contours
    number = 0
    red_number = 0
    green_number = 0
    global mc_and_dc_list
    segment_list = []
    original_segment_list = []
    coordinates_list = []
    xmin = 10000
    ymin = 10000
    xmax = 0
    ymax = 0
    w1 = 0
    h1 = 0
    counters = 0
    for c in cnts:

        approx = cv2.approxPolyDP(c, 0.009 * cv2.arcLength(c, True), True)
        area = cv2.contourArea(c)

        if ((len(approx) > 8) & (area < 4000) & (area > 100)):

            number += 1
            global w, h
            center, angle, w, h, x1, y1, x2, y2 = get_coordinates(
                actual_image, c, coordinates_list)
            if (x1 - w < xmin):
                xmin = x1 - w
            if (x2 + w > xmax):
                xmax = x2 + w
            if (y1 - h < ymin):
                ymin = y1 - h
            if (y2 + h > ymax):
                ymax = y2 + h
            w1 += w
            h1 += h
            counters = counters + 1
            #            print(x1-w,y1-h,x2+w,y2+h,counters,h1,h)
            crop_th = crop_image(thresholded_image, center, angle, w, h)
            crop = crop_image(loaded_image, center, angle, w, h)
            image = crop
            # create_dir(individual+file+'/')
            cv2.cv.SaveImage(newpath + '/' + 'contour_' + str(number) + '.jpg',
                             crop_th)
            cv2.cv.SaveImage(
                actual_contours_path + 'contour_' + str(number) + '.jpg', crop)
            # cv2.cv.SaveImage(individual+ file+'/' + str(random.randint(1,50000))  + '.jpg',crop)
            temp_image = PIL.Image.open(newpath + '/' + 'contour_' +
                                        str(number) + '.jpg')
            original_temp_image = PIL.Image.open(actual_contours_path +
                                                 'contour_' + str(number) +
                                                 '.jpg')
            segment_list.append(temp_image)
            original_segment_list.append(original_temp_image)

            # image = original_temp_image
            image = skimage.color.rgb2gray(
                skimage.io.imread(actual_contours_path + 'contour_' +
                                  str(number) + '.jpg'))
            delete_file(newpath + '/' + 'contour_' + str(number) + '.jpg')
            delete_file(actual_contours_path + 'contour_' + str(number) +
                        '.jpg')
            total = []
            h = image.shape[0]
            w = image.shape[1]

            for x in xrange(h):
                s = 0
                for y in xrange(w):
                    s += image[x][y]
                total.append(s)

            avg = [sum(total) / len(total)] * len(total)
            T = list(range(len(total)))
            t = np.array(T)
            power = np.array(total)
            totalnew = np.linspace(t.min(), t.max(), len(total))
            power_smooth = spline(t, power, totalnew)

            # ax = axs[1]
            sigma = 3
            x_g1d = gaussian_filter1d(totalnew, sigma)
            y_g1d = gaussian_filter1d(power_smooth, sigma)

            index = []
            temp = 0
            for i in xrange(1, len(y_g1d) - 1):
                if y_g1d[i] > y_g1d[i - 1] and y_g1d[i] > y_g1d[i + 1]:
                    index.append(i)

            if len(index) == 0:
                x_g1d = totalnew
                y_g1d = power_smooth
                for i in xrange(1, len(y_g1d) - 1):
                    if y_g1d[i] > y_g1d[i - 1] and y_g1d[i] > y_g1d[i + 1]:
                        index.append(i)

            cm = []

            for x in xrange(1, len(index)):
                for y in xrange(x):
                    if y_g1d[index[y]] < y_g1d[index[x]]:
                        temp = index[y]
                        index[y] = index[x]
                        index[x] = temp

            if len(index) > 0:
                mx = [y_g1d[index[0]]] * len(total)
                # plt.plot(t,mx)
                cent1 = index[0]
                # ax=axs[0]
                cm1 = (w / 2, cent1)
                cv2.circle(image, cm1, 3, (0, 1, 0), -1)
                cm.append(cm1)
            DCcount = 0

            if len(index) > 1 and y_g1d[index[1]] > avg[0] and abs(
                    y_g1d[cent1] - y_g1d[index[1]]) < abs(y_g1d[index[1]] -
                                                          y_g1d[int(avg[0])]):
                # if len(index)>1 and total[index[1]]>avg[0] and abs(cent1-index[1])>h/a and abs(total[cent1]-total[index[1]])<abs(total[index[1]]-total[int(avg[0])]):
                mx2 = [y_g1d[index[1]]] * len(total)
                # plt.plot(t,mx2)
                cent2 = index[1]
                cm2 = (w / 2, cent2)
                cv2.circle(image, cm2, 3, (0, 1, 0), -1)
                cm.append(cm2)
                DCcount += 1

            if len(cm) == 2:
                red_number += 1
                rect = cv2.minAreaRect(c)
                box = cv2.cv.BoxPoints(rect)
                temp_box = list(box)
                temp_box.append((1, 1))
                box = np.int0(box)

                temp_coordinates.append(temp_box)
                # print "coordinates of dc"
                # print box
                cv2.drawContours(actual_image, [box], 0, (0, 0, 255), 2)
            else:
                green_number += 1
                rect = cv2.minAreaRect(c)
                box = cv2.cv.BoxPoints(rect)
                temp_box = list(box)
                temp_box.append((0, 0))
                box = np.int0(box)

                temp_coordinates.append(temp_box)
                # print "coordinates of mc"
                # print box
                cv2.drawContours(actual_image, [box], 0, (0, 255, 0), 2)
    f = open(pathses, 'w')
    ##print(w1,h1,counters,w1/(counters),h1/(counters))
    #print(pathses)
    #print("dsfj sdjfhjdsfdsfjdsjf jdsfjds jfdsjjf sjfdjsfjdsh")
    f.write('{}  {}  {}  {}  {}  {}'.format(xmin, ymin, xmax, ymax,
                                            w1 / (counters), h1 / (counters)))
    f.close()
    coordinates_of_segments[file] = temp_coordinates
    cv2.imwrite(segment_path + filename, actual_image)
    time.sleep(0.30)
    #cv2.waitKey(50)
    print "^^^^^^^^^^^^^^", time.time()
    i = count + 1
    # while soft_sheet['A'+str(i)]==None:
    soft_sheet['A' + str(i)] = file
    soft_sheet['B' + str(i)] = number
    if not DCcount == 0:
        soft_sheet['C' + str(i)] = DCcount
    pathses, tail = os.path.split(pathses)
    pathses, tail = os.path.split(pathses)
    soft_data.save('data.xlsx')
    mc_and_dc_list.append([red_number, green_number, number, tail[8:]])
    return mc_and_dc_list, number, segment_list, original_segment_list, coordinates_list, coordinates_of_segments
예제 #45
0
ff=0
qq=float(1)
for i in range(M):
	ff=ff+W[i]*qq
	qq=qq*xx

ew1=0.0
for i in range(100):
	ew1=ew1+(ff[i]-YY[i])**2
eww1=np.sqrt(ew1/100)


#显示误差率
print("训练集误差率:"+str(eww))
print("测试集误差率:"+str(eww1))


# 画出这些点
xnew = np.linspace(X.min(),X.max(),300)
power_smooth = spline(X,test,xnew)

xnew1 = np.linspace(x.min(),x.max(),300)
power_smooth1 = spline(x,f,xnew1)

pt.plot(xnew,power_smooth,color="blue",label='sin(2*pi*x)')
pt.plot(xnew1,power_smooth1,color="red",label='fx')

pt.plot(X,Y,linestyle='',marker='.',label='traindata')

pt.legend(bbox_to_anchor=(1.0, 1), loc=1, borderaxespad=0.)
pt.show()
예제 #46
0
        noise_.append(test_noise)
    else:
        noise_.append(0)

    # Record data
    y_.append(y)
    setpoints_.append(pid.set_point)
    t_.append(i)
    outs_.append(output)

    # Let some time pass
    time.sleep(0.01)

# Smooth out rough edges
t_sm = np.linspace(min(t_), max(t_), 400)
y_sm = spline(t_, y_, t_sm)
outs_sm = spline(t_, outs_, t_sm)
fig = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(t_sm, y_sm, label='y')
plt.plot(t_, setpoints_, label='Setpoint')
plt.grid(True)
plt.xlim([0, max(t_)])
plt.title("PID controller on y variable")
plt.legend()
plt.subplot(2, 1, 2)
plt.plot(t_sm, outs_sm, label='u', color='red')
plt.plot(t_, noise_, label='Noise', color='black')
plt.grid(True)
plt.xlim([0, max(t_)])
plt.ylim([-3, test_noise + 1])
예제 #47
0
table.finalize(heavy=False, divider=True)
#table.print_header()

print
print 'Most recent '+str(trend_days)+'-day savings trend:', '$' + format_balance(trends[-1]/12) + ' per month,', \
                                                            '$' + format_balance(trends[-1]) + ' per year'

(x_data, total_balances_data, checking_balances_data, savings_balances_data,
 credit_balances_data, bank_balances_data, total_balance_diffs,
 bank_balance_diffs, credit_balance_diffs, bank_rises, bank_small_falls,
 bank_large_falls) = collapsed_calculated if plot_collapse_days else calculated

##### Plot output

x_smooth = np.linspace(min(x_data), max(x_data), 1000)
bank_balances_smooth = spline(x_data, bank_balances_data, x_smooth)
total_balances_smooth = spline(x_data, total_balances_data, x_smooth)
checking_balances_smooth = spline(x_data, checking_balances_data, x_smooth)
savings_balances_smooth = spline(x_data, savings_balances_data, x_smooth)
credit_balances_smooth = spline(x_data, credit_balances_data, x_smooth)

xs = x_data
bank_balances_ys = bank_balances_data
total_balances_ys = total_balances_data
checking_balances_ys = checking_balances_data
savings_balances_ys = savings_balances_data
credit_balances_ys = credit_balances_data

smooth = False

if smooth:
예제 #48
0
def smooth(xspan, data):
    x_points = np.linspace(xspan[0], xspan[-1],
                           25)  #change number for 'smoothness' level
    smooth_points = spline(xspan, data, x_points)
    return (x_points, smooth_points)
예제 #49
0
def plotScatters(L_f,
                 K_t,
                 lo_i=0,
                 outputDir="/Users/jimmy/Code/stochfilaments/output/",
                 saveDir="/Users/jimmy/Desktop/"):

    #ANALYTIC DATA
    #lo for cross-linkers only (Kt=0.01)
    loa_cl = [
        0, 0.1295, 0.6966, 2.7600, 9.2886, 26.4296, 59.7827, 72.1158, 51.0522,
        37.9080, 29.8891, 24.6716, 21.0468, 18.3768, 16.3464, 14.7404
    ]

    #Caclulate steady-state
    lo_ss = []
    for energy in ref_e:
        lo_ss.append(energy * common.kT *
                     (1.0 / (1.0 + (v / v_b) * math.exp(-energy))) /
                     (2.0 * 10.0 * K_t))

    mpl.rcParams['axes.color_cycle'] = ['y', 'k', 'darkgreen']
    #mpl.rcParams['axes.color_cycle'] = ['b', 'g', 'r', 'c']
    #mpl.rcParams['axes.color_cycle'] = ['darkorange', 'blueviolet', 'brown']

    numRuns = 250.0

    #open data
    l = pickle.load(
        open(outputDir + "/meanl-Lf%0.3f-Kt%0.3f.p" % (L_f, K_t), "rb"))
    err_l = pickle.load(
        open(outputDir + "/errmeanl-Lf%0.3f-Kt%0.3f.p" % (L_f, K_t), "rb"))

    l_s = pickle.load(
        open(outputDir + "/meanls-Lf%0.3f-Kt%0.3f.p" % (L_f, K_t), "rb"))
    err_ls = pickle.load(
        open(outputDir + "/errmeanls-Lf%0.3f-Kt%0.3f.p" % (L_f, K_t), "rb"))

    n = pickle.load(
        open(outputDir + "/meann-Lf%0.3f-Kt%0.3f.p" % (L_f, K_t), "rb"))
    err_n = pickle.load(
        open(outputDir + "/errmeann-Lf%0.3f-Kt%0.3f.p" % (L_f, K_t), "rb"))

    k_minus = ref_k_minus * L_f

    initVal = l[0] * K_t
    initErr = err_l[0]

    #Plot overlap
    #plt.figure(4, figsize = (2,2))
    #pyplot.locator_params(nbins=4)
    #plt.errorbar(ref_e, l, fmt='o-', yerr=err_l, label=r'$\mathsf{K_t = %g \/ pN / nm}$' % (K_t))

    #plt.xlabel(r'$\mathsf{\epsilon\/ (k_bT)}$', fontsize=12)
    #plt.ylabel(r'$\mathsf{\langle l_{overlap} \rangle\/(nm)}$', fontsize=12)

    #plt.savefig(saveDir + '/lmeanvsg0.pdf', format='pdf')

    #Plot force
    plt.figure(5, figsize=(2, 2))
    pyplot.locator_params(nbins=8)
    #plt.ylim((-0.1,5.5))
    #plt.xlim((3,15))

    #smooth data
    ehat = np.linspace(min(ref_e), max(ref_e), 100)
    lhat = spline(ref_e, l, ehat)

    lhat_cl = spline(ref_e, loa_cl, ehat)
    lhat_ss = spline(ref_e, lo_ss, ehat)

    print[(x - lo_i) * K_t for x in l]

    plt.plot(ehat, [(x - lo_i) * K_t for x in lhat],
             markersize=2,
             linewidth=2.0,
             label=r'$\mathsf{k_{off}^{cl} = %g \/ s^{-1}}$' % (k_minus))

    #plt.plot(ehat, [(x - lo_i) * K_t for x in lhat_cl], 'k-', markersize=1, linewidth=2.0)
    #plt.plot(ehat, [(x - lo_i) * K_t for x in lhat_ss], 'k--', markersize=3, linewidth=2.0)

    plt.xlabel(r'$\mathsf{\epsilon\/ (k_bT)}$', fontsize=12)
    plt.ylabel(r'$\mathsf{\langle F \rangle\/(pN)}$', fontsize=12)

    plt.savefig(saveDir + '/fmeanvsg0.pdf', format='pdf')
예제 #50
0
import pandas as pd
from scipy.interpolate import UnivariateSpline as spline

data = pd.read_csv("input", sep="\t")
data.index = range(data.shape[0])
data.columns = ["y"]
data["x"] = range(data.shape[0])

# position of the missing price
pos_missing = data.y.str.startswith("Missing")
data_missing = data[pos_missing]
data_training = data[pos_missing == False]

spline_model = spline(data_training.x, data_training.y, k=3, s=3)
pred_price = spline_model(data_missing.x)
print(*pred_price, sep="\n")
        tong = tong + in_image[i, j]
    luminance.append([i, tong // B[i][3]])

axisx = []
axisy = []
deriy = []
# print luminance
print luminance
size = len(luminance)
deriy.append(0)
for i in range(0, len(luminance)):
    axisx.append(luminance[i][0])
    axisy.append(luminance[i][1])

xnew = np.linspace(axisx[0], axisx[359], 360)
power_smooth = spline(axisx, axisy, xnew)
for i in range(1, size - 1):
    deriy.append((luminance[i + 1][1] - luminance[i - 1][1]) / 6)
deriy.append((luminance[size - 1][1] - luminance[size - 2][1]) / 3)

# in ra vi tri vi
# for i in range(0, 360):
#     if (deriy[i] == min(deriy)):
#         print i

# plt.plot(axisx, axisy, 'r'),
plt.plot(xnew, power_smooth, 'b')
plt.xlabel('Bandwidth Heigh (Image Heigh)')
plt.ylabel('Intensity Value')
plt.show()
예제 #52
0
def create_graphs_NN(X, Y, learning_rate, max_iterations, dataset_name,
                     random_state):

    train_accuracies = list()
    test_accuracies = list()
    train_times = list()
    predict_times = list()

    smooth = np.linspace(max_iterations.min(), max_iterations.max(), 300)
    for alpha in learning_rate:

        train_acc_list_iter = list()
        test_acc_list_iter = list()
        time_train_list_iter = list()
        time_predict_list_iter = list()
        for max_iter in max_iterations:

            nn = MLPClassifier(learning_rate_init=alpha,
                               activation='logistic',
                               solver='sgd',
                               max_iter=max_iter)

            # Create Training and Testing Data
            # train_X, test_X, train_Y, test_Y = train_test_split(X, Y, test_size=.20)

            kfold = KFold(n_splits=5, shuffle=True)

            train_acc_list = list()
            test_acc_list = list()
            time_train_list = list()
            time_predict_list = list()

            for train_index, test_index in kfold.split(X):
                train_X, test_X = X[train_index], X[test_index]
                train_Y, test_Y = Y[train_index], Y[test_index]
                # Create and Train Model

                # Train and Get Time
                time_start = time.time()
                nn.fit(train_X, train_Y)
                time_train = time.time() - time_start

                time_start = time.time()
                predict_Y_test = nn.predict(test_X)
                time_predict = time.time() - time_start

                predict_Y_train = nn.predict(train_X)

                test_acc = accuracy_score(test_Y, predict_Y_test)
                train_acc = accuracy_score(train_Y, predict_Y_train)

                train_acc_list.append(train_acc)
                test_acc_list.append(test_acc)
                time_train_list.append(time_train)
                time_predict_list.append(time_predict)

            train_acc_list_iter.append(np.mean(train_acc_list))
            test_acc_list_iter.append(np.mean(test_acc_list))
            time_train_list_iter.append(np.mean(time_train_list))
            time_predict_list_iter.append(np.mean(time_predict_list))

        train_accuracies.append(train_acc_list_iter)
        test_accuracies.append(test_acc_list_iter)
        predict_times.append(time_predict_list_iter)
        train_times.append(time_train_list_iter)

    colors = plt.cm.tab20(np.linspace(0, 1, 19))
    plot_names = list()
    for name in learning_rate:
        name = 'Learning Rate: %s' % name
        plot_names.append(name)

    plot_names = np.asarray(plot_names)
    for i in range(len(learning_rate)):
        # plt.plot(smooth_perc, 100 * spline(train_sizes, train_acc_list_iter[i], smooth))
        plt.plot(smooth,
                 100 * spline(max_iterations, test_accuracies[i], smooth),
                 color=colors[2 * (i - 1)])
        # plt.plot(max_iterations, test_accuracies[i])
    plt.legend(plot_names, loc='upper left', bbox_to_anchor=(1.05, 1))
    plt.xlabel("Epochs")
    plt.ylabel("Accuracy (%)")
    plt.title('%s - Accuracy vs Epochs with Varying Learning Rates' %
              dataset_name)
    plt.show()

    for i in range(len(learning_rate)):
        plt.plot(smooth,
                 spline(max_iterations, train_times[i], smooth),
                 color=colors[2 * (i - 1)])
    plt.legend(plot_names, loc='upper left', bbox_to_anchor=(1.05, 1))
    plt.xlabel("Epochs")
    plt.ylabel("Time (sec)")
    plt.title('%s - Training Time vs Epochs with Varying Learning Rates' %
              dataset_name)
    plt.show()

    for i in range(len(learning_rate)):
        plt.plot(smooth,
                 spline(max_iterations, predict_times[i], smooth),
                 color=colors[2 * (i - 1)])
    plt.legend(plot_names, loc='upper left', bbox_to_anchor=(1.05, 1))
    plt.xlabel("Epochs")
    plt.ylabel("Time (sec)")
    plt.title('%s - Predicting Time vs Epochs with Varying Learning Rates' %
              dataset_name)
    plt.show()
예제 #53
0
        rewardPerEpisode[0].append(i_episode)
        rewardPerEpisode[1].append(sum_reward)

        # print("eps", epsilon, "reward", sum_reward)
        # END OF EPISODE

        if i_episode % 100 == 0 and i_episode != 0 and len(
                lossPerTimeframe[0]) > 0:
            ## Plotting stuff
            plt.clf()

            xnew = np.linspace(
                np.array(rewardPerEpisode[0]).min(),
                np.array(rewardPerEpisode[0]).max(),
                float(EPISODES) / SMOOTH)
            power_smooth = spline(rewardPerEpisode[0], rewardPerEpisode[1],
                                  xnew)
            fig = plt.figure(0)
            ax1 = fig.add_subplot(111)
            ax1.plot(xnew, power_smooth, color='blue')
            plt.xlim(0, EPISODES)
            # plt.axis([0., EPISODES, 0., 500.])
            plt.title(ENVIRONMENT)
            plt.xlabel('episode')
            plt.ylabel('reward')
            plt.savefig(ENVIRONMENT + '_reward')

            plt.clf()
            fig = plt.figure(1)
            ax1 = fig.add_subplot(111)
            ax1.semilogy(lossPerTimeframe[0], lossPerTimeframe[1], color='red')
            # ax1.loglog(lossPerTimeframe[0], lossPerTimeframe[1], color='red')
예제 #54
0
def cuveify(x, y, p=50):
    new_x = np.linspace(min(x), max(x), p)
    new_y = spline(x, y, new_x)
    return new_x, new_y
def plot_uncertainty_mip(group):
    table = group.E_1PeV.zenith_22_5
    rec = DirectionReconstruction

    # constants for uncertainty estimation
    station = table.attrs.cluster.stations[0]
    r1, phi1 = station.calc_r_and_phi_for_detectors(1, 3)
    r2, phi2 = station.calc_r_and_phi_for_detectors(1, 4)

    R_list = get_median_core_distances_for_mips(group, range(1, 6))

    figure()
    x, y, y2 = [], [], []
    for N in range(1, 5):
        x.append(N)
        events = table.read_where('min_n134>=%d' % N)
        #query = '(n1 == N) & (n3 == N) & (n4 == N)'
        #vents = table.read_where(query)
        print len(events),
        errors = events['reference_theta'] - events['reconstructed_theta']
        # Make sure -pi < errors < pi
        errors = (errors + pi) % (2 * pi) - pi
        errors2 = events['reference_phi'] - events['reconstructed_phi']
        # Make sure -pi < errors2 < pi
        errors2 = (errors2 + pi) % (2 * pi) - pi
        #y.append(std(errors))
        #y2.append(std(errors2))
        y.append(
            (scoreatpercentile(errors, 83) - scoreatpercentile(errors, 17)) /
            2)
        y2.append(
            (scoreatpercentile(errors2, 83) - scoreatpercentile(errors2, 17)) /
            2)
        print "YYY", rad2deg(
            scoreatpercentile(errors2, 83) - scoreatpercentile(errors2, 17))

    plot(x, rad2deg(y), '^', label="Theta")
    plot(x, rad2deg(y2), 'v', label="Phi")
    Sx = x
    Sy = y
    Sy2 = y2
    print
    print "mip: min_n134, theta_std, phi_std"
    for u, v, w in zip(x, y, y2):
        print u, v, w
    print
    utils.savedata((x, y, y2))

    # Uncertainty estimate
    x = [1, 2, 3, 4, 5]
    phis = linspace(-pi, pi, 50)
    phi_errsq = mean(rec.rel_phi_errorsq(pi / 8, phis, phi1, phi2, r1, r2))
    theta_errsq = mean(rec.rel_theta1_errorsq(pi / 8, phis, phi1, phi2, r1,
                                              r2))
    y = TIMING_ERROR * std_t(x) * sqrt(phi_errsq)
    y2 = TIMING_ERROR * std_t(x) * sqrt(theta_errsq)

    mc = my_std_t_for_R(data, x, R_list)
    for u, v in zip(mc, R_list):
        print v, u, sqrt(u**2 + 1.2**2), sqrt((.66 * u)**2 + 1.2**2)
    mc = sqrt(mc**2 + 1.2**2)
    y3 = mc * sqrt(phi_errsq)
    y4 = mc * sqrt(theta_errsq)

    nx = linspace(1, 4, 100)
    y = spline(x, y, nx)
    y2 = spline(x, y2, nx)
    y3 = spline(x, y3, nx)
    y4 = spline(x, y4, nx)

    plot(nx, rad2deg(y), label="Gauss Phi")
    plot(nx, rad2deg(y2), label="Gauss Theta")
    plot(nx, rad2deg(y3), label="Monte Carlo Phi")
    plot(nx, rad2deg(y4), label="Monte Carlo Theta")
    # Labels etc.
    xlabel("Minimum number of particles")
    ylabel("Angle reconstruction uncertainty [deg]")
    #title(r"$\theta = 22.5^\circ$")
    legend(numpoints=1)
    xlim(.5, 4.5)
    utils.saveplot()
    print

    graph = GraphArtist()
    graph.plot(Sx, rad2deg(Sy), mark='o', linestyle='only marks')
    graph.plot(Sx, rad2deg(Sy2), mark='*', linestyle='only marks')
    graph.plot(nx, rad2deg(y), mark=None, linestyle='dashed,smooth')
    graph.plot(nx, rad2deg(y2), mark=None, linestyle='dashed,smooth')
    graph.set_xlabel("Minimum number of particles")
    graph.set_ylabel(r"Reconstruction uncertainty [\si{\degree}]")
    graph.set_xticks(range(1, 5))
    graph.set_ylimits(0, 32)
    artist.utils.save_graph(graph, dirname='plots')
    graph.plot(nx, rad2deg(y3), mark=None, linestyle='smooth')
    graph.plot(nx, rad2deg(y4), mark=None, linestyle='smooth')
    artist.utils.save_graph(graph, suffix='full', dirname='plots')
예제 #56
0
# for i in np.arange(0.0, 1.1, 0.1):
#     x1.append(i)
#     y1.append(i**2)
#
#
# for i in np.arange(0.0, 1.1, 0.1):
#     x2.append(i)
#     y2.append(np.math.sqrt(i))

# xl=[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5]
# yl=[10.0, 4.44, 2.5, 1.6, 1.11, 0.82, 0.62, 0.49, 0.4, 0.33]

x_f1 = np.array(x1)
y_f1 = np.array(y1)
xnew_f1 = np.linspace(x_f1.min(), x_f1.max(), 50)
ynew_f1 = spline(x_f1, y_f1, xnew_f1)

x_f2 = np.array(x1)
y_f2 = np.array(y1)
xnew_f2 = np.linspace(x_f2.min(), x_f2.max(), 50)
ynew_f2 = spline(x_f2, y_f2, xnew_f2)

plt.subplot(311)
plt.plot(x1, y1, 'bo', x1, y1, 'k')

plt.subplot(311)
plt.plot(x2, y2, 'bo', x2, y2, 'k')

plt.title('Grafik funkcije y(x)')
# #plt.xlabel('x axis')
# plt.ylabel('y osa')
예제 #57
0
def make_elevation_plots(DataDirectory, DEM_prefix, DEM_swathed, info_file,
                         data_file, field_site, smoothing, OutputFigureName):
    """
    Make nice plots of the elevations along the swath
    """

    # Set up fonts
    rcParams['font.family'] = 'sans-serif'
    rcParams['font.sans-serif'] = ['Liberation Sans']
    rcParams['font.size'] = 14

    distance, MeanValues, MinValues, MaxValues, Nlines, info = read_all_data(
        DataDirectory, DEM_prefix, info_file, data_file)

    # add the XY Plot
    fig = plt.figure(1, figsize=(cm2inch(25), cm2inch(15)))
    # set ratios for subplots
    ax = fig.add_subplot(111)

    distance = np.array(distance)
    if (smoothing):

        MeanValues = np.array(MeanValues)
        MinValues = np.array(MinValues)
        MaxValues = np.array(MaxValues)
        smooth_k = 0.7

        distance_interpolate = np.linspace(distance.min(),
                                           distance.max(),
                                           num=Nlines * smooth_k)
        MeanValues_smooth = spline(distance,
                                   MeanValues,
                                   distance_interpolate,
                                   order=1)
        MinValues_smooth = spline(distance,
                                  MinValues,
                                  distance_interpolate,
                                  order=1)
        MaxValues_smooth = spline(distance,
                                  MaxValues,
                                  distance_interpolate,
                                  order=1)

        ax.plot(distance_interpolate,
                MeanValues_smooth,
                'k-',
                lw=0.75,
                label="Mean")
        ax.plot(distance_interpolate,
                MinValues_smooth,
                '-',
                c='gray',
                lw=0,
                label="Max/min")
        ax.plot(
            distance_interpolate,
            MaxValues_smooth,
            '-',
            c='gray',
            lw=0,
        )
        ax.fill_between(distance_interpolate,
                        MaxValues_smooth,
                        MinValues_smooth,
                        color='#D5D5D5')
        plt.xlabel('Distance along swath (m)')
        plt.ylabel('Elevation (m)')
    else:
        ax.plot(distance, MeanValues, 'k-', lw=0.75, label="Mean")
        ax.plot(distance, MinValues, '-', c='gray', lw=0, label="Max/min")
        ax.plot(
            distance,
            MaxValues,
            '-',
            c='gray',
            lw=0,
        )
        ax.fill_between(distance, MaxValues, MinValues, color='#D5D5D5')
        plt.xlabel('Distance along swath (m)')
        plt.ylabel('Elevation (m)')

    temp = np.array(MinValues)
    temp2 = np.array(MaxValues)

    print(
        'I am plotting the data, the minimum altitude is %s and the maximum is %s.'
        % (temp.min(), temp2.max()))

    plt.ylim(temp.min() - temp.min() / 20, temp2.max() + temp2.max() / 20)
    plt.xlim(0, distance.max())
    plt.title(field_site)
    plt.legend(loc='upper right')

    # Save figure
    OutputFigureFormat = 'png'
    plt.savefig(DataDirectory + OutputFigureName + '.' + OutputFigureFormat,
                format=OutputFigureFormat,
                dpi=1000)
    #plt.tight_layout()
    plt.close()
예제 #58
0
    trainX = numpy.reshape(trainX, (trainX.shape[0], look_back))
    predictions = model.predict(h2o.H2OFrame(trainX))
    time.sleep(1)
    predictions = predictions.as_data_frame(use_pandas=True)
    predictions = numpy.array(predictions)
    dataset = numpy.append(dataset, predictions[-1])
    dataset = numpy.reshape(dataset, (dataset.shape[0], 1))

dataset = scaler.inverse_transform(dataset)
predictions = scaler.inverse_transform(predictions)
# To plot whole predictions
plt.style.use('ggplot')
x = numpy.arange(0, len(predictions))
y = numpy.array(dataset[len(dataset) - len(predictions):, ])
x_smooth = numpy.linspace(x.min(), x.max(), len(predictions) * 20)
y_smooth = spline(x, y, x_smooth)
z_smooth = spline(x, predictions, x_smooth)
plt.plot(x_smooth, y_smooth)
plt.plot(x_smooth, z_smooth, 'g')

# To plot forecast length
# x = numpy.arange(len(dataset) - forecast_length - 1, len(dataset))
# y = dataset[len(dataset) - forecast_length - 1:, ]
# x_smooth = numpy.linspace(x.min(), x.max(), 500)
# y_smooth = spline(x, y, x_smooth)
# plt.plot(x_smooth, y_smooth, 'g')

print(dataset)
plt.show()
h2o.remove_all()
예제 #59
0
                # rotate array
                data[i] = roll(data[i], -1)
                data[i][-1] = num
                count = num_p - 1
            else:
                data[i][count] = num

        # gyro
        pl.subplot(211)
        if (bar):
            pl.bar(0, data[1][count], color='b')
            pl.bar(1, data[2][count], color='g')
            pl.bar(2, data[3][count], color='r')
            pl.axis([0, 3, -300, 300])
        elif (smooth):
            pl.plot(T, spline(t, data[1], T), 'b')
            pl.plot(T, spline(t, data[2], T), 'g')
            pl.plot(T, spline(t, data[3], T), 'r')
            pl.axis([0, num_p, -300, 300])
        else:
            pl.plot(t, data[1], 'b')
            pl.plot(t, data[2], 'g')
            pl.plot(t, data[3], 'r')
            pl.axis([0, num_p, -300, 300])
        pl.title('Gyroscope')
        pl.ylabel('deg/s')

        # accel
        pl.subplot(212)
        if (bar):
            pl.bar(0, data[4][count], color='b')
def createfile(dest):
    """
    The script creates a text file at the passed in location,
    names file based on date
    """

    date_format = t.localtime(t.time())
    today = ' %d_%d_%d'%(date_format[2],date_format[1],date_format[0])
    name_date=' %d_%d_%d.txt'%(date_format[2],date_format[1],date_format[0])#assigning the file name andformat
    ##FileName = name_of_practical + Day_Month_Year


    name_of_practical = raw_input('Enter practical name: ')
       
    if not(path.isfile(dest+name_of_practical + name_date)):#checks if file already exists

        f = open(name_of_practical + name_date,'w')
        print("\nBiochemistry electronic write-up system\n")

        name = raw_input("Enter your name: ")
        f.write("Name: ")
        f.write(name)
        f.write('\n')
        
        Reg_number = raw_input("Enter your registration number: ")
        f.write("Reg_number: ")
        f.write(Reg_number)
        f.write('\n')
        
        program = raw_input("Enter your program name: ")
        f.write("Program: ")
        f.write(program)
        f.write('\n')
        
        f.write("Date: ")
        f.write(today)
        f.write('\n')
        
        coordinator = raw_input("Enter coordinator names: ")
        f.write("Coordinator(s): ")
        f.write(coordinator)
        f.write('\n'*2)
        
        f.write('\t\t\t')
        f.write(name_of_practical)
        f.write('\n'*2)
        
        f.write("Introduction\n")
        f.write(raw_input("\nEnter the introduction of the experiment:\n"))
        f.write('\n'*2)
        
        f.write("Materials and methods\n")
        f.write(raw_input("\nName the materials and methods used in the experiment:\n"))
        f.write('\n'*2)
        
        ##
        f.write("Recording results\n")
        print("Recording results\n")
        ##
        while True:
            try:
                upper_bound=int(raw_input("Enter the total number of recordings: "))
                while upper_bound <=4:#this is to make sure that all values entered are positive and atleast 5
                    print("please try again, you need a minimum of 5 readings")#the error message for negative numbers and values less than 5
                    upper_bound=int(raw_input("Enter the total number of x and y coordinates: "))
                break
            except ValueError:#this lets the program know what to do with with the errors by entering non-numeric data
                print("Non-numeric characters are invalid, please enter a positive number for the upper bound of atleast 5")
       
        # 
        x=[]
        x_axis=raw_input("What is the variable for the x-axis: ")       
                 
        for i in range (upper_bound):
            while True:
                try:
                    x.append(float(raw_input("Enter the value for the x-axis: ")))
                    break
                except ValueError:#this lets the program know what to do with with the errors by entering non-numeric data
                    print("Non-numeric characters are invalid, please enter numbers")      
            print(x)
            
        list_x = np.array(x)
        f.write('Results for ')
        f.write(x_axis)
        f.write('\n')
        f.write(str(x))
        f.write('\n')
        
        ##
        y = []
        y_axis = raw_input("\nwhat is the the variable for the y-axis: ")
        
        for i in range (upper_bound):
            while True:
                try:
                    y.append(float(raw_input("Enter the value for the y-axis: ")))
                    break
                except ValueError:#this lets the program know what to do with with the errors by entering non-numeric data
                    print("Non-numeric characters are invalid, please enter numbers")      
            print(y)
            
        list_y = np.array(y)
        f.write('\nResults for ')
        f.write(y_axis)
        f.write('\n')
        f.write(str(y))
        f.write('\n'*2)
        ######
        
        # Label the x and y axes and title the graph.
        
        xlabel(x_axis)
        ylabel(y_axis)
        title(name_of_practical)
        
        x_smooth = np.linspace(list_x.min() ,list_x.max(),300)
        y_smooth = spline(list_x, list_y, x_smooth)
        plt.plot(x_smooth,y_smooth)
        plt.show()
                
        f.write("Analysis\n")
        f.write(raw_input("\nEnter the analysis of the experiment:\n"))
        f.write("\n"*2)
        
        f.write("Discussion\n")
        f.write(raw_input("\nEnter the discussion of the experiment:\n"))
        f.write("\n"*2)
        
        f.write("Conclusion\n")
        f.write(raw_input("\nEnter the conclusion of the experiment:\n"))
        f.write("\n"*2)
        
        f.write("References\n")
        f.write(raw_input("\nEnter the refrences of the experiment:\n"))
        f.write("\n"*2)
        
        
        f.close()