C1_W3_Programming Assignment_IndexError

How can I test the code to see where the error might be? I might be misindexing somewhere or I could have an off-by-one error in there. This is the error message:

In the next cell I seem to get the correct output

This is my code:

### START CODE HERE (REPLACE INSTANCES OF 'None' with your code) ###

tries = 0

while tries < max_tries:
    # randomly sample sub-volume by sampling the corner voxel
    # hint: make sure to leave enough room for the output dimensions!
    # do not remove/delete the '0's
    start_x = np.random.randint(0, 79) # 240 max, 160 pixels)
    start_y = np.random.randint(0, 79) #240 is the max, 160 pixes
    start_z = np.random.randint(0, 138) #155 is the max, 16 pixels

    # extract relevant area of label
    y = label[start_x: start_x + output_x,
              start_y: start_y + output_y,
              start_z: start_z + output_z]
    
    # One-hot encode the categories.
    # This adds a 4th dimension, 'num_classes'
    # (output_x, output_y, output_z, num_classes)
    y = keras.utils.to_categorical(y, num_classes=4)

    # compute the background ratio (this has been implemented for you)
    bgrd_ratio = np.sum(y[:, :, :, 0])/(output_x * output_y * output_z)

    # increment tries counter
    tries += 1

    # if background ratio is below the desired threshold,
    # use that sub-volume.
    # otherwise continue the loop and try another random sub-volume
    if bgrd_ratio < background_threshold:

        # make copy of the sub-volume
        X = np.copy(image[start_x: start_x + output_x,
                          start_y: start_y + output_y,
                          start_z: start_z + output_z, :])
        
        # change dimension of X
        # from (x_dim, y_dim, z_dim, num_channels)
        # to (num_channels, x_dim, y_dim, z_dim)
        X = np.moveaxis(X, -1, 0) #

        # change dimension of y
        # from (x_dim, y_dim, z_dim, num_classes)
        # to (num_classes, x_dim, y_dim, z_dim)
        y = np.moveaxis(y, -1, 0)

        ### END CODE HERE ###
        
        # take a subset of y that excludes the background class
        # in the 'num_classes' dimension
        y = y[1:, :, :, :]

        return X, y

# if we've tried max_tries number of samples
# Give up in order to avoid looping forever.
print(f"Tried {tries} times to find a sub-volume. Giving up...")

Where should I look?

I found a “mislabled” previous entry. The issue was not really an off-by-one, but rather an off-by-two. I am not sure what I misunderstand but where I did -1 it should have been +1. with that, it works.