Skip to content

Commit

Permalink
make slice by time result in expected behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniBodor committed Dec 6, 2023
1 parent 7e9d379 commit 3dd8f7b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 65 deletions.
10 changes: 5 additions & 5 deletions eitprocessing/mixins/slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ def select_by_time( # pylint: disable=too-many-arguments
"cannot be selected by time."
)

if start is None:
if start is None or start < self.time[0]:
start_index = 0
elif start_inclusive:
start_index = bisect.bisect_left(self.time, start)
start_index = bisect.bisect_right(self.time, start) - 1
else:
start_index = bisect.bisect_right(self.time, start)
start_index = bisect.bisect_left(self.time, start)

if end is None:
end_index = len(self.time)
elif end_inclusive:
end_index = bisect.bisect_right(self.time, end) - 1
end_index = bisect.bisect_left(self.time, end) + 1
else:
end_index = bisect.bisect_left(self.time, end) - 1
end_index = bisect.bisect_right(self.time, end)

return self.select_by_index(
start=start_index,
Expand Down
145 changes: 85 additions & 60 deletions eitprocessing/mixins/test_slicing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"DraegerEITData_139828016111744\n",
"Frame (10) of <DraegerEITData_139828016111744>\n",
"Frame (10) of <DraegerEITData_139828016111744>\n",
"Slice (0-9) of <DraegerEITData_139828016111744>\n",
"No frames selected from <DraegerEITData_139828016111744>\n",
"Slice (10-11) of <DraegerEITData_139828016111744>\n"
"DraegerEITData_139996610960960\n",
"Frame (10) of <DraegerEITData_139996610960960>\n",
"Frame (10) of <DraegerEITData_139996610960960>\n",
"Frames (0-9) of <DraegerEITData_139996610960960>\n",
"No frames selected from <DraegerEITData_139996610960960>\n",
"Frames (10-11) of <DraegerEITData_139996610960960>\n"
]
}
],
Expand All @@ -76,44 +76,29 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Slicing at exact timepoints\n",
"These should all be identical\n",
"[75628.471 75628.521 75628.571]\n",
"[75628.471 75628.521 75628.571]\n",
"[75628.471 75628.521]\n",
"\n",
"[75628.471 75628.521 75628.571]\n",
"[75628.521 75628.571]\n",
"[75628.521]\n",
"\n",
"XXXXXXXXXXXXXXXXXXXXX\n",
"\n",
"[75628.471 75628.521 75628.571]\n",
"[75628.471 75628.521 75628.571]\n",
"[75628.471 75628.521 75628.571]\n",
"\n",
"[75628.471 75628.521 75628.571]\n",
"[75628.471 75628.521 75628.571]\n",
"[75628.471 75628.521 75628.571]\n",
"\n",
"XXXXXXXXXXXXXXXXXXXXX\n",
"\n",
"[75628.471 75628.521]\n",
"[75628.471 75628.521]\n",
"[75628.471 75628.521]\n",
"\n",
"[75628.521]\n",
"[75628.521]\n",
"[75628.521]\n"
"[75628.471 75628.521 75628.571]\n"
]
}
],
"source": [
"print('Slicing at exact timepoints')\n",
"print('These should all be identical')\n",
"\n",
"tp0 = 75628.471\n",
"tp2 = 75628.571\n",
"\n",
Expand All @@ -123,45 +108,85 @@
"print(\"\")\n",
"print(data.select_by_time(tp0,tp2).time)\n",
"print(data.select_by_time(tp0,tp2, start_inclusive=False).time)\n",
"print(data.select_by_time(tp0,tp2, start_inclusive=False, end_inclusive=False).time)\n",
"\n",
"print(\"\")\n",
"print('XXXXXXXXXXXXXXXXXXXXX')\n",
"print(\"\")\n",
"\n",
"tp0 = tp0-0.001\n",
"tp2 = tp2+0.001\n",
"\n",
"print(data.select_by_time(0,tp2).time)\n",
"print(data.select_by_time(0,tp2, start_inclusive=False).time)\n",
"print(data.select_by_time(0,tp2, end_inclusive=False).time)\n",
"print(\"\")\n",
"print(data.select_by_time(tp0,tp2).time)\n",
"print(data.select_by_time(tp0,tp2, start_inclusive=False).time)\n",
"print(data.select_by_time(tp0,tp2, start_inclusive=False, end_inclusive=False).time)\n",
"\n",
"print(\"\")\n",
"print('XXXXXXXXXXXXXXXXXXXXX')\n",
"print(\"\")\n",
"print(data.select_by_time(tp0,tp2, start_inclusive=False, end_inclusive=False).time)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Slicing in between timepoints\n",
"for each `False` should exclude be 1 smaller!\n",
"True True: [75628.471 75628.521 75628.571]\n",
"True False: [75628.471 75628.521]\n",
"False True: [75628.521 75628.571]\n",
"False False: [75628.521]\n"
]
}
],
"source": [
"print('Slicing in between timepoints')\n",
"print('for each `False` should exclude be 1 smaller!')\n",
"\n",
"tp0 = tp0+0.002\n",
"tp2 = tp2-0.002\n",
"tp0_ = tp0 + 0.01\n",
"tp2_ = tp2 - 0.01\n",
"\n",
"print(data.select_by_time(0,tp2).time)\n",
"print(data.select_by_time(0,tp2, start_inclusive=False).time)\n",
"print(data.select_by_time(0,tp2, end_inclusive=False).time)\n",
"print(\"\")\n",
"print(data.select_by_time(tp0,tp2).time)\n",
"print(data.select_by_time(tp0,tp2, start_inclusive=False).time)\n",
"print(data.select_by_time(tp0,tp2, start_inclusive=False, end_inclusive=False).time)\n"
"for start_incl in (True, False):\n",
" for end_incl in (True, False):\n",
" print(start_incl, end_incl, end=\": \")\n",
" print(data.select_by_time(tp0_,tp2_, start_incl, end_incl).time)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Slicing before first or after last timepoint\n",
"each set of 3 should be identical\n",
"\n",
"slice before 1st timepoint 75628.46100000001\n",
"[75628.471 75628.521 75628.571]\n",
"[75628.471 75628.521 75628.571]\n",
"[75628.471 75628.521 75628.571]\n",
"\n",
"slice after last timepoint: 75719.431\n",
"[75719.321 75719.371 75719.421]\n",
"[75719.321 75719.371 75719.421]\n",
"[75719.321 75719.371 75719.421]\n"
]
}
],
"source": [
"print('Slicing before first or after last timepoint')\n",
"print('each set of 3 should be identical')\n",
"print('')\n",
"\n",
"tp0__ = tp0 - 0.01\n",
"tp_2 = 75719.321\n",
"tpx__ = 75719.421 + 0.01\n",
"\n",
"print(f'slice before 1st timepoint {tp0__}')\n",
"print(data.time[:3])\n",
"print(data.select_by_time(tp0, tp2, start_inclusive=True).time)\n",
"print(data.select_by_time(tp0__, tp2, start_inclusive=False).time)\n",
"\n",
"print('')\n",
"\n",
"print(f'slice after last timepoint: {tpx__}')\n",
"print(data.time[-3:])\n",
"print(data.select_by_time(tp_2, tpx__, end_inclusive=True).time)\n",
"print(data.select_by_time(tp_2, tpx__, end_inclusive=False).time)\n"
]
},
{
"cell_type": "code",
Expand Down

0 comments on commit 3dd8f7b

Please sign in to comment.