Changeset ec7e360 in sasmodels for sasmodels/compare_many.py
- Timestamp:
- Dec 23, 2015 10:17:49 AM (8 years ago)
- Branches:
- master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- e21cc31
- Parents:
- ce166d3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/compare_many.py
rf4f3919 rec7e360 7 7 from . import core 8 8 from .kernelcl import environment 9 from .compare import (MODELS, randomize_ model, suppress_pd, eval_sasview,9 from .compare import (MODELS, randomize_pars, suppress_pd, eval_sasview, 10 10 eval_opencl, eval_ctypes, make_data, get_demo_pars, 11 columnize, constrain_pars, constrain_new_to_old) 11 columnize, constrain_pars, constrain_new_to_old, 12 make_engine) 12 13 13 14 def calc_stats(target, value, index): … … 34 35 print(','.join('"%s"'%c for c in columns)) 35 36 37 PRECISION = { 38 'fast': 1e-3, 39 'half': 1e-3, 40 'single': 5e-5, 41 'double': 5e-14, 42 'single!': 5e-5, 43 'double!': 5e-14, 44 'quad!': 5e-18, 45 'sasview': 5e-14, 46 } 36 47 def compare_instance(name, data, index, N=1, mono=True, cutoff=1e-5, 37 precision='double'):48 base='sasview', comp='double'): 38 49 model_definition = core.load_model_definition(name) 39 50 pars = get_demo_pars(model_definition) … … 47 58 # to allow them to update values in the current scope since nonlocal 48 59 # declarations are not available in python 2.7. 49 def try_model(fn, *args, **kw):60 def try_model(fn, pars): 50 61 try: 51 result , _ = fn(model_definition, pars_i, data, *args, **kw)62 result = fn(**pars) 52 63 except KeyboardInterrupt: 53 64 raise … … 60 71 result = np.NaN*data.x 61 72 return result 62 def check_model( label, target, value, acceptable):63 stats = calc_stats(target, value, index)64 co lumns.extend(stats)65 labels.append('GPU single')73 def check_model(pars): 74 base_value = try_model(calc_base, pars) 75 comp_value = try_model(calc_comp, pars) 76 stats = calc_stats(base_value, comp_value, index) 66 77 max_diff[0] = max(max_diff[0], stats[0]) 67 good[0] = good[0] and (stats[0] < acceptable) 78 good[0] = good[0] and (stats[0] < expected) 79 return list(stats) 80 81 82 calc_base = make_engine(model_definition, data, base, cutoff) 83 calc_comp = make_engine(model_definition, data, comp, cutoff) 84 expected = max(PRECISION[base], PRECISION[comp]) 68 85 69 86 num_good = 0 … … 72 89 for k in range(N): 73 90 print("%s %d"%(name, k)) 74 pars_i, seed = randomize_model(pars) 91 seed = np.random.randint(1e6) 92 pars_i = randomize_pars(pars, seed) 75 93 constrain_pars(model_definition, pars_i) 76 94 constrain_new_to_old(model_definition, pars_i) … … 79 97 80 98 good = [True] 81 labels = [] 82 columns = [] 83 target = try_model(eval_sasview) 84 #target = try_model(eval_ctypes, dtype='double', cutoff=0.) 85 #target = try_model(eval_ctypes, dtype='longdouble', cutoff=0.) 86 if precision == 'single': 87 value = try_model(eval_opencl, dtype='single', cutoff=cutoff) 88 check_model('GPU single', target, value, 5e-5) 89 single_value = value # remember for single/double comparison 90 elif precision == 'double': 91 if environment().has_type('double'): 92 label = 'GPU double' 93 value = try_model(eval_opencl, dtype='double', cutoff=cutoff) 94 else: 95 label = 'CPU double' 96 value = try_model(eval_ctypes, dtype='double', cutoff=cutoff) 97 check_model(label, target, value, 5e-14) 98 double_value = value # remember for single/double comparison 99 elif precision == 'quad': 100 value = try_model(eval_opencl, dtype='longdouble', cutoff=cutoff) 101 check_model('CPU quad', target, value, 5e-14) 102 if 0: 103 check_model('single/double', double_value, single_value, 5e-5) 104 99 columns = check_model(pars_i) 105 100 columns += [v for _,v in sorted(pars_i.items())] 106 101 if first: 102 labels = [" vs. ".join((calc_base.engine, calc_comp.engine))] 107 103 print_column_headers(pars_i, labels) 108 104 first = False … … 110 106 num_good += 1 111 107 else: 112 print(("%d,"%seed)+','.join("% g"%v for v in columns))108 print(("%d,"%seed)+','.join("%s"%v for v in columns)) 113 109 print('"good","%d/%d","max diff",%g'%(num_good, N, max_diff[0])) 114 110 … … 144 140 is set in compare.py defaults for each model. 145 141 146 PRECISION is the floating point precision to use for comparisons. 142 PRECISION is the floating point precision to use for comparisons. If two 143 precisions are given, then compare one to the other, ignoring sasview. 147 144 148 145 Available models: … … 151 148 152 149 def main(): 153 if len(sys.argv) != 6:150 if len(sys.argv) not in (6,7): 154 151 print_help() 155 152 sys.exit(1) … … 167 164 mono = sys.argv[4] == 'mono' 168 165 cutoff = float(sys.argv[4]) if not mono else 0 169 precision = sys.argv[5] 166 base = sys.argv[5] 167 comp = sys.argv[6] if len(sys.argv) > 6 else "sasview" 170 168 except: 171 169 traceback.print_exc() … … 173 171 sys.exit(1) 174 172 175 data, index = make_data(qmax=1.0, is2D=is2D, Nq=Nq) 173 data, index = make_data({'qmax':1.0, 'is2d':is2D, 'nq':Nq, 'res':0., 174 'accuracy': 'Low', 'view':'log'}) 176 175 model_list = [model] if model != "all" else MODELS 177 176 for model in model_list: 178 177 compare_instance(model, data, index, N=count, mono=mono, 179 cutoff=cutoff, precision=precision)178 cutoff=cutoff, base=base, comp=comp) 180 179 181 180 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.