<jupyter_start><jupyter_text>Compare particle runsIn this notebook, I compare the different Parcels output files, with the primary goal of analysing what the most efficient way is to decrease the number of particles 'stuck' on the coral. All output files are preceded by `'d2c'`, so I search the output folders for files containing this prefix, read the files and extract the metadata into a pandas dataframe. This dataframe can then be used for analysis.<jupyter_code>import pandas as pd
import re
import xarray as xr
import os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib as mpl
from matplotlib.colors import ListedColormap
import cmocean

plt.style.use('ggplot')
sdata = data.where(data['finaldistance'][:,-1]==0,drop=True)
bdata = data.where(data['closestobject'][:,-1]==len(objects),drop=True)

fig = plt.figure()
ax = plt.axes(xticks=[],yticks=[])
ax.set_title('Dashboard '+dfilename)
ax.text(0.05,0.95,'Flow: '+flow)
ax.text(0.05,0.9,'Time direction: '+fb)
ax.text(0.05,0.85,'Runtime: '+str(runtime.seconds)+' seconds')
ax.text(0.05,0.8,'Execution dt: '+str(dt.total_seconds())+' seconds')
ax.text(0.05,0.75,'Output dt: '+str(outputdt.total_seconds())+' seconds')
ax.text(0.05,0.7,'dx/dy: '+str(dx)+' m')
ax.text(0.05,0.65,'Number of particles released: '+str(len(data['traj'])))
ax.text(0.05,0.6,'Number of particles beached: '+str(len(bdata['traj'])))
ax.text(0.05,0.55,'Percentage of particles beached: '+str(np.round(len(bdata['traj'])/len(data['traj'])*100,1))+' %')
# plt.savefig('Figures/Dashboard-'+flow+fb)<jupyter_output><empty_output><jupyter_text>Run the next cell to create the dataframe and save it using pandas' `to_pickle`. If you want to analyse a saved dataframe, skip this cell.<jupyter_code>output = pd.DataFrame(columns=['flow','dx','time direction','dt','runtime','execution time','output dt','#particles','#stuck','%stuck','#beached','%beached','beaching scheme'])

foldernames = ['16objects','21objects']
    
for folder in foldernames:
    objects = np.load(folder+'/preprocessed/objects.npy')
    for file in os.listdir(folder+'/pfiles'):
            data = xr.open_dataset(folder+'/pfiles/'+file)
            sdata = data.where(data['finaldistance'][:,-1]==0,drop=True)
            bdata = data.where(data['closestobject'][:,-1]==len(objects),drop=True)
            filelist = file.split('-')
            flow = filelist[-3]
            fb = filelist[-1][:-3]
            
            filename = flow+'.nc'
            flowdata = xr.open_dataset(folder+'/'+filename)
            
            dx = flowdata['X'][1]-flowdata['X'][0]
            dx = dx.values

            outputdtlist = data.attrs['outputdt'].split()
            outputdt = float(outputdtlist[0])

            dtlist = data.attrs['dt'].split()
            dt = float(dtlist[0])

            runtimelist = data.attrs['runtime'].split()
            runtime = float(runtimelist[0])

            if 'executiontime' in data.attrs:
                exectimelist = data.attrs['executiontime'].split()
                exectime = float(exectimelist[0])
            else:
                exectime = None

            if 'beaching_strategy' in data.attrs:
                strategy = data.attrs['beaching_strategy']
            else:
                strategy = 0


            df = pd.DataFrame([[flow,dx,fb,dt,runtime,exectime,outputdt,len(data['traj']),len(sdata['traj']),np.round(len(sdata['traj'])/len(data['traj'])*100,2),len(bdata['traj']),np.round(len(bdata['traj'])/len(data['traj'])*100,2),strategy]],columns=['flow','dx','time direction','dt','runtime','execution time','output dt','#particles','#stuck','%stuck','#beached','%beached','beaching scheme'])
            frames = [output,df]
            output = pd.concat(frames)

