monte_carlo.py 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
from __future__ import absolute_import, division, print_function

from pathlib import Path
import os
import numpy as np
import matplotlib.pyplot as plt

from multiprocessing import Pool, cpu_count
from functools import partial

from astropy import units as u
from astropy.io import ascii
from astropy.wcs import WCS
from astropy.utils.console import ProgressBar
from astropy.table import vstack, Table

from scipy.optimize import curve_fit

from nikamap import NikaMap, Jackknife
from nikamap.utils import pos_uniform
from astropy.io import fits
22
from time import clock
23
import argparse
24 25
import sys
import datetime
26 27 28 29 30 31 32 33 34 35 36 37
'''
%load_ext autoreload
%autoreload 2
%matplotlib tk
'''

plt.ion()




def fake_worker(jkiter, min_threshold=2, nsources=8**2, flux=1*u.Jy,
LUSTIG Peter's avatar
LUSTIG Peter committed
38 39
                within=(0, 1), cat_gen=pos_uniform, parity_threshold=1,
                **kwargs):
40 41 42 43 44 45 46 47 48 49 50 51
    """The completness purity worker, create a fake dataset from an jackknifed
       image and return catalogs

    Parameters
    ----------
    img : :class:`nikamap.NikaMap`
        Jackknifed dataset
    min_threshold : float
        minimum threshold for the detection
    **kwargs :
        arguments for source injection
    """
LUSTIG Peter's avatar
LUSTIG Peter committed
52
    img = jkiter(parity_threshold)
53 54 55 56 57 58
    # img = IMAGE
    # Renormalize the stddev
    std = img.check_SNR()
    img.uncertainty.array *= std

    # Actually rather slow... maybe check the code ?
LUSTIG Peter's avatar
LUSTIG Peter committed
59
    # print(flux)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    img.add_gaussian_sources(nsources=nsources, within=within, peak_flux=flux,
                             cat_gen=cat_gen, **kwargs)

    # ... match filter it ...
    mf_img = img.match_filter(img.beam)
    std = mf_img.check_SNR()
    # print(std)
    mf_img.uncertainty.array *= std
    # print(mf_img.wcs)

    # ... and detect sources with the lowest threshold...
    # The gaussian fit from subpixel=True is very slow here...
    mf_img.detect_sources(threshold=min_threshold)

    return mf_img.sources, mf_img.fake_sources


plt.close('all')


LUSTIG Peter's avatar
LUSTIG Peter committed
80
# DATA_DIR_SERVER = "/data/NIKA/Reduced/G2_COMMON_MODE_ONE_BLOCK/v_1/"
81 82 83 84
DATA_DIR_SERVER = Path("/data/NIKA/Reduced/"
                       "HLS091828_common_mode_one_block/v_1")
DATA_DIR_MYPC = Path("/home/peter/Dokumente/Uni/Paris/Stage/data/v_1")
'''
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--raw",
                    action="store",
                    dest="rawdirectory", default="mypc",
                    help="Path of Raw File Directory or Key.")
parser.add_argument("-t", "--threshold",
                    action="store",
                    dest="threshold", type=float, default=3.,
                    help="SNR Detection Threshold")
parser.add_argument("-n", "--nsim", action="store",
                    dest="nsim", type=int, default=4,
                    help="Number of Simulations")
parser.add_argument("-c", "--cores", action="store",
                    dest="cores", type=int, default=1,
                    help="Number of Cores")
parser.add_argument("-f", "--flux", action="store",
                    dest="flux", type=float, default=10.,
                    help="Flux of the Created Sources in mJy")
parser.add_argument("-s", "--sources", action="store",
                    dest="nsources", type=int, default=64,
                    help="Number of Sources Injected per Simulation")

107 108 109 110 111

# parser.add_argument("-m", "--method", action="store",
#                     dest="method", default="unknown",
#                     help="Name of the Reduction Method for Outfile Header")

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
parser.add_argument("-o", "--outfile", action="store",
                    dest="outfile", default=None,
                    help=("Output File Name. If not specified auto-generated"
                          "from parameters"))
parser.add_argument("-d", "--outdir", action="store",
                    dest="outdir", default="montecarlo_results/",
                    help="Output directory. Must exist.")


args = parser.parse_args()
raw = args.rawdirectory
nsim = args.nsim
ncores = args.cores
flux = args.flux * u.mJy
# method = args.method
outfile = args.outfile
outdir = args.outdir
nsources = args.nsources
min_detection_threshold = args.threshold

if raw == "mypc":
    DATA_DIR = DATA_DIR_MYPC
elif raw == "server":
    DATA_DIR = DATA_DIR_SERVER
else:
137
    DATA_DIR = Path(raw)
138 139 140 141 142 143 144 145 146

if outfile is None:
    outfile = ('flux{}mJy_thresh{}_nsim{}.fits'
               .format(flux.to_value(unit=u.mJy),
                       min_detection_threshold,
                       nsim))

outfile = outdir + outfile

147 148
'''

