def test__get_header_string():
    f = PyposmatBadParametersFile()
    parameter_names = ['a', 'b', 'c']
    filename = 'test.out'

    f = PyposmatBadParametersFile()
    s = f.get_header_string(parameter_names=parameter_names)

    assert type(s) is str

    header_line_1 = ['sim_id'] + parameter_names + ['reason']
    header_line_2 = ['sim_id'] + len(parameter_names) * ['param'] + ['reason']
    s_test = "{}\n".format(",".join(header_line_1))
    s_test += "{}\n".format(",".join(header_line_2))

    assert s_test == s
Esempio n. 2
0
    def configure_pyposmat_badparameters_file(self, filename=None):
        if filename is not None:
            assert type(filename) is str
            self.pyposmat_badparameters_filename = filename

        self.pyposmat_badparameters = PyposmatBadParametersFile(
            filename=self.pyposmat_badparameters_filename,
            o_config=self.configuration)
def dev__get_header_string():
    testing_set = get_testing_set()

    print(os.path.isfile(testing_set['config_fn']), testing_set['config_fn'])

    o = PyposmatBadParametersFile(filename=testing_set['badparameters_out_fn'],
                                  config_fn=testing_set['config_fn'])
    print(o.get_header_string())
Esempio n. 4
0
def test____init____w_filename():
    test_badparameters_fn = 'test_filename.out'

    if os.path.isfile(test_badparameters_fn):
        os.remove(test_badparameters_fn)

    o = PyposmatBadParametersFile(filename=test_badparameters_fn)
    assert o.filename == test_badparameters_fn
    assert not os.path.isfile(test_badparameters_fn)
    assert o.configuration is None
def test__write_header_string__no_args():
    testing_set = get_testing_set()

    o = PyposmatBadParametersFile(filename=testing_set['badparameters_out_fn'],
                                  config_fn=testing_set['config_fn'])
    o.write_header_section()

    assert os.path.isfile(testing_set['badparameters_out_fn'])

    if os.path.isfile(testing_set['badparameters_out_fn']):
        os.remove(testing_set['badparameters_out_fn'])
def test__write_header_string__w_filename():
    testing_set = get_testing_set()
    badparameters_out_fn = 'pyposmat.badparameters.test'

    o = PyposmatBadParametersFile(filename=testing_set['badparameters_out_fn'],
                                  config_fn=testing_set['config_fn'])
    o.write_header_section(filename=badparameters_out_fn)

    assert os.path.isfile(badparameters_out_fn)

    if os.path.isfile(badparameters_out_fn):
        os.remove(badparameters_out_fn)
def test__write_header_section():
    parameter_names = ['a,b,c']
    filename = 'test.out'

    if os.path.isfile(filename):
        os.remove(filename)

    f = PyposmatBadParametersFile()
    f.write_header_section(parameter_names=parameter_names, filename=filename)

    assert os.path.isfile(filename)

    if os.path.isfile(filename):
        os.remove(filename)
def dev__get_header_string():
    parameter_names = ['a', 'b', 'c']
    filename = 'test.out'

    f = PyposmatBadParametersFile()
    s = f.get_header_string(parameter_names=parameter_names)

    header_line_1 = ['sim_id'] + parameter_names + ['reason']
    header_line_2 = ['sim_id'] + len(parameter_names) * ['param'] + ['reason']
    s_test = "{}\n".format(",".join(header_line_1))
    s_test += "{}\n".format(",".join(header_line_2))
    print(s_test)
    print(s)
    print(s_test == s)
def dev__write_header_section():
    print(80 * '-')
    print('{:^80}'.format('write_header_section'))
    print(80 * '-')

    testing_set = get_testing_set()

    if os.path.isfile(testing_set['filename']):
        os.remove(testing_set['filename'])

    f = PyposmatBadParametersFile()
    f.write_header_section(parameter_names=testing_set['parameter_names'],
                           filename=testing_set['filename'])

    with open(testing_set['filename'], 'r') as f:
        print(f.read())
Esempio n. 10
0
def test__read():
    testing_set = get_testing_set()

    assert os.path.isfile(testing_set['badparameters_in_fn'])
    assert os.path.isfile(testing_set['config_fn'])

    if os.path.isfile(testing_set['badparameters_out_fn']):
        m = "removing the badparameter_out_file:{}".format(
                testing_set['badparameters_out_fn'])
        print(m)
        os.remove(testing_set['badparameters_out_fn'])

    o = PyposmatBadParametersFile(
            filename=testing_set['badparameters_out_fn'],
            config_fn=testing_set['config_fn'])

    o.read(filename=testing_set['badparameters_in_fn'])

    assert isinstance(o.df,pd.DataFrame)
