Skip to content
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

Fix: reliable export hdf5 with small chunks size #2280

Merged
merged 2 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/vaex-hdf5/vaex/hdf5/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def layout(self, df, progress=None):
self._layout_called = True

def write(self, df, chunk_size=int(1e5), parallel=True, progress=None, column_count=1, export_threads=0):
chunk_size = ((chunk_size + 7) // 8) * 8 # round up to multiple of 8
assert self._layout_called, "call .layout() first"
N = len(df)
if N == 0:
Expand Down Expand Up @@ -247,7 +248,7 @@ def write(self, values):
raise ValueError("Cannot write to non-byte aligned offset")
null_buffer = values.buffers()[0]
if null_buffer is not None:
self.null_bitmap_array[byte_index1:byte_index2] = memoryview(null_buffer)
self.null_bitmap_array[byte_index1:byte_index2] = memoryview(null_buffer[:byte_index2-byte_index1])
else:
self.null_bitmap_array[byte_index1:byte_index2] = 0xff
if np.ma.isMaskedArray(self.to_array) and np.ma.isMaskedArray(values):
Expand Down
33 changes: 33 additions & 0 deletions tests/export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,36 @@ def test_export_hdf5_missing_values(tmpdir):
assert df2.x.tolist() == [1, None, 5, None, 10]
assert df2.y.tolist() == [1.1, None, 5.5, None, 10.10]
assert df2.z.tolist() == ['Yes', None, 'No', None, 'Maybe']

@pytest.mark.parametrize("chunk_size", [3, 33])
def test_export_hdf5_small_chunks_case_1(tmpdir, chunk_size):
x = pa.array([1, 2, None, None, 5])
y = pa.array([1.1, 2.2, None, None, 5.5])
s = pa.array(['dog', 'cat', None, None, 'mouse'])
df = vaex.from_arrays(x=x, y=y, s=s)

export_path = str(tmpdir.join('tmp.hdf5'))
df.export_hdf5(export_path, chunk_size=chunk_size)

df_hdf5 = vaex.open(export_path)
assert df_hdf5.shape == (5, 3)
assert df_hdf5.x.tolist() == [1, 2, None, None, 5]
assert df_hdf5.y.tolist() == [1.1, 2.2, None, None, 5.5]
assert df_hdf5.s.tolist() == ['dog', 'cat', None, None, 'mouse']


@pytest.mark.parametrize("chunk_size", [3, 33])
def test_export_hdf5_small_chunks_case_2(tmpdir, chunk_size):
x = pa.array([1, 2, None, None, None, None, None, None, 5])
y = pa.array([1.1, 2.2, None, None, None, None, None, None, 5.5])
s = pa.array(['dog', 'cat', None, None, None, None, None, None, 'mouse'])
df = vaex.from_arrays(x=x, y=y, s=s)

export_path = str(tmpdir.join('tmp.hdf5'))
df.export_hdf5(export_path, chunk_size=chunk_size)

df_hdf5 = vaex.open(export_path)
assert df_hdf5.shape == (9, 3)
assert df_hdf5.x.tolist() == [1, 2, None, None, None, None, None, None, 5]
assert df_hdf5.y.tolist() == [1.1, 2.2, None, None, None, None, None, None, 5.5]
assert df_hdf5.s.tolist() == ['dog', 'cat', None, None, None, None, None, None, 'mouse']