Sampling without replacement along a 2D grid in Cython without gil (can be in C++) - random

I have essentially a 2D grid of N by M (i.e. N rows, and M columns). I would like to sample from this grid without replacement in Cython without requiring the GIL. I would want something like this
for j in range(n_samples):
row_idx, col_idx = sample_without_replacement(grid)
So for example, say my 2D grid is 3 x 4, then I might get the following samples
(0, 3),
(0, 2),
(2, 1),
(3, 0)
Is there a simple way to do this in Cython?

Related

Iterate Over Circular Area Of Hexagonal Grid?

I am currently working with a hexagonal grid of cells which are indexed like this:
I am trying to find the simplest way to iterate over a circular area of this grid. For example, with (3, 3) as the center cell and a radius of one cell, I would want the loop to iterate over the cells (3, 3), (4, 4), (4, 3), (4, 2), (3, 2), (2, 3), (3, 4) (in any order). Given the coordinate of the center cell and a radius (excluding the center cell), how would you construct a two-dimensional loop to iterate over each cell/coordinate?
The basic idea:
Let's scan each row of the hexagon left-to-right.
How to do it:
Let R be the "cell radius".
Start at the center and move left R cells. Call this cell "A". A's row has 2*R-1 cells in it.
Now step one cell up/right. This cell begins a row with one less
cell.
Repeat step 2 until you've stepped up/right R times.
This covers the top half. Now do the lower half with similar steps (stepping down/right starting from A, to find the beginning of each of those rows).
Details
How do you step up/right?
The y coordinate increases by 1. The x coordinate increases by 1 only if y was odd.
How do you step down/right?
The y coordinate decreases by 1. The x coordinate increases by 1 only if y was odd.
The choice of grid system is what makes this one complicated. Because, for example, a step down and right might change your coordinates by (1, -1) or (0, -1) depending on which row you are in.
Therefore I would move to a different grid system, do the calculation there, then switch back to the grid system that you're showing.
Here is a demonstration of that in Python.
def loop (center, radius):
if 0 == radius:
yield center
else:
directions = [
( 1, -1),
( 0, -1),
(-1, 0),
(-1, 1),
( 0, 1),
( 1, 0),
]
cell = list(to_normalized(center))
cell[1] += radius
for direction in directions:
for i in range(radius):
cell[0] += direction[0]
cell[1] += direction[1]
yield to_grid(cell)
def to_normalized (cell):
return (cell[0] - (cell[1] // 2), cell[1])
def to_grid (cell):
return (cell[0] + (cell[1] // 2), cell[1])
for cell in loop((3, 3), 2):
print(cell)

Rearranging RGB values to GRB, GBR, BRG, BGR, and RBG through an entire directory

I have a directory of images for a CNN. I would like to be able to rearrange each band in a different order to help better train my model to allow it to recognize my objects. I so far have some code working with cv2. It is separating the bands, but I am having trouble rearranging the bands.
import cv2
import numpy
img = cv2.imread("IMG_4540.jpg")
g,b,r = cv2.split(img)
cv2.imwrite('green_channel.jpg', g)
I would like to have 6 separate images all with different band combinations from one singular image if possible.
You can just form all reorderings with numpy's indexing capabilities.
import numpy as np
from itertools import permutations
# first generate all sets of rearrangements you'd like to make..
orderings = [p for p in permutations(np.arange(3)) if p!=(0,1,2)]
# [(0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
# rbg, brg, and so on.
# then reorder along axis=-1 using these. (0,1,2) --> (0,2,1) and so on.
for order in orderings:
reordered = im[...,order]
# then save each an appropriate filename
cv2.imsave('filename.jpg', reordered)
del reordered, order

Use movement vector to detect impact in 2D field

Given a coordinate and a movement vector. How can I get the list of all blocks will be infected?
I think it's a little bit like "object in view" or "object collision"?
For example, the original point O is (2, 2), the movement vector "→" is (0, 1) and the blocks B should be [(1, 3), (1, 4), (2, 4), (3, 3), (3, 4)]
0123 y→
0.......
1...BB..
2..O→B..
3...BB..
x.......
↓.......
If the movement vector is (-1, 1), the B should be [(0, 2), (0, 3), (0, 4), (1, 4), (2, 4)]
0123 y→
0..BBB..
1...↗B..
2..O.B..
3.......
x.......
↓.......
I'm currently considering
point P (x, y) vector V (v, u)
x' = x + v
y' = y + u
set S = ([x'-1, x'+1], [y'-1, y'+1])
calculate a line L "y=ax+b" perpendicular to V crossing (x', y').
split S into two groups by L
chose the one V is facing
but I cannot find a way to achieve last two steps.
Any suggestion will be helpful.
I think what you are looking for is something called time-until-impact or time-until-collision or something like that.
But in most game engines this is not the way to go. What a standard technique for collision handling is, is space partitioning. That means, if you have n rigid bodies in your simulation then a naive algorithm requires n^2 checks (each with another) to see if a collision happened. However, performance-wise this is a killer. Instead you should partition your space via (`uniform partitioning, Oct-Trees, Z-ordering, etc.), then you only check with neighboring rigid bodies that are neighbors with the cell in question.
Note, that the velocity vector (what you call movement) vector is not really needed here because in practice it brings no benefits.

Finding the shape corresponding to a set of points

I'm working on a small problem right now.
I'm reading in from a file a set of points, and I am asked to find the area inside of them (there is more to the problem, but I'm not worried about that right now). I am given the number of points, and the problem I have is that my area function does not work if the points are not a edge traversal.
For example: If the set of points is [(0,0) , (1, 0), (1, 1), (0, 1)], it will correctly calculate the area as 1. However, if the set of points is given as [(0,0) , (1, 1), (1, 0), (0, 1)], it will return 0 as the area.
How can I take the list of points, and have it find a traversal (either clockwise or counterclockwise, it doesn't matter)? I'm not familiar with any fast algorithms to do so.
Note: This is not a convex-hull problem. The shape does not necessarily have to be convex. For example, the set of points [(0,0), (0,2), (1, 1), (2, 2), (2, 0)] is a valid shape.

Algorithm to iterate through image pixels

I'm sure something for this already exists so I figure why reinvent the wheel.
Does anyone know of an algorithm that will iterate through pixels in an image from the top left towards the bottom right.
So first it will check: (0, 0)
Then: (1, 0), (1, 1), (0, 1)
Then (2, 0), (2, 1), (2, 2), (1, 2), (0, 2)
... and so on....
Looking for the most efficient algorithm for this problem.
Thanks.
It seems you want to iterate in this manner:
1 4 9
↑ ↑
2→3 8
↑
5→6→7
...
This is just a for-loop:
for radius in range(squareImage.width):
for col in range(radius):
yield (radius, col)
for row in range(radius):
yield (radius-row, radius)
yield (0, radius)
Efficiency is not a concern here. It is impossible to do better than linear-in-number-of-pixels (like this) because you have to visit every pixel.

Resources