Skip to content

Commit

Permalink
Merge 503a9bb into f4b1cf5
Browse files Browse the repository at this point in the history
  • Loading branch information
phobson committed Jun 30, 2016
2 parents f4b1cf5 + 503a9bb commit 91ed7c3
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 143 deletions.
52 changes: 26 additions & 26 deletions pygridgen/boundary_interactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from copy import deepcopy

import numpy as np
import matplotlib.pyplot as plt
import numpy
from matplotlib import pyplot
from matplotlib.artist import Artist
from matplotlib.patches import Polygon, CirclePolygon
from matplotlib.lines import Line2D
Expand Down Expand Up @@ -122,8 +122,8 @@ def _get_ind_under_point(self, event):

# display coords
xt, yt = self._poly.get_transform().numerix_x_y(x, y)
d = np.sqrt((xt-event.x)**2 + (yt-event.y)**2)
indseq = np.nonzero(np.equal(d, np.amin(d)))
d = numpy.sqrt((xt-event.x)**2 + (yt-event.y)**2)
indseq = numpy.nonzero(numpy.equal(d, numpy.amin(d)))
ind = indseq[0]

if d[ind]>=self._epsilon:
Expand All @@ -132,11 +132,11 @@ def _get_ind_under_point(self, event):
return ind
except:
# display coords
xy = np.asarray(self._poly.xy)
xy = numpy.asarray(self._poly.xy)
xyt = self._poly.get_transform().transform(xy)
xt, yt = xyt[:, 0], xyt[:, 1]
d = np.sqrt((xt-event.x)**2 + (yt-event.y)**2)
indseq = np.nonzero(np.equal(d, np.amin(d)))[0]
d = numpy.sqrt((xt-event.x)**2 + (yt-event.y)**2)
indseq = numpy.nonzero(numpy.equal(d, numpy.amin(d)))[0]
ind = indseq[0]

