Commit edcf2dd8 authored by Ambra Di Piano's avatar Ambra Di Piano
Browse files

save maps in npy option

parent c5cfd1a8
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ mapper:
  plot: true
  region: false
  output: /data01/homes/dipiano/astroRT/astrort/testing/tmp
  save: npy

visibility:
  start_time: '2030-01-01T00:00:00'
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ def base_mapper(configuration_file, seeds=None):
        fitsmap = execute_mapper_no_visibility(configuration, log)
        log.info(f"Mapping (seed = {seed}) complete, took {time() - clock_map} s")
        # make plot
        if configuration['mapper']['plot']:
        if configuration['mapper']['plot'] and configuration['mapper']['save'] == 'fits':
            clock_plot = time()
            plotmap = plot_map(fitsmap, log)
            log.info(f"Plotting (seed = {seed}) complete, took {time() - clock_plot} s")
+11 −3
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@

import pytest
import numpy as np
from yaml import dump
from shutil import rmtree
from os import listdir
from os.path import isfile, join
@@ -17,21 +18,28 @@ from astrort.utils.wrap import load_yaml_conf

@pytest.mark.test_conf_file
@pytest.mark.parametrize('seeds', [None, list()])
def test_base_mapper(test_conf_file, seeds):
@pytest.mark.parametrize('save', ['fits', 'npy'])
def test_base_mapper(test_conf_file, seeds, save):

    # clean output
    conf = load_yaml_conf(test_conf_file)
    conf['mapper']['save'] = save
    rmtree(conf['mapper']['output'], ignore_errors=True)

    # run simulator
    base_simulator(test_conf_file)
    if type(seeds) == list:
        seeds = np.arange(conf['simulator']['samples'])
    base_mapper(test_conf_file, seeds)

    # write new mapper configuration
    update_conf_file = join(conf['mapper']['output'], 'tmp.yml')
    with open(update_conf_file, 'w+') as f:
        dump(conf, f, default_flow_style=False)
    base_mapper(update_conf_file, seeds)

    # check output
    expected_maps = conf['simulator']['samples']
    found_maps = len([f for f in listdir(conf['mapper']['output']) if isfile(join(conf['mapper']['output'], f)) and '.fits' in f and conf['simulator']['name'] in f and 'map' in f])
    found_maps = len([f for f in listdir(conf['mapper']['output']) if isfile(join(conf['mapper']['output'], f)) and conf['mapper']['save'] in f and conf['simulator']['name'] in f and 'map' in f])
    assert found_maps == expected_maps, f"Expected {expected_maps} maps, found {found_maps}"

+6 −3
Original line number Diff line number Diff line
@@ -152,9 +152,11 @@ def test_execute_mapper_no_visibility(test_conf_file):
    assert isfile(fitsmap)

@pytest.mark.test_conf_file
def test_plot_map(test_conf_file):
@pytest.mark.parametrize('save', ['fits', 'npy'])
def test_plot_map(test_conf_file, save):
    # clean output
    conf = load_yaml_conf(test_conf_file)
    conf['mapper']['save'] = save
    rmtree(conf['mapper']['output'], ignore_errors=True)
    conf['simulator']['samples'] = 1
    log = set_logger(logging.CRITICAL)
@@ -162,5 +164,6 @@ def test_plot_map(test_conf_file):
    # run simulator
    base_simulator(test_conf_file)
    fitsmap = execute_mapper_no_visibility(conf, log)
    if save == 'fits':
        plotmap = plot_map(fitsmap, log)
        assert isfile(plotmap)
 No newline at end of file
+17 −12
Original line number Diff line number Diff line
@@ -191,26 +191,19 @@ class Mapper():
        extent = [pointing['ra']-roi, pointing['ra']+roi, pointing['dec']-roi, pointing['dec']+roi]
        return extent

    def heatmap_with_smoothing(self, x, y, extent, sigma=1, bins=1000):
    def get_heatmap(self, x, y, extent, sigma=0, bins=1000):
        r = [[extent[0], extent[1]], [extent[2], extent[3]]]
        heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins)
        if sigma != 0:
            heatmap = gaussian_filter(heatmap, sigma=sigma)
        return heatmap.T

    def heatmap(self, x, y, extent, bins=1000):
        r = [[extent[0], extent[1]], [extent[2], extent[3]]]
        heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins)
        return heatmap.T

    def from_dl3_to_dl4(self, dl3_data, pointing, maproi=5, pixelsize=0.02, sigma=1):
    def from_dl3_to_dl4(self, dl3_data, pointing, maproi=5, pixelsize=0.02, sigma=0):
        ra = np.array(dl3_data.field('RA')).flatten()
        dec = np.array(dl3_data.field('DEC')).flatten()
        nbins = self.get_binning_size(maproi=maproi, pixelsize=pixelsize)
        extent = self.get_extent(pointing=pointing, roi=maproi)
        if sigma != 0:
            dl4_data = self.heatmap_with_smoothing(ra, dec, extent=extent, bins=nbins, sigma=sigma)
        else:
            dl4_data = self.heatmap(ra, dec, extent=extent, bins=nbins)
        dl4_data = self.get_heatmap(ra, dec, extent=extent, bins=nbins, sigma=sigma)
        return dl4_data

    def selection_cuts(self, dl3_data, pointing, trange=None, erange=None, maproi=None):
@@ -224,3 +217,15 @@ class Mapper():
        if len(dl3_data) == 0:
            self.log.warning("Empty photon list selection.")
        return dl3_data

    def get_countmap_in_npy(self, dl3_file, pixelsize=0.02, maproi=5, trange=None, erange=None, sigma=1, npyname='heatmap.npy'):
        dl3_hdr = self.get_dl3_hdr(dl3_file=dl3_file)
        pointing = {'ra': float(dl3_hdr['RA_PNT']), 'dec': float(dl3_hdr['DEC_PNT'])}
        dl3_data = self.get_dl3_data(dl3_file=dl3_file)
        dl3_data = self.selection_cuts(dl3_data=dl3_data, pointing=pointing, trange=trange, erange=erange, maproi=maproi)
        if sigma != 0:
            dl4_data = self.from_dl3_to_dl4(dl3_data=dl3_data, pointing=pointing, maproi=maproi, pixelsize=0.02, sigma=sigma)
        else:
            dl4_data = self.from_dl3_to_dl4(dl3_data=dl3_data, pointing=pointing, maproi=maproi, pixelsize=pixelsize)
        np.save(npyname, dl4_data, allow_pickle=True, fix_imports=True)
        return
Loading