<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import os <EOL> import warnings <EOL> import logging <EOL> import io_mp <EOL> import sampler <EOL> from cosmoHammer . likelihood . chain . LikelihoodComputationChain import ( <EOL> LikelihoodComputationChain ) <EOL> from cosmoHammer . sampler . CosmoHammerSampler import CosmoHammerSampler <EOL> from cosmoHammer . util . SampleFileUtil import SampleFileUtil <EOL> CH_subfolder = '<STR_LIT>' <EOL> CH_separator = '<STR_LIT:->' <EOL> name_arguments = '<STR_LIT>' <EOL> name_chain = '<STR_LIT>' <EOL> CH_prefix = '<STR_LIT>' <EOL> CH_user_arguments = { <EOL> '<STR_LIT>' : <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : int } , <EOL> '<STR_LIT>' : <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : int } , <EOL> '<STR_LIT>' : <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : int } } <EOL> def run ( cosmo , data , command_line ) : <EOL> """<STR_LIT>""" <EOL> parameter_names = data . get_mcmc_parameters ( [ "<STR_LIT>" ] ) <EOL> is_flat , is_bound = sampler . check_flat_bound_priors ( <EOL> data . mcmc_parameters , parameter_names ) <EOL> if not is_flat : <EOL> raise io_mp . ConfigurationError ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> if not is_bound : <EOL> raise io_mp . ConfigurationError ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> params = [ ] <EOL> for parameter in parameter_names : <EOL> params . append ( data . mcmc_parameters [ parameter ] [ '<STR_LIT>' ] ) <EOL> params = np . array ( params ) <EOL> CH_folder = os . path . join ( command_line . folder , CH_subfolder ) <EOL> if not os . path . exists ( CH_folder ) : <EOL> os . makedirs ( CH_folder ) <EOL> chain = LikelihoodComputationChain ( <EOL> min = params [ : , <NUM_LIT:1> ] , <EOL> max = params [ : , <NUM_LIT:2> ] ) <EOL> chain . addCoreModule ( data ) <EOL> chain . addCoreModule ( cosmo ) <EOL> for likelihood in data . lkl . itervalues ( ) : <EOL> chain . addLikelihoodModule ( likelihood ) <EOL> chain_name = [ a for a in command_line . folder . split ( os . path . sep ) if a ] [ - <NUM_LIT:1> ] <EOL> file_prefix = os . path . join ( command_line . folder , CH_subfolder , chain_name ) <EOL> data . CH_arguments = { } <EOL> for arg in CH_user_arguments : <EOL> value = getattr ( command_line , CH_prefix + arg ) <EOL> if value != - <NUM_LIT:1> : <EOL> data . CH_arguments [ arg ] = value <EOL> with open ( file_prefix + name_arguments , '<STR_LIT:w>' ) as arg_file : <EOL> for arg in data . CH_arguments : <EOL> arg_file . write ( <EOL> '<STR_LIT>' . join ( [ str ( arg ) , str ( data . CH_arguments [ arg ] ) ] ) + '<STR_LIT:\n>' ) <EOL> derived_util = DerivedUtil ( file_prefix ) <EOL> try : <EOL> num_threads = int ( os . environ [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> num_threads = <NUM_LIT:1> <EOL> sampler_hammer = CosmoHammerSampler ( <EOL> params = params , <EOL> likelihoodComputationChain = chain , <EOL> filePrefix = file_prefix , <EOL> walkersRatio = <NUM_LIT:50> , <EOL> burninIterations = <NUM_LIT:10> , <EOL> sampleIterations = <NUM_LIT:30> , <EOL> storageUtil = derived_util , <EOL> threadCount = num_threads , <EOL> ** data . CH_arguments ) <EOL> console_handler = logging . StreamHandler ( ) <EOL> console_handler . setLevel ( logging . DEBUG ) <EOL> logging . getLogger ( ) . addHandler ( console_handler ) <EOL> sampler_hammer . startSampling ( ) <EOL> def from_CH_output_to_chains ( folder ) : <EOL> """<STR_LIT>""" <EOL> chain_name = [ a for a in folder . split ( os . path . sep ) if a ] [ - <NUM_LIT:2> ] <EOL> base_name = os . path . join ( folder , chain_name ) <EOL> with open ( base_name + '<STR_LIT>' , '<STR_LIT:r>' ) as param_values_file : <EOL> chains = np . loadtxt ( param_values_file ) <EOL> with open ( base_name + '<STR_LIT>' , '<STR_LIT:r>' ) as lkl_values_file : <EOL> lkl = np . loadtxt ( lkl_values_file ) <EOL> lkl = np . array ( [ [ elem ] for elem in lkl ] ) <EOL> ones = np . array ( [ [ <NUM_LIT:1> ] for _ in range ( len ( lkl ) ) ] ) <EOL> final = np . concatenate ( ( ones , lkl , chains ) , axis = <NUM_LIT:1> ) <EOL> output_folder = os . path . join ( folder , '<STR_LIT:..>' ) <EOL> output_chain_path = os . path . join ( output_folder , name_chain ) <EOL> np . savetxt ( output_chain_path , final ) <EOL> class DerivedUtil ( SampleFileUtil ) : <EOL> """<STR_LIT>""" <EOL> def persistValues ( self , posFile , probFile , pos , prob , data ) : <EOL> """<STR_LIT>""" <EOL> derived = np . array ( <EOL> [ [ a for a in elem . itervalues ( ) ] for elem in data ] ) <EOL> final = np . concatenate ( ( pos , derived ) , axis = <NUM_LIT:1> ) <EOL> posFile . write ( "<STR_LIT:\n>" . join ( <EOL> [ "<STR_LIT:U+0020>" . join ( [ str ( q ) for q in p ] ) for p in final ] ) ) <EOL> posFile . write ( "<STR_LIT:\n>" ) <EOL> posFile . flush ( ) <EOL> probFile . write ( "<STR_LIT:\n>" . join ( [ str ( p ) for p in prob ] ) ) <EOL> probFile . write ( "<STR_LIT:\n>" ) <EOL> probFile . flush ( ) </s>
