| 1 |
import shutil |
| 2 |
from pathlib import Path |
| 3 |
from shutil import which |
| 4 |
from unittest import TestCase |
| 5 |
|
| 6 |
from shelxfile import Shelxfile |
| 7 |
from shelxfile.refine.refine import get_xl_version_string, find_shelxl_exe |
| 8 |
|
| 9 |
|
| 10 |
def clean_refine_files(basename='p21c'): |
| 11 |
Path('{}.res'.format(basename)).unlink(missing_ok=True) |
| 12 |
Path('{}.ins'.format(basename)).unlink(missing_ok=True) |
| 13 |
Path('{}.lst'.format(basename)).unlink(missing_ok=True) |
| 14 |
Path('{}.fcf'.format(basename)).unlink(missing_ok=True) |
| 15 |
Path('{}.fcf6'.format(basename)).unlink(missing_ok=True) |
| 16 |
Path('{}.cif'.format(basename)).unlink(missing_ok=True) |
| 17 |
Path('{}.hkl'.format(basename)).unlink(missing_ok=True) |
| 18 |
|
| 19 |
|
| 20 |
class TestRefine(TestCase): |
| 21 |
def setUp(self) -> None: |
| 22 |
if not find_shelxl_exe(): |
| 23 |
self.skipTest('SHELXL not found') |
| 24 |
res = Path('tests/resources/complete_run/p21c.res') |
| 25 |
shutil.copy(res, '.') |
| 26 |
self.shx = Shelxfile(debug=True) |
| 27 |
self.shx.read_file('./p21c.res') |
| 28 |
|
| 29 |
def tearDown(self) -> None: |
| 30 |
clean_refine_files('p21c') |
| 31 |
|
| 32 |
def test_get_xl_version_string_with_no_path(self): |
| 33 |
self.assertEqual('', get_xl_version_string('')) |
| 34 |
|
| 35 |
def test_get_xl_version_string_with_real_path(self): |
| 36 |
shx = which('shelxl') if which('shelxl') else which('xl') |
| 37 |
self.assertTrue(get_xl_version_string(shx) in ('2019/2', '2018/3', '2019/1', '2019/3', '')) |
| 38 |
|
| 39 |
def test_check_cycle_numbers(self): |
| 40 |
self.assertEqual('L.S. 10', str(self.shx.cycles)) |
| 41 |
|
| 42 |
def test_set_cycle_numbers_directly(self): |
| 43 |
self.shx.cycles.number = 3 |
| 44 |
self.assertEqual('L.S. 3', str(self.shx.cycles)) |
| 45 |
|
| 46 |
def test_set_cycle_numbers_by_set_function(self): |
| 47 |
self.shx.cycles.set_refine_cycles(4) |
| 48 |
self.assertEqual('L.S. 4', str(self.shx.cycles)) |
| 49 |
self.assertEqual('L.S. 4', self.shx._reslist[self.shx.cycles.index].__repr__()) |
| 50 |
|
| 51 |
def test_set_cycle_numbers_negative(self): |
| 52 |
self.shx.cycles.set_refine_cycles(-1) |
| 53 |
self.assertEqual('L.S. -1', str(self.shx.cycles)) |
| 54 |
|
| 55 |
def test_set_cycle_numbers_directly2(self): |
| 56 |
self.shx.cycles.cycles = 3 |
| 57 |
self.assertEqual('L.S. 10', str(self.shx.cycles)) |
| 58 |
|
| 59 |
|
| 60 |
class TestRefineFinishedmodel(TestCase): |
| 61 |
|
| 62 |
def setUp(self) -> None: |
| 63 |
if not find_shelxl_exe(): |
| 64 |
self.skipTest('SHELXL not found') |
| 65 |
res = Path('tests/resources/model_finished/p21c.res') |
| 66 |
hkl = Path('tests/resources/model_finished/p21c.hkl') |
| 67 |
shutil.copy(res, '.') |
| 68 |
shutil.copy(hkl, '.') |
| 69 |
self.shx = Shelxfile(debug=True) |
| 70 |
self.shx.read_file('./p21c.res') |
| 71 |
|
| 72 |
def tearDown(self) -> None: |
| 73 |
clean_refine_files('p21c') |
| 74 |
|
| 75 |
|
| 76 |
def test_refine_with_cycle_number_set_to_4_in_LScycles(self): |
| 77 |
self.assertTrue('L.S. 10' in Path('p21c.res').read_text()) |
| 78 |
self.shx.cycles.set_refine_cycles(2) |
| 79 |
self.shx.refine(backup_before=False) |
| 80 |
txt = Path('p21c.res').read_text() |
| 81 |
self.assertTrue('L.S. 2' in txt) |
| 82 |
|
| 83 |
def test_refine_with_cycle_number_set_to_4_in_refine(self): |
| 84 |
self.assertTrue('L.S. 10' in Path('p21c.res').read_text()) |
| 85 |
self.shx.refine(2, backup_before=False) |
| 86 |
txt = Path('p21c.res').read_text() |
| 87 |
self.assertTrue('L.S. 2' in txt) |
| 88 |
|
| 89 |
def test_refine_with_ANIS_inserted_and_cycle_number_set_to_4_in_refine(self): |
| 90 |
self.assertFalse('ANIS' in Path('p21c.res').read_text()) |
| 91 |
self.shx.insert_anis() |
| 92 |
self.shx.refine(3, backup_before=False) |
| 93 |
self.assertTrue('ANIS' in Path('p21c.ins').read_text()) |
| 94 |
self.assertFalse('ANIS' in Path('p21c.res').read_text()) |
| 95 |
|
| 96 |
def test_refine_with_ANIS_inserted_for_one_atom(self): |
| 97 |
a = self.shx.atoms.get_atom_by_name('C1_3') |
| 98 |
self.assertEqual(True, a.is_isotropic) |
| 99 |
self.shx.insert_anis(atoms='C1_3') |
| 100 |
self.shx.refine(1, backup_before=False) |
| 101 |
a = self.shx.atoms.get_atom_by_name('C1_3') |
| 102 |
self.assertEqual(False, a.is_isotropic) |
| 103 |
|
| 104 |
def test_refine_with_cycle_number_set_to_0_in_refine(self): |
| 105 |
self.assertTrue('L.S. 10' in Path('p21c.res').read_text()) |
| 106 |
self.refine = self.shx.refine(0, backup_before=False) |
| 107 |
txt = Path('p21c.res').read_text() |
| 108 |
self.assertTrue('L.S. 0' in txt) |
| 109 |
|
| 110 |
|
| 111 |
if __name__ == '__main__': |
| 112 |
print('foo') |