import%20marimo%0A%0A__generated_with%20%3D%20%220.17.6%22%0Aapp%20%3D%20marimo.App(width%3D%22columns%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20%20%20%20%20%20%20%20%20%20%20%20as%20%20%20%20%20mo%0A%20%20%20%20import%20pandas%20%20%20%20%20%20%20%20%20%20%20%20as%20%20%20%20%20pd%0A%20%20%20%20import%20astropy.units%20%20%20%20%20as%20%20%20%20%20u%0A%20%20%20%20import%20numpy%20%20%20%20%20%20%20%20%20%20%20%20%20as%20%20%20%20%20np%0A%20%20%20%20from%20%20%20numpy.typing%20%20%20%20%20%20import%20NDArray%0A%20%20%20%20from%20%20%20astropy.io%20%20%20%20%20%20%20%20import%20fits%0A%20%20%20%20from%20%20%20astropy.cosmology%20import%20FlatLambdaCDM%0A%20%20%20%20from%20%20%20astropy.table%20%20%20%20%20import%20Table%0A%0A%20%20%20%20import%20utils%20%20%20%20%20%20%20%20%20%20%20%20%20as%20%20%20%20%20myutils%0A%0A%20%20%20%20COSMO%20%3D%20FlatLambdaCDM(70%2C%200.3)%0A%20%20%20%20RNG%20%20%20%3D%20np.random.default_rng(seed%3D666)%0A%20%20%20%20return%20NDArray%2C%20RNG%2C%20Table%2C%20mo%2C%20np%2C%20pd%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20matplotlib%0A%20%20%20%20import%20matplotlib.pyplot%20%20%20as%20%20%20%20%20plt%0A%20%20%20%20from%20%20%20matplotlib.gridspec%20import%20GridSpec%0A%20%20%20%20from%20%20%20matplotlib%20%20%20%20%20%20%20%20%20%20import%20rc%0A%0A%20%20%20%20rc('font'%2C%20**%7B'family'%3A%20'serif'%2C%20'serif'%3A%20%5B'Times'%5D%7D)%0A%20%20%20%20rc('text'%2C%20usetex%3DTrue)%0A%0A%20%20%20%20matplotlib.rcParams%5B'mathtext.fontset'%5D%20%20%20%3D%20'custom'%0A%20%20%20%20matplotlib.rcParams%5B'mathtext.rm'%5D%20%20%20%20%20%20%20%20%3D%20'Times'%0A%20%20%20%20matplotlib.rcParams%5B'xtick.labelsize'%5D%20%20%20%20%3D%2014%0A%20%20%20%20matplotlib.rcParams%5B'ytick.labelsize'%5D%20%20%20%20%3D%2014%0A%20%20%20%20matplotlib.rcParams%5B'xtick.direction'%5D%20%20%20%20%3D%20'in'%0A%20%20%20%20matplotlib.rcParams%5B'ytick.direction'%5D%20%20%20%20%3D%20'in'%0A%20%20%20%20matplotlib.rcParams%5B'figure.figsize'%5D%20%20%20%20%20%3D%20(5%2C%205)%0A%20%20%20%20matplotlib.rcParams%5B'xtick.top'%5D%20%20%20%20%20%20%20%20%20%20%3D%20True%0A%20%20%20%20matplotlib.rcParams%5B'ytick.right'%5D%20%20%20%20%20%20%20%20%3D%20True%0A%20%20%20%20matplotlib.rcParams%5B'axes.labelsize'%5D%20%20%20%20%20%3D%2014%20%20%23%20Font%20size%20for%20axis%20labels%0A%20%20%20%20return%20(plt%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Loading%20catalogues%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Catalogue%20of%20substructure%20detections%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Table)%3A%0A%20%20%20%20cat_sub%20%3D%20Table.read('cds%2Fsubstructure_detections.fits').to_pandas()%0A%20%20%20%20cat_sub.head()%0A%20%20%20%20return%20(cat_sub%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Catalogue%20of%20selected%20sample%20of%20galaxies%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Table)%3A%0A%20%20%20%20cat_selection%20%3D%20Table.read('cds%2Fselected_sample.fits').to_pandas()%0A%20%20%20%20cat_selection.head()%0A%20%20%20%20return%20(cat_selection%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Master%20CWeb2025%20catalogue%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Table)%3A%0A%20%20%20%20cat_cweb%20%3D%20Table.read('COSMOSWeb_megamaster_v3.1.0.fits'%2C%20hdu%3D1)%5B%5B'ID_SE%2B%2B'%2C%20'LP_zfinal'%2C%20'LP_mass_med_PDF'%2C%20'LP_sfr_med_PDF'%2C%20'FLUX_MODEL_F277W'%2C%20'FLUX_MODEL_F444W'%5D%5D.to_pandas()%0A%20%20%20%20cat_cweb%0A%20%20%20%20return%20(cat_cweb%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Fraction%20of%20galaxies%20with%20substructures%20as%20a%20function%20of%20redshift%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(NDArray%2C%20RNG%2C%20np)%3A%0A%20%20%20%20def%20boostrap(n%3A%20int%2C%20probabilities%3A%20NDArray)%3A%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Boostrap%20realisations%0A%20%20%20%20%20%20%20%20boot%20%20%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20length%20%3D%20len(probabilities)%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(n)%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Randomly%20select%20galaxies%20with%20replacement%0A%20%20%20%20%20%20%20%20%20%20%20%20indices%20%20%20%20%3D%20RNG.integers(0%2C%20length%2C%20length)%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Fractions%20of%20galaxies%20(%25)%20with%20substructures%0A%20%20%20%20%20%20%20%20%20%20%20%20boot.append(np.nansum(probabilities%5Bindices%5D)%20%2F%20length%20*%20100)%0A%0A%20%20%20%20%20%20%20%20return%20boot%0A%20%20%20%20return%20(boostrap%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Intrinsic%20detection%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(boostrap%2C%20cat_cweb%2C%20cat_selection%2C%20np)%3A%0A%20%20%20%20%23%20Merge%20catalogue%20of%20selected%20galaxies%20with%20COSMOS%20Web%20catalogue%20to%20have%20the%20redshift%0A%20%20%20%20_match%20%3D%20cat_selection.merge(cat_cweb%2C%20on%3D'ID_SE%2B%2B'%2C%20how%3D'inner')%0A%0A%20%20%20%20frac_boot_int%20%3D%20%7B%0A%20%20%20%20%20%20%20%20'p_S_int%3E%3D1'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%20%20%20%20'p_C_int%3E%3D1'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%20%20%20%20'p_M_int%3E%3D1'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%20%20%20%20'p_L_int%3E%3D1'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20%23%20Loop%20through%20redshift%20bins%0A%20%20%20%20for%20_zbin%20in%20(zbins%20%3A%3D%20np.arange(1%2C%204%2C%20step_z%20%3A%3D%200.2))%3A%0A%0A%20%20%20%20%20%20%20%20%23%20Select%20galaxies%20in%20the%20given%20redshift%20bin%0A%20%20%20%20%20%20%20%20_mask_z%20%3D%20(_match%5B'LP_zfinal'%5D%20%3E%20_zbin)%20%26%20(_match%5B'LP_zfinal'%5D%20%3C%3D%20_zbin%20%2B%20step_z)%0A%20%20%20%20%20%20%20%20_selec%20%20%3D%20_match%5B_mask_z%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Evaluate%20fractions%20using%20bootstrapping%0A%20%20%20%20%20%20%20%20for%20_metric%20in%20frac_boot_int.keys()%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20_boot%20%3D%20boostrap(1000%2C%20_selec%5B_metric%5D.to_numpy())%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_int%5B_metric%5D%5B'med'%5D.append(np.nanmedian(_boot))%0A%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_int%5B_metric%5D%5B'low'%5D.append(np.nanquantile(_boot%2C%200.16))%0A%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_int%5B_metric%5D%5B'high'%5D.append(np.nanquantile(_boot%2C%200.84))%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20We%20dupplicate%20the%20last%20measured%20value%20to%20have%20the%20plot%20extend%20up%20to%20z%3D4%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20_zbin%20%3D%3D%20zbins%5B-1%5D%3A%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_int%5B_metric%5D%5B'med'%5D.append(frac_boot_int%5B_metric%5D%5B'med'%5D%5B-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_int%5B_metric%5D%5B'low'%5D.append(frac_boot_int%5B_metric%5D%5B'low'%5D%5B-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_int%5B_metric%5D%5B'high'%5D.append(frac_boot_int%5B_metric%5D%5B'high'%5D%5B-1%5D)%0A%0A%20%20%20%20zbins%20%3D%20np.append(zbins%2C%20zbins%5B-1%5D%20%2B%20step_z)%0A%20%20%20%20return%20frac_boot_int%2C%20step_z%2C%20zbins%0A%0A%0A%40app.cell%0Adef%20_(frac_boot_int%2C%20plt%2C%20zbins)%3A%0A%20%20%20%20_f%20%20%3D%20plt.figure(figsize%3D(5%2C%205))%0A%20%20%20%20_ax%20%3D%20_f.add_subplot(111)%0A%20%20%20%20%23ax.tick_params(direction%3D'in'%2C%20right%3DTrue%2C%20top%3DTrue%2C%20labelsize%3D20)%0A%20%20%20%20%0A%20%20%20%20%23%20All%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20frac_boot_int%5B'p_S_int%3E%3D1'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'k'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'All%20substructures'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20frac_boot_int%5B'p_S_int%3E%3D1'%5D%5B'low'%5D%2C%20frac_boot_int%5B'p_S_int%3E%3D1'%5D%5B'high'%5D%2C%20color%3D'k'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20%23%20Clumps%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20frac_boot_int%5B'p_C_int%3E%3D1'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'royalblue'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'Clumps'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20frac_boot_int%5B'p_C_int%3E%3D1'%5D%5B'low'%5D%2C%20frac_boot_int%5B'p_C_int%3E%3D1'%5D%5B'high'%5D%2C%20color%3D'royalblue'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20%23%20Moderately%20large%20substructures%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20frac_boot_int%5B'p_M_int%3E%3D1'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'orange'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'Moderately%20large'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20frac_boot_int%5B'p_M_int%3E%3D1'%5D%5B'low'%5D%2C%20frac_boot_int%5B'p_M_int%3E%3D1'%5D%5B'high'%5D%2C%20color%3D'orange'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20%23%20Extended%20substructures%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20frac_boot_int%5B'p_L_int%3E%3D1'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'plum'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'Extended'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20frac_boot_int%5B'p_L_int%3E%3D1'%5D%5B'low'%5D%2C%20frac_boot_int%5B'p_L_int%3E%3D1'%5D%5B'high'%5D%2C%20color%3D'plum'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20plt.legend(frameon%3DFalse%2C%20fontsize%3D10%2C%20loc%3D'upper%20left')%0A%20%20%20%20plt.xlabel('Redshift'%2C%20size%3D18)%0A%20%20%20%20plt.ylabel(r'Fraction%20of%20galaxies%20with%20substructures%20(%5C%25)'%2C%20size%3D18)%0A%20%20%20%20plt.title(r'Rest-frame%20%241%5C%2C%5Cmu%24m%20%5Ctextbf%7Bintrinsic%20detection%7D'%2C%20size%3D18)%0A%20%20%20%20plt.xlim(1%2C%204)%0A%20%20%20%20plt.ylim(0%2C%2071)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Optimal%20detection%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(boostrap%2C%20cat_cweb%2C%20cat_selection%2C%20np%2C%20step_z)%3A%0A%20%20%20%20%23%20Merge%20catalogue%20of%20selected%20galaxies%20with%20COSMOS%20Web%20catalogue%20to%20have%20the%20redshift%0A%20%20%20%20_match%20%3D%20cat_selection.merge(cat_cweb%2C%20on%3D'ID_SE%2B%2B'%2C%20how%3D'inner')%0A%0A%20%20%20%20frac_boot_opt%20%3D%20%7B%0A%20%20%20%20%20%20%20%20'p_S_opt%3E%3D1'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%20%20%20%20'p_C_opt%3E%3D1'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%20%20%20%20'p_M_opt%3E%3D1'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%20%20%20%20'p_L_opt%3E%3D1'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20%23%20Loop%20through%20redshift%20bins%0A%20%20%20%20for%20_zbin%20in%20(_zbins%20%3A%3D%20np.arange(1%2C%204%2C%20_step_z%20%3A%3D%200.2))%3A%0A%0A%20%20%20%20%20%20%20%20%23%20Select%20galaxies%20in%20the%20given%20redshift%20bin%0A%20%20%20%20%20%20%20%20_mask_z%20%3D%20(_match%5B'LP_zfinal'%5D%20%3E%20_zbin)%20%26%20(_match%5B'LP_zfinal'%5D%20%3C%3D%20_zbin%20%2B%20step_z)%0A%20%20%20%20%20%20%20%20_selec%20%20%3D%20_match%5B_mask_z%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Evaluate%20fractions%20using%20bootstrapping%0A%20%20%20%20%20%20%20%20for%20_metric%20in%20frac_boot_opt.keys()%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20_boot%20%3D%20boostrap(1000%2C%20_selec%5B_metric%5D.to_numpy())%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_opt%5B_metric%5D%5B'med'%5D.append(np.nanmedian(_boot))%0A%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_opt%5B_metric%5D%5B'low'%5D.append(np.nanquantile(_boot%2C%200.16))%0A%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_opt%5B_metric%5D%5B'high'%5D.append(np.nanquantile(_boot%2C%200.84))%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20We%20dupplicate%20the%20last%20measured%20value%20to%20have%20the%20plot%20extend%20up%20to%20z%3D4%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20_zbin%20%3D%3D%20_zbins%5B-1%5D%3A%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_opt%5B_metric%5D%5B'med'%5D.append(frac_boot_opt%5B_metric%5D%5B'med'%5D%5B-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_opt%5B_metric%5D%5B'low'%5D.append(frac_boot_opt%5B_metric%5D%5B'low'%5D%5B-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20frac_boot_opt%5B_metric%5D%5B'high'%5D.append(frac_boot_opt%5B_metric%5D%5B'high'%5D%5B-1%5D)%0A%20%20%20%20return%20(frac_boot_opt%2C)%0A%0A%0A%40app.cell%0Adef%20_(frac_boot_opt%2C%20plt%2C%20zbins)%3A%0A%20%20%20%20_f%20%20%3D%20plt.figure(figsize%3D(5%2C%205))%0A%20%20%20%20_ax%20%3D%20_f.add_subplot(111)%0A%20%20%20%20%23ax.tick_params(direction%3D'in'%2C%20right%3DTrue%2C%20top%3DTrue%2C%20labelsize%3D20)%0A%20%20%20%20%0A%20%20%20%20%23%20All%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20frac_boot_opt%5B'p_S_opt%3E%3D1'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'k'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'All%20substructures'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20frac_boot_opt%5B'p_S_opt%3E%3D1'%5D%5B'low'%5D%2C%20frac_boot_opt%5B'p_S_opt%3E%3D1'%5D%5B'high'%5D%2C%20color%3D'k'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20%23%20Clumps%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20frac_boot_opt%5B'p_C_opt%3E%3D1'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'royalblue'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'Clumps'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20frac_boot_opt%5B'p_C_opt%3E%3D1'%5D%5B'low'%5D%2C%20frac_boot_opt%5B'p_C_opt%3E%3D1'%5D%5B'high'%5D%2C%20color%3D'royalblue'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20%23%20Moderately%20large%20substructures%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20frac_boot_opt%5B'p_M_opt%3E%3D1'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'orange'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'Moderately%20large'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20frac_boot_opt%5B'p_M_opt%3E%3D1'%5D%5B'low'%5D%2C%20frac_boot_opt%5B'p_M_opt%3E%3D1'%5D%5B'high'%5D%2C%20color%3D'orange'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20%23%20Extended%20substructures%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20frac_boot_opt%5B'p_L_opt%3E%3D1'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'plum'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'Extended'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20frac_boot_opt%5B'p_L_opt%3E%3D1'%5D%5B'low'%5D%2C%20frac_boot_opt%5B'p_L_opt%3E%3D1'%5D%5B'high'%5D%2C%20color%3D'plum'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20plt.legend(frameon%3DFalse%2C%20fontsize%3D10%2C%20loc%3D'upper%20left'%2C%20ncols%3D2)%0A%20%20%20%20plt.xlabel('Redshift'%2C%20size%3D18)%0A%20%20%20%20plt.ylabel(r'Fraction%20of%20galaxies%20with%20substructures%20(%5C%25)'%2C%20size%3D18)%0A%20%20%20%20plt.title(r'Rest-frame%20%241%5C%2C%5Cmu%24m%20%5Ctextbf%7Boptimal%20detection%7D'%2C%20size%3D18)%0A%20%20%20%20plt.xlim(1%2C%204)%0A%20%20%20%20plt.ylim(0%2C%20100)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Fraction%20of%20flux%20in%20substructures%20as%20a%20function%20of%20redshift%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20%23%20Bins%20used%20to%20determine%20whether%20a%20substructure%20is%20a%20clump%2C%20moderately%20large%2C%20or%20extended%0A%20%20%20%20abins%20%20%20%20%20%20%3D%2010**np.arange(np.log10(1.27)%2C%202%2C%20step_area%20%3A%3D%200.5)%0A%20%20%20%20abins%0A%20%20%20%20return%20(abins%2C)%0A%0A%0A%40app.cell%0Adef%20_(abins%2C%20cat_cweb%2C%20cat_sub%2C%20np%2C%20pd%2C%20step_z)%3A%0A%20%20%20%20%23%20We%20remove%20galaxies%20with%20optimal%20substructures%20since%20we%20want%20to%20plot%20intrinsic%20substructures%20only%0A%20%20%20%20%23%20We%20match%20to%20the%20CWeb2025%20catalogue%20to%20have%20the%20redshift%0A%20%20%20%20_match%20%3D%20cat_sub%5Bcat_sub%5B'intrinsic_sub'%5D%5D.merge(cat_cweb%2C%20on%3D'ID_SE%2B%2B'%2C%20how%3D'left')%0A%0A%20%20%20%20%23%20Determine%20what%20kind%20of%20substructures%20the%20galaxies%20host%20based%20on%20the%20size%20of%20substructures%0A%20%20%20%20_match%5B'Sub_type'%5D%20%3D%20pd.cut(_match%5B'Area_kpc2'%5D%2C%0A%20%20%20%20%20%20%20%20bins%20%20%20%20%20%20%20%20%20%20%20%3D%20%5Babins%5B0%5D%2C%20abins%5B1%5D%2C%20abins%5B2%5D%2C%20abins%5B3%5D%5D%2C%0A%20%20%20%20%20%20%20%20labels%20%20%20%20%20%20%20%20%20%3D%20%5B'C'%2C%20'M'%2C%20'L'%5D%2C%0A%20%20%20%20%20%20%20%20right%20%20%20%20%20%20%20%20%20%20%3D%20False%2C%0A%20%20%20%20%20%20%20%20include_lowest%20%3D%20True%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Compute%20the%20flux%20of%20substructures%20weighted%20by%20the%20probability%20of%20belonging%20to%20the%20galaxy%0A%20%20%20%20_match%5B'Flux_weighted'%5D%20%3D%20_match%5B'Flux'%5D%20*%20_match%5B'probability'%5D%0A%0A%20%20%20%20%23%20For%20each%20galaxy%2C%20assign%20the%20flux%20of%20the%20entire%20galaxy%20in%20the%20F277W%20band%20if%20z%20%3C%202%2C%20otherwise%20F444W%0A%20%20%20%20_mask_z2%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20_match%5B'LP_zfinal'%5D%20%3C%202%0A%20%20%20%20_match%5B'Flux_gal'%5D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3D%20_match%5B'FLUX_MODEL_F444W'%5D%0A%20%20%20%20_match.loc%5B_mask_z2%2C%20'Flux_gal'%5D%20%3D%20_match%5B'FLUX_MODEL_F277W'%5D%5B_mask_z2%5D%0A%0A%20%20%20%20%23%20Convert%20the%20flux%20of%20the%20host%20galaxy%20from%20cgs%20to%20MJy%2Fsr%0A%20%20%20%20_match%5B'Flux_gal'%5D%20%20%20%20%20%20%20%20%20%20%20%3D%20_match%5B'Flux_gal'%5D%20*%201e23%20*%201e-6%20%2F%202.11590909090909e-14%0A%0A%20%20%20%20flux_fraction%20%3D%20%7B%0A%20%20%20%20%20%20%20%20'S'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%20%20%20%20'C'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%20%20%20%20'M'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%2C%0A%20%20%20%20%20%20%20%20'L'%20%3A%20%7B'med'%20%3A%20%5B%5D%2C%20'low'%20%3A%20%5B%5D%2C%20'high'%20%3A%20%5B%5D%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20%23%20Loop%20through%20redshift%20slices%20to%20get%20the%20flux%20fraction%20in%20substructures%0A%20%20%20%20for%20_zbin%20in%20(_zbins%20%3A%3D%20np.arange(1%2C%204%2C%20_step_z%20%3A%3D%200.2))%3A%0A%0A%20%20%20%20%20%20%20%20%23%20Select%20clumpy%20galaxies%20in%20the%20given%20redshift%20bin%0A%20%20%20%20%20%20%20%20%23%20Group%20by%20galaxies'%20ID%20to%20have%20the%20sum%20of%20flux%20in%20substructures%20per%20galaxy%0A%20%20%20%20%20%20%20%20_mask_z%20%3D%20(_match%5B'LP_zfinal'%5D%20%3E%20_zbin)%20%26%20(_match%5B'LP_zfinal'%5D%20%3C%3D%20_zbin%20%2B%20step_z)%0A%20%20%20%20%20%20%20%20_selec%20%20%3D%20_match%5B_mask_z%5D%0A%20%20%20%20%20%20%20%20_gb%20%20%20%20%20%3D%20_selec%5B%5B'ID_SE%2B%2B'%2C%20'Flux_gal'%2C%20'Flux_weighted'%5D%5D%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.groupby('ID_SE%2B%2B')%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.aggregate(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'Flux_gal'%20%3A%20lambda%20x%3A%20x.iloc%5B0%5D%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'Flux_weighted'%20%3A%20'sum'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20%20%20%20%20%23%20Compute%20the%20fraction%20of%20flux%20(%25)%20in%20substructures%20for%20each%20clumpy%20galaxy%0A%20%20%20%20%20%20%20%20_flux_sum%20%3D%20100%20*%20_gb%5B'Flux_weighted'%5D%20%2F%20(_gb%5B'Flux_weighted'%5D%20%2B%20_gb%5B'Flux_gal'%5D)%0A%0A%20%20%20%20%20%20%20%20flux_fraction%5B'S'%5D%5B'med'%5D.append(np.nanmedian(_flux_sum))%0A%20%20%20%20%20%20%20%20flux_fraction%5B'S'%5D%5B'low'%5D.append(np.nanquantile(_flux_sum%2C%200.16))%0A%20%20%20%20%20%20%20%20flux_fraction%5B'S'%5D%5B'high'%5D.append(np.nanquantile(_flux_sum%2C%200.84))%0A%0A%20%20%20%20%20%20%20%20%23%20Dupplicate%20the%20last%20bin%20for%20plotting%20purposes%0A%20%20%20%20%20%20%20%20if%20_zbin%20%3D%3D%20_zbins%5B-1%5D%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20flux_fraction%5B'S'%5D%5B'med'%5D.append(flux_fraction%5B'S'%5D%5B'med'%5D%5B-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20flux_fraction%5B'S'%5D%5B'low'%5D.append(flux_fraction%5B'S'%5D%5B'low'%5D%5B-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20flux_fraction%5B'S'%5D%5B'high'%5D.append(flux_fraction%5B'S'%5D%5B'high'%5D%5B-1%5D)%0A%0A%20%20%20%20%20%20%20%20%23%20Same%20calculations%20as%20before%20but%20targeting%20clumps%2C%20moderately%20large%20substructures%2C%20or%20extended%20ones%0A%20%20%20%20%20%20%20%20for%20_sub_type%20in%20flux_fraction.keys()%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20_sub_type%20%3D%3D%20'S'%3A%20continue%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20_selec%20%20%3D%20_match%5B_mask_z%20%26%20(_match%5B'Sub_type'%5D%20%3D%3D%20_sub_type)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_gb%20%20%20%20%20%3D%20_selec%5B%5B'ID_SE%2B%2B'%2C%20'Flux_gal'%2C%20'Flux_weighted'%5D%5D%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.groupby('ID_SE%2B%2B')%5C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.aggregate(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'Flux_gal'%20%3A%20lambda%20x%3A%20x.iloc%5B0%5D%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'Flux_weighted'%20%3A%20'sum'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20_flux_sum%20%3D%20100%20*%20_gb%5B'Flux_weighted'%5D%20%2F%20(_gb%5B'Flux_weighted'%5D%20%2B%20_gb%5B'Flux_gal'%5D)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20flux_fraction%5B_sub_type%5D%5B'med'%5D.append(np.nanmedian(_flux_sum))%0A%20%20%20%20%20%20%20%20%20%20%20%20flux_fraction%5B_sub_type%5D%5B'low'%5D.append(np.nanquantile(_flux_sum%2C%200.16))%0A%20%20%20%20%20%20%20%20%20%20%20%20flux_fraction%5B_sub_type%5D%5B'high'%5D.append(np.nanquantile(_flux_sum%2C%200.84))%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20_zbin%20%3D%3D%20_zbins%5B-1%5D%3A%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20flux_fraction%5B_sub_type%5D%5B'med'%5D.append(flux_fraction%5B_sub_type%5D%5B'med'%5D%5B-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20flux_fraction%5B_sub_type%5D%5B'low'%5D.append(flux_fraction%5B_sub_type%5D%5B'low'%5D%5B-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20flux_fraction%5B_sub_type%5D%5B'high'%5D.append(flux_fraction%5B_sub_type%5D%5B'high'%5D%5B-1%5D)%0A%20%20%20%20return%20(flux_fraction%2C)%0A%0A%0A%40app.cell%0Adef%20_(flux_fraction%2C%20plt%2C%20zbins)%3A%0A%20%20%20%20_f%20%20%3D%20plt.figure(figsize%3D(5%2C%205))%0A%20%20%20%20_ax%20%3D%20_f.add_subplot(111)%0A%20%20%20%20%0A%20%20%20%20%23%20All%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20flux_fraction%5B'S'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'k'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'All%20substructures'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20flux_fraction%5B'S'%5D%5B'low'%5D%2C%20flux_fraction%5B'S'%5D%5B'high'%5D%2C%20color%3D'k'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20%23%20Clumps%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20flux_fraction%5B'C'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'royalblue'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'Clumps'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20flux_fraction%5B'C'%5D%5B'low'%5D%2C%20flux_fraction%5B'C'%5D%5B'high'%5D%2C%20color%3D'royalblue'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20%23%20Moderately%20large%20substructures%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20flux_fraction%5B'M'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'orange'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'Moderately%20large'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20flux_fraction%5B'M'%5D%5B'low'%5D%2C%20flux_fraction%5B'M'%5D%5B'high'%5D%2C%20color%3D'orange'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20%23%20Extended%20substructures%0A%20%20%20%20plt.plot(%0A%20%20%20%20%20%20%20%20zbins%2C%20flux_fraction%5B'L'%5D%5B'med'%5D%2C%20%0A%20%20%20%20%20%20%20%20color%20%3D%20'plum'%2C%20drawstyle%20%3D%20'steps-post'%2C%0A%20%20%20%20%20%20%20%20ls%20%3D%20'-'%2C%20label%20%3D%20'Extended'%0A%20%20%20%20)%0A%0A%20%20%20%20plt.fill_between(zbins%2C%20flux_fraction%5B'L'%5D%5B'low'%5D%2C%20flux_fraction%5B'L'%5D%5B'high'%5D%2C%20color%3D'plum'%2C%20alpha%3D0.2%2C%20step%3D'post'%2C%20edgecolor%3D'none')%0A%0A%20%20%20%20plt.legend(frameon%3DFalse%2C%20fontsize%3D10%2C%20loc%3D'upper%20left'%2C%20ncol%3D2)%0A%20%20%20%20plt.xlabel('Redshift'%2C%20size%3D18)%0A%20%20%20%20plt.ylabel('Total%20fraction%20of%20flux%20in%20substructures%20(%5C%25)'%2C%20size%3D18)%0A%20%20%20%20plt.title('Flux%20in%20substructures%20(intrinsic%20detection)'%2C%20size%3D18)%0A%20%20%20%20plt.xlim(1%2C%204)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
f25737fa8fdb76de65826ae283539dfc338d6de68920a6d798d3e0c1cf13e2e2