diff --git a/.github/workflows/coveralls.yml b/.github/workflows/coveralls.yml index b2b5bb667..5050dff31 100644 --- a/.github/workflows/coveralls.yml +++ b/.github/workflows/coveralls.yml @@ -28,8 +28,8 @@ jobs: python -m pip install --upgrade pip python -m pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - python setup.py develop - python setup.py build_cannonsim + python -m pip install --editable . + cd tests/cannonsim/src; make; cd ../../.. pip install pytest-cov pip install coveralls - name: Create coverage report diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 0dfdfcdeb..331adc678 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -28,8 +28,8 @@ jobs: python -m pip install --upgrade pip python -m pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - python setup.py develop - python setup.py build_cannonsim + python -m pip install --editable . + cd tests/cannonsim/src; make; cd ../../.. pip install pytest-cov pip install coveralls - name: Lint with flake8 diff --git a/.gitignore b/.gitignore index ffd86c396..6f81966f1 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,3 @@ pep.sh *.csv env/ .ipynb_checkpoints/ -fusion_pce.*/ \ No newline at end of file diff --git a/README.md b/README.md index 93a8927aa..ceef4db83 100644 --- a/README.md +++ b/README.md @@ -89,16 +89,11 @@ git clone https://github.com/UCL-CCS/EasyVVUQ.git Note: As above, you need to be sure you are installing for Python 3 - if necessary replace `pip` with `pip3` and `python` with `python3` in the commands below. -We are trying to keep dependencies at a minimum but a few are inevitable, to install them use: +We are trying to keep dependencies at a minimum but a few are inevitable, to install these, install the EasyVVUQ library itself and build a test case use: ``` cd EasyVVUQ/ -pip install --use-feature=2020-resolver -r requirements.txt -``` - -Then the library can be installed using: -```buildoutcfg -python setup.py install +bash install_EasyVVUQ.sh ``` ## API diff --git a/easyvvuq/_version.py b/easyvvuq/_version.py index 304dd10d9..271f3d3d9 100644 --- a/easyvvuq/_version.py +++ b/easyvvuq/_version.py @@ -138,23 +138,22 @@ def git_get_keywords(versionfile_abs): # _version.py. keywords = {} try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() + with open(versionfile_abs, "r") as f: + for line in f.readlines(): + if line.strip().startswith("git_refnames ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["refnames"] = mo.group(1) + if line.strip().startswith("git_full ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["full"] = mo.group(1) + if line.strip().startswith("git_date ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["date"] = mo.group(1) except EnvironmentError: - pass + if os.getenv("EasyVVUQ_Debug"): print('EnvironmentError raised and ignored') return keywords @@ -513,7 +512,7 @@ def get_versions(): if cfg.parentdir_prefix: return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) except NotThisMethod: - pass + if os.getenv("EasyVVUQ_Debug"): print('NotThisMethod raised and ignored') return {"version": "0+unknown", "full-revisionid": None, "dirty": None, diff --git a/easyvvuq/actions/execute_local.py b/easyvvuq/actions/execute_local.py index d9fcee856..e2659ff13 100644 --- a/easyvvuq/actions/execute_local.py +++ b/easyvvuq/actions/execute_local.py @@ -116,7 +116,7 @@ def start(self, previous=None): try: previous['encoder_filename'] = self.encoder.target_filename except AttributeError: - pass + if os.getenv("EasyVVUQ_Debug"): print('AttributeError raised and ignored') return previous def finished(self): @@ -233,6 +233,8 @@ def start(self, previous=None): self.ret = subprocess.run( self.full_cmd, cwd=target_dir, stdout=stdout, stderr=stderr) + if isinstance(self.stdout, str): close(stdout) + if isinstance(self.stderr, str): close(stderr) return previous def finished(self): @@ -241,6 +243,8 @@ def finished(self): def finalise(self): """Performs clean-up if necessary. In this case it isn't. I think. """ + stdout.close() + stderr.close() pass def succeeded(self): diff --git a/easyvvuq/analysis/mcmc.py b/easyvvuq/analysis/mcmc.py index 6ad30d072..700b6b7a1 100644 --- a/easyvvuq/analysis/mcmc.py +++ b/easyvvuq/analysis/mcmc.py @@ -41,9 +41,7 @@ def plot_hist(self, input_parameter, chain=None, skip=0, merge=True): input_parameter = (input_parameter, 0) if merge: chain_keys = list(self.chains.keys()) - df = self.chains[chain_keys[0]][input_parameter].iloc[skip:] - for chain in chain_keys[1:]: - df.append(self.chains[chain][input_parameter].iloc[skip:]) + df = pd.concat([self.chains[ck][input_parameter].iloc[skip:] for ck in chain_keys]) plt.hist(df, 20) else: plt.hist(self.chains[chain][input_parameter].iloc[skip:], 20) diff --git a/easyvvuq/analysis/pce_analysis.py b/easyvvuq/analysis/pce_analysis.py index cea7e7e2c..8a20c0cfe 100644 --- a/easyvvuq/analysis/pce_analysis.py +++ b/easyvvuq/analysis/pce_analysis.py @@ -10,6 +10,7 @@ from .base import BaseAnalysisElement from .results import AnalysisResults from .qmc_analysis import QMCAnalysisResults +import traceback __author__ = 'Jalal Lakhlili' __license__ = "LGPL" @@ -475,29 +476,33 @@ def build_surrogate_der(Y_hat, verbose=False): results['sobols_total'][k] = ST # Sensitivity Analysis: Derivative based - dY_hat = build_surrogate_der(fit, verbose=False) - derivatives_first_dict = {} - Ndimensions = len(self.sampler.vary.vary_dict) - for i, param_name in enumerate(self.sampler.vary.vary_dict): - if self.sampler.nominal_value: - # Evaluate dY_hat['param'] at the nominal value of the parameters - values = self.sampler.nominal_value - logging.info(f"Using nominal value of the parameters to evaluate the derivative ") - derivatives_first_dict[param_name] = cp.polynomial(dY_hat[param_name])(*[v for v in values.values()]) - elif all([type(v) == type(cp.Normal()) for v in self.sampler.vary.vary_dict.values()]): - # Evaluate dY_hat['param'] at the mean of the parameters - logging.info(f"Using mean value of the parameters to evaluate the derivative ") - derivatives_first_dict[param_name] = cp.polynomial(dY_hat[param_name])(*[v.get_mom_parameters()["shift"][0] for v in self.sampler.vary.vary_dict.values()]) - elif all([type(v) == type(cp.Uniform()) for v in self.sampler.vary.vary_dict.values()]): - logging.info(f"Using mean value of the parameters to evaluate the derivative ") - # Evaluate dY_hat['param'] at the mean of the parameters - derivatives_first_dict[param_name] = cp.polynomial(dY_hat[param_name])(*[(v.lower + v.upper)/2.0 for v in self.sampler.vary.vary_dict.values()]) - else: - # Evaluate dY_hat['param'] at the zero vector - logging.info(f"Using zero vector to evaluate the derivative ") - derivatives_first_dict[param_name] = cp.polynomial(dY_hat[param_name])(*np.zeros(Ndimensions)) + try: + dY_hat = build_surrogate_der(fit, verbose=False) + derivatives_first_dict = {} + Ndimensions = len(self.sampler.vary.vary_dict) + for i, param_name in enumerate(self.sampler.vary.vary_dict): + if self.sampler.nominal_value: + # Evaluate dY_hat['param'] at the nominal value of the parameters + values = self.sampler.nominal_value + logging.info(f"Using nominal value of the parameters to evaluate the derivative ") + derivatives_first_dict[param_name] = cp.polynomial(dY_hat[param_name])(*[v for v in values.values()]) + elif all([type(v) == type(cp.Normal()) for v in self.sampler.vary.vary_dict.values()]): + # Evaluate dY_hat['param'] at the mean of the parameters + logging.info(f"Using mean value of the parameters to evaluate the derivative ") + derivatives_first_dict[param_name] = cp.polynomial(dY_hat[param_name])(*[v.get_mom_parameters()["shift"][0] for v in self.sampler.vary.vary_dict.values()]) + elif all([type(v) == type(cp.Uniform()) for v in self.sampler.vary.vary_dict.values()]): + logging.info(f"Using mean value of the parameters to evaluate the derivative ") + # Evaluate dY_hat['param'] at the mean of the parameters + derivatives_first_dict[param_name] = cp.polynomial(dY_hat[param_name])(*[(v.lower + v.upper)/2.0 for v in self.sampler.vary.vary_dict.values()]) + else: + # Evaluate dY_hat['param'] at the zero vector + logging.info(f"Using zero vector to evaluate the derivative ") + derivatives_first_dict[param_name] = cp.polynomial(dY_hat[param_name])(*np.zeros(Ndimensions)) + + results['derivatives_first'][k] = derivatives_first_dict - results['derivatives_first'][k] = derivatives_first_dict + except Exception: + traceback.print_exc() # Transform the relative numbers back to the absolute values if self.relative_analysis: diff --git a/easyvvuq/db/sql.py b/easyvvuq/db/sql.py index b21c323d9..7c07d10bf 100644 --- a/easyvvuq/db/sql.py +++ b/easyvvuq/db/sql.py @@ -9,10 +9,8 @@ import numpy as np from sqlalchemy.sql import case from sqlalchemy import create_engine, Column, Integer, String, ForeignKey -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import sessionmaker +from sqlalchemy.orm import sessionmaker, declarative_base from sqlalchemy import MetaData -from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import text from sqlalchemy.engine import Engine from sqlalchemy import event @@ -321,7 +319,7 @@ def update_sampler(self, sampler_id, sampler_element): The sampler that should be used as the new state """ - selected = self.session.query(SamplerTable).get(sampler_id) + selected = self.session.get(SamplerTable,sampler_id) selected.sampler = easyvvuq_serialize(sampler_element) self.session.commit() @@ -340,7 +338,7 @@ def resurrect_sampler(self, sampler_id): The 'live' sampler object, deserialized from the state in the db """ try: - serialized_sampler = self.session.query(SamplerTable).get(sampler_id).sampler + serialized_sampler = self.session.get(SamplerTable,sampler_id).sampler sampler = easyvvuq_deserialize(serialized_sampler.encode('utf-8')) except AttributeError: sampler = None @@ -581,7 +579,7 @@ def get_sampler_id(self, campaign_id): int The id of the sampler set for the specified campaign """ - sampler_id = self.session.query(CampaignTable).get(campaign_id).sampler + sampler_id = self.session.get(CampaignTable,campaign_id).sampler return sampler_id def set_sampler(self, campaign_id, sampler_id): @@ -594,7 +592,7 @@ def set_sampler(self, campaign_id, sampler_id): sampler_id: int ID of the sampler. """ - self.session.query(CampaignTable).get(campaign_id).sampler = sampler_id + self.session.get(CampaignTable,campaign_id).sampler = sampler_id self.session.commit() def campaign_dir(self, campaign_name=None): diff --git a/easyvvuq/encoders/jinja_encoder.py b/easyvvuq/encoders/jinja_encoder.py index 8b811bbe3..5016c29cb 100644 --- a/easyvvuq/encoders/jinja_encoder.py +++ b/easyvvuq/encoders/jinja_encoder.py @@ -64,7 +64,7 @@ def encode(self, params={}, target_dir=''): try: with open(self.template_fname, 'r') as template_file: template_txt = template_file.read() - self.template = Template(template_txt) + self.template = Template(template_txt, autoescape=True) except FileNotFoundError: raise RuntimeError( "the template file specified ({}) does not exist".format(self.template_fname)) diff --git a/easyvvuq/sampling/mc_sampler.py b/easyvvuq/sampling/mc_sampler.py index cfab7d9f5..5c6d311a2 100644 --- a/easyvvuq/sampling/mc_sampler.py +++ b/easyvvuq/sampling/mc_sampler.py @@ -49,13 +49,11 @@ def __init__(self, vary, n_mc_samples, **kwargs): None. """ - super().__init__(vary=vary, max_num=n_mc_samples, **kwargs) + super().__init__(vary=vary, count=0, max_num=n_mc_samples, **kwargs) # the number of uncertain inputs self.n_params = len(vary) # the number of MC samples, for each of the n_params + 2 input matrices self.n_mc_samples = n_mc_samples - self.vary = Vary(vary) - self.count = 0 # joint distribution self.joint = cp.J(*list(vary.values())) # create the Saltelli sampling plan diff --git a/easyvvuq/sampling/mcmc.py b/easyvvuq/sampling/mcmc.py index 4d71768cc..d20799b50 100644 --- a/easyvvuq/sampling/mcmc.py +++ b/easyvvuq/sampling/mcmc.py @@ -1,6 +1,6 @@ from .base import BaseSamplingElement import numpy as np - +import os class MCMCSampler(BaseSamplingElement, sampler_name='mcmc_sampler'): """A Metropolis-Hastings MCMC Sampler. @@ -142,12 +142,12 @@ def update(self, result, invalid): ignored_runs += list(result.loc[result[('chain_id', 0)] == chain_id]['run_id'].values) except KeyError: - pass + if os.getenv("EasyVVUQ_Debug"): print('KeyError raised and ignored') try: ignored_runs += list(invalid.loc[invalid[('chain_id', 0)] == chain_id]['run_id'].values) except KeyError: - pass + if os.getenv("EasyVVUQ_Debug"): print('KeyError raised and ignored') ignored_runs = [run[0] for run in ignored_runs] self.iteration += 1 return ignored_runs diff --git a/easyvvuq/sampling/qmc.py b/easyvvuq/sampling/qmc.py index 0c5e6eada..e6a88a62e 100644 --- a/easyvvuq/sampling/qmc.py +++ b/easyvvuq/sampling/qmc.py @@ -2,7 +2,8 @@ """ import chaospy as cp -from SALib.sample import saltelli +from SALib.sample import sobol +#from SALib.sample import saltelli from .base import BaseSamplingElement, Vary import logging @@ -80,7 +81,7 @@ def __init__(self, vary, n_mc_samples, count=0): "bounds": [[0, 1]] * self.n_params } - nodes = saltelli.sample(problem, n_mc_samples, calc_second_order=False) + nodes = sobol.sample(problem, n_mc_samples, calc_second_order=False,scramble=True) self._samples = self.distribution.inv(dist_U.fwd(nodes.transpose())) diff --git a/install_EasyVVUQ.sh b/install_EasyVVUQ.sh new file mode 100644 index 000000000..b4cf56f9d --- /dev/null +++ b/install_EasyVVUQ.sh @@ -0,0 +1,13 @@ +#1) Install Requirements +echo 'Install Requirements' +pip install -r requirements.txt + +#2) Install EasyVVUQ +echo 'Installing EasyVVUQ' +python -m pip install . + +#3) Build cannonsim test +echo 'Building cannonsim' +cd tests/cannonsim/src +make +cd ../../.. diff --git a/requirements.txt b/requirements.txt index d7febe31f..d022ddf9f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,4 +23,6 @@ dill tqdm qcg-pilotjob~=0.13.0 qcg-pilotjob-executor-api~=0.13.0 -h5py \ No newline at end of file +h5py +tomli +fipy diff --git a/setup.py b/setup.py index 43706433e..256de2933 100644 --- a/setup.py +++ b/setup.py @@ -5,39 +5,6 @@ import versioneer import subprocess - -class BuildCannonsimCommand(distutils.cmd.Command): - description = 'build cannonsim' - user_options = [] - - def initialize_options(self): - pass - - def finalize_options(self): - pass - - def run(self): - self.announce('Building cannonsim') - subprocess.check_call(['make'], cwd=path.abspath('./tests/cannonsim/src')) - - -class BuildNotebooks(distutils.cmd.Command): - description = 'build tutorials as Jupyter notebooks' - user_options = [] - - def initialize_options(self): - pass - - def finalize_options(self): - pass - - def run(self): - self.announce('Building tutorial as Jupyter notebooks') - tutorials = ['basic_tutorial.rst', 'cooling_coffee_cup.rst'] - for tutorial in tutorials: - subprocess.check_call(['rst2ipynb', tutorial, '-o', path.splitext(tutorial)[0] + '.ipynb'], cwd=path.abspath('./docs')) - - class BuildPyCommand(setuptools.command.build_py.build_py): def run(self): setuptools.command.build_py.build_py.run(self) @@ -48,8 +15,6 @@ def run(self): long_description = f.read() cmdclass = versioneer.get_cmdclass() -cmdclass['build_cannonsim'] = BuildCannonsimCommand -cmdclass['build_notebooks'] = BuildNotebooks cmdclass['build_py'] = BuildPyCommand setup( diff --git a/tests/sc/test_sobol.py b/tests/sc/test_sobol.py index c1e0ca5ba..ce717a3ca 100644 --- a/tests/sc/test_sobol.py +++ b/tests/sc/test_sobol.py @@ -4,7 +4,7 @@ import easyvvuq as uq import numpy as np import chaospy as cp - +from easyvvuq.actions import CreateRunDirectory, Encode, Decode, ExecuteLocal, Actions def print_exact_sobols(): V_i = np.zeros(d) @@ -18,9 +18,7 @@ def print_exact_sobols(): print('Exact 1st-order Sobol indices: ', V_i / V) -if __name__ == '__main__': - import matplotlib.pyplot as plt - plt.close('all') +def test_sobol_basic(): # number of unknown variables d = 5 @@ -79,20 +77,17 @@ def print_exact_sobols(): target_filename='sobol_in.json') decoder = uq.decoders.SimpleCSV(target_filename=output_filename, output_columns=output_columns) + execute = ExecuteLocal(f"{HOME}/sobol_model.py sobol_in.json") - # Add the SC app (automatically set as current app) - my_campaign.add_app(name="sc", - params=params, - encoder=encoder, - decoder=decoder) + actions = Actions(CreateRunDirectory(HOME), + Encode(encoder), execute, Decode(decoder)) + + campaign = uq.Campaign(name='sc', params=params, actions=actions) # Create the sampler vary = { "x1": cp.Uniform(0.0, 1.0), - "x2": cp.Uniform(0.0, 1.0), - "x3": cp.Uniform(0.0, 1.0), - "x4": cp.Uniform(0.0, 1.0), - "x5": cp.Uniform(0.0, 1.0)} + "x2": cp.Uniform(0.0, 1.0)} """ SPARSE GRID PARAMETERS @@ -102,32 +97,23 @@ def print_exact_sobols(): of 1D collocation points per level. Used to make e.g. clenshaw-curtis quadrature nested. """ + my_sampler = uq.sampling.SCSampler(vary=vary, polynomial_order=2, quadrature_rule="G", sparse=False, growth=False) - # Associate the sampler with the campaign - my_campaign.set_sampler(my_sampler) + campaign.set_sampler(my_sampler) - # Will draw all (of the finite set of samples) - my_campaign.draw_samples() - my_campaign.populate_runs_dir() - - # Use this instead to run the samples using EasyVVUQ on the localhost - my_campaign.apply_for_each_run_dir(uq.actions.ExecuteLocal( - "tests/sc/sobol_model.py sobol_in.json")) - - my_campaign.collate() + campaign.execute().collate() # Post-processing analysis analysis = uq.analysis.SCAnalysis(sampler=my_sampler, qoi_cols=output_columns) - my_campaign.apply_analysis(analysis) - - results = my_campaign.get_last_analysis() + results = campaign.analyse(qoi_cols=output_columns) print(results.sobols_first()) - print_exact_sobols() + assert results.sobols_first()["f"]["x1"] > 0.5 - plt.show() +if __name__ == '__main__': + test_sobol_basic() diff --git a/tests/test_action_replace.py b/tests/test_action_replace.py index a02cb6b16..1e8f0c67b 100644 --- a/tests/test_action_replace.py +++ b/tests/test_action_replace.py @@ -11,7 +11,7 @@ import pytest -@pytest.mark.skip(reason="fipy nonsense") +###@pytest.mark.skip(reason="fipy nonsense") def test_action_replace(): params = { "Qe_tot": {"type": "float", "min": 1.0e6, "max": 50.0e6, "default": 2e6}, diff --git a/tests/test_actions_execute_local.py b/tests/test_actions_execute_local.py index 9c24c67be..2377e8aa6 100644 --- a/tests/test_actions_execute_local.py +++ b/tests/test_actions_execute_local.py @@ -5,19 +5,18 @@ def test_create_run_directory(tmpdir): action = CreateRunDirectory(tmpdir) - previous = {'campaign_dir': 'test', 'run_id': 123456789, 'run_info': {'id': 123456789}} - previous = action.start(previous) + action.start({'campaign_dir': 'test', 'run_id': 123456789, 'run_info': {'id': 123456789}}) assert (os.path.exists( os.path.join( tmpdir, 'test', 'runs', 'runs_100000000-200000000', 'runs_123000000-124000000', 'runs_123450000-123460000', 'runs_123456700-123456800', 'run_123456789'))) - previous = {'campaign_dir': 'test', 'run_id': 0, 'run_info': {'id': 0}} - previous = action.start(previous) + + action.start({'campaign_dir': 'test', 'run_id': 0, 'run_info': {'id': 0}}) assert (os.path.exists( os.path.join( tmpdir, 'test', 'runs', 'runs_0-100000000', 'runs_0-1000000', 'runs_0-10000', 'runs_0-100'))) - previous = {'campaign_dir': 'test', 'run_id': 100, 'run_info': {'id': 100}} + action = CreateRunDirectory(tmpdir, flatten=True) - previous = action.start(previous) + action.start({'campaign_dir': 'test', 'run_id': 100, 'run_info': {'id': 100}}) assert (os.path.exists(os.path.join(tmpdir, 'test', 'runs', 'run_100'))) diff --git a/tests/test_actions_execute_slurm.py b/tests/test_actions_execute_slurm.py index 014659342..7f3dce59a 100644 --- a/tests/test_actions_execute_slurm.py +++ b/tests/test_actions_execute_slurm.py @@ -32,6 +32,6 @@ def test_action_status_slurm(mock_subprocess_run): mock_subprocess_run.return_value = slurm_result action = ExecuteSLURM('tutorials/epidemic/example.slurm', '$target_dir') - previous = {'rundir': '/tmp'} + # previous = {'rundir': '/tmp'} # action.start(previous) # assert(status.job_id == '65541') diff --git a/tests/test_analysis_qmc_analysis.py b/tests/test_analysis_qmc_analysis.py index b32e78b4e..01dc81133 100644 --- a/tests/test_analysis_qmc_analysis.py +++ b/tests/test_analysis_qmc_analysis.py @@ -13,7 +13,7 @@ def test_analyse(): "a": cp.Uniform(0.0, 1.0), "b": cp.Uniform(0.0, 1.0) } - sampler = QMCSampler(vary, 100) + sampler = QMCSampler(vary, 128) samples = {('run_id', 0): [], ('a', 0): [], ('b', 0): [], ('a+b', 0): []} for i, sample in enumerate(sampler): samples[('run_id', 0)].append(i) diff --git a/tests/test_campaign.py b/tests/test_campaign.py index 2464405ea..8164913fd 100644 --- a/tests/test_campaign.py +++ b/tests/test_campaign.py @@ -4,8 +4,6 @@ import os import logging import pytest -import shutil -from easyvvuq.db.sql import CampaignTable TEST_PATH = os.path.dirname(os.path.realpath(__file__)) LOGGER = logging.getLogger(__name__) diff --git a/tests/test_db.py b/tests/test_db.py index 5a52d1e0b..4157db1ee 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -55,7 +55,7 @@ def campaign(tmp_path, app_info): campaign = CampaignDB(location='sqlite:///{}/test.sqlite'.format(tmp_path)) campaign.create_campaign(info) campaign.tmp_path = str(tmp_path) - runs = [RunInfo('run', '.', 1, {'a': 1}, 1, 1) for _ in range(1010)] + runs = [RunInfo('run', '.', 1, {'a': 1}, 1, 1) for _ in range(910)] campaign.add_runs(runs) campaign.add_app(app_info) return campaign @@ -66,14 +66,14 @@ def test_db_file_created(campaign): def test_get_and_set_status(campaign): - run_ids = list(range(1, 1011)) + run_ids = list(range(1, 911)) assert (all([campaign.get_run_status(id_) == Status.NEW for id_ in run_ids])) campaign.set_run_statuses(run_ids, Status.ENCODED) assert (all([campaign.get_run_status(id_) == Status.ENCODED for id_ in run_ids])) def test_get_num_runs(campaign): - assert (campaign.get_num_runs() == 1010) + assert (campaign.get_num_runs() == 910) def test_app(campaign): @@ -130,7 +130,7 @@ def test_collation(campaign): assert (list(result.columns) == [('run_id', 0), ('iteration', 0), ('a', 0), ('b', 0), ('c', 0), ('c', 1)]) assert (list(result.iloc[100].values) == [101, 0, 1, 100, 101, 102]) - assert (result.count()[0] == 1010) + assert (result.count()[0] == 910) def test_mv_collation(tmp_path, app_info): diff --git a/tests/test_ensemble_boot.py b/tests/test_ensemble_boot.py index fef3fb7b1..b1592eb94 100644 --- a/tests/test_ensemble_boot.py +++ b/tests/test_ensemble_boot.py @@ -30,7 +30,7 @@ def test_confidence_interval(): dist = np.array([]) with pytest.raises(ValueError): - stat, low, high = confidence_interval(dist, 0.0, 0.05) + confidence_interval(dist, 0.0, 0.05) dist = np.array([0.0]) stat, low, high = confidence_interval(dist, 0.0, 0.05) assert (stat == low == high == 0.0) diff --git a/tests/test_mcmc.py b/tests/test_mcmc.py index 761f0e097..e70ae949c 100644 --- a/tests/test_mcmc.py +++ b/tests/test_mcmc.py @@ -3,11 +3,11 @@ import numpy as np import chaospy as cp import json -import matplotlib.pyplot as plt import pytest import sys from easyvvuq.actions import ExecutePython, Actions from dask.distributed import Client +import matplotlib.pyplot as plt HOME = os.path.abspath(os.path.dirname(__file__)) @@ -20,13 +20,12 @@ def rosenbrock(inputs): return {'value': 300.0 - y} -@pytest.mark.skip(reason="Broke due to pandas update. See issue #393.") +###@pytest.mark.skip(reason="Broke due to pandas update. See issue #393.") def test_mcmc(tmp_path): campaign = uq.Campaign(name="mcmc", work_dir=tmp_path) params = { "x1": {"type": "float", "default": 0.0}, "x2": {"type": "float", "default": 0.0}, - "out_file": {"type": "string", "default": "output.json"}, "chain_id": {"type": "integer", "default": 0} } encoder = uq.encoders.GenericEncoder(template_fname=os.path.abspath( @@ -50,7 +49,7 @@ def q(x, b=1): df = campaign.get_collation_result() analysis = uq.analysis.MCMCAnalysis(sampler) result = analysis.analyse(df) - result.plot_hist('x1') - result.plot_hist('x2') - result.plot_chains('x1') - result.plot_chains('x2') + plt.clf(); result.plot_hist('x1'); plt.savefig('/tmp/test_mcmc_hist_x1.png') + plt.clf(); result.plot_hist('x2'); plt.savefig('/tmp/test_mcmc_hist_x2.png') + plt.clf(); result.plot_chains('x1'); plt.savefig('/tmp/test_mcmc_chains_x1.png') + plt.clf(); result.plot_chains('x2'); plt.savefig('/tmp/test_mcmc_chains_x2.png') diff --git a/tests/test_multiapp.py b/tests/test_multiapp.py index 0c0d4ba61..e87a0469a 100644 --- a/tests/test_multiapp.py +++ b/tests/test_multiapp.py @@ -2,7 +2,6 @@ from easyvvuq.actions import Actions, Encode, Decode, CreateRunDirectory import chaospy as cp import os -import sys import pytest __copyright__ = """ diff --git a/tests/test_multiencoder.py b/tests/test_multiencoder.py index 8042195d1..735dd17bc 100644 --- a/tests/test_multiencoder.py +++ b/tests/test_multiencoder.py @@ -1,8 +1,6 @@ import easyvvuq as uq from easyvvuq.actions import Actions, Encode, Decode, CreateRunDirectory -import chaospy as cp import os -import sys import pytest __copyright__ = """ diff --git a/tests/test_multisampler.py b/tests/test_multisampler.py index 9344d3947..ca83b0319 100644 --- a/tests/test_multisampler.py +++ b/tests/test_multisampler.py @@ -2,7 +2,6 @@ from easyvvuq.actions import Actions, Encode, Decode, CreateRunDirectory import chaospy as cp import os -import sys import pytest __copyright__ = """ diff --git a/tests/test_sampling_mc.py b/tests/test_sampling_mc.py index f0c61f75e..fe9c42c8d 100644 --- a/tests/test_sampling_mc.py +++ b/tests/test_sampling_mc.py @@ -1,7 +1,6 @@ import pytest import chaospy as cp from easyvvuq.sampling import MCSampler -from easyvvuq.sampling.base import Vary def test_sampling(): diff --git a/tests/test_sampling_qmc.py b/tests/test_sampling_qmc.py index f76eb0bbc..6b55b1f0e 100644 --- a/tests/test_sampling_qmc.py +++ b/tests/test_sampling_qmc.py @@ -1,26 +1,25 @@ import pytest import chaospy as cp from easyvvuq.sampling import QMCSampler -from easyvvuq.sampling.base import Vary def test_init(): with pytest.raises(RuntimeError): - QMCSampler({}, 100) + QMCSampler({}, 128) with pytest.raises(RuntimeError): - QMCSampler([], 100) + QMCSampler([], 128) def test_is_finite(): vary = {'a': cp.Uniform(-5, 3), 'b': cp.Uniform(2, 10)} - sampler = QMCSampler(vary, 100) + sampler = QMCSampler(vary, 128) assert (sampler.is_finite()) def test_sampling(): vary = {'a': cp.Uniform(-5, 0), 'b': cp.Uniform(2, 10)} - sampler = QMCSampler(vary, 100) - assert (sampler.n_samples == 400) + sampler = QMCSampler(vary, 128) + assert (sampler.n_samples == 512) for _ in range(sampler.n_samples): sample = next(sampler) assert (sample['a'] >= -5 and sample['a'] <= 0) @@ -31,8 +30,8 @@ def test_sampling(): def test_resume(): vary = {'a': cp.Uniform(-5, 0), 'b': cp.Uniform(2, 10)} - sampler = QMCSampler(vary, 100, 390) - for _ in range(10): + sampler = QMCSampler(vary, 128, 500) + for _ in range(12): sample = next(sampler) with pytest.raises(StopIteration): next(sampler) diff --git a/tests/test_surrogate_workflow.py b/tests/test_surrogate_workflow.py index 323df998b..f47df9467 100644 --- a/tests/test_surrogate_workflow.py +++ b/tests/test_surrogate_workflow.py @@ -9,7 +9,7 @@ from tests.sc.sobol_model import sobol_g_func from easyvvuq.analysis.sc_analysis import SCAnalysisResults from easyvvuq.actions import CreateRunDirectory, Encode, Decode, ExecuteLocal, Actions, ExecutePython - +import matplotlib.pyplot as plt VARY = { "Pe": cp.Uniform(100.0, 200.0), @@ -17,7 +17,7 @@ } -@pytest.mark.skip(reason="Broke due to pandas update. See issue #395.") +###@pytest.mark.skip(reason="Broke due to pandas update. See issue #395.") @pytest.mark.parametrize('sampler', [ uq.sampling.RandomSampler( vary=VARY, max_num=100, analysis_class=uq.analysis.GaussianProcessSurrogate), @@ -72,6 +72,7 @@ def test_surrogate_workflow(tmpdir, sampler): assert np.max(np.abs(surrogate_y - model_y)) < 1e-6 # Attempt callibration with MCMC + del params['out_file'] # eliminate this (now) nuisance field campaign.add_app(name='surrogate', params=params, actions=Actions(ExecutePython(surrogate))) db_location = campaign.db_location campaign = None @@ -145,6 +146,7 @@ def proposal(x): def loglikelihood(x): return -((u - x) ** 2).sum() + init = {'Pe': [110.0], 'f': [2.0]} reloaded_campaign.set_sampler(uq.sampling.MCMCSampler(init, proposal, 'u', 1, loglikelihood)) iterator = reloaded_campaign.iterate(mark_invalid=True) @@ -154,3 +156,7 @@ def loglikelihood(x): assert (len(df) > 0) assert (len(df) <= 100) results = reloaded_campaign.analyse() + plt.clf(); results.plot_hist('Pe'); plt.savefig('/tmp/test_mcmc_hist_Pe.png') + plt.clf(); results.plot_hist('f'); plt.savefig('/tmp/test_mcmc_hist_f.png') + plt.clf(); results.plot_chains('Pe'); plt.savefig('/tmp/test_mcmc_chains_Pe.png') + plt.clf(); results.plot_chains('f'); plt.savefig('/tmp/test_mcmc_chains_f.png') diff --git a/tests/test_vector.py b/tests/test_vector.py index 7c7ffea63..bc8782eec 100644 --- a/tests/test_vector.py +++ b/tests/test_vector.py @@ -1,13 +1,8 @@ import easyvvuq as uq import chaospy as cp import os -import sys -import pytest import logging from pprint import pformat, pprint -from .gauss.encoder_gauss import GaussEncoder -from .gauss.decoder_gauss import GaussDecoder -from easyvvuq.decoders.json import JSONDecoder from easyvvuq.actions import Actions, Encode, Decode, CreateRunDirectory __copyright__ = """ @@ -88,7 +83,7 @@ def test_gauss_vector_sc(tmpdir): data = my_campaign.get_collation_result() analysis = uq.analysis.SCAnalysis(sampler=sampler, qoi_cols=["numbers"]) my_campaign.apply_analysis(analysis) - results = my_campaign.get_last_analysis() + my_campaign.get_last_analysis() def test_gauss_vector_pce(tmpdir): diff --git a/tutorials/easyvvuq_fusion_analysis.py b/tutorials/easyvvuq_fusion_analysis.py index ef72d2124..b8c144882 100755 --- a/tutorials/easyvvuq_fusion_analysis.py +++ b/tutorials/easyvvuq_fusion_analysis.py @@ -4,6 +4,7 @@ import time import numpy as np import matplotlib.pylab as plt +import os def sobols(P, coefficients): A = np.array(P.coefficients)!=0 @@ -38,7 +39,13 @@ def sobols(P, coefficients): # Read an old campaign time_start = time.time() -old_campaign = uq.Campaign(state_file="campaign_state.json", work_dir=".") +<<<<<<< HEAD +DIR = os.getenv('DIR') ## use the environment variable DIR to store the previous campaign directory path +old_campaign = uq.Campaign(name="fusion_pce.", db_location= f'sqlite:///{os.path.abspath(os.curdir)}/{DIR}/campaign.db') +======= +DIR = '[[DIR]]' ## replace with directory containing campaign.db +old_campaign = uq.Campaign(name="solps_pce.", db_location= f'{os.path.abspath(os.curdir)}/{DIR}/campaign.db') +>>>>>>> 5511dc091aadfb198b8fbe22ae8b4856db35afde time_end = time.time() print('Time for phase 1 = %.3f' % (time_end-time_start)) diff --git a/tutorials/fusion.py b/tutorials/fusion.py index c00cf90f8..17bf4c914 100755 --- a/tutorials/fusion.py +++ b/tutorials/fusion.py @@ -121,7 +121,7 @@ def solve_Te(Qe_tot=2e6, H0=0, Hw=0.1, Te_bc=100, chi=1, a0=1, R0=3, E0=1.5, b_p return Te.value, ne.value, mesh.cellCenters.value[0], mesh.cellCenters.value[0]/a if __name__ == '__main__': - Te, ne, rho, rho_norm = solve_Te() + solve_Te() """ to test: diff --git a/versioneer.py b/versioneer.py index 64fea1c89..5e3e79e2b 100644 --- a/versioneer.py +++ b/versioneer.py @@ -328,7 +328,7 @@ def get_root(): print("Warning: build in %s is using versioneer.py from %s" % (os.path.dirname(me), versioneer_py)) except NameError: - pass + print('NameError raised and ignored') return root @@ -574,7 +574,7 @@ def git_get_keywords(versionfile_abs): keywords["date"] = mo.group(1) f.close() except EnvironmentError: - pass + print('EnvironmentError raised and ignored') return keywords @@ -908,7 +908,7 @@ def get_versions(): return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, verbose) except NotThisMethod: - pass + print('NotThisMethod raised and ignored') try: root = os.path.realpath(__file__) @@ -927,13 +927,13 @@ def get_versions(): pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) return render(pieces, cfg.style) except NotThisMethod: - pass + print('NotThisMethod raised and ignored') try: if cfg.parentdir_prefix: return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) except NotThisMethod: - pass + print('NotThisMethod raised and ignored') return {"version": "0+unknown", "full-revisionid": None, "dirty": None, @@ -966,7 +966,7 @@ def git_get_keywords(versionfile_abs): keywords["date"] = mo.group(1) f.close() except EnvironmentError: - pass + print('EnvironmentError raised and ignored') return keywords @@ -1146,7 +1146,7 @@ def do_vcs_install(manifest_in, versionfile_source, ipy): present = True f.close() except EnvironmentError: - pass + print('EnvironmentError raised and ignored') if not present: f = open(".gitattributes", "a+") f.write("%s export-subst\n" % versionfile_source) @@ -1437,7 +1437,7 @@ def get_versions(verbose=False): print("got version from expanded keyword %s" % ver) return ver except NotThisMethod: - pass + print('NotThisMethod raised and ignored') try: ver = versions_from_file(versionfile_abs) @@ -1445,7 +1445,7 @@ def get_versions(verbose=False): print("got version from file %s %s" % (versionfile_abs, ver)) return ver except NotThisMethod: - pass + print('NotThisMethod raised and ignored') from_vcs_f = handlers.get("pieces_from_vcs") if from_vcs_f: @@ -1456,7 +1456,7 @@ def get_versions(verbose=False): print("got version from VCS %s" % ver) return ver except NotThisMethod: - pass + print('NotThisMethod raised and ignored') try: if cfg.parentdir_prefix: @@ -1465,7 +1465,7 @@ def get_versions(verbose=False): print("got version from parentdir %s" % ver) return ver except NotThisMethod: - pass + print('NotThisMethod raised and ignored') if verbose: print("unable to compute version") @@ -1750,7 +1750,8 @@ def do_setup(): for include in line.split()[1:]: simple_includes.add(include) except EnvironmentError: - pass + print('EnvironmentError raised and ignored') + # That doesn't cover everything MANIFEST.in can do # (http://docs.python.org/2/distutils/sourcedist.html#commands), so # it might give some false negatives. Appending redundant 'include'