Skip to content

Commit

Permalink
Implements gso tests for #53, fixed bug in cloning Coordinates and La…
Browse files Browse the repository at this point in the history
…ndscapePosition
  • Loading branch information
brianjimenez committed Aug 8, 2023
1 parent 169c752 commit 7c0a36f
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 320 deletions.
4 changes: 2 additions & 2 deletions lightdock/gso/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Coordinates(object):
dimensions of the solution space used in the objective function."""

def __init__(self, values):
self._values = values
self._values = values.copy()
self.dimension = len(self._values)

def __getitem__(self, index):
Expand All @@ -42,7 +42,7 @@ def __ne__(self, other):

def clone(self):
"""Get a copy of the current coordinate"""
return Coordinates(self._values * 1)
return Coordinates(self._values)

def __add__(self, other):
"""Adds two coordinates"""
Expand Down
4 changes: 2 additions & 2 deletions lightdock/gso/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def __init__(self, file_name=None):
self._config = ConfigParser()
try:
if file_name:
self._config.readfp(open(file_name))
self._config.read_file(open(file_name))
else:
self._config.readfp(
self._config.read_file(
open(Path(os.environ["LIGHTDOCK_CONF_PATH"]) / "glowworm.conf")
)
except Exception as e:
Expand Down
4 changes: 2 additions & 2 deletions lightdock/gso/searchspace/landscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LandscapePosition(object):

def __init__(self, objective_function, coordinates, step=DEFAULT_STEP_SIZE):
self.objective_function = objective_function
self.coordinates = coordinates
self.coordinates = coordinates.clone()
self.step = step

def evaluate_objective_function(self):
Expand All @@ -42,7 +42,7 @@ def __ne__(self, other):
def clone(self):
"""Creates a copy of this landscape position"""
return LandscapePosition(
self.objective_function, self.coordinates.clone(), self.step
self.objective_function, self.coordinates, self.step
)

def __add__(self, other):
Expand Down
42 changes: 13 additions & 29 deletions lightdock/test/gso/searchspace/test_docking_landscape.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""Tests for DockingLandscapePosition class"""

import os
from pathlib import Path
import shutil
from nose.tools import assert_almost_equal
import pytest
import numpy as np
from pathlib import Path
from lightdock.gso.searchspace.landscape import DockingLandscapePosition
from lightdock.gso.coordinates import Coordinates
from lightdock.scoring.mj3h.driver import MJ3h, MJ3hAdapter
Expand All @@ -15,28 +13,14 @@


class TestDockingLandscapePosition:
def __init__(self):
def setup_class(self):
self.path = Path(__file__).absolute().parent
self.test_path = self.path / "scratch_docking_landscape"
self.golden_data_path = self.path / "golden_data"
atoms, _, chains = parse_complex_from_file(
self.golden_data_path / "1PPErec.pdb"
)
self.receptor = Complex(chains, atoms)

def setup(self):
try:
shutil.rmtree(self.test_path)
except OSError:
pass
os.mkdir(self.test_path)

def teardown(self):
try:
shutil.rmtree(self.test_path)
except OSError:
pass

