|
GAMBIT
v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
|
Go to the documentation of this file. 32 from multiprocessing import cpu_count 36 default_contour = 95.4 37 default_contour_style = "Solid" 38 default_contour_width = 2 39 default_colour = "blue" 40 default_n_contours = 150 41 n_threads = cpu_count() 46 if isinstance(keys, basestring): 47 error = " Region \""+name+ "\" must have key \""+ str(keys)+ "\"." 48 is_ok = (keys in node) 50 error = " Region \""+name+ "\" must have at least one of the keys "+ str(keys)+ "." 55 if not is_ok: sys.exit(error) 59 sys.exit( '\n Usage: colouring [prime/combine] file1 file2\n where file1 is a YAML file that defines the regions you want to find.\n file2 is the pip file that defines the pippi run to modify.\n') 65 commandLineOptions = { 'prime':prime, 'combine':combine } 67 if (len(arguments) is not 4): 75 command = commandLineOptions[arguments[1]] 76 command(arguments[2], arguments[3]) 79 print 'Unrecognised colouring mode: '+arguments[1] 86 def prime(region_file, pip_file): 91 yaml_loader = yaml.full_load if hasattr(yaml, 'full_load') else yaml.load 92 regions = yaml_loader(file(region_file, 'r')) 93 pip = file(pip_file, 'r').read() 96 if regions[ "drawing_order"]: 97 drawing_order = regions[ "drawing_order"] 99 sys.exit( 'ERROR: No drawing_order specified in '+region_file) 103 parse_dir = re.search( 'parse_dir\s*=\s*("(.*)"|\'(.*)\')', pip) 104 parse_dir = parse_dir.group(2 if parse_dir.group(2) else 3) 106 chain_name = re.search( 'main_chain\s*=\s*("(.*)"|\'(.*)\')', pip) 107 chain_name = chain_name.group(2 if chain_name.group(2) else 3) 108 chain_name = re.sub( r'.*/|\..?.?.?$', '', chain_name) 109 best_filename = parse_dir+ "/"+chain_name+ ".best" 111 best_file = file(best_filename, 'r') 112 for x in best_file.readlines(): 113 if not x.startswith( '#'): 115 bestFit = float(parts[1]) 120 for name in drawing_order: 121 if (verbose): print "Priming pippi run for extracting region: "+name 122 mechanism = regions[name] 125 check_node(mechanism, name, [ "extra_1D_plots", "extra_2D_plots"]) 131 newpip = copy.deepcopy(pip) 134 contour = default_contour 135 if ( "contour_levels" in mechanism): contour = mechanism[ "contour_levels"] 136 newpip = re.sub( 'contour_levels\s*=.*\n', 'contour_levels = '+ str(contour)+ '\n', newpip) 137 if ( "extra_1D_plots" in mechanism): newpip = re.sub( 'oneD_plot_quantities\s*=\s*', 'oneD_plot_quantities = '+ str(mechanism[ "extra_1D_plots"])+ ' ', newpip) 138 if ( "extra_2D_plots" in mechanism): newpip = re.sub( 'twoD_plot_quantities\s*=\s*', 'twoD_plot_quantities = '+ str(mechanism[ "extra_2D_plots"])+ ' ', newpip) 139 newpip = re.sub( "bf_lnlike_for_profile_like\s*=\s*", "bf_lnlike_for_profile_like = "+ str(bestFit)+ " ", newpip) 142 newpip = re.sub( 'parse_dir\s*=.*\n', 'parse_dir = \'parse_'+name+ '\'\n', newpip) 143 newpip = re.sub( 'data_ranges\s*=\s*', 'data_ranges = '+mechanism[ "cut"]+ ' ', newpip) 144 if ( "preamble" in mechanism): newpip = re.sub( 'preamble\s*=.*\n', 'preamble = \''+mechanism[ "preamble"]+ '\'\n', newpip) 145 newpip = re.sub( 'assign_to_pippi_datastream\s*=\s*', 'assign_to_pippi_datastream = ' 146 +mechanism[ "datastream"]+ ' \\\n ', newpip) 147 newpip = re.sub( 'quantity_labels\s*=\s*', 'quantity_labels = ' 148 +mechanism[ "label"]+ ' \\\n ', newpip) 151 newpip = re.sub( 'script_dir\s*=.*\n', 'script_dir = \'scripts_'+name+ '\'\n', newpip) 152 colour = default_colour 153 if ( "colour" in mechanism): colour = mechanism[ "colour"] 154 newpip = re.sub( 'colour_scheme\s*=.*\n', 'colour_scheme = Blockshading_'+colour+ '\n', newpip) 157 newpip = re.sub( 'plot_dir\s*=.*\n', 'plot_dir = \'plots_'+name+ '\'\n', newpip) 160 filename = os.path.splitext(pip_file) 161 filename = filename[0]+ "_"+name+filename[1] 162 with open(filename, "w") as f: 164 print " Generated new pip file "+filename 172 regions = yaml.load(file(region_file, 'r')) 173 pip = file(pip_file, 'r').read() 176 if regions[ "drawing_order"]: 177 drawing_order = regions[ "drawing_order"] 179 sys.exit( 'ERROR: No drawing_order specified in '+region_file) 182 script_dir = re.search( 'script_dir\s*=\s*("(.*)"|\'(.*)\')', pip) 183 script_dir = script_dir.group(2 if script_dir.group(2) else 3) 184 like2D_scripts = [x for x in os.listdir(script_dir) if "like2D" in x] 187 combo_dir = script_dir+ '_combined' 188 if not os.path.isdir(combo_dir): os.mkdir(combo_dir) 191 plot_dir = re.search( 'plot_dir\s*=\s*("(.*)"|\'(.*)\')', pip) 192 plot_dir = plot_dir.group(2 if plot_dir.group(2) else 3) 193 plot_combo_dir = plot_dir+ '_combined' 194 if not os.path.isdir(plot_combo_dir): os.mkdir(plot_combo_dir) 197 bulkscript_name = 'make_combined_'+re.sub( '\.pip', '', pip_file)+ '_plots.bsh' 198 bulkscript = file(bulkscript_name, 'w') 199 bulkscript.write( 'cd '+combo_dir+ '\n') 202 for script_index, script_name in enumerate(like2D_scripts): 208 contour_commands = [] 211 regional_script = None 214 for name in drawing_order: 217 fullname = script_dir+ '_'+name+ '/'+script_name 218 if not os.path.isfile(fullname): continue 219 regional_script = file(fullname, 'r').read() 222 region_names.append(name) 225 contour_style = regions[name][ "contour_style"] if "contour_style" in regions[name] else default_contour_style 228 marker_command = re.findall( '\s\s--draw-marker.*\n', regional_script) 229 if marker_command: marker_commands.append(marker_command[0]) 232 fill_colour = re.findall( '--#...', regional_script) 233 fill_colours.append(fill_colour[1][2:]) 236 plot_command = re.findall( '\s\s--plot.*fill-transparency 1.*\n', regional_script) 237 plot_commands.append(plot_command[0]) 240 contour_command_list = re.findall( '\s\s--draw-contour.*\n', regional_script) 242 for i,x in enumerate(contour_command_list): 243 contour_command_list[i] = re.sub( 'width.*\n', 'width '+ str(default_contour_width)+ '\\\n', x) 244 contour_command_list[i] = re.sub( 'style\s*.*?\s', 'style '+contour_style+ ' ', contour_command_list[i]) 245 min_contour = min(min_contour, float(re.search( '--draw-contour\s(.*?)\s', contour_command_list[i]). group(1))) 246 contour_commands.append(contour_command_list) 247 min_contours.append(min_contour) 250 if (regional_script is None): continue 253 axis_styles = re.findall( '\s\s--axis-style\s*[^y]*\n', regional_script) 256 base_script = file(script_dir+ '/'+script_name, 'r').readlines() 260 for x in base_script[2:]: 261 preamble = preamble+x 262 if x.startswith( ' --xyz-map'): break 265 text = [re.sub( 'White', 'Black',x) for x in base_script if x.startswith( ' --draw-text') or x.startswith( ' --legend')] 267 outfile = open(combo_dir+ '/'+script_name, 'w') 268 outfile.write( '#!/usr/bin/env bash\n') 269 outfile.write( '# This plot script created by the pippi scripter \'colouring\' on '+datetime.datetime.now().strftime( '%c')+ '\n') 272 for region in region_names: outfile.write( 'n_contours_'+re.sub( '-', '_',region)+ '='+ str(default_n_contours)+ '\n') 275 outfile.write(preamble) 278 for region, plot_command in enumerate(plot_commands): 279 outfile.write( ' --color \''+fill_colours[region]+ '\' \\\n') 280 outfile.write(plot_command) 281 outfile.write( ' $(echo $(for (( i=0; i<$n_contours_'+re.sub( '-', '_',region_names[region])+ '; i++ )); do echo "--draw-contour ' 282 '$(echo "'+ str(min_contours[region])+ ' + '+ str(1.0-min_contours[region])+ '*$i/($n_contours_' 283 +re.sub( '-', '_',region_names[region])+ '-1)" | bc -l) /style Solid /width '+ str(default_contour_width)+ ' "; done)) \\\n') 284 outfile.write( ' --draw-contour 1.000 /style Solid /width '+ str(default_contour_width)+ '\\\n') 287 for region, plot_command in enumerate(plot_commands): 288 outfile.write(plot_command) 289 for x in contour_commands[region]: outfile.write(x) 292 for marker_command in marker_commands: 293 outfile.write(marker_command) 296 for x in text: outfile.write(x) 297 for x in axis_styles: outfile.write(x) 300 pdf_name = re.sub( "\.bsh", ".pdf", script_name) 301 bulkscript.write( '(echo "./'+script_name+ '" && ./' + script_name + 302 ' && gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile='+ 303 '../' + plot_combo_dir + '/' + pdf_name + ' ' + pdf_name + 304 ' && rm ' + pdf_name + ') &\n') 305 if (script_index%n_threads == n_threads - 1 ): bulkscript.write( 'wait\n') 309 os.chmod(combo_dir+ '/'+script_name, stat.S_IRWXU | stat.S_IRWXG) 312 bulkscript.write( 'wait\n') 313 bulkscript.write( 'echo "Generated all combined plots. Now go write a paper!"\n') 315 os.chmod(bulkscript_name, stat.S_IRWXU | stat.S_IRWXG)
std::string str Shorthand for a standard string.
def combine(region_file, pip_file)
def check_node(node, name, keys)
def prime(region_file, pip_file)
|