From ab240ef463ecd4399f1e78618fc1361d96d8759f Mon Sep 17 00:00:00 2001 From: Coos Baakman Date: Wed, 14 Dec 2022 15:39:44 +0100 Subject: [PATCH 1/4] adds the option for more than one dimension setting --- deeprankcore/utils/grid.py | 47 ++++++++++++++++++++------------------ tests/utils/test_graph.py | 4 +++- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/deeprankcore/utils/grid.py b/deeprankcore/utils/grid.py index 36610cc2b..4337e3919 100644 --- a/deeprankcore/utils/grid.py +++ b/deeprankcore/utils/grid.py @@ -27,26 +27,27 @@ class GridSettings: """Objects of this class hold the settings to build a grid. The grid is basically a multi-divided 3D cube with the following properties: - - points_count: the number of points on one edge of the cube - - size: the length in Å of one edge of the cube - - resolution: the size in Å of one edge subdivision. Also the distance between two points on the edge. + + - sizes: x, y, z sizes of the box in Å + - points_counts: the number of points on the x, y, z edges of the cube + - resolutions: the size in Å of one x, y, z edge subdivision. Also the distance between two points on the edge. """ - def __init__(self, points_count: int, size: float): - self._points_count = points_count - self._size = size + def __init__(self, points_counts: np.array, sizes: np.array): + self._points_counts = points_counts + self._sizes = sizes @property - def resolution(self) -> float: - return self._size / self._points_count + def resolutions(self) -> float: + return self._sizes / self._points_counts @property - def size(self) -> float: - return self._size + def sizes(self) -> float: + return self._sizes @property - def points_count(self) -> int: - return self._points_count + def points_counts(self) -> int: + return self._points_counts class Grid: @@ -69,19 +70,21 @@ def __init__(self, id_: str, settings: GridSettings, center: np.array): def _set_mesh(self, settings: GridSettings, center: np.array): "builds the grid points" - half_size = settings.size / 2 + half_size_x = settings.sizes[0] / 2 + half_size_y = settings.sizes[1] / 2 + half_size_z = settings.sizes[2] / 2 - min_x = center[0] - half_size - max_x = center[0] + half_size - self._xs = np.linspace(min_x, max_x, num=settings.points_count) + min_x = center[0] - half_size_x + max_x = center[0] + half_size_x + self._xs = np.linspace(min_x, max_x, num=settings.points_counts[0]) - min_y = center[1] - half_size - max_y = center[1] + half_size - self._ys = np.linspace(min_y, max_y, num=settings.points_count) + min_y = center[1] - half_size_y + max_y = center[1] + half_size_y + self._ys = np.linspace(min_y, max_y, num=settings.points_counts[1]) - min_z = center[2] - half_size - max_z = center[2] + half_size - self._zs = np.linspace(min_z, max_z, num=settings.points_count) + min_z = center[2] - half_size_z + max_z = center[2] + half_size_z + self._zs = np.linspace(min_z, max_z, num=settings.points_counts[2]) self._ygrid, self._xgrid, self._zgrid = np.meshgrid( self._ys, self._xs, self._zs diff --git a/tests/utils/test_graph.py b/tests/utils/test_graph.py index 2e05a1016..8f1fcacf7 100644 --- a/tests/utils/test_graph.py +++ b/tests/utils/test_graph.py @@ -61,7 +61,9 @@ def test_graph_build_and_export(): # pylint: disable=too-many-locals graph.write_to_hdf5(hdf5_path) # export grid to hdf5 - grid_settings = GridSettings(20, 20.0) + grid_settings = GridSettings(np.array((20, 21, 21)), np.array((20.0, 21.0, 21.0))) + assert np.all(grid_settings.resolutions == np.array((1.0, 1.0, 1.0))) + graph.write_as_grid_to_hdf5(hdf5_path, grid_settings, MapMethod.FAST_GAUSSIAN) # check the contents of the hdf5 file From 62950db4aa1f014941c74a01e034619f4f6dcc37 Mon Sep 17 00:00:00 2001 From: Coos Baakman Date: Thu, 15 Dec 2022 12:11:23 +0100 Subject: [PATCH 2/4] test that the dimensions are correct --- tests/utils/test_graph.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/utils/test_graph.py b/tests/utils/test_graph.py index 8f1fcacf7..64b83f7c3 100644 --- a/tests/utils/test_graph.py +++ b/tests/utils/test_graph.py @@ -97,5 +97,6 @@ def test_graph_build_and_export(): # pylint: disable=too-many-locals assert "value" in mapped_group[feature_name] data = mapped_group[feature_name]["value"][()] assert len(np.nonzero(data)) > 0, f"{feature_name}: all zero" + assert np.all(data.shape == grid_settings.sizes) finally: shutil.rmtree(tmp_dir_path) # clean up after the test From b2b840961d1d0c244f1a40a51e035c85fcdd9ef3 Mon Sep 17 00:00:00 2001 From: Coos Baakman Date: Thu, 15 Dec 2022 12:12:13 +0100 Subject: [PATCH 3/4] test that the dimensions are correct --- tests/utils/test_graph.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils/test_graph.py b/tests/utils/test_graph.py index 64b83f7c3..359525008 100644 --- a/tests/utils/test_graph.py +++ b/tests/utils/test_graph.py @@ -97,6 +97,6 @@ def test_graph_build_and_export(): # pylint: disable=too-many-locals assert "value" in mapped_group[feature_name] data = mapped_group[feature_name]["value"][()] assert len(np.nonzero(data)) > 0, f"{feature_name}: all zero" - assert np.all(data.shape == grid_settings.sizes) + assert np.all(data.shape == grid_settings.points_counts) finally: shutil.rmtree(tmp_dir_path) # clean up after the test From 5570782f5a39d67473e2d71a22be508d66050775 Mon Sep 17 00:00:00 2001 From: Coos Baakman Date: Thu, 15 Dec 2022 12:50:24 +0100 Subject: [PATCH 4/4] work with lists in gridsettings --- deeprankcore/utils/grid.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/deeprankcore/utils/grid.py b/deeprankcore/utils/grid.py index 4337e3919..ea6a005c0 100644 --- a/deeprankcore/utils/grid.py +++ b/deeprankcore/utils/grid.py @@ -4,7 +4,7 @@ from enum import Enum -from typing import Dict, Union +from typing import Dict, Union, List import numpy as np import h5py import itertools @@ -33,20 +33,23 @@ class GridSettings: - resolutions: the size in Å of one x, y, z edge subdivision. Also the distance between two points on the edge. """ - def __init__(self, points_counts: np.array, sizes: np.array): + def __init__(self, points_counts: List[int], sizes: List[float]): + assert len(points_counts) == 3 + assert len(sizes) == 3 + self._points_counts = points_counts self._sizes = sizes @property - def resolutions(self) -> float: - return self._sizes / self._points_counts + def resolutions(self) -> List[float]: + return [self._sizes[i] / self._points_counts[i] for i in range(3)] @property - def sizes(self) -> float: - return self._sizes + def sizes(self) -> List[float]: + return self._sizes.tolist() @property - def points_counts(self) -> int: + def points_counts(self) -> List[int]: return self._points_counts