aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/plot_sens.py
blob: 18e0c037c54bedbb21a05ca2ca486d4a90bd1278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#! /usr/bin/env python
# author : S. Mandalia
#          s.p.mandalia@qmul.ac.uk
#
# date   : April 28, 2018

"""
HESE BSM flavour ratio analysis plotting script
"""

from __future__ import absolute_import, division

import os
import argparse
from functools import partial
from copy import deepcopy

import numpy as np
import numpy.ma as ma

from golemflavor import fr as fr_utils
from golemflavor import llh as llh_utils
from golemflavor import plot as plot_utils
from golemflavor.enums import DataType, Texture
from golemflavor.misc import enum_parse, parse_bool, parse_enum, print_args
from golemflavor.misc import gen_identifier, SortingHelpFormatter
from golemflavor.param import Param, ParamSet


MASK_X = (0.3, 0.7)


def process_args(args):
    """Process the input args."""
    if args.data is not DataType.REAL:
        args.injected_ratio = fr_utils.normalise_fr(args.injected_ratio)

    # Anon points
    anon = []
    if args.dimensions[0] == 3:
        anon.append([0.825, 0.845])
        anon.append([0.865, 0.875])
        anon.append([0.875, 0.885])
        anon.append([0.905, 0.915])
        anon.append([0.925, 0.935])
    if args.dimensions[0] == 4:
        anon.append([0.165, 0.175])
        anon.append([0.805, 0.825])
        anon.append([0.835, 0.845])
        anon.append([0.855, 0.885])
        anon.append([0.965, 0.975])
    if args.dimensions[0] == 5:
        anon.append([0.895, 0.905])
        anon.append([0.955, 0.965])
    if args.dimensions[0] == 6:
        anon.append([0.115, 0.125])
        anon.append([0.855, 0.865])
    if args.dimensions[0] == 7:
        # anon.append([0.815, 0.835])
        anon.append([0.875, 0.885])
    if args.dimensions[0] == 8:
        anon.append([0.915, 0.935])
        anon.append([0.875, 0.895])
        anon.append([0.845, 0.855])

    if args.source_ratios is not None:
        if args.x_segments is not None:
            raise ValueError('Cannot do both --source-ratios and --x-segments')
        if len(args.source_ratios) % 3 != 0:
            raise ValueError(
                'Invalid source ratios input {0}'.format(args.source_ratios)
            )

        srs = [args.source_ratios[3*x:3*x+3]
               for x in range(int(len(args.source_ratios)/3))]
        args.source_ratios = map(fr_utils.normalise_fr, srs)
    elif args.x_segments is not None:
        x_array = np.linspace(0, 1, args.x_segments)
        sources = []
        for x in x_array:
            if x >= MASK_X[0] and x <= MASK_X[1]: continue
            skip = False
            for a in anon:
                if (a[1] > x) & (x > a[0]):
                    print 'Skipping src', x
                    skip = True
                    break
            if skip: continue
            sources.append([x, 1-x, 0])
        args.source_ratios = sources
    else:
        raise ValueError('Must supply either --source-ratios or --x-segments')

    args.dimensions = np.sort(args.dimensions)


def parse_args(args=None):
    """Parse command line arguments"""
    parser = argparse.ArgumentParser(
        description="HESE BSM flavour ratio analysis plotting script",
        formatter_class=SortingHelpFormatter,
    )
    parser.add_argument(
        '--datadir', type=str,
        default='/data/user/smandalia/flavour_ratio/data/sensitivity',
        help='Path to data directory'
    )
    parser.add_argument(
        '--segments', type=int, default=10,
        help='Number of new physics scales to evaluate'
    )
    parser.add_argument(
        '--split-jobs', type=parse_bool, default='True',
        help='Did the jobs get split'
    )
    parser.add_argument(
        '--dimensions', type=int, nargs='*', default=[3, 6],
        help='Set the new physics dimensions to consider'
    )
    parser.add_argument(
        '--source-ratios', type=int, nargs='*', default=None,
        required=False, help='Set the source flavour ratios'
    )
    parser.add_argument(
        '--x-segments', type=int, default=None,
        required=False, help='Number of segments in x'
    )
    parser.add_argument(
        '--texture', type=partial(enum_parse, c=Texture),
        default='none', choices=Texture, help='Set the BSM mixing texture'
    )
    parser.add_argument(
        '--data', default='asimov', type=partial(enum_parse, c=DataType),
        choices=DataType, help='select datatype'
    )
    parser.add_argument(
        '--plot-x', type=parse_bool, default='False',
        help='Make sensitivity plot x vs limit'
    )
    parser.add_argument(
        '--plot-table', type=parse_bool, default='False',
        help='Make sensitivity table plot'
    )
    parser.add_argument(
        '--plot-statistic', type=parse_bool, default='False',
        help='Plot MultiNest evidence or LLH value'
    )
    llh_utils.llh_argparse(parser)
    if args is None: return parser.parse_args()
    else: return parser.parse_args(args.split())


