1 | """ |
---|
2 | This is getting really messy. |
---|
3 | |
---|
4 | All err results are going into the same dir, and it can't really be changed. |
---|
5 | """ |
---|
6 | |
---|
7 | |
---|
8 | #---------------------------------------------------------------------------- |
---|
9 | # Import necessary modules |
---|
10 | #---------------------------------------------------------------------------- |
---|
11 | |
---|
12 | # Standard modules |
---|
13 | import os |
---|
14 | from os import sep, path |
---|
15 | from csv import writer |
---|
16 | from time import localtime, strftime |
---|
17 | |
---|
18 | # Related major packages |
---|
19 | from Numeric import arange, array, zeros, Float, where, greater, less, \ |
---|
20 | compress, argmin, choose, searchsorted, sqrt, sum |
---|
21 | |
---|
22 | import project |
---|
23 | from os import sep |
---|
24 | from anuga.shallow_water.data_manager import csv2dict |
---|
25 | from anuga.utilities.numerical_tools import ensure_numeric, err, norm |
---|
26 | |
---|
27 | from anuga.utilities.interp import interp |
---|
28 | |
---|
29 | def get_max_min_condition_array(min, max, vector): |
---|
30 | |
---|
31 | SMALL_MIN = -1e10 # Not that small, but small enough |
---|
32 | vector = ensure_numeric(vector) |
---|
33 | assert min > SMALL_MIN |
---|
34 | no_maxs = where(less(vector,max), vector, SMALL_MIN) |
---|
35 | #print "no_maxs", no_maxs |
---|
36 | band_condition = greater(no_maxs, min) |
---|
37 | return band_condition |
---|
38 | |
---|
39 | |
---|
40 | def auto_rrms(outputdir_tag, scenarios, quantity='stage', |
---|
41 | y_location_tag=':0.0'): |
---|
42 | """ |
---|
43 | Given a bunch of scenarios that have CSV guage files, calc the |
---|
44 | err, Number_of_samples and rmsd for all gauges in each scenario. |
---|
45 | Write this info to a file for each scenario. |
---|
46 | """ |
---|
47 | for run_data in scenarios: |
---|
48 | location_sims = [] |
---|
49 | location_exps = [] |
---|
50 | for gauge_x in run_data['gauge_x']: |
---|
51 | gauge_x = str(gauge_x) |
---|
52 | location_sims.append(gauge_x + y_location_tag) |
---|
53 | location_exps.append(gauge_x) |
---|
54 | |
---|
55 | id = run_data['scenario_id'] |
---|
56 | outputdir_name = id + outputdir_tag |
---|
57 | file_sim = outputdir_name + '_' + quantity + ".csv" |
---|
58 | file_exp = id + '_exp_' + quantity + '.csv' |
---|
59 | file_err = outputdir_name + "_" + quantity + "_err.csv" |
---|
60 | |
---|
61 | |
---|
62 | simulation, _ = csv2dict(file_sim) |
---|
63 | experiment, _ = csv2dict(file_exp) |
---|
64 | |
---|
65 | time_sim = [float(x) for x in simulation['time']] |
---|
66 | time_exp = [float(x) for x in experiment['Time']] |
---|
67 | time_sim = ensure_numeric(time_sim) |
---|
68 | time_exp = ensure_numeric(time_exp) |
---|
69 | #print "min(time_exp)", min(time_exp) |
---|
70 | #print "max(time_exp)", max(time_exp) |
---|
71 | |
---|
72 | condition = get_max_min_condition_array(run_data['wave_times'][0], |
---|
73 | run_data['wave_times'][1], |
---|
74 | time_exp) |
---|
75 | time_exp_cut = compress(condition, time_exp) #, axis=axis) |
---|
76 | #print "min(time_exp_cut)", min(time_exp_cut) |
---|
77 | #print "max(time_exp_cut)", max(time_exp_cut) |
---|
78 | |
---|
79 | #assert min(time_sim) < min(time_exp) |
---|
80 | |
---|
81 | print "Writing to ", file_err |
---|
82 | |
---|
83 | err_list = [] |
---|
84 | points = [] |
---|
85 | rmsd_list = [] |
---|
86 | for location_sim, location_exp in map(None, location_sims, |
---|
87 | location_exps): |
---|
88 | quantity_sim = [float(x) for x in simulation[location_sim]] |
---|
89 | quantity_exp = [float(x) for x in experiment[location_exp]] |
---|
90 | |
---|
91 | quantity_exp_cut = compress(condition, quantity_exp) |
---|
92 | |
---|
93 | # Now let's do interpolation |
---|
94 | quantity_sim_interp = interp(quantity_sim, time_sim, time_exp_cut) |
---|
95 | |
---|
96 | assert len(quantity_sim_interp) == len(quantity_exp_cut) |
---|
97 | norm = err(quantity_sim_interp, |
---|
98 | quantity_exp_cut, |
---|
99 | 2, relative = False) # 2nd norm (rel. RMS) |
---|
100 | err_list.append(norm) |
---|
101 | points.append(len(quantity_sim_interp)) |
---|
102 | rmsd_list.append(norm/sqrt(len(quantity_sim_interp))) |
---|
103 | #print "norm", norm |
---|
104 | #for i in range(len(quantity_sim_interp)): |
---|
105 | |
---|
106 | #print "quantity_sim_interp", quantity_sim_interp[i] |
---|
107 | #print "quantity_exp_cut", quantity_exp_cut[i] |
---|
108 | assert len(location_exps) == len(err_list) |
---|
109 | |
---|
110 | # Writing the file out for one scenario |
---|
111 | a_writer = writer(file(file_err, "wb")) |
---|
112 | a_writer.writerow(["x location", "err", "Number_of_samples", "rmsd"]) |
---|
113 | a_writer.writerows(map(None, |
---|
114 | location_exps, |
---|
115 | err_list, |
---|
116 | points, |
---|
117 | rmsd_list)) |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | def load_sensors(quantity_file): |
---|
122 | """ |
---|
123 | Load a csv file, where the first row is the column header and |
---|
124 | the first colum explains the rows. |
---|
125 | """ |
---|
126 | #slope, _ = csv2dict(file_sim) |
---|
127 | |
---|
128 | # Read the depth file |
---|
129 | dfid = open(quantity_file) |
---|
130 | lines = dfid.readlines() |
---|
131 | dfid.close() |
---|
132 | |
---|
133 | title = lines.pop(0) |
---|
134 | n_time = len(lines) |
---|
135 | n_sensors = len(lines[0].split(','))-1 # -1 to remove time |
---|
136 | times = zeros(n_time, Float) #Time |
---|
137 | depths = zeros(n_time, Float) # |
---|
138 | sensors = zeros((n_time,n_sensors), Float) |
---|
139 | quantity_locations = title.split(',') #(',') |
---|
140 | quantity_locations.pop(0) # remove 'time' |
---|
141 | |
---|
142 | # Doing j.split(':')[0] drops the y location |
---|
143 | locations = [float(j.split(':')[0]) for j in quantity_locations] |
---|
144 | |
---|
145 | for i, line in enumerate(lines): |
---|
146 | fields = line.split(',') #(',') |
---|
147 | fields = [float(j) for j in fields] |
---|
148 | times[i] = fields[0] |
---|
149 | sensors[i] = fields[1:] # 1: to remove time |
---|
150 | |
---|
151 | #print "times",times |
---|
152 | #print "locations", locations |
---|
153 | #print "sensors", sensors |
---|
154 | return times, locations, sensors |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | # Return a bunch of lists |
---|
160 | # The err files, for all scenarios |
---|
161 | def err_files(scenarios, outputdir_tag, quantity='stage'): |
---|
162 | """ |
---|
163 | The err files, for a list of scenarios |
---|
164 | """ |
---|
165 | file_errs = [] |
---|
166 | for scenario in scenarios: |
---|
167 | id = scenario['scenario_id'] |
---|
168 | outputdir_name = id + outputdir_tag |
---|
169 | file_err = outputdir_name + "_" + quantity + "_err.csv" |
---|
170 | file_errs.append(file_err) |
---|
171 | return file_errs |
---|
172 | |
---|
173 | |
---|
174 | def compare_different_settings(outputdir_tag, scenarios, quantity='stage'): |
---|
175 | |
---|
176 | files = err_files(scenarios, outputdir_tag, quantity=quantity) |
---|
177 | err = 0.0 |
---|
178 | number_of_samples = 0 |
---|
179 | for run_data, file in map(None, scenarios, files): |
---|
180 | |
---|
181 | simulation, _ = csv2dict(file) |
---|
182 | err_list = [float(x) for x in simulation['err']] |
---|
183 | number_of_samples_list = [float(x) for x in \ |
---|
184 | simulation['Number_of_samples']] |
---|
185 | |
---|
186 | if number_of_samples is not 0: |
---|
187 | err_list.append(err) |
---|
188 | number_of_samples_list.append(number_of_samples) |
---|
189 | err, number_of_samples = err_addition(err_list, number_of_samples_list) |
---|
190 | rmsd = err/sqrt(number_of_samples) |
---|
191 | print outputdir_tag + " " + str(rmsd) |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | def err_addition(err_list, number_of_samples_list): |
---|
196 | """ |
---|
197 | err1 is the err value (sqrt(sum_over_x&y((xi - yi)^2))) for a set of values |
---|
198 | number_of_samples1 is the number of values associated with the err. |
---|
199 | |
---|
200 | If this function gets used alot, maybe pull this out and make it an object |
---|
201 | """ |
---|
202 | err = norm(ensure_numeric(err_list)) |
---|
203 | number_of_samples = sum(ensure_numeric(number_of_samples_list)) |
---|
204 | |
---|
205 | return err, number_of_samples |
---|
206 | |
---|
207 | |
---|
208 | #------------------------------------------------------------- |
---|
209 | if __name__ == "__main__": |
---|
210 | """ |
---|
211 | """ |
---|
212 | from scenarios import scenarios |
---|
213 | |
---|
214 | |
---|
215 | scenarios = [scenarios[0]] # !!!!!!!!!!!!!!!!!!!!!! |
---|
216 | #scenarios = scenarios[4:] # !!!!!!!!!!!!!!!!!!!!!! |
---|
217 | |
---|
218 | |
---|
219 | outputdir_tag = "_nolmts_wdth_0.1_z_0.0_ys_0.5_mta_0.01" |
---|
220 | calc_norms = True |
---|
221 | #calc_norms = False |
---|
222 | if calc_norms: |
---|
223 | auto_rrms(outputdir_tag, scenarios, "stage", y_location_tag=':0.0') |
---|
224 | compare_different_settings(outputdir_tag, scenarios, "stage") |
---|
225 | |
---|