|
5 | 5 |
|
6 | 6 | from flask import Flask, Blueprint, Response, request, send_from_directory |
7 | 7 |
|
8 | | -from struct_utils import detect_format, poscar_to_ase, refine, get_formula |
| 8 | +from struct_utils import detect_format, poscar_to_ase, refine, get_formula, order_disordered |
9 | 9 | from cif_utils import cif_to_ase, ase_to_eq_cif |
10 | | -from prediction import get_prediction, get_aligned_descriptor, get_ordered_descriptor, get_legend, load_ml_models |
11 | | -from common import SERVE_UI, ML_MODELS |
| 10 | +from prediction import prop_models, get_prediction, get_aligned_descriptor, get_ordered_descriptor, get_legend, load_ml_models |
| 11 | +from common import SERVE_UI, ML_MODELS, connect_database |
| 12 | +from knn_sample import knn_sample |
| 13 | +from similar_els import materialize, score |
| 14 | +from prediction_ranges import TOL_QUALITY |
12 | 15 |
|
13 | 16 |
|
14 | 17 | app_labs = Blueprint('app_labs', __name__) |
@@ -41,27 +44,47 @@ def html_formula(string): |
41 | 44 |
|
42 | 45 | if SERVE_UI: |
43 | 46 | @app_labs.route('/', methods=['GET']) |
| 47 | + @app_labs.route('/props.html', methods=['GET']) |
44 | 48 | def index(): |
45 | | - return send_from_directory(static_path, 'index.html') |
46 | | - @app_labs.route('/index.css', methods=['GET']) |
47 | | - def style(): |
48 | | - return send_from_directory(static_path, 'index.css') |
| 49 | + return send_from_directory(static_path, 'props.html') |
| 50 | + |
| 51 | + @app_labs.route('/common.css', methods=['GET']) |
| 52 | + def css(): |
| 53 | + return send_from_directory(static_path, 'common.css') |
| 54 | + |
49 | 55 | @app_labs.route('/player.html', methods=['GET']) |
50 | 56 | def player(): |
51 | 57 | return send_from_directory(static_path, 'player.html') |
52 | 58 |
|
| 59 | + @app_labs.route('/design.html', methods=['GET']) |
| 60 | + def md(): |
| 61 | + return send_from_directory(static_path, 'design.html') |
| 62 | + |
| 63 | + @app_labs.route('/jquery.min.js', methods=['GET']) |
| 64 | + def jquery(): |
| 65 | + return send_from_directory(static_path, 'jquery.min.js') |
| 66 | + |
| 67 | + @app_labs.route('/nouislider.min.js', methods=['GET']) |
| 68 | + def nouislider(): |
| 69 | + return send_from_directory(static_path, 'nouislider.min.js') |
| 70 | + |
53 | 71 | @app_labs.after_request |
54 | 72 | def add_cors_header(response): |
55 | 73 | response.headers['Access-Control-Allow-Origin'] = '*' |
56 | 74 | return response |
57 | 75 |
|
58 | 76 | @app_labs.route("/predict", methods=['POST']) |
59 | 77 | def predict(): |
| 78 | + """ |
| 79 | + A main endpoint for the properties |
| 80 | + prediction, based on the provided CIF |
| 81 | + or POSCAR |
| 82 | + """ |
60 | 83 | if 'structure' not in request.values: |
61 | 84 | return fmt_msg('Invalid request') |
62 | 85 |
|
63 | 86 | structure = request.values.get('structure') |
64 | | - if not 0 < len(structure) < 32768: |
| 87 | + if not 0 < len(structure) < 200000: |
65 | 88 | return fmt_msg('Request size is invalid') |
66 | 89 |
|
67 | 90 | if not is_plain_text(structure): |
@@ -116,6 +139,110 @@ def predict(): |
116 | 139 | content_type='application/json' |
117 | 140 | ) |
118 | 141 |
|
| 142 | +@app_labs.route("/download_cif", methods=['POST']) |
| 143 | +def download_cif(): |
| 144 | + """ |
| 145 | + An utility endpoint to force |
| 146 | + a browser file (CIF) download |
| 147 | + """ |
| 148 | + structure = request.values.get('structure') |
| 149 | + title = request.values.get('title') |
| 150 | + |
| 151 | + if not structure or not title: |
| 152 | + return fmt_msg('Invalid request') |
| 153 | + |
| 154 | + if not 0 < len(structure) < 100000: |
| 155 | + return fmt_msg('Request size is invalid') |
| 156 | + |
| 157 | + return Response(structure, mimetype="chemical/x-cif", headers={ |
| 158 | + "Content-Disposition": "attachment;filename=%s.cif" % title |
| 159 | + }) |
| 160 | + |
| 161 | +@app_labs.route("/design", methods=['POST']) |
| 162 | +def design(): |
| 163 | + """ |
| 164 | + A main endpoint for generating |
| 165 | + the CIF structure based on |
| 166 | + the provided values of the properties |
| 167 | + """ |
| 168 | + if 'numerics' not in request.values: |
| 169 | + return fmt_msg('Invalid request') |
| 170 | + |
| 171 | + try: numerics = json.loads(request.values.get('numerics')) |
| 172 | + except: |
| 173 | + return fmt_msg('Invalid request') |
| 174 | + if type(numerics) != dict: |
| 175 | + return fmt_msg('Invalid request') |
| 176 | + |
| 177 | + user_ranges_dict = {} |
| 178 | + |
| 179 | + for prop_id in prop_models: |
| 180 | + if prop_id not in numerics or type(numerics[prop_id]) != list or len(numerics[prop_id]) != 2: |
| 181 | + return fmt_msg('Invalid request') |
| 182 | + try: user_ranges_dict[prop_id + '_min'], user_ranges_dict[prop_id + '_max'] = float(numerics[prop_id][0]), float(numerics[prop_id][1]) |
| 183 | + except: |
| 184 | + return fmt_msg('Invalid request') |
| 185 | + |
| 186 | + if user_ranges_dict['w_min'] == 0 and user_ranges_dict['w_max'] == 0: |
| 187 | + user_ranges_dict['w_min'], user_ranges_dict['w_max'] = -100, 100 # NB. any band gap is allowed |
| 188 | + |
| 189 | + cursor, connection = connect_database() |
| 190 | + |
| 191 | + result, error = None, "No results (outside of prediction capabilities)" |
| 192 | + |
| 193 | + els_samples = knn_sample(cursor, user_ranges_dict) |
| 194 | + for els_sample in els_samples: |
| 195 | + #print "TRYING TO MATERIALIZE", ", ".join(els_sample) |
| 196 | + |
| 197 | + scoring, error = materialize(els_sample, active_ml_models) |
| 198 | + if error or not scoring: |
| 199 | + continue |
| 200 | + |
| 201 | + result = score(scoring, user_ranges_dict) |
| 202 | + break |
| 203 | + |
| 204 | + connection.close() |
| 205 | + |
| 206 | + if result: |
| 207 | + answer_props = {prop_id: result['prediction'][prop_id]['value'] for prop_id in result['prediction']} |
| 208 | + answer_props['t'] /= 100000 # normalization 10**5 |
| 209 | + |
| 210 | + if 'disordered' in result['structure'].info: |
| 211 | + result['structure'], error = order_disordered(result['structure']) |
| 212 | + if error: return fmt_msg(error) |
| 213 | + result['structure'].center(about=0.0) |
| 214 | + |
| 215 | + formula = get_formula(result['structure']) |
| 216 | + |
| 217 | + result_quality, aux_info = 0, [] |
| 218 | + for k, v in answer_props.items(): |
| 219 | + aux_info.append([ |
| 220 | + prop_models[k]['name'].replace(' ', '_'), |
| 221 | + sample[k + '_min'], |
| 222 | + v, |
| 223 | + sample[k + '_max'], |
| 224 | + prop_models[k]['units'] |
| 225 | + ]) |
| 226 | + tol = (sample[k + '_max'] - sample[k + '_min']) * TOL_QUALITY |
| 227 | + if sample[k + '_min'] - tol < v < sample[k + '_max'] + tol: |
| 228 | + result_quality += 1 |
| 229 | + |
| 230 | + return Response( |
| 231 | + json.dumps({ |
| 232 | + 'vis_cif': ase_to_eq_cif( |
| 233 | + result['structure'], |
| 234 | + supply_sg=False, |
| 235 | + mpds_labs_loop=[result_quality] + aux_info |
| 236 | + ), |
| 237 | + 'props': answer_props, |
| 238 | + 'formula': html_formula(formula), |
| 239 | + 'title': formula |
| 240 | + }, indent=4, escape_forward_slashes=False |
| 241 | + ), |
| 242 | + content_type='application/json' |
| 243 | + ) |
| 244 | + return fmt_msg(error) |
| 245 | + |
119 | 246 |
|
120 | 247 | if __name__ == '__main__': |
121 | 248 | if sys.argv[1:]: |
|
0 commit comments