Skip to content

Commit ccb4b81

Browse files
committed
add some better tests for indexing
1 parent dc220c4 commit ccb4b81

File tree

2 files changed

+41
-53
lines changed

2 files changed

+41
-53
lines changed

explore.ipynb

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
"name": "stdout",
4141
"output_type": "stream",
4242
"text": [
43-
"[[[ 0.44273075 -0.17070776 -0.7056614 ]\n",
44-
" [ 0.99363405 -0.7815089 1.3557754 ]\n",
45-
" [ 0.5848475 0.59523624 -1.4611949 ]]\n",
43+
"[[[-0.03777862 0.34548774 -0.67263967]\n",
44+
" [ 0.3058082 0.29156172 -0.9545453 ]\n",
45+
" [ 0.70072883 -1.5905756 0.06846493]]\n",
4646
"\n",
47-
" [[ 0.09116387 -1.4376557 0.5421799 ]\n",
48-
" [ 1.1627685 1.9053004 1.2552763 ]\n",
49-
" [ 0.2825793 -0.8584084 -1.4131508 ]]]\n"
47+
" [[ 0.41885415 0.04308635 -0.457416 ]\n",
48+
" [-1.1773857 -0.15212785 1.2121304 ]\n",
49+
" [ 1.3966681 -0.16751394 -2.1650443 ]]]\n"
5050
]
5151
}
5252
],
@@ -57,36 +57,32 @@
5757
},
5858
{
5959
"cell_type": "code",
60-
"execution_count": 4,
60+
"execution_count": 5,
6161
"metadata": {},
6262
"outputs": [
6363
{
6464
"name": "stdout",
6565
"output_type": "stream",
6666
"text": [
67-
"[[[ 0.09116387 -1.4376557 0.5421799 ]\n",
68-
" [ 1.1627685 1.9053004 1.2552763 ]\n",
69-
" [ 0.2825793 -0.8584084 -1.4131508 ]]]\n"
67+
"[[ 0.41885415 0.04308635 -0.457416 ]]\n"
7068
]
7169
}
7270
],
7371
"source": [
74-
"array_sliced = Array(ds, (0, 0), (1, 1), adapter=slice(1,2))\n",
72+
"array_sliced = Array(ds, (0, 0), (1, 1), adapter=(slice(1,2), 0, slice(None)))\n",
7573
"print(array_sliced[array_sliced.roi])"
7674
]
7775
},
7876
{
7977
"cell_type": "code",
80-
"execution_count": 5,
78+
"execution_count": 6,
8179
"metadata": {},
8280
"outputs": [
8381
{
8482
"name": "stdout",
8583
"output_type": "stream",
8684
"text": [
87-
"[[[10.091164 8.562345 10.54218 ]\n",
88-
" [11.162768 11.9053 11.255277]\n",
89-
" [10.282579 9.141592 8.586849]]]\n"
85+
"[[10.418854 10.043086 9.542584]]\n"
9086
]
9187
}
9288
],
@@ -97,30 +93,28 @@
9793
},
9894
{
9995
"cell_type": "code",
100-
"execution_count": 6,
96+
"execution_count": 7,
10197
"metadata": {},
10298
"outputs": [
10399
{
104100
"name": "stdout",
105101
"output_type": "stream",
106102
"text": [
107-
"[[[ 0.44273075 -0.17070776 -0.7056614 ]\n",
108-
" [ 0.99363405 -0.7815089 1.3557754 ]\n",
109-
" [ 0.5848475 0.59523624 -1.4611949 ]]\n",
110-
"\n",
111-
" [[10.091164 8.562345 10.54218 ]\n",
112-
" [11.162768 11.9053 11.255277 ]\n",
113-
" [10.282579 9.141592 8.586849 ]]]\n"
103+
"[10.418854 10.043086]\n",
104+
"['d1']\n"
114105
]
115106
}
116107
],
117108
"source": [
118-
"print(ds[:])"
109+
"array_sliced.adapt(np.s_[0])\n",
110+
"array_sliced.adapt(np.s_[:2])\n",
111+
"print(array_sliced[array_sliced.roi])\n",
112+
"print(array_sliced.axis_names)"
119113
]
120114
},
121115
{
122116
"cell_type": "code",
123-
"execution_count": 7,
117+
"execution_count": 8,
124118
"metadata": {},
125119
"outputs": [
126120
{
@@ -133,8 +127,8 @@
133127
" [False False False]]\n",
134128
"\n",
135129
" [[ True True True]\n",
136-
" [ True True True]\n",
137-
" [ True True True]]]\n"
130+
" [False False False]\n",
131+
" [False False False]]]\n"
138132
]
139133
}
140134
],
@@ -146,7 +140,7 @@
146140
},
147141
{
148142
"cell_type": "code",
149-
"execution_count": 8,
143+
"execution_count": 9,
150144
"metadata": {},
151145
"outputs": [
152146
{
@@ -156,8 +150,8 @@
156150
"traceback": [
157151
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
158152
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
159-
"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",
160-
"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",
153+
"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",
154+
"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",
161155
"\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."
162156
]
163157
}

tests/test_array.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,11 @@ def test_constructor():
2020
with pytest.raises(AssertionError):
2121
Array(data, offset, (1, 1))
2222

23-
# ROI not multiple of voxel size
24-
# This is fine!
25-
# with pytest.raises(AssertionError):
2623
Array(data, offset, (1, 1, 3))
27-
# with pytest.raises(AssertionError):
2824
Array(data, offset, (1, 1, 4))
2925

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

35-
# ROI shape doesn't align with voxel size
36-
# No longer possible, we only specify the offset
37-
# roi = Roi((0, 0, 0), (11, 11, 11))
38-
# with pytest.raises(AssertionError):
39-
# Array(data, roi, (1, 1, 2))
40-
41-
# ROI outside of provided data
42-
# No longer possible, we only provide the offset
43-
# roi = Roi((0, 0, 0), (20, 20, 20))
44-
# with pytest.raises(AssertionError):
45-
# Array(data, roi, (1, 1, 1))
46-
# with pytest.raises(AssertionError):
47-
# Array(data, roi, (2, 2, 1))
48-
# with pytest.raises(AssertionError):
49-
# Array(data, roi, (2, 2, 2), data_offset=(0, 0, 2))
50-
5128
def test_dtype():
5229
for dtype in [np.float32, np.uint8, np.uint64]:
5330
assert Array(np.zeros((1,), dtype=dtype), (0,), (1,)).dtype == dtype
@@ -222,3 +199,20 @@ def test_adapters():
222199

223200
a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=lambda x: x + 0.5)
224201
assert a.dtype == float
202+
203+
def test_slicing():
204+
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))
205+
206+
a.adapt(np.s_[0:3, 1, :])
207+
assert a.shape == (3, 2)
208+
assert a.axis_names == ["c0^", "d1"]
209+
assert a.units == [""]
210+
211+
a.adapt(np.s_[2, :])
212+
assert a.shape == (2,)
213+
assert a.axis_names == ["d1"]
214+
assert a.units == [""]
215+
216+
a[:] = 42
217+
218+
assert all([x == 42 for x in a._source_data[2, 1, :]]), a._source_data

0 commit comments

Comments
 (0)