-1

I am trying to read through the numbers within a 1D array, but I seem to be having some trouble. It seems that this is a common issue, but I can't seem to figure out the solution in my case.

I have extracted a list of star coordinates from an image, and want to read through the x and y values:

# Find centres of mass of each labeled objects
xy = np.array(ndimage.center_of_mass(data, labeled, range(1, num_objects+1)))

# Unpack star coordinates, split array into x and y components
print(xy)
x, y = np.hsplit(xy, 2)

# array of coordinates
star_coords = [x, y]
print(star_coords)

for i in range(len(star_coords)):
    # Fit a PSF to each star
    print(x[i])
    x = x[i]
    y = y[i]
    x2, y2, amplitude, intensity, sigma_y_fitted, sigma_x_fitted = fitPSF(im_array, global_mean, x, y, config)

When I use the for loop to go through the elements in my 1D array of values (len = 4031) and reference it as x[i], I receive an error that says index 1 is out of bounds for axis 0 with size 1. I assume this means the code is interpreting the [i] index across columns rather than rows. How do I fix this so that way it reads down the elements of the array rather than across the non-existent columns?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Jimbles
  • 19
  • 5
  • 1
    `start_coords` is a 2 element list, so `i` in the last loop ranges over [0,1]. You have a `print(xy)` statement, but don't show that, so I don't have a clear idea of `xy` is like, or what the `hsplit` produces. Again, you `print(star_coords)` but don't show it. Why are you indexing `x` and `y` with that `i`? – hpaulj Oct 15 '21 at 16:38

1 Answers1

0

At this point:

x, y = np.hsplit(xy, 2)

x and y are each an array with however many rows you supplied in your data. But then in the first iteration of your for loop, you reassign x and y:

x = x[i]
y = y[i]

Now, x is only the first row of what x originally pointed to, and similarly for y. Thus, on the second iteration of the for loop, you try to access x[1], which doesn't exist, because x now has only one row.

Also, as hpaulj points out, you're iterating over the length of star_coords, not xy. star_coords only contains two elements.

A quick fix would be to check the length of xy (or x, or y) instead, and use different variable names for the rows within the loop:

for i in range(len(xy)):
    x1 = x[i]
    y2 = y[i]
    x2, y2, amplitude, intensity, sigma_y_fitted, sigma_x_fitted = fitPSF(
        im_array, global_mean, x1, y1, config
    )

Although it would be cleaner to avoid the range(len(...)) antipattern and use zip instead:

for x1, y1 in zip(x, y):
    x2, y2, amplitude, intensity, sigma_y_fitted, sigma_x_fitted = fitPSF(
        im_array, global_mean, x1, y1, config
    )

Or you could iterate over the xy array directly, but I'm unsure exactly what that would look like without a better idea of the structure of your array.

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
  • Ah I see, so then the issue is fixed by saying "x = star_coords[0][i]"and "y = star_coords[1][i]" – Jimbles Oct 15 '21 at 16:06
  • That would be one way to do it, but it's awkward to define `x` as all the rows, then redefine it to a single row. You're going to look at that code in six months and wonder what the heck is going on. – CrazyChucky Oct 15 '21 at 16:09
  • @Jimbles Please see my edit for suggestions on ways to solve it. It's worth noting that in Python, if you find yourself using a lot of indexes, there's quite possibly a cleaner way. – CrazyChucky Oct 15 '21 at 16:17