def dev__filter_by_cost_function(): print(80*'-') print('{:^80}'.format('filter_by_cost_functio')) testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) weights = o.get_weights_by_z_error_normalization() pct_to_keep = 0.95 is_survive_idx,filter_info = o.filter_by_cost_function( loss_function_type='abs_error', cost_function_type='weighted_sum', weights=weights, pct_to_keep=pct_to_keep ) print('type(is_survive_idx):{}'.format(type(is_survive_idx))) print('type(cost_function_info:{}'.format(type(filter_info))) assert isinstance(is_survive_idx,set) assert isinstance(filter_info,OrderedDict) print('<---- START: str__filter_by_cost_function_filter_info') print(o.str__filter_by_cost_function_filter_info(filter_info=filter_info)) print('<---- END: str__filter_by_cost_function_filter_info')
def dev__filter_by_qoi_constraints(): print(80*'-') print('{:^80}'.format('filter_by_qoi_constraints')) testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) #for k,v in o.qoi_constraints.items(): # print(k,v) #for k,v in o.configuration.qoi_constraints.items(): # print(k,v) if 'qoi_constraints' not in o.configuration.qoi_constraints: m = "cannot test for qoi_constraints because it is not defined in the configuration file" print(m) return else: is_survive_idx,qoi_constraint_info = o.filter_by_qoi_constraints() print('type(is_survive_idx):{}'.format(type(is_survive_idx))) assert isinstance(is_survive_idx,set) print('type(qoi_constraint_info:{}'.format(type(qoi_constraint_info))) assert isinstance(qoi_constraint_info,OrderedDict) print(o.str__qoi_constraint_info(qoi_constraint_info=qoi_constraint_info))
def dev__get_descriptive_statistics__from_kde_file(): print(80 * '-') print( '{:^80}'.format('method -> get_descriptive_statistics__from_kde_file')) testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] kde_data_fn = testing_set['kde_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) assert os.path.isfile(kde_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn, results_data_fn=results_data_fn) kde_data = PyposmatDataFile() kde_data.read(filename=kde_data_fn) descriptive_statistics = o.get_descriptive_statistics(df=kde_data.df) print( o.str__descriptive_statistics( descriptive_statistics=descriptive_statistics)) print(kde_data.df.shape)
def test__calculate_pareto_set(self,df=None,fn_out=None,sz=500): rscs=testing_resources() fn_data=rscs['fn_data'] pda=PyposmatDataAnalyzer() pda.read_data_file(filename=fn_data) assert type(pda.datafile) is PyposmatDataFile
def test__read_data_file__validpath(): rscs=testing_resources() fn_data=rscs['fn_data'] pda=PyposmatDataAnalyzer() pda.read_data_file(filename=fn_data) assert type(pda.datafile) is PyposmatDataFile
def test__read_configuration_file__validpath(): rscs=testing_resources() fn_config=rscs['fn_config'] pda=PyposmatDataAnalyzer() pda.read_configuration_file(filename=fn_config) assert type(pda.configuration) is PyposmatConfigurationfile
def test__filter_by_qoi_constraints(): testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) is_survive_idx,qoi_constraint_info = o.filter_by_qoi_constraints() assert isinstance(is_survive_idx,set) assert isinstance(qoi_constraint_info,OrderedDict)
def test__filter_by_pareto_set_membership(): testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) is_survive_idx,pareto_set_info = o.filter_by_pareto_membership() assert all([isinstance(v,int) for v in is_survive_idx]) assert isinstance(is_survive_idx,set) assert isinstance(pareto_set_info,OrderedDict)
def dev__analyze_results(): print(80*'-') print('{:^80}'.format('method -> analyze_results')) testing_set = get_testing_set() i_iteration = testing_set['i_iteration'] config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) o.analyze_results(i_iteration=i_iteration)
def test__analyze_results(): testing_set = get_testing_set() i_iteration = testing_set['i_iteration'] config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) is_survive_idx,filter_info = o.analyze_results(i_iteration=i_iteration) assert isinstance(is_survive_idx,set) assert isinstance(filter_info,OrderedDict)
def dev__analyze_results(): print(80*'-') print('{:^80}'.format('method -> analyze_results')) testing_set = get_testing_set() i_iteration = testing_set['i_iteration'] config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) is_survive_idx,filter_info = o.analyze_results(i_iteration=i_iteration) print('type(is_survive_idx):{}'.format(str(type(is_survive_idx)))) print('type(filter_info):{}'.format(str(type(filter_info))))
def dev__get_descriptive_statistics(): testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) descriptive_statistics = o.get_descriptive_statistics() assert isinstance(descriptive_statistics,OrderedDict) assert 'qois' in descriptive_statistics for qn in o.qoi_names: assert qn in descriptive_statistics['qois'] print(o.str__descriptive_statistics(descriptive_statistics=descriptive_statistics))
def dev_get_weights_by_qoi_target_normalization(): testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) weights = o.get_weights_by_qoi_target_normalization() assert isinstance(weights,OrderedDict) for k,v in weights.items(): assert k in o.qoi_names assert isinstance(v,float) print(o.str__cost_function_weights(weights=weights))
def test__write_kde_file(): o = PyposmatDataAnalyzer() o.read_configuration(filename=pyposmat_config_fn) o.read_data(filename=pyposmat_data_fn) o.write_kde_file(filename=pyposmat_kde_fn) assert os.path.isfile(pyposmat_kde_fn) # cleanup os.remove(pyposmat_kde_fn)
def get_best_parameterization(config_fn,data_fn,metric_name='d_metric',o_config=None,o_data=None): _analyzer = PyposmatDataAnalyzer() _analyzer.read_configuration_file(filename=config_fn) _analyzer.read_data_file(filename=data_fn) # calculate the scoring metric if metric_name is 'd_metric': _df = _analyzer.calculate_d_metric(df=_analyzer.datafile.df) else: s = "The metric name {} is unsupported" s = s.format(metric_name) raise PyposmatUnsupportedPotentialScoringMetric(s) _data = PyposmatDataFile() _data.read(filename=data_fn) _data.df = _df _data.subselect_by_score(score_name='d_metric',n=1) _free_parameter_names = _analyzer.configuration.free_parameter_names _parameter_best_dict = OrderedDict() for pn in _free_parameter_names: _parameter_best_dict[pn] = _data.sub_parameter_df.iloc[0][pn] return _parameter_best_dict
def test__property__n_potentials_start(): testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] o = PyposmatDataAnalyzer(config_fn=config_fn, results_data_fn=results_data_fn) assert isinstance(o.n_potentials_start, int)
def dev__filter_by_pareto_set_membership(): print(80*'-') print('{:^80}'.format('filter_by_pareto_membership')) testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] assert os.path.isfile(config_fn) assert os.path.isfile(results_data_fn) o = PyposmatDataAnalyzer(config_fn=config_fn,results_data_fn=results_data_fn) is_survive_idx,pareto_set_info = o.filter_by_pareto_membership() print(is_survive_idx) assert all([isinstance(v,int) for v in is_survive_idx]) assert isinstance(is_survive_idx,set) assert isinstance(pareto_set_info,OrderedDict) print('type(is_survive_idx):{}'.format(type(is_survive_idx))) print('type(pareto_set_info):{}'.format(type(pareto_set_info)))
def __load_results_files(self,src_dir): PYPOSPACK_RESULTS_FN_FMT="pyposmat.results.{}.out" for i in range(self.n_iterations): fn=os.path.join(src_dir,PYPOSPACK_RESULTS_FN_FMT.format(i)) print('reading {}...'.format(fn)) self.iteration[i]['results']=PyposmatDataAnalyzer() self.iteration[i]['results'].read_configuration_file(filename=self.fn_config) try: self.iteration[i]['results'].read_data_file(filename=fn) except FileNotFoundError as e: self.iteration[i]['results'] = None
def dev__property__n_potentials_start(): print(80 * '-') print('{:^80}'.format('property -> n_potentials_start')) testing_set = get_testing_set() config_fn = testing_set['config_fn'] results_data_fn = testing_set['results_fn'] o = PyposmatDataAnalyzer(config_fn=config_fn, results_data_fn=results_data_fn) print("type(o.n_potentials_start)):{}".format(type(o.n_potentials_start))) print("o.n_potentials_start:{}".format(o.n_potentials_start))
def dev__write_kde_file(): kde_data_fn = 'pyposmat.kde.test.out' try: os.remove(kde_data_fn) except FileNotFoundError as e: pass testing_set = get_testing_set() o = PyposmatDataAnalyzer() o.initialize_configuration(config_fn=testing_set['config_fn']) o.initialize_results_data(results_data_fn=testing_set['results_data_fn']) is_survive_idx, filter_info = o.analyze_results( i_iteration=testing_set['i_iteration']) o.write_kde_file(filename=kde_data_fn) print(o.kde_data.df['sim_id'])
def analyze_results(self, i_iteration): data_fn = os.path.join(\ self.root_directory, self.data_directory, 'pyposmat.results.{}.out'.format(i_iteration)) config_fn = os.path.join(\ self.root_directory, self.configuration_filename) kde_fn = os.path.join(\ self.root_directory, self.data_directory, 'pyposmat.kde.{}.out'.format(i_iteration+1)) data_analyzer = PyposmatDataAnalyzer() data_analyzer.read_configuration_file(filename=config_fn) data_analyzer.read_data_file(filename=data_fn) data_analyzer.write_kde_file(filename=kde_fn)
def test____init____path_arguments(): rscs=testing_resources() fn_config=rscs['fn_config'] fn_data=rscs['fn_data'] pda=PyposmatDataAnalyzer( fn_config=fn_config, fn_data=fn_data) assert type(pda) is PyposmatDataAnalyzer assert type(pda.configuration) is PyposmatConfigurationFile assert type(pda.datafile) is PyposmatDataFile assert type(pda.df) is pd.DataFrame assert type(pda.parameter_names) is list assert type(pda.error_names) is list assert type(pda.qoi_names) is list assert type(pda.names) is list
def get_parameter_variance( config_fn,data_fn, metric_name='d_metric', n=100, o_config=None, o_data=None): """ Args: config_fn (str): data_fn (str): metric_name (str): (default:d_metric) n (int): the number of best metric values o_config (pypospack.config.data.PyposmatConfigurationFile) o_data (pypospack.config.data.PyposmatDataFile) Returns: collections.OrderedDict Raises: PyposmatUnknownPotentialScoringMetric """ _analyzer = PyposmatDataAnalyzer() _analyzer.read_configuration_file(filename=config_fn) _analyzer.read_data_file(filename=data_fn) # calculate the scoring metric if metric_name is 'd_metric': _df = _analyzer.calculate_d_metric(df=_analyzer.datafile.df) else: s = "The metric name {} is unsupported" s = s.format(metric_name) raise PyposmatUnsupportedPotentialScoringMetric(s) _data = PyposmatDataFile() _data.read(filename=data_fn) _data.df = _df _data.subselect_by_score(score_name='d_metric',n=n) _param_std_df = _data.sub_parameter_df.std(axis=0) _parameter_std_dict = OrderedDict() for pn in _analyzer.parameter_names: _parameter_std_dict[pn] =_param_std_df.to_dict()[pn] return _parameter_std_dict
import numpy as np from pypospack.pyposmat.data import PyposmatDataFile, PyposmatDataAnalyzer if __name__ == "__main__": import os import pypospack.utils _pypospack_root = pypospack.utils.get_pypospack_root_directory() _data_in_directory = os.path.join( _pypospack_root, 'examples', 'Ni__eam__born_exp_fs__sensitivityanalysis', 'data__from_pareto_optimization') _pyposmat_data_fn = os.path.join(_data_in_directory, 'pyposmat.kde.6.out') _pyposmat_config_fn = os.path.join(_data_in_directory, 'pyposmat.config.in') analyzer = PyposmatDataAnalyzer() analyzer.read_configuration_file(filename=_pyposmat_config_fn) analyzer.read_data_file(filename=_pyposmat_data_fn) df = analyzer.calculate_d_metric(df=analyzer.datafile.df) data = PyposmatDataFile() data.read(filename=_pyposmat_data_fn) data.df = df data.subselect_by_score(score_name="d_metric", n=100) # print(data.sub_df) param_stdev_df = data.sub_parameter_df.std(axis=0) param_mean_df = data.sub_parameter_df.mean(axis=0) print("parameter standard deviations:\n{}".format(param_stdev_df)) print("parameter means:\n{}".format(param_mean_df))
def test__read_data(): o = PyposmatDataAnalyzer() o.read_data(filename=pyposmat_data_fn) assert type(o.data) is PyposmatDataFile
configuration.qoi_constraints = config.qoi_constraints configuration.structures = config.structure_db configuration.potential = config.potential_formalism configuration.sampling_type = config.sampling configuration.sampling_distribution = config.parameter_distribution configuration.sampling_constraints = config.parameter_constraints #<------------- write configuration file configuration.write(filename=_configuration_filename) #-------------------------------------------------------------------------- # TEST CONFIGURATION FILE #-------------------------------------------------------------------------- configuration.read(filename=_configuration_filename) #-------------------------------------------------------------------------- # ANALYZE DATA #-------------------------------------------------------------------------- data_directory = 'data' pyposmat_data_filename = 'pyposmat.results.9.out' pyposmat_configuration_filename = 'pyposmat.config.in' data_analyzer = PyposmatDataAnalyzer() data_analyzer.read_configuration_file( filename=pyposmat_configuration_filename) data_analyzer.read_data_file( filename=os.path.join(data_directory, pyposmat_data_filename)) #data_analyzer.filter_performance_requirements() #data_analyzer.calculate_pareto_set() data_analyzer.write_kde_file(filename='pyposmat.kde.out') exit()
import yaml import pypospack.utils from pypospack.io.filesystem import OrderedDictYAMLLoader from pypospack.pyposmat.data import PyposmatConfigurationFile from pypospack.pyposmat.data import PyposmatDataFile from pypospack.pyposmat.data import PyposmatDataAnalyzer data_directory = os.path.join(pypospack.utils.get_pypospack_root_directory(), 'data', 'Si__sw__data', 'pareto_optimization_1') config_fn = os.path.join(data_directory, 'pyposmat.config.in') analysis_fn = 'pyposmat.analysis.out' assert os.path.isdir(data_directory) assert os.path.isfile(config_fn) o = PyposmatDataAnalyzer() o.initialize_configuration(config_fn=config_fn) n_iterations = o.configuration.n_iterations print("n_iterations:{}".format(n_iterations)) # read analysis results to file system if os.path.isfile(analysis_fn): with open(analysis_fn, 'r') as f: analysis_results = yaml.load(f, OrderedDictYAMLLoader) else: analysis_results = OrderedDict() for i in range(n_iterations): if i not in analysis_results: print('i_iteration:{}'.format(i))
def analyze_results(self, i_iteration, data_fn=None, config_fn=None, kde_fn=None, analysis_fn=None): """ analyze the results of the simulation this method analyzes the results of the simulation, and does post simulation tasks, such as filtering by qoi performance, pareto optimization, etc. Args: data_fn(str): the path of the data file. By default this is set to none where the the file will be determine by i_iteration and internal attributes config_fn(str): the path of the data file. By default this is set to none where the the file will be determine by i_iteration and internal attributes kde_fn(str): the path of the data file. By default this is set to none where the the file will be determine by i_iteration and internal attributes """ if data_fn is None: data_fn = os.path.join(\ self.root_directory, self.data_directory, 'pyposmat.results.{}.out'.format(i_iteration)) if config_fn is None: config_fn = os.path.join(\ self.root_directory, self.configuration_filename) if kde_fn is None: kde_fn = os.path.join(\ self.root_directory, self.data_directory, 'pyposmat.kde.{}.out'.format(i_iteration+1)) if analysis_fn is None: analysis_fn = os.path.join(self.root_directory, self.data_directory, 'pyposmat.analysis.out') data_analyzer = PyposmatDataAnalyzer() data_analyzer.initialize_configuration(config_fn=config_fn) data_analyzer.analyze_results_data(i_iteration, filename=data_fn) assert isinstance(data_analyzer.results_statistics, OrderedDict) if os.path.isfile(analysis_fn): data_analyzer.read_analysis_file(filename=analysis_fn) self.log( data_analyzer.str__results_descriptive_statistics( statistics=data_analyzer.results_statistics)) self.log(data_analyzer.str__qoi_filtering_summary()) data_analyzer.write_kde_file(filename=kde_fn) data_analyzer.analyze_kde_data(i_iteration, filename=kde_fn) assert isinstance(data_analyzer.kde_statistics, OrderedDict) self.log( data_analyzer.str__kde_descriptive_statistics( statistics=data_analyzer.kde_statistics)) data_analyzer.update_analysis(i_iteration) data_analyzer.write_analysis_file(filename=analysis_fn)
import os, copy, argparse from pypospack.pyposmat.data import PyposmatDataAnalyzer if __name__ == "__main__": _fn_config = os.path.join("data__Ni__eam__born_exp_bjs_05", "pyposmat.config.in") _fn_data = os.path.join("data__Ni__eam__born_exp_bjs_04", "pyposmat.kde.9.out") _fn_kde_out = os.path.join("data__Ni__eam__born_exp_bjs_05", "pyposmat.kde.00.out") pda = PyposmatDataAnalyzer(fn_config=_fn_config, fn_data=_fn_data) pda.write_kde_file(filename=_fn_kde_out)
import os, copy, argparse from collections import OrderedDict from pypospack.pyposmat.data import PyposmatDataFile from pypospack.pyposmat.data import PyposmatConfigurationFile from pypospack.pyposmat.data import PyposmatDataAnalyzer if __name__ == "__main__": _fn_config = os.path.join("resources", "pyposmat.config.in") _fn_data = os.path.join("resources", "pyposmat.results.0.out") _fn_pareto_out = os.path.join("pyposmat.pareto.out") pda = PyposmatDataAnalyzer(fn_config=_fn_config, fn_data=_fn_data) pareto_df = pda.calculate_pareto_set() datafile = PyposmatDataFile() datafile.df = pareto_df datafile.parameter_names = pda.parameter_names datafile.qoi_names = pda.qoi_names datafile.error_names = pda.error_names datafile.names = ['sim_id'] \ +datafile.parameter_names\ +datafile.qoi_names\ +datafile.error_names datafile.types = ['sim_id']\ +len(datafile.parameter_names)*['param']\ +len(datafile.qoi_names)*['qoi_names']\ +len(datafile.error_names)*['error_names'] datafile.write(_fn_pareto_out) datafile.read(_fn_pareto_out)