HeloiseS / hoki

Bridging the gap between observation and theory
https://heloises.github.io/hoki/intro.html
BSD 3-Clause "New" or "Revised" License
47 stars 8 forks source link

[DOCS] More "pythonic" way for creating pixel coordinates in Voronoi binning example #101

Open Knusper opened 5 months ago

Knusper commented 5 months ago

The Voronoi binning example in the tutorial contains the following code:


# First we need to make lists of pixel Coordinates
# note this will be stupid big with large data cubes
# in that case find a smartter way to make these arrays

# ... X is a 1D list of length 150**2
X = []
for x in np.arange(0, cube.shape[2]):
    X+=[x]*cube.shape[2]

x =  np.array(X)

# ... Y is a 1D list of length 150**2
Y=[]
for y in np.arange(0,cube.shape[1]):
    Y+=list(np.arange(0,cube.shape[1]))

y =  np.array(Y)

I don't know if finding a smarter way was meant an exercise for the reader, but I certainly think there is a more efficient way provided by numpy to do this.

https://heloises.github.io/hoki/Voronoi_binning_of_SED_data.html#Book-keeping---creating-the-pixel-coordinate,-signal-and-noise-arrays

This does the trick:

X, Y = np.indices((cube.shape[2], cube.shape[1]))
x = X.flatten()
y = Y.flatten()

Can provide PR if you want.