Commit 67f85bce4211117eaf1dcdf4c19c6b7c5e9d9e80
1 parent
4c745e05
Exists in
master
Generate a CSV file with the scaling laws for each model.
/spend 20h
Showing
1 changed file
with
63 additions
and
17 deletions
Show diff stats
flaskr/controllers/main_controller.py
... | ... | @@ -3,7 +3,7 @@ import geopy |
3 | 3 | import sqlalchemy |
4 | 4 | |
5 | 5 | from flask import Blueprint, render_template, flash, request, redirect, \ |
6 | - url_for, abort, send_from_directory | |
6 | + url_for, abort, send_from_directory, Response | |
7 | 7 | from os.path import join |
8 | 8 | |
9 | 9 | from flaskr.extensions import cache, basic_auth |
... | ... | @@ -14,9 +14,11 @@ from flaskr.geocoder import CachedGeocoder |
14 | 14 | from flaskr.core import generate_unique_id |
15 | 15 | from flaskr.content import content |
16 | 16 | |
17 | -# from io import StringIO | |
18 | 17 | from yaml import safe_dump as yaml_dump |
19 | 18 | |
19 | +import csv | |
20 | +# from io import StringIO | |
21 | +from cStringIO import StringIO | |
20 | 22 | |
21 | 23 | main = Blueprint('main', __name__) |
22 | 24 | |
... | ... | @@ -24,6 +26,27 @@ main = Blueprint('main', __name__) |
24 | 26 | OUT_ENCODING = 'utf-8' |
25 | 27 | |
26 | 28 | |
29 | +# ----------------------------------------------------------------------------- | |
30 | +# refactor this outta here, like in core? | |
31 | + | |
32 | +def get_emission_models(): | |
33 | + emission_models_confs = content.models | |
34 | + emission_models = [] | |
35 | + | |
36 | + for model_conf in emission_models_confs: | |
37 | + model_file = model_conf.file | |
38 | + the_module = importlib.import_module("flaskr.laws.%s" % model_file) | |
39 | + | |
40 | + model = the_module.EmissionModel(model_conf) | |
41 | + # model.configure(extra_model_conf) | |
42 | + | |
43 | + emission_models.append(model) | |
44 | + | |
45 | + return emission_models | |
46 | + | |
47 | +# ----------------------------------------------------------------------------- | |
48 | + | |
49 | + | |
27 | 50 | @main.route('/favicon.ico') |
28 | 51 | def favicon(): # we want it served from the root, not from static/ |
29 | 52 | return send_from_directory( |
... | ... | @@ -209,17 +232,7 @@ def compute(): # process the queue of estimation requests |
209 | 232 | |
210 | 233 | # GRAB AND CONFIGURE THE EMISSION MODELS ################################## |
211 | 234 | |
212 | - emission_models_confs = content.models | |
213 | - emission_models = [] | |
214 | - | |
215 | - for model_conf in emission_models_confs: | |
216 | - model_file = model_conf.file | |
217 | - the_module = importlib.import_module("flaskr.laws.%s" % model_file) | |
218 | - | |
219 | - model = the_module.EmissionModel(model_conf) | |
220 | - # model.configure(extra_model_conf) | |
221 | - | |
222 | - emission_models.append(model) | |
235 | + emission_models = get_emission_models() | |
223 | 236 | |
224 | 237 | # print(emission_models) |
225 | 238 | |
... | ... | @@ -419,9 +432,6 @@ def consult_estimation(public_id, extension): |
419 | 432 | if estimation.status in unavailable_statuses: |
420 | 433 | abort(404) |
421 | 434 | |
422 | - import csv | |
423 | - from cStringIO import StringIO | |
424 | - | |
425 | 435 | si = StringIO() |
426 | 436 | cw = csv.writer(si, quoting=csv.QUOTE_ALL) |
427 | 437 | cw.writerow([u"city", u"address", u"co2 (g)"]) |
... | ... | @@ -443,12 +453,48 @@ def consult_estimation(public_id, extension): |
443 | 453 | ]) |
444 | 454 | |
445 | 455 | # HTTP headers? |
446 | - return si.getvalue().strip('\r\n') | |
456 | + # return si.getvalue().strip('\r\n') | |
457 | + return Response( | |
458 | + response=si.getvalue().strip('\r\n'), | |
459 | + headers={ | |
460 | + 'Content-type': 'text/csv', | |
461 | + 'Content-disposition': "attachment; filename=%s.csv"%public_id, | |
462 | + }, | |
463 | + ) | |
447 | 464 | |
448 | 465 | else: |
449 | 466 | abort(404) |
450 | 467 | |
451 | 468 | |
469 | +@main.route("/scaling_laws.csv") | |
470 | +def get_scaling_laws_csv(): | |
471 | + distances = [ | |
472 | + 500., 1000., 1500., 2500., 3000., 4500., | |
473 | + 5000., 8000., 10000., 12000., | |
474 | + ] | |
475 | + models = get_emission_models() | |
476 | + | |
477 | + si = StringIO() | |
478 | + cw = csv.writer(si, quoting=csv.QUOTE_ALL) | |
479 | + | |
480 | + header = ['distance'] + [model.slug for model in models] | |
481 | + cw.writerow(header) | |
482 | + | |
483 | + for distance in distances: | |
484 | + row = [distance] | |
485 | + for model in models: | |
486 | + row.append(model.compute_airplane_distance_footprint(distance)) | |
487 | + cw.writerow(row) | |
488 | + | |
489 | + return Response( | |
490 | + response=si.getvalue().strip('\r\n'), | |
491 | + headers={ | |
492 | + 'Content-type': 'text/csv', | |
493 | + 'Content-disposition': 'attachment; filename=scaling_laws.csv', | |
494 | + }, | |
495 | + ) | |
496 | + | |
497 | + | |
452 | 498 | @main.route("/test") |
453 | 499 | @basic_auth.required |
454 | 500 | def dev_test(): | ... | ... |