Trees | Indices | Help |
---|
|
1 from __future__ import division 2 import collections 3 import math 4 import os 5 try: 6 import madgraph 7 except ImportError: 8 import internal.sum_html as sum_html 9 import internal.misc as misc 10 import internal.files as files 11 else: 12 import madgraph.madevent.sum_html as sum_html 13 import madgraph.various.misc as misc 14 import madgraph.iolibs.files as files 15 16 pjoin = os.path.join 1719 20 start, stop = -1,1 #original interval 21 22506 507 50824 # information that we need to get to create a new grid 25 self.oneFail = False # check if one splitted job fails to pass cuts 26 self.mc_hel= mc_hel 27 self.grid_base = collections.defaultdict(int) 28 self.original_grid = collections.defaultdict(int) 29 self.non_zero_grid = collections.defaultdict(int) 30 self.ng =0 31 self.maxinvar=0 32 self.nonzero = 0 33 self.max_on_axis = collections.defaultdict(lambda: -1) 34 self.min_on_axis = collections.defaultdict(lambda: 1) 35 # information that we need to evaluate the cross-section/error 36 self.sum_wgt = 0 37 self.sum_abs_wgt = 0 38 self.sum_wgt_square =0 39 self.max_wgt = 0 40 self.nb_ps_point = 0 41 self.target_evt = 0 42 self.nb_sample = 0 43 self.force_max_wgt = [] # list of weight for the secondary unweighting 44 45 # 46 self.results = sum_html.Combine_results('combined') 47 self.discrete_grid = ""48 49 50 5153 return float(value.replace('d','e'))5456 57 self.nb_sample += 1 58 if isinstance(path, str): 59 finput = open(path) 60 elif isinstance(path, file): 61 finput=path 62 else: 63 raise Exception, "path should be a path or a file descriptor" 64 65 line = finput.readline() 66 if self.nonzero == 0: 67 #first information added 68 try: 69 self.nonzero, self.ng, self.maxinvar = [int(i) for i in line.split()] 70 except ValueError: 71 self.nb_ps_point += 500000 72 self.onefail = True 73 return 74 else: 75 info = [self.convert_to_number(i) for i in line.split()] 76 if len(info) == 0: 77 #file empty (should be due to no point pass cuts) 78 self.nb_ps_point += 500000 79 self.oneFail = True 80 return 81 elif len(info) !=3: 82 raise Exception, "wrong formatting of %s"% finput.name 83 84 nonzero, ng, maxinvar = info 85 self.nonzero+=nonzero 86 assert ng == self.ng 87 assert maxinvar == self.maxinvar 88 89 90 line = finput.readline() 91 data = [self.convert_to_number(i) for i in line.split()] 92 for j in range(self.maxinvar): 93 for i in range(self.ng): 94 self.grid_base[(i,j)] += data.pop(0) 95 96 line = finput.readline() 97 data = [self.convert_to_number(i) for i in line.split()] 98 for j in range(self.maxinvar): 99 for i in range(self.ng): 100 self.original_grid[(i,j)] = data.pop(0) 101 102 line = finput.readline() 103 data = [self.convert_to_number(i) for i in line.split()] 104 for j in range(self.maxinvar): 105 for i in range(self.ng): 106 self.non_zero_grid[(i,j)] += int(data.pop(0)) 107 108 #minimal value for each variable of integraion 109 line = finput.readline() 110 data = [self.convert_to_number(i) for i in line.split()] 111 for j in range(self.maxinvar): 112 self.min_on_axis[j] = min(self.min_on_axis[j],data.pop(0)) 113 114 #maximum value for each variable of integraion 115 line = finput.readline() 116 data = [self.convert_to_number(i) for i in line.split()] 117 for j in range(self.maxinvar): 118 self.max_on_axis[j] = max(self.max_on_axis[j],data.pop(0)) 119 120 # cumulative variable for the cross-section 121 line = finput.readline() 122 data = [self.convert_to_number(i) for i in line.split()] 123 self.sum_wgt += data[0] 124 self.sum_abs_wgt += data[1] 125 self.sum_wgt_square += data[2] 126 self.max_wgt = max(self.max_wgt, data[3]) 127 self.nb_ps_point += data[4] 128 if self.target_evt: 129 assert self.target_evt == data[5], "%s != %s" % (self.target_evt, data[5]) 130 else: 131 self.target_evt += data[5] 132 self.force_max_wgt.append(data[6]) 133 134 135 136 # discrete sampler/helicity information 137 if not self.mc_hel: 138 self.helicity_line = finput.readline() 139 140 if not self.discrete_grid: 141 self.discrete_grid = DiscreteSampler(finput) 142 else: 143 self.discrete_grid.add(finput)144 145 146148 149 if isinstance(path, str): 150 finput = open(path) 151 fname = path 152 elif isinstance(path, file): 153 finput=path 154 fname = finput.name 155 else: 156 raise Exception, "path should be a path or a file descriptor" 157 158 159 return self.results.add_results(fname,finput)160 161162 - def write_grid_for_submission(self, Pdir, G, n_split, nb_events, mode='survey', 163 conservative_factor = 1.0):164 """ Generate the grid for the resubmission """ 165 166 Gdirs = [] #build the the list of directory 167 for i in range(n_split): 168 path = pjoin(Pdir, "G%s_%s" % (G, i+1)) 169 Gdirs.append(path) 170 171 # Create the new grid and put it in all directory 172 if not os.path.exists(pjoin(Pdir,"G%s" % G)): 173 os.mkdir(pjoin(Pdir,"G%s" % G)) 174 175 self.write_associate_grid(pjoin(Pdir,"G%s" % G,'ftn25'), nb_events, 176 conservative_factor=conservative_factor, mode=mode) 177 178 # In refine, the link is automatically created by the job 179 if mode=='survey': 180 for Gdir in Gdirs[:]: 181 files.ln(pjoin(Pdir,"G%s" % G, 'ftn25'), Gdir)182185 """use the grid information to create the grid associate""" 186 187 if not self.nb_ps_point: 188 # PS return 0 so pointless to continue 189 return 190 191 new_grid = self.get_new_grid() 192 193 fsock = open(path, 'w') 194 data = [] 195 for var in range(self.maxinvar): 196 for i in range(self.ng): 197 data.append(new_grid[(i,var)]) 198 199 # misc.sprint("plotting grid") 200 # self.plot_grid(new_grid, 1) 201 202 while len(data) >= 4: 203 v1, v2, v3, v4 = data[:4] 204 data = data[4:] 205 fsock.write('%+.16f %+.16f %+.16f %+.16f \n' % (v1, v2, v3, v4)) 206 207 # if data is not a multiple of 4 write the rest. 208 for v in data: 209 fsock.write('%+.16f' % v) 210 if data: 211 fsock.write('\n') 212 213 214 mean = self.sum_wgt*self.target_evt/self.nb_ps_point 215 216 #means that this the max number of iteration 217 # The division by max_iter is just to be more conservative 218 twgt = mean / 8.0 / nb_event 219 trunc_max = 0.10 220 force_max_wgt = self.get_max_wgt(trunc_max) 221 222 fsock.write('%s %s \n' %(twgt, force_max_wgt)) 223 224 if not self.mc_hel: 225 fsock.write(self.helicity_line) 226 227 # At this stage we chose the small_contrib_threshold and damping power 228 # parameter of the discrete grids depending on the mode currently being 229 # run. 230 for dimension in self.discrete_grid.values(): 231 if mode=='survey': 232 # For the survey, we use the conservative 3% with the third root 233 dimension.small_contrib_threshold = 0.03 234 dimension.damping_power = 1.0/3.0 235 elif mode=='refine': 236 # For the refine, we use the more aggressive 0.3% with the square root 237 dimension.small_contrib_threshold = 0.003 238 dimension.damping_power = 0.5 239 else: 240 dimension.small_contrib_threshold = 0.0 241 dimension.damping_power = 0.5 242 243 self.discrete_grid.write(fsock) 244 245 return twgt246248 """Compute the force max weight for the secondary unweighting 249 This correspond to the weight which allow "trunc_max" (1%) of the event 250 to have a weight larger than one.""" 251 252 253 th_maxwgt = [R.th_maxwgt for R in self.results] 254 th_nunwgt = [R.th_nunwgt for R in self.results] 255 nb_data = len(th_nunwgt) 256 total_sum = sum(th_maxwgt[i] * th_nunwgt[i] for i in xrange(nb_data)) 257 info = sorted([ (th_maxwgt[i], th_nunwgt[i]) for i in xrange(nb_data) 258 if th_nunwgt[i] > 0], 259 reverse=True) 260 261 if len(info) == 0: 262 maxwgt = max(th_maxwgt) 263 if maxwgt==0: 264 return -1 265 else: 266 return maxwgt 267 268 xsum = 0 269 nb_event = 0 270 i = 0 271 while (i != len(info) and xsum-info[i][0] * nb_event < trunc_max * total_sum): 272 xsum += info[i][0]*info[i][1] 273 nb_event += info[i][1] 274 i += 1 275 else: 276 # Naively we want to return info[i-1] 277 # We want to be smarter and find the value which has exactly 278 # a trunc_max error. 279 # For this we solve the following equation 280 # xsum_old - X * nb_event_old = trunc_max *total_sum 281 282 wgt = (xsum - trunc_max *total_sum )/ nb_event 283 284 if __debug__: 285 if len(info) == i: 286 assert 0 < wgt < info[i-1][0] 287 else: 288 assert info[i][0] < wgt < info[i-1][0] 289 290 return wgt291293 """return an estimate of the number of unweighted events for a given weight""" 294 295 if max_wgt == -1: 296 max_wgt = self.get_max_wgt() 297 298 #th_maxwgt = [R.th_maxwgt for R in self.results] 299 #th_nunwgt = [R.th_nunwgt for R in self.results] 300 301 # 1. estimate based on the information in results.dat 302 #estimate1 = sum([max(R.nunwgt*R.maxwgt/max_wgt, R.nunwgt) 303 # for R in self.results if R.nunwgt]) 304 305 # 2. estimate based on the information of the theoretical information 306 #info = zip(self.th_maxwgt, self.th_nunwgt) 307 #estimate2 = sum(max(N, N*W/max_wgt, N) for N,W in info) 308 309 # 3. 310 total_nunwgt = 0 311 for i in range(len(self.results)): 312 #take the data 313 maxwgt1 = self.results[i].maxwgt 314 nunwgt1 = self.results[i].nunwgt 315 maxwgt2 = self.results[i].th_maxwgt 316 nunwgt2 = self.results[i].th_nunwgt 317 318 if maxwgt1 > maxwgt2: 319 maxwgt1, maxwgt2 = maxwgt2, maxwgt1 320 nunwgt1, nunwgt2 = nunwgt2, nunwgt1 321 322 assert nunwgt1 >= nunwgt2 323 324 if max_wgt <= maxwgt1: 325 # we can not return more event than those written on he disk 326 total_nunwgt += self.results[i].nunwgt 327 elif max_wgt > maxwgt2: 328 total_nunwgt += nunwgt2 * maxwgt2 / max_wgt 329 else: 330 # solve the equation a+b/x=N with the two know point 331 332 a = (nunwgt1 * maxwgt1 - nunwgt2 * maxwgt2) / (maxwgt1 -maxwgt2) 333 b = maxwgt1 * maxwgt2 / (maxwgt1 -maxwgt2) *(nunwgt2 -nunwgt1) 334 335 to_add = a + b / max_wgt 336 337 assert round(nunwgt2) <= round(to_add) <= round(nunwgt1) 338 total_nunwgt += min(to_add, self.results[i].nunwgt) 339 return total_nunwgt340342 """make a plot of the grid.""" 343 344 try: 345 import matplotlib 346 except Exception: 347 return 348 else: 349 import matplotlib.pyplot as plt 350 import matplotlib.gridspec as gridspec 351 352 gs1 = gridspec.GridSpec(2, 1, height_ratios=[5,1]) 353 gs1.update(wspace=0, hspace=0) # set the spacing between axes. 354 ax = plt.subplot(gs1[0]) 355 data = [] 356 for i in range(self.ng): 357 data.append(grid[(i,var)]) 358 ftnvalues = [(i+1)/len(data)for i in range(len(data)) if i < len(data)] 359 360 ax.plot(data, ftnvalues, label="ftn26") 361 ax.legend() 362 plt.title('grid') 363 ax.set_ylim([0,1]) 364 plt.axis('on') 365 i=0 366 while os.path.exists("matplotlib%s.png" % i): 367 i+=1 368 plt.savefig("matplotlib%s.png" % i) 369 misc.sprint("file save in matplotlib%s.png" % i)370372 """return the cross-section error""" 373 374 if self.nb_ps_point == 0 or self.nonzero == 0: 375 return 0, 0, 0 376 elif self.oneFail: #one of the split fail due to cut but not all of them 377 if self.nonzero < 10*len(self.results): 378 # no real success in any of the run. Declare failure. 379 return 0, 0 , 0 380 381 mean = self.sum_wgt*self.target_evt/self.nb_ps_point 382 rmean = self.sum_abs_wgt*self.target_evt/self.nb_ps_point 383 384 vol = 1/self.target_evt 385 sigma = self.sum_wgt_square/vol**2 386 sigma -= self.nonzero * mean**2 387 sigma /= self.nb_ps_point*(self.nb_ps_point -1) 388 389 return mean, rmean, math.sqrt(abs(sigma))390 391 392 393 394396 397 new_grid = collections.defaultdict(int) 398 399 for var in range(self.maxinvar): 400 one_grid = self.get_new_grid_for_var(var) 401 for j,value in enumerate(one_grid): 402 new_grid[(j,var)] = value 403 404 return new_grid405 406 407409 """return the combine grid for a given variable""" 410 411 #1. biais the grid to allow more points where the fct is zero. 412 grid = collections.defaultdict(int) 413 for i in range(self.ng): 414 if self.non_zero_grid[(i, var)] != 0: 415 factor = min(10000, self.nonzero/self.non_zero_grid[(i,var)]) 416 grid[(i, var)] = self.grid_base[(i, var)] * factor 417 418 419 #2. average the grid 420 def average(a,b,c): 421 if b==0: 422 return 0 423 elif a==0: 424 return (b+c)/2 425 elif c==0: 426 return (a+b)/2 427 else: 428 return (a+b+c)/3429 430 tmp_grid = collections.defaultdict(int) 431 for j in range(self.ng): 432 tmp_grid[(j, var)] = average(grid[(j-1, var)],grid[(j, var)],grid[(j+1, var)]) 433 grid = tmp_grid 434 435 436 437 #3. takes the logs to help the re-binning to converge faster 438 sum_var = sum([grid[(j,var)] for j in range(self.ng)]) 439 for j in range(self.ng): 440 if grid[(j,var)]: 441 x0 = 1e-14+grid[(j,var)]/(sum_var+1e-99) 442 grid[(j,var)] = ((x0-1)/math.log(x0))**1.5 443 444 445 start, stop = 0, self.ng-1 446 start_bin, end_bin = 0, 1 447 test_grid = [0]*self.ng # a first attempt for the new grid 448 449 # special Dealing with first/last bin for handling endpoint. 450 xmin, xmax = self.min_on_axis[var], self.max_on_axis[var] 451 if (xmin- (-1) > (self.original_grid[(1,var)] - self.original_grid[(0,var)])): 452 start = 1 453 start_bin = xmin - (self.original_grid[(1,var)] - self.original_grid[(0,var)])/5 454 test_grid[0] = start_bin 455 else: 456 xmin = -1 457 if (1- xmax) > (self.original_grid[(self.ng-1,var)] - self.original_grid[(self.ng-2, var)]): 458 stop = self.ng -2 459 xmax = xmax + (self.original_grid[(self.ng-1,var)] - self.original_grid[(self.ng-2, var)])/5 460 test_grid[self.ng-1] = xmax 461 else: 462 xmax = 1 463 test_grid[self.ng-1] = xmax 464 465 466 #compute the value in order to have the same amount in each bin 467 sum_var = sum([grid[(j,var)] for j in range(self.ng)]) 468 avg = sum_var / (stop-start+1) 469 cumulative = 0 470 pos_in_original_grid = -1 471 for j in range(start,stop): 472 while cumulative < avg and pos_in_original_grid < self.ng: 473 #the previous bin (if any) is fully belonging to one single bin 474 #of the new grid. adding one to cumulative up to the point that 475 #we need to split it 476 pos_in_original_grid += 1 477 cumulative += grid[(pos_in_original_grid, var)] 478 start_bin = end_bin 479 end_bin = max(xmin,min(xmax,self.original_grid[(pos_in_original_grid, var)])) 480 cumulative -= avg 481 #if pos_in_original_grid == 0: 482 # print grid[(pos_in_original_grid,var)] 483 # print cumulative 484 if end_bin != start_bin and cumulative and grid[(pos_in_original_grid,var)]: 485 test_grid[j] = end_bin - (end_bin-start_bin)*cumulative / \ 486 grid[(pos_in_original_grid,var)] 487 else: 488 test_grid[j] = end_bin 489 490 # Ensure a minimal distance between each element of the grid 491 sanity = True 492 for j in range(1, self.ng): 493 if test_grid[j] - test_grid[j-1] < 1e-14: 494 test_grid[j] = test_grid[j-1] + 1e-14 495 if test_grid[j] > xmax: 496 sanity = False 497 break 498 # not in fortran double check of the sanity from the top. 499 if not sanity: 500 for j in range(1, self.ng): 501 if test_grid[-1*j] > xmax - j * 1e-14: 502 test_grid[-1*j] = xmax - j * 1e-14 503 504 # return the new grid 505 return test_grid510 """ """ 511 516 520625522 """parse the input""" 523 524 # Example of input: 525 # <DiscreteSampler_grid> 526 # Helicity 527 # 10 # Attribute 'min_bin_probing_points' of the grid. 528 # 1 # Attribute 'grid_mode' of the grid. 1=='default',2=='initialization' 529 # 0.01 # Attribute 'small_contrib_threshold' of the grid. 530 # 0.5 # Attribute 'damping_power' of the grid. 531 # # binID n_entries weight weight_sqr abs_weight 532 # 1 255 1.666491280568920E-002 4.274101502263763E-004 1.666491280568920E-002 533 # 2 0 0.000000000000000E+000 0.000000000000000E+000 0.000000000000000E+000 534 # 3 0 0.000000000000000E+000 0.000000000000000E+000 0.000000000000000E+000 535 # 4 235 1.599927969559557E-002 3.935536991290621E-004 1.599927969559557E-002 536 # </DiscreteSampler_grid> 537 538 539 540 if isinstance(fpath, str): 541 if '\n' in fpath: 542 fsock = (line+'\n' for line in fpath.split('\n')) 543 else: 544 fsock = open(fpath) 545 else: 546 fsock =fpath 547 548 while 1: 549 try: 550 line = fsock.next() 551 except StopIteration: 552 break 553 line = line.lower() 554 if '<discretesampler_grid>' in line: 555 grid = self.get_grid_from_file(fsock) 556 tag = (grid.name, grid.grid_type) 557 if mode == 'init' or tag not in self: 558 self[tag] = grid 559 elif mode == 'add': 560 if grid.grid_type == 1 and grid.grid_mode == 1: 561 # reference grid not in init mode. They should 562 #all be the same so no need to make the sum 563 continue 564 self[tag] += grid565567 """read the stream and define the grid""" 568 569 # <DiscreteSampler_grid> 570 # Helicity 571 # 1 # grid_type: 1 for a reference and 2 for a running grid. 572 # 10 # Attribute 'min_bin_probing_points' of the grid. 573 # 1 # Attribute 'grid_mode' of the grid. 1=='default',2=='initialization' 574 # # binID n_entries weight weight_sqr abs_weight 575 # 0.01 # Attribute 'small_contrib_threshold' of the grid. 576 # 0.5 # Attribute 'damping_power' of the grid. 577 # 1 512 7.658545534133427E-003 9.424671508005602E-005 7.658545534133427E-003 578 # 4 478 8.108669631788431E-003 1.009367301168054E-004 8.108669631788431E-003 579 # </DiscreteSampler_grid> 580 581 582 def next_line(fsock): 583 line = fsock.next() 584 if '#' in line: 585 line = line.split('#',1)[0] 586 line = line.strip() 587 if line == '': 588 return next_line(fsock) 589 else: 590 return line591 592 #name 593 firstline = next_line(fsock) 594 if '#' in firstline: 595 firstline = firstline.split('#',1)[0] 596 name = firstline.strip() 597 grid = DiscreteSamplerDimension(name) 598 599 # line 2 grid_type 600 line = next_line(fsock) 601 grid.grid_type = int(line) 602 603 # line 3 min_bin_probing_points 604 line = next_line(fsock) 605 grid.min_bin_probing_points = int(line) 606 607 # line 4 grid_mode 608 line = next_line(fsock) 609 grid.grid_mode = int(line) 610 611 # line 5 small_contrib_threshold 612 line = next_line(fsock) 613 grid.small_contrib_threshold = float(line) 614 615 # line 6 damping_power 616 line = next_line(fsock) 617 grid.damping_power = float(line) 618 619 # line 7 and following grid information 620 line = next_line(fsock) 621 while 'discretesampler_grid' not in line.lower(): 622 grid.add_bin_entry(*line.split()) 623 line = next_line(fsock) 624 return grid627 """write into a file""" 628 629 if isinstance(path, str): 630 fsock = open(path, 'w') 631 else: 632 fsock = path 633 634 for dimension in self.values(): 635 if dimension.grid_type != 1: #1 is for the reference grid 636 continue 637 dimension.update(self[(dimension.name, 2)]) #2 is for the run grid 638 dimension.write(fsock)639 640642 """ """ 643760 761645 646 self.name = name 647 self.min_bin_probing_points = 10 648 self.grid_mode = 1 #1=='default',2=='initialization' 649 self.grid_type = 1 # 1=='ref', 2=='run' 650 # The attribute below controls at which point we damp the probing 651 # of small contributions. It corresponds to the contribution relative 652 # to the averaged contribution of all bins. It must be >=0.0 and < 0.5 653 # typically 654 self.small_contrib_threshold = 0.0 655 # The power of the corresponding damping, typically 0.5 656 self.damping_power = 0.5657659 """update the reference with the associate running grid """ 660 661 assert self.name == running_grid.name 662 assert self.grid_type == 1 and running_grid.grid_type == 2 663 664 if self.grid_mode == 1: 665 #no need of special update just the sum is fine 666 self += running_grid 667 else: 668 self.grid_mode = 1 #end initialisation 669 #need to check if running_grid has enough entry bin per bin 670 # if this is the case take that value 671 # otherwise use the ref one (but rescaled) 672 sum_ref = sum(w.abs_weight for w in self.values()) 673 sum_run = sum(w.abs_weight for w in running_grid.values()) 674 ratio = sum_run / sum_ref 675 sum_ref_sqr = sum(w.weight_sqr for w in self.values()) 676 sum_run_sqr = sum(w.weight_sqr for w in running_grid.values()) 677 ratio_sqr = sum_run_sqr / sum_ref_sqr 678 679 self.min_bin_probing_points = 80 680 for bin_id, bin_info in running_grid.items(): 681 if bin_info.n_entries > self.min_bin_probing_points: 682 bin_ref = self[bin_id] 683 self[bin_id] = bin_info 684 else: 685 wgt_run = bin_info.n_entries / self.min_bin_probing_points 686 wgt_ref = (self.min_bin_probing_points - bin_info.n_entries)/ self.min_bin_probing_points 687 bin_ref = self[bin_id] 688 # modify the entry 689 bin_ref.weight = bin_ref.weight * ratio * wgt_ref + bin_info.weight * wgt_run 690 bin_ref.abs_weight = bin_ref.abs_weight * ratio * wgt_ref + bin_info.abs_weight * wgt_run 691 bin_ref.weight_sqr = bin_ref.weight_sqr *ratio_sqr * wgt_ref + bin_info.weight_sqr * wgt_run 692 bin_ref.n_entries = self.min_bin_probing_points 693 694 #remove bin if entry if zero 695 for key in self.keys(): 696 if not self[key].abs_weight: 697 del self[key] 698 699 700 return self701703 # # binID n_entries weight weight_sqr abs_weight 704 # 3 0 0.000000000000000E+000 0.000000000000000E+000 0.000000000000000E+000 705 # 4 235 1.599927969559557E-002 3.935536991290621E-004 1.599927969559557E-002 706 707 self[bin_id] = Bin_Entry(n_entries, weight, weight_sqr, abs_weight)708710 """adding the entry of the second inside this grid""" 711 712 for bin_id, bin_info in grid.items(): 713 if bin_id in self: 714 self[bin_id] += bin_info 715 else: 716 self[bin_id] = bin_info 717 return self718720 """write the grid in the correct formatted way""" 721 722 if isinstance(path, str): 723 fsock = open(path, 'w') 724 else: 725 fsock = path 726 727 template = """ <DiscreteSampler_grid> 728 %(name)s 729 %(grid_type)s # grid_type. 1=='ref', 2=='run' 730 %(min_bin_probing_points)s # Attribute 'min_bin_probing_points' of the grid. 731 %(grid_mode)s # Attribute 'grid_mode' of the grid. 1=='default',2=='initialization' 732 %(small_contrib)s # Attribute 'small_contrib_threshold' of the grid. 733 %(damping_power)s # Attribute 'damping_power' of the grid. 734 # binID n_entries weight weight_sqr abs_weight 735 %(bins_informations)s 736 </DiscreteSampler_grid> 737 """ 738 739 #order the bin from higest contribution to lowest 740 bins = [o for o in self.items()] 741 def compare(x,y): 742 if x[1].weight - y[1].weight <0: 743 return 1 744 else: 745 return -1746 747 bins.sort(cmp=compare) 748 749 data = {'name': self.name, 750 'min_bin_probing_points': self.min_bin_probing_points, 751 'grid_mode': self.grid_mode, 752 'grid_type': self.grid_type, 753 'bins_informations' : '\n'.join(' %s %s' % (bin_id,str(bin_info)) \ 754 for (bin_id, bin_info) in bins), 755 'small_contrib': '%3.3f'%self.small_contrib_threshold, 756 'damping_power': '%3.3f'%self.damping_power 757 } 758 759 fsock.write(template % data)763 """ One bin (of the Discrite Sampler grid) """ 764797766 """initialize the bin information""" 767 768 self.n_entries = int(n_entries) 769 self.weight = float(weight) 770 self.weight_sqr = float(weight_sqr) 771 self.abs_weight = float(abs_weight)772774 """adding two bin together""" 775 tot_entries = (self.n_entries + other.n_entries) 776 if not tot_entries: 777 return self 778 779 self.weight = (self.n_entries * self.weight + 780 other.n_entries * other.weight) / tot_entries 781 782 self.weight_sqr = (self.n_entries * self.weight_sqr + 783 other.n_entries * other.weight_sqr) / tot_entries 784 785 self.abs_weight = (self.n_entries * self.abs_weight + 786 other.n_entries * other.abs_weight) / tot_entries 787 788 789 self.n_entries = tot_entries 790 791 return self792
Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Thu Dec 20 09:18:12 2018 | http://epydoc.sourceforge.net |