Skip to content

Commit

Permalink
upgrade to pandas 2.0 groupby (#21)
Browse files Browse the repository at this point in the history
- Fix outer value lookup with vector data (#19)
- Fix compatibility with groupby from pandas 2.0
  • Loading branch information
edasubert authored Jun 2, 2023
1 parent f875d47 commit 48c34eb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

Version 3.1.5
-------------

- Fix outer value lookup with vector data (#19)
- Fix compatibility with groupby from pandas 2.0

Version 3.1.4
-------------
- switch hemisphere constants to be in line with Allen Institute
Expand Down
47 changes: 43 additions & 4 deletions tests/test_voxel_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,49 @@ def test_lookup():
assert_raises(VoxcellError, v.lookup, [[2, 10]])


def test_lookup_vector_data():
raw = np.array([[[11], [12]], [[21], [22]]])
v = test_module.VoxelData(raw, (2, 2))
assert_array_equal(v.lookup([[1, 1], [3, 3]]), [[11], [22]])
@pytest.mark.parametrize(
"raw, voxel_dimensions, coordinates, outer_value, expected",
[
(
[[[11, 11], [12, 12]], [[21, 21], [22, 22]]],
(2, 2),
[[1, 1], [3, 3]],
None,
[[11, 11], [22, 22]],
),
(
[[[11, 11], [12, 12]], [[21, 21], [22, 22]]],
(2, 2),
[[10, 1]],
[10, 10],
[[10, 10]],
),
(
np.zeros((1, 1, 1)),
(1, 1, 1),
[(-1, -1, -1)],
0,
[0],
),
(
np.zeros((1, 1, 1, 1)),
(1, 1, 1),
[(-1, -1, -1)],
0,
[(0,)],
),
(
np.zeros((1, 1, 1, 2)),
(1, 1, 1),
[(-1, -1, -1)],
(0, 0),
[(0, 0)],
),
],
)
def test_lookup_vector_data(raw, voxel_dimensions, coordinates, outer_value, expected):
voxel_data = test_module.VoxelData(np.array(raw), voxel_dimensions)
assert_array_equal(voxel_data.lookup(coordinates, outer_value), expected)


def test_positions_to_indices():
Expand Down
10 changes: 4 additions & 6 deletions voxcell/traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@ def assign_conditional(self, positions, preassigned):

unique_assigned = preassigned.drop_duplicates()
for values_comb in unique_assigned.values:

if len(values_comb) == 1:
values_comb = values_comb[0]
hashable = values_comb
else:
hashable = tuple(values_comb)
hashable = tuple(values_comb)

if hashable in subsections:
subdist = subsections[hashable]
Expand Down Expand Up @@ -179,6 +174,9 @@ def split(self, attributes):
"""
grouped_distributions = {}
for attr_values, traits in self.traits.groupby(attributes):
# this is backwards compatibility: https://github.com/pandas-dev/pandas/issues/42795
if len(attributes) == 1:
attr_values = tuple(attr_values)

dists = self.distributions.iloc[traits.index]

Expand Down
6 changes: 5 additions & 1 deletion voxcell/voxel_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ def lookup(self, positions, outer_value=None):
voxel_idx = self.positions_to_indices(positions, strict=outer_value is None)
outer_mask = np.any(voxel_idx == VoxelData.OUT_OF_BOUNDS, axis=-1)
if np.any(outer_mask):
result = np.full(voxel_idx.shape[:-1], outer_value)
result = np.full(
voxel_idx.shape[:-1] + self.payload_shape,
outer_value,
dtype=self.raw.dtype
)
inner_mask = np.logical_not(outer_mask) # pylint: disable=assignment-from-no-return
result[inner_mask] = self._lookup_by_indices(voxel_idx[inner_mask])
else:
Expand Down

0 comments on commit 48c34eb

Please sign in to comment.