def propertiesIni(self): if os.path.exists(self.propertiesIniFile()): return iniFile.iniFile(self.propertiesIniFile()) else: ini = iniFile.iniFile() ini.original_filename = self.propertiesIniFile() return ini
def __init__(self, parent, ini): QDialog.__init__(self, parent) self.table = QTableWidget(self) self.table.verticalHeader().hide() layout = QGridLayout() layout.addWidget(self.table, 0, 0) button = QPushButton("Update") self.connect(button, SIGNAL("clicked()"), self.doUpdate) layout.addWidget(button, 1, 0) self.setLayout(layout) self.setWindowTitle(self.tr('Settings')) headers = ['parameter', 'value'] self.table.setColumnCount(len(headers)) self.table.setHorizontalHeaderLabels(headers) self.table.verticalHeader().setVisible(False) self.table.setSelectionBehavior(QAbstractItemView.SelectItems) self.table.setSelectionMode(QAbstractItemView.SingleSelection) names = iniFile(os.path.join(os.path.dirname(__file__), 'analysis_defaults.ini')) items = [] self.ini = ini for key in ini.readOrder: if key in names.params: items.append(key) for key in ini.params: if not key in items and key in names.params: items.append(key) nblank = 3 self.rows = len(items) + nblank self.table.setRowCount(self.rows) for irow, key in enumerate(items): item = QTableWidgetItem(str(key)) item.setFlags(item.flags() ^ Qt.ItemIsEditable) self.table.setItem(irow, 0, item) item = QTableWidgetItem(ini.string(key)) self.table.setItem(irow, 1, item) for i in range(nblank): item = QTableWidgetItem(str("")) irow = len(items) + i self.table.setItem(irow, 0, item) item = QTableWidgetItem(str("")) self.table.setItem(irow, 1, item) self.table.resizeRowsToContents() self.table.resizeColumnsToContents() self.table.horizontalHeader().setStretchLastSection(True); self.table.setEditTriggers(QAbstractItemView.AllEditTriggers) h = self.table.verticalHeader().length() + 40 h = min(QApplication.desktop().screenGeometry().height() * 4 / 5, h) self.resize(300, h) self.setAttribute(Qt.WA_DeleteOnClose)
def showSettings(self): """ Callback for action 'Show settings' """ if not self.plotter: QMessageBox.warning(self, "settings", "Open chains first ") return if isinstance(self.iniFile, basestring): self.iniFile = iniFile(self.iniFile) dlg = DialogSettings(self, self.iniFile) dlg.show()
def __init__(self, path="xamppcontrolpanel.glade", root="PrefsWindow", domain=None, **kwargs): path = os.path.join(glade_dir, path) SimpleGladeApp.__init__(self, path, root, domain, **kwargs) self.pathTextBox.set_text(self.parent.get_xampp_path()) self.HTTPD = self.parent.get_xampp_path().strip() + '/etc/httpd.conf' self.MySQL = self.parent.get_xampp_path().strip() + '/etc/my.cnf' self.BACKUP = self.parent.get_xampp_path().strip() + '/etc/bu_httpd.conf' self.get_Apache_Setting() self.sqlFile = iniFile(self.MySQL) self.get_SQL_Setting() self.modified = False
def __init__(self, path="xampp-control-panel.glade", root="MainWindow", domain=None, **kwargs): path = os.path.join(glade_dir, path) SimpleGladeApp.__init__(self, path, root, domain, **kwargs) # default location of config file for this user (~/.xampp_cp); # config file has format specified for ConfigParser module, e.g. # [main] # xampp_path=/opt/lampp self.CONFIG_FILE_PATH = posix.environ['HOME'] + "/xampp_cp" self.CONFIG_FILE = iniFile(self.CONFIG_FILE_PATH) # set up the stop buttons that are hidden at the start - the execute buttons were created in glade :) self.mainStopButton = gtk.Button(None, gtk.STOCK_MEDIA_STOP) self.mainStopButton.set_border_width(5) self.mainStopButton.connect("clicked", self.on_mainStopButton_clicked) self.vbox1.pack_start(self.mainStopButton, True, False) self.vbox1.reorder_child(self.mainStopButton, 0) self.mainStopButton.hide() self.apacheStopButton = gtk.Button(None, gtk.STOCK_MEDIA_STOP) self.apacheStopButton.set_border_width(0) self.apacheStopButton.connect("clicked", self.on_apacheStopButton_clicked) self.table1.attach(self.apacheStopButton, 2, 3, 0, 1) self.apacheStopButton.hide() self.mysqlStopButton = gtk.Button(None, gtk.STOCK_MEDIA_STOP) self.mysqlStopButton.set_border_width(0) self.mysqlStopButton.connect("clicked", self.on_mysqlStopButton_clicked) self.table1.attach(self.mysqlStopButton, 2, 3, 1, 2) self.mysqlStopButton.hide() self.proftpdStopButton = gtk.Button(None, gtk.STOCK_MEDIA_STOP) self.proftpdStopButton.set_border_width(0) self.proftpdStopButton.connect("clicked", self.on_proftpdStopButton_clicked) self.table1.attach(self.proftpdStopButton, 2, 3, 2, 3) self.proftpdStopButton.hide() # get path configuration from .xcp file xampppath = self.get_xampp_path() if xampppath == "": self.set_xampp_path("/opt/lampp") # initialise the interface self.init_interface()
def num_unequal(filename, cmpFcn): """ Check whether two files are numerically unequal for the given compare function. :param filename: The base name of the files to check. :param cmpFcn: The default comparison function. Can be overriden by the filetolmatrix. :return: True, when the files do not match, false else. """ orig_name = os.path.join(args.ini_dir, args.diff_to, filename) with open(orig_name) as f: origMat = [[x for x in ln.split()] for ln in f] # Check if the first row has one more column, which is the # if len(origMat[0]) == len(origMat[1]) + 1: origBase = 1 origMat[0] = origMat[0][1:] else: origBase = 0 new_name = os.path.join(args.ini_dir, args.out_files_dir, filename) with open(new_name) as f: newMat = [[x for x in ln.split()] for ln in f] if len(newMat[0]) == len(newMat[1]) + 1: newBase = 1 newMat[0] = newMat[0][1:] else: newBase = 0 if len(origMat) - origBase != len(newMat) - newBase: if args.verbose_diff_output: printlog('num rows do not match in %s: %d != %d' % (filename, len(origMat), len(newMat))) return True if newBase == 1: cols = [s[0] + 'x' + s[1] if len(s) == 2 and s != 'nu' else s for s in newMat[0]] else: cols = range(len(newMat[0])) tolerances = get_tolerance_vector(filename, cols) row = 0 col = 0 try: if tolerances: inifilenameparts = filename.rsplit('_', 2) inifilename = '_'.join(inifilenameparts[0:2]) if inifilenameparts[1] != 'transfer' else inifilenameparts[0] inifilename += "_params.ini" inifilename = os.path.join(args.ini_dir, args.out_files_dir, inifilename) try: # The following split fails for *_transfer_out.* files where it not needed anyway. inifile = iniFile() inifile.readFile(inifilename) except: printlog("Could not open inifilename: %s" % inifilename) for o_row, n_row in zip(origMat[origBase:], newMat[newBase:]): row += 1 if len(o_row) != len(n_row): if args.verbose_diff_output: printlog('num columns do not match in %s: %d != %d' % (filename, len(o_row), len(n_row))) return True col = 0 of_row = [float(f) for f in o_row] nf_row = [] for f in n_row: try: nf_row += [float(f)] except ValueError: sp = customsplit(f) nf_row += [ float(sp[0] + 'E' + sp[1]) ] oldrowdict = False newrowdict = False for o, n in zip(of_row, nf_row): if isinstance(tolerances[col], Ignore): pass else: cond, tols = tolerances[col] # When the column condition is bool (True or False) or a function # returning False, then skip this column. if isinstance(cond, bool) or not cond(inifile): pass else: if isinstance(tols, float): if not cmpFcn(o, n, tols): if args.verbose_diff_output: printlog('value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True elif not isinstance(tols, Ignore): if not oldrowdict: oldrowdict = dict(zip(cols, of_row)) newrowdict = dict(zip(cols, nf_row)) if isinstance(tols, list): cand = False for lim, rhs in tols: if lim < newrowdict["L"]: cand = rhs else: break if isinstance(cand, float): if not cmpFcn(o, n, cand): if args.verbose_diff_output: printlog('value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True elif not isinstance(cand, Ignore): if not cand(oldrowdict, newrowdict): if args.verbose_diff_output: printlog('value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True else: if not tols(oldrowdict, newrowdict): if args.verbose_diff_output: printlog('value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True col += 1 return False else: # if args.verbose_diff_output: # printlog("Skipped file %s" % (filename)) return False except ValueError as e: printlog("ValueError: '%s' at %d, %d in file: %s" % (e.message, row, col + 1, filename)) return True
# the plotting matlab run is optional and only if you are using plot_ext=m in getdist plot_types = ['.', '_2D.'] # you don't need these for python plots generated separately # '_tri.m' is very slow for so many if args.plot_data is None: data_dir = batch.batchPath + 'plot_data' + os.sep else: data_dir = os.path.abspath(args.plot_data) + os.sep ini_dir = batch.batchPath + 'getdist' + os.sep checkDir(data_dir) checkDir(ini_dir) if not args.plots: for jobItem in Opts.filteredBatchItems(): ini = iniFile.iniFile() ini.params['file_root'] = jobItem.chainRoot checkDir(jobItem.distPath) ini.params['out_dir'] = jobItem.distPath ini.params['plot_data_dir'] = data_dir custom_plot = batch.commonPath + 'plots' + os.sep + jobItem.paramtag + '.ini' custom_plot2 = batch.commonPath + 'plots' + os.sep + jobItem.name + '.ini' if os.path.exists(custom_plot2): ini.includes.append(custom_plot2) elif os.path.exists(custom_plot): ini.includes.append(custom_plot) elif len(jobItem.param_set) > 0: ini.params['plot_2D_param'] = jobItem.param_set[0] ini.defaults.append(batch.commonPath + base_ini) tag = '' if jobItem.isImportanceJob or args.burn_removed:
# Get CAMB input parameters from best-fit .minimum file from cosmomc import sys, os, iniFile from getdist import ResultObjs if len(sys.argv) < 3: print 'Usage: python/bestFitCAMB.py chain_root iniName' sys.exit() root = os.path.abspath(sys.argv[1]) pars = {'ombh2':'omegabh2', 'omch2':'omegach2', 'omnuh2':'omeganuh2', 'hubble':'H0', 'w':'w', 'helium_fraction':'yheused', 'scalar_amp(1)':'A' , 'scalar_spectral_index(1)':'ns', 'scalar_nrun(1)':'nrun', 'initial_ratio(1)':'r', 're_optical_depth':'tau', 're_delta_redshift':'deltazrei', 'massless_neutrinos':'nnu'} ini = iniFile.iniFile() ini.params['re_use_optical_depth'] = True ini.params['temp_cmb'] = 2.7255 ini.params['CMB_outputscale'] = 2.7255e6 ** 2. ini.defaults.append('params.ini') bf = ResultObjs.bestFit(root + '.minimum', setParamNameFile=root + '.paramnames', want_fixed=True) for camb, cosmomc in pars.items(): par = bf.parWithName(cosmomc) if par is not None: ini.params[camb] = par.best_fit ini.params['scalar_amp(1)'] = float(ini.params['scalar_amp(1)']) / 1e9 nmassive = 1 neffstandard = 3.046 / 3
def loadDataset(self, froot): if not '.dataset' in froot: froot += '.dataset' ini = iniFile.iniFile(froot) self.readIni(ini)
def num_unequal(filename, cmpFcn): """ Check whether two files are numerically unequal for the given compare function. :param filename: The base name of the files to check. :param cmpFcn: The default comparison function. Can be overriden by the filetolmatrix. :return: True, when the files do not match, false else. """ orig_name = os.path.join(args.ini_dir, args.diff_to, filename) with open(orig_name) as f: origMat = [[x for x in ln.split()] for ln in f] # Check if the first row has one more column, which is the # if len(origMat[0]) == len(origMat[1]) + 1: origBase = 1 origMat[0] = origMat[0][1:] else: origBase = 0 new_name = os.path.join(args.ini_dir, args.out_files_dir, filename) with open(new_name) as f: newMat = [[x for x in ln.split()] for ln in f] if len(newMat[0]) == len(newMat[1]) + 1: newBase = 1 newMat[0] = newMat[0][1:] else: newBase = 0 if len(origMat) - origBase != len(newMat) - newBase: if args.verbose_diff_output: printlog('num rows do not match in %s: %d != %d' % (filename, len(origMat), len(newMat))) return True if newBase == 1: cols = [ s[0] + 'x' + s[1] if len(s) == 2 and s != 'nu' else s for s in newMat[0] ] else: cols = range(len(newMat[0])) tolerances = get_tolerance_vector(filename, cols) row = 0 col = 0 try: if tolerances: inifilenameparts = filename.rsplit('_', 2) inifilename = '_'.join( inifilenameparts[0:2] ) if inifilenameparts[1] != 'transfer' else inifilenameparts[0] inifilename += "_params.ini" inifilename = os.path.join(args.ini_dir, args.out_files_dir, inifilename) try: # The following split fails for *_transfer_out.* files where it not needed anyway. inifile = iniFile() inifile.readFile(inifilename) except: printlog("Could not open inifilename: %s" % inifilename) for o_row, n_row in zip(origMat[origBase:], newMat[newBase:]): row += 1 if len(o_row) != len(n_row): if args.verbose_diff_output: printlog('num columns do not match in %s: %d != %d' % (filename, len(o_row), len(n_row))) return True col = 0 of_row = [float(f) for f in o_row] nf_row = [] for f in n_row: try: nf_row += [float(f)] except ValueError: sp = customsplit(f) nf_row += [float(sp[0] + 'E' + sp[1])] oldrowdict = False newrowdict = False for o, n in zip(of_row, nf_row): if isinstance(tolerances[col], Ignore): pass else: cond, tols = tolerances[col] # When the column condition is bool (True or False) or a function # returning False, then skip this column. if isinstance(cond, bool) or not cond(inifile): pass else: if isinstance(tols, float): if not cmpFcn(o, n, tols): if args.verbose_diff_output: printlog( 'value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True elif not isinstance(tols, Ignore): if not oldrowdict: oldrowdict = dict(zip(cols, of_row)) newrowdict = dict(zip(cols, nf_row)) if isinstance(tols, list): cand = False for lim, rhs in tols: if lim < newrowdict["L"]: cand = rhs else: break if isinstance(cand, float): if not cmpFcn(o, n, cand): if args.verbose_diff_output: printlog( 'value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True elif not isinstance(cand, Ignore): if not cand(oldrowdict, newrowdict): if args.verbose_diff_output: printlog( 'value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True else: if not tols(oldrowdict, newrowdict): if args.verbose_diff_output: printlog( 'value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True col += 1 return False else: # if args.verbose_diff_output: # printlog("Skipped file %s" % (filename)) return False except ValueError as e: printlog("ValueError: '%s' at %d, %d in file: %s" % (e.message, row, col + 1, filename)) return True
def makeGrid(batchPath, settingName=None, settings=None, readOnly=False, interactive=False): batchPath = os.path.abspath(batchPath) + os.sep # 0: chains, 1: importance sampling, 2: best-fit, 3: best-fit and Hessian cosmomcAction = 0 if not settings: if not settingName: if not pathIsGrid(batchPath): raise Exception('Need to give name of setting file if batchPath/config does not exist') readOnly = True sys.path.insert(0, batchPath + 'config') settings = __import__(iniFile.iniFile(batchPath + 'config/config.ini').params['setting_file'].replace('.py', '')) else: settings = __import__(settingName, fromlist=['dummy']) batch = batchJob.batchJob(batchPath, settings.ini_dir) # priors and widths for parameters which are varied if not hasattr(settings, 'params'): params = dict() params['mnu'] = '0.02 0 5 0.1 0.03' params['omegak'] = '-0.0008 -0.3 0.3 0.001 0.001' # starting exactly on flat seems to confuse minimizer params['w'] = '-0.995 -3 -0.3 0.02 0.02' params['nnu'] = '3.046 0.05 10 0.05 0.05' params['nrun'] = '0 -1 1 0.005 0.001' params['nrunrun'] = '0 -1 1 0.005 0.001' params['r'] = '0 0 3 0.03 0.03' params['Alens'] = '1 0 10 0.05 0.05' params['yhe'] = '0.245 0.1 0.5 0.006 0.006' params['alpha1'] = '0 -1 1 0.0003 0.0003' params['deltazrei'] = '0.5 0.1 3 0.3 0.3' params['wa'] = '0 -2 2 0.3 0.3' params['meffsterile'] = '0.1 0 3 0.1 0.03' params['Aphiphi'] = '1 0 10 0.02 0.02' params['Alensf'] = '1 0 10 0.03 0.03' params['nt'] = '0 -3 3 0.2 0.02' settings.params = params if hasattr(settings, 'skip'): batch.skip = settings.skip batch.makeItems(settings, messages=not readOnly) if readOnly: for jobItem in [b for b in batch.jobItems]: if not jobItem.chainExists(): batch.jobItems.remove(jobItem) batch.save() print 'OK, configured grid with %u existing chains' % (len(batch.jobItems)) return batch else: batch.makeDirectories(settings.__file__) batch.save() start_at_bestfit = getattr(settings, 'start_at_bestfit', False) for jobItem in batch.items(wantSubItems=False): jobItem.makeChainPath() ini = iniFile.iniFile() for param in jobItem.param_set: ini.params['param[' + param + ']'] = settings.params[param] if 'mnu' in jobItem.param_set: ini.params['num_massive_neutrinos'] = 3 if 'meffsterile' in jobItem.param_set: ini.params['param[mnu]'] = '0.06' ini.params['param[nnu]'] = '3.1 3.046 10 0.05 0.05' ini.params['num_massive_neutrinos'] = 1 ini.params['accuracy_level'] = 1.2 # to use 4 rather than 3 momentum modes if 'yhe' in jobItem.param_set: ini.params['bbn_consistency'] = False if 'r' in jobItem.param_set: ini.params['compute_tensors'] = True if 'nt' in jobItem.param_set: ini.params['inflation_consistency'] = False ini.params['lmax_tensor'] = 1000 # ini.params['pivot_k'] = 0.002 if hasattr(settings, 'extra_opts'): ini.params.update(settings.extra_opts) ini.params['file_root'] = jobItem.chainRoot cov_dir_name = getattr(settings, 'cov_dir', 'planck_covmats') covdir = os.path.join(batch.basePath, cov_dir_name) covmat = os.path.join(covdir, jobItem.name + '.covmat') if not os.path.exists(covmat): covNameMappings = getattr(settings, 'covNameMappings', None) mapped_name_norm = jobItem.makeNormedName(covNameMappings)[0] covmat_normed = os.path.join(covdir, mapped_name_norm + '.covmat') covmat = covmat_normed if not os.path.exists(covmat) and hasattr(jobItem.data_set, 'covmat'): covmat = batch.basePath + jobItem.data_set.covmat if not os.path.exists(covmat) and hasattr(settings, 'covmat'): covmat = batch.basePath + settings.covmat if os.path.exists(covmat): ini.params['propose_matrix'] = covmat if getattr(settings, 'newCovmats', True): ini.params['MPI_Max_R_ProposeUpdate'] = 20 else: hasCov = False ini.params['MPI_Max_R_ProposeUpdate'] = 20 covmat_try = [] if 'covRenamer' in dir(settings): covmat_try += settings.covRenamer(jobItem.name) covmat_try += settings.covRenamer(mapped_name_norm) if hasattr(settings, 'covrenames'): for aname in [jobItem.name, mapped_name_norm]: covmat_try += [aname.replace(old, new, 1) for old, new in settings.covrenames if old in aname] for new1, old1 in settings.covrenames: if old1 in aname: name = aname.replace(old1, new1, 1) covmat_try += [name.replace(old, new, 1) for old, new in settings.covrenames if old in name] if 'covWithoutNameOrder' in dir(settings): if covNameMappings: removes = copy.deepcopy(covNameMappings) else: removes = dict() for name in settings.covWithoutNameOrder: if name in jobItem.data_set.names: removes[name] = '' covmat_try += [jobItem.makeNormedName(removes)[0]] covdir2 = os.path.join(batch.basePath, getattr(settings, 'cov_dir_fallback', cov_dir_name)) for name in covmat_try: covmat = os.path.join(batch.basePath, covdir2, name + '.covmat') if os.path.exists(covmat): ini.params['propose_matrix'] = covmat print 'covmat ' + jobItem.name + ' -> ' + name hasCov = True break if not hasCov: print 'WARNING: no matching specific covmat for ' + jobItem.name ini.params['start_at_bestfit'] = start_at_bestfit updateIniParams(ini, jobItem.data_set.params, batch.commonPath) for deffile in settings.defaults: ini.defaults.append(batch.commonPath + deffile) if hasattr(settings, 'override_defaults'): ini.defaults = [batch.commonPath + deffile for deffile in settings.override_defaults] + ini.defaults ini.params['action'] = cosmomcAction ini.saveFile(jobItem.iniFile()) if not start_at_bestfit: setMinimize(jobItem, ini) variant = '_minimize' ini.saveFile(jobItem.iniFile(variant)) # add ini files for importance sampling runs for imp in jobItem.importanceJobs(): if batch.hasName(imp.name.replace('_post', '')): raise Exception('importance sampling something you already have?') for minimize in (False, True): if minimize and not getattr(imp, 'want_minimize', True): continue ini = iniFile.iniFile() updateIniParams(ini, imp.importanceSettings, batch.commonPath) if cosmomcAction == 0 and not minimize: for deffile in settings.importanceDefaults: ini.defaults.append(batch.commonPath + deffile) ini.params['redo_outroot'] = imp.chainRoot ini.params['action'] = 1 else: ini.params['file_root'] = imp.chainRoot if minimize: setMinimize(jobItem, ini) variant = '_minimize' else: variant = '' ini.defaults.append(jobItem.iniFile()) ini.saveFile(imp.iniFile(variant)) if cosmomcAction != 0: break if not interactive: return batch print 'Done... to run do: python python/runbatch.py ' + batchPath if not start_at_bestfit: print '....... for best fits: python python/runbatch.py ' + batchPath + ' --minimize' print '' print 'for importance sampled: python python/runbatch.py ' + batchPath + ' --importance' print 'for best-fit for importance sampled: python python/runbatch.py ' + batchPath + ' --importance_minimize'