def dev__write_simulation_exception():
    import importlib

    testing_set = get_testing_set()

    o = PyposmatBadParametersFile()
    o.write_header_section(parameter_names=testing_set['parameter_names'],
                           filename=testing_set['filename'])

    module_name = 'pypospack.exceptions'
    module = importlib.import_module(module_name)

    class_names = ['LammpsSimulationError']
    for class_name in class_names:
        sim_id = class_name
        m = "message"
        exception = getattr(module,
                            class_name)(m,
                                        parameters=testing_set['parameters'])
        o.write_simulation_exception(sim_id=sim_id, exception=exception)
Esempio n. 12
0
def test____init____w_filename_config_fn():
    testing_set = get_testing_set()

    assert os.path.isfile(testing_set['badparameters_in_fn'])
    assert os.path.isfile(testing_set['config_fn'])

    if os.path.isfile(testing_set['badparameters_out_fn']):
        m = "removing the badparameter_out_file:{}".format(
            testing_set['badparameters_out_fn'])
        print(m)
        os.remove(testing_set['badparameters_out_fn'])

    o = PyposmatBadParametersFile(filename=testing_set['badparameters_out_fn'],
                                  config_fn=testing_set['config_fn'])

    assert o.filename == testing_set['badparameters_out_fn']

    from pypospack.pyposmat.data import PyposmatConfigurationFile
    o_config = PyposmatConfigurationFile()
    o_config.read(filename=testing_set['config_fn'])
    assert isinstance(o.configuration, PyposmatConfigurationFile)
    assert isinstance(o.parameter_names, list)
    assert set(o.parameter_names) == set(o_config.parameter_names)
def test__get_header_string():
    testing_set = get_testing_set()

    o_config = PyposmatConfigurationFile()
    o_config.read(filename=testing_set['config_fn'])
    assert os.path.isfile(testing_set['config_fn'])

    f = PyposmatBadParametersFile(filename=testing_set['badparameters_out_fn'],
                                  config_fn=testing_set['config_fn'])
    s = f.get_header_string()

    assert type(s) is str

    header_line_1 = ['sim_id'] \
            + o_config.parameter_names \
            + ['reason']
    header_line_2 = ['sim_id'] \
            + len(o_config.parameter_names)*['param'] \
            + ['reason']
    s_test = "{}\n".format(",".join(header_line_1))
    s_test += "{}\n".format(",".join(header_line_2))

    assert s_test == s
Esempio n. 14
0
def test____init____wo_filename():
    o = PyposmatBadParametersFile()
    assert o.filename == 'pyposmat.badparameters.out'
    assert o.configuration is None
Esempio n. 15
0
    def merge_error_files(self, i_iteration):
        """ merge the pyposmat data files

        Args:
            i_iteration(int): the current iteration which just finished
            last_datafile_fn(str,optional): the filename of the last dataset in the data directory.
            new_datafile_fn(str,optional): where to output the file results 
        """

        badparameters_fn = os.path.join(self.data_directory,
                                        'pyposmat.badparameters.out')

        data_dir = self.data_directory
        rank_dirs = [
            v for v in os.listdir(self.root_directory) if v.startswith('rank_')
        ]
        filenames = [
            os.path.join(self.root_directory, v, 'pyposmat.badparameters.out')
            for v in rank_dirs
        ]

        # consolidate rank directories
        badparameters_new = None
        badparameters_next = None
        for i, v in enumerate(filenames):
            if badparameters_new is None:
                try:
                    badparameters_new = PyposmatBadParametersFile(
                        o_config=self.configuration)
                    badparameters_new.read(filename=v)
                except FileNotFoundError as e:
                    self.log("no bad parameters file at {}".format(v))

            else:
                try:
                    badparameters_next = PyposmatBadParametersFile(
                        o_config=self.configuration)
                    badparameters_next.read(filename=v)
                    badparameters_new.df = pd.concat(
                        [badparameters_new.df, badparameters_next.df])
                except FileNotFoundError as e:
                    self.log("no bad parameters file as {}".format(v))

        # determine the sim_id for bad parameters of the sim_id
        if badparameters_new.df is None:
            # no previous bad paramters found
            # TODO: need to implement something here to deal with bad parameters
            pass

        else:
            nrows = len(badparameters_new.df)
            sim_id_fmt = '{:0>2}_{:0>6}'
            sim_id_str = [
                sim_id_fmt.format(i_iteration, i) for i in range(nrows)
            ]
            badparameters_new.df['sim_id'] = sim_id_str

            if self.configuration.sampling_type[i_iteration][
                    'type'] == "from_file":
                badparameters_new.write(filename=badparameters_fn)

            else:
                self.log(
                    "merging with bad candidates from previous simulations")
                self.log("\tfilename:{}".format(badparameters_fn))
                badparameters = PyposmatBadParametersFile(
                    o_config=self.configuration)

                try:
                    badparameters.read(filename=badparameters_fn)
                    badparameters.df = pd.concat(
                        [badparameters.df, badparameters_new.df])
                    badparameters.write(filename=badparameters_fn)
                except FileNotFoundError as e:
                    if i_iteration == 0:
                        badparameters_new.write(filename=badparameters_fn)
                    else:
                        raise