def cards_road(self) : db.distribute() global player_cards, cpu_cards, remainder_cards player_cards, cpu_cards, remainder_cards = [], [], [] for no in db.player_num : player_cards.append(pygame.image.load("%s%d.png" % (root, no)).convert_alpha()) for no in db.cpu_num : cpu_cards.append(pygame.image.load("%s%d.png" % (root, no)).convert_alpha()) for no in db.remainder_num : remainder_cards.append(pygame.image.load("%s%d.png" % (root, no)).convert_alpha())
def main(): runs = {} with open( os.path.join(testdir, "tests-mcmc.txt"), "rb" ) as names: # open(os.path.join(testdir,'topdirs.p'),'rb') as names: # names = cpkl.load(names) for name in names: meta = setup(name[:-1]) runs[name[:-4]] = meta # make initial plots distribute.run_offthread_sync(plots.initial_plots(runs)) # runs = {'runs':tests} global init_runs init_runs = runs nps = mp.cpu_count() # -1 # fork off all of the plotter threads, for run_name in runs.keys(): print ("starting run of " + str(run_name)) dist = distribute.distribute(plots.all_plotters, start=True, meta=runs[run_name]) print ("plotter threads for: " + run_name + " started") # inject the distribution handler into the metadata object runs[run_name].dist = dist print ("setting dist on {} to {}".format(run_name, runs[run_name].dist)) pool = mp.Pool(nps) # may add back plot-only functionality later keys = runs.keys() # [run.key.add(t=name) for name in lrange(names)] print ("generating {} keys".format(len(keys))) pool.map(fsamp, keys) for run in runs.keys(): runs[run].dist.finish() print ("ending run of: " + str(run))
def compute_phase_trace_gpu(self, X): X = X.flatten() V_all = type(self).model.cuda_integrate_three_rk4(X, self.network.coupling_strength, self.dt/float(self.stride), self.N_output, self.stride) V_all = np.swapaxes(V_all, 1, 2) # V_all[initial condition, voltage trace, time index] return distribute.distribute(self.phase_trace_gpu, 'i', range(V_all.shape[0]), kwargs={'V': V_all})
def computePhaseTrace_gpu(self, X): X = X.flatten() V_all = model.cuda_integrate_four_rk4(X, self.network.get_coupling(), self.dt/float(self.stride), self.N_output, self.stride) V_all = np.swapaxes(V_all, 1, 2) # V_all[initial condition, voltage trace, time index] return distribute.distribute(self(type).phaseTrace_gpu, 'i', range(V_all.shape[0]), kwargs={'V': V_all})
def compute_phase_trace_gpu(self, X): X = X.flatten() V_all = model.cuda_integrate_four_rk4(X, self.network.get_coupling(), self.dt/float(self.stride), self.N_output, self.stride) V_all = np.swapaxes(V_all, 1, 2) # V_all[initial condition, voltage trace, time index] return fm.distribute(phase_trace_gpu, 'i', range(V_all.shape[0]), kwargs={'V': V_all})
def compute_prc(self, kick=0.01, N_integrate=10**4, remove=True): if self.PRC_COMPUTED: return # if the PRC has already been computed, do nothing self.find_orbit(remove=remove) if True: # !!!the alternative is not yet stable!!! #if not orb.AUTO_ENABLED: # !!!the alternative is not yet stable!!! ### COMPUTE PRC WITH SHIFT AND INTEGRATION ### phase, shiftedStates = self.shifted_orbit(kick=kick) # copies of shifted orbits separate for each dimension shiftedStates = np.concatenate(shiftedStates) # make one long set of initial values dt = self.period/float(N_integrate) # integrate the shifted trajectories! if self.model.CUDA_ENABLED: iterStates = self.model.cuda_integrate_one_rk4_nosave(shiftedStates, dt, N_integrate) else: iterStates = np.array([self.model.integrate_one_rk4_nosave(shiftedStates[i], dt, N_integrate) for i in xrange(shiftedStates.shape[0])]) iterStates = [iterStates[i*self.N_PRC:(i+1)*self.N_PRC] for i in xrange(self.dimensions)] # divide into the dimensions again phase = np.concatenate((phase, [2.*np.pi])) # full circle for dim in xrange(self.dimensions): # compute phases from iterated kicked states iSdim = iterStates[dim] def findClosest(i): # function to parallelize minimization return self.closestPhase(iSdim[i], phase[i]) kickedPhase = np.asarray(distribute.distribute(findClosest, 'i', xrange(iSdim.shape[0]))) PRC_i = (kickedPhase-phase[:-1])/kick PRC_i = np.concatenate((PRC_i, [PRC_i[0]])) self.prcurve[dim].makeModel(PRC_i, phase) ### COMPUTE PRC WITH THE ADJOINT METHOD ### else: # if AUTO_ENABLED phase = tl.PI2*np.arange(self.N_PRC)/float(self.N_PRC) X = self.evaluate_orbit(phase) #t = self.period/tl.PI2 * phase t = 1./tl.PI2 * phase self.write_solution(t, X, mode=1) # mode=1: adjoint self.model.createAutoCode2(self.autoname+'.c', period=self.period) # create auto code to solve the adjoint equation as well. solution = orb.auto.run(self.autoname)(2) # the second label if remove: self.auto_clean() tX = np.array(solution.toArray()) phase = tl.PI2*tX[:, 0] PRC = tX[:, 1+self.dimensions:] # save new solution for dim in xrange(self.dimensions): # compute phases from iterated kicked states self.prcurve[dim].makeModel(PRC[:, dim], phase) self.PRC_COMPUTED = True
def findRoots(self, GRID=10): phase = tl.PI2*np.arange(GRID)/float(GRID) dphi = phase[1] phase += dphi/2. # shift into the open field (0, 2pi)x(0, 2pi) ### find roots from a grid ### def findRoot(n): i, j = n/GRID, np.mod(n, GRID) fp = [[0., 0.], [0., 0.]] try: sol = opt.root(self.__call__, x0=[phase[i], phase[j]], tol=10.**-11, method='broyden2') if sol.success: jac = np.array([scipy.optimize.approx_fprime(sol.x, lambda x: self.f[i](x[0], x[1]), epsilon=0.25*dphi) for i in xrange(self.dimensions)]) eigenvalues = scipy.linalg.eigvals(jac) if not any([eigen == 0. for eigen in eigenvalues]): fp = [sol.x, eigenvalues] except: pass return fp fps = distribute.distribute(findRoot, 'n', np.arange(GRID**2)) fixedPoints = [fixedPoint_torus(x=fp[0], eigenvalues=fp[1]) for fp in fps] ### discard doubles ### self.fixedPoints = [fixedPoints[0]] for newfp in fixedPoints: conditions = [oldfp.distance2(newfp) > 0.1*dphi for oldfp in self.fixedPoints] if all(conditions): self.fixedPoints.append(newfp)
def main(): meta = setup() p_runs = {key(p=p):perparam(meta, p) for p in lrange(meta.params)} s_runs = {p.add(s=s):persurv(meta, p_runs[p], s) for s in lrange(meta.survs) for p in p_runs.keys()} n_runs = {s.add(n=n):persamp(meta, s_runs[s], n) for n in xrange(meta.samps) for s in s_runs.keys()} i_runs = {n.add(i=i):perinit(meta, n_runs[n], i) for i in lrange(meta.inits) for n in n_runs.keys()} runs = {'p_runs': p_runs, 's_runs': s_runs, 'n_runs': n_runs, 'i_runs': i_runs} global init_runs init_runs = runs # make initial plots distribute.run_offthread_sync(iplots.initial_plots, meta, runs) nps = mp.cpu_count()#-1 for s_run in s_runs.values(): print ('starting run of: ' + str(s_run.key)) # fork off all of the plotter threads, dist = distribute.distribute(plots.all_plotters, start = True, meta = meta, p_run = s_run.p_run, s_run = s_run, n_runs = s_run.n_runs, i_runs = [i_run for n_run in s_run.n_runs for i_run in n_run.i_runs]) # inject the distribution handler into the metadata object print ('plotter threads started') s_run.dist = dist print ('setting dist on {} to {}'.format(s_run, s_run.dist)) pool = mp.Pool(nps) # may add back plot-only functionality later keys = [s_run.key.add(n=n, i=i) for i in lrange(meta.inits) for n in xrange(meta.samps)] print ('generating {} keys'.format(len(keys))) pool.map(fsamp, keys) dist.finish() print('ending run of: ' + str(s_run.key))
from datetime import datetime from dateutil.relativedelta import relativedelta import crawling import distribute import database url = 'http://gall.dcinside.com/board/lists?id=f_drama&page=' # date = datetime(2019,2,14) date = datetime.today() - relativedelta(days=1) dramaList = crawling.crawling(date, url) # for drama in dramaList: # print(drama) sortList = distribute.distribute(dramaList) dramaDailyList = [] year, month, day = date.year, date.month, date.day for sort in sortList.items(): dramaDailyList.append({ 'name': sort[0], 'count': len(sort[1]), 'year': year, 'month': month, 'day': day }) # database.insertDramaDaily(dramaDailyList) print(date)
def computePhaseTrace_cpu(self, X): return distribute.distribute(self.phaseTrace_cpu, 'i', range(X.shape[0]), kwargs={'X': X})
pickle_in = open("../../Q1_data/data.pkl", "rb") rawDict = pickle.load(pickle_in) xDict , yDict = np.hsplit(rawDict, 2) xTrain, xTest, yTrain, yTest = train_test_split(xDict, yDict, test_size=0.1, random_state=57) temp = np.concatenate((xTrain, yTrain), axis =1) np.random.shuffle(temp) xTrain, yTrain = np.hsplit(temp, 2) dx , dy = distribute(xTrain, yTrain) bias, variance = train(dx, dy, xTest, yTest) table = np.vstack(([i for i in range(1, 100)], bias, variance)).T print(tabulate(table, headers=["Degree", "Bias", "Variance"], tablefmt='psql')) plt.plot(bias, color = 'red',label='bias') plt.plot(variance, color = 'blue', label='variance') plt.legend() plt.show() # now that we have the bias and the variance ,we can plot this data or whatever
from imageIO import * import os from distribute import distribute from combine_A_and_B import combine # Resize the image to smaller size for root, dirs, files in os.walk("SourceImage"): for file in files: try: if not os.path.exists(os.path.join("ResizedImage", file)): imsave(os.path.join("ResizedImage", file), imresize(os.path.join(root, file))) print("Resize Image: " + file) except: print("Not Vilid Image: " + file) s = input( "Now please run the matlab script: smoothing.m.\nPress y after finishing, n for exit: (y/n)\n" ) if not (s == "y" or s == "Y"): exit() s = input( "Now please run the matlab script: sketching.m.\nPress y after finishing, n for exit: (y/n)\n" ) if not (s == "y" or s == "Y"): exit() distribute() combine()
def sweep_phase_space(self, jitter=False, widget=None, event=None): self.dt, self.stride, self.N_output = self.system.dt, self.system.stride, self.system.N_output(self.traces.CYCLES) self.echo('computing using '+torus_2D.PROCESSOR[self.USE_GPU]+' ...') torus_2D.erase_traces(self) self.erase_basins() initial_phases = np.arange(0., 1., 1./float(self.GRID)) try: X = self.system.load_initial_conditions(initial_phases, jitter=jitter) # X[initial_condition, variable] except: X = self.system.load_initial_conditions(initial_phases) # ... not all systems know about jittering. t0 = time.time() D = compute_phase_trace[self.USE_GPU](self, X) self.echo('traces computed.') # find attractors attractors = [att.attractor( [[0., 0.]], initial_state=[0., 0.] ).classify()] basin_hash = -np.ones((self.GRID, self.GRID), int) for i in xrange(self.GRID): for j in xrange(self.GRID): trajectory = D[i*self.GRID+j] initial_state = initial_phases[[j, i]] try: new_attractor = att.attractor( trajectory, initial_state=initial_state ).classify() except: basin_hash[i, j] = 0 continue conditions = [attr_i.distance2attractor(new_attractor) > 0.1 for attr_i in attractors] for (c, condition) in enumerate(conditions): if not condition: basin_hash[i, j] = c if basin_hash[i, j] < 0: attractors.append(new_attractor) basin_hash[i, j] = len(attractors)-1 # refine attractor def refine_attractor(initial_condition): t, V_j = self.traces.computeTraces(initial_condition, plotit=False) V_j = np.transpose(V_j) ti, trajectory = tl.phase_difference(V_j, V_trigger=type(self).V_trigger) try: new_attr = att.attractor(trajectory).classify() return new_attr except: return att.attractor( [[0., 0.]], initial_state=[0., 0.] ).classify() initial_conditions = [] for i in xrange(1, len(attractors), 1): attr = attractors[i] initial_conditions.append( self.system.load_initial_condition(attr.initial_state[1], attr.initial_state[0])) if len(initial_conditions): self.traces.CYCLES *= 4 new_attractors = [att.attractor( [[0., 0.]], initial_state=[0., 0.] ).classify()] new_attractors.extend( distribute.distribute(refine_attractor, 'initial_condition', initial_conditions) ) self.traces.CYCLES /= 4 attractors = [att.attractor( [[0., 0.]], initial_state=[0., 0.] ).classify()] for (j, new_att_j) in enumerate(new_attractors): conditions = [new_att_j.distance2attractor(attr_i) < 0.1 for attr_i in attractors] if any(conditions): which = conditions.index(True) else: which = len(attractors) attractors.append(new_att_j) for l in xrange(basin_hash.shape[0]): for m in xrange(basin_hash.shape[1]): if basin_hash[l, m] == j: basin_hash[l, m] = which basins = np.ones((self.GRID, self.GRID, 4), float) for i in xrange(self.GRID): for j in xrange(self.GRID): basins[j, i, :3] = attractors[basin_hash[i, j]].color[:3] # plot attractors for attractor in attractors: attractor.plot(self.ax_basins, 'o', mec='w', mew=1.0, ms=7.) # plot basins if self.basin_image == None: self.basin_image = self.ax_basins.imshow(basins, interpolation='nearest', aspect='equal', extent=(0., 1., 0., 1.), origin='lower') else: self.basin_image.set_data(basins) self.ax_basins.set_xlim(0., 1.) self.ax_basins.set_ylim(0., 1.) # plot traces N_ARROW = self.GRID/4 # Plot <X> arrow symbols in each direction. for i in xrange(self.GRID): for j in xrange(self.GRID): d = D[i*self.GRID+j] color = basins[j, i, :3] plotArrow = self.PLOT_ARROWS and (i % N_ARROW == 0) and (j % N_ARROW == 0) # every n-th grid point for the top plot try: tl.plot_phase_2D(d[:, 0], d[:, 1], axes=self.ax_traces, color=color, PI=0.5, arrows=plotArrow) except: pass for attractor in attractors: attractor.plot(self.ax_traces, 'o', mec='w', mew=1.0, ms=7.) self.fig.canvas.draw() self.echo("used %.2g sec" % (time.time()-t0)) return basin_hash, attractors
def compute_prc(self, kick=0.01, N_integrate=10**4, remove=True): if self.PRC_COMPUTED: return # if the PRC has already been computed, do nothing self.find_orbit(remove=remove) if True: # !!!the alternative is not yet stable!!! #if not orb.AUTO_ENABLED: # !!!the alternative is not yet stable!!! ### COMPUTE PRC WITH SHIFT AND INTEGRATION ### phase, shiftedStates = self.shifted_orbit( kick=kick ) # copies of shifted orbits separate for each dimension shiftedStates = np.concatenate( shiftedStates) # make one long set of initial values dt = self.period / float(N_integrate) # integrate the shifted trajectories! if self.model.CUDA_ENABLED: iterStates = self.model.cuda_integrate_one_rk4_nosave( shiftedStates, dt, N_integrate) else: iterStates = np.array([ self.model.integrate_one_rk4_nosave( shiftedStates[i], dt, N_integrate) for i in xrange(shiftedStates.shape[0]) ]) iterStates = [ iterStates[i * self.N_PRC:(i + 1) * self.N_PRC] for i in xrange(self.dimensions) ] # divide into the dimensions again phase = np.concatenate((phase, [2. * np.pi])) # full circle for dim in xrange(self.dimensions ): # compute phases from iterated kicked states iSdim = iterStates[dim] def findClosest(i): # function to parallelize minimization return self.closestPhase(iSdim[i], phase[i]) kickedPhase = np.asarray( distribute.distribute(findClosest, 'i', xrange(iSdim.shape[0]))) PRC_i = (kickedPhase - phase[:-1]) / kick PRC_i = np.concatenate((PRC_i, [PRC_i[0]])) self.prcurve[dim].makeModel(PRC_i, phase) ### COMPUTE PRC WITH THE ADJOINT METHOD ### else: # if AUTO_ENABLED phase = tl.PI2 * np.arange(self.N_PRC) / float(self.N_PRC) X = self.evaluate_orbit(phase) #t = self.period/tl.PI2 * phase t = 1. / tl.PI2 * phase self.write_solution(t, X, mode=1) # mode=1: adjoint self.model.createAutoCode2( self.autoname + '.c', period=self.period ) # create auto code to solve the adjoint equation as well. solution = orb.auto.run(self.autoname)(2) # the second label if remove: self.auto_clean() tX = np.array(solution.toArray()) phase = tl.PI2 * tX[:, 0] PRC = tX[:, 1 + self.dimensions:] # save new solution for dim in xrange(self.dimensions ): # compute phases from iterated kicked states self.prcurve[dim].makeModel(PRC[:, dim], phase) self.PRC_COMPUTED = True
def compute_phase_trace_cpu(self, X): return distribute.distribute(self.phase_trace_cpu, 'i', range(X.shape[0]), kwargs={'X': X})
def sweep_phase_space(self, widget=None, event=None): self.dt, self.stride, self.N_output = self.system.dt, self.system.stride, self.system.N_output( self.traces.CYCLES) self.echo('computing using ' + torus_2D.PROCESSOR[self.USE_GPU] + ' ...') torus_2D.erase_traces(self) self.erase_basins() initial_phases = np.arange(0., 1., 1. / float(self.GRID)) X = self.system.load_initial_conditions( initial_phases) # X[initial_condition, variable] t0 = time.time() D = compute_phase_trace[self.USE_GPU](self, X) self.echo('traces computed.') # find attractors attractors = [ att.attractor([[0., 0.]], initial_state=[0., 0.]).classify() ] basin_hash = -np.ones((self.GRID, self.GRID), int) for i in xrange(self.GRID): for j in xrange(self.GRID): trajectory = D[i * self.GRID + j] initial_state = initial_phases[[j, i]] try: new_attractor = att.attractor( trajectory, initial_state=initial_state).classify() except: basin_hash[i, j] = 0 continue conditions = [ attr_i.distance2attractor(new_attractor) > 0.1 for attr_i in attractors ] for (c, condition) in enumerate(conditions): if not condition: basin_hash[i, j] = c if basin_hash[i, j] < 0: attractors.append(new_attractor) basin_hash[i, j] = len(attractors) - 1 # refine attractor #""" def refine_attractor(initial_condition): t, V_j = self.traces.compute_traces(initial_condition, plotit=False) V_j = np.transpose(V_j) ti, trajectory = tl.phase_difference( V_j, V_trigger=type(self).V_trigger) try: new_attr = att.attractor(trajectory).classify() return new_attr except: return att.attractor([[0., 0.]], initial_state=[0., 0.]).classify() initial_conditions = [] for i in xrange(1, len(attractors), 1): attr = attractors[i] initial_conditions.append( self.system.load_initial_condition(attr.initial_state[1], attr.initial_state[0])) if len(initial_conditions): self.traces.CYCLES *= 4 new_attractors = [ att.attractor([[0., 0.]], initial_state=[0., 0.]).classify() ] new_attractors.extend( fm.distribute(refine_attractor, 'initial_condition', initial_conditions)) self.traces.CYCLES /= 4 #""" attractors = [ att.attractor([[0., 0.]], initial_state=[0., 0.]).classify() ] for (j, new_att_j) in enumerate(new_attractors): conditions = [ new_att_j.distance2attractor(attr_i) < 0.1 for attr_i in attractors ] if any(conditions): which = conditions.index(True) else: which = len(attractors) attractors.append(new_att_j) for l in xrange(basin_hash.shape[0]): for m in xrange(basin_hash.shape[1]): if basin_hash[l, m] == j: basin_hash[l, m] = which basins = np.ones((self.GRID, self.GRID, 4), float) for i in xrange(self.GRID): for j in xrange(self.GRID): basins[j, i, :3] = attractors[basin_hash[i, j]].color[:3] # plot attractors for attractor in attractors: attractor.plot(self.ax_basins, 'o', mec='w', mew=1.0, ms=7.) # plot basins if self.basin_image == None: self.basin_image = self.ax_basins.imshow(basins, interpolation='nearest', aspect='equal', extent=(0., 1., 0., 1.), origin='lower') else: self.basin_image.set_data(basins) self.ax_basins.set_xlim(0., 1.) self.ax_basins.set_ylim(0., 1.) # plot traces for i in xrange(self.GRID): for j in xrange(self.GRID): d = D[i * self.GRID + j] color = basins[j, i, :3] #""" try: tl.plot_phase_2D(d[:, 0], d[:, 1], axes=self.ax_traces, c=color, PI=0.5) #last = d[-1, :] #tl.plot_phase_2D(d[:, 0], d[:, 1], axes=self.ax_traces, c=tl.clmap(tl.PI2*last[1], tl.PI2*last[0]), PI=0.5) #tl.plot_phase_2D(d[:, 0], d[:, 1], axes=self.ax_traces, c=tl.clmap_patterns(last[1], last[0]), PI=0.5) except: pass #""" for attractor in attractors: attractor.plot(self.ax_traces, 'o', mec='w', mew=1.0, ms=7.) self.fig.canvas.draw() self.echo("used %.2g sec" % (time.time() - t0)) return basin_hash, attractors
def compute_phase_trace_cpu(self, X): return fm.distribute(self.phase_trace_cpu, 'i', range(X.shape[0]), kwargs={'X': X})