indexToValue() question

How do I...?
Post Reply
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

indexToValue() question

Post by joleenf »

Hi,

How does indexToValue() work for a linear1Dset.

If "a" is a navigatedImage and I used

ds=a.getDomainSet()

I would expect that

print ds.indexToValue([0])

would return indices of [0][0], but instead the output is

array([F, [array('f', [0.0]), array('f', [1.0])])


How is indexToValue supposed to work?

Thanks,
Joleen
User avatar
bobc
Posts: 990
Joined: Mon Nov 15, 2010 5:57 pm

Re: indexToValue() question

Post by bobc »

Hi Joleen -

Can you please run the following command and post the output?

Code: Select all

print ds

Thanks -
Bob Carp
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: indexToValue() question

Post by joleenf »

Hi Bob,

I did not have that example in my session any longer, so I ran the commands again. From the output below, I kind of see the reason behind the answer that I getting, but a person better at explaining the behavior would be very helpful information for me.

Thanks,
Joleen

Screen Shot 2015-09-21 at 9.49.17 AM.png
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: indexToValue() question

Post by joleenf »

Hi,

From what I can from the data in the image below, I think the explanation is

The single value index increases in a range from 0-N (in the example 0-20) from Left to Right, Top to Bottom

Linear2DSet: Length = 20
Dimension 1: Length = 4 Range = 0.0 to 3.0
Dimension 2: Length = 5 Range = 4.0 to 0.0

Dimension 1: is from 0-n (in the example 0-3) from left to right
Dimension 2: is from 0-n (in the example 4-0) from top left to bottom left (meaning a y index decreasing from top to bottom)

Screen Shot 2015-09-23 at 10.00.29 AM.png


Is this correct? What would happen in cases where there are multiple levels? Would I need to make a 2D grid before using indexToValue()?

I am just trying to perform a box average over multiple pixels in an image and make sure I know what I am averaging. I believe my first attempt was completely incorrect....

Code: Select all

def averageOverPixels(grid,boxsize=(10,10)):
   """
   newGrid=averageOverPixels(grid,boxsize=(x,y))

   Create a new grid which is the average value in a specified box
   for each pixel in original grid.
   """
   if not (grid.isFlatField()):
     grid = grid[0]
   else:
     grid = grid

   #get the grid size
   domainDimensions=getDomainDimension(grid)
   if (domainDimension >= 3):
      make2D(grid)
 
   [nx,ny]=getDomainSizes(grid)
   ds=grid.getDomainSet()
   
   outGrid=grid.clone()

   for x in xrange(0,nx,boxsize[0]):
       for y in xrange(0,ny,boxsize[1]):
           indices=[]
           for xx in xrange(x,x+boxsize[0]):
               for yy in xrange(y,y+boxsize[1]):
                   currentIndex= yy*nx + xx
                   print currentIndex, xx, yy, ds.indexToValue([currentIndex])
                   indices.append(currentIndex)
           thisAverage=computeAverage(grid,indices)
           outGrid=replace(outGrid,indices,thisAverage)

   return outGrid



I am sorry this is so elementary, but I am a bit stuck on how to index the correct locations in the 1D array.

Joleen
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: indexToValue() question

Post by joleenf »

Or maybe the formula I have is correct, and McIDAS-V handles the inversion of the x,y index internally?

for x in xrange(0,nx):
for y in xrange(0,ny):
gridx,gridy=ds.gridToValue([[x],[y]])
print gridx[0],gridy[0], x,y
User avatar
Rick
Posts: 404
Joined: Fri Jan 16, 2009 8:20 pm

Re: indexToValue() question

Post by Rick »

Joleen,

This is a code snipit from one of training sessions. Does it help you understand the index problem you might be having?

Rick

Code: Select all

        # This is the loop for reading/writing and displaying the data
        for line in range(lineSize):
            for element in range(elementSize):


                # Set a variable to point to the location in the array to
                # start reading data
                arrayOffset = line*elementSize+element


                # albedoObject is a one dimensional array containing a list
                # of all lines of data
                albedo = albedoArray[0][arrayOffset]
                temp24Diff = temp2sub4Array[0][arrayOffset]
                temp46Diff = temp4sub6Array[0][arrayOffset]
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: indexToValue() question

Post by joleenf »

Hi Rick,

It looks like we are performing the same calculation. I think everything should be okay. My question relates to the fact that the y indexing can be flipped depending on the data source, how will I know, and will it matter. I think I should be okay with the indexing as I have written my code because if indexing is flipped in the visad version of the result, it seems to be handled internally in the code...but I was not certain. At the same time, I was getting inconsistent results with the code posted above. It seemed to work for randomly created fields, for areas, but for one hdf grid that I used, it seemed to fail completely returning the same grid as was input rather than the average. This is why I turned to the indexing question. I can't figure out why that would happen.

Joleen
User avatar
tdrink
Posts: 157
Joined: Wed Apr 15, 2009 7:45 pm

Re: indexToValue() question

Post by tdrink »

Indexes refer to the position in the grid, the values or samples are the result
of the mapping from grid position to Rn (real number space) so an increasing
index could map to a decreasing location in space. This is determined when
the Set is created and is fixed.
Post Reply