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: keep fields when unionizing empty datasets #82

Merged
merged 1 commit into from
Mar 18, 2024
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
9 changes: 8 additions & 1 deletion cryosparc/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def append_many(
first_dset = datasets[0]
datasets = tuple(d for d in datasets if len(d) > 0) # skip empty datasets
if not datasets:
return cls(first_dset) # so that fields are kept
return cls(first_dset) # keep the same fields as the first

if not repeat_allowed:
all_uids = n.concatenate([dset["uid"] for dset in datasets])
Expand Down Expand Up @@ -354,7 +354,14 @@ def union_many(
Returns:
Dataset: combined dataset, or empty dataset if none are provided.
"""
if not datasets:
return cls()

first_dset = datasets[0]
datasets = tuple(d for d in datasets if len(d) > 0) # skip empty datasets
if not datasets:
return cls(first_dset) # keep the same fields as the first

keep_fields = cls.common_fields(*datasets, assert_same_fields=assert_same_fields)
keep_masks = []
keep_uids = n.array([], dtype=n.uint64)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ def test_union_many_empty():
assert len(Dataset.union_many().rows()) == 0


def test_union_empty_fields():
d1 = Dataset.allocate(0, [("field", "f4")])
d2 = Dataset.allocate(0, [("field", "f4")])
d3 = d1.union(d2)
assert len(d3) == 0
assert d3.fields() == ["uid", "field"]


def test_allocate_many_separate():
for _ in range(66_000):
allocated = []
Expand Down