Skip to content

Commit

Permalink
add back notebook test
Browse files Browse the repository at this point in the history
  • Loading branch information
carderne committed Feb 17, 2024
1 parent 3f27e5a commit f54a87a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ lint:
.PHONY: test
test:
pytest
./examples/test.sh
2 changes: 1 addition & 1 deletion examples/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
"metadata": {},
"outputs": [],
"source": [
"true_pos, false_neg = gf.accuracy(grid_truth, guess_out, aoi_in)\n",
"true_pos, false_neg = gf.accuracy(grid_truth_in, guess_out, aoi_in)\n",
"print(f\"Points identified as grid that are grid: {100*true_pos:.0f}%\")\n",
"print(f\"Actual grid that was missed: {100*false_neg:.0f}%\")"
]
Expand Down
16 changes: 16 additions & 0 deletions examples/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

cd examples

# convert the example notebook to a script
python -m jupyter nbconvert --to script example.ipynb


# disable Jupyter and plt.imshow output
sed -i -e 's/jupyter=True/jupyter=False/g' example.py
sed -i -e 's/plt.imshow(.*)//g' example.py

# run script
python example.py

rm example.py example.py-e || true
41 changes: 22 additions & 19 deletions gridfinder/gridfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,22 @@ def optimise(

while len(queue):
_, current_loc = heappop(queue)
current_i = current_loc[0]
current_j = current_loc[1]
current_dist = dist[current_loc]

for x in (-1, 0, 1):
for y in (-1, 0, 1):
next_i = current_i + x
next_j = current_j + y
next_i = current_loc[0] + x
next_j = current_loc[1] + y
next_loc = (next_i, next_j)

# ensure we're within bounds
if next_i < 0 or next_j < 0 or next_i >= max_i or next_j >= max_j:
continue

# ensure we're not looking at the same spot
if next_loc == current_loc:
continue

# skip if we've already set dist to 0
if dist[next_loc] == 0.0:
if (
(x == 0 and y == 0) # same spot
or dist[next_loc] == 0.0 # already zerod
or next_i < 0 # out of bounds
or next_j < 0
or next_i >= max_i
or next_j >= max_j
):
continue

# if the location is connected
Expand All @@ -169,12 +165,14 @@ def optimise(

next_dist = current_dist + dist_add

# visited before
if visited[next_loc]:
if next_dist < dist[next_loc]:
dist[next_loc] = next_dist
prev[next_loc] = current_loc
heappush(queue, (next_dist, next_loc))

# brand new cell - progress!
else:
heappush(queue, (next_dist, next_loc))
visited[next_loc] = 1
Expand All @@ -186,9 +184,14 @@ def optimise(
progress_new = int(100 * counter / max_cells)
if progress_new > progress:
progress = progress_new
if progress % 5 == 0:
print(progress)
else:
print(".")

with nb.objmode():
print_progress(progress)
print()
return dist


def print_progress(progress: int) -> None:
if progress % 5 == 0:
print(progress, end="", flush=True)
else:
print(".", end="", flush=True)

0 comments on commit f54a87a

Please sign in to comment.