import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

#Load file
dt=pd.read_csv("sevn_output/output_0.csv")
#Give a look to the columns
print(dt.columns)
#Consider only the final states
dt=dt.drop_duplicates(["ID","name"], keep='last')

#Load evolved file
dte=pd.read_csv("sevn_output/evolved_0.dat",sep='\s+')
#Give a look to the columns
print(dte.columns)
dte=dte.rename(columns={'#ID': 'ID','Mass_0':"Mzams_0", 'Mass_1':"Mzams_1"})
#After change
print(dte.columns)

#Join the two dataset
dt = dt.merge(dte, on=["ID","name"],  how="inner", suffixes=("","_ini") )
# - on: column(s, can be a list of columns) to match during the merge of the two tables. The colum(s) has(have) to be present in both the tables
# - how: type of join to use, see documentation here and the next slide
# - suffixes: columns with the same name in the two tables (not used in on) will be renamed adding these suffixes.
#Give a look to the columns
print(dt.columns)


#Create filter indexes
idx0 = (dt.RemnantType_0==6)
idx1 = (dt.RemnantType_1==6)
idxb0 = idx0  & dt.Semimajor.notnull()
idxb1 = idx1  & dt.Semimajor.notnull()
idxm0 = idxb0 & (dt.GWtime + dt.BWorldtime  <= 14000)
idxm1 = idxb1 & (dt.GWtime + dt.BWorldtime  <= 14000)


#Filter and join masses
AllBH = pd.concat([dt[idx0].Mass_0,dt[idx1].Mass_1])
BoundBH = pd.concat([dt[idxb0].Mass_0,dt[idxb1].Mass_1])
MergingBH = pd.concat([dt[idxm0].Mass_0,dt[idxm1].Mass_1])

#Filter and join initial masses
AllBHzams = pd.concat([dt[idx0].Mzams_0,dt[idx1].Mzams_1])
BoundBHzams = pd.concat([dt[idxb0].Mzams_0,dt[idxb1].Mzams_1])
MergingBHzams = pd.concat([dt[idxm0].Mzams_0,dt[idxm1].Mzams_1])

#Filter and join initial semimajor axis
AllBHa = pd.concat([dt[idx0].a,dt[idx1].a])
BoundBHa = pd.concat([dt[idxb0].a,dt[idxb1].a])
MergingBHa = pd.concat([dt[idxm0].a,dt[idxm1].a])


#Plot
plt.figure(figsize=(10,5))

plt.subplot(1,2,1)
plt.scatter(AllBHzams,AllBH,zorder=1,edgecolor="k",s=30,label="All")
plt.scatter(BoundBHzams,BoundBH,zorder=2,edgecolor="k",s=30, label="Bound")
plt.scatter(MergingBHzams,MergingBH,zorder=3,edgecolor="k",s=30, label="Merging")
plt.plot(np.linspace(0,140),np.linspace(0,140),ls="dashed",c="gray")
plt.xscale("log")
plt.yscale("log")
plt.ylabel("BH mass [M$_\odot$]",fontsize=18)
plt.xlabel("$M\mathrm{zams}$  [M$_\odot$]",fontsize=18)
plt.gca().tick_params(axis='both', which='major', labelsize=18)
plt.legend(fontsize=16)

plt.subplot(1,2,2)
plt.scatter(AllBHa,AllBH,zorder=1,edgecolor="k",s=30,label="All")
plt.scatter(BoundBHa,BoundBH,zorder=2,edgecolor="k",s=30,label="Bound")
plt.scatter(MergingBHa,MergingBH,zorder=3,edgecolor="k",s=30,label="Merging")
plt.xscale("log")
plt.yscale("log")
plt.xlabel("Semimajor initial  [R$_\odot$]",fontsize=18)
plt.ylabel("BH mass [M$_\odot$]",fontsize=18)
plt.gca().tick_params(axis='both', which='major', labelsize=18)



plt.tight_layout()
plt.savefig("analysis3.png")
plt.show()