149 150 151 152 153 154 155 156
'''
IMAGE = NikaMap.read(('/home/peter/Dokumente/Uni/Paris/'
                      'Stage/N2CLS/ForMarseille/'
                      'HLS091828_common_mode_kids_out/map_JK.fits'))
IMAGE.plot()
plt.show(block=True)
print(IMAGE)
'''
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

flux = np.geomspace(1, 10, 3) * u.mJy
nsim = 2
min_detection_threshold = 5
nsources = 5
outdir = Path("montecarlo_results/")
outdir = Path("testdir")
ncores = 2

timeprefix = '{date:%y%m%d_%H%M%S}_'.format(date=datetime.datetime.now())

if not outdir.exists():
    print('creating directory {}'.format(outdir))
    outdir.mkdir()


# print(flux)
if DATA_DIR_SERVER.exists():
    DATA_DIR = DATA_DIR_SERVER
elif DATA_DIR_MYPC.exists():
    DATA_DIR = DATA_DIR_MYPC
else:
    sys.exit("Raw data path not found. Exit.")


jk_filenames = list(Path(DATA_DIR).glob('*/map.fits'))
for i in range(len(jk_filenames)):
    jk_filenames[i] = str(jk_filenames[i])

186 187 188 189
message = '''\
###############################################################
#                  Running Monte Carlo Tests                  #
#                                                             #
190
#          No Simulations per flux:    {:5d}                  #
191 192
#          Number of CPUs used:        {:5d}                  #
#          Minimum Detection Threshold:{:5.1f}                  #
193
#          No Different Fluxes:        {:5d}                  #
194 195
#          Number of Injected Sources: {:5d}                  #
###############################################################
196
'''.format(nsim, ncores, min_detection_threshold, len(flux), nsources)
197 198


199 200 201
print(message, "\n")
print("Creating Jackkife Object")
t0 = clock()
202
jk_iter = Jackknife(jk_filenames, n=nsim)
203 204
nm = jk_iter()
min_source_dist = 2 * nm.beam.fwhm_pix.value
205

206
print('Done in {:.2f}s'.format(clock()-t0))
207
print('Begin Monte Carlo')
208
jk_iter_list = [jk_iter] * nsim
LUSTIG Peter's avatar
LUSTIG Peter committed
209
p = Pool(ncores)
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

for _flux in flux:

    helpfunc = partial(fake_worker, **{'min_threshold':
                                       min_detection_threshold,
                                       'nsources': nsources, 'flux': _flux,
                                       'within': (0, 1),
                                       'cat_gen': pos_uniform,
                                       'dist_threshold': min_source_dist,
                                       'parity_threshold': 0.5})

    print('Simulation with {:.2f}...'.format(_flux))

    if(1):
        res = p.map(helpfunc, jk_iter_list)
        res = list(zip(*res))

        DETECTED_SOURCES, FAKE_SOURCES = res[:]
    else:
        DETECTED_SOURCES = []
        FAKE_SOURCES = []
        with ProgressBar(nsim) as bar:
            for iloop in range(nsim):
                tmpsource, tmpfakesource = helpfunc(jk_iter)
                DETECTED_SOURCES.append(tmpsource)
                FAKE_SOURCES.append(tmpfakesource)
                bar.update()

    # To merge all the fake_sources and sources catalogs
    fake_sources = Table()
    sources = Table()
    for _fake, _detected in zip(FAKE_SOURCES[:], DETECTED_SOURCES[:]):
        n_fake = len(fake_sources)
        n_detected = len(sources)

        if _detected is not None:
            _detected['ID'] = _detected['ID'] + n_detected
            _detected['fake_sources'] = _detected['fake_sources'] + n_fake
            sources = vstack([sources, _detected])

        _fake['ID'] = _fake['ID'] + n_fake
        _fake['find_peak'] = _fake['find_peak'] + n_detected

        fake_sources = vstack([fake_sources, _fake])

    fname = ('flux{:.2f}mJy_thresh{}_nsim{}.fits'
             .format(_flux.to_value(unit=u.mJy),
                     min_detection_threshold, nsim))

    fname = timeprefix + fname
    outfile = outdir / fname

    phdu = fits.PrimaryHDU()
LUSTIG Peter's avatar
LUSTIG Peter committed
264
    phdu.header['influx'] = '{}'.format(_flux)
265 266 267 268 269 270 271 272 273 274
    phdu.header['nsim'] = nsim
    phdu.header['sourcespersim'] = nsources
    phdu.header['dthresh'] = min_detection_threshold
    hdul = [phdu]
    if len(sources) > 0:
        hdul.append(fits.BinTableHDU(data=sources, name='Detected_Sources'))
    hdul.append(fits.BinTableHDU(data=fake_sources, name='Fake_Sources'))
    hdul = fits.HDUList(hdul)
    hdul.writeto(outfile, overwrite=False)
    print('results written to {}'.format(outfile))