Skip to content

Commit

Permalink
add some better tests for indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
pattonw committed Jun 21, 2024
1 parent dc220c4 commit ccb4b81
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 53 deletions.
54 changes: 24 additions & 30 deletions explore.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0.44273075 -0.17070776 -0.7056614 ]\n",
" [ 0.99363405 -0.7815089 1.3557754 ]\n",
" [ 0.5848475 0.59523624 -1.4611949 ]]\n",
"[[[-0.03777862 0.34548774 -0.67263967]\n",
" [ 0.3058082 0.29156172 -0.9545453 ]\n",
" [ 0.70072883 -1.5905756 0.06846493]]\n",
"\n",
" [[ 0.09116387 -1.4376557 0.5421799 ]\n",
" [ 1.1627685 1.9053004 1.2552763 ]\n",
" [ 0.2825793 -0.8584084 -1.4131508 ]]]\n"
" [[ 0.41885415 0.04308635 -0.457416 ]\n",
" [-1.1773857 -0.15212785 1.2121304 ]\n",
" [ 1.3966681 -0.16751394 -2.1650443 ]]]\n"
]
}
],
Expand All @@ -57,36 +57,32 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0.09116387 -1.4376557 0.5421799 ]\n",
" [ 1.1627685 1.9053004 1.2552763 ]\n",
" [ 0.2825793 -0.8584084 -1.4131508 ]]]\n"
"[[ 0.41885415 0.04308635 -0.457416 ]]\n"
]
}
],
"source": [
"array_sliced = Array(ds, (0, 0), (1, 1), adapter=slice(1,2))\n",
"array_sliced = Array(ds, (0, 0), (1, 1), adapter=(slice(1,2), 0, slice(None)))\n",
"print(array_sliced[array_sliced.roi])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[10.091164 8.562345 10.54218 ]\n",
" [11.162768 11.9053 11.255277]\n",
" [10.282579 9.141592 8.586849]]]\n"
"[[10.418854 10.043086 9.542584]]\n"
]
}
],
Expand All @@ -97,30 +93,28 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0.44273075 -0.17070776 -0.7056614 ]\n",
" [ 0.99363405 -0.7815089 1.3557754 ]\n",
" [ 0.5848475 0.59523624 -1.4611949 ]]\n",
"\n",
" [[10.091164 8.562345 10.54218 ]\n",
" [11.162768 11.9053 11.255277 ]\n",
" [10.282579 9.141592 8.586849 ]]]\n"
"[10.418854 10.043086]\n",
"['d1']\n"
]
}
],
"source": [
"print(ds[:])"
"array_sliced.adapt(np.s_[0])\n",
"array_sliced.adapt(np.s_[:2])\n",
"print(array_sliced[array_sliced.roi])\n",
"print(array_sliced.axis_names)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand All @@ -133,8 +127,8 @@
" [False False False]]\n",
"\n",
" [[ True True True]\n",
" [ True True True]\n",
" [ True True True]]]\n"
" [False False False]\n",
" [False False False]]]\n"
]
}
],
Expand All @@ -146,7 +140,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand All @@ -156,8 +150,8 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mthresholded\u001b[49m\u001b[43m[\u001b[49m\u001b[43mthresholded\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mroi\u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m thresholded[thresholded\u001b[38;5;241m.\u001b[39mroi] \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m10\u001b[39m\n",
"File \u001b[0;32m~/Work/Packages/funlib.persistence/funlib/persistence/arrays/array.py:211\u001b[0m, in \u001b[0;36mArray.__setitem__\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 209\u001b[0m da\u001b[38;5;241m.\u001b[39mstore(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdata[roi_slices], \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_source_data, regions\u001b[38;5;241m=\u001b[39mroi_slices)\n\u001b[1;32m 210\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 211\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[1;32m 212\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThis array is not writeable since you have applied a custom callable \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 213\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124madapter that may or may not be invertable.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 214\u001b[0m )\n",
"Cell \u001b[0;32mIn[9], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mthresholded\u001b[49m\u001b[43m[\u001b[49m\u001b[43mthresholded\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mroi\u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m=\u001b[39m thresholded[thresholded\u001b[38;5;241m.\u001b[39mroi] \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m10\u001b[39m\n",
"File \u001b[0;32m~/Work/Packages/funlib.persistence/funlib/persistence/arrays/array.py:314\u001b[0m, in \u001b[0;36mArray.__setitem__\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 311\u001b[0m da\u001b[38;5;241m.\u001b[39mstore(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdata[key], \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_source_data, regions\u001b[38;5;241m=\u001b[39mregion_slices)\n\u001b[1;32m 313\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 314\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[1;32m 315\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThis array is not writeable since you have applied a custom callable \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 316\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124madapter that may or may not be invertable.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 317\u001b[0m )\n",
"\u001b[0;31mRuntimeError\u001b[0m: This array is not writeable since you have applied a custom callable adapter that may or may not be invertable."
]
}
Expand Down
40 changes: 17 additions & 23 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,11 @@ def test_constructor():
with pytest.raises(AssertionError):
Array(data, offset, (1, 1))

# ROI not multiple of voxel size
# This is fine!
# with pytest.raises(AssertionError):
Array(data, offset, (1, 1, 3))
# with pytest.raises(AssertionError):
Array(data, offset, (1, 1, 4))

# ROI begin doesn't align with voxel size
# This is fine!
# with pytest.raises(AssertionError):
Array(data, (1, 1, 1), (1, 1, 2))

# ROI shape doesn't align with voxel size
# No longer possible, we only specify the offset
# roi = Roi((0, 0, 0), (11, 11, 11))
# with pytest.raises(AssertionError):
# Array(data, roi, (1, 1, 2))

# ROI outside of provided data
# No longer possible, we only provide the offset
# roi = Roi((0, 0, 0), (20, 20, 20))
# with pytest.raises(AssertionError):
# Array(data, roi, (1, 1, 1))
# with pytest.raises(AssertionError):
# Array(data, roi, (2, 2, 1))
# with pytest.raises(AssertionError):
# Array(data, roi, (2, 2, 2), data_offset=(0, 0, 2))

def test_dtype():
for dtype in [np.float32, np.uint8, np.uint64]:
assert Array(np.zeros((1,), dtype=dtype), (0,), (1,)).dtype == dtype
Expand Down Expand Up @@ -222,3 +199,20 @@ def test_adapters():

a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=lambda x: x + 0.5)
assert a.dtype == float

def test_slicing():
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))

a.adapt(np.s_[0:3, 1, :])
assert a.shape == (3, 2)
assert a.axis_names == ["c0^", "d1"]
assert a.units == [""]

a.adapt(np.s_[2, :])
assert a.shape == (2,)
assert a.axis_names == ["d1"]
assert a.units == [""]

a[:] = 42

assert all([x == 42 for x in a._source_data[2, 1, :]]), a._source_data

0 comments on commit ccb4b81

Please sign in to comment.