1
+ import numpy as np
2
+ from mhealpy import HealpixMap
3
+ from mhealpy .pixelfunc .moc import *
4
+ from mhealpy .pixelfunc .single import *
5
+ from cosipy import MOCTSMap , test_data , SpacecraftFile
6
+ import matplotlib .pyplot as plt
7
+ from copy import deepcopy
8
+ import astropy .units as u
9
+ from astropy .coordinates import SkyCoord
10
+ from pathlib import Path
11
+ from histpy import Histogram
12
+ from threeML import Powerlaw
13
+ import os
14
+
15
+ def test_upscale_moc_map ():
16
+
17
+ test_moc_map_path = test_data .path / "test_MOC_Map.fits"
18
+
19
+ moc_map_ts = HealpixMap .read_map (test_moc_map_path )
20
+
21
+ mother_uniq = np .array ([ 5 , 6 , 9 , 10 , 13 , 14 ])
22
+
23
+ new_order = 1
24
+
25
+ m_new , uniq_child_all = MOCTSMap .upscale_moc_map (moc_map_ts , uniq_mother = mother_uniq , new_order = new_order )
26
+
27
+ assert np .allclose (uniq_child_all ,
28
+ np .array ([20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 ]))
29
+
30
+ assert np .allclose (m_new [:],
31
+ np .array ([205615 , 368891 , 368891 , 368891 , 368891 , 356267 , 356267 , 356267 ,
32
+ 356267 , 199132 , 172127 , 268767 , 268767 , 268767 , 268767 , 888147 ,
33
+ 888147 , 888147 , 888147 , 252898 , 215294 , 346345 , 346345 , 346345 ,
34
+ 346345 , 378671 , 378671 , 378671 , 378671 , 234107 ]))
35
+
36
+
37
+
38
+ def test_uniq2skycoord ():
39
+
40
+ coord = MOCTSMap .uniq2skycoord (20 )
41
+
42
+ assert type (coord ) is SkyCoord
43
+
44
+ assert np .allclose (coord .l .deg , 135.0 )
45
+
46
+ assert np .allclose (coord .b .deg , 19.47122063449069 )
47
+
48
+
49
+ def test_uniq2pixidx ():
50
+
51
+ test_moc_map_path = test_data .path / "test_MOC_Map.fits"
52
+
53
+ moc_map_ts = HealpixMap .read_map (test_moc_map_path )
54
+
55
+ uniq = np .array ([9 , 10 ])
56
+
57
+ idx = MOCTSMap .uniq2pixidx (moc_map_ts , uniq )
58
+
59
+ assert np .allclose (idx ,
60
+ np .array ([5 ,6 ]))
61
+
62
+
63
+ def test_fill_up_moc_map ():
64
+
65
+ test_moc_map_path = test_data .path / "test_MOC_Map.fits"
66
+
67
+ moc_map_ts = HealpixMap .read_map (test_moc_map_path )
68
+
69
+ ts_fit_results = np .array ([np .arange (12 ), np .repeat (1. , moc_map_ts .uniq .size )]).T
70
+
71
+ new_map = MOCTSMap .fill_up_moc_map (np .arange (12 ), moc_map_ts , ts_fit_results )
72
+
73
+ assert np .allclose (new_map [:],
74
+ np .repeat (1. , moc_map_ts .uniq .size ))
75
+
76
+
77
+
78
+ def test_moc_ts_fit ():
79
+
80
+ src_bkg_path = test_data .path / "ts_map_src_bkg.h5"
81
+ bkg_path = test_data .path / "ts_map_bkg.h5"
82
+ response_path = test_data .path / "test_full_detector_response.h5"
83
+
84
+ orientation_path = test_data .path / "20280301_2s.ori"
85
+ ori = SpacecraftFile .parse_from_file (orientation_path )
86
+
87
+ src_bkg = Histogram .open (src_bkg_path ).project (['Em' , 'PsiChi' , 'Phi' ])
88
+ bkg = Histogram .open (bkg_path ).project (['Em' , 'PsiChi' , 'Phi' ])
89
+
90
+ # define a powerlaw spectrum
91
+ index = - 3
92
+ K = 10 ** - 3 / u .cm / u .cm / u .s / u .keV
93
+ piv = 100 * u .keV
94
+ spectrum = Powerlaw ()
95
+ spectrum .index .value = index
96
+ spectrum .K .value = K .value
97
+ spectrum .piv .value = piv .value
98
+ spectrum .K .unit = K .unit
99
+ spectrum .piv .unit = piv .unit
100
+
101
+ moc_fit = MOCTSMap (data = src_bkg ,
102
+ bkg_model = bkg ,
103
+ response_path = response_path ,
104
+ orientation = ori , # we don't need orientation since we are using the precomputed galactic reaponse
105
+ cds_frame = "local" )
106
+
107
+ moc_map = moc_fit .moc_ts_fit (max_moc_order = 1 , # this is the maximum order of the final map
108
+ top_number = 3 , # In each iterations, only the pixels with top 8 likelihood values will be split in the next iteration
109
+ energy_channel = [2 ,3 ], # The energy channel used to perform the fit.
110
+ spectrum = spectrum )
111
+
112
+ assert np .allclose (moc_map [:],
113
+ np .array ([51.28650334 , 51.30366672 , 51.30492629 , 51.11675696 , 51.15264209 ,
114
+ 51.09963846 , 51.18497425 , 51.25122002 , 51.30205529 , 51.28966325 ,
115
+ 51.29978837 , 51.29973519 , 51.07010273 , 51.09357204 , 51.29127877 ,
116
+ 51.27676366 , 51.147458 , 51.30377498 , 51.30232323 , 51.18908082 ,
117
+ 51.26570991 ]))
118
+
119
+ coord = SkyCoord (0 , 0 , unit = "deg" , frame = "galactic" )
120
+
121
+ moc_fit .plot_ts (moc_map = moc_map , skycoord = coord , containment = 0.9 , save_plot = True )
122
+
123
+ plot_path = Path ("ts_map.png" )
124
+
125
+ assert plot_path .exists ()
126
+
127
+ if plot_path .exists ():
128
+ os .remove (plot_path )
0 commit comments