def filterSignalsWithProbePhasing(dfProbe,dfSignal,plot=False,title='', timeFWHM=timeFWHM): import pandas as pd import numpy as np dfSignalOld=dfSignal.copy() dfSignal=dfSignal.copy() def ranges(nums): nums = sorted(set(nums)) gaps = [[s, e] for s, e in zip(nums, nums[1:]) if s+1 < e] edges = iter(nums[:1] + sum(gaps, []) + nums[-1:]) return list(zip(edges, edges)) # create Mask. Only keep data wher probe current is < 0. NAN elsewhere dfMask=pd.DataFrame(dfSignal.copy().to_numpy(),columns=['Data']) dfMask['time']=dfSignal.copy().index dfMask.Data[dfProbe.iloc[:,0].to_numpy()>0]=np.nan # add linear interpolations to fill-in NANs a=np.isnan(dfMask.Data.to_numpy()) indexNan=dfMask[a].index rangeNan=ranges(indexNan.to_numpy()) for i,r in enumerate(rangeNan): if r[0]==0: continue if r[-1]==dfMask.shape[0]-1: continue dfMask.Data.iloc[(r[0]):(r[1]+1)]=np.interp(dfMask.iloc[(r[0]):(r[1]+1)].time.to_numpy(), [dfMask.iloc[(r[0]-1)].time,dfMask.iloc[(r[1]+1)].time], [dfMask.iloc[(r[0]-1)].Data,dfMask.iloc[(r[1]+1)].Data]) dfMask=dfMask.set_index('time') dfMask=dfMask.dropna() dfMask=_gaussianFilter_df(dfMask.copy(),timeFWHM=timeFWHM,plot=False,filterType='low') dfSignal=filterDFByTime(dfSignal.copy(),dfMask.index[0],dfMask.index[-1]) dfResult=pd.DataFrame(dfSignal.values-dfMask.values,index=dfMask.index.to_numpy()) if plot==True: fig,ax=_plt.subplots(3,sharex=True) ax[0].plot(dfProbe.index*1e3,dfProbe,label='Probe\nCurrent') ax[1].plot(dfSignalOld.index*1e3,dfSignalOld,label='Original') ax[1].plot(dfMask.index*1e3,dfMask,label='Offset') ax[2].plot(dfResult.index*1e3,dfResult,label='Result') _plot.finalizeSubplot( ax[0], ylabel='A', title=title, ) _plot.finalizeSubplot(ax[1], ) _plot.finalizeSubplot(ax[2], xlabel='Time (ms)', ) _plot.finalizeFigure(fig,figSize=[6,4.5]) return dfResult
def makePlot(): """ Plot all relevant plots """ fig, p1 = _plt.subplots() p1.plot(time * 1e3, qStar, label=r'q$^*$') p1.plot(time * 1e3, qStarCorrected, label=r'q$^* * 1.15$') _plot.finalizeSubplot(p1, xlabel='Time (ms)', ylabel=r'q$^*$', ylim=[1, 5]) _plot.finalizeFigure(fig, title='%s' % shotno)
def leastSquareModeAnalysis( df, angles, modeNumbers=[0,-1,-2], timeFWHM_phaseFilter=0.1e-3, plot=False, title=''): """ Parameters ---------- df : pandas.core.frame.DataFrame Dataframe with multiple columns associated with different angles index = time angles : numpy.ndarray array of angles associated with the columns in df mode numbers : list of ints mode numbers to be analyzed timeFWHM_phaseFilter : float timewidth associated with pre-frequency calculating low-pass filter Returns ------- dfResults : pandas.core.frame.DataFrame output Examples -------- :: import hbtepLib as hbt df,dfRaw,dfMeta=hbt.get.taData_df(100000) angles=dfMeta.Phi.values dfResults=leastSquareModeAnalysis( df*1e4, angles, [0,-1,-2]) dfResults.plot() """ # initialize n=len(angles) m=len(modeNumbers)*2 if 0 in modeNumbers: m-=1 # construct A matrix A=_np.zeros((n,m)) i=0 for mode in modeNumbers: if mode == 0: A[:,i]=1 i+=1 else: A[:,i]=_np.sin(mode*angles) A[:,i+1]=_np.cos(mode*angles) i+=2 Ainv=_np.linalg.pinv(A) # perform least squares analysis x=Ainv.dot(df.transpose().values) # calculate amplitudes, phases, frequencies, etc. dfResults=_pd.DataFrame(index=df.index) i=0 for mode in modeNumbers: if mode == 0: dfResults['0']=x[i,:] i+=1 else: dfResults['%dSin'%mode]=x[i,:] dfResults['%dCos'%mode]=x[i+1,:] dfResults['%sX'%mode]=1j*dfResults['%sSin'%mode]+dfResults['%dCos'%mode] dfResults['%sAmp'%mode]=_np.sqrt(dfResults['%dSin'%mode]**2+dfResults['%dCos'%mode]**2) dfResults['%sPhase'%mode]=_np.arctan2(dfResults['%dSin'%mode],dfResults['%dCos'%mode]) if type(timeFWHM_phaseFilter) != type(None): dfResults['%sPhaseFilt'%mode]=_gaussianFilter_df( _pd.DataFrame(_unwrapPhase(dfResults['%dPhase'%mode]),index=df.index), timeFWHM=timeFWHM_phaseFilter, plot=False, filterType='low') else: dfResults['%sPhaseFilt'%mode]=_unwrapPhase(dfResults['%dPhase'%mode]) dfResults['%sFreq'%mode]=_np.gradient(dfResults['%sPhaseFilt'%mode])/_np.gradient(df.index.to_numpy())/(_np.pi*2) i+=2 if plot==True: from johnspythonlibrary2.Process.Pandas import filterDFByColOrIndex import johnspythonlibrary2.Plot as _plot import matplotlib.pyplot as _plt from johnspythonlibrary2.Process.Misc import extractIntsFromStr fig,ax=_plt.subplots(3,sharex=True) dfTemp=filterDFByColOrIndex(dfResults,'Amp') for key,val in dfTemp.iteritems(): modeNum=extractIntsFromStr(key)[0] ax[0].plot(val.index*1e3,val,label=modeNum) _plot.finalizeSubplot(ax[0], ylabel='', subtitle='amp.', title='%s'%title) dfTemp=filterDFByColOrIndex(dfResults,'Phase') dfTemp=filterDFByColOrIndex(dfTemp,'Filt',invert=True) for key,val in dfTemp.iteritems(): modeNum=extractIntsFromStr(key)[0] ax[1].plot(val.index*1e3,val,'.',label=modeNum) _plot.finalizeSubplot(ax[1], ylabel='rad', subtitle='phase', # title='%s'%title, ) dfTemp=filterDFByColOrIndex(dfResults,'Freq') for key,val in dfTemp.iteritems(): modeNum=extractIntsFromStr(key)[0] ax[2].plot(val.index*1e3,val*1e-3,label=modeNum) _plot.finalizeSubplot(ax[2], ylabel='kHz', subtitle='Freq.', xlabel='Time (ms)' ) _plot.finalizeFigure(fig) return dfResults
def sxrData(shotno=98170, tStart=_TSTART, tStop=_TSTOP, plot=False, dropBadChannels=True, forceDownload=False): """ Downloads (and optionally plots) soft xray sensor data. Parameters ---------- shotno : int shot number of desired data tStart : float time (in seconds) to trim data before default is 0 ms tStop : float time (in seconds) to trim data after default is 10 ms plot : bool default is False dropBadChannels : bool Drops bad channels Notes ----- Channels 5, 8, 10, 13 and 15 are considered "bad". These particular channels are frequently "missing" from the tree, inlclude anamolous data, or their signals are attenuated. Example ------- :: df=sxrData(106000,plot=True,tStart=1.5e-3,tStop=5.2e-3) """ # subfunctions @_backupDFs def dfSXR(shotno, dfSXRMeta): dfSXR = mdsData(shotno, dfSXRMeta.addresses.to_list(), columnNames=dfSXRMeta.index.to_list()) return dfSXR # load meta data sensor = 'SXR' try: directory = _path.dirname(_path.realpath(__file__)) dfMeta = _readOdsToDF('%s/listOfAllSensorsOnHBTEP.ods' % directory, sensor).set_index('names') except: dfMeta = _readOdsToDF('listOfAllSensorsOnHBTEP.ods', sensor).set_index('names') # load raw data df = dfSXR(shotno, dfMeta, forceDownload=forceDownload) # drop bad channels if dropBadChannels == True: badSensors = dfMeta[dfMeta.bad == True].index.to_list() dfMeta = dfMeta.drop(badSensors, axis=0) df = df.drop(columns=badSensors) # trim time df = _filterDFByTime(df, tStart, tStop) # optional high-pass filter dfHP = _gaussianFilter_df(df, timeFWHM=0.4e-3) # optional plot if plot == True: if True: # raw data fig, ax, cax = _plot.subplotsWithColormaps(2, sharex=True) cax[0].remove() for key, val in df.iteritems(): ax[0].plot(val, label=key) _plot.finalizeSubplot( ax[0], title='%d, raw' % shotno, ) temp = _np.copy(df.columns).astype(str) columns = _np.array([ '%d' % int(temp[i][-2:]) for i in range(len(temp)) ]).astype(float) df2 = df.copy() df2.columns = columns fig, ax, _ = _plotHbt.stripeyPlot( df2, fig=fig, ax=ax[1], cax=cax[1], # title='%d, raw'%shotno, colorMap='magma_r', zlim=[0, df.max().max()], levels=_np.linspace(0, df.max().max(), 41), ylabel='Channel #') _plot.finalizeFigure(fig) if True: # filtered data fig, ax, cax = _plot.subplotsWithColormaps(2, sharex=True) cax[0].remove() for key, val in dfHP.iteritems(): ax[0].plot(val, label=key) _plot.finalizeSubplot( ax[0], title='%d, high-pass filtered' % shotno, ) temp = _np.copy(dfHP.columns).astype(str) columns = _np.array([ '%d' % int(temp[i][-2:]) for i in range(len(temp)) ]).astype(float) dfHP2 = dfHP.copy() dfHP2.columns = columns fig, ax, _ = _plotHbt.stripeyPlot( dfHP2, fig=fig, ax=ax[1], cax=cax[1], # title='%d, high-pass filtered'%shotno, ylabel='Channel #') _plot.finalizeFigure(fig) return df, dfHP