def test_clone(self):
atoms, _, chains = parse_complex_from_file(
self.golden_data_path / "1PPElig.pdb"
Expand All @@ -59,8 +43,8 @@ def test_clone(self):

landscape_position_2.translation[0] = 5.0

assert_almost_equal(5.0, landscape_position_2.translation[0])
assert_almost_equal(0.0, landscape_position_1.translation[0])
assert 5.0 == pytest.approx(landscape_position_2.translation[0])
assert 0.0 == pytest.approx(landscape_position_1.translation[0])

def test_repr(self):
atoms, _, chains = parse_complex_from_file(
Expand Down Expand Up @@ -91,7 +75,7 @@ def test_evaluate_objective_function_no_movement(self):
scoring_function, coordinates, adapter.receptor_model, adapter.ligand_model
)

assert_almost_equal(2.02, landscape_position.evaluate_objective_function())
assert 2.02 == pytest.approx(landscape_position.evaluate_objective_function())

def test_evaluate_objective_function_rotation_y_axis_180(self):
atoms, _, chains = parse_complex_from_file(
Expand All @@ -105,7 +89,7 @@ def test_evaluate_objective_function_rotation_y_axis_180(self):
scoring_function, coordinates, adapter.receptor_model, adapter.ligand_model
)

assert_almost_equal(-1.4, landscape_position.evaluate_objective_function())
assert -1.4 == pytest.approx(landscape_position.evaluate_objective_function())

def test_evaluate_objective_function_rotation_y_axis_180_translation_10(self):
atoms, _, chains = parse_complex_from_file(
Expand All @@ -119,7 +103,7 @@ def test_evaluate_objective_function_rotation_y_axis_180_translation_10(self):
scoring_function, coordinates, adapter.receptor_model, adapter.ligand_model
)

assert_almost_equal(6.39, landscape_position.evaluate_objective_function())
assert 6.39 == pytest.approx(landscape_position.evaluate_objective_function())

def test_distance2_same_landscape_position(self):
atoms, _, chains = parse_complex_from_file(
Expand All @@ -135,7 +119,7 @@ def test_distance2_same_landscape_position(self):
landscape_position2 = DockingLandscapePosition(
scoring_function, coordinates, adapter.receptor_model, adapter.ligand_model
)
assert_almost_equal(0.0, landscape_position1.distance2(landscape_position2))
assert 0.0 == pytest.approx(landscape_position1.distance2(landscape_position2))

def test_distance2_10A_translation_x(self):
atoms, _, chains = parse_complex_from_file(
Expand All @@ -156,8 +140,8 @@ def test_distance2_10A_translation_x(self):
adapter2.receptor_model,
adapter2.ligand_model,
)
assert_almost_equal(100.0, landscape_position1.distance2(landscape_position2))
assert_almost_equal(10.0, landscape_position1.distance(landscape_position2))
assert 100.0 == pytest.approx(landscape_position1.distance2(landscape_position2))
assert 10.0 == pytest.approx(landscape_position1.distance(landscape_position2))

def test_distance2_minus_10A_translation_y(self):
atoms, _, chains = parse_complex_from_file(
Expand All @@ -178,8 +162,8 @@ def test_distance2_minus_10A_translation_y(self):
adapter2.receptor_model,
adapter2.ligand_model,
)
assert_almost_equal(100.0, landscape_position1.distance2(landscape_position2))
assert_almost_equal(10.0, landscape_position1.distance(landscape_position2))
assert 100.0 == pytest.approx(landscape_position1.distance2(landscape_position2))
assert 10.0 == pytest.approx(landscape_position1.distance(landscape_position2))

def test_move_step_rot_full_step_trans_half(self):
atoms, _, chains = parse_complex_from_file(
Expand Down
9 changes: 3 additions & 6 deletions lightdock/test/gso/searchspace/test_j1.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Tests for J1 function"""

import pytest
from lightdock.gso.searchspace.benchmark_ofunctions import J1
from lightdock.gso.coordinates import Coordinates
from nose.tools import assert_almost_equals


class TestJ1:
def __init__(self):
def setup_class(self):
self.expected_values = [
[
6.671280296717448e-05,
Expand Down Expand Up @@ -49,7 +49,4 @@ def test_compute_J1_matrix(self):
j1 = J1()
for i in range(5):
for j in range(5):
assert_almost_equals(
self.expected_values[i][j],
j1(Coordinates([-3.0 + j * 1.5, -3.0 + i * 1.5])),
)
assert self.expected_values[i][j] == pytest.approx(j1(Coordinates([-3.0 + j * 1.5, -3.0 + i * 1.5])))
9 changes: 3 additions & 6 deletions lightdock/test/gso/searchspace/test_j2.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Tests for J2 function"""

import pytest
from lightdock.gso.searchspace.benchmark_ofunctions import J2
from lightdock.gso.coordinates import Coordinates
from nose.tools import assert_almost_equals


class TestJ2:
def __init__(self):
def setup_class(self):
self.expected_values = [
[50, 51.25, 25, 51.25, 50],
[51.25, 52.5, 26.25, 52.5, 51.25],
Expand All @@ -19,7 +19,4 @@ def test_compute_J1_matrix(self):
j2 = J2()
for i in range(5):
for j in range(5):
assert_almost_equals(
self.expected_values[i][j],
j2(Coordinates([-5.0 + j * 2.5, -5.0 + i * 2.5])),
)
assert self.expected_values[i][j] == pytest.approx(j2(Coordinates([-5.0 + j * 2.5, -5.0 + i * 2.5])))
9 changes: 3 additions & 6 deletions lightdock/test/gso/searchspace/test_j3.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Tests for J3 function"""

import pytest
from lightdock.gso.searchspace.benchmark_ofunctions import J3
from lightdock.gso.coordinates import Coordinates
from nose.tools import assert_almost_equals


class TestJ3:
def __init__(self):
def setup_class(self):
self.expected_values = [
[
3.80536808724531,
Expand Down Expand Up @@ -49,7 +49,4 @@ def test_compute_J1_matrix(self):
j3 = J3()
for i in range(5):
for j in range(5):
assert_almost_equals(
self.expected_values[i][j],
j3(Coordinates([-10.0 + j * 5.0, -10.0 + i * 5.0])),
)
assert self.expected_values[i][j] == pytest.approx(j3(Coordinates([-10.0 + j * 5.0, -10.0 + i * 5.0])))
9 changes: 3 additions & 6 deletions lightdock/test/gso/searchspace/test_j4.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Tests for J4 function"""

import pytest
from lightdock.gso.searchspace.benchmark_ofunctions import J4
from lightdock.gso.coordinates import Coordinates
from nose.tools import assert_almost_equals


class TestJ4:
def __init__(self):
def setup_class(self):
self.expected_values = [
[29, 28, 27, 26, 25, 25],
[28, 27, 26, 25, 24, 24],
Expand All @@ -20,7 +20,4 @@ def test_compute_J1_matrix(self):
j4 = J4()
for i in range(6):
for j in range(6):
assert_almost_equals(
self.expected_values[i][j],
j4(Coordinates([-2.0 + j * 0.8, -2.0 + i * 0.8])),
)
assert self.expected_values[i][j] == pytest.approx(j4(Coordinates([-2.0 + j * 0.8, -2.0 + i * 0.8])))
9 changes: 3 additions & 6 deletions lightdock/test/gso/searchspace/test_j5.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Tests for J5 function"""

from nose.tools import assert_equals
import pytest
from lightdock.gso.searchspace.benchmark_ofunctions import J5
from lightdock.gso.coordinates import Coordinates


class TestJ5:
def __init__(self):
def setup_class(self):
self.expected_values = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1],
Expand All @@ -29,7 +29,4 @@ def test_compute_J1_matrix(self):
j5 = J5()
for i in range(15):
for j in range(15):
assert_equals(
self.expected_values[i][j],
j5(Coordinates([-6.24 + j * 0.89143, -6.24 + i * 0.89143])),
)
assert self.expected_values[i][j] == pytest.approx(j5(Coordinates([-6.24 + j * 0.89143, -6.24 + i * 0.89143])))
18 changes: 7 additions & 11 deletions lightdock/test/gso/searchspace/test_landscape.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Tests for LandscapePosition class"""

import pytest
from math import sqrt
from lightdock.gso.coordinates import Coordinates
from lightdock.gso.searchspace.landscape import LandscapePosition
from lightdock.gso.searchspace.benchmark_ofunctions import J1
from nose.tools import assert_almost_equals


class TestLandscapePosition:
def __init__(self):
def setup_class(self):
self.objective_function = J1()
self.coordinates1 = Coordinates([0.0, 0.0])
self.coordinates2 = Coordinates([1.0, 1.0])
Expand All @@ -19,9 +19,7 @@ def __init__(self):
def test_LandscapePosition_with_J1(self):
landscape_point = LandscapePosition(self.objective_function, self.coordinates1)

assert_almost_equals(
0.9810118431238463, landscape_point.evaluate_objective_function()
)
assert 0.9810118431238463 == pytest.approx(landscape_point.evaluate_objective_function())

def test_LandscapePosition_with_J1_matrix(self):
expected_values = [
Expand Down Expand Up @@ -66,9 +64,7 @@ def test_LandscapePosition_with_J1_matrix(self):
for j in range(5):
coord = Coordinates([-3.0 + i * 1.5, -3.0 + j * 1.5])
landscape_point = LandscapePosition(self.objective_function, coord)
assert_almost_equals(
expected_values[j][i], landscape_point.evaluate_objective_function()
)
assert expected_values[j][i] == pytest.approx(landscape_point.evaluate_objective_function())

def test_equals(self):
landscape_point1 = LandscapePosition(self.objective_function, self.coordinates1)
Expand Down Expand Up @@ -150,18 +146,18 @@ def test_distance_J1(self):
landscape_point1 = LandscapePosition(self.objective_function, self.coordinates2)
landscape_point2 = LandscapePosition(self.objective_function, self.coordinates1)

assert_almost_equals(sqrt(2.0), landscape_point1.distance(landscape_point2))
assert sqrt(2.0) == pytest.approx(landscape_point1.distance(landscape_point2))

def test_distance2_J1(self):
landscape_point1 = LandscapePosition(self.objective_function, self.coordinates2)
landscape_point2 = LandscapePosition(self.objective_function, self.coordinates1)

assert_almost_equals(2.0, landscape_point1.distance2(landscape_point2))
assert 2.0 == pytest.approx(landscape_point1.distance2(landscape_point2))

def test_norm(self):
landscape_point1 = LandscapePosition(self.objective_function, self.coordinates2)

assert_almost_equals(sqrt(2.0), landscape_point1.norm())
assert sqrt(2.0) == pytest.approx(landscape_point1.norm())

def test_move_using_default_step(self):
landscape_point1 = LandscapePosition(self.objective_function, self.coordinates1)
Expand Down
8 changes: 4 additions & 4 deletions lightdock/test/gso/searchspace/test_ofunction.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Tests for ObjectiveFunction interface"""

import pytest
from lightdock.gso.searchspace.ofunction import ObjectiveFunction
from nose.tools import raises


class TestObjectiveFunction:
@raises(NotImplementedError)
def test_call(self):
function = ObjectiveFunction()
function(coordinates=[])
with pytest.raises(NotImplementedError):
function = ObjectiveFunction()
function(coordinates=[])
2 changes: 1 addition & 1 deletion lightdock/test/gso/test_boundaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class TestBoundingBox:
def __init__(self):
def setup_class(self):
self.dimension1 = Boundary(0, 1)
self.dimension2 = Boundary(0, 5)

Expand Down
Loading

0 comments on commit 7c0a36f

Please sign in to comment.