average over time

Post any questions, ideas, or topics related to Jython and Python scripting.
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

average over time

Post by hproe »

Hi -

I am trying to calculate an average over an image sequence and get the following error:
TypeError: averageOverTime(): 1st agrument can't be coerced to visad.FieldImpl

The code goes like:

Code: Select all

imgSeq=[]
<<getting images by stepping through time>>
metaSingChan,img=getADDEImage(...)
imgSeq.append(img)
<<all time steps done>>
imgAverage=averageOverTime(imgSeq)

I might add that imgSeq displays well in 'Image Sequence Display'

cheers, HP
User avatar
mhiley
Posts: 90
Joined: Mon Jan 23, 2012 10:22 pm

Re: average over time

Post by mhiley »

Hi HP!

Unfortunately we haven't fully worked out the interoperability of formulas like "averageOverTime" and the way we are doing things in the scripting API (appending to Python lists). For the time being, I suspect something like this might do the trick:

Code: Select all

# (at top of script):
from visad.meteorology import ImageSequenceImpl

# ( ... your script ... )

# running imgSeq through the ImageSequenceImpl "constructor" should
# turn it into something that averageOverTime can work with:
imgAverage = averageOverTime( ImageSequenceImpl(imgSeq) )


Let me know if that helps!
Mike
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: average over time

Post by tomw »

To follow on to what Mike said: Most, if not all, the library functions deal with data objects that are obtained using other library routines -- and these are usually of the type visad.FieldImpl or visad.FlatField (or extensions thereof).

When you create a Python list ("array") of objects and want to work with them, you might need to roll-your-own. In this case, perhaps something like:

Code: Select all

...your code...
imgAvg = imgSeq[0].clone;
for i in xrange(1,len(imgSeq)):
  imgAvg = imgAvg + imgSeq[i];
imgAvg = imgAvg / len(imgSeq);
 


But as Mike pointed out, for a time sequence of images, there is a "helper" method to take the "list" and create a suitable FieldImpl object that can then be used with other methods/functions.
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

Re: average over time

Post by hproe »

Mike and Tom -

In both cases I get the following error:
visad.UnitException: Set: units dimension 1 does not match Domain dimension 2

Note that this is the full error message in the shell - no line numbners given.

Funny enough I now get the same message also for my original code.

HP
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: average over time

Post by tomw »

HP -- Something in one (or more) of your image domains is not compatible with the other(s). I suggest that, as you read the image files, you print out the domain set....something like:

Code: Select all

print getDomainSet(imgSeq[i]);


or more completely, look at the complete structure:

Code: Select all

print whatTypes(imgSeq[i]);


and see what might be different...
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

Re: average over time

Post by hproe »

Tom and Mike -

Please disregard my former email - I have made a mix-up between 2 code version. Running now the intended code with your proposals implented, I get the following error messages:

MIKE:
java.lang.NullPointerException

TOM:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 54, in average_FD
TypeError: unsupported operand type(s) for /: 'instancemethod' and 'int'
Line 51 refers to the line ' imgAvg = imgAvg/len(imgSeq);'

HP
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: average over time

Post by tomw »

Sorry, HP. the "clone" should be "clone()" -- that's why it said "instancemethod" in the error message... So the line should read:

Code: Select all

imgAvg = imgSeq[0].clone();
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

Re: average over time

Post by hproe »

Tom, now I get the NullPointerException, like Mike's proposal.
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: average over time

Post by tomw »

Is one of the imgSeq items null? How about printing out the domain or the whatTypes() as you loop through? Also, my example will not work at all if the first item in imgSeq is null...
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

Re: average over time

Post by hproe »

Tom and Mike -

The good news is that both your methods appear to work. When I use Tom's method and print out whatTypes() I get the following analysis (average over 2x2 pixels):
imgSeq(0)
FunctionType:
Domain has 2 components:
0. RealType: ImageElement
0. Name = ImageElement
1. RealType: ImageLine
1. Name = ImageLine
Range:
RealTupleType has 1 components:
0. RealType: Band9_TEMP
0. Name = Band9_TEMP
0. Unit: K
FlatField of length = 8
((ImageElement, ImageLine) -> Band9_TEMP)
Domain has 2 components:
Linear2DSet: Length = 8
0. Linear1DSet (ImageElement) Range = 0.0 to 3.0 step 1.0
1. Linear1DSet (ImageLine) Range = 1.0 to 0.0 step -1.0
CoordinateSystem: (ImageElement, ImageLine) ==> (Latitude, Longitude)
Range has 1 components:
0. FloatSet (Band9_TEMP) Dimension = 1
0. number missing = 0

imgSeq(1)
FunctionType:
Domain has 2 components:
0. RealType: ImageElement
0. Name = ImageElement
1. RealType: ImageLine
1. Name = ImageLine
Range:
RealTupleType has 1 components:
0. RealType: Band9_TEMP
0. Name = Band9_TEMP
0. Unit: K
FlatField of length = 8
((ImageElement, ImageLine) -> Band9_TEMP)
Domain has 2 components:
Linear2DSet: Length = 8
0. Linear1DSet (ImageElement) Range = 0.0 to 3.0 step 1.0
1. Linear1DSet (ImageLine) Range = 1.0 to 0.0 step -1.0
CoordinateSystem: (ImageElement, ImageLine) ==> (Latitude, Longitude)
Range has 1 components:
0. FloatSet (Band9_TEMP) Dimension = 1
0. number missing = 0

imgAvg
FunctionType:
Domain has 2 components:
0. RealType: ImageElement
0. Name = ImageElement
1. RealType: ImageLine
1. Name = ImageLine
Range:
RealTupleType has 1 components:
0. RealType: Band9_TEMP
0. Name = Band9_TEMP
0. Unit: K
FlatField of length = 8
((ImageElement, ImageLine) -> Band9_TEMP)
Domain has 2 components:
Linear2DSet: Length = 8
0. Linear1DSet (ImageElement) Range = 0.0 to 3.0 step 1.0
1. Linear1DSet (ImageLine) Range = 1.0 to 0.0 step -1.0
CoordinateSystem: (ImageElement, ImageLine) ==> (Latitude, Longitude)
Range has 1 components:
0. FloatSet (Band9_TEMP) Dimension = 1
0. number missing = 0

However, I get a NullPointerException when I want to display imgAvg like

Code: Select all

imgDisp=firstDisplay()
imgLayer=imgDisp.createLayer('Image Display',imgAvg)

Sorry again for my rather confusing approach earlier on.

HP
Post Reply