本文整理汇总了Python中menu.Menu.get_choice方法的典型用法代码示例。如果您正苦于以下问题:Python Menu.get_choice方法的具体用法?Python Menu.get_choice怎么用?Python Menu.get_choice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类menu.Menu
的用法示例。
在下文中一共展示了Menu.get_choice方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: change_plot
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def change_plot(self):
# options that apply to all subplots
options = [('Change axis limits', self.plot.change_limits),
('Change axis labels', self.plot.label_axes),
('Add legend', self.plot.add_legend)]
# change appearance of a PDF
if self.pdf_exists():
options.extend([('Change constraint color',
self.change_pdf_color)])
# options specific to 1D or 2D plots
subplot_1d_exists = False
subplot_2d_exists = False
subplot_2d_has_multiple_contours = False
for ax_row in self.plot.axes:
for ax in ax_row:
if hasattr(ax, 'parameters'):
if len(ax.parameters) == 1:
subplot_1d_exists = True
elif len(ax.parameters) == 2:
subplot_2d_exists = True
if len(ax.pdfs) > 1:
subplot_2d_has_multiple_contours = True
if subplot_2d_has_multiple_contours:
options.extend([('Change order of contour layers',
self.plot.change_layer_order)])
options = OrderedDict(options)
m = Menu(options=options.keys(), exit_str='Cancel')
m.get_choice()
if m.choice != m.exit:
options[m.choice]()
#self.plot.plot_grid.tight_layout(self.plot.figure)
self.plot.figure.set_tight_layout(True)
plt.draw()
示例2: add_chain
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def add_chain(self):
pdf = self.choose_pdf(require_no_chain=True)
# select from list of currently defined chains (from all pdfs),
# previously used chains (read from file),
# or get files and name for new chain (if new, save to file)
if pdf is not None:
if len(self.history['chains']) > 0:
chain_history = sorted(self.history['chains'],
key=lambda x: x[0], reverse=True)
sort_indices = sorted(range(len(self.history['chains'])),
key=lambda i: \
self.history['chains'][i][0],
reverse=True)
options = [ch[1] for ch in chain_history]
details = ['Chains:\n' + '\n'.join(
[textwrap.fill(s, initial_indent=' ',
subsequent_indent=' ') \
for s in sorted(ch[2])]) \
for ch in chain_history]
m = Menu(options=options, more=details,
exit_str='New chain',
header='Choose a chain:\n' + \
'(add ? to the number to get ' + \
'more info on a chain)')
m.get_choice()
if m.choice == m.exit:
pdf.add_chain(*self.define_new_chain())
else:
self.history['chains'][sort_indices[m.i_choice]][0] += 1
pdf.add_chain(*self.history['chains'] \
[sort_indices[m.i_choice]][1:])
else:
pdf.add_chain(*self.define_new_chain())
示例3: add_likelihood
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def add_likelihood(self):
pdf = self.choose_pdf()
if pdf is not None:
if len(self.history['likelihoods']) > 0:
lk_history = sorted(self.history['likelihoods'],
key=lambda x: x[1], reverse=True)
sort_indices = sorted(range(len(self.history['likelihoods'])),
key=lambda i: \
self.history['likelihoods'][i][1],
reverse=True)
options = [lk[0] for lk in lk_history]
details = ['Likelihoods:\n' + '\n'.join(
[' ' + s + ': ' + str(lk[2][s]) \
for s in sorted(lk[2].keys())]) \
for lk in lk_history]
m = Menu(options=options, more=details,
exit_str='New likelihood',
header='Choose a likelihood function:\n' + \
'(add ? to the number to get ' + \
'more info on a likelihood)')
m.get_choice()
if m.choice == m.exit:
new_lk = self.define_new_likelihood(pdf)
pdf.add_likelihood(new_lk[0], **new_lk[2])
else:
self.history['likelihoods'][sort_indices[m.i_choice]][1] \
+= 1
lk = self.history['likelihoods'][sort_indices[m.i_choice]]
pdf.add_likelihood(lk[0], **lk[2])
else:
new_lk = self.define_new_likelihood(pdf)
pdf.add_likelihood(new_lk[0], **new_lk[2])
示例4: delete_history
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def delete_history(self):
m = Menu(options=['Likelihoods', 'Chains', 'All'], exit_str='Cancel')
m.get_choice()
if m.choice == 'Likelihoods' or m.choice == 'All':
self.history['likelihoods'] = []
if m.choice == 'Chains' or m.choice == 'All':
self.history['chains'] = []
示例5: start_interactive
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def start_interactive(self, options=None):
menu = Menu()
menu.get_choice(options=self.active_options(options))
while menu.choice != menu.exit:
if options[menu.choice]['condition']():
options[menu.choice]['action']()
menu.get_choice(options=self.active_options(options))
self.save_and_exit()
示例6: compute_1d_stats
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def compute_1d_stats(self):
pdf = self.choose_pdf(require_data=True)
if pdf is not None:
parameters = pdf.choose_parameters()
m = Menu(options=['mean and standard deviation',
'equal-tail limits',
'upper limit',
'lower limit'],
exit_str=None, header='Choose statistics to compute:')
m.get_choice()
pdf.compute_1d_stats(parameters, stats=m.choice)
示例7: define_new_chain
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def define_new_chain(self):
files = []
while len(files) == 0:
for f in raw_input('\nChain file names?\n> ').split():
new_files = glob.glob(f)
if len(new_files) == 0:
print 'No files matching ' + f + ' found.'
elif not np.array([os.path.isfile(nf) \
for nf in new_files]).all():
print 'Some of the files specified are not valid.'
else:
files += new_files
name = raw_input('Label for chain?\n> ')
burn_in = utils.get_input_float( \
'Burn-in fraction or number of samples?\n> ')[0]
mult_column = utils.get_input_integer( \
'Multiplicity column? (Enter -1 if none.)\n> ')[0]
if mult_column < 0:
mult_column = None
lnlike_column = utils.get_input_integer( \
'Log likelihood column? (Enter -1 if none.)\n> ')[0]
if lnlike_column < 0:
lnlike_column = None
first_par_column = utils.get_input_integer( \
'Column of first chain parameter?\n> ')[0]
m = Menu(options=['File named as chain label + .paramnames',
'A different file',
'Header of chain files'],
exit_str=None,
header='Where are the chain parameter names?')
m.get_choice()
paramname_file = None
params_in_header = False
if m.i_choice == 1:
paramname_file = raw_input('Enter file name:\n> ')
elif m.i_choice == 2:
params_in_header = True
chain_settings = (name, files, burn_in,
mult_column, lnlike_column, first_par_column,
paramname_file, params_in_header)
# check if name is already in history; if so, replace with new
for chain in list(self.history['chains']):
if chain[0] == name:
self.history['chains'].remove(chain)
self.history['chains'].append([1] + list(chain_settings))
return chain_settings
示例8: choose_pdf
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def choose_pdf(self, name=None,
require_data=False, require_no_chain=False):
chosen_pdf = None
pdfs = list(self.pdfs)
for pdf in list(pdfs):
if name and pdf.name == name:
chosen_pdf = pdf
if (require_no_chain and pdf.has_mcmc()) or \
(require_data and (pdf.chain is None)):
pdfs.remove(pdf)
if not chosen_pdf:
m = Menu(options=[pdf.name for pdf in pdfs], exit_str='Cancel',
header='Choose a constraint:')
m.get_choice()
if m.choice != m.exit:
chosen_pdf = pdfs[m.i_choice]
return chosen_pdf
示例9: set_up_pdf
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def set_up_pdf(self, settings=None):
if (settings is not None) and \
('name' in settings) and ('model' in settings):
name = settings['name']
model = settings['model']
print '\nConstraint name: ' + str(name)
print 'Model name: ' + str(model)
else:
name = raw_input('\nLabel for constraint?\n> ')
m = Menu(options=['LCDM', 'wCDM'], exit_str=None,
header='Choose a model class:')
m.get_choice()
model = m.choice
new_pdf = PostPDF(name, model)
if settings is not None:
new_pdf.settings = settings
self.pdfs += [new_pdf]
self.settings['pdfs'][name] = new_pdf.settings
示例10: add_chain
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def add_chain(self, name, files, burn_in=None,
mult_column=0, lnlike_column=1, first_par_column=2,
paramname_file=None, params_in_header=False,
update_order=True):
# check if already have chain, if files exist, if name is unique
# check if model is consistent?
self.chain_files.extend(files)
self.settings['chain_name'] = name
self.settings['chain_files'] = self.chain_files
self.settings['chain_burn_in'] = burn_in
self.settings['chain_mult_column'] = mult_column
self.settings['chain_lnlike_column'] = lnlike_column
self.settings['chain_first_par_column'] = first_par_column
self.settings['chain_paramname_file'] = paramname_file
self.settings['chain_params_in_header'] = params_in_header
self.chain = MCMCChain(name, files, burn_in,
mult_column, lnlike_column, first_par_column,
paramname_file, params_in_header)
for pdf_element in self.settings['order']:
if pdf_element[0] == 'chain':
# only add likelihoods and/or derived parameters
# that come before the chain
break
elif pdf_element[0] == 'likelihood':
lk_name = pdf_element[1]
lk_settings = self.settings['likelihoods'][lk_name]
lk_parameters = lk_settings['parameters']
lk_priors = lk_settings['priors']
if lk_name not in self.likelihoods:
self.add_likelihood(lk_name, update_order=False,
**lk_settings)
# if likelihood(s) already included in constraint
# when chain is added, check if likelihood parameters
# are also in the chain (if so, rename chain parameters)
extra_parameters = []
lk_chain_parameters = []
for i, p in enumerate(lk_parameters):
if 'chain_parameters' in lk_settings:
if lk_settings['chain_parameters'][i] is None:
extra_parameters.append(p)
else:
self.chain.parameters[ \
self.chain.parameters.index( \
lk_settings['chain_parameters'][i])] = p
else:
m = Menu(options=self.chain.parameters,
exit_str='New',
header='If the parameter ' + p + \
' from likelihood ' + lk_name + \
'\nis in the chain, select the ' + \
'corresponding parameter;\n' + \
'otherwise choose "New":')
m.get_choice()
if m.choice == m.exit:
extra_parameters.append(p)
lk_chain_parameters.append(None)
else:
self.chain.parameters[ \
self.chain.parameters.index(m.choice)] = p
lk_chain_parameters.append(m.choice)
if 'chain_parameters' not in lk_settings:
lk_settings['chain_parameters'] = \
list(lk_chain_parameters)
# add extra parameters defined in likelihoods
self.chain.extend(extra_parameters,
[lk_priors[lk_parameters.index(p)] for \
p in extra_parameters])
# importance sample
if lk_settings['form'] != 'Flat':
self.chain.importance_sample(self.likelihoods[lk_name])
# recompute derived parameters if necessary
elif pdf_element[0] == 'derived_parameter':
par_name = pdf_element[1]
par_settings = self.settings['derived_parameters'][par_name]
if 'chain_parameter' in par_settings:
if par_settings['chain_parameter'] is None:
self.add_derived_parameter( \
par_name, par_settings['function'],
par_settings['parameters'], update_order=False)
else:
self.chain.parameters[ \
self.chain.parameters.index( \
par_settings['chain_parameter'])] = par_name
else:
m = Menu(options=self.chain.parameters, exit_str='New',
header='If the derived parameter ' + \
par_name + \
' is in the chain,\n' + \
'select the corresponding ' + \
'parameter;\notherwise choose "New":')
m.get_choice()
if m.choice == m.exit:
self.add_derived_parameter( \
par_name, par_settings['function'],
par_settings['parameters'], update_order=False)
par_settings['chain_parameter'] = None
#.........这里部分代码省略.........
示例11: define_new_likelihood
# 需要导入模块: from menu import Menu [as 别名]
# 或者: from menu.Menu import get_choice [as 别名]
def define_new_likelihood(self, pdf):
# if chain exists, choose some parameters from there
# can also add new parameters
name = raw_input('Label for likelihood?\n> ')
m = Menu(options=['Flat', 'Gaussian',
'SNe (Gaussian with analytic marginalization)',
'Inverse Gaussian'],
exit_str=None,
header='Choose the form of the likelihood:')
m.get_choice()
form = m.choice
if form[:3] == 'SNe':
form = 'SNe'
lk_dict = {'form': form}
priors = []
if form != 'SNe':
parameters = pdf.choose_parameters(allow_extra_parameters=True)
pdf.add_parameters(parameters)
lk_dict['parameters'] = parameters
if form == 'Flat':
for p in parameters:
if len(pdf.settings['parameters'][p]) != 2:
new_prior = utils.get_input_float( \
'Enter lower and upper limits of the prior on ' + \
p + ':\n> ', num=2)
priors.append(sorted(new_prior))
else:
priors.append(pdf.settings['parameters'][p])
elif form in ['Gaussian', 'Inverse Gaussian']:
means = utils.get_input_float('Enter mean values:\n> ',
num=len(parameters))
variances = utils.get_input_float('Enter variances:\n> ',
num=len(parameters))
for i, p in enumerate(parameters):
if len(pdf.settings['parameters'][p]) != 2:
priors.append([means[i] - 5.*np.sqrt(variances[i]),
means[i] + 5.*np.sqrt(variances[i])])
else:
priors.append(pdf.settings['parameters'][p])
covariance = np.diag(variances)
for i, j in itertools.combinations(range(len(parameters)), 2):
covariance[i, j] = utils.get_input_float( \
'Cov(' + parameters[i] + ', ' + parameters[j] + ')?\n> ')[0]
covariance[j, i] = covariance[i, j]
lk_dict['means'] = means
lk_dict['covariance'] = covariance.tolist()
elif form == 'SNe':
sn_z_mu_file = raw_input('Name of file with mean SN distance ' + \
'modulus vs. z?\n> ')
sn_z, sn_mu = np.loadtxt(sn_z_mu_file, unpack=True)
lk_dict['means'] = list(sn_mu)
sn_cov_file = raw_input('Name of file with SN covariance ' + \
'matrix?\n> ')
sn_cov = np.loadtxt(sn_cov_file)
lk_dict['covariance'] = sn_cov.tolist()
sn_parameters = []
for i, z in enumerate(sn_z):
p_sn_z = 'mu_SN_z' + str(z)
sn_parameters.append(p_sn_z)
priors.append([sn_mu[i] - 5.*np.sqrt(sn_cov[i,i]),
sn_mu[i] + 5.*np.sqrt(sn_cov[i,i])])
pdf.add_parameters(sn_parameters)
lk_dict['parameters'] = sn_parameters
lk_dict['priors'] = priors
new_lk = (name, 1, lk_dict)
# check if name is already in history; if so, replace with new
for lk in list(self.history['likelihoods']):
if lk[0] == name:
self.history['likelihoods'].remove(lk)
self.history['likelihoods'].append(list(new_lk))
return new_lk