-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbkgd_ll_area.c
More file actions
155 lines (104 loc) · 3.12 KB
/
bkgd_ll_area.c
File metadata and controls
155 lines (104 loc) · 3.12 KB
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
#include <glib.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <util/util.h>
#include "model.h"
#include "bkgd_evo_mdl.h"
static FILE *get_out_fh(Config *config, char *parm_name) {
char *path;
char *dir;
FILE *fh;
dir = config_get_str(config, "OUTPUT_DIR");
path = g_strconcat(dir, parm_name, ".txt", NULL);
fh = fopen(path, "w");
if(fh == NULL) {
g_error("get_out_fh: could not open file '%s'", path);
}
g_free(path);
return fh;
}
void scan_parm_area(FILE *fh, Model *mdl, char *param_name, double start,
double end, int n_step) {
int i;
double orig_val, cur_val, step_sz;
double ll, deriv;
fprintf(stderr, "%s [%g-%g]\n", param_name, start, end);
orig_val = model_get_param_val(mdl, param_name);
step_sz = (end - start) / (double)n_step;
/* report LL at original param value as well */
ll = bkgd_evo_mdl_calc_ll(mdl, TRUE, TRUE);
fprintf(stderr, "first call: LL=%.10f\n", ll);
deriv = model_get_param_deriv(mdl, param_name);
fprintf(fh, "%g %.10g %.10g\n", orig_val, ll, deriv);
cur_val = start;
for(i = 0; i < n_step; i++) {
model_set_param_val(mdl, param_name, cur_val);
ll = bkgd_evo_mdl_calc_ll(mdl, TRUE, TRUE);
deriv = model_get_param_deriv(mdl, param_name);
fprintf(fh, "%g %.10g %.10g\n", cur_val, ll, deriv);
cur_val += step_sz;
}
/* restore paramter to original value */
model_set_param_val(mdl, param_name, orig_val);
}
#define PARAM_OFFSET 0.02
void explore_parm(Config *config, char *parm_name, Model *mdl) {
char *key;
double val, start, end;
int n_step;
FILE *fh;
fprintf(stderr, "parm_name=%s\n", parm_name);
n_step = config_get_long(config, "N_SCAN_STEP");
val = model_get_param_val(mdl, parm_name);
key = g_strconcat(parm_name, "_START", NULL);
util_str_uc(key);
if(config_has_key(config, key)) {
start = config_get_double(config, key);
} else {
start = val - (val * PARAM_OFFSET);
}
g_free(key);
key = g_strconcat(parm_name, "_END", NULL);
util_str_uc(key);
if(config_has_key(config, key)) {
end = config_get_double(config, key);
} else {
end = val + (val * PARAM_OFFSET);
}
g_free(key);
n_step = config_get_long(config, "N_SCAN_STEP");
fh = get_out_fh(config, parm_name);
scan_parm_area(fh, mdl, parm_name, start, end, n_step);
fclose(fh);
}
void explore_ll_surface(Config *config, Model *mdl) {
char **parm_names;
int n_parm, i;
parm_names = config_get_str_array(config, "PARAM_NAMES", &n_parm);
for(i = 0; i < n_parm; i++) {
explore_parm(config, parm_names[i], mdl);
}
}
/**
* Main function
*/
int main(int argc, char **argv) {
Config *config;
Model *mdl;
if(argc != 2) {
fprintf(stderr, "usage: %s <config_file>\n",
argv[0]);
exit(2);
}
fprintf(stderr, "Reading config\n");
config = config_read_file(argv[1], CONFIG_MISSING_KEY_ERROR);
fprintf(stderr, "creating model\n");
mdl = bkgd_evo_mdl_new(config);
bkgd_evo_mdl_read_data(mdl, config);
fprintf(stderr, "scanning LL surface\n");
explore_ll_surface(config, mdl);
bkgd_evo_mdl_free(mdl);
return 0;
}