# more trouble than its worth? for example, mode_list=None overides its effect #exec(pyfusion.utils.process_cmd_line_args()) if mask is None: mask = np.identity(len(sel)) if DAfilename is None: raise ValueError('New version - no need for DAfilename==None') try: type(thisDA) except: thisDA=None if (thisDA is None) or thisDA.name != DAfilename: print("reading in {d}".format(d=DAfilename)) from pyfusion.data.DA_datamining import DA, report_mem thisDA = DA(DAfilename,load=1) # wasteful for large files: dd=thisDA.copyda() dd = thisDA.da if clear_modes: old_modes = {} if verbose>0: print('clearing modes') for key in 'N,NN,M,MM,mode_id'.split(','): #print(key, len(dd[key])) old_modes.update({key: dd.pop(key,None)}) #clear all the mode keys if not isinstance(dd, dict): raise LookupError("dd not loaded into memory - can't store") if mode is None: mode = mode_list[0]
""" Example 5 - Figure 7 Adding the Alfven speed for each item into the data set. colour """ from pyfusion.data.DA_datamining import DA import numpy as np import matplotlib.pyplot as plt plt.figure('Example 5 - Figure 7') DA766 = DA('H1_766.npz', load=1) # Load data from 30 shots into a dictionary of arrays (DA) DA766.extract( locals() ) # Extract all the data into local variables, such as freq, t_mid, phases[0] wb = np.where((amp > 0.05) & (a12 > 0.7))[ 0] # find large amplitude, 'rotating' modes (see text). mp = 1.67e-27 mu_0 = 4 * np.pi * 1e-7 V_A = b_0 / np.sqrt( mu_0 * n_e * 1e18 * mp) # Calculate VA as a local variable , an array of the same size as the # extracted data DA766.update( dict(V_A=V_A )) # Add it to the dictionary - no problem if you use the same name # for the dictionary key DA766.save( 'H1_766_new') # Save the data set (under a new name if you prefer). # When you load the dataset in the future, the variable V_A will be there. plt.plot( k_h, freq * 1e3 / V_A, '.c'
from pyfusion.data.DA_datamining import DA from pyfusion.utils.utils import fix2pi_skips, modtwopi from pyfusion.visual.sp import sp from pyfusion.data.convenience import between, bw, btw, decimate, his, broaden DAfilename = 'PF2_121206MPRMSv2_Par_fixModes_chirp_ff.npz' from pyfusion.utils import process_cmd_line_args exec(process_cmd_line_args()) DA300 = DA(DAfilename, load=1) dd = DA300.copyda() DA300.extract(locals()) DA300.info() print('DA300', DAfilename)
shot = dd['shot'] last_check = seconds() for s in np.unique(dd['shot']): ws = np.where(s == shot)[0] if np.min(np.diff(dd['t_mid'][ws])) < 0: print('time order problem in ' + str(s)) else: print('OK'), if seconds() - last_check > 20: print('\n') last_check = seconds() from pyfusion.data.DA_datamining import DA, report_mem #DA_test = DA('/data/datamining/old/PF2_130813_50_5X_1.5_5b_rms_1_diags.npz') DA_test = DA(DAfilename) dd = DA_test.da #dd=ddd if 'phorig' not in dd.keys(): print('*** phorig not present') else: phorig = dd['phorig'] ph0 = dd['phases'][:, 0] b_0 = dd['b_0'] if (ph0 * phorig >= 0).all(): print('phases are untouched') else: pos = ph0 * phorig * b_0 if (pos >= 0).all():
from pyfusion.data.DA_datamining import DA da=DA('/mythextra/datamining/DA300_384_rms1_b5a_f32_diags.npz') time run -i pyfusion/examples/cluster_phases_three_colour.py DAfilename='/mythextra/datamining/DA300_384_rms1_b5a_f32_diags.npz' cl cl=26 time run -i pyfusion/examples/cluster_phases_three_colour.py DAfilename='/mythextra/datamining/DA300_384_rms1_b5a_f32_diags.npz' d_big=.5 run -i pyfusion/examples/density_cluster.py n_bins=70 n_plot=600 scale=.03 method='unsafe' dphase=1 clmax=35 sel=range(11,16) clgrey=-1 run -i pyfusion/examples/density_cluster.py n_bins=70 n_plot=600 scale=.03 method='unsafe' dphase=1 clmax=35 sel=range(11,16) clgrey=-1 savez_compressed('DA300_32_50639_600_70_bins',clinds=clinds, subset=subset, subset_counts=subset_counts,dphase=dphase,sel=sel,n_bins=n_bins,cmd="run -i pyfusion/examples/density_cluster.py n_bins=70 n_plot=600 scale=.03 method='unsafe' dphase=1 clmax=35 sel=range(11,16) clgrey=-1") clusterfile clusterfile='DA300_32_50639_600_70_bins' time run -i pyfusion/examples/cluster_phases_three_colour.py DAfilename='/mythextra/datamining/DA300_384_rms1_b5a_f32_diags.npz' cl=0 d_big=1.2 clusterfile='DA300_32_50639_600_70_bins' import pyximport; pyximport.install() import pyfusion.utils import pyfusion.utils.dist_mp import pyfusion.utils.dist_nogil x=np.load(clusterfile) for k in x.keys(): exec("{v}=x['{k}']".format(v=k,k=k)) subset=subset[:,sel] frlow=0 frhigh=1e10 run -i pyfusion/examples/cluster_phases_three_colour.py DAfilename='/2TBRAID/datamining/54_117_MP2012_384_rms_1_f32_part_params_114.npz' clusterfile='pyfusion/ideal_toroidal_modes.npz' cl=9 run -i x=np.load(clusterfile)
_var_defaults = """ DAfilename='../../datamining/DA/PF2_130813_50_5X_1.5_5b_rms_1_diags.npz' outfilename=None keep_open = 0 complevel=2 complib = 'zlib' # 'blosc' is fastest, but zlib can be compressed further "offline" # with blosc, get dataset </w_p> cannot be read, user defined filter is not available var='phases' # ?? maybe was used to develop """ exec(_var_defaults) exec(process_cmd_line_args()) filters = tb.Filters(complevel=complevel, complib=complib) dd = DA(DAfilename).da if outfilename is None: (base, ext) = os.path.splitext(os.path.realpath(DAfilename)) outfilename = base + os.path.extsep + 'h5' outf = tb.openFile(outfilename, "a") for var in dd.keys(): st_copy = seconds() if var in [nd.name for nd in outf.listNodes('/')]: raise LookupError('{f} already has a node "{n}"'.format( f=outf.filename, n=var)) val = dd[ var] # need to hold it in memory this way to avoid multiple access
#_PYFUSION_TEST_@@script from matplotlib import pyplot as plt import numpy as np from numpy import where, array from pyfusion.data.DA_datamining import DA from pyfusion.acquisition.read_text_pyfusion import read_text_pyfusion, plot_fs_DA, merge_ds from pyfusion.visual.window_manager import rmw, cmw, omw, lmw, smw from pyfusion.utils.utils import fix2pi_skips, modtwopi from pyfusion.visual.sp import off, on, tog from pyfusion.data.convenience import between, bw, btw, decimate, his, broaden dd=DA('W7X_MIR/DAMIRNOV_41_13_nocache_15_3m_2018fl.zip') dd.info() fig4, axs4=plt.subplots(4,1) figfs, axf=plt.subplots(1,1) plot_fs_DA(dd, ax=axf) w6=where((btw(dd['freq'],4,8)) & (dd['amp']>0.0015) & (dd['t_mid']>3.2) &(dd['shot'] == 180904035))[0] plot_fs_DA(dd, ax=axf, inds=w6,marker='s',ms=300,alpha=0.5) axs4[0].plot(array([modtwopi(ph,offset=0) for ph in dd['phases'][w6]]).T,'c',lw=.5) w6=where((btw(dd['freq'],4,8)) & (dd['amp']>0.015) & (dd['t_mid']>3.2) &(dd['shot'] == 180904035))[0] axs4[0].plot(array([modtwopi(ph,offset=0) for ph in dd['phases'][w6]]).T,'b',lw=.5) axs4[0].set_title('4-8kHz, after 3.2s') w0=where((btw(dd['freq'],0,2)) & (dd['amp']>0.0015) & (dd['t_mid']>3.2) &(dd['shot'] == 180904035))[0] plot_fs_DA(dd, ax=axf, inds=w0,marker='s',ms=300,alpha=0.5) axs4[1].plot(array([modtwopi(ph,offset=0) for ph in dd['phases'][w0]]).T,'b',lw=.5) axs4[1].set_title('0-2kHz, > 3.2s') w6e=where((btw(dd['freq'],4,12)) & (dd['amp']>0.015) & (dd['t_mid']<3.2) &(dd['shot'] == 180904035))[0] plot_fs_DA(dd, ax=axf, inds=w6e,marker='^',ms=300,alpha=0.5) axs4[2].plot(array([modtwopi(ph,offset=0) for ph in dd['phases'][w6e]]).T,'c',lw=.5) w6e=where((btw(dd['freq'],4,12)) & (dd['amp']>0.025) & (dd['t_mid']<3.2) &(dd['shot'] == 180904035))[0]
dafile=sys.argv[1] if len(sys.argv)>2: minpts=int(sys.argv[2]) if not(os.path.exists(dafile)): print('try LP/ folder') dafile = 'LP/' + dafile try: if da.name == dafile: print('NOT reloading file - run without -i to force reload') else: raise NameError except NameError: da = DA(dafile) areas = 'uncalibrated' if (len(da['Te'][0])<minpts): raise LookupError('fewer channels ({nc}) than minpts'. format(nc = len(da['Te'][0]))) try: if 'params' in da['info']: if da['info']['params']['pyfusion_version'] > '0.6.7b': areas = 'approximate' else: print('*** Warning - incorrect limiter numbers? ***') except: print('******* Really Old data file??***********')
""" A VERY small MP+HMP set, just shot 65139 - note that the copy and the extract() both duplicate data, which is good for development and debugging, but wasteful of space. """ from pyfusion.data.DA_datamining import DA from pyfusion.utils.utils import fix2pi_skips, modtwopi from pyfusion.visual.sp import sp from pyfusion.data.convenience import between, btw, bw, decimate, his, broaden _var_default = """ DAfilename='DA65MP2010HMPno612b5_M_N_fmax.npz' """ exec(_var_default) from pyfusion.utils import process_cmd_line_args exec(process_cmd_line_args()) DA65 = DA(DAfilename, load=1) dd = DA65.copyda() DA65.extract(locals()) DA65.info() print('DA65', DAfilename)
#!/usr/bin/env python """ Show the info dict of a DA_datamining npz file This won't work from ~/bin unless we include pyfusion in PYTHONPATH - do this with a crude hack below import os print(os.path.dirname(__file__)) _PYFUSION_TEST_@@pyfusion/examples/JSPF_tutorial/H1_766.npz _PYFUSION_TEST_@@pyfusion/examples/JSPF_tutorial/LP20160309_52_L53_2k2short.npz """ import matplotlib.pyplot as plt import sys sys.path.append('/home/bdb112/pyfusion/working/pyfusion/') from pyfusion.data.DA_datamining import DA if len(sys.argv) < 2: raise Exception('Syntax is: DA_info "filename"') else: filename = sys.argv[1] from pyfusion.data.DA_datamining import Masked_DA, DA da = DA(filename) print(da['info'])
alldas = [] for ts in np.linspace(tstart, tstart + seg_len * dt, nfull, endpoint=False): results = LP.process_swept_Langmuir(t_range=[ts, tend], dtseg=seg_len, plot_DA=plot_DA, **kwargs) if hasattr(LP, 'da'): alldas.append(LP.da) # this one must start at a V maximum half_kwargs = dict(dtseg=seg_len // 2, **kwargs) fit_params = half_kwargs['fit_params'] cycavg_h = fit_params['cycavg'] if cycavg_h is not None and (seg_len // 2) < cycavg_h[0]: cycavg_h = [seg_len // 2, 1, cycavg_h[2]] fit_params.update(dict(cycavg=cycavg_h)) half_kwargs.update(dict(fit_params=fit_params)) for ts in np.linspace(tstart, tstart + seg_len * dt, nhalf, endpoint=False): results = LP.process_swept_Langmuir(t_range=(ts, tend), plot_DA=plot_DA, **half_kwargs) if hasattr(LP, 'da'): alldas.append(LP.da) bigda = DA(alldas[0].copyda()) for da in alldas[1:]: bigda.append(da)
exec(pf.utils.process_cmd_line_args()) # prepare an empty lists for data - lists are easy to append to ne_profile = [] shot = [] t_mid = [] dev = pf.getDevice(device_name) # open the device (choose the experiment) for shot_number in shot_range: try: d = dev.acq.getdata(shot_number, ne_set) if time_range != None: d.reduce_time(time_range) sections = d.segment(n_samples, overlap) print(d.history, len(sections)) for ss,t_seg in enumerate(sections): ne_profile.append(np.average(t_seg.signal,axis=1)) t_mid.append(np.average(t_seg.timebase)) shot.append(shot_number) except exception as reason: print 'Error {e} on shot {s}'.format(e=reason, s=shot) # make the dictionary of arrays and put it in a DA object myDA = DA(dict(shot=shot, ne_profile=ne_profile, t_mid=t_mid)) myDA.save('ne_profile')
""" extract selected shots from a pytables hdf5 file Algorithm should be applicable for any 'on disk' variable format """ import pyfusion from pyfusion.data.DA_datamining import DA import numpy as np dd = DA('$DA/DAHJ59k.npz', load=1).copyda() shots = dd['shot'] ushots = np.unique(shots) np.random.seed(0) # ensure repeatability = # chose 10 randomly myshots = ushots[np.random.uniform(len(ushots), size=10).astype(int)] inds = [] for shot in myshots: inds.extend(np.where(shots == shot)[0]) # 1,2,3,4,6,7,8, #diff 1,1,1,2,1,1,1 #ones 1,1,1,0,1,1,1 #ups 3 #downs 2, # sort in the order of the table - not necessarily shot order inds = np.sort(inds) ones = (np.diff(inds) == 1).astype(int)
nl = '\n' ofilename = None LPfile = 'LP/20160224_25_L53.npz' if len(sys.argv) > 1 and sys.argv[1][0] != '-': LPfile = sys.argv[1] if ofilename is None: path, nameext = os.path.split(LPfile) (name, ext) = os.path.splitext(nameext) ofilename = os.path.join(path, name + '.csv') jfilename = os.path.join(path, name + '.json') try: from pyfusion.data.DA_datamining import DA da = DA(LPfile) dat = da.da masked = da.masked except ImportError: raise datnpz = np.load(LPfile) dat = {} for k in datnpz: dat[k] = datnpz[k].tolist() masked = dat info = dat.get('info') params = info['params'] channels = info['channels']
print('Only {n} files found for {fl}'.format(fl=file_list, n=len(file_list))) (ds_list, comment_list) = read_text_pyfusion(file_list, debug=debug, exception=exception, target=target) if len(ds_list) == 0: raise LookupError('no valid files found in the {n} files in {f}' .format(f=file_list,n=len(file_list))) if isinstance(file_list_in, str): comment_list = [file_list_in] + comment_list if append_old_method: ds_list.append(dd) comment_list.extend(dd['info']['comment']) if append: dd = merge_ds(ds_list, comment_list, old_dd=dd) else: dd = merge_ds(ds_list, comment_list) if save_filename != "None": from pyfusion.data.DA_datamining import DA DAtest=DA(dd) DAtest.info() DAtest.save(save_filename)
from pyfusion.data.DA_datamining import DA from matplotlib import pyplot as plt da9 = DA('LP20160309_42_L57_errest_cov_lpf=9.npz') da99 = DA('LP20160309_42_L57_errest_cov_lpf=99.npz') axs = da9.plot('Te', sharey='all') for i in range(len(da99['info']['channels'])): axs[i].plot(da99['t_mid'], da99.masked['Te'][:, i]) plt.ylim(0, 60) plt.show(0)
cmap = cm.jet # see also cm.gray_r etc tm = localtime() hdr = str('PF2_{yy:02d}{mm:02d}{dd:02d}_'.format(yy=tm.tm_year - 2000, mm=tm.tm_mon, dd=tm.tm_mday, hh=tm.tm_hour)) # a local da wins, if not there then try DA_file try: da except: print('No da, check if there is a file') if DA_file is not None: try: da = DA(DA_file, load=1) except: print('DA_file {df} not found'.format(df=DA_file)) flucfiles = '{hdr}*{sht}*'.format(sht=sht, hdr=hdr) if not sht in da.da['shot']: print('shot {sht} not found, highest in {n} is {h}'.format( sht=sht, n=da.name, h=np.max(da.da['shot']))), # look for the flucfile remotely print(' acessing remote - may hang if sshfs') if len(glob('/h1svr2/tmp/' + flucfiles)) > 0: flucfiles = '/h1svr2/tmp/' + flucfiles remerge = 1 # print('using remote files') else: reprocess = 1
# work through each time segment, extracting the average density during that time for seg in sections: ne_profile.append(np.average(seg.signal,axis=1)) # axis=1 -> average over time, not channel t_mid.append(np.average(seg.timebase)) shot.append(shot_number) except Exception as reason: bads.append([shot_number, reason]) msg = 'skipping shot {s}' if verbose>0: msg += ' because\n {r}' print(msg.format(s=shot_number, r=reason)) # store the data in a DA (Dictionary of Arrays) object, which is like a DataFrame in R or python panda #myDA = pf.data.DA_datamining.Masked_DA( # DAordict = dict(shot=shot, ne_profile=np.array(ne_profile), t_mid=t_mid), valid_keys=['ne_profile'], mask=0) myDA = DA(dict(shot=shot, ne_profile=np.array(ne_profile), t_mid=t_mid)) myDA.masked = Masked_DA(valid_keys=['ne_profile'], baseDA=myDA) myDA.da['mask'] = np.ones(shape=np.shape(myDA[myDA.masked.valid_keys[0]])).astype(np.uint8) channels = [ch.name.split(':')[-1] for ch in seg.channels] myDA.infodict.update(dict(channels = channels)) for (c, ch) in enumerate(channels): if np.any([ex in ch for ex in exclude]): # if any of exclude in that channel myDA.da['mask'][:,c] = 0 myDA.save('/tmp/density_scan') # the next step - write to arff # myDA.write_arff('ne_profile.arff',['ne_profile']) import matplotlib.pyplot as plt def pl(array, comment=None,**kwargs): plt.figure(num=comment) # coment written to window title plt.plot(array, **kwargs)
# The Initial paste #_PYFUSION_TEST_@@SCRIPT %run pyfusion/examples/cluster_info.py figure();co.plot_clusters_phase_lines() figure();co.plot_kh_freq_all_clusters();ylim(0,80) from pyfusion.data.DA_datamining import DA, report_mem, append_to_DA_file DA_7677=DA('DA_7677.npz') DA_7677.extract(locals()) sht=76670 #86514 # 76670 # 672, 616 # typically paste from here down thr = 1.5 scl=1500 clim=None figure() boydsdata=1 if boydsdata: ph = -phases else: ph = phases w15=where((ml[15].one_rms(ph)<thr) & (shot==sht))[0];len(w15) w4=where((ml[4].one_rms(ph)<thr) & (shot==sht))[0];len(w4) w=union1d(w4,w15) if len(w)>0: scatter(t_mid[w],freq[w],scl*amp[w],c='b',label='n=5/m=4') w6=where((ml[6].one_rms(ph)<thr) & (shot==sht))[0];len(w6) w1=where((ml[1].one_rms(ph)<thr) & (shot==sht))[0];len(w1) w10=where((ml[10].one_rms(ph)<thr) & (shot==sht))[0];len(w10) w=union1d(w1,w10)
import numpy as np from pyfusion.data.DA_datamining import Masked_DA, DA from matplotlib import pyplot as plt dal = DA ('LP/LP20170920_7_UTDU_2k8.npz') I0arr = dal['I0'] wbad = np.where((I0arr > .02) | (I0arr < 0))[0] rejected = float(len(wbad))/len(I0arr.flatten()) print('{n} rejected ({pc:.0f}%)'.format(n=wbad, pc=rejected*100)) I0arr[wbad] = np.nan IUpper = np.nanmean(I0arr, axis=0) plt.step(range(len(IUpper)), IUpper) plt.show(0)
files = ['LP/LP20160309_52_L53_2k2small.npz'] if len(sys.argv) > 1: arg1 = sys.argv[1] if arg1.startswith('@'): files = np.loadtxt(arg1[1:], dtype='S') else: files = [arg1] else: print('using existing list <files> if run with -i') tmax = float(sys.argv[2]) if len(sys.argv) > 2 else None for n, f in enumerate(files): # (bads): da = DA(f) shot = [da['date'][0], da['progId'][0]] diag = 'W7X_L53_LP0107' if '53' in da['info']['channels'][ 0] else 'W7X_L57_LP0107' LP = Langmuir_data(shot, diag, 'W7X_L5UALL') kwargs = dict(threshchan=0, initial_TeVfI0={ 'I0': None, 'Te': 30, 'Vp': 5 }, fit_params=(dict(alg='amoeba', lpf=21)), dtseg=2000, overlap=2) old_t_range = da.infodict['params']['t_range'] if old_t_range is None:
clearfigs = 1 # set to 0 to overlay (not great) """ exec(_var_defaults) from pyfusion.utils import process_cmd_line_args exec(process_cmd_line_args()) try: da if oldDAfilename != DAfilename: 1 / 0 # create an error to force reload print('Using old data') except: print('loading {f}'.format(f=DAfilename)) da = DA(DAfilename) oldDAfilename = DAfilename da.extract(locals(), 'shot,phases,beta,freq,frlow,frhigh,t_mid,amp,a12') print('loading {f}'.format(f=clusterfile)) x = np.load(clusterfile) for k in x.keys(): exec("{v}=x['{k}']".format(v=k, k=k)) start_mem = report_mem(msg='cluster_phases') w5 = np.where((dists(subset[clinds[cl][0]], phases[:, sel]) < d_big) & (bw(freq, frlow, frhigh)) & (shot == shot))[0] print(len(w5), len(unique(shot[w5]))) ph5 = phases[w5] wc = np.where(dists(subset[clinds[cl][0]], ph5[:, sel]) < d_med)[0] wcc = np.where(dists(subset[clinds[cl][0]], ph5[:, sel]) < d_sml)[0]
#run pyfusion/examples/small_65.py import numpy as np import pylab as pl import pyfusion from pyfusion.data.DA_datamining import DA from pyfusion.data.convenience import whr, btw, his, decimate size_scale=30 cind = 0 colorset=('b,g,r,c,m,y,k,orange,purple,lightgreen,gray'.split(',')) # to be rotated DA65MPH=DA('DA65MP2010HMPno612b5_M_N_fmax.npz',load=1) DA65MPH.extract(locals()) pyfusion.config.set('Plots',"FT_Axis","[0.5,4,0,80000]") """ run -i pyfusion/examples/plot_specgram.py shot_number=65139 channel_number=1 NFFT=1024 pl.set_cmap(cm.gray_r) pl.clim(-60,-0) """ sc_kw=dict(edgecolor='k',linewidth = 0.3) for n in (-1,0,1): for m in (-2, -1,1,2): w =np.where((N==n) & (M==m) & (_binary_svs < 99) & btw(freq,frlow,frhigh))[0] if len(w) != 0: col = colorset[cind] pl.scatter(t_mid[w], freq[w], size_scale*amp[w], color=col, label='m,n=~{m},{n}'.format(m=m, n=n),**sc_kw) cind += 1 w=np.where((_binary_svs < 99) & btw(freq,frlow,frhigh) & btw(MM, 0,130) & (NN== -4))[0] col = colorset[cind] ; cind+=1
""" Script to plot probe signals in time for the Marfe shot 52 for Uwe, IAEA """ import matplotlib.pyplot as plt from pyfusion.data.DA_datamining import Masked_DA, DA from pyfusion.data.signal_processing import smooth_n plt.rc('font', size=18, weight='normal') plt.rc('lines', lw=2) DA523=DA('LP/to_uwe/LP20160309_52_L53_2k2.npz') DA513=DA('LP/to_uwe/LP20160309_51_L53_2k2.npz') DA527=DA('LP/to_uwe/LP20160309_52_L57_2k2.npz') DA517=DA('LP/to_uwe/LP20160309_51_L57_2k2.npz') DA7 = DA527 fig, axs = plt.subplots(2, 1, sharey='none', sharex='all') axn, axT = axs for ch in [1,4,5,6,7]: axT.plot(DA7['t'],DA7['Te'][:,ch], label=DA7['info']['channels'][ch],lw=2) axn.plot(DA7['t'],DA7['ne18'][:,ch], label=DA7['info']['channels'][ch],lw=2) axn.legend() axT.legend() axT.set_ylabel('Te (ev)') axn.set_ylabel('ne/10^18') axT.set_ylim(0,40) axn.set_ylim(0,16) axn.set_xlim(0.40, 0.65)
from pyfusion.data.DA_datamining import DA import numpy as np import matplotlib.pyplot as plt plt.figure('Example 4') # Load pre-stored data from 30 shots into a dictionary of arrays (DA) DA766 = DA('H1_766.npz', load=1) # Extract all the data into local variables, such as freq, t_mid, phases[0] DA766.extract(locals()) # Look at one set of instances - probe phase differences - plotting frequency # vs time, size indicates amplitude plt.scatter(t_mid, freq, amp, edgecolor='lightblue') # Find large amplitude, 'rotating' modes (see text). [0] is required by wb = np.where((amp > 0.05) & (a12 > 0.7))[0] # the numpy where() function # Overplot with larger symbols proportional to amplitude, coloured by a12 plt.scatter(t_mid[wb], freq[wb], 300 * amp[wb], a12[wb]) plt.xlabel(r'$k_H$') plt.ylabel(r'${\rm frequency/V_{Alfv\'en}}$', fontdict={'fontsize': 'large'}) plt.xlim(0, 0.042) plt.ylim(0, 150) plt.show()
print('Selecting before clustering ') phases = phases[sel, :] # WAS phases[:,sel] - how did it ever work? for k in misc.keys(): misc.update({k: misc[k][sel]}) # this was missing too fo = clust.feature_object(phases, misc) co = fo.cluster(method='EM_VMM', n_clusters=n_clusters, n_iterations=n_iterations, start='k_means', n_cpus=n_cpus, number_of_starts=number_of_starts) co.plot_clusters_phase_lines() # show clusters # extract the meta data corresponding to the instances selected DA(DAfilename, load=1).extract(locals(), inds=co.feature_obj.misc_data_dict['serial']) while cl >= 0: cl = int(input('\n cluster number to view, -1 or ^C to quit? ')) w = co.members(cl) pl.figure('cluster {cl}'.format(cl=cl)) pl.scatter(t_mid[w], freq[w], 500 * amp[w]) pl.ylim(0, max(freq) / 2) pl.show(0) """ # simplest possible version run pyfusion/examples/medium_300.py import pyfusion.clustering as clust phases=DA300.da.pop('phases') fo = clust.feature_object(phases, DA300.da)
nerr_bin_edges = [0, .2, .4, .7, 1, 2, 5] nerr_scale = 0.5 areas = [4, 2, .5, .2, .1, .05] areas = [3, 2, 1, .5, .2, .1] cols = ['r', 'g', 'b'] lw_scale = 1 marker = '|' intrp = 1 tstart = 1.0 tend = 1.05 lw = 0.5 period = 20e-6 exec(process_cmd_line_args()) da = DA(dafile, load=1, verbose=verbose) tm, = da.extract(varnames='t_mid') idt = np.argsort(da['t_mid']) tm, = da.extract(varnames='t_mid', inds=idt) area_scale = np.sqrt(len(tm)) / 10 if area_scale is None else area_scale nerr_bin_edges = nerr_scale * np.array(nerr_bin_edges) fig, axs = plt.subplots(3, 1, sharex='all', squeeze=True) for k, key in enumerate('Te:I0:Vf'.split(':')): val, err = da.extract(varnames='{k},e{k}'.format(k=key), inds=idt) wgood = np.where(~np.isnan(val) & ~np.isnan(err))[0] val = val[wgood] err = err[wgood] tmg = tm[wgood] # needs to be unsigned, also Vf is a problem, how to know average
version=0 # 0 fixed ctrs, 1 evolves """ exec(_var_defaults) #exec(';'.join(_var_defaults.split())) from pyfusion.utils import process_cmd_line_args exec(process_cmd_line_args()) # Slow: around 1000 13 element lists/second - probably could eliminate # more than one at a time. However it may make more sense to look for # clusters - this will work if there are two types of shapes. from pyfusion.data.DA_datamining import DA, report_mem, append_to_DA_file if DAfile != '': da = DA(DAfile, load=1) else: print('try for a da in locals()') try: da except NameError as reason: print(' no da - did you use run -i?') wh = da['indx'] # all of them #wh = np.where((btw(da['freq'],2,4)) & (da['amp']>0.012) )[0] wh = np.where(da['shot'] == 180904035)[0] phs = da['phases'][wh].tolist() inds = da['indx'][wh].tolist() ctrs = np.mean(modtwopi(phs, 0), 0) num = len(ctrs)
else: filename = sys.argv[1] if os.path.split(filename)[1] == filename: print(os.getcwd(), end='') print(filename) block_me = not hasattr(sys, 'ps1') # from_emacs was called block_me from_emacs = not hasattr(sys, 'ps1') if from_emacs: print('from emacs') os.environ['PYFUSION_VERBOSE'] = '-1' # keep clutter down import pyfusion pyfusion.VERBOSE = -1 # why doesn't the environ above work? da = DA(filename, load=1, verbose=pyfusion.VERBOSE) if 'info' in da: pprint.pprint(da['info']) if hasattr(da, 'info'): da.info() sys.stdout.flush( ) # works, but not effective in emacs, at least when ! is used. if len(sys.argv) > 2: key = sys.argv[2] elif 'ne18' in da: typ = 'LP' key = 'ne18' elif 'phases' in da: typ = 'FS' key = 'phases'
dt = (probedata.utc[0] - t0_utc) / 1e9 # gas_g=[3.1, 3.1, 3.7, 3.7, 3.1, 3.1, 3.7, 3.7, 3.1, 3.1, 3.7, 3.7, 3.1, 3.1, 3.7, 3.7, 3.1, 3.1, 3.7, 3.7, 3.1, 3.1] # time_g=[0, 0.099, 0.1, 0.155, 0.156, 0.162, 0.163, 0.196, 0.197, 0.211, 0.212, 0.24, 0.241, 0.266, 0.267, 0.290, 0.291, 0.327, 0.328, 0.341, 0.342, 0.8] # Corrected using Maciej's email time_g = np.array([ 0, 0.099, 0.1, 0.149, 0.150, 0.160, 0.161, 0.191, 0.191, 0.210, 0.211, 0.230, 0.231, 0.260, 0.261, 0.280, 0.281, 0.320, 0.321, 0.340, 0.341, 0.8 ]) gas_g = np.array([ 3.1, 3.1, 3.7, 3.7, 3.1, 3.1, 3.7, 3.7, 3.1, 3.1, 3.7, 3.7, 3.1, 3.1, 3.7, 3.7, 3.1, 3.1, 3.7, 3.7, 3.1, 3.1 ]) # LP5342.process_swept_Langmuir(t_range=[0.9,1.4],dtseg=.002,initial_TeVfI0=dict(Te=20,Vf=0,I0=None),filename='20160309_42_L532_short') #da532=DA('20160309_42_L532_short.npz') da532 = DA('LP/LP20160309_42_L53_2k2.npz') #da532=DA('LP/LP20160309_52_L53_2k2.npz') #da532=DA('LP/LP20160309_44_L53.npz') da572 = DA('LP/LP20160309_42_L57_2k2.npz') #da572=DA('LP/LP20160309_52_L57_2k2.npz') # offsets for medium text, not including dot size, second element is the probe number to avoid voffs = dict(LP17=[.002, 20], LP10=[.002, 6], LP09=[0.001, 5], LP06=[-.001, 1], LP12=[.002, 14]) fig, (axcorr, axmap) = plt.subplots(2, 1) da = da532 chans = np.array(da['info']['channels'])