Skip to content

Commit

Permalink
Merge pull request #9 from chris-delg/testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
chris-delg authored Aug 28, 2024
2 parents b304221 + b93cd06 commit 771534b
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
__pycache__/
.pytest_cache
.vscode/
example_data/
build/
Expand Down
12 changes: 6 additions & 6 deletions benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def run_write_tests(self, num_of_gigabytes: int, show_results: bool,
avg_graph: Optional[matplotlib.axes._axes.Axes] = None) -> None:

# error checking to see if chosen lib exists in test
if choose_lib and choose_lib not in set(self.__zarr_writers.keys()):
if choose_lib != None and choose_lib not in set(self.__zarr_writers.keys()):
raise ValueError(f"There is no library of name \"{choose_lib}\".")

gb_in_bytes = 1073741824 # represents number of bytes in a GB
Expand Down Expand Up @@ -94,7 +94,7 @@ def run_write_tests(self, num_of_gigabytes: int, show_results: bool,

# prints info to the terminal
print(f"Multiplier on first dimension : {multiplier}x\n{lib_name} -> creating zarr : {total_time} seconds")
print(f"The zarr folder is of size {folder_size(writer.data_path)}\n\n")
print(f"The zarr folder is of size {formatted_folder_size(writer.data_path)}\n\n")

curr_data_size = np.prod(new_shape) # 3d array filled with 1 byte ints so multiplication gives accurate size in bytes
file_sizes.append(curr_data_size * 10**-9) # converts bytes to GB
Expand All @@ -115,13 +115,13 @@ def run_write_tests(self, num_of_gigabytes: int, show_results: bool,
self.__print_results(additional_info=(f"Write Test GB Soft Cap: {num_of_gigabytes}GB"))


def run_append_test(self, num_of_gigabytes: int, show_results: bool,
def run_append_tests(self, num_of_gigabytes: int, show_results: bool,
choose_lib: Optional[str] = None,
graph: Optional[matplotlib.axes._axes.Axes] = None,
avg_graph: Optional[matplotlib.axes._axes.Axes] = None) -> None:

# error checking to see if chosen lib exists in test
if choose_lib and choose_lib not in set(self.__zarr_writers.keys()):
if choose_lib != None and choose_lib not in set(self.__zarr_writers.keys()):
raise ValueError(f"There is no library of name \"{choose_lib}\".")

gb_in_bytes = 1073741824 # represents number of bytes in a GB
Expand Down Expand Up @@ -155,7 +155,7 @@ def run_append_test(self, num_of_gigabytes: int, show_results: bool,

# prints info to the terminal
print(f"Multiplier on first dimension : {multiplier}x\n{lib_name} -> appending zarr : {total_time} seconds")
print(f"The zarr folder is of size {folder_size(writer.data_path)}\n\n")
print(f"The zarr folder is of size {formatted_folder_size(writer.data_path)}\n\n")

curr_data_size = np.prod(new_shape) # 3d array filled with 1 byte ints so multiplication gives accurate size in bytes
write_numbers.append(multiplier) # converts bytes to GB
Expand All @@ -180,7 +180,7 @@ def run_all_tests(self, append_test_gigabytes: int, write_test_gigabytes: int,
append_graph: Optional[matplotlib.axes._axes.Axes] = None, append_avg_graph: Optional[matplotlib.axes._axes.Axes] = None,
write_graph: Optional[matplotlib.axes._axes.Axes] = None, write_avg_graph: Optional[matplotlib.axes._axes.Axes] = None) -> None:

self.run_append_test(num_of_gigabytes=append_test_gigabytes, show_results=False, choose_lib=choose_lib, graph=append_graph, avg_graph=append_avg_graph)
self.run_append_tests(num_of_gigabytes=append_test_gigabytes, show_results=False, choose_lib=choose_lib, graph=append_graph, avg_graph=append_avg_graph)
self.run_write_tests(num_of_gigabytes=write_test_gigabytes, show_results=False, choose_lib=choose_lib, graph=write_graph, avg_graph=write_avg_graph)
self.__print_results(additional_info=(f"Write Test GB Soft Cap: {write_test_gigabytes}GB | Append Test GB Soft Cap: {append_test_gigabytes}GB"))

1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# needed so that pytest can identify this layer as the root layer of the project (will throw errors if deleted)
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies:
- ome-zarr
- zarr=2.15*
- pip:
- pytest
- cmake
- ml-dtypes
- tensorstore
58 changes: 58 additions & 0 deletions tests/benchmark_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from benchmark import Benchmark
import pytest


@pytest.fixture
def benchmark() -> Benchmark:
shape = [256, 256, 256]
chunks = [64, 64, 64]
benchmark = Benchmark(shape=shape, chunks=chunks)

return benchmark


def test_benchmark_get_shape(benchmark: Benchmark):
assert benchmark.shape == [256, 256, 256]


def test_benchmark_get_chunks(benchmark: Benchmark):
assert benchmark.chunks == [64, 64, 64]


@pytest.mark.parametrize(
("lib_name"),
[
(""),
("."),
(" ")
]
)
def test_if_incorrect_lib_name_in_write_tests_fails(benchmark: Benchmark, lib_name: str):
with pytest.raises(ValueError):
benchmark.run_write_tests(num_of_gigabytes=1, show_results=False, choose_lib=lib_name)


@pytest.mark.parametrize(
("lib_name"),
[
(""),
("."),
(" ")
]
)
def test_if_incorrect_lib_name_in_append_tests_fails(benchmark: Benchmark, lib_name: str):
with pytest.raises(ValueError):
benchmark.run_append_tests(num_of_gigabytes=1, show_results=False, choose_lib=lib_name)


@pytest.mark.parametrize(
("lib_name"),
[
(""),
("."),
(" ")
]
)
def test_if_incorrect_lib_name_in_run_all_tests_fails(benchmark: Benchmark, lib_name: str):
with pytest.raises(ValueError):
benchmark.run_all_tests(append_test_gigabytes=1, write_test_gigabytes=1, choose_lib=lib_name)
57 changes: 57 additions & 0 deletions tests/common_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from zarr_libraries.common import folder_size_in_bytes, formatted_folder_size
import os
import pytest
from tempfile import TemporaryDirectory


@pytest.fixture
def temp_dir():
with TemporaryDirectory() as temp_dir:
yield temp_dir


@pytest.mark.parametrize(
("num_mb"),
[
(1),
(10),
(50),
(100),
(500),
(750)
]
)
def test_folder_size_in_bytes(temp_dir, num_mb: int):
mb_in_bytes = 1048576
MBs = num_mb * mb_in_bytes

# creating a test file of size variable size
with open(temp_dir + "/test.bin", "wb") as f:
f.write(os.urandom(MBs))

# custom tolerance of 4096 bytes which is about 0.00390625 MB's
assert abs(folder_size_in_bytes(temp_dir) - MBs) <= 4096


@pytest.mark.parametrize(
("num_mb"),
[
(1),
(10),
(50),
(100),
(500),
(750)
]
)
def test_formatted_folder_size(temp_dir, num_mb: int):
mb_in_bytes = 1048576
MBs = num_mb * mb_in_bytes

# creating a test file of size variable size
with open(temp_dir + "/test.bin", "wb") as f:
f.write(os.urandom(MBs))

# checking if the formatted size w/o the decimal values is equal to expected
assert formatted_folder_size(temp_dir).split(".")[0] == str(num_mb)

32 changes: 32 additions & 0 deletions tests/cpp_zarr_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from zarr_libraries.cpp_zarr.cpp_zarr import *
from zarr_libraries.common import folder_size_in_bytes
from pathlib import Path
import shutil
import numpy as np
import pytest


def test_getting_data_path_from_cpp_zarr():
cpp_zarr = Cpp_Zarr()

expected_path = str((Path(__file__).parent / "../zarr_libraries/example_data/cpp_zarr_data/test.zarr").resolve())
assert cpp_zarr.data_path == expected_path


@pytest.mark.parametrize(
("shape", "chunks"),
[
([256, 256, 256], [64, 64, 64]),
([64, 128, 256], [32, 64, 64]),
]
)
def test_cpp_zarr_writes_correct_amount_of_data(shape: list, chunks: list):
cpp_zarr = Cpp_Zarr()

cpp_zarr.write_zarr(shape=shape, chunks=chunks)
expected_size = np.prod(shape)

# the actual size can be larger but can never be smaller that expected size
assert folder_size_in_bytes(cpp_zarr.data_path) >= expected_size
shutil.rmtree(cpp_zarr.data_path)

33 changes: 33 additions & 0 deletions tests/ome_zarr_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from zarr_libraries.ome_ngff.ome_zarr import Ome_Zarr
from zarr_libraries.common import folder_size_in_bytes
from pathlib import Path
import numpy as np
import shutil
import pytest


def test_getting_data_path_from_ome_zarr():
ome_zarr = Ome_Zarr()

expected_path = str((Path(__file__).parent / "../zarr_libraries/example_data/ome_zarr_data/test.zarr").resolve())
assert ome_zarr.data_path == expected_path


@pytest.mark.parametrize(
("shape", "chunks"),
[
([256, 256, 256], [64, 64, 64]),
([64, 128, 256], [32, 64, 64]),
]
)
def test_ome_zarr_writes_correct_amount_of_data(shape: list, chunks: list):
ome_zarr = Ome_Zarr()

zarr_data = np.random.randint(low=0, high=256, size=shape, dtype=np.uint8)
ome_zarr.write_zarr(chunks=chunks, zarr_data=zarr_data)
expected_size = np.prod(shape)

# the actual size can be larger but can never be smaller that expected size
assert folder_size_in_bytes(ome_zarr.data_path) >= expected_size
shutil.rmtree(ome_zarr.data_path)

60 changes: 60 additions & 0 deletions tests/tensorstore_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from zarr_libraries.tensorstore.tensorstore_zarr import Tensorstore
from zarr_libraries.common import folder_size_in_bytes
from pathlib import Path
import shutil
import numpy as np
import pytest


def test_getting_data_path_from_tensorstore():
tensorstore = Tensorstore()

expected_path = str((Path(__file__).parent / "../zarr_libraries/example_data/tensorstore_data/test.zarr").resolve())
assert tensorstore.data_path == expected_path


@pytest.mark.parametrize(
("shape", "chunks"),
[
([256, 256, 256], [64, 64, 64]),
([64, 128, 256], [32, 64, 64]),
]
)
def test_tensorstore_writes_correct_amount_of_data(shape: list, chunks: list):
tensorstore = Tensorstore()

zarr_data = np.random.randint(low=0, high=256, size=shape, dtype=np.uint8)
tensorstore.write_zarr(shape=shape, chunks=chunks, zarr_data=zarr_data)
expected_size = np.prod(shape)

# the actual size can be larger but can never be smaller that expected size
assert folder_size_in_bytes(tensorstore.data_path) >= expected_size
shutil.rmtree(tensorstore.data_path)


@pytest.mark.parametrize(
("shape", "chunks"),
[
([256, 256, 256], [64, 64, 64]),
([64, 128, 256], [32, 64, 64]),
]
)
def test_zarr_python_append_writes_correct_amount_of_data(shape: list, chunks: list):
tensorstore = Tensorstore()

zarr_data = np.random.randint(low=0, high=256, size=shape, dtype=np.uint8)
tensorstore.append_zarr(shape=shape, chunks=chunks, new_shape=shape, zarr_data=zarr_data, multiplier=1)
expected_size = np.prod(shape)

# checking the size of the first append
assert folder_size_in_bytes(tensorstore.data_path) >= expected_size

new_shape = [shape[0] * (2), *shape[1:]]
new_data = np.random.randint(low=0, high=256, size=shape, dtype=np.uint8)
tensorstore.append_zarr(shape=shape, chunks=chunks, new_shape=new_shape, zarr_data=new_data, multiplier=2)
expected_size = np.prod(new_shape)

# changing the shape and comparing the expected size after the second append
assert folder_size_in_bytes(tensorstore.data_path) >= expected_size
shutil.rmtree(tensorstore.data_path)

59 changes: 59 additions & 0 deletions tests/zarr_python_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from zarr_libraries.zarr_python.zarr_python import Zarr_Python
from zarr_libraries.common import folder_size_in_bytes
from pathlib import Path
import shutil
import numpy as np
import pytest


def test_getting_data_path_from_zarr_python():
zarr_python = Zarr_Python()

expected_path = str((Path(__file__).parent / "../zarr_libraries/example_data/zarr_python_data/test.zarr").resolve())
assert zarr_python.data_path == expected_path


@pytest.mark.parametrize(
("shape", "chunks"),
[
([256, 256, 256], [64, 64, 64]),
([64, 128, 256], [32, 64, 64]),
]
)
def test_zarr_python_writes_correct_amount_of_data(shape: list, chunks: list):
zarr_python = Zarr_Python()

zarr_data = np.random.randint(low=0, high=256, size=shape, dtype=np.uint8)
zarr_python.write_zarr(shape=shape, chunks=chunks, zarr_data=zarr_data)
expected_size = np.prod(shape)

# the actual size can be larger but can never be smaller that expected size
assert folder_size_in_bytes(zarr_python.data_path) >= expected_size
shutil.rmtree(zarr_python.data_path)


@pytest.mark.parametrize(
("shape", "chunks"),
[
([256, 256, 256], [64, 64, 64]),
([64, 128, 256], [32, 64, 64]),
]
)
def test_zarr_python_append_writes_correct_amount_of_data(shape: list, chunks: list):
zarr_python = Zarr_Python()

zarr_data = np.random.randint(low=0, high=256, size=shape, dtype=np.uint8)
zarr_python.append_zarr(shape=shape, chunks=chunks, zarr_data=zarr_data)
expected_size = np.prod(shape)

# checking the size of the first append
assert folder_size_in_bytes(zarr_python.data_path) >= expected_size

new_shape = [shape[0] * (2), *shape[1:]]
new_data = np.random.randint(low=0, high=256, size=shape, dtype=np.uint8)
zarr_python.append_zarr(shape=shape, chunks=chunks, zarr_data=new_data)
expected_size = np.prod(new_shape)

# changing the shape and comparing the expected size after the second append
assert folder_size_in_bytes(zarr_python.data_path) >= expected_size
shutil.rmtree(zarr_python.data_path)
Loading

0 comments on commit 771534b

Please sign in to comment.