if d[ind]>=self._epsilon:
Expand Down Expand Up @@ -201,7 +201,7 @@ def _key_press_callback(self, event):
s1 = xys[i+1]
d = dist_point_to_segment(p, s0, s1)
if d<=self._epsilon:
self._poly.xy = np.array(
self._poly.xy = numpy.array(
list(self._poly.xy[:i+1]) +
[(event.xdata, event.ydata)] +
list(self._poly.xy[i+1:]))
Expand All @@ -212,7 +212,7 @@ def _key_press_callback(self, event):
s1 = xys[0]
d = dist_point_to_segment(p, s0, s1)
if d<=self._epsilon:
self._poly.xy = np.array(
self._poly.xy = numpy.array(
list(self._poly.xy) +
[(event.xdata, event.ydata)])
self._line.set_data(zip(*self._poly.xy))
Expand Down Expand Up @@ -280,7 +280,7 @@ def __init__(self, x, y=None, beta=None, ax=None, proj=None,
**gridgen_options):

if isinstance(x, str):
bry_dict = np.load(x)
bry_dict = numpy.load(x)
x = bry_dict['x']
y = bry_dict['y']
beta = bry_dict['beta']
Expand All @@ -289,7 +289,7 @@ def __init__(self, x, y=None, beta=None, ax=None, proj=None,
raise ValueError('Boundary must have at least four points.')

if ax is None:
ax = plt.gca()
ax = pyplot.gca()

self._ax = ax

Expand Down Expand Up @@ -357,7 +357,7 @@ def save_bry(self, bry_file='bry.pickle'):
f.close()

def load_bry(self, bry_file='bry.pickle'):
bry_dict = np.load(bry_file)
bry_dict = numpy.load(bry_file)
x = bry_dict['x']
y = bry_dict['y']
self._line.set_data(x, y)
Expand Down Expand Up @@ -386,23 +386,23 @@ class edit_mask_mesh(object): # pragma: no cover
def _on_key(self, event):
if event.key == 'e':
self._clicking = not self._clicking
plt.title('Editing %s -- click "e" to toggle' % self._clicking)
plt.draw()
pyplot.title('Editing %s -- click "e" to toggle' % self._clicking)
pyplot.draw()

def _on_click(self, event):
x, y = event.xdata, event.ydata
if event.button==1 and event.inaxes is not None and self._clicking == True:
d = (x-self._xc)**2 + (y-self._yc)**2
if isinstance(self.xv, np.ma.MaskedArray):
idx = np.argwhere(d[~self._xc.mask] == d.min())
if isinstance(self.xv, numpy.ma.MaskedArray):
idx = numpy.argwhere(d[~self._xc.mask] == d.min())
else:
idx = np.argwhere(d.flatten() == d.min())
idx = numpy.argwhere(d.flatten() == d.min())
self._mask[idx] = float(not self._mask[idx])
i, j = np.argwhere(d == d.min())[0]
i, j = numpy.argwhere(d == d.min())[0]
self.mask[i, j] = float(not self.mask[i, j])
self._pc.set_array(self._mask)
self._pc.changed()
plt.draw()
pyplot.draw()

def __init__(self, xv, yv, mask, **kwargs):
if xv.shape != yv.shape:
Expand All @@ -420,19 +420,19 @@ def __init__(self, xv, yv, mask, **kwargs):
land_color = kwargs.pop('land_color', (0.6, 1.0, 0.6))
sea_color = kwargs.pop('sea_color', (0.6, 0.6, 1.0))

cm = plt.matplotlib.colors.ListedColormap([land_color, sea_color],
cm = pyplot.matplotlib.colors.ListedColormap([land_color, sea_color],
name='land/sea')
self._pc = plt.pcolor(xv, yv, mask, cmap=cm, vmin=0, vmax=1, **kwargs)
self._pc = pyplot.pcolor(xv, yv, mask, cmap=cm, vmin=0, vmax=1, **kwargs)
self._xc = 0.25*(xv[1:,1:]+xv[1:,:-1]+xv[:-1,1:]+xv[:-1,:-1])
self._yc = 0.25*(yv[1:,1:]+yv[1:,:-1]+yv[:-1,1:]+yv[:-1,:-1])

if isinstance(self.xv, np.ma.MaskedArray):
if isinstance(self.xv, numpy.ma.MaskedArray):
self._mask = mask[~self._xc.mask]
else:
self._mask = mask.flatten()

plt.connect('button_press_event', self._on_click)
plt.connect('key_press_event', self._on_key)
pyplot.connect('button_press_event', self._on_click)
pyplot.connect('key_press_event', self._on_key)
self._clicking = False
plt.title('Editing %s -- click "e" to toggle' % self._clicking)
plt.draw()
pyplot.title('Editing %s -- click "e" to toggle' % self._clicking)
pyplot.draw()
58 changes: 29 additions & 29 deletions pygridgen/csa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import ctypes
from pkg_resources import resource_filename

import numpy as np
import matplotlib.pyplot as plt
import numpy
from matplotlib import pyplot


class csa(object):
Expand Down Expand Up @@ -46,30 +46,30 @@ class csa(object):
Examples
--------
>>> import csa
>>> xin = np.random.randn(10000)
>>> yin = np.random.randn(10000)
>>> zin = np.sin( xin**2 + yin**2 ) / (xin**2 + yin**2 )
>>> xout, yout = np.mgrid[-3:3:10j, -3:3:10j]
>>> xin = numpy.random.randn(10000)
>>> yin = numpy.random.randn(10000)
>>> zin = numpy.sin( xin**2 + yin**2 ) / (xin**2 + yin**2 )
>>> xout, yout = numpy.mgrid[-3:3:10j, -3:3:10j]
>>> csa_interp = csa.CSA(xin, yin, zin)
>>> zout = csa_interp(xout, yout)
>>> csa_interp.zin = np.cos( xin + yin**2 )
>>> csa_interp.zin = numpy.cos( xin + yin**2 )
>>> zout = csa_interp
>>> print(zout)
"""

try:
path = os.path.dirname(resource_filename('pygridgen', '_csa.so'))
_csa = np.ctypeslib.load_library('_csa', path)
_csa = numpy.ctypeslib.load_library('_csa', path)
except OSError:
path = os.path.join(site.getsitepackages()[0], 'pygridgen')
_csa = np.ctypeslib.load_library('_csa', path)
_csa = numpy.ctypeslib.load_library('_csa', path)

_csa.csa_approximatepoints2.restype = ctypes.POINTER(ctypes.c_double)

def __init__(self, xin, yin, zin, sigma=None, npmin=3, npmax=40, k=140, nppc=5):
self.xin = np.asarray(xin)
self.yin = np.asarray(yin)
self.xin = numpy.asarray(xin)
self.yin = numpy.asarray(yin)

if xin.size != yin.size:
raise ValueError('xin and yin must have the same number '
Expand All @@ -90,24 +90,24 @@ def zin(self):
return self._zin
@zin.setter
def zin(self, value):
zin = np.asarray(value)
zin = numpy.asarray(value)
if zin.size != self.xin.size:
raise ValueError('zin must have the same number of elements as '
'xin and yin')
self._zin = value

def _calculate_points(self, xout, yout):

xout = np.asarray(xout)
yout = np.asarray(yout)
xout = numpy.asarray(xout)
yout = numpy.asarray(yout)

nin = self.xin.size
nout = xout.size

if self.sigma is None:
sigma = ctypes.POINTER(ctypes.c_double)()
else:
sigma = (ctypes.c_double * nin)(*(self.sigma * np.ones_like(self.xin)))
sigma = (ctypes.c_double * nin)(*(self.sigma * numpy.ones_like(self.xin)))

zout = None

Expand All @@ -126,9 +126,9 @@ def _calculate_points(self, xout, yout):
ctypes.c_int(self.nppc) #int nppc
)

zout = np.asarray([zout[i] for i in range(nout)])
zout = numpy.asarray([zout[i] for i in range(nout)])
zout.shape = xout.shape
return np.ma.masked_where(np.isnan(zout), zout)
return numpy.ma.masked_where(numpy.isnan(zout), zout)

def __call__(self, xout, yout):
"""
Expand All @@ -147,8 +147,8 @@ def __call__(self, xout, yout):
"""

xout = np.asarray(xout)
yout = np.asarray(yout)
xout = numpy.asarray(xout)
yout = numpy.asarray(yout)
return self._calculate_points(xout, yout)

def plot(self, xout, yout, ax=None, mesh_opts=None, scatter_opts=None):
Expand All @@ -175,7 +175,7 @@ def plot(self, xout, yout, ax=None, mesh_opts=None, scatter_opts=None):
"""

if ax is None:
fig, ax = plt.subplots()
fig, ax = pyplot.subplots()
else:
fig = ax.figure

Expand All @@ -185,7 +185,7 @@ def plot(self, xout, yout, ax=None, mesh_opts=None, scatter_opts=None):
if scatter_opts is None:
scatter_opts = {}

fig, ax = plt.subplots()
fig, ax = pyplot.subplots()
zout = self._calculate_points(xout, yout)
ax.pcolormesh(xout, yout, zout, **mesh_opts)
ax.scatter(self.xin, self.yin, 10, self.zin, **scatter_opts)
Expand All @@ -194,23 +194,23 @@ def plot(self, xout, yout, ax=None, mesh_opts=None, scatter_opts=None):


if __name__ == '__main__':
xin = np.random.randn(10000)
yin = np.random.randn(10000)
zin = np.sin( xin**2 + yin**2 ) / (xin**2 + yin**2 )
sigma = 0.01 * np.ones_like(xin)
xin = numpy.random.randn(10000)
yin = numpy.random.randn(10000)
zin = numpy.sin( xin**2 + yin**2 ) / (xin**2 + yin**2 )
sigma = 0.01 * numpy.ones_like(xin)

print(' ### Set up input data points')

xout, yout = np.mgrid[-3:3:100j, -3:3:100j]
xout, yout = numpy.mgrid[-3:3:100j, -3:3:100j]

csa_interp = CSA(xin, yin, zin)
fig, (ax1, ax2) = plt.subplots(ncols=2)
fig, (ax1, ax2) = pyplot.subplots(ncols=2)
csa_interp.plot(xout, yout, ax=ax1, mesh_opts=dict(vmin=-1, vmax=1),
scatter_opts=dict(vmin=-1, vmax=1, edgecolors='none'))

csa_interp.zin = np.cos( xin + yin**2 )
csa_interp.zin = numpy.cos( xin + yin**2 )
csa_interp.plot(xout, yout, ax=ax2, mesh_opts=dict(vmin=-1, vmax=1),
scatter_opts=dict(vmin=-1, vmax=1, edgecolors='none'))

plt.show()
pyplot.show()

0 comments on commit 91ed7c3

Please sign in to comment.