|
| 1 | +"""Unit tests for Point class, i.e., Voxel, Coordinate etc..""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +import darsia |
| 6 | + |
| 7 | + |
| 8 | +def test_make_coordinate(): |
| 9 | + # Single coordinate - Coordinate |
| 10 | + coord = darsia.Coordinate([1.0, 2.0]) |
| 11 | + assert isinstance(coord, darsia.Coordinate) |
| 12 | + assert np.allclose(coord, [1.0, 2.0]) |
| 13 | + |
| 14 | + # Single coordinate - make_coordinate |
| 15 | + coord = darsia.make_coordinate([1.0, 2.0]) |
| 16 | + assert isinstance(coord, darsia.Coordinate) |
| 17 | + assert np.allclose(coord, [1.0, 2.0]) |
| 18 | + |
| 19 | + # Multiple coordinates - CoordinateArray |
| 20 | + coords = darsia.CoordinateArray([[1.0, 2.0], [3.0, 4.0]]) |
| 21 | + assert isinstance(coords, darsia.CoordinateArray) |
| 22 | + assert np.allclose(coords, [[1.0, 2.0], [3.0, 4.0]]) |
| 23 | + |
| 24 | + # Multiple coordinates - make_coordinate |
| 25 | + coords = darsia.make_coordinate([[1.0, 2.0], [3.0, 4.0]]) |
| 26 | + assert isinstance(coords, darsia.CoordinateArray) |
| 27 | + assert np.allclose(coords, [[1.0, 2.0], [3.0, 4.0]]) |
| 28 | + |
| 29 | + |
| 30 | +def test_make_voxel(): |
| 31 | + # Single voxel - Voxel |
| 32 | + voxel = darsia.Voxel([1, 2]) |
| 33 | + assert isinstance(voxel, darsia.Voxel) |
| 34 | + assert np.allclose(voxel, [1, 2]) |
| 35 | + |
| 36 | + # Single voxel - make_voxel |
| 37 | + voxel = darsia.make_voxel([1, 2]) |
| 38 | + assert isinstance(voxel, darsia.Voxel) |
| 39 | + assert np.allclose(voxel, [1, 2]) |
| 40 | + |
| 41 | + # Multiple voxels - VoxelArray |
| 42 | + voxels = darsia.VoxelArray([[1, 2], [3, 4]]) |
| 43 | + assert isinstance(voxels, darsia.VoxelArray) |
| 44 | + assert np.allclose(voxels, [[1, 2], [3, 4]]) |
| 45 | + |
| 46 | + # Multiple voxels - make_voxel |
| 47 | + voxels = darsia.make_voxel([[1, 2], [3, 4]]) |
| 48 | + assert isinstance(voxels, darsia.VoxelArray) |
| 49 | + assert np.allclose(voxels, [[1, 2], [3, 4]]) |
| 50 | + |
| 51 | + |
| 52 | +def test_coordinatearray_getitem(): |
| 53 | + # Define CoordinateArray |
| 54 | + coords = darsia.CoordinateArray([[1.0, 2.0], [3.0, 4.0]]) |
| 55 | + |
| 56 | + # Getitem - single coordinate |
| 57 | + coord = coords[0] |
| 58 | + assert isinstance(coord, darsia.Coordinate) |
| 59 | + assert np.allclose(coord, [1.0, 2.0]) |
| 60 | + |
| 61 | + # Iterator |
| 62 | + for coord in coords: |
| 63 | + assert isinstance(coord, darsia.Coordinate) |
| 64 | + |
| 65 | + |
| 66 | +def test_voxelarray_getitem(): |
| 67 | + # Define VoxelArray |
| 68 | + voxels = darsia.VoxelArray([[1, 2], [3, 4]]) |
| 69 | + |
| 70 | + # Getitem - single voxel |
| 71 | + voxel = voxels[0] |
| 72 | + assert isinstance(voxel, darsia.Voxel) |
| 73 | + assert np.allclose(voxel, [1, 2]) |
| 74 | + |
| 75 | + # Iterator |
| 76 | + for voxel in voxels: |
| 77 | + assert isinstance(voxel, darsia.Voxel) |
| 78 | + |
| 79 | + |
| 80 | +def test_point_to_coordinate(): |
| 81 | + # Define reference image |
| 82 | + image = darsia.Image( |
| 83 | + img=np.zeros((3, 4), dtype=float), dimensions=[1, 1], space_dim=2 |
| 84 | + ) |
| 85 | + |
| 86 | + # From Coordinate |
| 87 | + coord = darsia.Coordinate([0.6, 0.2]) |
| 88 | + to_coord = coord.to_coordinate(image.coordinatesystem) |
| 89 | + assert isinstance(to_coord, darsia.Coordinate) |
| 90 | + assert np.allclose(to_coord, [0.6, 0.2]) |
| 91 | + |
| 92 | + # From Voxel |
| 93 | + voxel = darsia.Voxel([2, 1]) |
| 94 | + to_coord = voxel.to_coordinate(image.coordinatesystem) |
| 95 | + assert isinstance(to_coord, darsia.Coordinate) |
| 96 | + assert np.allclose(to_coord, [0.25, 1 / 3]) |
| 97 | + |
| 98 | + |
| 99 | +def test_point_to_voxel(): |
| 100 | + # Define reference image |
| 101 | + image = darsia.Image( |
| 102 | + img=np.zeros((3, 4), dtype=float), dimensions=[1, 1], space_dim=2 |
| 103 | + ) |
| 104 | + |
| 105 | + # From Coordinate |
| 106 | + coord = darsia.Coordinate([0.6, 0.2]) |
| 107 | + to_voxel = coord.to_voxel(image.coordinatesystem) |
| 108 | + assert isinstance(to_voxel, darsia.Voxel) |
| 109 | + assert np.allclose(to_voxel, [2, 2]) |
| 110 | + |
| 111 | + # From Voxel |
| 112 | + voxel = darsia.Voxel([2, 1]) |
| 113 | + to_voxel = voxel.to_voxel(image.coordinatesystem) |
| 114 | + assert isinstance(to_voxel, darsia.Voxel) |
| 115 | + assert np.allclose(to_voxel, [2, 1]) |
| 116 | + |
| 117 | + |
| 118 | +def test_point_array_to_coordinate(): |
| 119 | + # Define reference image |
| 120 | + image = darsia.Image( |
| 121 | + img=np.zeros((3, 4), dtype=float), dimensions=[1, 1], space_dim=2 |
| 122 | + ) |
| 123 | + |
| 124 | + # From CoordinateArray |
| 125 | + coords = darsia.CoordinateArray([[0.6, 0.2], [0.1, 0.9]]) |
| 126 | + to_coords = coords.to_coordinate(image.coordinatesystem) |
| 127 | + assert isinstance(to_coords, darsia.CoordinateArray) |
| 128 | + assert np.allclose(to_coords, [[0.6, 0.2], [0.1, 0.9]]) |
| 129 | + |
| 130 | + # From VoxelArray |
| 131 | + voxels = darsia.VoxelArray([[2, 1], [1, 3]]) |
| 132 | + to_coords = voxels.to_coordinate(image.coordinatesystem) |
| 133 | + assert isinstance(to_coords, darsia.CoordinateArray) |
| 134 | + assert np.allclose(to_coords, [[0.25, 1 / 3], [0.75, 2 / 3]]) |
| 135 | + |
| 136 | + |
| 137 | +def test_point_array_to_voxel(): |
| 138 | + # Define reference image |
| 139 | + image = darsia.Image( |
| 140 | + img=np.zeros((3, 4), dtype=float), dimensions=[1, 1], space_dim=2 |
| 141 | + ) |
| 142 | + |
| 143 | + # From CoordinateArray |
| 144 | + coords = darsia.CoordinateArray([[0.6, 0.2], [0.1, 0.9]]) |
| 145 | + to_voxels = coords.to_voxel(image.coordinatesystem) |
| 146 | + assert isinstance(to_voxels, darsia.VoxelArray) |
| 147 | + assert np.allclose(to_voxels, [[2, 2], [0, 0]]) |
| 148 | + |
| 149 | + # From VoxelArray |
| 150 | + voxels = darsia.VoxelArray([[2, 1], [1, 3]]) |
| 151 | + to_voxels = voxels.to_voxel(image.coordinatesystem) |
| 152 | + assert isinstance(to_voxels, darsia.VoxelArray) |
| 153 | + assert np.allclose(to_voxels, [[2, 1], [1, 3]]) |
0 commit comments