def learn(self, samples, epochs=25000, noise=0, test_samples=None, show_progress=True): ''' Learn given distribution using n data List of sample sets Number of epochs to be ran for each sample set ''' # Check if samples is a list if type(samples) not in [tuple,list]: samples = (samples,) epochs = (epochs,) n = 0 # total number of epochs to be ran for j in range(len(samples)): n += epochs[j] self.entropy = [] self.distortion = [] if show_progress: bar = ProgressBar(widgets=[Percentage(), Bar()], maxval=n).start() index = 0 for j in range(len(samples)): self.samples = samples[j] I = np.random.randint(0,self.samples.shape[0],n) for i in range(epochs[j]): # Set sigma and learning rate via time t = index/float(n) lrate = self.lrate_i*(self.lrate_f/self.lrate_i)**t sigma = self.sigma_i*(self.sigma_f/self.sigma_i)**t C = self.adj.copy() # Learn something S = self.samples[I[i]] + noise*(2*np.random.random(len(self.samples[I[i]]))-1) S = np.minimum(np.maximum(S,0),1) self.learn_data(S,lrate,sigma) #self.learn_data(self.samples[I[i]],lrate,sigma) if i%100 == 0: self.entropy.append(((self.adj-C)**2).sum()) if test_samples is not None: distortion = self.compute_distortion(test_samples) else: distortion = self.compute_distortion(self.samples) self.distortion.append(distortion) if show_progress: bar.update(index+1) index = index+1 if show_progress: bar.finish()
def learn(self, samples, epochs=25000, noise=0, test_samples=None, show_progress=True): ''' Learn given distribution using n data :Parameters: `samples` : [numpy array, ...] List of sample sets `epochs` : [int, ...] Number of epochs to be ran for each sample set ''' # Check if samples is a list if type(samples) not in [tuple,list]: samples = (samples,) epochs = (epochs,) n = 0 # total number of epochs to be ran for j in range(len(samples)): n += epochs[j] self.entropy = [] self.distortion = [] if show_progress: bar = ProgressBar(widgets=[Percentage(), Bar()], maxval=n).start() index = 0 for j in range(len(samples)): self.samples = samples[j] I = np.random.randint(0,self.samples.shape[0],n) for i in range(epochs[j]): # Set sigma and learning rate according to current time t = index/float(n) lrate = self.lrate_i*(self.lrate_f/self.lrate_i)**t sigma = self.sigma_i*(self.sigma_f/self.sigma_i)**t C = self.codebook.copy() # Learn data S = self.samples[I[i]] + noise*(2*np.random.random(len(self.samples[I[i]]))-1) S = np.minimum(np.maximum(S,0),1) self.learn_data(S,lrate,sigma) #self.learn_data(self.samples[I[i]],lrate,sigma) if i%100 == 0: self.entropy.append(((self.codebook-C)**2).sum()) if test_samples is not None: distortion = self.compute_distortion(test_samples) else: distortion = self.compute_distortion(self.samples) self.distortion.append(distortion) if show_progress: bar.update(index+1) index = index+1 if show_progress: bar.finish()
file.write('''set size ratio 1\n''') file.write( '''set title "Dynamic Self-Organising Map\\nNicolas Rougier & Yann Boniface"\n''' ) file.write( '''set label "Self-organisation onto a cube surface" at screen .5, screen .1 center\n''' ) file.write( '''set label "(http://www.loria.fr/~rougier/)" at screen .5, screen .065 center textcolor lt 3\n''' ) file.write('''set view %d,%d\n''' % (rot_x, rot_z)) file.write('''set terminal pngcairo size 512,512\n''') file.write('''set output '%s.png'\n''' % filename) # file.write('''splot '%s' using 1:2:3, '%s' with point pt 6 lw .1\n''' % (datafile,datafile)) file.write('''splot '%s' using 1:2:3\n''' % (datafile)) file.close() file = open(datafile, 'w') C = net.codebook for x in range(C.shape[0]): for y in range(C.shape[1]): file.write('%.3f %.3f %.3f\n' % (C[x, y, 0], C[x, y, 1], C[x, y, 2])) file.write('''\n''') file.close() subprocess.call(['/usr/bin/gnuplot', plotfile]) net.learn_data(samples[I[i]]) bar.update(i) bar.finish() # os.system('''mencoder 'mf:///tmp/image*.png' -mf type=png:fps=25 -Ovc lavc -lavcopts \ # vcodec=mpeg4:vbitrate=2500 -oac copy -o cube.avi''')
from progress import ProgressBar from time import sleep from random import random my_list = range(20) print 'TEST: Using a default ProgressBar' sleep(1.0) pb = ProgressBar(len(my_list), 20) pb.start() for i in my_list: sleep(0.3) pb.tick() pb.finish() print 'TEST: Using a custom design' sleep(1.0) pb = ProgressBar(len(my_list), 20, bookends='<{}>', bar_char='/', empty_char='-') pb.start() for i in my_list: sleep(0.3) pb.tick() pb.finish() print 'TEST: Using a minimal design' sleep(1.0) pb = ProgressBar(len(my_list), 20, bookends='', bar_char='-', empty_char=' ', show_percent=True, show_iter=False, show_time=False) pb.start()
#!/usr/bin/env python2 # -*- coding: utf-8 -*- from progress import ProgressBar from animation import * from time import sleep p = ProgressBar(" Doing the 1st thing:", max_value=20, width=20) for i in range(21): p.update(i) sleep(.25) p.finish("Success") a = Animation("Waiting for the 2nd thing:") for i in range(21): a.next() sleep(.25) a.finish("Success") a = Animation("Waiting for the 3rd thing:", animation=ANIMATE_VGROW) for i in range(21): a.next() sleep(.25) a.finish("Success") a = Animation("Waiting for the 4th thing:", animation=ANIMATE_HGROW) for i in range(21): a.next() sleep(.25) a.finish("Success") a = Animation("Waiting for the 5th thing:", animation=ANIMATE_COUNT)
file.write('''set xrange [-.5:1.5]\n''') file.write('''set yrange [-.5:1.5]\n''') file.write('''set zrange [-.5:1.5]\n''') file.write('''set style data line\n''') file.write('''set ticslevel 0\n''') file.write('''set size ratio 1\n''') file.write('''set title "Dynamic Self-Organising Map\\nNicolas Rougier & Yann Boniface"\n''') file.write('''set label "Self-reorganisation from sphere to spheres surface" at screen .5, screen .1 center\n''') file.write('''set label "(http://www.loria.fr/~rougier/)" at screen .5, screen .065 center textcolor lt 3\n''') file.write('''set view %d,%d\n''' % (rot_x,rot_z)) file.write('''set terminal pngcairo size 512,512\n''') file.write('''set output '%s.png'\n''' % filename) # file.write('''splot '%s' using 1:2:3, '%s' with point pt 6 lw .1\n''' % (datafile,datafile)) file.write('''splot '%s' using 1:2:3\n''' % (datafile)) file.close() file = open(datafile, 'w') C = net.codebook for x in range(C.shape[0]): for y in range(C.shape[1]): file.write('%.3f %.3f %.3f\n' % (C[x,y,0],C[x,y,1],C[x,y,2])) file.write('''\n''') file.close() subprocess.call(['/usr/bin/gnuplot', plotfile]) net.learn_data(samples[I[i]]) bar.update(i) bar.finish() # os.system('''mencoder 'mf:///tmp/sphere*.png' -mf type=png:fps=25 -Ovc lavc -lavcopts \ # vcodec=mpeg4:vbitrate=2500 -oac copy -o sphere.avi''')
from progress import ProgressBar from time import sleep from random import random my_list = range(20) print 'TEST: Using a default ProgressBar' sleep(1.0) pb = ProgressBar(len(my_list), 20) pb.start() for i in my_list: sleep(0.3) pb.tick() pb.finish() print 'TEST: Using a custom design' sleep(1.0) pb = ProgressBar(len(my_list), 20, bookends='<{}>', bar_char='/', empty_char='-') pb.start() for i in my_list: sleep(0.3) pb.tick() pb.finish() print 'TEST: Using a minimal design' sleep(1.0) pb = ProgressBar(len(my_list),
def parse_crux_search_txt(filename): """Iterate over records in a search.{target,decoy}.txt. Crux txt format files are tab-delimited with 30 fields*, described in the online documentation [1]. This function returns an iterator which yields a dictionary with the fields and their values. * 'decoy q-value (p-value)' is not output by Crux, at least as of v1.33. [1] http://noble.gs.washington.edu/proj/crux/txt-format.html Arguments: filename: Name of the crux search-for-matches output. Returns: Dictionary that maps field names to values. Only fields that are non-empty in the input exist in the returned dictionary. Many of the fields are not usually set in the output of crux search-for-matches, and will not be available. """ fields = ['scan', # int 'charge', # int 'spectrum precursor m/z', # float 'spectrum neutral mass', # float 'peptide mass', # float 'delta_cn', # float 'sp score', # float 'sp rank', # float 'xcorr score', # float 'xcorr rank', # int 'p-value', # float 'Weibull est. q-value', # float 'decoy q-value (xcorr)', # float 'percolator score', # float 'percolator rank', # int 'percolator q-value', # float 'q-ranker score', # float 'q-ranker q-value', # float 'b/y ions matched', # int 'b/y ions total', # int 'matches/spectrum', # int 'sequence', # string 'cleavage type', # string 'protein id', # string 'flanking aa', # string 'unshuffled sequence', # string 'eta', # float 'beta', # float 'shift', # float 'corr'] # float casts = [ int, int, float, float, float, float, float, float, float, int, float, float, float, float, int, float, float, float, int, int, int, str, str, str, str, str, float, float, float, float ] assert(len(fields) == len(casts)) _mandatories = [ 'scan', 'charge', 'spectrum precursor m/z', 'spectrum neutral mass', 'xcorr score', 'xcorr rank', 'sequence' ] def conv(f, value): value = value.strip() if len(value): return f(value) def validate(record): return all(record.has_key(m) for m in _mandatories) widgets = [ Percentage(), Bar(), ETA() ] progress = ProgressBar(widgets = widgets, maxval = os.path.getsize(filename)).start() with open(filename) as f: reader = csv.reader(f, delimiter='\t') # Header row = reader.next() if row != fields: raise ParseError('Header: ', filename, 1, ' '.join(row)) # Body for row in reader: progress.update(f.tell()) if len(row) != len(fields): raise ParseError('Line: ', filename, reader.line_num, ' '.join(row)) r = dict((k, conv(f,x)) for k, f, x in zip(fields, casts, row)) if r: if not validate(r): raise ParseError('Missing: ', filename, reader.line_num, ' '.join(row)) yield r progress.finish() sys.stdout.write('\n')