def _create_state_labels(self): num_vert = self.number_of_vertices # Implement a way in the future for >26 vertices multiport if num_vert > len(ascii_uppercase): raise KeyError(' This code does not generate multiports with more\ than one 26 vertices.') if num_vert < 3: raise KeyError('Multiport must have atleast 3 verices.') nums = cycle(['1', '3', '2', '4', 'X']) # Iterable 1,3,2,4,X,1,3,2.. label_array = np.chararray((num_vert, 5)) # num_vert by 5 arr of '' for row in prange(num_vert): vert = deque(self.vertex_labels) vert.rotate(-row) labs = list(vert) labs = [labs[x] for x in [0, 1, -1]] # connected vertices NN labs.extend(labs[0] * 2) # Creates 3 by 5 arr of ABCAA, BCABB, CABCC label_array[row, :] = labs label_array = label_array.flatten().tolist() # ABCAABCABBCABCC label_array = [ label_array[i].decode() for i in prange(len(label_array)) ] # Convert Bytes to Str return [i + next(nums) for i in label_array] # A3, B2, C4, AX etc
def odd_even_sort(arr): n = len(arr) for i in range(len(arr)): if i & 1 == 0: with nogil, parallel.parallel(): for j in prange(0, n - 1, 2): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] else: with nogil, parallel.parallel(): for j in prange(1, n - 1, 2): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] print("iteration %d:" % (i + 1), *arr)
def get_similarity_score(sent_pickle,dep_pickle,alpha): #load the pickle files sent_mx=load_pickle('',sent_pickle) dep_mx=load_pickle('',dep_pickle) sent_num=len(sent_mx) score_dic={} #calculate the similarity for ii in tqdm(prange(sent_num)): # ii means the ii-th sentence similarity_arr=np.zeros(sent_num) similarity_arr=list(similarity_arr) for jj in range(sent_num): if jj==ii: similarity_arr[jj]=1 else: similarity_arr[jj]=alpha*cosine_similarity(sent_mx[ii],sent_mx[jj])+(1-alpha)*cosine_similarity(dep_mx[ii],dep_mx[jj]) top_k_nearest=np.argsort(similarity_arr)[-100:] top_k_score=[similarity_arr[i] for i in top_k_nearest] score_dic[str(ii)]=(top_k_nearest,top_k_score) #print(score_dic[str(ii)]) with open('./data/ds/score_dic.pickle', 'wb') as f: # Pickle the 'data' dictionary using the highest protocol available. pickle.dump(score_dic, f, pickle.HIGHEST_PROTOCOL) return score_dic
def integrate_async(f, a: cy.double, b: cy.double, *, n_jobs: cy.int = 2, n_iter: cy.int = 1000): executor = ftres.ThreadPoolExecutor(max_workers=n_jobs) spawn = partial(executor.submit, integrate, f, n_iter=n_iter // n_jobs) step = (b - a) / n_jobs fs = [spawn(a + i * step, a + (i + 1) * step) for i in prange(n_jobs, nogil=True)] return sum(f.result() for f in ftres.as_completed(fs))
def prange_regression(n: cython.int, data: list): """ >>> prange_regression(10, list(range(1, 4))) 19 """ s: cython.int = 0 i: cython.int d: cython.int[3] = data for i in prange(n, num_threads=3, nogil=True): s += d[i % 3] return s
def lg_m_bin( ): data_dir = '../huge-results' mb = merger_post_process( data_dir ); z_list = np.loadtxt( 'redshift_list' ); z_list = np.append( 0, z_list ); n_reprod = 100; bins = 100; for i in prange( z_list.size, schedule='dynamic' ): z = z_list[ i ]; mb.read_files( z, n_reprod ); mb.mass_bin_all( );
def prange_with_gil_call_nogil(n: cython.int, x): """ >>> sum(3*i for i in range(10)) 135 >>> prange_with_gil(10, 3) 135 """ i: cython.int s: cython.int = 0 for i in prange(n, num_threads=3, nogil=True): with cython.gil: s += use_nogil(x, i) return s
def pd(n): """Prime decomposition function.""" while n % 2 == 0: yield 2 n /= 2 q = w(n) for i in prange(start=3, stop=q, step=2, nogil=True, schedule='guided'): while n % i == 0: yield 2 n /= i if n > 2: yield 2
def measurement(nodes, psi): """ @brief Makes a measurement of the state of the particle in the quantum walk with a matrix of probability values for each node in the circle. @param nodes, the number of nodes in the circle @param psi, the output state of the quantum walk @return array of probability amplitudes for each node on the circle """ prob = [] for i in prange(nodes): m_p = basis(nodes, i) * basis(nodes, i).dag() # |x><x| Outer Prod measure = tensor(qeye(2), m_p) prob_amp = abs((psi * measure).tr()) # Probability Amplitude prob.append(prob_amp) return prob
def getDisparity(left_img, right_img, patch_radius, min_disp, max_disp): """% left_img and right_img are both H x W and you should return a H x W % matrix containing the disparity d for each pixel of left_img. Set % disp_img to 0 for pixels where the SSD and/or d is not defined, and for d % estimates rejected in Part 2. patch_radius specifies the SSD patch and % each valid d should satisfy min_disp <= d <= max_disp.""" """ For each pixel in the left image, find the matching pixel in the same row in the right image via SSD""" H, W = left_img.shape[:2] disp_img = np.zeros((H, W)) start_time = time.monotonic() for row in prange(patch_radius, H - patch_radius, schedule='static'): for col in range(patch_radius + max_disp, W - patch_radius): left_patch = left_img[row - patch_radius:row + patch_radius + 1, col - patch_radius:col + patch_radius + 1] ssd_arr = [] for disp in range(min_disp, max_disp + 1): right_patch = right_img[row - patch_radius:row + patch_radius + 1, col - patch_radius - disp:col + patch_radius + 1 - disp] ssd = np.sum((left_patch - right_patch)**2) ssd_arr.append(ssd) ssd_arr = np.asarray(ssd_arr) min_ssd = np.min(ssd_arr) min_idx = np.argmin(ssd_arr) best_disp = min_idx + min_disp num_similar = np.sum(ssd_arr <= 1.5 * min_ssd) if (min_disp < best_disp < max_disp) and num_similar <= 3: # Sub-pixel refinement x = [best_disp - 1, best_disp, best_disp + 1] y = ssd_arr[min_idx - 1:min_idx + 2] p = np.polyfit(x, y, deg=2) best_disp = -p[1] / (2 * p[0]) disp_img[row, col] = best_disp end_time = time.monotonic() print(f"Total Time: {end_time-start_time}") return disp_img
def n2means(samples, mask): global indi n = 5 print('Idividual: ', indi) indi += 1 new_samples = [] cdef int num_thteads with nogil, parallel(): num_thteads = openmp.omp_get_num_threads() for sample in prange(samples, nogil=True): new_sample = [] print(len(mask)) for j in range(1, len(samples[0])): if mask[j] == 1: new_sample.append(sample[j]) new_samples.append([sample[0], np.array(new_sample)]) best_score = np.inf best_model = None for i in range(n): groups = kmeans(new_samples, 2) e0, e1 = eval_group(groups) erro = 0 if e0['ALL'] >= e0['AML']: erro += e0['AML'] erro += e1['ALL'] else: erro += e1['AML'] erro += e0['ALL'] if erro <= best_score: best_score = erro best_model = groups return best_score
def quantum_walk(state, t_step, nodes, angle): """ @brief Applies the unitary transformation t_step times on a line of length modulo the number of nodes to mimic a circle. @param state, the initial state of the particle |psi>_initial @param t_step, the number of steps to apply U to |psi>_initial @param nodes, the number of nodes of the circle @param angle, the angle to rotate the coin states @return Unmeasured Output state of the Quantum Walk """ position_state = basis(nodes, 0) psi = ket2dm(tensor(state, position_state)) U_hat = unitary_transform(nodes, angle) for step in prange(t_step): if step > nodes: step = np.mod(step, nodes) psi = U_hat * psi * U_hat.dag() else: psi = U_hat * psi * U_hat.dag() return psi
class OptimizedModel(Model): def __init__(self, N, index, library_size, transcript_abundance=None, theta=[], seed=None, threads=1): Model.__init__(self, N, index, library_size, transcript_abundance, theta, seed, threads) self.threads = threads # DONE @cython.boundscheck(False) def model(self, double kappa, double [:, :] beta): M = np.zeros((self.K-1, self.N.shape[1])) cdef long i, j, k cdef long K = self.K cdef long threads = self.threads cdef Py_ssize_t n = self.N.shape[1] cdef long [:, :] N = self.N cdef double [:, :] lm = self.log_model(kappa, beta) cdef double [:] c = np.zeros(n) cdef double [:, :] m = M #for i in range(n): for i in prange(n, schedule="static", nogil=True, num_threads=threads): for j in range(N[0,i]): c[i] += log(j+1) for j in range(N[1,i]): c[i] += log(j+1) for k in range(K-1): m[k,i] = exp(lm[k,i] - c[i]) return M
def plot_pdf(prob_mat): """ @brief Plots probability density function @param prob_mat the array of probability amplitudes after measurement @return Plot of Position vs Probability """ #lattice_position = np.arange(-len(prob_mat)/2+1,len(prob_mat)/2+1) lattice_position = prange(len(prob_mat)) #plt.plot(lattice_position, prob_mat) #plt.xlim([-len(prob_mat)/2+2, len(prob_mat)/2+2]) #plt.ylim([min(prob_mat), max(prob_mat)+0.01]) #plt.ylabel('Probability') #plt.xlabel('Position of particle') #plt.show() #(username = '******', key='ZWhMxlc9xwnfQp45Jh8y') """ df = pd.DataFrame({'x': lattice_position, 'y': prob_mat}) df.head() data = [go.Bar(x=df['x'], y=df['y'])] response = py.plot(data, filename='testing_123') url=response['url'] filename=response['filename'] print(url) print(filename) """ df = pd.DataFrame(data={'Probability': prob_mat}) print(df) ax = df.plot(kind='bar', colormap='jet', grid=True, title='Quantum Walk around a N-Node Circle') ax.set_xlabel('Position of Photon') ax.set_ylabel('Probability')
# Set (threads) if n_threads >= NUM_PROCESSORS: n_threads = NUM_PROCESSORS - 2 if n_threads <= 0: n_threads = 1 # Initial print statement. print('sample px and py before starting bin loop:') print(' -> ', np.random.choice(px, 10)) print(' -> ', np.random.choice(py, 10)) print('\n') print('threads : ', n_threads) print('mlim_min: ', mlim_min) print('mlim_med: ', mlim_med) print('mlim_max: ', mlim_max) with nogil, parallel(num_threads=n_threads): for i in prange(n_stars, schedule='dynamic'): # Check to see if star is in FOV of grid. if ( px[i] >= boundary_x or px[i] <= 0 or py[i] >= boundary_y or py[i] <= 0): missed += 1 # If star is in the grid. else: sat_number = satid[i] sat_age = tsat[sat_number] sat_bound = bsat[sat_number] apparent_mag = ap_mags[i] # If the star is unbound [0]. if not sat_bound:
## divide between processors np.where(B, dist_func(av, bv), None) #slice B, av and bv for each processors def my_func(i, x, B): av, bv = np.meshgrid(x[i], x[i+1:], sparse=True) return f_vect(av, bv) ## then convert to sparse. Note that the array of i needs a_i += i when combining with others, while a_j += i + 1 def f(r, x, q): for _ in r: data = f_vect(*np.meshgrid(x[_+1:], x[_], sparse=True)) # get indexes where data not 0, argwhere? q.put((data[data > 0], _, _+1+indexes))) import cython from cython.parallel import prange for i in prange(B.shape[0]-1, nogil=True): my_func(i, x, B) print(res) import multiprocessing as mp #initialise s_d, s_i, s_j as shared arrays of length x.shape[0]*x.shape[0] #set all in s_d = 0 q = mp.Queue() for i in range(nproc): p = mp.Process(target=f, args=(xrange(i,x.shape[0],nproc), x, q)) p.start()
def lcMp(process, t_obs, radius_1, radius_2, sbratio, incl, light_3 = 0, t_zero = 0, period = 1, a = None, q = 1, f_c = None, f_s = None, ldc_1 = None, ldc_2 = None, gdc_1 = None, gdc_2 = None, didt = None, domdt = None, rotfac_1 = 1, rotfac_2 = 1, bfac_1 = None, bfac_2 = None, heat_1 = None, heat_2 = None, lambda_1 = None, lambda_2 = None, vsini_1 = None, vsini_2 = None, t_exp=None, n_int=None, grid_1='default', grid_2='default', ld_1=None, ld_2=None, shape_1='sphere', shape_2='sphere', spots_1=None, spots_2=None, exact_grav=False, verbose=1): """ Calculate the light curve of a binary star This function calculates the light curve of a binary star using the ellc binary star model [1]. Parameters ---------- t_obs : array_like Times or phases of observation. The units and time system used must be consistent with t_zero and period. radius_1 : float Radius of star 1 in units of the semi-major axis of the binary. The radius is defined to be the same as a sphere with the same volume as the ellipsoid used to approximate the shape of the star. Set radius_1=1 to fix radius at limiting radius in the Roche potential. radius_2 : float Radius of star 2 in units of the semi-major axis of the binary. The radius is defined to be the same as a sphere with the same volume as the ellipsoid used to approximate the shape of the star. Set radius_2=1 to fix radius at limiting radius in the Roche potential. sbratio : float Surface brightness ratio, S_2/S_1 incl : float Inclination in degrees. light_3 : float, optional Third light contribution relative to total flux from both stars at time t_zero excluding eclipse effects. t_zero : float, optional Time (or phase) of mid-eclipse for star 1 by star 2. The units and time system must be consistent with the values in t_obs. Default is 0. period : float, optional Orbital period of the binary or (for phased data) 1. For binary systes with apsidal motion, this is the anomalistic period. If light-time effect or Doppler boosting is to be calculated correctly then the units of period must be days, otherwise arbitrary units can be used provided that these are consistent with t_zero and t_obs. Default is 1. a : {None, float}, optional Semi-major axis in solar radii for calculation of light travel time and Doppler boosting. q : float, optional Mass ratio m_2/m_1. Default is 1. f_c : {None, float}, optional For eccentric orbits with eccentricity e and longitude of periastron w, f_c = sqrt(e).cos(w) If not None, then f_s must also be specified. Default is None. f_s : {None, float}, optional For eccentric orbits with eccentricity e and longitude of periastron w, f_s = sqrt(e).sin(w) If not None, then f_c must also be specified. Default is None. ldc_1 : {None, float, 2-, 3- or 4-tuple of floats}, optional Limb darkening coefficients for star 1. Number of elements must match the selected limb-darkening law. Default is None ldc_2 : {None, float, 2-, 3- or 4-tuple of floats}, optional Limb darkening coefficients for star 2. Number of elements must match the selected limb-darkening law. Default is None gdc_1 : {None, float}, optional Gravity darkening exponent for star 1. gdc_2 : {None, float}, optional Gravity darkening exponent for star 2. didt : {None, float}, optional Rate of change of inclination [degrees/anomalistic period] Default is None. domdt : {None, float}, optional Apsidal motion rate [degrees/anomalistic period]. Default is None. rotfac_1 : float, optional Asynchronous rotation factor for star 1, F_1 Default is 1. rotfac_2 : float, optional Asynchronous rotation factor for star 2, F_2 Default is 1. bfac_1 : {None, float}, optional Doppler boosting factor, star 1 N.B. Doppler boosting is not calculated is parameter a is None Default is None. bfac_2 : {None, float}, optional Doppler boosting factor, star 2 N.B. Doppler boosting is not calculated is parameter a is None Default is None. heat_1 : {None, scalar, 3-tuple of floats}, optional If scalar, coefficient of simplified reflection model. If 3-tuple, parameters of heating+reflection model for star 1, [H_0, H_1, u_H] H_0 is the coefficient, H_1 is the exponent and u_H is the linear limb-darkening coefficient. Default is None. heat_2 : {None, scalar, 3-tuple of floats}, optional If scalar, coefficient of simplified reflection model. If 3-tuple, parameters of heating+reflection model for star 2, [H_0, H_1, u_H] H_0 is the coefficient, H_1 is the exponent and u_H is the linear limb-darkening coefficient. Default is None. lambda_1 : {None, float}, optional Sky-projected angle between orbital and rotation axes, star 1 [degrees] N.B. lambda_1 is only used if shape_1='sphere' Default is None. lambda_2 : {None, float}, optional Sky-projected angle between orbital and rotation axes, star 2 [degrees] N.B. lambda_2 is only used if shape_2='sphere' Default is None. vsini_1 : {None, float}, optional V_rot.sini for calculation of R-M effect for star 1 [km/s] See notes below. Default is None. vsini_2 : {None, float}, optional V_rot.sini for calculation of R-M effect for star 2 [km/s] See notes below. Default is None. t_exp : {None, float, array_like}, optional Exposure time in the same units as t_obs and, if array_like, with the same number of elements. Default is None. n_int : {None, int, array_like} Number of integration points used to account for finite exposure time. Set n_int or elements of n_int to 1 to make no correction for finite integration time. If array_like then set elements to 0 to use linear interpolation of the other values in the light curve to estimate the light curve at the corresponding values of t_obs. Default is None. grid_1 : {"very_sparse", "sparse", "default", "fine", "very_fine"}, optional Grid size used to calculate the flux from star 1. Default is "default" grid_2 : {"very_sparse", "sparse", "default", "fine", "very_fine"}, optional Grid size used to calculate the flux from star 2. Default is "default" ld_1 : {None, "lin", "quad", "sing", "claret", "log", "sqrt", "exp"} Limb darkening law for star 1 Default is None ld_2 : {None, "lin", "quad", "sing", "claret", "log", "sqrt", "exp"} Limb darkening law for star 2 Default is None shape_1 : {"roche", "roche_v", "sphere", "poly1p5", "poly3p0"} Model used to calculate the shape of star 1 - see Notes. Default is "sphere". shape_2 : {"roche", "roche_v", "sphere", "poly1p5", "poly3p0"} Model used to calculate the shape of star 2 - see Notes. Default is "sphere". spots_1 : (4, n_spots_1) array_like Parameters of the spots on star 1. For each spot the parameters, in order, are longitude, latitude, size and brightness factor. All three angles are in degrees. spots_2 : (4, n_spots_2) array_like Parameters of the spots on star 2. For each spot the parameters, in order, are longitude, latitude, size and brightness factor. All three angles are in degrees. exact_grav : {True|False} Use point-by-point calculation of local surface gravity for calculation of gravity darkening is True, otherwise use (much faster) approximation based on functional form fit to local gravity at 4 points on the star. Returns ------- flux : ndarray Flux in arbitrary units. Notes ----- The asynchronous rotation factors rotfac_1 and rotfac_2 are used to calculate the shapes of the stars (unless spherical stars are specified). These rotation factors are relative to the actual synchronous rotation rate (rotation period = orbital period) not pseudo-synchronous rotation rate. The effect of the spot on the light curve is calculated using the algorithm by Eker [2] for circular spots on a spherical star with quadratic limb darkening. If the limb-darkening law used for the main calculation is not linear or quadratic then the coefficients of the limb-darkening law used for the calculation of the effects of the spots are set so that the intensity distribution matches at mu = 0, 0.5 and 1. N.B. The effect of each spot on the light curve is additive so overlapping spots can result in non-physical negative fluxes for some regions of the star. For the calculation of the star shape, the rotation and orbital angular momentum vectors are assumed parallel. The shape of each star is approximated by a triaxial ellipsoid with semi-major axes (A,B,C) and offset towards companion, D, from the centre-of-mass of the star towards the companion star. For the option "roche" the definition of the Roche potential from Wilson [3] is used and the values of A, B, C, D are set to that intersection points of the triaxial ellipsoid with x-, y- and z-axes lie on an equipotential surface. For the options "poly1p5" and "poly3p0" the star is assumed to behave as a polytrope with index n=1.5 or n=3, respectively. The tidal and rotational distortion of the polytrope are assumed to be independent. The tidal distortion for polytropes is from Chandrasekhar [4] and the rotational distortion is calculated by interpolation in Table 1 of James [5]. In eccentric orbits, the volume of the star is assumed to be constant. In general, the volume is calculated from the volume of the approximating ellipsoid. In the case of synchronous rotation, the volume of the star can be calculated using equation (2.18) from Kopal "Dynamics of Close Binary Systems" (Springer, 1978) by selecting the star shape model "roche_v". Example ------- >>> import ellc >>> import numpy as np >>> import matplotlib.pyplot as plt >>> t = np.arange(-0.25,0.75, 0.001) >>> spots_1 = [[30,180],[45,-45],[25,35],[0.2,0.8]] >>> flux = ellc.lc(t,radius_1=0.1,radius_2=0.05,sbratio=0.2, ... incl=89.95,q=0.5,ld_1='quad',ldc_1=[0.65,0.2],ld_2='lin',ldc_2=0.45, ... shape_1='poly3p0',shape_2='poly1p5',spots_1=spots_1) >>> plt.plot(t,flux) >>> plt.show() References ---------- .. [1] Maxted, P.F.L. 2016. A fast, flexible light curve model for detached eclipsing binary stars and transiting exoplanets. A&A 591, A111, 2016. .. [2] Eker, 1994, ApJ, 420, 373. .. [3] Wilson, 1979, ApJ, 234, 1054. .. [4] Chandrasekhar, 1933, MNRAS, 93, 449. .. [5] James, 1964, ApJ, 140, 552. """ # Copy control parameters into an np.array gridname_to_gridsize = { "very_sparse" : 4, "sparse" : 8, "default" : 16, "fine" : 24, "very_fine" : 32, } n1 = gridname_to_gridsize.get(grid_1,None) if n1 is None: raise Exception("Invalid grid size name") n2 = gridname_to_gridsize.get(grid_2,None) if n2 is None: raise Exception("Invalid grid size name") ldstr_to_ldcode = { "none" : 0, "lin" : 1, "quad" : 2, "sing" : 3, "claret" : 4, "log" : -1, "sqrt" : -2, "exp" : -3 } if ld_1 is None: ldstr_1 = 'none' else: ldstr_1 = ld_1 if ld_2 is None: ldstr_2 = 'none' else: ldstr_2 = ld_2 l1 = ldstr_to_ldcode.get(ldstr_1,None) if l1 is None: raise Exception("Invalid limb darkening law name") l2 = ldstr_to_ldcode.get(ldstr_2,None) if l2 is None: raise Exception("Invalid limb darkening law name") shapename_to_shapecode = { "roche_v" : -2, "roche" : -1, "sphere" : 0, "poly1p5" : 1, "poly3p0" : 2, } s1 = shapename_to_shapecode.get(shape_1,None) if s1 is None: raise Exception("Invalid star shape name") s2 = shapename_to_shapecode.get(shape_2,None) if s2 is None: raise Exception("Invalid star shape name") if spots_1 is None: spar_1 = np.zeros([1,1]) n_spots_1 = 0 else: spar_1 = np.array(spots_1) if (spar_1.ndim != 2) or (spar_1.shape[0] != 4 ): raise Exception("spots_1 is not (4, n_spots_1) array_like") n_spots_1 = spar_1.shape[1] if spots_2 is None: spar_2 = np.zeros([1,1]) n_spots_2 = 0 else: spar_2 = np.array(spots_2) if (spar_2.ndim != 2) or (spar_2.shape[0] != 4 ): raise Exception("spots_2 is not (4, n_spots_2) array_like") n_spots_2 = spar_2.shape[1] ipar = np.array([n1,n2,n_spots_1,n_spots_2,l1,l2,s1,s2,1,0+exact_grav], dtype=int) # Copy binary parameters into an np.array if (radius_1 <= 0) or (radius_1 > 1): raise ValueError("radius_1 argument out of range") if (radius_1 == 1) and (shape_1 != "roche"): raise ValueError("radius_1=1 only allowed for Roche potential") if (radius_2 <= 0) or (radius_2 > 1): raise ValueError("radius_2 argument out of range") if (radius_2 == 1) and (shape_2 != "roche"): raise ValueError("radius_2=1 only allowed for Roche potential") par = np.zeros(37) par[0] = t_zero par[1] = period par[2] = sbratio par[3] = radius_1 par[4] = radius_2 par[5] = incl par[6] = light_3 if a is not None : par[7] = a if (f_c is None) and (f_s is None): pass elif (f_c is not None) and (f_s is not None): par[8] = f_c par[9] = f_s else: raise Exception("Must specify both f_c and f_s or neither.") if q <= 0 : raise ValueError("Mass ratio q must be positive.") par[10] = q ld_to_n = { "none" : 0, "lin" : 1, "quad" : 2, "sing" : 3, "claret" : 4, "log" : 2, "sqrt" : 2, "exp" : 2 } ld_n_1 = ld_to_n.get(ldstr_1,None) try: par[11:11+ld_n_1] = ldc_1 except: raise Exception("ldc_1 and ld_1 are inconsistent") ld_n_2 = ld_to_n.get(ldstr_2,None) try: par[15:15+ld_n_2] = ldc_2 except: raise Exception("ldc_2 and ld_2 are inconsistent") if gdc_1 is not None : par[19] = gdc_1 if gdc_2 is not None : par[20] = gdc_2 if didt is not None : par[21] = didt if domdt is not None : par[22] = domdt par[23] = rotfac_1 par[24] = rotfac_2 if bfac_1 is not None : par[25] = bfac_1 if bfac_2 is not None : par[26] = bfac_2 if heat_1 is not None : t = np.array(heat_1) if t.size == 1: par[27] = t elif t.size == 3: par[27:30] = t else: raise Exception('Invalid size for array heat_1') if heat_2 is not None : t = np.array(heat_2) if t.size == 1: par[30] = t elif t.size == 3: par[30:33] = t else: raise Exception('Invalid size for array heat_2') if lambda_1 is not None : par[33] = lambda_1 if lambda_2 is not None : par[34] = lambda_2 if vsini_1 is not None : par[35] = vsini_1 if vsini_2 is not None : par[36] = vsini_2 t_obs_array = np.array(t_obs) n_obs = len(t_obs_array) if t_exp is None: t_exp_array = np.zeros(n_obs) else: t_exp_array = np.ones(n_obs)*t_exp if n_int is not None : if np.amax(n_int) < 1 : raise Exception("No n_int values > 1.") if np.amin(n_int) < 0 : raise Exception("Invalid negative n_int value(s).") n_int_array = np.array(np.ones(n_obs)*n_int, dtype=int) else: n_int_array = np.ones(n_obs, dtype=int) # Create list of times for calculation, weights for integration and # indices to relate these both back to the original t_obs array i_obs = np.arange(0,n_obs) t_calc = t_obs_array[n_int_array == 1] w_calc = np.ones_like(t_calc) i_calc = i_obs[n_int_array == 1] n_int_max = np.amax(n_int_array) for i_int in np.unique(n_int_array[n_int_array > 1]) : t_obs_i = t_obs_array[n_int_array == i_int] t_exp_i = t_exp_array[n_int_array == i_int] i_obs_i = i_obs[n_int_array == i_int] for j_int in range(0,i_int): t_calc = np.append(t_calc, t_obs_i+(j_int/(i_int-1.)-0.5)*t_exp_i) i_calc = np.append(i_calc,i_obs_i) if (j_int == 0) or (j_int == i_int-1): w_calc = np.append(w_calc, 0.5*np.ones_like(t_obs_i)/(i_int-1.)) else: w_calc = np.append(w_calc, np.ones_like(t_obs_i)/(i_int-1.)) lc_rv_flags = ellc_f.ellc.lc(t_calc,par,ipar,spar_1,spar_2,verbose) flux = np.zeros(n_obs) lista = len(t_calc) with parallel(num_threads = process): for j in prange(lista, schedule='dynamic'): if np.isnan(lc_rv_flags[j,0]): print('Bad flux:',lc_rv_flags[j,:]) lc_dummy = ellc_f.ellc.lc(t_calc[j],par,ipar,spar_1,spar_2,9) return -1 flux[i_calc[j]] += lc_rv_flags[j,0]*w_calc[j] t_obs_0 = t_obs_array[n_int_array == 0 ] # Points to be interpolated n_obs_0 = len(t_obs_0) if n_obs_0 > 0 : i_sort = np.argsort(t_calc) t_int = t_calc[i_sort] f_int = lc_rv_flags[i_sort,0] flux[n_int_array == 0 ] = np.interp(t_obs_0,t_int,f_int) return flux
random_state=20) # Insert Column vector of 1s for first entry in feature vector # Allows to treat bias as trainable paramter inside weight maatrix than # a different variable X = np.c_[np.ones((X.shape[0])), X] # Initialize Weight Matrix (Should have same no. of columns as input fetures) print('[INFO] starting training...') W = np.random.uniform(size=(X.shape[1], )) # Initiliaze list to store loss value per epoch lossInfo = [] # Loop over given amount of epochs for epoch in prange(0, args['epochs']): # Dot Product between the full training data X and the weight matrix W # Take the dot product output and feed the values through the activation # function to give prediction values. preds = sigmoid_activation(X.dot(W)) # Find the Error Difference between Prediction and True Values error = preds - y # Find squared loss sum of error loss = np.sum(error**2) lossInfo.append(loss) print('[INFO] epoch #{}, loss={:.7f}'.format(epoch + 1, loss)) # Compute Gradient by X.T dotp Error scaled by no. of data points in X gradient = X.T.dot(error) / X.shape[0] # Move the weight matrix in the neg. direction of the gradient W += -args['alpha'] * gradient
# Hold indices of current words and # the cooccurrence count. cdef int word_a, word_b cdef double count, learning_rate, gradient # Loss and gradient variables. cdef double prediction, entry_weight, loss # Iteration variables cdef int i, j, shuffle_index # We iterate over random indices to simulate # shuffling the cooccurrence matrix. with nogil: for j in prange(no_cooccurrences, num_threads=no_threads, schedule='dynamic'): shuffle_index = shuffle_indices[j] word_a = row[shuffle_index] word_b = col[shuffle_index] count = counts[shuffle_index] # Get prediction prediction = 0.0 for i in range(dim): prediction = prediction + wordvec[word_a, i] * wordvec[word_b, i] prediction = prediction + wordbias[word_a] + wordbias[word_b] # Compute loss and the example weight. entry_weight = double_min(1.0, (count / max_count)) ** alpha
cdef long i, k cdef long K = self.K cdef long threads = self.threads cdef Py_ssize_t n = self.N.shape[1] cdef long [:, :] N = self.N cdef long [:] s_N = sum_of_N cdef double [:] ld = self.ld cdef double [:] delta = self.delta cdef double [:, :] lm = LM for k in range(K-1): #for i in range(n): for i in prange(n, schedule="static", nogil=True, num_threads=threads): # DEBUG c_1K = ld[i] * exp(delta[0] + beta[0,k]) # DEBUG c_2K = ld[i] * exp(delta[1] + beta[1,k]) lm[k,i] = gsl_sf_lngamma(s_N[i] + kappa) - gsl_sf_lngamma(kappa) lm[k,i] += kappa * log(kappa) lm[k,i] += N[0,i] * (log(ld[i]) + delta[0] + beta[0,k]) lm[k,i] += N[1,i] * (log(ld[i]) + delta[1] + beta[1,k]) lm[k,i] -= (s_N[i] + kappa) * (log((ld[i] * exp(delta[0] + beta[0,k])) + (ld[i] * exp(delta[1] + beta[1,k])) + kappa)) return LM # DONE @cython.boundscheck(False) def first_drvt_log_model_kappa(self, double kappa, double [:, :] beta):
with open('word_to_idx.pkl', 'wb') as f: pickle.dump(word_to_idx, f, pickle.HIGHEST_PROTOCOL) with open('idx_to_word.pkl', 'wb') as f: pickle.dump(idx_to_word, f, pickle.HIGHEST_PROTOCOL) vocab_len = len(dictionary.keys()) print("vocab_len",vocab_len) word_word_matrix = sp.sparse.lil_matrix((vocab_len,vocab_len)) similarity_function, information_based_bool = get_similarity_object(args.similarity_function) if(os.path.exists(args.similarity_function+".npy")): word_word_matrix = np.load(args.similarity_function+".npy") else: for i in prange(0,vocab_len,num_threads=4): print("i",i) w1 = idx_to_word[i] for j in range(i,vocab_len): w2 = idx_to_word[j] word_word_matrix[i][j] = get_score(w1,w2,args.pooling) word_word_matrix[j][i] = word_word_matrix[i][j] np.save(args.similarity_function+".npy",word_word_matrix)
print "Image Height %s" % height # We will perform 3 passes of the bux blur # effect to approximate Gaussian blurring for pass_num in range(num_passes): print "In iteration %s of %s" % (pass_num + 1, num_passes) # We need to loop over the workgroups here, # because unlike OpenCL, they are not # automatically set up by Python last_pass = False if pass_num == num_passes - 1: print "In Last Pass" last_pass = True # Loop over all groups and call tiltshift once per group for group_corner_x in prange(0, global_size[0], local_size[0]): for group_corner_y in range(0, global_size[1], local_size[1]): #print "GROUP CONRER %s %s" % (group_corner_x, group_corner_y) # Run tilt shift over the group and store the results in host_image_tilt_shifted tiltshift(input_image, output_image, local_memory, width, height, buf_width, buf_height, halo, sat, con, last_pass, middle_in_focus, in_focus_radius, group_corner_x, group_corner_y) # Now put the output of the last pass into the input of the next pass input_image = output_image end_time = time.time() print "Took %s seconds to run %s passes" % (end_time - start_time, num_passes)
def build_V(self): # V_me_pphh V_me_pppp V_me_hhhh # [channel][tb_i][tb_j] print("self.tb_k.state_hh : ", len(self.tb_k.state_hh)) #print("ch_list : ",ch_list) # ch = 0 # for tb_t in self.tb_k.state_hh: # if(len(tb_t)>0): # print("ch : ",ch,"\t state_hh[ch].size : ",len(tb_t)) # ch+=1 V_hhhh_list = [] V_pphh_list = [] V_pppp_list = [] for ch in self.ch_list: # hhhh #print("ch : ",ch) size_hh = len(self.tb_k.state_hh[ch]) size_pp = len(self.tb_k.state_pp[ch]) # print("size_hh : ", size_hh) # print("size_pp : ", size_pp) #self.V_hhhh[ch]=self.V_hhhh[ch].reshape(size_hh) v_hhhh_t = np.zeros((size_hh, size_hh)) nonzero_cont = 0 for tb_hh_f in prange(self.tb_k.state_hh[ch]): #[0:4]: index_f = tb_hh_f.index for tb_hh_i in self.tb_k.state_hh[ch]: #[0:4]: index_i = tb_hh_i.index val = self.Minn_me.cal_V_neu_as(tb_hh_f, tb_hh_i) # if val != 0.0 and ch == 0: # print("ch : ",ch,"\t index_f : ",index_f,"\t index_i : ",index_i,"\t val : ",val) # a_i = self.tb_k.state_hh[ch][index_f].index_1 # b_i = self.tb_k.state_hh[ch][index_f].index_2 # i_i = self.tb_k.state_hh[ch][index_i].index_1 # j_i = self.tb_k.state_hh[ch][index_i].index_2 # a_xyz = self.sps_k.state[a_i].k_xyz # b_xyz = self.sps_k.state[b_i].k_xyz # i_xyz = self.sps_k.state[i_i].k_xyz # j_xyz = self.sps_k.state[j_i].k_xyz # a_xyz[np.argwhere(a_xyz<0)]=-1 # a_xyz[np.argwhere(a_xyz>0)]=+1 # b_xyz[np.argwhere(b_xyz<0)]=-1 # b_xyz[np.argwhere(b_xyz>0)]=+1 # i_xyz[np.argwhere(i_xyz<0)]=-1 # i_xyz[np.argwhere(i_xyz>0)]=+1 # j_xyz[np.argwhere(j_xyz<0)]=-1 # j_xyz[np.argwhere(j_xyz>0)]=+1 # nonzero_cont +=1 # # print(a_xyz,b_xyz,i_xyz,j_xyz) #print(index_f,index_i) v_hhhh_t[index_f][index_i] = val V_hhhh_list.append(v_hhhh_t) #print("++++++ nonzero : ",nonzero_cont) #sys.exit() # pphh v_pphh_t = np.zeros((size_pp, size_hh)) for tb_pp_f in prange(self.tb_k.state_pp[ch]): index_f = tb_pp_f.index for tb_hh_i in self.tb_k.state_hh[ch]: index_i = tb_hh_i.index val = self.Minn_me.cal_V_neu_as(tb_pp_f, tb_hh_i) v_pphh_t[index_f][index_i] = val V_pphh_list.append(v_pphh_t) # pppp #size_ch_pp = len(self.tb_k.state_pp[ch]) v_pppp_t = np.zeros((size_pp, size_pp)) for tb_pp_f in prange(self.tb_k.state_pp[ch]): index_f = tb_pp_f.index for tb_pp_i in self.tb_k.state_pp[ch]: index_i = tb_pp_i.index val = self.Minn_me.cal_V_neu_as(tb_pp_f, tb_pp_i) v_pppp_t[index_f][index_i] = val V_pppp_list.append(v_pppp_t) # =========++++++++========== # self.V_hhhh = np.array(V_hhhh_list) self.V_pphh = np.array(V_pphh_list) self.V_pppp = np.array(V_pppp_list)
@author: Parth """ from datetime import datetime import urllib.request from bs4 import BeautifulSoup import google import time import re import whois import socket import numpy as np import pandas as pd from cython.parallel import prange itime = datetime.now() for i in prange(0, 10000): pass ftime = datetime.now() print(ftime - itime) df = pd.read_csv("100-legitimate-art.txt") sep_protocol = df['websites'].str.split("://", expand=True) get_domain = sep_protocol[1].str.split("/", 1, expand=True) get_domain.columns = ["domain", 'path'] protocol_dname_path = pd.concat([sep_protocol[0], get_domain], axis=1) protocol_dname_path.columns = ['protocol', 'domain', 'path'] #Used to classify the URL based on its length def url_length(url): if len(url) < 54:
from cython.parallel import prange from cython cimport boundscheck, wraparound cimport numpy as np import math import scipy.io as sio import time @boundscheck(False) @wraparound(False) cpdef double[:,:] para_train(double[:,:] inputs, double[:] labels, double[:,:] init_theta1, double[:,:] init_theta2, double batchsize, double learningrate, double batch_iteration): cdef int n_cores = 64 cdef double[:,:] theta1 = init_theta1 cdef double[:,:] theta2 = init_theta2 cdef double[:] index = np.random.shuffle(len(inputs)) cdef double[:,:] theta1_tmp cdef double[:,:] theta2_tmp for i in prange(n_cores, no_gil=True):### theta1_tmp = init_theta1 theta2_tmp = init_theta2 with gil: for j in range(batch_iteration):## minibatch = [inputs[x] for x in index[i * batchsize * iteration + j * batchsize: \ i * batchsize * iteration + (j + 1) * batchsize]] (theta1_tmp, theta2_tmp) = train(minibatch, labels, theta1_tmp, theta2_tmp, learningrate, 1) theta1 += theta1_tmp theta2 += theta2_tmp return (theta1/n_cores - init_theta1, theta2/n_cores - init_theta2)
import numpy as np import cython from cython.parallel import prange from libc.stdlib cimport malloc, free @cython.boundscheck(False) @cython.wraparound(False) def juliaset_cython_omp(double [:] x, double [:] y, double complex c, double lim, int maxit): cdef: int [:, ::1] julia = np.zeros((x.size, y.size), dtype = np.int32) double tmp, zr, zi, lim2 = lim*lim double cr = c.real, ci = c.imag int i, j, nx=x.size, ny=y.size int *ite for j in prange(ny, nogil=True, schedule='dynamic'): ite = <int *> malloc(sizeof(int)) for i in range(nx): zr = x[i] zi = y[j] ite[0] = 0 while (zr*zr + zi*zi) < lim2 and ite[0] < maxit: zr, zi = zr*zr - zi*zi + cr, 2*zr*zi + ci ite[0] += 1 julia[j, i] = ite[0] free(ite) return julia # %%
from cython.parallel import parallel, prange from libc.stdlib cimport abort, malloc, free cdef Py_ssize_t idx, i, n = 100 cdef int * local_buf cdef size_t size = 10 with nogil, parallel(): local_buf = <int *> malloc(sizeof(int) * size) if local_buf == NULL: abort() # populate our local buffer in a sequential loop for i in xrange(size): local_buf[i] = i * 2 # share the work using the thread-local buffer(s) for i in prange(n, schedule='guided'): func(local_buf) free(local_buf)
def lcParallel(t_obs, radius_1, radius_2, sbratio, incl, light_3=0, t_zero=0, period=1, a=None, q=1, f_c=None, f_s=None, ldc_1=None, ldc_2=None, gdc_1=None, gdc_2=None, didt=None, domdt=None, rotfac_1=1, rotfac_2=1, bfac_1=None, bfac_2=None, heat_1=None, heat_2=None, lambda_1=None, lambda_2=None, vsini_1=None, vsini_2=None, t_exp=None, n_int=None, grid_1='default', grid_2='default', ld_1=None, ld_2=None, shape_1='sphere', shape_2='sphere', spots_1=None, spots_2=None, exact_grav=False, verbose=1): # Copy control parameters into an np.array gridname_to_gridsize = { "very_sparse": 4, "sparse": 8, "default": 16, "fine": 24, "very_fine": 32, } n1 = gridname_to_gridsize.get(grid_1, None) if n1 is None: raise Exception("Invalid grid size name") n2 = gridname_to_gridsize.get(grid_2, None) if n2 is None: raise Exception("Invalid grid size name") ldstr_to_ldcode = { "none": 0, "lin": 1, "quad": 2, "sing": 3, "claret": 4, "log": -1, "sqrt": -2, "exp": -3 } if ld_1 is None: ldstr_1 = 'none' else: ldstr_1 = ld_1 if ld_2 is None: ldstr_2 = 'none' else: ldstr_2 = ld_2 l1 = ldstr_to_ldcode.get(ldstr_1, None) if l1 is None: raise Exception("Invalid limb darkening law name") l2 = ldstr_to_ldcode.get(ldstr_2, None) if l2 is None: raise Exception("Invalid limb darkening law name") shapename_to_shapecode = { "roche_v": -2, "roche": -1, "sphere": 0, "poly1p5": 1, "poly3p0": 2, } s1 = shapename_to_shapecode.get(shape_1, None) if s1 is None: raise Exception("Invalid star shape name") s2 = shapename_to_shapecode.get(shape_2, None) if s2 is None: raise Exception("Invalid star shape name") if spots_1 is None: spar_1 = np.zeros([1, 1]) n_spots_1 = 0 else: spar_1 = np.array(spots_1) if (spar_1.ndim != 2) or (spar_1.shape[0] != 4): raise Exception("spots_1 is not (4, n_spots_1) array_like") n_spots_1 = spar_1.shape[1] if spots_2 is None: spar_2 = np.zeros([1, 1]) n_spots_2 = 0 else: spar_2 = np.array(spots_2) if (spar_2.ndim != 2) or (spar_2.shape[0] != 4): raise Exception("spots_2 is not (4, n_spots_2) array_like") n_spots_2 = spar_2.shape[1] ipar = np.array( [n1, n2, n_spots_1, n_spots_2, l1, l2, s1, s2, 1, 0 + exact_grav], dtype=int) # Copy binary parameters into an np.array if (radius_1 <= 0) or (radius_1 > 1): raise ValueError("radius_1 argument out of range") if (radius_1 == 1) and (shape_1 != "roche"): raise ValueError("radius_1=1 only allowed for Roche potential") if (radius_2 <= 0) or (radius_2 > 1): raise ValueError("radius_2 argument out of range") if (radius_2 == 1) and (shape_2 != "roche"): raise ValueError("radius_2=1 only allowed for Roche potential") par = np.zeros(37) par[0] = t_zero par[1] = period par[2] = sbratio par[3] = radius_1 par[4] = radius_2 par[5] = incl par[6] = light_3 if a is not None: par[7] = a if (f_c is None) and (f_s is None): pass elif (f_c is not None) and (f_s is not None): par[8] = f_c par[9] = f_s else: raise Exception("Must specify both f_c and f_s or neither.") if q <= 0: raise ValueError("Mass ratio q must be positive.") par[10] = q ld_to_n = { "none": 0, "lin": 1, "quad": 2, "sing": 3, "claret": 4, "log": 2, "sqrt": 2, "exp": 2 } ld_n_1 = ld_to_n.get(ldstr_1, None) try: par[11:11 + ld_n_1] = ldc_1 except: raise Exception("ldc_1 and ld_1 are inconsistent") ld_n_2 = ld_to_n.get(ldstr_2, None) try: par[15:15 + ld_n_2] = ldc_2 except: raise Exception("ldc_2 and ld_2 are inconsistent") if gdc_1 is not None: par[19] = gdc_1 if gdc_2 is not None: par[20] = gdc_2 if didt is not None: par[21] = didt if domdt is not None: par[22] = domdt par[23] = rotfac_1 par[24] = rotfac_2 if bfac_1 is not None: par[25] = bfac_1 if bfac_2 is not None: par[26] = bfac_2 if heat_1 is not None: t = np.array(heat_1) if t.size == 1: par[27] = t elif t.size == 3: par[27:30] = t else: raise Exception('Invalid size for array heat_1') if heat_2 is not None: t = np.array(heat_2) if t.size == 1: par[30] = t elif t.size == 3: par[30:33] = t else: raise Exception('Invalid size for array heat_2') if lambda_1 is not None: par[33] = lambda_1 if lambda_2 is not None: par[34] = lambda_2 if vsini_1 is not None: par[35] = vsini_1 if vsini_2 is not None: par[36] = vsini_2 t_obs_array = np.array(t_obs) n_obs = len(t_obs_array) if t_exp is None: t_exp_array = np.zeros(n_obs) else: t_exp_array = np.ones(n_obs) * t_exp if n_int is not None: if np.amax(n_int) < 1: raise Exception("No n_int values > 1.") if np.amin(n_int) < 0: raise Exception("Invalid negative n_int value(s).") n_int_array = np.array(np.ones(n_obs) * n_int, dtype=int) else: n_int_array = np.ones(n_obs, dtype=int) # Create list of times for calculation, weights for integration and # indices to relate these both back to the original t_obs array i_obs = np.arange(0, n_obs) t_calc = t_obs_array[n_int_array == 1] w_calc = np.ones_like(t_calc) i_calc = i_obs[n_int_array == 1] n_int_max = np.amax(n_int_array) print(np.unique(n_int_array[n_int_array > 1])) with cython.nogil, parallel(num_threads=2): for i_int in prange(np.unique(n_int_array[n_int_array > 1]), schedule='dynamic'): t_obs_i = t_obs_array[n_int_array == i_int] t_exp_i = t_exp_array[n_int_array == i_int] i_obs_i = i_obs[n_int_array == i_int] for j_int in range(0, i_int): t_calc = np.append( t_calc, t_obs_i + (j_int / (i_int - 1.) - 0.5) * t_exp_i) i_calc = np.append(i_calc, i_obs_i) if (j_int == 0) or (j_int == i_int - 1): w_calc = np.append( w_calc, 0.5 * np.ones_like(t_obs_i) / (i_int - 1.)) else: w_calc = np.append(w_calc, np.ones_like(t_obs_i) / (i_int - 1.)) lc_rv_flags = ellc_f.ellc.lc(t_calc, par, ipar, spar_1, spar_2, verbose) flux = np.zeros(n_obs) for j in range(0, len(t_calc)): if np.isnan(lc_rv_flags[j, 0]): print('Bad flux:', lc_rv_flags[j, :]) lc_dummy = ellc_f.ellc.lc(t_calc[j], par, ipar, spar_1, spar_2, 9) return -1 flux[i_calc[j]] += lc_rv_flags[j, 0] * w_calc[j] t_obs_0 = t_obs_array[n_int_array == 0] # Points to be interpolated n_obs_0 = len(t_obs_0) if n_obs_0 > 0: i_sort = np.argsort(t_calc) t_int = t_calc[i_sort] f_int = lc_rv_flags[i_sort, 0] flux[n_int_array == 0] = np.interp(t_obs_0, t_int, f_int) return flux