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]

sl_red = compact_str(np.unique(shot[w5[wcc]]))
sl_green = compact_str(np.unique(shot[w5[wc]]))
Ejemplo n.º 2
0
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
    absavg = np.average(np.abs(val), weights=1 / (0.1 + err))
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
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'
Ejemplo n.º 5
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
Ejemplo n.º 6
0
""" A 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, bw, btw, decimate, his, broaden

_var_default = """
DAfilename='300_small.npz'
"""
exec(_var_default)

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)
Ejemplo n.º 7
0
    mcmd = '''python pyfusion/examples/merge_text_pyfusion.py 'file_list=np.sort(glob("{tfile}"))' exception=Exception save_filename={DA_file}'''.format(
        sht=sht, tfile=flucfiles, DA_file=tmpDA_file)
    sub_pipe = subprocess.Popen(mcmd,
                                shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
    print('merging')
    (resp, err) = sub_pipe.communicate()
    print(resp, err)
    newda = DA(tmpDA_file, load=1)
    da.append(newda.da)

    print('{n} shots in {da}'.format(n=len(np.unique(da.da['shot'])),
                                     da=da.name))

da.extract(locals())

if boydsdata:
    ph = -phases
else:
    ph = phases

ws = np.where(sht == shot)[0]
ph = ph[ws]

print('where on {0:,}x{1} phases '.format(*np.shape(ph))),
w15 = where((ml[15].one_rms(ph) < thr))[0]
len(w15)
w4 = where((ml[4].one_rms(ph) < thr))[0]
len(w4)
w = np.union1d(w4, w15)
Ejemplo n.º 8
0
""" 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_defaults="""
DAfilename='DA65MP2010HMPno612b5_M_N_fmax.npz'
"""

exec(_var_defaults)

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)

Ejemplo n.º 9
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')   # symbol '.', colour cyan - we see the values are in the range sqrt beta
plt.plot(k_h[wb], freq[wb]*1e3/V_A[wb],'ob',ms=12) # selecting large amplitude as above, we see beginnings of fig 5
plt.xlabel(r'$k_H$'); plt.ylabel(r'${\rm frequency/V_{Alfven}}$',fontdict={'fontsize':'large'})
plt.show()
# Note - there will be a warning "invalid value encountered in sqrt" due to noise in n_e
# This could be eliminated by selecting using np.where(n_e >= 0)[0]
Ejemplo n.º 10
0
is under development - contact the authors for the latest copy.
 
Takes about 1 minute
Python3 produces a different clustering to Python2 - not sure why?
"""


from sklearn import mixture
from pyfusion.data.DA_datamining import DA, report_mem
import numpy as np
import matplotlib.pyplot as plt
# approx size used in pub  figure(figsize=((11.9,7.6)))
plt.figure('Example 6 - Figure 8')

DA76 = DA('H1_766.npz',load=1)
DA76.extract(locals())

np.random.seed(0)       # ensure the same result (useful for examples)
gmm = mixture.GMM(n_components=16, covariance_type='spherical')
m16 = gmm.fit(phases)   # fit 16 Gaussians
cids = m16.predict(phases)      # assign each point to a cluster id

for c in [7, 9]:    # show the two most interesting clusters in freq vs k_h
                    # select cluster members of sufficient amplitude, a12, and after 5ms
    w = np.where((c == cids) & (amp > 0.08) & (a12 > 0.5) & (t_mid > 0.005))[0]
    # add artificial noise to the k_h value to show points 'hidden' under others
    dither = .008 * np.random.random(len(w))
    # colored by cluster
    plt.scatter(k_h[w] + dither, np.sqrt(ne_1[w])*freq[w], 700*amp[w], 'bgrcmkycmrgrcmykc'[c]) 

plt.ylim(0, 60); plt.xlim(0.2, 1)
Ejemplo n.º 11
0
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]

sl_red = compact_str(np.unique(shot[w5[wcc]]))
Ejemplo n.º 12
0
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)

# get the variables into local scope - so they can be accessed directly
myDA.extract(locals())        

# the ne profiles are in an Nx15 array, where N is the numer of channels
pl(ne_profile[40,:],'one profile')

# plot a sequence of profiles, showing every fifth 
for prof in ne_profile[10:20:5]:
    pl(prof)

# plot all profiles by using the transpose operator to get profiles
pl(ne_profile.T, 'all profiles',color='b',linewidth=.01)

# without the transpose, you will get the time variation for the data
pl(ne_profile, 'time variation, all channels',color='b',linewidth=.3)

# see all profiles as a false colour image
Ejemplo n.º 13
0
""" 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)
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
# 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)
Ejemplo n.º 16
0
# The Initial paste
%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)
if len(w)>0: scatter(t_mid[w],freq[w],scl*amp[w],c='g',label='n=4/m=3')
Ejemplo n.º 17
0
if remerge:  #  
    #tmpDA_file holds the merged flucstrucs until they can be merged into da
    tmpDA_file = '/tmp/DA_{sht}.npz'.format(sht=sht)
    # flucfiles is the wildcard name for the files processed on h1svr or locally
    mcmd = '''python pyfusion/examples/merge_text_pyfusion.py 'file_list=np.sort(glob("{tfile}"))' exception=Exception save_filename={DA_file}'''.format(sht=sht,tfile=flucfiles,DA_file=tmpDA_file)
    sub_pipe = subprocess.Popen(mcmd,  shell=True, stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
    print('merging') 
    (resp,err) = sub_pipe.communicate()
    print(resp,err)
    newda = DA(tmpDA_file,load=1)
    da.append(newda.da)
    
    print('{n} shots in {da}'.format(n=len(np.unique(da.da['shot'])),da=da.name))

da.extract(locals())

if boydsdata:
    ph = -phases
else:
    ph = phases

ws = np.where(sht == shot)[0]
ph = ph[ws]

print('where on {0:,}x{1} phases '.format(*np.shape(ph))),
w15=where((ml[15].one_rms(ph)<thr))[0];len(w15)
w4=where((ml[4].one_rms(ph)<thr))[0];len(w4)
w=np.union1d(w4,w15)
colls = []
if len(w)>0: colls.append(pl.scatter(t_mid[w],freq[w],scl*amp[w],c='b',label='n=5/m=4'))