Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lightning/types/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from lightning.types.base import Base
from lightning.types.decorators import viztype
from lightning.types.utils import array_to_im
from lightning.types.utils import array_to_im, get_points_from_polygon


@viztype
Expand All @@ -27,12 +27,15 @@ def clean(imagedata):
outdict = [array_to_im(imagedata)]

return {'images': outdict}

@property
def coords(self):

def get_coords(self, as_points=False):
user_data = self.get_user_data()['settings']
if 'coords' in user_data.keys():
return user_data['coords']
if 'coords' in user_data.keys():
coords = user_data['coords']
if as_points:
return [get_points_from_polygon(x) for x in coords]
else:
return coords
else:
return []

Expand Down
29 changes: 26 additions & 3 deletions lightning/types/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from numpy import asarray, array, ndarray, vstack, newaxis, nonzero, concatenate, transpose, atleast_2d, size, isscalar

from numpy import asarray, array, ndarray, vstack, newaxis, nonzero, concatenate, transpose, atleast_2d, size, isscalar, meshgrid, where
from matplotlib.path import Path

def add_property(d, prop, name):

Expand Down Expand Up @@ -206,4 +206,27 @@ def list_to_regions(reg):
checkthree = all(map(lambda x: len(x) == 3, reg))
if not (checktwo or checkthree):
raise Exception("All region names must be two letters (for US) or three letters (for world)")
return reg
return reg

def get_points_from_polygon(coords):
"""
Given a list of pairs of points which define a polygon, return a list of points interior to the polygon
"""

bounds = array(coords).astype('int')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace array with asarray


bMax = bounds.max(0)
bMin = bounds.min(0)

path = Path(bounds)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break

grid = meshgrid(range(bMin[0],bMax[0]+1),range(bMin[1],bMax[1]+1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and elsewhere put spaces after commas within function argument lists


gridFlat = zip(grid[0].ravel(),grid[1].ravel())

inside = path.contains_points(gridFlat).reshape(grid[0].shape).astype('int')
inside = where(inside)
inside = vstack([inside[0],inside[1]]).T + bMin[-1::-1]
inside.tolist()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tolist() is not in place, make sure to call it on the object that will be returned.


return inside