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