def test_detrend(): plt.close('all') print() print('>>>>>>>>>>>>>>>>>>> Test detrend <<<<<<<<<<<<<<<<<<<<<<<<') flap.delete_data_object('*') print("**** Generating 8 sine signals with variable frequency.") d = flap.get_data('TESTDATA', name='TEST-1-*', object_name='TEST-1', options={ 'Signal': 'Sin', 'Freq': [1e3, 5E3], 'Length': 0.005 }) print("**** Detrending in 2 intervals with second order poly fit.") plt.figure() flap.plot('TEST-1', axes='Time') flap.detrend('TEST-1', intervals={ 'Time': flap.Intervals(0.001, 0.0015, step=0.003, number=2) }, options={'Trend': ['Poly', 2]}, output_name='TEST-1_detrend') flap.plot('TEST-1_detrend', axes='Time')
def test_storage(signals='TEST-*', timerange=[0, 0.001]): print() print( "\n>>>>>>>>>>>>>>>>>>> Test storage operations on test data <<<<<<<<<<<<<<<<<<<<<<<<" ) flap.delete_data_object('*', exp_id='*') if (type(signals) is list): s = "[" for sig in signals: s += sig + " " s += "]" else: s = signals print("**** Reading signal " + s + " for time range [" + str(timerange[0]) + '-' + str(timerange[1]) + '] with no_data=True') d = flap.get_data('TESTDATA', name=signals, options={'Scaling': 'Volt'}, object_name='TESTDATA', coordinates={'Time': timerange}, no_data=True) print("**** Storage contents") flap.list_data_objects() print() print("**** Reading the same with data") d = flap.get_data('TESTDATA', name=signals, options={'Scaling': 'Volt'}, object_name='TESTDATA', coordinates={'Time': timerange}) print("**** Storage contents") flap.list_data_objects()
def test_cpsd(): plt.close('all') print() print( '>>>>>>>>>>>>>>>>>>> Test cpsd (Cross Spectral Power Density) <<<<<<<<<<<<<<<<<<<<<<<<' ) flap.delete_data_object('*') print("**** Generating 8 random data, 1 million points each.") d = flap.get_data('TESTDATA', name='TEST-1-[1-8]', options={ 'Signal': 'Random', 'Length': 1 }, object_name='TESTDATA') print("**** Calculating all cpsd") flap.cpsd('TESTDATA', options={ 'Norm': True, 'Interval': 50, 'Log': True, 'Res': 10, 'Range': [100, 1e5] }, output_name='TESTDATA_cpsd') flap.abs_value('TESTDATA_cpsd', output_name='TESTDATA_cpsd_abs') print( "**** Plotting coherency between channels 1-2 and its significance level." ) plt.figure() flap.plot('TESTDATA_cpsd_abs', axes='Frequency', slicing={ 'Row (Ref)': 1, 'Row': 2 }, options={ 'Log y': True, 'Log x': True, 'Error': False }) flap.error_value('TESTDATA_cpsd_abs').plot(slicing={ 'Row (Ref)': 1, 'Row': 2 }) plt.figure() print( "**** Plotting mean coherence in 1e4-1e5 frequency range as a function of row index." ) flap.slice_data('TESTDATA_cpsd_abs', slicing={ 'Frequency': flap.Intervals(1e4, 1e5) }, summing={ 'Frequency': 'Mean' }).plot(axes='Row (Ref)', options={'Y sep': 1.5})
def test_mdsplus(): plt.close('all') print("**** Reading an explicit MDS signal.") flap.delete_data_object('*') try: # Explicit MDSPlus reference #d=flap.get_data('NSTX_MDSPlus', # name='IP', # exp_id=141398, # object_name='TEST_MDS' # ) d = flap.get_data('NSTX_MDSPlus', name='\EFIT01::\PSIRZ', exp_id=141399, object_name='TEST_MDS') #print(d.coordinate('Time')[0].shape) #print(d.coordinate('Device R')[0].shape) #print(d.coordinate('Device Z')[0].shape) #print(d.coordinate('Dimension 1')[0]) print(d.data) #print(d.data.shape) except Exception as e: raise e flap.plot('TEST_MDS', plot_type='animation', axes=['Device R', 'Device z', 'Time'], options={ 'Z range': [0, 0.05], 'Wait': 0.0, 'Clear': False }) #flap.plot('TEST_MDS',plot_type='contour',axes=['Time','Dimension 1'],options={}) #flap.plot('TEST_MDS') flap.list_data_objects()
def test_resample(): plt.close('all') print() print(">>>>>>>>>>>>> Test signal resampling (interpolation) <<<<<<<<<<<") flap.delete_data_object('*') print( "**** Generating two test signals with different sampling frequency.") flap.get_data('TESTDATA', name='TEST-1-1', options={ 'Scaling': 'Volt', 'Frequency': 1e3, 'Samplerate': 1e6 }, object_name='TEST-1MHz', coordinates={'Time': [0, 0.001]}) flap.get_data('TESTDATA', name='TEST-1-1', options={ 'Scaling': 'Volt', 'Frequency': 1.5e3, 'Samplerate': 3e6 }, object_name='TEST-3MHz', coordinates={'Time': [0, 0.001]}) print("\n***** Resampling from lower to higher frequency.") plt.figure() flap.plot('TEST-1MHz', axes='Time', plot_options={'marker': 'o'}) flap.plot('TEST-3MHz', plot_options={'marker': 'o'}) flap.slice_data('TEST-1MHz', slicing={'Time': flap.get_data_object('TEST-3MHz')}, options={'Interpol': 'Linear'}, output_name='TEST-1MHz_resample') flap.plot('TEST-1MHz_resample', plot_options={'marker': 'x'}) print("\n***** Resampling from higher to lower frequency.") plt.figure() flap.plot('TEST-1MHz', axes='Time', plot_options={'marker': 'o'}) flap.plot('TEST-3MHz', plot_options={'marker': 'o'}) flap.slice_data('TEST-3MHz', slicing={'Time': flap.get_data_object('TEST-1MHz')}, options={'Interpol': 'Linear'}, output_name='TEST-3MHz_resample') flap.plot('TEST-3MHz_resample', plot_options={'marker': 'x'}) print("\n***** Cutting parts.") plt.figure() flap.slice_data( 'TEST-1MHz', slicing={'Time': flap.Intervals([1e-4, 5e-4], [2e-4, 7e-4])}, options={'Slice': 'Simple'}, output_name='TEST-1MHz_parts') flap.plot('TEST-1MHz_parts', axes='Time', plot_options={'marker': 'o'}) flap.list_data_objects()
def test_plot_multi_xy(): print() print("\n>>>>>>>>>>>>>>>>>>> Test plot multi x-y --------") plt.close('all') plt.figure() flap.delete_data_object('*') d = flap.get_data('TESTDATA', name='TEST-1-*', options={'Scaling': 'Volt'}, object_name='TEST') print("**** Storage contents") flap.list_data_objects() flap.plot('TEST', axes='Time', options={'All points': False, 'Y sep': 4})
def test_mdsplus_efit(): plt.close('all') print("**** Reading all EFIT MDS signals.") flap.delete_data_object('*') try: d = flap_mdsplus.FlapEFITObject() d.get_data('NSTX_MDSPlus', exp_id=141399) print(d) except Exception as e: raise e # flap.plot('TEST_MDS',plot_type='animation',axes=['Device R','Device Z','Time'],options={'Z range':[0,512],'Wait':0.0,'Clear':False}) flap.list_data_objects()
def test_coordinates(): print() print( "\n>>>>>>>>>>>>>>>>>>> Testing adding coordinates <<<<<<<<<<<<<<<<<<<<<<<<" ) flap.delete_data_object('*', exp_id='*') print("**** Reading signal TEST-2-5 for time range [0,0.1]") d = flap.get_data('TESTDATA', name='TEST-2-5', options={'Scaling': 'Volt'}, object_name='TEST-1-1', coordinates={'Time': [0, 0.1]}) print("**** Storage contents") flap.list_data_objects() print() print("**** Adding Device x coordinate") flap.add_coordinate('TEST-1-1', exp_id='*', coordinates=['Device x', 'Device z', 'Device y']) print("**** Storage contents") flap.list_data_objects() print() print("**** Reading all test signals for time range [0,0.001]") d = flap.get_data('TESTDATA', name='TEST-*', options={'Scaling': 'Volt'}, object_name='TESTDATA', coordinates={'Time': [0, 0.001]}) print("**** Storage contents") flap.list_data_objects() print() print("**** Adding Device x coordinate") flap.add_coordinate('TESTDATA', exp_id='*', coordinates=['Device x', 'Device z', 'Device y']) print("**** Storage contents") flap.list_data_objects() print( "**** Getting the time coordinate. The result will be a 3D object with 1 element in all dimensions except along the time." ) t = flap.get_data_object_ref('TESTDATA').coordinate( 'Time', options={'Chang': True})[0].flatten() plt.figure() plt.plot(t) plt.xlabel('Index') plt.ylabel('Time')
def test_testdata(): print() print( "\n>>>>>>>>>>>>>>>>>>> Test TESTDATA data source <<<<<<<<<<<<<<<<<<<<<<<<" ) flap.delete_data_object('*', exp_id='*') print( "**** Generating 0.01 s long test data signals on [4,5] matrix with fixed frequency changing from channel to channel" ) d = flap.get_data('TESTDATA', name='TEST-*-*', options={ 'Scaling': 'Volt', 'Frequency': [1e3, 1e4], 'Length': 1e-2, 'Row number': 4, 'Column number': 5 }, object_name='TESTDATA') plt.figure() print("**** Plotting row 2") d.plot(slicing={'Row': 2}, axes=['Time']) print( "**** Generating 0.01 s long test data signal with linearly changing frequency: 10-100kHz" ) f = np.linspace(1e4, 1e5, num=11) coord = flap.Coordinate(name='Time', start=0.0, step=0.001, mode=flap.CoordinateMode(equidistant=True), dimension_list=[0]) f_obj = flap.DataObject(data_array=f, coordinates=[coord], data_unit=flap.Unit(name='Frequency', unit='Hz')) flap.list_data_objects(f_obj) d = flap.get_data('TESTDATA', name='TEST-1-1', options={ 'Scaling': 'Volt', 'Frequency': f_obj, 'Length': 0.01, 'Row number': 1, 'Column number': 1 }, object_name='TESTDATA') plt.figure() d.plot(axes='Time', options={'All': True})
def test_select_multislice(): plt.close('all') print() print( '>>>>>>>>>>>>>>>>>>> Test select on maxima and multi slice <<<<<<<<<<<<<<<<<<<<<<<<' ) flap.delete_data_object('*') d = flap.get_data('TESTDATA', name='TEST-1-1', object_name='TEST-1-1', options={'Length': 0.050}) print( "**** Selecting 100 microsec long intervals around the maxima of the signal." ) d_int = flap.select_intervals('TEST-1-1', coordinate='Time', options={ 'Select': None, 'Length': 0.0001, 'Event': { 'Type': 'Max-weight', 'Threshold': 1, 'Thr-type': 'Sigma' } }, plot_options={'All points': True}, output_name='SELECT') flap.list_data_objects() d_int.plot(axes=['__Data__', 0], plot_type='scatter', options={'Force': True}) if (d_int is not None): print("**** Overplotting the signals in the selected intervals.") flap.slice_data('TEST-1-1', slicing={'Time': d_int}, output_name='TEST-1-1_sliced') flap.list_data_objects() plt.figure() n_int = d_int.shape[0] for i in range(n_int): flap.plot('TEST-1-1_sliced', slicing={'Interval(Time)': i}, axes='Rel. Time in int(Time)')
def test_gpi_trace(exp_id=140620): flap.delete_data_object('*') print("\n------- test data read with NSTX GPI data --------") d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI') print("**** Storage contents") flap.list_data_objects() plt.close('all') #print("**** Filtering GPI") #d_filter=flap.filter_data('GPI',output_name='GPI_filt',coordinate='Time', # options={'Type':'Highpass','f_low':1e2/1e3,'Design':'Chebyshev II'}) #Data is in milliseconds print("**** Plotting filtered GPI") #flap.plot('GPI',plot_type='animation', # slicing={'Time':flap.Intervals(550.,580.)}, # axes=['Device R','Device z','Time'], # options={'Z range':[0,512],'Wait':0.0,'Clear':False}) # print(d.time) flap.list_data_objects()
def test_NSTX_GPI_norm_flux_coord(exp_id=141918): flap.delete_data_object('*') print("\n------- test data read with NSTX GPI data and plotting it as a function of normalized flux coordinates --------") d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI') print("**** Storage contents") flap.list_data_objects() plt.close('all') print("**** Adding normalized flux coordinates.") print("--- %s seconds ---" % (time.time() - start_time)) #d.add_coordinate(coordinates='Flux r',exp_id=exp_id) print("--- %s seconds ---" % (time.time() - start_time)) d.add_coordinate(coordinates='Flux theta',exp_id=exp_id) flap.list_data_objects() print('Coordinate was read.') print("--- %s seconds ---" % (time.time() - start_time)) flap.plot('GPI',plot_type='animation',\ slicing={'Time':flap.Intervals(250.,260.)},\ axes=['Flux R','Flux theta','Time'],\ options={'Z range':[0,512],'Wait':0.0,'Clear':False}) print("--- %s seconds ---" % (time.time() - start_time))
def test_simple_slice(): print() print("\n>>>>>>>>>>>>>>>>>>> Test simple slice <<<<<<<<<<<<<<<<<<<<<<<<") flap.delete_data_object('*') print("**** Reading all test signals for time range [0,0.001]") d = flap.get_data('TESTDATA', name='TEST-*', options={'Scaling': 'Volt'}, object_name='TESTDATA', coordinates={'Time': [0, 0.001]}) print("**** Adding Device coordinates") flap.add_coordinate('TESTDATA', coordinates=['Device x', 'Device z', 'Device y']) print("**** Storage contents before slice") flap.list_data_objects() print("**** Slicing with {'Signal name': 'TEST-1-*'}") flap.slice_data('TESTDATA', slicing={'Signal name': 'TEST-1-*'}, output_name='TESTDATA_slice') print("**** Sliced object") flap.list_data_objects(name='TESTDATA_slice')
def test_mdsplus(): plt.close('all') print("**** Reading an explicit MDS signal.") flap.delete_data_object('*') try: # Explicit MDSPlus reference d=flap.get_data('W7X_MDSPlus', name='\QMC::TOP.HARDWARE:ACQ132_168:CHANNELS:INPUT_03', exp_id='20181018.003', object_name='TEST_MDS' ) except Exception as e: raise e print("**** Reading a virtual signal") try: # Explicit MDSPlus reference d1=flap.get_data('W7X_MDSPlus', name='PCI-1-16', exp_id='20180904.027', object_name="PCI-1-16" ) except Exception as e: raise e d1.plot(axes='Time') print("**** Reading multiple complex virtual signals in part of the time.") try: # Explicit MDSPlus reference d2=flap.get_data('W7X_MDSPlus', name=['CR-B','CR-C','CR-D','CR-E'], exp_id='20181018.003', coordinates={'Time':[4,4.1]}, object_name="CR" ) except Exception as e: raise e plt.figure() d2.abs_value().plot(axes='Time',options={'All':True}) flap.list_data_objects()
def test_plot_xy(): print() print("\n>>>>>>>>>>>>>>>>>>> Test plot x-y <<<<<<<<<<<<<<<<<<<<<<<<") plt.close('all') flap.delete_data_object('*') print("**** Reading signal TEST-2-5 for time range [0,0.1]") d = flap.get_data('TESTDATA', name='TEST-2-5', options={'Scaling': 'Volt'}, object_name='TEST-1-1', coordinates={'Time': [0, 0.1]}) print("**** Default plot") plt.figure() d.plot() print("**** Plotting time vs data") plt.figure() d.plot(axes=['__Data__', 'Time']) print("**** Reading all test signals for time range [0,0.001]") d = flap.get_data('TESTDATA', name='TEST-*', options={'Scaling': 'Volt'}, object_name='TESTDATA', coordinates={'Time': [0, 0.001]}) print("**** Adding Device coordinates") flap.add_coordinate('TESTDATA', exp_id='*', coordinates=['Device x', 'Device z', 'Device y']) flap.list_data_objects() print("**** Plotting measurement points in device corodinates.") plt.figure() flap.plot('TESTDATA', axes=['Device x', 'Device z'], plot_type='scatter') print("**** Plotting Device x as a function of Row.") plt.figure() flap.plot( 'TESTDATA', axes=['Row', 'Device x'], plot_type='scatter', )
def test_saveload(): print() print("\n>>>>>>>>>>>>>>>>>>> Test save/load <<<<<<<<<<<<<<<<<<<<<<<<") flap.delete_data_object('*') print("**** Storage contents before save.") flap.list_data_objects() print("**** Saving all storage and deleting storage contents.") flap.save('*', 'flap_save_test.dat') flap.delete_data_object('*') print("**** Storage contents after erasing.") flap.list_data_objects() flap.load('flap_save_test.dat') print("**** Storage contents after loading.") flap.list_data_objects() flap.delete_data_object('*') print("**** Storage contents after erasing.") d = flap.load('flap_save_test.dat', options={'No': True}) print(d) print("**** Storage contents after loading with 'No storage' option.") flap.list_data_objects() flap.delete_data_object('*') flap.save([d, 'test'], 'flap_save_test.dat') d1 = flap.load('flap_save_test.dat', options={'No': True}) print(d1)
def calculate_elm_properties_vs_energy_drop(elm_window=500e-6, elm_duration=100e-6, after_time_threshold=2e-3, averaging='before_after', #The type of averaging for the _avg results ['before_after', 'full', 'elm'] recalc=False, #Recalculate the results and do not load from the pickle file plot=False, #Plot the results with matplotlib plot_error=False, pdf=False, #Save the results into a PDF normalized_structure=True, normalized_velocity=True, subtraction_order=1, dependence_error_threshold=0.5, #Line fitting error dependence relative error threshold. Results under this value are plotted into a text file. test=False, plot_linear_fit=True, plot_energy=False, plot_only_good=False, ): if averaging not in ['before_after', 'full', 'elm']: raise ValueError('Averaging should be one of the following: before_after, full, elm') flap.delete_data_object('*') wd=flap.config.get_all_section('Module NSTX_GPI')['Working directory'] result_filename=wd+'/processed_data/'+'elm_energy_dependence_'+averaging+'_avg' if normalized_structure: result_filename+='_ns' if normalized_velocity: result_filename+='_nv' result_filename+='_so'+str(subtraction_order) scaling_db_file_before=result_filename+'_before.pickle' scaling_db_file_after=result_filename+'_after.pickle' if test: import matplotlib.pyplot as plt plt.figure() if plot_energy: import matplotlib.pyplot as plt plt.figure() energy_pdf=PdfPages(wd+'/plots/all_energy.pdf') if not os.path.exists(scaling_db_file_before) or not os.path.exists(scaling_db_file_after) or recalc: #Load and process the ELM database database_file='/Users/mlampert/work/NSTX_workspace/db/ELM_findings_mlampert_velocity_good.csv' db=pandas.read_csv(database_file, index_col=0) elm_index=list(db.index) #Defining the variables for the calculation for ind_before_after in range(2): energy_results={'Total':[], 'Diamagnetic':[], 'Poloidal':[]} gpi_results_avg={'Velocity ccf':[], 'Velocity str avg':[], 'Velocity str max':[], 'Size avg':[], 'Size max':[], 'Area avg':[], 'Area max':[], 'Elongation avg':[], 'Elongation max':[], 'Angle avg':[], 'Angle max':[], 'Str number':[], } gpi_results_max=copy.deepcopy(gpi_results_avg) if ind_before_after == 0: scaling_db_file = scaling_db_file_before else: scaling_db_file = scaling_db_file_after for index_elm in range(len(elm_index)): elm_time=db.loc[elm_index[index_elm]]['ELM time']/1000. shot=int(db.loc[elm_index[index_elm]]['Shot']) if normalized_velocity: if normalized_structure: str_add='_ns' else: str_add='' filename=flap_nstx.analysis.filename(exp_id=shot, working_directory=wd+'/processed_data', time_range=[elm_time-2e-3,elm_time+2e-3], comment='ccf_velocity_pfit_o'+str(subtraction_order)+'_ct_0.6_fst_0.0'+str_add+'_nv', extension='pickle') else: filename=wd+'/processed_data/'+db.loc[elm_index[index_elm]]['Filename']+'.pickle' #grad.slice_data(slicing=time_slicing) status=db.loc[elm_index[index_elm]]['OK/NOT OK'] if status != 'NO': velocity_results=pickle.load(open(filename, 'rb')) for key in gpi_results_avg: ind_nan=np.isnan(velocity_results[key]) velocity_results[key][ind_nan]=0. time=velocity_results['Time'] elm_time_interval_ind=np.where(np.logical_and(time >= elm_time-elm_duration, time <= elm_time+elm_duration)) nwin=int(elm_window/2.5e-6) n_elm=int(elm_duration/2.5e-6) elm_time=(time[elm_time_interval_ind])[np.argmin(velocity_results['Frame similarity'][elm_time_interval_ind])] elm_time_ind=int(np.argmin(np.abs(time-elm_time))) energy_data=get_nstx_efit_energy_data(exp_id=shot) if plot_energy: plt.cla() for key in ['Poloidal', 'Diamagnetic', 'Total']: plt.plot(energy_data['Time'], energy_data[key], label=key) plt.plot([elm_time,elm_time],[0,1e6]) plt.title('Energy vs time for ' + str(shot)+' @ '+f"{elm_time:.6f}") plt.legend(loc='upper right', shadow=True) plt.xlabel('Time') plt.ylabel('Energy [J]') plt.xlim(elm_time-50e-3,elm_time+50e-3) energy_pdf.savefig() index_time=np.argmin(np.abs(energy_data['Time']-elm_time)) if energy_data['Time'][index_time] <= elm_time and ind_before_after == 1: index_time = index_time + 1 if energy_data['Time'][index_time] > elm_time and ind_before_after == 0: index_time = index_time - 1 energy_status='OK' if (ind_before_after == 0 and (energy_data['Time'][index_time+1]-elm_time > after_time_threshold) or ind_before_after == 1 and (energy_data['Time'][index_time]-elm_time > after_time_threshold) ): energy_status='NO' if (energy_status != 'NO'): for keys in energy_results.keys(): print(keys) energy_results[keys].append(energy_data[keys][index_time]) for key in gpi_results_avg: if averaging == 'before_after': #This separates the before and after times with the before and after Thomson results. if ind_before_after == 0: gpi_results_avg[key].append(np.mean(velocity_results[key][elm_time_ind-nwin:elm_time_ind],axis=0)) elif ind_before_after == 1: gpi_results_avg[key].append(np.mean(velocity_results[key][elm_time_ind+n_elm:elm_time_ind+nwin],axis=0)) elif averaging == 'full': gpi_results_avg[key].append(np.mean(velocity_results[key][elm_time_ind+n_elm:elm_time_ind+nwin],axis=0)) elif averaging == 'elm': gpi_results_avg[key].append(velocity_results[key][elm_time_ind]) if len(velocity_results[key][elm_time_ind-nwin:elm_time_ind+nwin].shape) == 2: max_ind_0=np.argmax(np.abs(velocity_results[key][elm_time_ind-nwin:elm_time_ind+nwin,0]),axis=0) max_ind_1=np.argmax(np.abs(velocity_results[key][elm_time_ind-nwin:elm_time_ind+nwin,1]),axis=0) gpi_results_max[key].append([velocity_results[key][elm_time_ind-nwin:elm_time_ind+nwin,0][max_ind_0], velocity_results[key][elm_time_ind-nwin:elm_time_ind+nwin,1][max_ind_1]]) else: max_ind=np.argmax(np.abs(velocity_results[key][elm_time_ind-nwin:elm_time_ind+nwin]),axis=0) gpi_results_max[key].append(velocity_results[key][elm_time_ind-nwin:elm_time_ind+nwin][max_ind]) for variable in [energy_results, gpi_results_avg, gpi_results_max]: for key in variable: variable[key]=np.asarray(variable[key]) pickle.dump((energy_results, gpi_results_avg, gpi_results_max), open(scaling_db_file,'wb')) if ind_before_after == 0: energy_results_before=copy.deepcopy(energy_results) gpi_results_avg_before=copy.deepcopy(gpi_results_avg) gpi_results_max_before=copy.deepcopy(gpi_results_max) if ind_before_after == 1: energy_results_after=copy.deepcopy(energy_results) gpi_results_avg_after=copy.deepcopy(gpi_results_avg) gpi_results_max_after=copy.deepcopy(gpi_results_max) else: energy_results_before, gpi_results_avg_before, gpi_results_max_before = pickle.load(open(scaling_db_file_before,'rb')) energy_results_after, gpi_results_avg_after, gpi_results_max_after = pickle.load(open(scaling_db_file_after,'rb')) if plot_energy: energy_pdf.close() if plot: import matplotlib matplotlib.use('QT5Agg') import matplotlib.pyplot as plt else: import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt from numpy import logical_and as AND non_zero_ind_before=AND(energy_results_before['Poloidal'] != 0., AND(energy_results_before['Diamagnetic'] != 0., energy_results_before['Total'] != 0.)) non_zero_ind_after=AND(energy_results_after['Poloidal'] != 0., AND(energy_results_after['Diamagnetic'] != 0., energy_results_after['Total'] != 0.)) energy_results_before['Poloidal change']=energy_results_after['Poloidal']-energy_results_before['Poloidal'] energy_results_after['Poloidal change']=energy_results_after['Poloidal']-energy_results_before['Poloidal'] energy_results_before['Diamagnetic change']=energy_results_after['Diamagnetic']-energy_results_before['Diamagnetic'] energy_results_after['Diamagnetic change']=energy_results_after['Diamagnetic']-energy_results_before['Diamagnetic'] energy_results_before['Total change']=energy_results_after['Total']-energy_results_before['Total'] energy_results_after['Total change']=energy_results_after['Total']-energy_results_before['Total'] energy_results_before['Poloidal relative change']=energy_results_after['Poloidal']/energy_results_before['Poloidal']-1 energy_results_after['Poloidal relative change']=energy_results_after['Poloidal']/energy_results_before['Poloidal']-1 energy_results_before['Diamagnetic relative change']=energy_results_after['Diamagnetic']/energy_results_before['Diamagnetic']-1 energy_results_after['Diamagnetic relative change']=energy_results_after['Diamagnetic']/energy_results_before['Diamagnetic']-1 energy_results_before['Total relative change']=energy_results_after['Total']/energy_results_before['Total']-1 energy_results_after['Total relative change']=energy_results_after['Total']/energy_results_before['Total']-1 energy_results_before['Poloidal relative change']=(energy_results_after['Poloidal']-energy_results_before['Poloidal'])/energy_results_before['Total'] energy_results_after['Poloidal relative change']=(energy_results_after['Poloidal']-energy_results_before['Poloidal'])/energy_results_before['Total'] energy_results_before['Diamagnetic relative change']=(energy_results_after['Diamagnetic']-energy_results_before['Diamagnetic'])/energy_results_before['Total'] energy_results_after['Diamagnetic relative change']=(energy_results_after['Diamagnetic']-energy_results_before['Diamagnetic'])/energy_results_before['Total'] energy_results_before['Total relative change']=(energy_results_after['Total']-energy_results_before['Total'])/energy_results_before['Total'] energy_results_after['Total relative change']=(energy_results_after['Total']-energy_results_before['Total'])/energy_results_before['Total'] del AND y_variables_before=[gpi_results_avg_before,gpi_results_max_before] y_variables_after=[gpi_results_avg_after,gpi_results_max_after] title_addon=['(temporal avg)','(range max)'] radvert=['radial', 'vertical'] for var_ind in range(len(y_variables_after)): if pdf: filename=result_filename.replace('processed_data', 'plots')+'_'+title_addon[var_ind].replace('(','').replace(')','').replace(' ','_') pdf_pages=PdfPages(filename+'.pdf') file=open(filename+'_linear_dependence.txt', 'wt') for key_gpi in y_variables_after[var_ind].keys(): for key_grad in energy_results_after.keys(): for i in range(2): if len(y_variables_after[var_ind][key_gpi].shape) == 2: fig, ax = plt.subplots() y_var_before=y_variables_before[var_ind][key_gpi][:,i][non_zero_ind_before] non_zero_ind_y_before=np.where(y_var_before != 0.) y_var_after=y_variables_after[var_ind][key_gpi][:,i][non_zero_ind_after] non_zero_ind_y_after=np.where(y_var_after != 0.) gpi_key_str_addon=' '+radvert[i] elif len(y_variables_after[var_ind][key_gpi].shape) == 1: if i == 1: continue fig, ax = plt.subplots() y_var_before=y_variables_before[var_ind][key_gpi][non_zero_ind_before] non_zero_ind_y_before=np.where(y_var_before != 0.) y_var_after=y_variables_after[var_ind][key_gpi][non_zero_ind_after] non_zero_ind_y_after=np.where(y_var_after != 0.) gpi_key_str_addon=' ' for before_after_str in ['Before', 'After']: if before_after_str == 'Before': y_var=y_var_before non_zero_ind_y=non_zero_ind_y_before non_zero_ind=non_zero_ind_before energy_results=energy_results_before color='tab:blue' fit_color='blue' if before_after_str == 'After': y_var=y_var_after non_zero_ind_y=non_zero_ind_y_after non_zero_ind=non_zero_ind_after energy_results=energy_results_after color='red' fit_color='black' ind_nan=np.logical_not(np.isnan(energy_results[key_grad][non_zero_ind][non_zero_ind_y])) try: val,cov=np.polyfit(energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan], y_var[non_zero_ind_y][ind_nan], 1, cov=True) except: val=[np.nan, np.nan] cov=np.asarray([[np.nan,np.nan], [np.nan,np.nan]]) a=val[0] b=val[1] delta_a=np.sqrt(cov[0,0]) delta_b=np.sqrt(cov[1,1]) good_plot=True if (np.abs(delta_a/a) < dependence_error_threshold and np.abs(delta_b/b) < dependence_error_threshold): file.write(before_after_str+' result:\n') file.write(key_gpi+' '+gpi_key_str_addon+' = '+f"{b:.4f}"+' +- '+f"{delta_b:.4f}"+' + ('+f"{a:.4f}"+'+-'+f"{delta_a:.4f}"+') * '+key_grad+' energy_results'+'\n') file.write('Relative error: delta_b/b: '+f"{np.abs(delta_b/b*100):.6f}"+'% , delta_a/a: '+f"{np.abs(delta_a/a*100):.6f}"+'%\n\n') elif plot_only_good: good_plot=False if good_plot: ax.scatter(energy_results[key_grad][non_zero_ind][non_zero_ind_y], y_var[non_zero_ind_y], marker='o', color=color) if plot_linear_fit: ax.plot(energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan], a*energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan]+b, color=fit_color) ind_sorted=np.argsort(energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan]) ax.fill_between(energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan][ind_sorted], (a-delta_a)*energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan][ind_sorted]+(b-delta_b), (a+delta_a)*energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan][ind_sorted]+(b+delta_b), color=color, alpha=0.3) ax.fill_between(energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan][ind_sorted], (a-delta_a)*energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan][ind_sorted]+(b+delta_b), (a+delta_a)*energy_results[key_grad][non_zero_ind][non_zero_ind_y][ind_nan][ind_sorted]+(b-delta_b), color=color, alpha=0.3) if key_grad == 'Pressure': dimension='[kPa/m]' elif key_grad == 'Temperature': dimension='[keV/m]' elif key_grad == 'Density': dimension='[1/m3/m]' else: dimension='' if 'Velocity' in key_gpi: dimension_gpi='[m/s]' if 'Size' in key_gpi: dimension_gpi='[m]' ax.set_xlabel(key_grad+' energy results '+dimension) ax.set_ylabel(key_gpi+' '+title_addon[var_ind]+' '+dimension_gpi) ax.set_title(key_grad+' energy results'+' vs. '+key_gpi+gpi_key_str_addon+' '+title_addon[var_ind]) fig.tight_layout() if pdf: pdf_pages.savefig() if pdf: pdf_pages.close() if not plot: plt.close('all') file.close()
def test_NSTX_GPI_data_animation(exp_id=141918): flap.delete_data_object('*') print("\n------- test data read with NSTX GPI data --------") d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI') print("**** Storage contents") flap.list_data_objects() plt.close('all') print("**** Filtering GPI") #d_filter=flap.filter_data('GPI',output_name='GPI_filt',coordinate='Time', # options={'Type':'Highpass','f_low':1e2/1e3,'Design':'Chebyshev II'}) #Data is in milliseconds print('Gathering MDSPlus data') flap.get_data('NSTX_MDSPlus', name='\EFIT01::\RBDRY', exp_id=exp_id, object_name='SEP X OBJ' ) flap.get_data('NSTX_MDSPlus', name='\EFIT01::\ZBDRY', exp_id=exp_id, object_name='SEP Y OBJ' ) # flap.get_data('NSTX_MDSPlus', # name='\EFIT01::\RLIM', # exp_id=exp_id, # object_name='LIM X OBJ' # ) # # flap.get_data('NSTX_MDSPlus', # name='\EFIT01::\ZLIM', # exp_id=exp_id, # object_name='LIM Y OBJ' # ) d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\PSIRZ', exp_id=exp_id, object_name='PSI RZ OBJ' ) d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\GAPIN', exp_id=exp_id, object_name='GAPIN' ) d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\SSIMAG', exp_id=exp_id, object_name='SSIMAG' ) d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\SSIBRY', exp_id=exp_id, object_name='SSIBRY' ) efit_options={'Plot separatrix': True, 'Separatrix X': 'SEP X OBJ', 'Separatrix Y': 'SEP Y OBJ', 'Separatrix color': 'red', # 'Plot limiter': True, # 'Limiter X': 'LIM X OBJ', # 'Limiter Y': 'LIM Y OBJ', # 'Limiter color': 'white', 'Plot flux': True, 'Flux XY': 'PSI RZ OBJ',} print("**** Plotting filtered GPI") flap.plot('GPI',plot_type='animation', slicing={'Time':flap.Intervals(250.,260.)}, axes=['Device R','Device z','Time'], options={'Z range':[0,512],'Wait':0.0,'Clear':False, 'EFIT options':efit_options}) flap.list_data_objects()
def show_nstx_gpi_video_frames(exp_id=None, time_range=None, start_time=None, n_frame=20, logz=False, z_range=[0,512], plot_filtered=False, normalize=False, cache_data=False, plot_flux=False, plot_separatrix=False, flux_coordinates=False, device_coordinates=False, new_plot=True, save_pdf=False, colormap='gist_ncar', save_for_paraview=False, colorbar_visibility=True ): if time_range is None and start_time is None: print('time_range is None, the entire shot is plotted.') if time_range is not None: if (type(time_range) is not list and len(time_range) != 2): raise TypeError('time_range needs to be a list with two elements.') if start_time is not None: if type(start_time) is not int and type(start_time) is not float: raise TypeError('start_time needs to be a number.') if not cache_data: #This needs to be enhanced to actually cache the data no matter what flap.delete_data_object('*') if exp_id is not None: print("\n------- Reading NSTX GPI data --------") if cache_data: try: d=flap.get_data_object_ref(exp_id=exp_id,object_name='GPI') except: print('Data is not cached, it needs to be read.') d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI') else: d=flap.get_data('NSTX_GPI',exp_id=exp_id,name='',object_name='GPI') object_name='GPI' else: raise ValueError('The experiment ID needs to be set.') if time_range is None: time_range=[start_time,start_time+n_frame*2.5e-6] if normalize: flap.slice_data(object_name, slicing={'Time':flap.Intervals(time_range[0]-1/1e3*10, time_range[1]+1/1e3*10)}, output_name='GPI_SLICED_FOR_FILTERING') norm_obj=flap.filter_data('GPI_SLICED_FOR_FILTERING', exp_id=exp_id, coordinate='Time', options={'Type':'Lowpass', 'f_high':1e3, 'Design':'Elliptic'}, output_name='GAS_CLOUD') norm_obj.data=np.flip(norm_obj.data,axis=0) norm_obj=flap.filter_data('GAS_CLOUD', exp_id=exp_id, coordinate='Time', options={'Type':'Lowpass', 'f_high':1e3, 'Design':'Elliptic'}, output_name='GAS_CLOUD') norm_obj.data=np.flip(norm_obj.data,axis=0) coefficient=flap.slice_data('GAS_CLOUD', exp_id=exp_id, slicing={'Time':flap.Intervals(time_range[0],time_range[1])}, output_name='GPI_GAS_CLOUD').data data_obj=flap.slice_data('GPI', exp_id=exp_id, slicing={'Time':flap.Intervals(time_range[0],time_range[1])}) data_obj.data = data_obj.data/coefficient flap.add_data_object(data_obj, 'GPI_SLICED_DENORM') object_name='GPI_SLICED_DENORM' if plot_filtered: print("**** Filtering GPI") object_name='GPI_FILTERED' try: flap.get_data_object_ref(object_name, exp_id=exp_id) except: flap.filter_data(object_name, exp_id=exp_id, coordinate='Time', options={'Type':'Highpass', 'f_low':1e2, 'Design':'Chebyshev II'}, output_name='GPI_FILTERED') #Data is in milliseconds if plot_flux or plot_separatrix: print('Gathering MDSPlus EFIT data.') oplot_options={} if plot_separatrix: flap.get_data('NSTX_MDSPlus', name='\EFIT01::\RBDRY', exp_id=exp_id, object_name='SEP X OBJ' ) flap.get_data('NSTX_MDSPlus', name='\EFIT01::\ZBDRY', exp_id=exp_id, object_name='SEP Y OBJ' ) if plot_flux: d=flap.get_data('NSTX_MDSPlus', name='\EFIT01::\PSIRZ', exp_id=exp_id, object_name='PSI RZ OBJ' ) x_axis='Device R' y_axis='Device z' else: oplot_options=None if flux_coordinates: print("**** Adding Flux r coordinates") d.add_coordinate(coordinates='Flux r',exp_id=exp_id) x_axis='Flux r' y_axis='Device z' elif device_coordinates: x_axis='Device R' y_axis='Device z' if (not device_coordinates and not plot_separatrix and not flux_coordinates): x_axis='Image x' y_axis='Image y' if start_time is not None: start_sample_num=flap.slice_data(object_name, slicing={'Time':start_time}).coordinate('Sample')[0][0,0] if n_frame == 30: ny=6 nx=5 if n_frame == 20: ny=5 nx=4 gs=GridSpec(nx,ny) for index_grid_x in range(nx): for index_grid_y in range(ny): plt.subplot(gs[index_grid_x,index_grid_y]) if start_time is not None: slicing={'Sample':start_sample_num+index_grid_x*ny+index_grid_y} else: time=time_range[0]+(time_range[1]-time_range[0])/(n_frame-1)*(index_grid_x*ny+index_grid_y) slicing={'Time':time} d=flap.slice_data(object_name, slicing=slicing, output_name='GPI_SLICED') slicing={'Time':d.coordinate('Time')[0][0,0]} if plot_flux: flap.slice_data('PSI RZ OBJ',slicing=slicing,output_name='PSI RZ SLICE',options={'Interpolation':'Linear'}) oplot_options['contour']={'flux':{'Data object':'PSI RZ SLICE', 'Plot':True, 'Colormap':None, 'nlevel':51}} if plot_separatrix: flap.slice_data('SEP X OBJ',slicing=slicing,output_name='SEP X SLICE',options={'Interpolation':'Linear'}) flap.slice_data('SEP Y OBJ',slicing=slicing,output_name='SEP Y SLICE',options={'Interpolation':'Linear'}) oplot_options['path']={'separatrix':{'Data object X':'SEP X SLICE', 'Data object Y':'SEP Y SLICE', 'Plot':True, 'Color':'red'}} visibility=[True,True] if index_grid_x != nx-1: visibility[0]=False if index_grid_y != 0: visibility[1]=False flap.plot('GPI_SLICED', plot_type='contour', exp_id=exp_id, axes=[x_axis,y_axis,'Time'], options={'Z range':z_range, 'Interpolation': 'Closest value', 'Clear':False, 'Equal axes':True, 'Plot units':{'Device R':'m', 'Device z':'m'}, 'Axes visibility':visibility, 'Colormap':colormap, 'Colorbar':colorbar_visibility, 'Overplot options':oplot_options, }, plot_options={'levels':255}, ) actual_time=d.coordinate('Time')[0][0,0] #plt.title(str(exp_id)+' @ '+f"{actual_time*1000:.4f}"+'ms') plt.title(f"{actual_time*1000:.3f}"+'ms') if save_pdf: if time_range is not None: plt.savefig('NSTX_GPI_video_frames_'+str(exp_id)+'_'+str(time_range[0])+'_'+str(time_range[1])+'_nf_'+str(n_frame)+'.pdf') else: plt.savefig('NSTX_GPI_video_frames_'+str(exp_id)+'_'+str(start_time)+'_nf_'+str(n_frame)+'.pdf')
def test_ccf(): plt.close('all') print() print( '>>>>>>>>>>>>>>>>>>> Test ccf (Cross Correlation Function) <<<<<<<<<<<<<<<<<<<<<<<<' ) flap.delete_data_object('*') print( "**** Generating 10x15 random test signals, 5000 points each, 1 MHz sampling." ) flap.get_data('TESTDATA', name='TEST-*-*', options={ 'Length': 0.005, 'Signal': 'Random' }, object_name='TESTDATA') print("**** Filtering with 10 microsec integrating filter.") flap.filter_data('TESTDATA', coordinate='Time', options={ 'Type': 'Int', 'Tau': 1e-5 }, output_name='TESTDATA_filt') flap.list_data_objects() plt.figure() print("**** Plotting an original and a filtered signal.") flap.plot('TESTDATA', slicing={'Row': 1, 'Column': 1}, axes='Time') flap.plot('TESTDATA_filt', slicing={'Row': 1, 'Column': 1}) print('**** Calculating the 10x15x10x15 CCFs, each 5000 samples.') print('**** CCF START') start = time.time() flap.ccf('TESTDATA_filt', coordinate='Time', options={ 'Trend': 'Mean', 'Range': [-1e-4, 1e-4], 'Res': 1e-5, 'Norm': True }, output_name='CCF') stop = time.time() print('**** CCF STOP') print("**** Calculation time: {:6.3f} ms/signal".format( 1000 * (stop - start) / (10 * 15 * 10 * 15))) flap.list_data_objects() print( "**** Plotting spatiotemporal correlation function at ref row, column 3,3, column 3" ) plt.figure() flap.plot('CCF', slicing={ 'Row (Ref)': 3, 'Column (Ref)': 3, 'Column': 3 }, axes=['Time lag'], plot_type='multi xy') print("**** Slicing TESTDATA_filt for row: 1-3, column:1-4") flap.slice_data('TESTDATA_filt', slicing={ 'Row': [1, 2, 3], 'Column': [1, 2, 3, 4] }, output_name='TESTDATA_filt_3x4') print('**** Calculating CCFs, between original and sliced TESTDATAfilt') print('**** CCF START') flap.ccf('TESTDATA_filt', ref='TESTDATA_filt_3x4', coordinate='Time', options={ 'Trend': 'Mean', 'Range': [-1e-4, 1e-4], 'Res': 1e-5, 'Norm': True }, output_name='CCF_ref') print('**** CCF STOP') flap.list_data_objects() print( "**** Plotting spatiotemporal correlation function at ref row, column 3,3, column 3" ) plt.figure() flap.plot('CCF_ref', slicing={ 'Row (Ref)': 3, 'Column (Ref)': 3, 'Column': 3 }, axes=['Time lag'], plot_type='multi xy')
def test_image(): plt.close('all') print() print('>>>>>>>>>>>>>>>>>>> Test image <<<<<<<<<<<<<<<<<<<<<<<<') flap.delete_data_object('*') print("**** Generating a sequence of test images") flap.get_data('TESTDATA', name='VIDEO', object_name='TEST_VIDEO', options={ 'Length': 0.1, 'Samplerate': 1e3, 'Frequency': 10, 'Spotsize': 100 }) flap.list_data_objects() print("***** Showing one image") plt.figure() flap.plot('TEST_VIDEO', slicing={'Time': 30e-3 / 4}, plot_type='image', axes=['Image x', 'Image y'], options={'Clear': True}) plt.figure() print("**** Showing a sequence of images and saving to test_video.avi") flap.plot('TEST_VIDEO', plot_type='anim-image', axes=['Image x', 'Image y', 'Time'], options={ 'Z range': [0, 4095], 'Wait': 0.01, 'Clear': True, 'Video file': 'test_video.avi', 'Colorbar': True, 'Aspect ratio': 'equal' }) plt.figure() print( "*** Showing the same images as contour plots and saving to test_video_contour.avi" ) flap.plot('TEST_VIDEO', plot_type='anim-contour', axes=['Image x', 'Image y', 'Time'], options={ 'Z range': [0, 4095], 'Wait': 0.01, 'Clear': True, 'Video file': 'test_video_contour.avi', 'Colorbar': False }) print("*** Converting data object x, y coordinates to non-equidistant.") d = flap.get_data_object('TEST_VIDEO') coord_x = d.get_coordinate_object('Image x') index = [0] * 3 index[coord_x.dimension_list[0]] = ... x = np.squeeze(d.coordinate('Image x', index=index)[0]) coord_x.mode.equidistant = False coord_x.values = x coord_x.shape = x.shape coord_y = d.get_coordinate_object('Image y') index = [0] * 3 index[coord_y.dimension_list[0]] = ... y = np.squeeze(d.coordinate('Image y', index=index)[0]) coord_y.mode.equidistant = False coord_y.values = y coord_y.shape = y.shape flap.add_data_object(d, "TEST_VIDEO_noneq") flap.list_data_objects() plt.figure() print("**** Showing this video and saving to test_video_noneq.avi") flap.plot('TEST_VIDEO_noneq', plot_type='anim-image', axes=['Image x', 'Image y', 'Time'], options={ 'Z range': [0, 4095], 'Wait': 0.01, 'Clear': True, 'Video file': 'test_video_noneq.avi', 'Colorbar': True, 'Aspect ratio': 'equal' })
def test_filter(): plt.close('all') print() print('>>>>>>>>>>>>>>>>>>> Test filter <<<<<<<<<<<<<<<<<<<<<<<<') flap.delete_data_object('*') print( "**** Generating 10 square wave signals and filtering with integrating filter, 10 microsec" ) t = np.arange(1000) * 1e-6 d = np.ndarray((len(t), 10), dtype=float) for i in range(10): d[:, i] = np.sign(np.sin(math.pi * 2 * (1e4 + i * 1e3) * t)) + 1 c = flap.Coordinate(name='Time', unit='Second', mode=flap.CoordinateMode(equidistant=True), start=0.0, step=1e-6, dimension_list=[0]) d = flap.DataObject(data_array=d, coordinates=[c]) flap.add_data_object(d, "Signal") plt.figure() d.plot(options={'Y sep': 3}) di = d.filter_data(coordinate='Time', intervals=flap.Intervals(np.array([1e-4, 6e-4]), np.array([2e-4, 8e-4])), options={ 'Type': 'Int', 'Tau': 10e-6 }).plot(options={'Y sep': 3}) print("**** Filtering with differential filter, 10 microsec") plt.figure() d.plot(options={'Y sep': 3}) flap.filter_data('Signal', output_name='Signal_filt', coordinate='Time', intervals=flap.Intervals(np.array([1e-4, 6e-4]), np.array([2e-4, 8e-4])), options={ 'Type': 'Diff', 'Tau': 10e-6 }) flap.plot('Signal_filt', options={'Y sep': 3}) print( "**** Generating random data, 1 million points and overplotting spectra with various filters." ) d = flap.get_data('TESTDATA', name='TEST-1-1', options={ 'Signal': 'Random', 'Scaling': 'Digit', 'Length': 1 }, object_name='Signal') plt.figure() flap.filter_data('Signal', output_name='Signal_filt', coordinate='Time', options={ 'Type': 'Int', 'Tau': 16e-6 }) flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid.plt_axis_list[-1].set_title("{'Type':'Int','Tau':16e-6}") plt.figure() flap.filter_data('Signal', output_name='Signal_filt', coordinate='Time', options={ 'Type': 'Diff', 'Tau': 16e-6 }) flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid.plt_axis_list[-1].set_title("{'Type':'Diff','Tau':16e-6}") plt.figure() flap.filter_data('Signal', output_name='Signal_filt', coordinate='Time', options={ 'Type': 'Lowpass', 'f_high': 5e4 }) flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid.plt_axis_list[-1].set_title("{'Type':'Lowpass','f_high':5e4}") plt.figure() flap.filter_data('Signal', output_name='Signal_filt', coordinate='Time', options={ 'Type': 'Highpass', 'f_low': 1e4, 'f_high': 5e4 }) flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid.plt_axis_list[-1].set_title( "{'Type':'Highpass','f_low':1e4,'f_high':5e4}") plt.figure() flap.filter_data('Signal', output_name='Signal_filt', coordinate='Time', options={ 'Type': 'Bandpass', 'f_low': 5e3, 'f_high': 5e4 }) flap.apsd('Signal',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid = flap.apsd('Signal_filt',options={'Log':True,'Res':20,'Range':[100,5e5]},output_name='Signal_APSD')\ .plot(options={'Log x':True, 'Log y': True}) plotid.plt_axis_list[-1].set_title( "{'Type':'Bandpass','f_low':5e3,'f_high':5e4}") plt.figure() print("**** Bandpower signal [5e4-2e5] Hz, inttime 20 microsec") flap.filter_data('Signal', output_name='Signal_filt', coordinate='Time', options={ 'Type': 'Bandpass', 'f_low': 5e4, 'f_high': 2e5, 'Power': True, 'Inttime': 20e-6 }) plotid = flap.plot('Signal_filt') plotid.plt_axis_list[-1].set_title( "'Type':'Bandpass','f_low':5e4,'f_high':2e5, 'Power':True, 'Inttime':20e-6}" )
def test_apsd(): plt.close('all') print() print( '>>>>>>>>>>>>>>>>>>> Test apsd (Auto Power Spectral Density) <<<<<<<<<<<<<<<<<<<<<<<<' ) flap.delete_data_object('*') #plt.close('all') print( '**** Generating test signals with frequency changing from channel to channel.' ) d = flap.get_data('TESTDATA', name='TEST*', object_name='TEST-1_S', options={ 'Signal': 'Sin', 'F': [1e3, 1e4], 'Length': 1. }) print('**** Calculating 150 APSDs, each 1 million sample.') print('**** APSD START') start = time.time() flap.apsd('TEST-1_S', output_name='TEST-1_APSD_Sin1', options={ 'Res': 12, 'Int': 10 }) stop = time.time() print('**** APSD STOP') print("**** Calculation time: {:5.2f} second/signal".format( (stop - start) / 150.)) plt.figure() flap.plot('TEST-1_APSD_Sin1', slicing={'Row': 1}, axes='Frequency', options={ 'All': True, 'X range': [0, 5e3] }) plt.title('TEST-1-1_APSD_Sin1') print("**** Testing with a complex signal.") flap.delete_data_object('*') d = flap.get_data('TESTDATA', name='TEST-1-1', object_name='TEST-1-1_CS', options={'Signal': 'Complex-Sin'}) flap.apsd('TEST-1-1_CS', coordinate='Time', output_name='TEST-1-1_APSD_Complex-Sin', options={ 'Res': 10, 'Range': [-1e5, 1e5] }) flap.slice_data('TEST-1-1_APSD_Complex-Sin', slicing={'Frequency': flap.Intervals(-5e3, 5e3)}, output_name='TEST-1-1_APSD_Complex-Sin_sliced') flap.list_data_objects() plt.figure() flap.plot('TEST-1-1_APSD_Complex-Sin_sliced', axes='Frequency', options={'All': True}) plt.title('TEST-1-1_APSD_Complex-Sin_sliced') print( "**** Testing interval selection in apsd. APSD from 8 intervals, each 80 ms long." ) d = flap.get_data('TESTDATA', name='TEST-1-1', object_name='TEST-1-1', options={ 'Signal': 'Sin', 'Length': 1 }) intervals = flap.Intervals(0, 0.08, step=0.1, number=8) flap.apsd('TEST-1-1', output_name='TEST-1-1_APSD', intervals=intervals, options={ 'Res': 12, 'Int': 10 }) plt.figure() flap.plot('TEST-1-1_APSD', options={'X range': [0, 5e3]})
def test_plot(): print() print( "\n>>>>>>>>>>>>>>>>>>> Testing various plot modes <<<<<<<<<<<<<<<<<<<<<<<<" ) flap.delete_data_object('*') plt.close('all') print("**** Generating some test data.") length = 0.01 flap.get_data('TESTDATA', name='TEST-1-1', options={ 'Signal': 'Sin', 'Length': length }, object_name='TEST-1-1') flap.get_data('TESTDATA', name='TEST-1-2', options={ 'Signal': 'Const.', 'Length': length }, object_name='TEST-1-2') flap.get_data('TESTDATA', name='TEST-1-3', options={ 'Signal': 'Const.', 'Length': length }, object_name='TEST-1-3') flap.get_data('TESTDATA', name='TEST-1-[1-5]', options={ 'Signal': 'Sin', 'Length': length }, object_name='TESTDATA') flap.get_data('TESTDATA', name='TEST-1-1', options={ 'Signal': 'Complex-Sin', 'Length': length }, object_name='TEST-1-1_comp') flap.get_data('TESTDATA', name='TEST-1-2', options={ 'Signal': 'Complex-Sin', 'Length': length }, object_name='TEST-1-2_comp') flap.get_data('TESTDATA', name='TEST-1-[1-5]', options={ 'Signal': 'Complex-Sin', 'Length': length }, object_name='TESTDATA_comp') print("**** Creating a single plot in the upper left corner.") plt.figure() gs = GridSpec(2, 2) plt.subplot(gs[0, 0]) plot_1 = flap.plot('TEST-1-1') print("**** Creating a multi xy on the right side.") plt.subplot(gs[:, 1]) plot_2 = flap.plot('TESTDATA') plot_2 = flap.plot('TESTDATA', slicing={'Signal name': ['TEST-1-2', 'TEST-1-3']}) print("**** Overplotting into the first plot.") flap.plot('TEST-1-3', plot_id=plot_1) print("**** Plotting two complex signals into the same plot.") plt.figure() plot_3 = flap.plot('TEST-1-1_comp') plot_3 = flap.plot('TEST-1-2_comp') print( "**** Plotting absolute value and phase of multiple complex signals.") plt.figure() gs = GridSpec(1, 2) plt.subplot(gs[0, 0]) plot_4 = flap.abs_value('TESTDATA_comp').plot() plt.subplot(gs[0, 1]) plot_5 = flap.phase('TESTDATA_comp').plot(options={'Y sep': 10}) # plot_4 = flap.plot('TESTDATA_comp') print("**** Image plot of testdata and some single plots.") plt.figure() gs = GridSpec(2, 2) plt.subplot(gs[0, 0]) plot_5 = flap.plot('TESTDATA', axes=['Time', 'Row'], plot_type='image', options={ 'Colormap': 'bwr', 'Z range': [-2, 2] }) plt.subplot(gs[1, 0]) plot_6 = flap.plot('TESTDATA', slicing={'Signal name': 'TEST-1-1'}, axes=['Time'], options={'Y range': [-2, 2]}, plot_options={'linestyle': '-'}) flap.plot('TESTDATA', slicing={'Signal name': 'TEST-1-2'}, axes=['Time'], plot_options={'linestyle': '--'}) legend = ['Row 1', 'Row 2'] plot_6.plt_axis_list[0].legend(legend) plt.subplot(gs[:, 1]) plot_7 = flap.plot('TESTDATA', plot_type='multi xy', axes='Time')
import matplotlib.lines as mlines import numpy as np import os import math flap_nstx.register() database_file = '/Users/mlampert/work/NSTX_workspace/ELM_findings_mlampert_velocity_good.csv' db = pandas.read_csv(database_file, index_col=0) elm_index = list(db.index) elm = 0. failed_elms = [] number_of_failed_elms = 0 for index in elm_index: flap.delete_data_object('*') shot = int(db.loc[index]['Shot']) elm_time = db.loc[index]['ELM time'] / 1000. wd = flap.config.get_all_section('Module NSTX_GPI')['Working directory'] filename = wd + '/' + db.loc[index]['Filename'] status = db.loc[index]['OK/NOT OK'] try: pickle.load(open(filename + '.pickle', 'rb')) except: print(filename) if status != 'NO' and shot == 141321: print('Calculating ' + str(shot) + ' at ' + str(elm_time)) # try: calculate_nstx_gpi_avg_frame_velocity( exp_id=shot, #time_range=[elm_time-0.5e-3,elm_time+0.5e-3],