def generate_cord(): """ TODO: Pass the parameters as input arguments. Parameters: - Fs : sampling frequency - F0 : frequency of the notes forming chord - gain : gains of individual notes in the chord - duration : duration of the chord in second - alpha : attenuation in KS algorithm """ Fs = 48000 # D2, D3, F3, G3, F4, A4, C5, G5 F0 = 440 * pylab.array( [2**-(31.0/12), 2**-(19.0/12), 2**-(16.0/12), 2**(-14.0/12), 2**-(4.0/12), 1.0, 2**(3.0/12), 2**(10.0/12)]) gain = [1.2, 3.0, 1.0, 2.2, 1.0, 1.0, 1.0, 3.5] duration = 4.0 alpha = 0.9785 # Number of samples in the chord. nbsample_chord = Fs * duration # This is used to correct alpha later, so that all the notes # decay together (with the same decay rate). first_duration = pylab.ceil(nbsample_chord / pylab.round_(Fs/F0[0])) # Initialization. chord = pylab.zeros(nbsample_chord) for i, f in enumerate(F0): print("Working on %g / %g" % (i+1, len(F0))) # Get M and duration parameter. current_M = pylab.round_(Fs/f) current_duration = pylab.ceil(nbsample_chord / current_M) # Correct current alpha so that all the notes decay together # (with the same decay rate) current_alpha = alpha ** (first_duration / current_duration) # Let Paul's high D on the bass ring a bit longer. if i == 1: current_alpha = current_alpha ** 0.8 # Generate input and output of KS algorithm. x = pylab.rand(current_M) y = ks(x, current_alpha, int(current_duration)) y = y[:int(nbsample_chord)] # Construct the chord by adding the generated note (with the # appropriate gain). chord = chord + gain[i] * y return Fs, duration, chord
def pie_chart(): plb.figure("How do we get to work") plb.axes([.035, 0.035, 0.9, 0.9]) l = 'car', 'truck', 'boat', 'dingie', 'train', 'plane', 'bus', 'rocket', 'tram', 'other' b = plb.round_(plb.random(10), decimals=2) c = ['blue', 'red', 'green', 'gray', 'yellowgreen', 'gold', 'lightskyblue', 'lightcoral', 'cyan', 'orange'] e = (0, 0, 0, 0, 0, 0, 0, 0.05, 0, 0) plb.cla() plb.pie(b, explode=e, labels=l, colors=c, radius=.75, autopct='%1.0f%%', shadow=True, startangle=15) plb.axis('equal') plb.xticks(()); plb.yticks(()) plb.pause(5)
def get_chans(msfile, fi=1418e06, di=100): ''' For msfile, takes a desired frequency (in Hz) and returns the lower and upper bounds for a window on either side of fi, in lengths of di ''' tb.open(msfile+'/SPECTRAL_WINDOW'); f1 = chan_freq = tb.getcol('CHAN_FREQ')[0]; df = tb.getcol('CHAN_WIDTH')[0]; i = pl.round_((f1-fi)/df); tb.close(); spw_lower = i - di; spw_upper = i + di; if spw_lower < 0: spw_lower = 0; spw_upper = 200; return(str(int(spw_lower)), str(int(spw_upper)));
def probeData(settings): print "Probing data", settings.fileName samplesPerCode = int( round(settings.samplingFreq / (settings.codeFreqBasis / settings.codeLength))) samples = getSamples.int8(settings.fileName, 10 * samplesPerCode, settings.skipNumberOfBytes) #Initialize figure fig = pylab.figure() pylab.clf() #X axis timeScale = [x*(1/settings.samplingFreq) for x in \ range(0,int(round((5e-3 + 1/settings.samplingFreq)*settings.samplingFreq)))] #Time domain plot pylab.subplot(2, 2, 1) plot_max = int(round(samplesPerCode / 50)) pylab.plot([1000 * i for i in timeScale[0:plot_max]], samples[0:plot_max]) pylab.title('Time domain plot') pylab.xlabel('Time (ms)') pylab.ylabel('Amplitude') #Frequency domain plot (Pxx,freqs) = matplotlib.mlab.psd(x = samples-numpy.mean(samples),\ noverlap = 1024,\ NFFT = 2048,\ Fs = settings.samplingFreq/1e6) pylab.subplot(2, 2, 2) pylab.semilogy(freqs, Pxx) pylab.title('Frequency Domain Plot') pylab.xlabel('Frequency (MHz)') pylab.ylabel('Magnitude') #Histogram pylab.subplot(2, 2, 3) xticks = pylab.unique(samples) pylab.hist(samples, len(xticks)) axis = pylab.axis() pylab.axis([min(samples), max(samples), axis[2], axis[3]]) xticks = pylab.unique(pylab.round_(xticks)) pylab.xticks(xticks) pylab.title('Histogram') return fig
def get_chans(msfile, fi=1418e06, di=100): """ For msfile, takes a desired frequency (in Hz) and returns the lower and upper bounds for a window on either side of fi, in lengths of di """ tb.open(msfile + "/SPECTRAL_WINDOW") f1 = chan_freq = tb.getcol("CHAN_FREQ")[0] df = tb.getcol("CHAN_WIDTH")[0] i = pl.round_((f1 - fi) / df) tb.close() spw_lower = i - di spw_upper = i + di if spw_lower < 0: spw_lower = 0 spw_upper = 200 return (str(int(spw_lower)), str(int(spw_upper)))
def probeData(settings): print "Probing data", settings.fileName samplesPerCode = int(round(settings.samplingFreq / (settings.codeFreqBasis / settings.codeLength))) samples = getSamples.int8(settings.fileName,10*samplesPerCode,settings.skipNumberOfBytes) #Initialize figure fig = pylab.figure() pylab.clf() #X axis timeScale = [x*(1/settings.samplingFreq) for x in \ range(0,int(round((5e-3 + 1/settings.samplingFreq)*settings.samplingFreq)))] #Time domain plot pylab.subplot(2,2,1) plot_max = int(round(samplesPerCode/50)) pylab.plot([1000*i for i in timeScale[0:plot_max]],samples[0:plot_max]) pylab.title('Time domain plot') pylab.xlabel('Time (ms)') pylab.ylabel('Amplitude') #Frequency domain plot (Pxx,freqs) = matplotlib.mlab.psd(x = samples-numpy.mean(samples),\ noverlap = 1024,\ NFFT = 2048,\ Fs = settings.samplingFreq/1e6) pylab.subplot(2,2,2) pylab.semilogy(freqs,Pxx) pylab.title('Frequency Domain Plot') pylab.xlabel('Frequency (MHz)') pylab.ylabel('Magnitude') #Histogram pylab.subplot(2,2,3) xticks = pylab.unique(samples) pylab.hist(samples,len(xticks)) axis = pylab.axis() pylab.axis([min(samples),max(samples),axis[2],axis[3]]) xticks = pylab.unique(pylab.round_(xticks)) pylab.xticks(xticks) pylab.title('Histogram'); return fig
def getLogCurve(group, args): """Return logistic curve of each Pandas GroupBy group""" plateFlag = args[0] finalTime = args[1][group.name] # key = (sample, well) time = py.linspace(0.0, finalTime, 100) logistic = GrowthCurve.logistic(time, group["y0"][0], group["asymptote"][0], group["maxgrowth"][0], group["lag"][0]) # Create DataFrame object with logistic curve d = {"od": logistic} if plateFlag: d["mainsource"] = group["mainsource"][0] d["compound"] = group["compound"][0] df = pd.DataFrame(d, index=py.round_(time, decimals=3)) df.index.name = "time" return df
def imslice(myimage, v, theta, x0, y0, D): ia.open(myimage); pix = pl.absolute(pl.round_((ia.summary()['incr']*180./pl.pi)*3600.)[0]) if v=='major': v = [0., D]; v2 = [0., -D]; if v=='minor': v = [D, 0]; v2 = [-D, 0]; x1 = pl.array((v)); x1r = pl.dot(rotmax(theta), x1); x2 = pl.array((v2)); x2r = pl.dot(rotmax(theta), x2); j1 = x0+x1r[0]; j2 = x0+x2r[0]; k1 = y0+x1r[1]; k2 = y0+x2r[1]; s = ia.getslice([j1, j2], [k1, k2], npts=500, method='linear') x = s['distance']*pix-D*pix; y = s['pixel']; ia.close(); return x, y
msno.matrix(train) # ## Data Visualization # # ### Let's visualize the data to gain a new perspective. # # ### Class Distribution # # #### I can see from the plot below that there are more ships than icebergs in our dataset but still the class is not unbalanced. # In[186]: count_iceberg = train.is_iceberg.value_counts() plt.figure(figsize=(10, 10)) b = plb.round_(count_iceberg.values) c = ['green', 'red'] e = [0, 0.1] l = 'Ship', 'Iceberg' plb.pie(b, explode=e, labels=l, colors=c, radius=.9, autopct='%1.2f%%', shadow=True, startangle=15) #plt.xticks(range(2), ['Ships', 'Icebergs']) plt.title('Class Distribution: Ship or Iceberg') plt.show()
def print_mare(vars): if 'p_obs' in vars: are = pl.atleast_1d( pl.absolute( (vars['p_obs'].value - vars['pi'].value) / vars['pi'].value)) print 'mare:', pl.round_(pl.median(are), 2)
def curveFit(group, args): """Perform logistic fit on each growth curve using Pandas GroupBy groups""" sample, rep, well = group.name dataLogParams = args[0] plateFlag = args[1] growthFlag = args[2] # Perform logistic fitting try: if plateFlag: gCurve = GrowthCurve.GrowthCurve(group["od"], sample, rep, well, growthFlag) else: gCurve = GrowthCurve.GrowthCurve(group, sample, rep, well, growthFlag) except Exception as e: util.printStatus("sample: {}, rep: {}, well: {}".format(*group.name)) util.printStatus(e) util.exitScript() # Add logistic parameters to DataFrame time = group.index.get_level_values("time") dataLogParams["y0"].loc[sample, rep, well] = gCurve.y0 dataLogParams["maxgrowth"].loc[sample, rep, well] = gCurve.maxGrowthRate dataLogParams["asymptote"].loc[sample, rep, well] = gCurve.asymptote dataLogParams["lag"].loc[sample, rep, well] = gCurve.lag dataLogParams["growthlevel"].loc[sample, rep, well] = gCurve.growthLevel dataLogParams["glscaled"].loc[sample, rep, well] = gCurve.glScaled dataLogParams["r"].loc[sample, rep, well] = gCurve.expGrowth dataLogParams["auc_raw"].loc[sample, rep, well] = gCurve.auc_raw dataLogParams["auc_rshift"].loc[sample, rep, well] = gCurve.auc_rshift dataLogParams["auc_log"].loc[sample, rep, well] = gCurve.auc_log dataLogParams["auc_lshift"].loc[sample, rep, well] = gCurve.auc_lshift dataLogParams["growthclass"].loc[sample, rep, well] = gCurve.growthClass dataLogParams["err_mse"].loc[sample, rep, well] = gCurve.mse if plateFlag: try: m = group["mainsource"][0] except TypeError as e: stars = "*" * 55 err = "\n" + stars + "\nERROR: trying to obtain 'mainsource' from " err += " ".join((sample, rep, well)) + "\n" + stars util.printStatus(err) util.printStatus(e) util.printStatus(group["mainsource"].values) util.printStatus(group) util.exitScript() try: c = group["compound"][0] except TypeError as e: stars = "*" * 55 err = "\n" + stars + "\nERROR: trying to obtain 'compound' from " err += " ".join((sample, rep, well)) + "\n" + stars util.printStatus(err) util.printStatus(e) util.printStatus(group["compound"].values) util.printStatus(group) util.exitScript() try: dataLogParams["mainsource"].loc[sample, rep, well] = m dataLogParams["compound"].loc[sample, rep, well] = c except TypeError as e: stars = "*" * 55 err = "\n" + stars + "\nERROR: trying to use loc() method on " err += " ".join((sample, rep, well)) + "\n" + stars util.printStatus(err) util.printStatus(e) util.printStatus(group) util.exitScript() # Create Series object of logistic values to return idxNames = ["sample", "rep", "well", "time"] index = zip([group.name] * len(time), time) index = [x[0] + tuple([x[1]]) for x in index] index = pd.MultiIndex.from_tuples(index, names=idxNames) # Create DataFrame object with new logistic curve d = {"od": gCurve.dataLogistic} if plateFlag: d["mainsource"] = m d["compound"] = c df = pd.DataFrame(d, index=py.round_(time, decimals=3)) df.index.name = "time" return df
plb.hist(unif_dist, bins=24, histtype='stepfilled', normed=True, color='cyan', label='Uniform') plb.hist(gaus_dist, bins=24, histtype='stepfilled', normed=True, color='orange', label='Gaussian', alpha=0.065) plb.legend(loc='upper left') plb.title('Gaussian vs Uniform distribution/Histogram') plb.xlabel('Value') plb.ylabel('Frequency') plb.grid(True) plb.show() # Pie Chart plb.figure('How do we get to work:') plb.axes([0.035, 0.035, 0.9, 0.9]) l = 'Car', 'Truck', 'Boat', 'Dingie', 'Train', 'Plane', 'Bus', 'Rocket', 'Tram', 'Other' b = plb.round_(plb.random(10), decimals=2) c = ['blue', 'red', 'green', 'gray', 'yellowgreen', 'gold', 'lightskyblue', 'lightcoral', 'cyan', 'orange'] e = (0, 0, 0, 0, 0, 0, 0, 0.05, 0, 0) #'explode' the 8th slice only plb.cla() plb.pie(b, explode = e, labels=l, colors=c, radius=.75, autopct='%1.2f%%', shadow=True, startangle=15) #we set aspect ratio to 'equal' so the pie is drawn in a circle plb.axis('equal') plb.xticks(()); plb.yticks(()) plb.show() # Contour plot: def f(x,y): return (2- x/3 + x**6 +2.125*y) * plb.exp(-x**2-y**2)
def print_mare(vars): if 'p_obs' in vars: are = pl.atleast_1d(pl.absolute((vars['p_obs'].value - vars['pi'].value)/vars['pi'].value)) print 'mare:', pl.round_(pl.median(are), 2)