{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating simulations using `deepszsim`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tutorial provides instructions on how to create galaxy cluster simulations with `deepszsim`. For a step-by-step guide to the functions included in the simulation, see `demo_full_pipeline.ipynb`" ] }, { "cell_type": "markdown", "metadata": { "id": "lOkPyRZEQWsm" }, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "lOkPyRZEQWsm" }, "outputs": [], "source": [ "from deepszsim import make_sz_cluster, dm_halo_dist, visualization, simclusters\n", "\n", "import time\n", "import h5py" ] }, { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "## Creating simulations by hand" ] }, { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "Create a new flat redshift and virial mass distribution using the function `flastdist_halo` from `dm_halo_dist` which uses random uniform generation. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [], "source": [ "#Generate a new flat z, Mvir distribution and save to file: \n", "nsources=10 #Number of halos to generate\n", "zdist,mdist=dm_halo_dist.flatdist_halo(0.01,1.1,1e14,2e15,nsources) #Generate a flat z, Mvir distribution for sims" ] }, { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "Save our simulated data to a h5 file titled `massdist.h5`. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "sourceid=int(time.time()) #Create an initial ctime for the halo ID list to save catalog\n", "idlist=[sourceid+x for x in range(len(zdist))] #Create the halo ID list for catalog\n", "\n", "#Save this array to a h5 file\n", "with h5py.File('massdist.h5', 'w') as data:\n", " data.create_dataset('Redshift', data=zdist)\n", " data.create_dataset('Mass', data=mdist)\n", " data.create_dataset('id', data=idlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now simulate submaps using our mass and redshift distribution using the `simulate_T_submaps` function from `make_sz_cluster`. This function returns an array of dicts with the following attributes (each dict contains the full information of each sim/cluster):\n", "- M200\n", "- R200\n", "- redshift_z\n", "- y_central\n", "- ID\n", "- cmb_map\n", "- noise_map\n", "- final_map" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "clusters = simclusters.simulate_clusters(mdist, zdist)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(clusters.id_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating simulations automatically" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternately, we can do everything from soup to nuts with the `simulate_clusters` class" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/sammcd/micromamba/envs/szsims/lib/python3.11/site-packages/deepszsim/filters.py:38: RuntimeWarning: Mean of empty slice.\n", " ring_mean = dT_map[(r >= radmax_pixels) & (r < radius_out_pixels)].mean()\n", "/Users/sammcd/micromamba/envs/szsims/lib/python3.11/site-packages/numpy/core/_methods.py:129: RuntimeWarning: invalid value encountered in scalar divide\n", " ret = ret.dtype.type(ret / rcount)\n" ] } ], "source": [ "_ = clusters.get_T_maps()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This class instance populates an attribute named `clusters`, which is a dictionary. This dictionary is indexed by a run ID, and each of its values is itself a dictionary, which in turn contains two more dictionaries: `params` and `maps`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['0822711_021_556314',\n", " '0552296_070_281581',\n", " '1372133_084_806690',\n", " '1371424_008_408584',\n", " '1998618_075_449070',\n", " '0198589_065_786397',\n", " '1525903_108_312178',\n", " '1124602_018_031953',\n", " '1846569_059_568057',\n", " '0517993_041_352332']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k10 = list(clusters.clusters.keys())\n", "k10" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "({'M200': 822711781329317.2,\n", " 'redshift_z': 0.2176700775626845,\n", " 'R200': 1.8181656839508509,\n", " 'angsize_arcmin': 5.5262467021573425,\n", " 'angsize500_arcmin': 5.5262467021573425,\n", " 'image_size_pixels': 41,\n", " 'dT_central': -242.36963689525484,\n", " 'ap': -45.38114063656835},\n", " dict_keys(['conv_map', 'CMB_map', 'signal_map', 'beamsig_map', 'final_map']))" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clusters.clusters[k10[0]]['params'], clusters.clusters[k10[0]]['maps'].keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "this class instance has a `save_map` method that can save individual maps as specified, or all clusters into a single nested file with `nest_h5 = True`, or all clusters individually" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# c10.save_map()" ] } ], "metadata": { "anaconda-cloud": {}, "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.10" } }, "nbformat": 4, "nbformat_minor": 4 }