{ "cells": [ { "cell_type": "markdown", "id": "2148ff56-339e-4831-bcb3-03a36c4738ed", "metadata": {}, "source": [ "# Extended source sensitivity calculator\n", "\n", "This notebook shows how to compute a extended source sensitivity for a specific model. In the current status of cosipy, we are limited by the available responses, i.e at 511, 1157, 1173, 1333 and 1809 keV and a coarse continuum. Keep in mind that due to the coarseness of the current responses, the sensitivity might be underestimated. \n", "\n", "The collaboration is currently developping a neural network approach for the response to use with an unbinned analysis. This will allow us to fully exploit COSI power. " ] }, { "cell_type": "code", "execution_count": 1, "id": "2d780467-ec91-481b-90a8-353f9db61ac8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
15:21:22 WARNING The naima package is not available. Models that depend on it will not be functions.py:43\n", " available \n", "\n" ], "text/plain": [ "\u001b[38;5;46m15:21:22\u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m The naima package is not available. Models that depend on it will not be \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=38245;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/functions/functions_1D/functions.py\u001b\\\u001b[2mfunctions.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=820159;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/functions/functions_1D/functions.py#43\u001b\\\u001b[2m43\u001b[0m\u001b]8;;\u001b\\\n", "\u001b[38;5;46m \u001b[0m \u001b[1;38;5;251mavailable \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b[2m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it functions.py:65\n", " will not be available. \n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m The GSL library or the pygsl wrapper cannot be loaded. Models that depend on it \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=167690;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/functions/functions_1D/functions.py\u001b\\\u001b[2mfunctions.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=307444;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/functions/functions_1D/functions.py#65\u001b\\\u001b[2m65\u001b[0m\u001b]8;;\u001b\\\n", "\u001b[38;5;46m \u001b[0m \u001b[1;38;5;251mwill not be available. \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b[2m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING The ebltable package is not available. Models that depend on it will not be absorption.py:33\n", " available \n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m The ebltable package is not available. Models that depend on it will not be \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=94778;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/functions/functions_1D/absorption.py\u001b\\\u001b[2mabsorption.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=555992;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/functions/functions_1D/absorption.py#33\u001b\\\u001b[2m33\u001b[0m\u001b]8;;\u001b\\\n", "\u001b[38;5;46m \u001b[0m \u001b[1;38;5;251mavailable \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b[2m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
15:21:23 INFO Starting 3ML! __init__.py:44\n", "\n" ], "text/plain": [ "\u001b[38;5;46m15:21:23\u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;49mINFO \u001b[0m \u001b[1;38;5;251m Starting 3ML! \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=488426;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py\u001b\\\u001b[2m__init__.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=110256;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py#44\u001b\\\u001b[2m44\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING WARNINGs here are NOT errors __init__.py:45\n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m WARNINGs here are \u001b[0m\u001b[1;31mNOT\u001b[0m\u001b[1;38;5;251m errors \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=250620;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py\u001b\\\u001b[2m__init__.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=335980;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py#45\u001b\\\u001b[2m45\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING but are inform you about optional packages that can be installed __init__.py:46\n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m but are inform you about optional packages that can be installed \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=119419;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py\u001b\\\u001b[2m__init__.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=882949;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py#46\u001b\\\u001b[2m46\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING to disable these messages, turn off start_warning in your config file __init__.py:47\n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m \u001b[0m\u001b[1;31m to disable these messages, turn off start_warning in your config file\u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=470243;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py\u001b\\\u001b[2m__init__.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=750564;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py#47\u001b\\\u001b[2m47\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING no display variable set. using backend for graphics without display (agg) __init__.py:53\n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m no display variable set. using backend for graphics without display \u001b[0m\u001b[1;38;5;251m(\u001b[0m\u001b[1;38;5;251magg\u001b[0m\u001b[1;38;5;251m)\u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=889165;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py\u001b\\\u001b[2m__init__.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=80745;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py#53\u001b\\\u001b[2m53\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING ROOT minimizer not available minimization.py:1208\n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m ROOT minimizer not available \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=500782;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/minimizer/minimization.py\u001b\\\u001b[2mminimization.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=17242;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/minimizer/minimization.py#1208\u001b\\\u001b[2m1208\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING Multinest minimizer not available minimization.py:1218\n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m Multinest minimizer not available \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=365338;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/minimizer/minimization.py\u001b\\\u001b[2mminimization.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=530513;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/minimizer/minimization.py#1218\u001b\\\u001b[2m1218\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING PyGMO is not available minimization.py:1228\n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m PyGMO is not available \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=792143;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/minimizer/minimization.py\u001b\\\u001b[2mminimization.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=484923;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/minimizer/minimization.py#1228\u001b\\\u001b[2m1228\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
15:21:24 WARNING Could not import plugin FermiLATLike.py. Do you have the relative instrument __init__.py:126\n", " software installed and configured? \n", "\n" ], "text/plain": [ "\u001b[38;5;46m15:21:24\u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m Could not import plugin FermiLATLike.py. Do you have the relative instrument \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=195716;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py\u001b\\\u001b[2m__init__.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=861023;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/__init__.py#126\u001b\\\u001b[2m126\u001b[0m\u001b]8;;\u001b\\\n", "\u001b[38;5;46m \u001b[0m \u001b[1;38;5;251msoftware installed and configured? \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b[2m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
WARNING No fermitools installed lat_transient_builder.py:44\n", "\n" ], "text/plain": [ "\u001b[38;5;46m \u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m No fermitools installed \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=339303;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/utils/data_builders/fermi/lat_transient_builder.py\u001b\\\u001b[2mlat_transient_builder.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=2719;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/utils/data_builders/fermi/lat_transient_builder.py#44\u001b\\\u001b[2m44\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "ename": "ImportError", "evalue": "cannot import name 'SpatialTemplate_2D_Healpix' from 'cosipy.threeml.custom_functions' (/uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/cosipy/threeml/custom_functions.py)", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mImportError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 27\u001b[39m\n\u001b[32m 25\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mcosipy\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mdata_io\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m EmCDSBinnedData\n\u001b[32m 26\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mcosipy\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01msource_injector\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m SourceInjector\n\u001b[32m---> \u001b[39m\u001b[32m27\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mcosipy\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mthreeml\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mcustom_functions\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m SpatialTemplate_2D_Healpix\n\u001b[32m 28\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mhistpy\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Histogram\n\u001b[32m 29\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmhealpy\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m HealpixMap\n", "\u001b[31mImportError\u001b[39m: cannot import name 'SpatialTemplate_2D_Healpix' from 'cosipy.threeml.custom_functions' (/uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/cosipy/threeml/custom_functions.py)" ] } ], "source": [ "import os\n", "import logging\n", "import sys\n", "\n", "\n", "\n", "logger = logging.getLogger('cosipy')\n", "logger.setLevel(logging.INFO)\n", "logger.addHandler(logging.StreamHandler(sys.stdout))\n", "\n", "os.environ[\"OMP_NUM_THREADS\"] = \"1\"\n", "os.environ[\"MKL_NUM_THREADS\"] = \"1\"\n", "os.environ[\"NUMEXPR_NUM_THREADS\"] = \"1\"\n", "\n", "\n", "from cosipy.spacecraftfile import SpacecraftHistory\n", "from cosipy.response.FullDetectorResponse import FullDetectorResponse\n", "from cosipy.response import PointSourceResponse,ExtendedSourceResponse\n", "from cosipy.util import fetch_wasabi_file\n", "\n", "from cosipy.statistics import PoissonLikelihood\n", "from cosipy.background_estimation import FreeNormBinnedBackground\n", "from cosipy.interfaces import ThreeMLPluginInterface\n", "from cosipy.response import BinnedThreeMLModelFolding, BinnedInstrumentResponse, BinnedThreeMLExtendedSourceResponse\n", "from cosipy.data_io import EmCDSBinnedData\n", "from cosipy.source_injector import SourceInjector\n", "from cosipy.threeml.custom_functions import SpatialTemplate_2D_Healpix\n", "from histpy import Histogram\n", "from mhealpy import HealpixMap\n", "\n", "from scoords import SpacecraftFrame\n", "\n", "from astropy.time import Time\n", "import astropy.units as u\n", "from astropy.coordinates import SkyCoord, Galactic\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "from threeML import *\n", "from astromodels import *\n", "\n", "from pathlib import Path\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "id": "dbe965ab-12fb-40a8-82a5-a4a19510b6e0", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "## Download the data" ] }, { "cell_type": "code", "execution_count": 3, "id": "6b1a4a9d-78cd-4f46-9329-baf31f2c6349", "metadata": {}, "outputs": [], "source": [ "data_path = Path(\"\") # /path/to/files. Current dir by default" ] }, { "cell_type": "code", "execution_count": null, "id": "d5b68117-177b-4553-b62e-361bd7d456cd", "metadata": {}, "outputs": [], "source": [ "#Download the Orientation file\n", "fetch_wasabi_file('COSI-SMEX/develop/Data/Orientation/DC3_final_530km_3_month_with_slew_15sbins_GalacticEarth_SAA.fits', checksum = 'e86df2407eb052cf0c1db4a8e7598727')" ] }, { "cell_type": "code", "execution_count": 3, "id": "7d720757-2f47-471e-baa7-ebf9495a2477", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading cosi-pipeline-public/COSI-SMEX/DC3/Data/Responses/extended_source_response/extended_source_response_Al26_merged.h5\n" ] }, { "ename": "ClientError", "evalue": "An error occurred (404) when calling the HeadObject operation: Not Found", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mClientError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 17\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m#Download the extended sourde response\u001b[39;00m\n\u001b[32m 2\u001b[39m \u001b[38;5;66;03m#Choose the response depending on your model\u001b[39;00m\n\u001b[32m 3\u001b[39m \n\u001b[32m (...)\u001b[39m\u001b[32m 15\u001b[39m \n\u001b[32m 16\u001b[39m \u001b[38;5;66;03m#Model for a 1809 keV line\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m17\u001b[39m \u001b[43mfetch_wasabi_file\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mCOSI-SMEX/DC3/Data/Responses/extended_source_response/extended_source_response_Al26_merged.h5\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43mchecksum\u001b[49m\u001b[43m \u001b[49m\u001b[43m=\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m218d088159c6e11d0ddecc03ca9fa399\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m 20\u001b[39m \u001b[38;5;66;03m#Model for an other line or continuum source\u001b[39;00m\n\u001b[32m 21\u001b[39m \u001b[38;5;66;03m#fetch_wasabi_file('COSI-SMEX/DC3/Data/Responses/extended_source_response/extended_source_response_continuum_merged.h5',checksum = '16fe005d3ab924ad98322b6579aabf2a')\u001b[39;00m\n", "\u001b[36mFile \u001b[39m\u001b[32m~/software/COSItools/cosipy_interfaces/cosipy/util/data_fetching.py:245\u001b[39m, in \u001b[36mfetch_wasabi_file\u001b[39m\u001b[34m(file, output, overwrite, unzip, unzip_output, checksum, bucket, endpoint, access_key, secret_key)\u001b[39m\n\u001b[32m 241\u001b[39m \u001b[38;5;66;03m# We'll get here only if:\u001b[39;00m\n\u001b[32m 242\u001b[39m \u001b[38;5;66;03m# - The output file doesn't exist\u001b[39;00m\n\u001b[32m 243\u001b[39m \u001b[38;5;66;03m# - The output file exists, has the wrong checksum/ETag, and overwrite = True\u001b[39;00m\n\u001b[32m 244\u001b[39m logger.info(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mDownloading \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbucket\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n\u001b[32m--> \u001b[39m\u001b[32m245\u001b[39m \u001b[43ms3\u001b[49m\u001b[43m.\u001b[49m\u001b[43mdownload_file\u001b[49m\u001b[43m(\u001b[49m\u001b[43mBucket\u001b[49m\u001b[43m=\u001b[49m\u001b[43mbucket\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mKey\u001b[49m\u001b[43m=\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mFilename\u001b[49m\u001b[43m=\u001b[49m\u001b[43moutput\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[36mFile \u001b[39m\u001b[32m~/software/COSItools/cosipy_interfaces/Pyenv/lib/python3.11/site-packages/botocore/context.py:123\u001b[39m, in \u001b[36mwith_current_context.
13:45:49 WARNING External parameter Background already exist in the model. Overwriting it... model.py:593\n", "\n" ], "text/plain": [ "\u001b[38;5;46m13:45:49\u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m External parameter Background already exist in the model. Overwriting it\u001b[0m\u001b[1;33m...\u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=476485;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/core/model.py\u001b\\\u001b[2mmodel.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=959805;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/core/model.py#593\u001b\\\u001b[2m593\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
13:45:49 INFO set the minimizer to minuit joint_likelihood.py:1017\n", "\n" ], "text/plain": [ "\u001b[38;5;46m13:45:49\u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;49mINFO \u001b[0m \u001b[1;38;5;251m set the minimizer to minuit \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=990961;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/classicMLE/joint_likelihood.py\u001b\\\u001b[2mjoint_likelihood.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=789203;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/classicMLE/joint_likelihood.py#1017\u001b\\\u001b[2m1017\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "ITERATION 1\n" ] }, { "data": { "text/html": [ "
13:46:50 WARNING External parameter Background already exist in the model. Overwriting it... model.py:593\n", "\n" ], "text/plain": [ "\u001b[38;5;46m13:46:50\u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m External parameter Background already exist in the model. Overwriting it\u001b[0m\u001b[1;33m...\u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=116475;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/core/model.py\u001b\\\u001b[2mmodel.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=582445;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/core/model.py#593\u001b\\\u001b[2m593\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
13:46:50 INFO set the minimizer to minuit joint_likelihood.py:1017\n", "\n" ], "text/plain": [ "\u001b[38;5;46m13:46:50\u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;49mINFO \u001b[0m \u001b[1;38;5;251m set the minimizer to minuit \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=745403;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/classicMLE/joint_likelihood.py\u001b\\\u001b[2mjoint_likelihood.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=94671;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/classicMLE/joint_likelihood.py#1017\u001b\\\u001b[2m1017\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "ITERATION 2\n" ] }, { "data": { "text/html": [ "
13:47:52 WARNING External parameter Background already exist in the model. Overwriting it... model.py:593\n", "\n" ], "text/plain": [ "\u001b[38;5;46m13:47:52\u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;134mWARNING \u001b[0m \u001b[1;38;5;251m External parameter Background already exist in the model. Overwriting it\u001b[0m\u001b[1;33m...\u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=135532;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/core/model.py\u001b\\\u001b[2mmodel.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=773056;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/astromodels/core/model.py#593\u001b\\\u001b[2m593\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
13:47:52 INFO set the minimizer to minuit joint_likelihood.py:1017\n", "\n" ], "text/plain": [ "\u001b[38;5;46m13:47:52\u001b[0m\u001b[38;5;46m \u001b[0m\u001b[38;5;49mINFO \u001b[0m \u001b[1;38;5;251m set the minimizer to minuit \u001b[0m\u001b[1;38;5;251m \u001b[0m\u001b]8;id=164682;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/classicMLE/joint_likelihood.py\u001b\\\u001b[2mjoint_likelihood.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=481407;file:///uni-mainz.de/homes/sgallego/software/COSItools/cosipy_DC4/Pyenv_DC4/lib/python3.13/site-packages/threeML/classicMLE/joint_likelihood.py#1017\u001b\\\u001b[2m1017\u001b[0m\u001b]8;;\u001b\\\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "# We start with 100 but higher would be better\n", "# To adjust depending on how fast the LH fit is\n", "# You can also run a script in parallel and save the likelihood ratio test instead of using a for loop\n", "\n", "nbiter = 100 # put this number to 100\n", "\n", "LHratiotest = []\n", "\n", "for i in range(nbiter) :\n", "\n", " print(f\"ITERATION {i}\")\n", " \n", " #create a signal dataset with the source injector\n", " spectrum.F.value = F.value\n", " spectrum.F.free = True\n", " signal = injector.inject_model(model = model, make_spectrum_plot = False ,data_save_path = None)#,fluctuate=True)\n", "\n", " \n", " #create a bkg dataset from the bkg model\n", " bkg_poissonsamp = Histogram(bg_model.axes)\n", " bkg_poissonsamp[:] += np.random.poisson(lam = bg_model.to_dense()[:])\n", "\n", " # Set background parameter, which is used to fit the amplitude of the background, and instantiate the COSI 3ML plugin\n", " # here we define only one bkg model but we could add more\n", " bkg_dist = {\"Background\":bg_model.project('Em', 'Phi', 'PsiChi')}\n", " \n", " # Workaround to avoid inf values. Out bkg should be smooth, but currently it's not.\n", " for bckfile in bkg_dist.keys() :\n", " bkg_dist[bckfile] += sys.float_info.min\n", "\n", " #combine the data + the bck like we would get for real data\n", " newdata = EmCDSBinnedData(signal.project('Em', 'Phi', 'PsiChi') \n", " + bkg_poissonsamp.project('Em', 'Phi', 'PsiChi')\n", " )\n", " bkg = FreeNormBinnedBackground(bkg_dist,\n", " sc_history=sc_orientation,\n", " copy = True)\n", "\n", " #change the data of the extended source response\n", " esr._data = newdata\n", " \n", "\n", " ##====\n", "\n", "\n", "\n", " like_fun = PoissonLikelihood(newdata, response, bkg)\n", "\n", " cosi = ThreeMLPluginInterface('cosi',\n", " like_fun,\n", " response,\n", " bkg)\n", "\n", " # Nuisance parameter guess, bounds, etc.\n", " cosi.bkg_parameter['Background'] = Parameter(\"Background\", # background parameter\n", " 1, # initial value of parameter\n", " min_value=0, # minimum value of parameter\n", " max_value=10, # maximum value of parameter\n", " delta=0.05, # initial step used by fitting engine\n", " unit = u.Hz\n", " )\n", " \n", " # Set the source Model\n", " cosi.set_model(model)\n", "\n", "\n", " plugins = DataList(cosi) # If we had multiple instruments, we would do e.g. DataList(cosi, lat, hawc, ...)\n", "\n", " like = JointLikelihood(model, plugins, verbose = False)\n", "\n", " #do the fit\n", " like.fit(compute_covariance = False,quiet=True)\n", "\n", " #get L1\n", " L1 = cosi.get_log_like()\n", "\n", " spectrum.F.value = 0 #null hypothesis (no signal)\n", " spectrum.F.free = False\n", " \n", " #do the fit again\n", " like.fit(compute_covariance = False,quiet=True)\n", " \n", " #Get L0\n", " L0 = cosi.get_log_like()\n", " \n", " #save LH ratio test\n", " LHratiotest.append( -2 * (L0-L1) )\n", "\n" ] }, { "cell_type": "markdown", "id": "77f74ceb-cd5f-4c8d-8653-e40d2be0ecb0", "metadata": {}, "source": [ "## Plot the LH ratio test\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "0a064e2e-fa2a-43d3-a49a-f0f52c818608", "metadata": {}, "source": [ "When testing high flux value for your model, some \"islands\" in the TS distribution could appears (peaks with very high TS). This happens when a signal event lands in a bin where the estimated background was originally 0, and then filled with an arbitrary floor value. This is not a problem with the sensitivity estimation algorithm as this doesn't affect significantly the median of a TS distribution of a flux near threshold, so the estimated sensitivity is still sound. \n", "See this issue for more informations : https://github.com/cositools/cosipy/issues/598" ] }, { "cell_type": "code", "execution_count": 13, "id": "02a9f721-b864-453a-8935-32e461b09830", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "COSI is 3 sigma sensitive to 3e-05 ph/cm2/s. You can continue to the next cell\n" ] } ], "source": [ "if np.median(LHratiotest) <= 9 :\n", " print(f\"COSI is not sensitive to this model with a flux of {F.value} ph/cm2/s. Please try a higher flux\")\n", "else :\n", " print(f\"COSI is 3 sigma sensitive to {F.value} ph/cm2/s. You can continue to the next cell\")\n", "\n", "fig,ax = plt.subplots()\n", "ax.hist(LHratiotest,bins=30)\n", "ax.axvline(np.median(LHratiotest),label=f\"median {np.median(LHratiotest):.3f}\",color=\"r\",linestyle=\"--\")\n", "ax.legend()\n", "ax.set_xlabel(\"LH ratio test\")\n", "ax.set_ylabel(\"counts\")\n", "ax.set_title(f\"Model - flux {F.value} ph/cm2/s\")\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "155e30d4-cbc2-4fc0-92bd-03d20339f1f0", "metadata": {}, "source": [ "## Extrapolate the sensitivity to 2 years\n", "\n", "DC4 simulations only includes 3 months of orbit. To get the sensitivity after 2 years (nominal life time of COSI) as a first approximation we can scale the flux by $\\frac{1}{\\sqrt{T}}$ " ] }, { "cell_type": "code", "execution_count": 14, "id": "fbf1883a-1633-4986-9dcc-fd17d021e041", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 sigma sensitivity after 2 years of COSI observation : 1.0606601717798212e-05\n" ] } ], "source": [ "# 3 * 8 = 24 months = 2 years \n", "\n", "sensi_2y = F.value/np.sqrt(8)\n", "\n", "print(f\"3 sigma sensitivity after 2 years of COSI observation : {sensi_2y}\")\n" ] } ], "metadata": { "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.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }