path and bbox

Hi, wonder if anyone can help me with path and bbox. I have a set of ocean drifter tracks and I want to know if they pass through a particular boxed area. This is straightforward to do but I wanted to try to do it with matplotlib.transforms and matplotlib.path, which look well-suited to this kind of task using ‘intersects_bbox’.

The essence of my code is below. I have a single drifter track (lon, lat) and have defined a boxed area. At no time does the drifter approach or enter the box - however, the final line,

print track.intersects_bbox(bbox)

returns True, which it shouldn’t. In total I have 340 tracks. My code says 74 pass though the box, but by counting there should be only about 9. Any help appreciated, thanks, Evan

import numpy as N

import numpy.ma as MA
import matplotlib.transforms as BB
import matplotlib.path as PP

In [200]: lon
Out[200]:
masked_array(data = [-15.52 -15.521 -15.541 …, – -- --],

  mask = [False False False ...,  True  True  True],
  fill_value=1e+20)

In [201]: lat
Out[201]:
masked_array(data = [29.2 29.2 29.196 …, – -- --],
mask = [False False False …, True True True],

  fill_value=1e+20)

In [202]: len(lon), len(lat)
Out[202]: (3750, 3750)

track = MA.transpose([lon, lat])
track = PP.Path(track)
bbox = BB.Bbox.from_extents(-15.95, 29.6, -15.9, 29.65)

In [206]: print track.intersects_bbox(bbox)

1

Evan Mason wrote:

Hi, wonder if anyone can help me with path and bbox. I have a set of ocean drifter tracks and I want to know if they pass through a particular boxed area. This is straightforward to do but I wanted to try to do it with matplotlib.transforms and matplotlib.path, which look well-suited to this kind of task using 'intersects_bbox'.

The essence of my code is below. I have a single drifter track (lon, lat) and have defined a boxed area. At no time does the drifter approach or enter the box - however, the final line,

    print track.intersects_bbox(bbox)

returns True, which it shouldn't. In total I have 340 tracks. My code says 74 pass though the box, but by counting there should be only about 9. Any help appreciated, thanks, Evan

import numpy as N
import numpy.ma <http://numpy.ma> as MA
import matplotlib.transforms as BB
import matplotlib.path as PP

In [200]: lon
Out[200]:
masked_array(data = [-15.52 -15.521 -15.541 ..., -- -- --],
      mask = [False False False ..., True True True],
      fill_value=1e+20)

In [201]: lat
Out[201]:
masked_array(data = [29.2 29.2 29.196 ..., -- -- --],
      mask = [False False False ..., True True True],
      fill_value=1e+20)

In [202]: len(lon), len(lat)
Out[202]: (3750, 3750)

track = MA.transpose([lon, lat])
track = PP.Path(track)
bbox = BB.Bbox.from_extents(-15.95, 29.6, -15.9, 29.65)

In [206]: print track.intersects_bbox(bbox)
1

Evan,

Can you strip the problem down more? That is, find one track that is misdiagnosed, and then try to find the shortest segment of it that still gets misdiagnosed. You could try subsampling and/or chopping chunks off the ends. If you can get it down to a track with only a few points, so that you can email a very short but complete script with illustrative data, that would make it much easier for someone to figure out what is going on.

I suspect it is related to the masked values. If you strip the masked points out of your lon and lat arrays, do you get the same result?
Try making an example with only 3 points, say with the middle one masked, and then unmasked.

Eric

I just added a test of intersects_bbox to SVN that seems to be working correctly for short paths containing masked values. It would appear that masked values *should* be dealt with correctly (that is, in exactly the same way as they are drawn) by the intersection code. However, there is probably a side case here that I'm missing. As Eric suggests, see if you can break it down into a simple example that fails and hopefully the problem and/or solution will be more obvious from that.

Cheers,
Mike

Eric Firing wrote:

···

Evan Mason wrote:
  

Hi, wonder if anyone can help me with path and bbox. I have a set of ocean drifter tracks and I want to know if they pass through a particular boxed area. This is straightforward to do but I wanted to try to do it with matplotlib.transforms and matplotlib.path, which look well-suited to this kind of task using 'intersects_bbox'.

The essence of my code is below. I have a single drifter track (lon, lat) and have defined a boxed area. At no time does the drifter approach or enter the box - however, the final line,

    print track.intersects_bbox(bbox)

returns True, which it shouldn't. In total I have 340 tracks. My code says 74 pass though the box, but by counting there should be only about 9. Any help appreciated, thanks, Evan

import numpy as N
import numpy.ma <http://numpy.ma> as MA
import matplotlib.transforms as BB
import matplotlib.path as PP

In [200]: lon
Out[200]:
masked_array(data = [-15.52 -15.521 -15.541 ..., -- -- --],
      mask = [False False False ..., True True True],
      fill_value=1e+20)

In [201]: lat
Out[201]:
masked_array(data = [29.2 29.2 29.196 ..., -- -- --],
      mask = [False False False ..., True True True],
      fill_value=1e+20)

In [202]: len(lon), len(lat)
Out[202]: (3750, 3750)

track = MA.transpose([lon, lat])
track = PP.Path(track)
bbox = BB.Bbox.from_extents(-15.95, 29.6, -15.9, 29.65)

In [206]: print track.intersects_bbox(bbox)
1
    
Evan,

Can you strip the problem down more? That is, find one track that is misdiagnosed, and then try to find the shortest segment of it that still gets misdiagnosed. You could try subsampling and/or chopping chunks off the ends. If you can get it down to a track with only a few points, so that you can email a very short but complete script with illustrative data, that would make it much easier for someone to figure out what is going on.

