-
-
Notifications
You must be signed in to change notification settings - Fork 25
Transpose #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Transpose #375
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5778167
Add new measurements
FrancescAlted 3deceab
[TEST] Added new tests
ricardosp4 aa0b8e9
[BENCH] Added the transpose benchmark
ricardosp4 ddea451
Restored matmul bench into main.
ricardosp4 a9b5af8
Merge branch 'main' into transpose
FrancescAlted 255bb1b
Merge branch 'Blosc:main' into transpose
ricardosp4 5317a80
[FIX] Resolving commits of PR.
ricardosp4 9c05ebc
Merge branch 'transpose' of github.com:ricardosp4/python-blosc2 into …
ricardosp4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"metadata": {}, | ||
"cell_type": "code", | ||
"source": [ | ||
"import numpy as np\n", | ||
"import blosc2\n", | ||
"import time\n", | ||
"import plotly.express as px\n", | ||
"import pandas as pd" | ||
], | ||
"id": "55765646130156ef", | ||
"outputs": [], | ||
"execution_count": null | ||
}, | ||
{ | ||
"metadata": {}, | ||
"cell_type": "code", | ||
"source": [ | ||
"sizes = [(100, 100), (500, 500), (500, 1000), (1000, 1000), (2000, 2000), (3000, 3000), (4000, 4000), (5000, 5000)]\n", | ||
"sizes_mb = [(np.prod(size) * 8) / 2**20 for size in sizes] # Convert to MB\n", | ||
"results = {\"numpy\": [], \"blosc2\": []}" | ||
], | ||
"id": "1cfb7daa6eee1401", | ||
"outputs": [], | ||
"execution_count": null | ||
}, | ||
{ | ||
"metadata": {}, | ||
"cell_type": "code", | ||
"source": [ | ||
"for method in [\"numpy\", \"blosc2\"]:\n", | ||
" for size in sizes:\n", | ||
" arr = np.random.rand(*size)\n", | ||
" arr_b2 = blosc2.asarray(arr)\n", | ||
"\n", | ||
" start_time = time.perf_counter()\n", | ||
"\n", | ||
" if method == \"numpy\":\n", | ||
" np.transpose(arr).copy()\n", | ||
" elif method == \"blosc2\":\n", | ||
" blosc2.transpose(arr_b2)\n", | ||
"\n", | ||
" end_time = time.perf_counter()\n", | ||
" time_b = end_time - start_time\n", | ||
"\n", | ||
" print(f\"{method}: shape={size}, Performance = {time_b:.6f} s\")\n", | ||
" results[method].append(time_b)" | ||
], | ||
"id": "384d0ad7983a8d26", | ||
"outputs": [], | ||
"execution_count": null | ||
}, | ||
{ | ||
"metadata": {}, | ||
"cell_type": "code", | ||
"source": [ | ||
"df = pd.DataFrame({\n", | ||
" \"Matrix Size (MB)\": sizes_mb,\n", | ||
" \"NumPy Time (s)\": results[\"numpy\"],\n", | ||
" \"Blosc2 Time (s)\": results[\"blosc2\"]\n", | ||
"})\n", | ||
"\n", | ||
"fig = px.line(df,\n", | ||
" x=\"Matrix Size (MB)\",\n", | ||
" y=[\"NumPy Time (s)\", \"Blosc2 Time (s)\"],\n", | ||
" title=\"Performance of Matrix Transposition (NumPy vs Blosc2)\",\n", | ||
" labels={\"value\": \"Time (s)\", \"variable\": \"Method\"},\n", | ||
" markers=True)\n", | ||
"\n", | ||
"fig.show()" | ||
], | ||
"id": "c71ffb39eb28992c", | ||
"outputs": [], | ||
"execution_count": null | ||
}, | ||
{ | ||
"metadata": {}, | ||
"cell_type": "code", | ||
"source": [ | ||
"%%time\n", | ||
"shapes = [\n", | ||
" (100, 100), (2000, 2000), (3000, 3000), (4000, 4000), (3000, 7000),\n", | ||
" (5000, 5000), (6000, 6000), (7000, 7000), (8000, 8000), (6000, 12000),\n", | ||
" (9000, 9000), (10000, 10000),\n", | ||
" (10500, 10500), (11000, 11000), (11500, 11500), (12000, 12000),\n", | ||
" (12500, 12500), (13000, 13000), (13500, 13500), (14000, 14000),\n", | ||
" (14500, 14500), (15000, 15000), (15500, 15500), (16000, 16000),\n", | ||
" (16500, 16500), (17000, 17000)\n", | ||
"]\n", | ||
"chunkshapes = [None, (150, 300), (200, 500), (500, 200), (1000, 1000)]\n", | ||
"\n", | ||
"sizes = []\n", | ||
"time_total = []\n", | ||
"chunk_labels = []\n", | ||
"\n", | ||
"for shape in shapes:\n", | ||
" size_mb = (np.prod(shape) * 8) / (2 ** 20)\n", | ||
"\n", | ||
" matrix_np = np.linspace(0, 1, np.prod(shape)).reshape(shape)\n", | ||
"\n", | ||
" t0 = time.perf_counter()\n", | ||
" result_numpy = np.transpose(matrix_np).copy()\n", | ||
" numpy_time = time.perf_counter() - t0\n", | ||
"\n", | ||
" time_total.append(numpy_time)\n", | ||
" sizes.append(size_mb)\n", | ||
" chunk_labels.append(\"NumPy\")\n", | ||
"\n", | ||
" print(f\"NumPy: Shape={shape}, Time = {numpy_time:.6f} s\")\n", | ||
"\n", | ||
" for chunk in chunkshapes:\n", | ||
" matrix_blosc2 = blosc2.asarray(matrix_np, chunks=chunk)\n", | ||
"\n", | ||
" t0 = time.perf_counter()\n", | ||
" result_blosc2 = blosc2.transpose(matrix_blosc2)\n", | ||
" blosc2_time = time.perf_counter() - t0\n", | ||
"\n", | ||
" sizes.append(size_mb)\n", | ||
" time_total.append(blosc2_time)\n", | ||
" chunk_labels.append(f\"{chunk[0]}x{chunk[1]}\" if chunk else \"Auto\")\n", | ||
"\n", | ||
" print(f\"Blosc2: Shape={shape}, Chunks = {matrix_blosc2.chunks}, Time = {blosc2_time:.6f} s\")\n", | ||
"\n", | ||
"df = pd.DataFrame({\n", | ||
" \"Matrix Size (MB)\": sizes,\n", | ||
" \"Time (s)\": time_total,\n", | ||
" \"Chunk Shape\": chunk_labels\n", | ||
"})\n", | ||
"\n", | ||
"fig = px.line(df,\n", | ||
" x=\"Matrix Size (MB)\",\n", | ||
" y=\"Time (s)\",\n", | ||
" color=\"Chunk Shape\",\n", | ||
" title=\"Performance of Matrix Transposition (Blosc2 vs NumPy)\",\n", | ||
" labels={\"value\": \"Time (s)\", \"variable\": \"Metric\"},\n", | ||
" markers=True)\n", | ||
"fig.show()" | ||
], | ||
"id": "bcdd8aa5f65df561", | ||
"outputs": [], | ||
"execution_count": null | ||
}, | ||
{ | ||
"metadata": {}, | ||
"cell_type": "code", | ||
"source": "", | ||
"id": "1d2f48f370ba7e7a", | ||
"outputs": [], | ||
"execution_count": null | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"name": "python3", | ||
"language": "python", | ||
"display_name": "Python 3 (ipykernel)" | ||
} | ||
}, | ||
"nbformat": 5, | ||
"nbformat_minor": 9 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import blosc2 | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
((3, 3), (2, 2), (1, 1)), | ||
((12, 11), (7, 5), (6, 2)), | ||
((1, 5), (1, 4), (1, 3)), | ||
((51, 603), (22, 99), (13, 29)), | ||
((10,), (5,), None), | ||
((31,), (14,), (9,)), | ||
] | ||
) | ||
def shape_chunks_blocks(request): | ||
return request.param | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dtype", | ||
{np.int32, np.int64, np.float32, np.float64}, | ||
) | ||
def test_transpose(shape_chunks_blocks, dtype): | ||
shape, chunks, blocks = shape_chunks_blocks | ||
a = blosc2.linspace(0, 1, shape=shape, chunks=chunks, blocks=blocks, dtype=dtype) | ||
at = blosc2.transpose(a) | ||
|
||
na = a[:] | ||
nat = np.transpose(na) | ||
|
||
np.testing.assert_allclose(at, nat) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dtype", | ||
{np.complex64, np.complex128}, | ||
) | ||
def test_complex(shape_chunks_blocks, dtype): | ||
shape, chunks, blocks = shape_chunks_blocks | ||
real_part = blosc2.linspace(0, 1, shape=shape, chunks=chunks, blocks=blocks, dtype=dtype) | ||
imag_part = blosc2.linspace(1, 0, shape=shape, chunks=chunks, blocks=blocks, dtype=dtype) | ||
complex_matrix = real_part + 1j * imag_part | ||
|
||
a = blosc2.asarray(complex_matrix) | ||
at = blosc2.transpose(a) | ||
|
||
na = a[:] | ||
nat = np.transpose(na) | ||
|
||
np.testing.assert_allclose(at, nat) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"scalar", | ||
{ | ||
1, # int | ||
5.1, # float | ||
1 + 2j, # complex | ||
np.int8(2), # NumPy int8 | ||
np.int16(3), # NumPy int16 | ||
np.int32(4), # NumPy int32 | ||
np.int64(5), # NumPy int64 | ||
np.float32(5.2), # NumPy float32 | ||
np.float64(5.3), # NumPy float64 | ||
np.complex64(0 + 3j), # NumPy complex64 | ||
np.complex128(2 - 4j), # NumPy complex128 | ||
}, | ||
) | ||
def test_scalars(scalar): | ||
at = blosc2.transpose(scalar) | ||
nat = np.transpose(scalar) | ||
|
||
np.testing.assert_allclose(at, nat) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"shape", | ||
[ | ||
(3, 3, 3), | ||
(12, 10, 10), | ||
(10, 10, 10, 11), | ||
(5, 4, 3, 2, 1, 1), | ||
], | ||
) | ||
def test_dims(shape): | ||
a = blosc2.linspace(0, 1, shape=shape) | ||
|
||
with pytest.raises(ValueError): | ||
blosc2.transpose(a) | ||
|
||
|
||
def test_disk(): | ||
a = blosc2.linspace(0, 1, shape=(3, 4), urlpath="a_test.b2nd", mode="w") | ||
c = blosc2.transpose(a, urlpath="c_test.b2nd", mode="w") | ||
|
||
na = a[:] | ||
nc = np.transpose(na) | ||
|
||
np.testing.assert_allclose(c, nc, rtol=1e-6) | ||
|
||
blosc2.remove_urlpath("a_test.b2nd") | ||
blosc2.remove_urlpath("c_test.b2nd") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.