def main():
    args = parse_args()
    process_args(args)
    print_args(args)

    dims = len(args.dimensions)
    srcs = len(args.source_ratios)
    if args.texture is Texture.NONE:
        textures = [Texture.OEU, Texture.OET, Texture.OUT]
        if args.plot_table:
            textures = [Texture.OET, Texture.OUT]
    else:
        textures = [args.texture]
    texs = len(textures)

    prefix = ''
    # prefix = 'noprior'

    # Initialise data structure.
    statistic_arr = np.full((dims, srcs, texs, args.segments, 2), np.nan)

    print 'Loading data'
    argsc = deepcopy(args)
    for idim, dim in enumerate(args.dimensions):
        argsc.dimension = dim

        datadir = args.datadir + '/DIM{0}'.format(dim)
        # Array of scales to scan over.
        boundaries = fr_utils.SCALE_BOUNDARIES[argsc.dimension]
        eval_scales = np.linspace(
            boundaries[0], boundaries[1], args.segments-1
        )
        eval_scales = np.concatenate([[-100.], eval_scales])

        for isrc, src in enumerate(args.source_ratios):
            argsc.source_ratio = src

            for itex, texture in enumerate(textures):
                argsc.texture = texture

                base_infile = datadir + '/{0}/{1}/'.format(
                    *map(parse_enum, [args.stat_method, args.data])
                ) + r'{0}/fr_stat'.format(prefix) + gen_identifier(argsc)

                print '== {0:<25} = {1}'.format('base_infile', base_infile)

                if args.split_jobs:
                    for idx_sc, scale in enumerate(eval_scales):
                        infile = base_infile + '_scale_{0:.0E}'.format(
                            np.power(10, scale)
                        )
                        try:
                            print 'Loading from {0}'.format(infile+'.npy')
                            statistic_arr[idim][isrc][itex][idx_sc] = \
                                np.load(infile+'.npy')[0]
                        except:
                            print 'Unable to load file {0}'.format(
                                infile+'.npy'
                            )
                            # raise
                            continue
                else:
                    print 'Loading from {0}'.format(base_infile+'.npy')
                    try:
                        statistic_arr[idim][isrc][itex] = \
                            np.load(base_infile+'.npy')
                    except:
                        print 'Unable to load file {0}'.format(
                            base_infile+'.npy'
                        )
                        continue

    data = ma.masked_invalid(statistic_arr)

    print 'data', data
    if args.plot_statistic:
        print 'Plotting statistic'

        for idim, dim in enumerate(args.dimensions):
            argsc.dimension = dim

            # Array of scales to scan over.
            boundaries = fr_utils.SCALE_BOUNDARIES[argsc.dimension]
            eval_scales = np.linspace(
                boundaries[0], boundaries[1], args.segments-1
            )
            eval_scales = np.concatenate([[-100.], eval_scales])

            for isrc, src in enumerate(args.source_ratios):
                argsc.source_ratio = src
                for itex, texture in enumerate(textures):
                    argsc.texture = texture

                    base_infile = args.datadir + '/DIM{0}/{1}/{2}/'.format(
                        dim, *map(parse_enum, [args.stat_method, args.data])
                    ) + r'{0}/fr_stat'.format(prefix) + gen_identifier(argsc)
                    basename = os.path.dirname(base_infile)
                    outfile = basename[:5]+basename[5:].replace('data', 'plots')
                    outfile += '/' + os.path.basename(base_infile)

                    label = r'$\text{Texture}=' + plot_utils.texture_label(texture)[1:]
                    plot_utils.plot_statistic(
                        data        = data[idim][isrc][itex],
                        outfile     = outfile,
                        outformat   = ['png'],
                        args        = argsc,
                        scale_param = scale,
                        label       = label
                    )

    basename = args.datadir[:5]+args.datadir[5:].replace('data', 'plots')
    baseoutfile = basename + '/{0}/{1}/'.format(
        *map(parse_enum, [args.stat_method, args.data])
    ) + r'{0}'.format(prefix)

    argsc = deepcopy(args)
    if args.plot_x:
        for idim, dim in enumerate(args.dimensions):
            print '|||| DIM = {0}'.format(dim)
            argsc.dimension = dim
            plot_utils.plot_x(
                data      = data[idim],
                outfile   = baseoutfile + '/hese_x_DIM{0}'.format(dim),
                outformat = ['png', 'pdf'],
                args      = argsc,
                normalise = True
            )

    if args.plot_table:
        plot_utils.plot_table_sens(
            data      = data,
            outfile   = baseoutfile + '/hese_table',
            outformat = ['png', 'pdf'],
            args      = args,
            show_lvatmo = True
        )


main.__doc__ = __doc__


if __name__ == '__main__':
    main()