I suspect it is related to the masked values. If you strip the masked points out of your lon and lat arrays, do you get the same result?
Try making an example with only 3 points, say with the middle one masked, and then unmasked.

Eric

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
  
--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Eric, I tried what you say about masked values, and that has helped. However there are still a few tracks that creep through. Below is a short script that shows what I mean. It loads in a matfile with the drifter track, which I will send to you separately.

%run path_example.py

import pylab as P
import numpy as N
from scipy import io
from matplotlib.transforms import Bbox as BB
import matplotlib.path as PP

P.close(‘all’)

Load drifter track (lon, lat)

lonlat = io.loadmat(‘lonlat’)
lon = N.squeeze(lonlat.values()[1])
lat = N.squeeze(lonlat.values()[0])

Set up box

box = BB.from_extents(-16.6, 28.05, -15.65, 28.15)
bx = [box.xmin, box.xmax, box.xmax, box.xmin, box.xmin]

by = [box.ymin, box.ymin, box.ymax, box.ymax, box.ymin]

track_all = N.transpose([lon, lat])
track_1 = N.transpose([lon[81:295], lat[81:295]])
track_2 = N.transpose([lon[81:290], lat[81:290]])

track_all = PP.Path(track_all)

track_1 = PP.Path(track_1)
track_2 = PP.Path(track_2)

Test if drifter passes thru box (it doesn’t so should be 0)

print track_all.intersects_bbox(box), ‘Should be 0’
print track_1.intersects_bbox(box), ‘Should be 0’

print track_2.intersects_bbox(box), ‘Should be 0’

P.figure()
P.plot(lon, lat, ‘r’, lw=0.3)
P.plot(lon[81:290], lat[81:290], ‘g’, lw=0.5)
P.plot(bx, by, ‘b’)

P.show()

···

On Tue, Sep 9, 2008 at 2:02 AM, Eric Firing <efiring@…202…> wrote:

Evan Mason wrote:

Hi, wonder if anyone can help me with path and bbox. I have a set of ocean drifter tracks and I want to know if they pass through a particular boxed area. This is straightforward to do but I wanted to try to do it with matplotlib.transforms and matplotlib.path, which look well-suited to this kind of task using ‘intersects_bbox’.

The essence of my code is below. I have a single drifter track (lon, lat) and have defined a boxed area. At no time does the drifter approach or enter the box - however, the final line,

print track.intersects_bbox(bbox)

returns True, which it shouldn’t. In total I have 340 tracks. My code says 74 pass though the box, but by counting there should be only about 9. Any help appreciated, thanks, Evan

import numpy as N

import numpy.ma <http://numpy.ma> as MA

import matplotlib.transforms as BB

import matplotlib.path as PP

In [200]: lon

Out[200]:

masked_array(data = [-15.52 -15.521 -15.541 …, – -- --],

  mask = [False False False ...,  True  True  True],

  fill_value=1e+20)

In [201]: lat

Out[201]:

masked_array(data = [29.2 29.2 29.196 …, – -- --],

  mask = [False False False ...,  True  True  True],

  fill_value=1e+20)

In [202]: len(lon), len(lat)

Out[202]: (3750, 3750)

track = MA.transpose([lon, lat])

track = PP.Path(track)

bbox = BB.Bbox.from_extents(-15.95, 29.6, -15.9, 29.65)

In [206]: print track.intersects_bbox(bbox)

1

Evan,

Can you strip the problem down more? That is, find one track that is misdiagnosed, and then try to find the shortest segment of it that still gets misdiagnosed. You could try subsampling and/or chopping chunks off the ends. If you can get it down to a track with only a few points, so that you can email a very short but complete script with illustrative data, that would make it much easier for someone to figure out what is going on.

I suspect it is related to the masked values. If you strip the masked points out of your lon and lat arrays, do you get the same result?

Try making an example with only 3 points, say with the middle one masked, and then unmasked.

Eric

Michael Droettboom wrote:

I just added a test of intersects_bbox to SVN that seems to be working correctly for short paths containing masked values. It would appear that masked values *should* be dealt with correctly (that is, in exactly the same way as they are drawn) by the intersection code. However, there is probably a side case here that I'm missing. As Eric suggests, see if you can break it down into a simple example that fails and hopefully the problem and/or solution will be more obvious from that.

Mike,

I was wrong; it has nothing to do with masked values. It is much simpler: it appears that for the purpose of this test, the path is being treated as if it were closed. See attached. I think this accounts for Evan's mis-identified tracks.

Eric

bbox_intersect2.py (493 Bytes)

Ah, yes. The code was written with the assumption that the paths were treated as filled (which doesn't really matter whether they are closed or not). I have added a "filled" kwarg to Path.intersects_path and Path.intersects_bbox that when True treats the path(s) as filled, and when False does not. In order to not change existing functionality, this argument defaults to "True". To resolve Evan's problem, one would now explicitly pass "filled=False" to intersects_bbox.

Cheers,
Mike

Eric Firing wrote:

···

Michael Droettboom wrote:

I just added a test of intersects_bbox to SVN that seems to be working correctly for short paths containing masked values. It would appear that masked values *should* be dealt with correctly (that is, in exactly the same way as they are drawn) by the intersection code. However, there is probably a side case here that I'm missing. As Eric suggests, see if you can break it down into a simple example that fails and hopefully the problem and/or solution will be more obvious from that.

Mike,

I was wrong; it has nothing to do with masked values. It is much simpler: it appears that for the purpose of this test, the path is being treated as if it were closed. See attached. I think this accounts for Evan's mis-identified tracks.

Eric

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA