def Readphfile_i(ascifile, k): # read ascii file import string from numpy import math _field = 0 f = file(ascifile, 'r') tit = f.readline() s = f.readlines() f.close() fieldin = {} for i in range(len(s)): if s[i][0:3] == '***': _field = _field + 1 fieldin[_field] = i field = raw_input('How many standard fields [' + str(_field) + ']? ') if not field: field = _field else: field = int(field) mj, mh, mk, st = {}, {}, {}, {} J, JH, HK, JK = {}, {}, {}, {} name_field = [] tj, th, tk = [], [], [] aj, ah, ak = [], [], [] numstar = [] for jj in range(1, field + 1): kk = fieldin[jj] xxx, _name_field, num = string.split(s[kk]) numstar.append(num) tj, th, tk = string.split(s[kk + 1]) aj, ah, ak = string.split(s[kk + 2]) _j, _h, _k = [], [], [] _J, _JH, _HK, _JK, _st = [], [], [], [], [] for ii in range(kk + 3, kk + 3 + int(num)): varj, varh, vark, varst, varJ, varJH, varHK = string.split(s[ii]) if aj != '999': varj = float(varj) + 2.5 * math.log10( float(tj)) - k['J'] * float(aj) if ah != '999': varh = float(varh) + 2.5 * math.log10( float(th)) - k['H'] * float(ah) if ak != '999': vark = float(vark) + 2.5 * math.log10( float(tk)) - k['K'] * float(ak) _j.append(-float(varj) + float(varJ)) _h.append(-float(varh) + (float(varJ) - float(varJH))) _k.append(-float(vark) + (float(varJ) - float(varJH) - float(varHK))) _st.append(varst) _J.append(float(varJ)) _JH.append(float(varJH)) _JK.append(float(varJH) + float(varHK)) _HK.append(float(varHK)) name_field.append(_name_field) mj[jj], mh[jj], mk[jj], st[jj], J[jj], JH[jj], HK[ jj] = _j, _h, _k, _st, _J, _JH, _HK JK[jj] = _JK return mj, mh, mk, st, J, JH, HK, JK, field, name_field
def Readphfile_o(ascifile,k): # read ascii file import string from numpy import math _field=0 f=file(ascifile,'r') tit=f.readline() s=f.readlines() f.close() fieldin={} for i in range(len(s)): if s[i][0:3]=='***': _field=_field+1 fieldin[_field]=i field=raw_input('How many standard fields ['+str(_field)+']? ') if not field: field=_field else: field=int(field) mu,mb,mv,mr,mi,st={},{},{},{},{},{} V,BV,UB,VR,RI,VI={},{},{},{},{},{} name_field=[] tu,tb,tv,tr,ti=[],[],[],[],[] au,ab,av,ar,ai=[],[],[],[],[] numstar=[] for j in range(1,field+1): kk=fieldin[j] xxx,_name_field,num=string.split(s[kk]) numstar.append(num) tu,tb,tv,tr,ti=string.split(s[kk+1]) au,ab,av,ar,ai=string.split(s[kk+2]) _u,_b,_v,_r,_i=[],[],[],[],[] _V,_BV,_UB,_VR,_RI,_VI,_st=[],[],[],[],[],[],[] for ii in range(kk+3,kk+3+int(num)): varu,varb,varv,varr,vari,varst,varV,varBV,varUB,varVR,varRI=string.split(s[ii]) if au!='999': varu=float(varu)+2.5*math.log10(float(tu))-k['U']*float(au) if ab!='999': varb=float(varb)+2.5*math.log10(float(tb))-k['B']*float(ab) if av!='999': varv=float(varv)+2.5*math.log10(float(tv))-k['V']*float(av) if ar!='999': varr=float(varr)+2.5*math.log10(float(tr))-k['R']*float(ar) if ai!='999': vari=float(vari)+2.5*math.log10(float(ti))-k['I']*float(ai) _u.append(-float(varu)+(float(varV)+float(varBV)+float(varUB))) _b.append(-float(varb)+(float(varV)+float(varBV))) _v.append(-float(varv)+float(varV)) _r.append(-float(varr)+(float(varV)-float(varVR))) _i.append(-float(vari)+(float(varV)-float(varVR)-float(varRI))) _st.append(varst) _V.append(float(varV)) _BV.append(float(varBV)) _UB.append(float(varUB)) _VR.append(float(varVR)) _VI.append(float(varVR)+float(varRI)) _RI.append(float(varRI)) name_field.append(_name_field) mu[j],mb[j],mv[j],mr[j],mi[j],st[j],V[j],BV[j],UB[j],VR[j],RI[j]=_u,_b,_v,_r,_i,_st,_V,_BV,_UB,_VR,_RI VI[j]=_VI return mu,mb,mv,mr,mi,st,V,BV,UB,VR,RI,VI,field,name_field
def Readphfile_s(ascifile,k): # read ascii file import string from numpy import math _field=0 f=file(ascifile,'r') tit=f.readline() s=f.readlines() f.close() fieldin={} for i in range(len(s)): if s[i][0:3]=='***': _field=_field+1 fieldin[_field]=i field=raw_input('How many standard fields ['+str(_field)+']? ') if not field: field=_field else: field=int(field) mu,mg,mr,mi,mz,st={},{},{},{},{},{} r,gr,ug,ri,iz,gi={},{},{},{},{},{} name_field=[] tu,tg,tr,ti,tz=[],[],[],[],[] au,ag,ar,ai,az=[],[],[],[],[] numstar=[] for j in range(1,field+1): kk=fieldin[j] xxx,_name_field,num=string.split(s[kk]) numstar.append(num) tu,tg,tr,ti,tz=string.split(s[kk+1]) au,ag,ar,ai,az=string.split(s[kk+2]) _u,_g,_r,_i,_z=[],[],[],[],[] _rr,_ug,_gr,_ri,_iz,_gi,_st=[],[],[],[],[],[],[] for ii in range(kk+3,kk+3+int(num)): var_u,var_g,var_r,var_i,var_z,varst,varr,vargr,varug,varri,variz=string.split(s[ii]) if au!='999': var_u=float(var_u)+2.5*math.log10(float(tu))-k['u']*float(au) if ag!='999': var_g=float(var_g)+2.5*math.log10(float(tg))-k['g']*float(ag) if ar!='999': var_r=float(var_r)+2.5*math.log10(float(tr))-k['r']*float(ar) if ai!='999': var_i=float(var_i)+2.5*math.log10(float(ti))-k['i']*float(ai) if az!='999': var_z=float(var_z)+2.5*math.log10(float(tz))-k['z']*float(az) _u.append(-float(var_u)+(float(varr)+float(vargr)+float(varug))) _g.append(-float(var_g)+(float(varr)+float(vargr))) _r.append(-float(var_r)+float(varr)) _i.append(-float(var_i)+(float(varr)-float(varri))) _z.append(-float(var_z)+(float(varr)-float(varri)-float(variz))) _st.append(varst) _rr.append(float(varr)) _ug.append(float(varug)) _gr.append(float(vargr)) _ri.append(float(varri)) _gi.append(float(vargr)+float(varri)) _iz.append(float(variz)) name_field.append(_name_field) mu[j],mg[j],mr[j],mi[j],mz[j],st[j],r[j],ug[j],gr[j],ri[j],iz[j]=_u,_g,_r,_i,_z,_st,_rr,_ug,_gr,_ri,_iz gi[j]=_gi return mu,mg,mr,mi,mz,st,r,gr,ug,ri,iz,gi,field,name_field
def calcIdf(text, word): sumValue = sum([1.0 for i in text if word in i]) if sumValue != 0.0: idfValue = math.log10(len(text) / sumValue) else: idfValue = 0.0 return idfValue
def get_rms(): global zdok save_zdok = zdok set_zdok(0) snap = adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2) rmsSnap = np.std(snap) loadingFactor = -20.0 * math.log10(128 / rmsSnap) print "If0 Rms = %f, loading factor = %f" % (rmsSnap, loadingFactor) set_zdok(1) snap = adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2) rmsSnap = np.std(snap) loadingFactor = -20.0 * math.log10(128 / rmsSnap) print "If1 Rms = %f, loading factor = %f" % (rmsSnap, loadingFactor) if zdok != save_zdok: set_zdok(save_zdok)
def get_rms(): global zdok save_zdok = zdok set_zdok(0) snap=adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2) rmsSnap = np.std(snap) loadingFactor = -20.0*math.log10(128/rmsSnap) print "If0 Rms = %f, loading factor = %f" % (rmsSnap,loadingFactor) set_zdok(1) snap=adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2) rmsSnap = np.std(snap) loadingFactor = -20.0*math.log10(128/rmsSnap) print "If1 Rms = %f, loading factor = %f" % (rmsSnap,loadingFactor) if zdok != save_zdok: set_zdok(save_zdok)
def computeSRR(self , residual=None): ''' routine to compute approximation Signal To Residual ratio so far using: .. math::SRR = 10 \log_10 \frac{\| \tilde{x} \|^2}{\| \tilde{x} - x \|^2} where :math:`\tilde{x}` is the reconstructed signal and :math:`x` the original ''' if not isinstance(self.recomposedSignal , signals.Signal): return NINF # recomposedEnergy = sum((self.recomposedSignal.dataVec)**2) recomposedEnergy = self.recomposedSignal.energy if recomposedEnergy <= 0: return NINF if residual is None: resEnergy = sum((self.originalSignal.dataVec - self.recomposedSignal.dataVec)**2) else: resEnergy = residual.energy # resEnergy = sum((self.originalSignal.dataVec - self.recomposedSignal.dataVec)**2) if resEnergy == 0: return PINF return 10*math.log10( recomposedEnergy/ resEnergy )
def mutualInfor(): allAgeSer = Series.from_csv(path="D://data//allAppNum.dat", sep="\t") allAgeSer = allAgeSer / totalUserNum ageTypeNum = Series({ "0": 99934.0, "1": 377981.0, "2": 396867.0, "3": 37225.0, "4": 9226.0, "5": 1693.0, "6": 565.0, "7": 4259.0 }) for ageType in ageTypeNum.index: oneAgeSer = Series.from_csv(path="D://data//app" + ageType + ".dat", sep="\t") oneAgeSer = oneAgeSer / ageTypeNum[ageType] p_y = ageTypeNum[ageType] / totalUserNum outSer = Series({}) index = 0 for key, val in oneAgeSer.iteritems(): if len(key.strip()) > 0 and key in allAgeSer: mi = val * p_y * (math.log10(val / allAgeSer[key]) / math.log10(2)) if mi > 0: outSer[key] = mi index += 1 if index % 100 == 0: print index, ageType, key, mi print ageType + " sorting..." outSer.sort_values(ascending=False, inplace=True) perNum = outSer.count() * 0.5 print "outputing... " + str(outSer.count()) + " " + str(perNum) index = 0 with open("D://data//mi" + str(ageType) + ".dat", "w") as outFile: for key, val in outSer.iteritems(): index += 1 if index < perNum: outLine = key + "\t" + str(val) outFile.write(outLine + "\n")
def mutualInfor(): allAgeSer = Series.from_csv(path="D://data//allAppNum.dat", sep="\t") allAgeSer = allAgeSer / totalUserNum ageTypeNum = Series({ "A": 1221.0, "B": 46149.0, "C": 21213.0, "D": 2406.0, "E": 382.0, "F": 57.0, "G": 20.0, "H": 104.0 }) for ageType in ageTypeNum.index: oneAgeSer = Series.from_csv(path="D://data//app" + ageType + ".dat", sep="\t") oneAgeSer = oneAgeSer / ageTypeNum[ageType] p_y = ageTypeNum[ageType] / totalUserNum outSer = Series({}) index = 0 for key, val in oneAgeSer.iteritems(): if len(key.strip()) > 0 and key in allAgeSer: mi = val * p_y * (math.log10(val / allAgeSer[key]) / math.log10(2)) if mi > 0: outSer[key] = mi index += 1 if index % 100 == 0: print index, ageType, key, mi print ageType + " sorting..." outSer.sort_values(ascending=False, inplace=True) perNum = outSer.count() * 0.3 print "outputing... " + str(outSer.count()) + " " + str(perNum) index = 0 with open("D://data//mi" + ageType + ".dat", "w") as outFile: for key, val in outSer.iteritems(): index += 1 if index < perNum: outLine = key + "\t" + str(val) outFile.write(outLine + "\n")
def dosnap(fr=0, name=None, rpt=1, donot_clear=False, plot=True): """ Takes a snapshot and uses fit_cores to fit a sine function to each core separately assuming a CW signal is connected to the input. The offset, gain and phase differences are reoprted for each core as well as the average of all four. The parameters are: fr The frequency of the signal generator. It will default to the last frequency set by set_freq() name the name of the file into which the snapshot is written. 5 other files are written. Name.c1 .. name.c4 contain themeasurements from cores a, b, c and d. Note that data is taken from cores in the order a, c, b, d. A line is appended to the file name.fit containing signal freq, average zero, average amplitude followed by triplets of zero, amplitude and phase differences for cores a, b, c and d name defaults to if0 or if1, depending on the current zdok rpt The number of repeats. Defaults to 1. The c1 .. c4 files mentioned above are overwritten with each repeat, but new rows of data are added to the .fit file for each pass. """ global freq if name == None: name = "if%d" % (zdok) avg_pwr_sinad = 0 if fr == 0: fr = freq for i in range(rpt): snap = adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2) if (plot): plt.clf() plt.plot(snap) plt.show(block=False) rmsSnap = np.std(snap) loadingFactor = -20.0 * math.log10(128 / rmsSnap) print "Rms = %f, loading factor = %f" % (rmsSnap, loadingFactor) if i == rpt - 1: np.savetxt(name, snap, fmt='%d') ogp, pwr_sinad = fit_cores.fit_snap(snap, fr, samp_freq, name,\ clear_avgs = ((i == 0) and not donot_clear), prnt = (i == rpt-1)) avg_pwr_sinad += pwr_sinad return ogp, avg_pwr_sinad / rpt
def dosnap(fr=0, name=None, rpt = 1, donot_clear=False, plot=True): """ Takes a snapshot and uses fit_cores to fit a sine function to each core separately assuming a CW signal is connected to the input. The offset, gain and phase differences are reoprted for each core as well as the average of all four. The parameters are: fr The frequency of the signal generator. It will default to the last frequency set by set_freq() name the name of the file into which the snapshot is written. 5 other files are written. Name.c1 .. name.c4 contain themeasurements from cores a, b, c and d. Note that data is taken from cores in the order a, c, b, d. A line is appended to the file name.fit containing signal freq, average zero, average amplitude followed by triplets of zero, amplitude and phase differences for cores a, b, c and d name defaults to if0 or if1, depending on the current zdok rpt The number of repeats. Defaults to 1. The c1 .. c4 files mentioned above are overwritten with each repeat, but new rows of data are added to the .fit file for each pass. """ global freq if name == None: name = "if%d" % (zdok) avg_pwr_sinad = 0 if fr == 0: fr = freq for i in range(rpt): snap=adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2) if(plot): plt.clf() plt.plot(snap) plt.show(block = False) rmsSnap = np.std(snap) loadingFactor = -20.0*math.log10(128/rmsSnap) print "Rms = %f, loading factor = %f" % (rmsSnap,loadingFactor) if i == rpt-1: np.savetxt(name, snap,fmt='%d') ogp, pwr_sinad = fit_cores.fit_snap(snap, fr, samp_freq, name,\ clear_avgs = ((i == 0) and not donot_clear), prnt = (i == rpt-1)) avg_pwr_sinad += pwr_sinad return ogp, avg_pwr_sinad/rpt
def getEntropy(p): tmp = math.log10(p) / math.log10(2) return -(p * tmp)
time_format = "%y %m %d %H %M" starttime_hhmm = datetime.datetime.strptime(Start_simulation, time_format) starttime_round = round_minutes(starttime_hhmm, 'down', 60) # arrotonda per difetto starttime endemittime_hhmm = datetime.datetime.strptime(End_emission, time_format) endemittime_round = round_minutes(endemittime_hhmm, 'up', 60) # arrotonda per eccesso endemittime runtime = endemittime_round - starttime_round # numero ore arrotondate tra inizio e fine emissione n_runs = np.int(np.floor(runtime.total_seconds() / deltat_plumemom)) # numero run di PlumeMoM par_mfr_log10 = float(math.log10(par_mfr)) er_mfr_log10 = float(math.log10(er_mfr)) par_dur_sec = (int(paroxysm_duration_h) * int(3600)) + (int(paroxysm_duration_min) * int(60)) er_dur_sec = int(eruption_duration_mo) * int(2592000) + int( eruption_duration_day) * int(86400) + int(eruption_duration_h) * int(3600) toter_dur_h = (int(par_dur_sec) + int(er_dur_sec) + (int(Start_simulation_minute) * int(60)) + (int(60) - int(End_emission_minute) * int(60))) / int(3600) totmass_par = float(par_mfr) * int(par_dur_sec) totmass_er = float(er_mfr) * int(er_dur_sec) if paroxysm_duration_h == 0: totmass_1st_h = float(totmass_par) + float(er_mfr) * (float(3600) -
End_emission = str(End_emission_year) + str(" ") + str( End_emission_month) + str(" ") + str(End_emission_day) + str(" ") + str( End_emission_hour) + str(" ") + str(End_emission_minute) time_format = "%y %m %d %H %M" starttime_hhmm = datetime.datetime.strptime(Start_simulation, time_format) starttime_round = round_minutes(starttime_hhmm, 'down', 60) # arrotonda per difetto starttime endemittime_hhmm = datetime.datetime.strptime(End_emission, time_format) endemittime_round = round_minutes(endemittime_hhmm, 'up', 60) # arrotonda per eccesso endemittime runtime = endemittime_round - starttime_round # numero ore arrotondate tra inizio e fine emissione n_runs = np.int(np.floor(runtime.total_seconds() / deltat_plumemom)) # numero run di PlumeMoM er_mfr_log10 = float(math.log10(er_mfr)) er_mfr_log10in = str( ((str(er_mfr_log10) + str(", ")) * (int(n_runs) - int(1)))) er_mfr_log10 = str(er_mfr_log10) with open("mass_flow_rates_eruption.txt", "w") as f1: f1.write(er_mfr_log10in), f1.write(er_mfr_log10) f1.close() print "MFR eruption (log10_kg/s) = ", er_mfr_log10 print '\n' print "Done"
def plotTF(self , labelX=True , labelY=True, fontsize=12 , ylim=None, patchColor=None , labelColor=None , multicolor=False ,axes=None ,maxAtoms=None, recenter=None,keepValues=False, french=False,Alpha=False,logF=False): """ A time Frequency plot routine using Matplotlib each atom of the approx is plotted in a time-frequency plane with a gray scale for amplitudes Many options are available: - labelX : whether or not to add the Time label on the X axis - labelY : whether or not to add the Frequency label on the Y axis - fontsize : the label fontSize - ylim : the frequency range of the plot - patchColor : specify a single color for all atoms - maxAtoms : specify a maximum number of atom to take into account. Usually atoms are ordered by decreasing amplitude due to MP rules, meaning only the most salient atoms will be plotted - recenter : a couple of values to offset the time and frequency localization of atoms - keepValues : use atom Amplitudes for atom colorations - french : all labels in french - Alpha : use transparency - logF : frequency axis in logarithmic scale """ # plt.figure() if french: labx = "Temps (s)" laby = "Frequence (Hz)" else: labx = "Time (s)" laby = "Frequency (Hz)" Fs = self.samplingFrequency # first loop to recover max value maxValue = 0 maxFreq = 1.0 minFreq = 22000.0 if not keepValues: valueArray = array([math.log10(abs(atom.getAmplitude())) for atom in self.atoms]) else: valueArray = array([abs(atom.getAmplitude()) for atom in self.atoms]) if multicolor: cmap = cc.LinearSegmentedColormap('jet',abs(valueArray) ) for atom in self.atoms: if abs(atom.mdct_value) > maxValue: maxValue = abs(atom.mdct_value) # normalize to 0 -> 1 # if min(valueArray) < 0: if len(valueArray) > 0 and not keepValues: valueArray = valueArray - min(valueArray) valueArray = valueArray/max(valueArray) # Get target axes if axes is None: axes = plt.gca() # normalize the colors # if multicolor: # normedValues = cc.Normalize(valueArray) if maxAtoms is not None: maxTom = min(maxAtoms,len(self.atoms)) else: maxTom = len(self.atoms) # Deal with static offsets if recenter is not None: yOffset= recenter[0] xOffset= recenter[1] else: yOffset,xOffset = 0,0 # print yOffset,xOffset if Alpha: alphaCoeff=0.6 else: alphaCoeff=1 patches = [] colors = [] for i in range(maxTom): atom = self.atoms[i] L = atom.length K = L/2 freq = atom.frequencyBin f = float(freq)*float(Fs)/float(L) bw = ( float(Fs) / float(K) ) #/ 2 p = float(atom.timePosition + K) / float(Fs) l = float(L - K/2) / float(Fs) # print "f = " ,f , " Hz" if f>maxFreq: maxFreq = f+bw+bw if f < minFreq: minFreq = f-bw-10*bw # xy = p, f-bw, xy = p - xOffset, f - yOffset, width, height = l, bw # xy = p - xOffset, log10(f) - yOffset, if logF: xy = p - xOffset , (12*log2(f - yOffset)) height = 1.0 # BUGFIX # print xy , f colorvalue = math.sqrt(valueArray[i]) if multicolor: patchtuplecolor = (math.sqrt(colorvalue), math.exp(-((colorvalue - 0.35)**2)/(0.15)), 1-math.sqrt(colorvalue)) colors.append(colorvalue) else: if patchColor is None: patchtuplecolor = (1-math.sqrt(colorvalue),1- math.sqrt(colorvalue),1- math.sqrt(colorvalue)) colors.append(colorvalue) else: patchtuplecolor = patchColor colors.append(patchColor) # patchtuplecolor = (colorvalue, colorvalue, colorvalue) # patch = mpatches.Rectangle(xy, width, height, facecolor=patchtuplecolor, edgecolor=patchtuplecolor , zorder=colorvalue ) # xy = p + L/2 , f-bw, patch = mpatches.Ellipse(xy, width, height, facecolor=patchtuplecolor, edgecolor=patchtuplecolor , zorder=colorvalue ) if keepValues: patches.append(patch) else: axes.add_patch(patch) if keepValues: # first sort the patches by values sortedIndexes = valueArray.argsort() PatchArray = array(patches) p = PatchCollection(PatchArray[sortedIndexes].tolist(),linewidths=0.,cmap=matplotlib.cm.copper_r,match_original=False, alpha=alphaCoeff) # print array(colors).shape p.set_array(valueArray[sortedIndexes]) axes.add_collection(p) if labelColor is None: labelColor = 'k' # finalize graphic options axes.set_xlim(0- xOffset , (self.length)/float(Fs)- xOffset) if ylim is None: axes.set_ylim(max(0 ,minFreq)- yOffset , maxFreq- yOffset) else: axes.set_ylim(ylim) if labelY: axes.set_ylabel(laby , fontsize=fontsize , color=labelColor) # plt.ylabel.set_color(labelColor) else: plt.setp(axes, 'yticklabels', []) if labelX: axes.set_xlabel(labx, fontsize=fontsize) else: plt.setp(axes, 'xticklabels', []) if keepValues: plt.colorbar(p)