output.to_pickle('RunsComp.pkl')
output = pd.read_pickle('RunsComp.pkl')
dx.values
# comp = output.where(abs(output['dt']) == 0.01)
# comp = output.where(output['flow'] == 'waveparabolic')
# output = comp.dropna() 
output
my_cmap = ListedColormap(['dodgerblue','g','purple','darkorange','red','purple'])
%matplotlib qt

fig = plt.figure()
ax = plt.axes()
ax.set_title('Boundary conditions')
ax.set_ylabel('Particles stuck [%]')
ax.set_xlabel('Boundary condition')
bp0 = ax.scatter([0]*len(output['%beached'][output['beaching scheme']==0]),output['%beached'][output['beaching scheme']==0],c = [my_cmap(0)],alpha=0.7,label='0',edgecolors='k')
bp1 = ax.scatter([1]*len(output['%beached'][output['beaching scheme']==1]),output['%beached'][output['beaching scheme']==1],c = [my_cmap(1)],alpha=0.7,label='1',edgecolors='k')
bp2 = ax.scatter([2]*len(output['%beached'][output['beaching scheme']==2]),output['%beached'][output['beaching scheme']==2],c = [my_cmap(2)],alpha=0.7,label='2',edgecolors='k')
ax.set_xticks([0,1,2])
ax.set_xticklabels([0,1,2])
plt.savefig('Figures/boundary_conditions')
%matplotlib qt
dx_cmap = plt.get_cmap('cmo.matter',len(np.unique(output['beaching scheme'])))

fig = plt.figure()
ax = plt.axes()
ax.set_title('Beaching')
ax.set_ylabel('Particles beached [%]')
ax.set_xlabel('dt [s]')
sc0 = ax.scatter(output['dt'][output['beaching scheme']==0],output['%beached'][output['beaching scheme']==0],c = [my_cmap(0)],alpha=0.7,label='0',edgecolors='k')
sc1 = ax.scatter(output['dt'][output['beaching scheme']==1],output['%beached'][output['beaching scheme']==1],c = [my_cmap(1)],alpha=0.7,label='1',edgecolors='k')
sc2 = ax.scatter(output['dt'][output['beaching scheme']==2],output['%beached'][output['beaching scheme']==2],c = [my_cmap(2)],alpha=0.7,label='2',edgecolors='k',s=50,marker='^')
sc3 = ax.scatter(output['dt'][output['beaching scheme']==3],output['%beached'][output['beaching scheme']==3],c = [my_cmap(3)],alpha=0.7,label='3',edgecolors='k')
# sc4 = ax.scatter(output['dt'][output['beaching scheme']==4],output['%beached'][output['beaching scheme']==4],c = [my_cmap(4)],label='4',edgecolors='k')
# sc5 = ax.scatter(output['dt'][output['beaching scheme']==5],output['%beached'][output['beaching scheme']==5],c = [my_cmap(5)],label='5',edgecolors='k')
ax.legend(title = 'beaching scheme')
ax.set_xscale('log')
ax.set_xlim(left=0.00005)
ax.set_ylim(top=10)
# ax.set_yscale('log')
plt.savefig('Figures/runcomp-beaching-dtdx')
%matplotlib qt

fig = plt.figure()
ax = plt.axes()
ax.set_title('Stuck')
ax.set_ylabel('Particles stuck [%]')
ax.set_xlabel('dt [s]')
sc0 = ax.scatter(output['dt'][output['beaching scheme']==0],output['%stuck'][output['beaching scheme']==0],c = [my_cmap(0)],alpha=0.7,label='0',edgecolors='k')
sc1 = ax.scatter(output['dt'][output['beaching scheme']==1],output['%stuck'][output['beaching scheme']==1],c = [my_cmap(1)],alpha=0.7,label='1',edgecolors='k')
sc2 = ax.scatter(output['dt'][output['beaching scheme']==2],output['%stuck'][output['beaching scheme']==2],c = [my_cmap(2)],alpha=0.7,label='2',edgecolors='k',s=50,marker='^')
sc3 = ax.scatter(output['dt'][output['beaching scheme']==3],output['%stuck'][output['beaching scheme']==3],c = [my_cmap(3)],alpha=0.7,label='3',edgecolors='k')
# sc4 = ax.scatter(output['dt'][output['beaching scheme']==4],output['%stuck'][output['beaching scheme']==4],c = [my_cmap(4)],label='4',edgecolors='k')
ax.legend(title='beaching scheme',loc = 'upper center')
ax.set_xscale('log')
ax.set_xlim(left=0.00005)
ax.set_ylim(bottom=0)
ax.set_ylim(top=10)
# plt.savefig('Figures/runcomp-stuck-dtdx')
%matplotlib qt