[((86, 125), 'pandas.read_csv', 'pd.read_csv', (['"""sevn_output/output_0.csv"""'], {}), "('sevn_output/output_0.csv')\n", (97, 125), True, 'import pandas as pd\n'), ((278, 330), 'pandas.read_csv', 'pd.read_csv', (['"""sevn_output/evolved_0.dat"""'], {'sep': '"""\\\\s+"""'}), "('sevn_output/evolved_0.dat', sep='\\\\s+')\n", (289, 330), True, 'import pandas as pd\n'), ((1264, 1309), 'pandas.concat', 'pd.concat', (['[dt[idx0].Mass_0, dt[idx1].Mass_1]'], {}), '([dt[idx0].Mass_0, dt[idx1].Mass_1])\n', (1273, 1309), True, 'import pandas as pd\n'), ((1319, 1366), 'pandas.concat', 'pd.concat', (['[dt[idxb0].Mass_0, dt[idxb1].Mass_1]'], {}), '([dt[idxb0].Mass_0, dt[idxb1].Mass_1])\n', (1328, 1366), True, 'import pandas as pd\n'), ((1378, 1425), 'pandas.concat', 'pd.concat', (['[dt[idxm0].Mass_0, dt[idxm1].Mass_1]'], {}), '([dt[idxm0].Mass_0, dt[idxm1].Mass_1])\n', (1387, 1425), True, 'import pandas as pd\n'), ((1470, 1517), 'pandas.concat', 'pd.concat', (['[dt[idx0].Mzams_0, dt[idx1].Mzams_1]'], {}), '([dt[idx0].Mzams_0, dt[idx1].Mzams_1])\n', (1479, 1517), True, 'import pandas as pd\n'), ((1531, 1580), 'pandas.concat', 'pd.concat', (['[dt[idxb0].Mzams_0, dt[idxb1].Mzams_1]'], {}), '([dt[idxb0].Mzams_0, dt[idxb1].Mzams_1])\n', (1540, 1580), True, 'import pandas as pd\n'), ((1596, 1645), 'pandas.concat', 'pd.concat', (['[dt[idxm0].Mzams_0, dt[idxm1].Mzams_1]'], {}), '([dt[idxm0].Mzams_0, dt[idxm1].Mzams_1])\n', (1605, 1645), True, 'import pandas as pd\n'), ((1695, 1730), 'pandas.concat', 'pd.concat', (['[dt[idx0].a, dt[idx1].a]'], {}), '([dt[idx0].a, dt[idx1].a])\n', (1704, 1730), True, 'import pandas as pd\n'), ((1741, 1778), 'pandas.concat', 'pd.concat', (['[dt[idxb0].a, dt[idxb1].a]'], {}), '([dt[idxb0].a, dt[idxb1].a])\n', (1750, 1778), True, 'import pandas as pd\n'), ((1791, 1828), 'pandas.concat', 'pd.concat', (['[dt[idxm0].a, dt[idxm1].a]'], {}), '([dt[idxm0].a, dt[idxm1].a])\n', (1800, 1828), True, 'import pandas as pd\n'), ((1836, 1863), 'matplotlib.pyplot.figure', 'plt.figure', ([], {'figsize': '(10, 5)'}), '(figsize=(10, 5))\n', (1846, 1863), True, 'import matplotlib.pyplot as plt\n'), ((1864, 1884), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(1)', '(2)', '(1)'], {}), '(1, 2, 1)\n', (1875, 1884), True, 'import matplotlib.pyplot as plt\n'), ((1883, 1956), 'matplotlib.pyplot.scatter', 'plt.scatter', (['AllBHzams', 'AllBH'], {'zorder': '(1)', 'edgecolor': '"""k"""', 's': '(30)', 'label': '"""All"""'}), "(AllBHzams, AllBH, zorder=1, edgecolor='k', s=30, label='All')\n", (1894, 1956), True, 'import matplotlib.pyplot as plt\n'), ((1952, 2031), 'matplotlib.pyplot.scatter', 'plt.scatter', (['BoundBHzams', 'BoundBH'], {'zorder': '(2)', 'edgecolor': '"""k"""', 's': '(30)', 'label': '"""Bound"""'}), "(BoundBHzams, BoundBH, zorder=2, edgecolor='k', s=30, label='Bound')\n", (1963, 2031), True, 'import matplotlib.pyplot as plt\n'), ((2028, 2118), 'matplotlib.pyplot.scatter', 'plt.scatter', (['MergingBHzams', 'MergingBH'], {'zorder': '(3)', 'edgecolor': '"""k"""', 's': '(30)', 'label': '"""Merging"""'}), "(MergingBHzams, MergingBH, zorder=3, edgecolor='k', s=30, label=\n    'Merging')\n", (2039, 2118), True, 'import matplotlib.pyplot as plt\n'), ((2179, 2196), 'matplotlib.pyplot.xscale', 'plt.xscale', (['"""log"""'], {}), "('log')\n", (2189, 2196), True, 'import matplotlib.pyplot as plt\n'), ((2197, 2214), 'matplotlib.pyplot.yscale', 'plt.yscale', (['"""log"""'], {}), "('log')\n", (2207, 2214), True, 'import matplotlib.pyplot as plt\n'), ((2215, 2262), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""BH mass [M$_\\\\odot$]"""'], {'fontsize': '(18)'}), "('BH mass [M$_\\\\odot$]', fontsize=18)\n", (2225, 2262), True, 'import matplotlib.pyplot as plt\n'), ((2261, 2319), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""$M\\\\mathrm{zams}$  [M$_\\\\odot$]"""'], {'fontsize': '(18)'}), "('$M\\\\mathrm{zams}$  [M$_\\\\odot$]', fontsize=18)\n", (2271, 2319), True, 'import matplotlib.pyplot as plt\n'), ((2381, 2404), 'matplotlib.pyplot.legend', 'plt.legend', ([], {'fontsize': '(16)'}), '(fontsize=16)\n', (2391, 2404), True, 'import matplotlib.pyplot as plt\n'), ((2406, 2426), 'matplotlib.pyplot.subplot', 'plt.subplot', (['(1)', '(2)', '(2)'], {}), '(1, 2, 2)\n', (2417, 2426), True, 'import matplotlib.pyplot as plt\n'), ((2425, 2495), 'matplotlib.pyplot.scatter', 'plt.scatter', (['AllBHa', 'AllBH'], {'zorder': '(1)', 'edgecolor': '"""k"""', 's': '(30)', 'label': '"""All"""'}), "(AllBHa, AllBH, zorder=1, edgecolor='k', s=30, label='All')\n", (2436, 2495), True, 'import matplotlib.pyplot as plt\n'), ((2491, 2567), 'matplotlib.pyplot.scatter', 'plt.scatter', (['BoundBHa', 'BoundBH'], {'zorder': '(2)', 'edgecolor': '"""k"""', 's': '(30)', 'label': '"""Bound"""'}), "(BoundBHa, BoundBH, zorder=2, edgecolor='k', s=30, label='Bound')\n", (2502, 2567), True, 'import matplotlib.pyplot as plt\n'), ((2563, 2650), 'matplotlib.pyplot.scatter', 'plt.scatter', (['MergingBHa', 'MergingBH'], {'zorder': '(3)', 'edgecolor': '"""k"""', 's': '(30)', 'label': '"""Merging"""'}), "(MergingBHa, MergingBH, zorder=3, edgecolor='k', s=30, label=\n    'Merging')\n", (2574, 2650), True, 'import matplotlib.pyplot as plt\n'), ((2641, 2658), 'matplotlib.pyplot.xscale', 'plt.xscale', (['"""log"""'], {}), "('log')\n", (2651, 2658), True, 'import matplotlib.pyplot as plt\n'), ((2659, 2676), 'matplotlib.pyplot.yscale', 'plt.yscale', (['"""log"""'], {}), "('log')\n", (2669, 2676), True, 'import matplotlib.pyplot as plt\n'), ((2677, 2735), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Semimajor initial  [R$_\\\\odot$]"""'], {'fontsize': '(18)'}), "('Semimajor initial  [R$_\\\\odot$]', fontsize=18)\n", (2687, 2735), True, 'import matplotlib.pyplot as plt\n'), ((2734, 2781), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""BH mass [M$_\\\\odot$]"""'], {'fontsize': '(18)'}), "('BH mass [M$_\\\\odot$]', fontsize=18)\n", (2744, 2781), True, 'import matplotlib.pyplot as plt\n'), ((2847, 2865), 'matplotlib.pyplot.tight_layout', 'plt.tight_layout', ([], {}), '()\n', (2863, 2865), True, 'import matplotlib.pyplot as plt\n'), ((2866, 2894), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""analysis3.png"""'], {}), "('analysis3.png')\n", (2877, 2894), True, 'import matplotlib.pyplot as plt\n'), ((2895, 2905), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (2903, 2905), True, 'import matplotlib.pyplot as plt\n'), ((2119, 2138), 'numpy.linspace', 'np.linspace', (['(0)', '(140)'], {}), '(0, 140)\n', (2130, 2138), True, 'import numpy as np\n'), ((2138, 2157), 'numpy.linspace', 'np.linspace', (['(0)', '(140)'], {}), '(0, 140)\n', (2149, 2157), True, 'import numpy as np\n'), ((2317, 2326), 'matplotlib.pyplot.gca', 'plt.gca', ([], {}), '()\n', (2324, 2326), True, 'import matplotlib.pyplot as plt\n'), ((2780, 2789), 'matplotlib.pyplot.gca', 'plt.gca', ([], {}), '()\n', (2787, 2789), True, 'import matplotlib.pyplot as plt\n')]