Skip to content
This repository was archived by the owner on Jul 20, 2025. It is now read-only.

Commit 38fa6df

Browse files
committed
Exemplify a console usage of crystal structure design
1 parent fe8857c commit 38fa6df

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

mpds_ml_labs/test_design.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
from __future__ import division
3+
import random
4+
import time
5+
6+
from struct_utils import order_disordered
7+
from knn_sample import knn_sample
8+
from similar_els import materialize, score
9+
from common import connect_database, KNN_TABLE, ML_MODELS
10+
from cif_utils import ase_to_eq_cif
11+
from prediction import prop_models, load_ml_models
12+
from prediction_ranges import prediction_ranges, TOL_QUALITY
13+
14+
15+
result, error = None, "No results (outside of prediction capabilities)"
16+
sample = {}
17+
18+
for prop_id in prediction_ranges:
19+
dice = random.choice([0, 1])
20+
bound = (prediction_ranges[prop_id][0] + prediction_ranges[prop_id][1]) / 3
21+
if dice:
22+
sample[prop_id + '_min'] = float(bound * 2)
23+
sample[prop_id + '_max'] = float(prediction_ranges[prop_id][1])
24+
else:
25+
sample[prop_id + '_min'] = float(prediction_ranges[prop_id][0])
26+
sample[prop_id + '_max'] = float(bound)
27+
28+
active_ml_models = load_ml_models(ML_MODELS, debug=False)
29+
30+
starttime = time.time()
31+
32+
cursor, connection = connect_database()
33+
34+
els_samples = knn_sample(cursor, sample)
35+
for els_sample in els_samples:
36+
37+
scoring, error = materialize(els_sample, active_ml_models)
38+
if error or not scoring:
39+
continue
40+
41+
result = score(scoring, sample)
42+
break
43+
44+
connection.close()
45+
46+
if not result: raise RuntimeError(error)
47+
48+
answer_props = {prop_id: result['prediction'][prop_id]['value'] for prop_id in result['prediction']}
49+
50+
# normalization 10**5
51+
answer_props['t'] /= 100000
52+
sample['t_min'] /= 100000
53+
sample['t_max'] /= 100000
54+
55+
if 'disordered' in result['structure'].info:
56+
result['structure'], error = order_disordered(result['structure'])
57+
if error: raise RuntimeError(error)
58+
59+
result['structure'].center(about=0.0)
60+
61+
result_quality, aux_info = 0, []
62+
for k, v in answer_props.items():
63+
aux_info.append([
64+
prop_models[k]['name'].replace(' ', '_'),
65+
sample[k + '_min'],
66+
v,
67+
sample[k + '_max'],
68+
prop_models[k]['units']
69+
])
70+
tol = (sample[k + '_max'] - sample[k + '_min']) * TOL_QUALITY
71+
if sample[k + '_min'] - tol < v < sample[k + '_max'] + tol:
72+
result_quality += 1
73+
74+
print(ase_to_eq_cif(result['structure'], supply_sg=False, mpds_labs_loop=[result_quality] + aux_info))
75+
print("Done in %1.2f sc" % (time.time() - starttime))

0 commit comments

Comments
 (0)