fig = plt.figure()
ax = plt.axes()
ax.set_title('Beached vs Stuck')
ax.set_ylabel('Particles beached [%]')
ax.set_xlabel('Particles stuck [%]')
x = np.linspace(0,5)
y = x
ax.plot(x,y,label='beached = stuck')
sc0 = ax.scatter(output['%stuck'][output['beaching scheme']==0],output['%beached'][output['beaching scheme']==0],c = [my_cmap(0)],edgecolors='k',alpha=0.7,label='0')
sc1 = ax.scatter(output['%stuck'][output['beaching scheme']==1],output['%beached'][output['beaching scheme']==1],c = [my_cmap(1)],edgecolors='k',alpha=0.7,label='1')
sc2 = ax.scatter(output['%stuck'][output['beaching scheme']==2],output['%beached'][output['beaching scheme']==2],c='w',edgecolors='k',alpha=0.7,label='2')
sc3 = ax.scatter(output['%stuck'][output['beaching scheme']==3],output['%beached'][output['beaching scheme']==3],c = [my_cmap(3)],edgecolors='k',alpha=0.7,label='3')
# sc4 = ax.scatter(output['%stuck'][output['beaching scheme']==4],output['%beached'][output['beaching scheme']==4],c='yellow',edgecolors='k',alpha=0.7,label='4')
# ax.set_ylim(bottom=0)
# ax.set_xlim(left=0)
ax.legend(title = 'beaching scheme')
# plt.savefig('Figures/runcomp-beached-vs-stuck')
%matplotlib qt

fig = plt.figure()
ax = plt.axes()
ax.set_title('Beached vs Stuck')
ax.set_ylabel('Particles beached/stuck [%]')
ax.set_xlabel('Beaching scheme')

sc0 = ax.scatter(output['beaching scheme'][output['dt']==0.01],output['%beached'][output['dt']==0.01],c = 'orangered',edgecolors='k',alpha=0.7,label='0.01')
sc1 = ax.scatter(output['beaching scheme'][output['dt']==0.001],output['%beached'][output['dt']==0.001],c = 'deepskyblue',edgecolors='k',alpha=0.7,label='0.001')
sc2 = ax.scatter(output['beaching scheme'][output['dt']==0.0001],output['%beached'][output['dt']==0.0001],c = 'greenyellow',edgecolors='k',alpha=0.7,label='0.0001')
# sc0 = ax.scatter(output['beaching scheme'][output['dt']==0.01],output['%stuck'][output['dt']==0.01],c = 'orangered',marker='^',edgecolors='k',alpha=0.7,label='0')
# sc1 = ax.scatter(output['beaching scheme'][output['dt']==0.001],output['%stuck'][output['dt']==0.001],c = 'deepskyblue',marker='^',edgecolors='k',alpha=0.7,label='1')
ax.legend(title = 'dt')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22920807615859398, 'fraction_numerical': 0.03263939742650905, 'mean_word_length': 6.7158999192897495, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '125487', 'license': 'MIT', 'n_tokens_mistral': 3655, 'n_tokens_neox': 3585, 'n_words': 478, 'path': '2D Analysis/AnalysisRunComp.ipynb', 'repo_name': 'OceanParcels/CoralDispersion', 'size': '9559'}