Changeset 7cf2cfd in sasmodels for compare_many.py
- Timestamp:
- Nov 22, 2015 9:37:15 PM (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:
- 3b4243d
- Parents:
- 677ccf1
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
compare_many.py
rab55943 r7cf2cfd 1 1 #!/usr/bin/env python 2 3 2 import sys 3 import traceback 4 4 5 5 import numpy as np 6 6 7 from sasmodels import core 7 8 from sasmodels.kernelcl import environment 8 9 from compare import (MODELS, randomize_model, suppress_pd, eval_sasview, 9 eval_opencl, eval_ctypes, make_data, get_demo_pars) 10 eval_opencl, eval_ctypes, make_data, get_demo_pars, 11 columnize) 10 12 11 13 def get_stats(target, value, index): … … 13 15 relerr = resid/target[index] 14 16 srel = np.argsort(relerr) 15 p90 = int(len(relerr)*0.90)17 #p90 = int(len(relerr)*0.90) 16 18 p95 = int(len(relerr)*0.95) 17 19 maxrel = np.max(relerr) … … 27 29 groups.append(p) 28 30 groups.extend(['']*(len(stats)-1)) 31 groups.append("Parameters") 29 32 columns = ['Seed'] + stats*len(parts) + list(sorted(pars.keys())) 30 33 print(','.join('"%s"'%c for c in groups)) … … 32 35 33 36 def compare_instance(name, data, index, N=1, mono=True, cutoff=1e-5): 37 model_definition = core.load_model_definition(name) 34 38 pars = get_demo_pars(name) 35 39 header = '\n"Model","%s","Count","%d"'%(name, N) 36 40 if not mono: header += ',"Cutoff",%g'%(cutoff,) 37 41 print(header) 42 43 # Stuff the failure flag into a mutable object so we can update it from 44 # within the nested function. Note that the nested function uses "pars" 45 # which is dynamically scoped, not lexically scoped in this context. That 46 # is, pars is replaced each time in the loop, so don't assume that it is 47 # the default values defined above. 48 def trymodel(fn, *args, **kw): 49 try: 50 result, _ = fn(model_definition, pars, data, *args, **kw) 51 except: 52 result = np.NaN 53 traceback.print_exc() 54 return result 55 56 num_good = 0 38 57 first = True 39 58 for _ in range(N): … … 41 60 if mono: suppress_pd(pars) 42 61 43 target, _ = eval_sasview(name, pars, data) 62 # Force parameter constraints on a per-model basis. 63 if name in ('teubner_strey','broad_peak'): 64 pars['scale'] = 1.0 65 #if name == 'parallelepiped': 66 # pars['a_side'],pars['b_side'],pars['c_side'] = \ 67 # sorted([pars['a_side'],pars['b_side'],pars['c_side']]) 44 68 45 env = environment() 46 gpu_single_value,_ = eval_opencl(name, pars, data, dtype='single', cutoff=cutoff) 47 gpu_single = get_stats(target, gpu_single_value, index) 48 if env.has_double: 49 gpu_double_value,_ = eval_opencl(name, pars, data, dtype='double', cutoff=cutoff) 50 gpu_double = get_stats(target, gpu_double_value, index) 69 70 good = True 71 labels = [] 72 columns = [] 73 if 1: 74 sasview_value = trymodel(eval_sasview) 75 if 0: 76 gpu_single_value = trymodel(eval_opencl, dtype='single', cutoff=cutoff) 77 stats = get_stats(sasview_value, gpu_single_value, index) 78 columns.extend(stats) 79 labels.append('GPU single') 80 good = good and (stats[0] < 1e-14) 81 if 0 and environment().has_double: 82 gpu_double_value = trymodel(eval_opencl, dtype='double', cutoff=cutoff) 83 stats = get_stats(sasview_value, gpu_double_value, index) 84 columns.extend(stats) 85 labels.append('GPU double') 86 good = good and (stats[0] < 1e-14) 87 if 1: 88 cpu_double_value = trymodel(eval_ctypes, dtype='double', cutoff=cutoff) 89 stats = get_stats(sasview_value, cpu_double_value, index) 90 columns.extend(stats) 91 labels.append('CPU double') 92 good = good and (stats[0] < 1e-14) 93 if 0: 94 stats = get_stats(cpu_double_value, gpu_single_value, index) 95 columns.extend(stats) 96 labels.append('single/double') 97 good = good and (stats[0] < 1e-14) 98 99 columns += [v for _,v in sorted(pars.items())] 100 if first: 101 print_column_headers(pars, labels) 102 first = False 103 if good: 104 num_good += 1 51 105 else: 52 gpu_double = [0]*len(gpu_single) 53 cpu_double_value,_ = eval_ctypes(name, pars, data, dtype='double', cutoff=cutoff) 54 cpu_double = get_stats(target, cpu_double_value, index) 55 single_double = get_stats(cpu_double_value, gpu_single_value, index) 106 print(("%d,"%seed)+','.join("%g"%v for v in columns)) 107 print '"%d/%d good"'%(num_good, N) 56 108 57 values = (list(gpu_single) + list(gpu_double) + list(cpu_double)58 + list(single_double) + [v for _,v in sorted(pars.items())])59 if gpu_single[0] > 5e-5:60 if first:61 print_column_headers(pars,'GPU single|GPU double|CPU double|single/double'.split('|'))62 first = False63 print(("%d,"%seed)+','.join("%g"%v for v in values))64 109 65 def main(): 66 try: 67 model = sys.argv[1] 68 assert (model in MODELS) or (model == "all") 69 count = int(sys.argv[2]) 70 is2D = sys.argv[3].startswith('2d') 71 assert sys.argv[3][1] == 'd' 72 Nq = int(sys.argv[3][2:]) 73 mono = sys.argv[4] == 'mono' 74 cutoff = float(sys.argv[4]) if not mono else 0 75 except: 76 import traceback; traceback.print_exc() 77 models = "\n ".join("%-7s: %s"%(k,v.__name__.replace('_',' ')) 78 for k,v in sorted(MODELS.items())) 79 print("""\ 80 usage: compare_many.py MODEL COUNT (1dNQ|2dNQ) (CUTOFF|mono) 110 def print_usage(): 111 print "usage: compare_many.py MODEL COUNT (1dNQ|2dNQ) (CUTOFF|mono)" 81 112 82 MODEL is the model name of the model, which is one of: 83 %s 84 or "all" for all the models in alphabetical order. 113 114 def print_models(): 115 print(columnize(MODELS, indent=" ")) 116 117 118 def print_help(): 119 print_usage() 120 print("""\ 121 122 MODEL is the model name of the model or "all" for all the models 123 in alphabetical order. 85 124 86 125 COUNT is the number of randomly generated parameter sets to try. A value … … 98 137 choice of polydisperse parameters, and the number of points in the distribution 99 138 is set in compare.py defaults for each model. 100 """%(models,)) 139 140 Available models: 141 """) 142 print_models() 143 144 def main(): 145 if len(sys.argv) == 1: 146 print_help() 147 sys.exit(1) 148 149 model = sys.argv[1] 150 if not (model in MODELS) and (model != "all"): 151 print 'Bad model %s. Use "all" or one of:' 152 print_models() 153 sys.exit(1) 154 try: 155 count = int(sys.argv[2]) 156 is2D = sys.argv[3].startswith('2d') 157 assert sys.argv[3][1] == 'd' 158 Nq = int(sys.argv[3][2:]) 159 mono = sys.argv[4] == 'mono' 160 cutoff = float(sys.argv[4]) if not mono else 0 161 except: 162 print_usage() 101 163 sys.exit(1) 102 164
Note: See TracChangeset
for help on using the changeset viewer.