openkim / kliff

KIM-based Learning-Integrated Fitting Framework for interatomic potentials.
https://kliff.readthedocs.io
GNU Lesser General Public License v2.1
34 stars 20 forks source link

Refactor UQ tests #126

Open mjwen opened 1 year ago

mjwen commented 1 year ago

Hi @yonatank93, in #125 I have refactored the tests to use more recent techniques in pytest to make it easier to maintain and write new tests.

I've slightly tweaked the UQ tests to make it work. But it would be great if you can make a bit more updates.

Let me know if it is unclear and I'd be happy to explain more.

yonatank93 commented 1 year ago
  • A lot of stuff are not in functions, but in module level (for example, lines 28--70 in this file). Is it possible to move them into functions and call to get them?

@mjwen Can you tell me which file you are referring to?

mjwen commented 1 year ago

My bad, forget to insert links. I was referring to test files in this directory: https://github.com/openkim/kliff/tree/master/tests/uq

yonatank93 commented 1 year ago

I assume you are referring to, for example, lines 28--79 in test_mcmc.py? Those lines are for getting data to use in mcmc test. I can see it now that there are a lot of overlaps in the setup in test_mcmc.py and test_bootstrap_empirical.py. When I wrote these tests, I wrote them independently. But I can see that we can put all these setups into one file.

If I put them in conftest.py, is there anything else I need to change in the test scripts? Or do I just need to import conftest?

mjwen commented 1 year ago

Great!

You don't import conftest, but directly use the function names defined in conftest.

You put them in a conftest.py file in the UQ folder, and then use fixture to define them. For example,

@pytest.fixture()
def func1():
     return "aha" 

Then in your actual tests you can pass the function name defined in conftest.py (func1 here) as an argument. For example, in your test_mcmc.py, you can write

test_a_feacture(func1):
   # here func1 will be "aha"

Note, this will only be effective for functions defined with test_* as the function name.

For example, I have defined test_data_dir here, and I can use it anywhere for all tests, e.g. in this function

yonatank93 commented 1 year ago

I see. This is a new thing for me. Thanks for telling me about it. I will work on this if you want. I might be slow, though.

mjwen commented 1 year ago

Thanks! No need to rush at all. Take your time.