Python流程算法

huangapple go评论49阅读模式
英文:

Python streamline algorithm

问题

目标:

我有两个数组 vxvy,分别表示速度分量。我想编写一个流线算法:

  1. 输入一个点的坐标 (seed)
  2. 根据其速度分量评估哪些像素位于输入点的路径上
  3. 返回seed点路径上点的索引

问题/疑问:

我最初编写了一个前向欧拉算法,但它对我的问题解决得非常差。我被建议将我的问题视为一个常微分方程(ODE),其中 dx/dt = v_x(t) 和 dy/dt = v_y(t)。我可以进行速度的插值,但在使用Scipy解ODE时遇到了困难。我应该怎么做?

自制算法:

我有两个数组 vxvy,表示速度分量。当一个数组中有NaN值时,另一个数组中也有NaN值。我从一个起始点seed开始,想要跟踪这个点根据速度分量经过的单元格。我对速度分量进行插值,以便将它们输入ODE求解器。

示例:

这段代码测试了一个10x11速度数组的算法。我在ODE求解器这一步卡住了。

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import RegularGridInterpolator
from scipy.integrate import odeint


# 创建坐标
x = np.linspace(0, 10, 100)
y = np.linspace(11, 20, 90)
Y, X = np.meshgrid(x, y)

# 创建速度场
vx = -1 - X**2 + Y
vy = 1 + X - Y**2

# 起始点
J = 5
I = 14

# 插值速度场
interpvx = RegularGridInterpolator((y, x), vx)
interpvy = RegularGridInterpolator((y, x), vy)

# 解ODE以获取点的路径,但我不知道该在参数t中放什么
# solx = odeint(interpvx, interpvx((I, J)), np.linspace(0, 1, 501))
# soly = odeint(interpvy, interpvx((I, J)), np.linspace(0, 1, 501))
英文:

Goal:

I have 2 arrays vx and vy representing velocity components. I want to write a streamline algorithm:

  1. Input the coordinates of a point (seed)
  2. Evaluate which pixels are on the path of the input point based on its velocity components
  3. Return the indices of the points in the path of the seed point

Issue/Question:

I initially wrote a Euler-forward algorithm that was solving very poorly my problem. I was advised to consider my problem as an Ordinary Differential Equation (ODE) where dx/dt = v_x(t) and dy/dt = v_y(t).
I can interpolate my velocities but struggle with solving the ODE with Scipy. How could I do that ?

Homemade algorithm:

I have 2 arrays vx and vy representing velocity components. When one has a NaN, the other has one too. I have a point from which I start, the seed point. I want to track which cells this point went through based on the velocity components.
I interpolate the velocity components vx and vy in order to input them in an ODE solver.

Example:

This code tests the algorithm for a 10x11 velocities array. I am blocked at the ODE solver.

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import RegularGridInterpolator
from scipy.integrate import odeint


# Create coordinates
x = np.linspace(0, 10, 100)
y = np.linspace(11, 20, 90)
Y, X = np.meshgrid(x, y)

# Create velocity fields
vx = -1 - X**2 + Y
vy = 1 + X - Y**2


# Seed point
J = 5
I = 14

# Interpolate the velocity fields
interpvx = RegularGridInterpolator((y,x), vx)
interpvy = RegularGridInterpolator((y,x), vy)

# Solve the ODE to get the point's path, but I don't know what to put for the parameter t
#solx = odeint(interpvx, interpvx((I,J)), np.linspace(0,1,501))
#soly = odeint(interpvy, interpvx((I,J)), np.linspace(0,1,501))

答案1

得分: 0

以下是代码的翻译部分:

我与某人合作他提出了一个解决方案我并不是要为这个答案而取得荣誉但考虑到 Python 没有一个直接的算法希望这对某人有用

import numpy as np
# 创建坐标
x = np.linspace(0, 1000, 100)
y = np.linspace(0, 1000, 90)
Y, X = np.meshgrid(x, y)

# 创建速度场
vx = -1 - X**2 + Y
vy = 1 + X - Y**2

# 种子点
J = 5
I = 14

# 插值速度
from scipy.interpolate import RegularGridInterpolator
X, Y = np.meshgrid(x, y)
fx = RegularGridInterpolator((y, x), vx, bounds_error=False, fill_value=None)
fy = RegularGridInterpolator((y, x), vy, bounds_error=False, fill_value=None)

# 定义要积分的速度函数:
def f(t, y):
    return np.squeeze([fy(y), fx(y)])

# 解决一个种子点
from scipy.integrate import solve_ivp
sol = solve_ivp(f, [0, 100], [J, I], t_eval=np.arange(0, 100, 1))

请注意,这只是代码的翻译部分,不包括问题部分。

英文:

Someone I work with came with a solution, I am not taking credit for the answer. But considering there is no streamline algorithm for Python, hopefully that will be useful to someone here:

import numpy as np
# Create coordinates
x = np.linspace(0, 1000, 100)
y = np.linspace(0, 1000, 90)
Y, X = np.meshgrid(x, y)

# Create velocity fields
vx = -1 - X**2 + Y
vy = 1 + X - Y**2


# Seed point
J = 5
I = 14

# Interpolate the velocities
from scipy.interpolate import RegularGridInterpolator
X, Y = np.meshgrid(x,y)
fx = RegularGridInterpolator((y, x), vx, bounds_error=False, fill_value=None)
fy = RegularGridInterpolator((y, x), vy, bounds_error=False, fill_value=None)

# define the velocity function to be integrated:
def f(t, y):
    return np.squeeze([fy(y), fx(y)])

# Solve for a seed point
from scipy.integrate import solve_ivp
sol = solve_ivp(f, [0, 100], [J,I], t_eval=np.arange(0,100,1))

答案2

得分: 0

matplotlib.pyplot.streamplot的代码可以在GitHub上查看。我需要类似的功能,但需要访问构成流线的点,而不仅仅是绘图。我稍微修改了matplotlib.pyplot.streamplot使用的代码,删除了与绘图相关的语句,并将返回的变量设置为流线中的点序列。如在matplotlib代码中的原始注释中所提到的,streamline方法使用二阶Runge-Kutta算法和自适应步长来生成流线。修改后的代码如下,我将函数重命名为streamplot2。我没有删除我不使用的参数,但当然可以删除以使解决方案更清晰。

可以按以下方式调用该函数:

streams = streamplot2(X, Y, dx, dy, broken_streamlines=False, integration_direction="forward", start_points=start_points_coords)

其中start_points_coords是一个形状为Nx2的数组,N等于起始点的数量(可以为1条流线)。

该函数返回一个流线列表,其中每条流线是一个形状为Mx2的数组,其中M是流线中的点数。

然后可以按以下方式绘制流线(没有额外的装饰):

for j in np.arange(len(streams)):
    plt.scatter(streams[j][:, 0], streams[j][:, 1], s=1)

以下是完整的修改后的代码:

#(代码已截断,只包含上面提到的相关部分)

这段代码提供了一个修改后的streamplot函数,允许你生成流线并访问流线中的点,以及如何调用和绘制流线。如果需要完整的代码,请从GitHub或相关资源中获取。

英文:

The code of matplotlib.pyplot.streamplot can be checked on GitHub. I needed a similar functionality but needed access to the points that made up the streamlines, not just the plot. I slightly modified the code used by matplotlib.pyplot.streamplot by removing the statements related to plotting and setting the variable returned to the sequence of points in the streamlines. As mentioned in the original comments in the matplotlib code, the streamline method uses the second order Runge-Kutta algorithm with adaptive step size to generate the streamlines. The modified code is below, I have renamed the function streamplot2. I didn't remove the parameters I don't use, but of course it can be done to make the solution cleaner.

You can call the function as follows:

streams = streamplot2(X, Y, dx, dy, broken_streamlines=False, integration_direction = "forward", start_points = start_points_coords)

where start_points_coords is the array with shape Nx2, with N equal to the number of starting points (it can be 1 for 1 streamline).

The function returns a list of streamline where each streamline is an array with shape Mx2, where M is the number of points in the streamline.

You can then plot the streamlines in the following way (no frills)

for j in np.arange(len(streams)):
   plt.scatter(streams[j][:,0],streams[j][:,1],s = 1)

Full modified code below:

"""
Streamlines for 2D vector fields.
"""
import numpy as np
import matplotlib as mpl
from matplotlib import _api, cm, patches
import matplotlib.colors as mcolors
import matplotlib.collections as mcollections
import matplotlib.lines as mlines
__all__ = ['streamplot']
def streamplot2(x, y, u, v, density=1, linewidth=None, color=None,
cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
minlength=0.1, transform=None, zorder=None, start_points=None,
maxlength=4.0, integration_direction='both',
broken_streamlines=True):
"""
Draw streamlines of a vector flow.
Parameters
----------
x, y : 1D/2D arrays
Evenly spaced strictly increasing arrays to make a grid.  If 2D, all
rows of *x* must be equal and all columns of *y* must be equal; i.e.,
they must be as if generated by ``np.meshgrid(x_1d, y_1d)``.
u, v : 2D arrays
*x* and *y*-velocities. The number of rows and columns must match
the length of *y* and *x*, respectively.
density : float or (float, float)
Controls the closeness of streamlines. When ``density = 1``, the domain
is divided into a 30x30 grid. *density* linearly scales this grid.
Each cell in the grid can have, at most, one traversing streamline.
For different densities in each direction, use a tuple
(density_x, density_y).
linewidth : float or 2D array
The width of the streamlines. With a 2D array the line width can be
varied across the grid. The array must have the same shape as *u*
and *v*.
color : color or 2D array
The streamline color. If given an array, its values are converted to
colors using *cmap* and *norm*.  The array must have the same shape
as *u* and *v*.
cmap, norm
Data normalization and colormapping parameters for *color*; only used
if *color* is an array of floats. See `~.Axes.imshow` for a detailed
description.
arrowsize : float
Scaling factor for the arrow size.
arrowstyle : str
Arrow style specification.
See `~matplotlib.patches.FancyArrowPatch`.
minlength : float
Minimum length of streamline in axes coordinates.
start_points : (N, 2) array
Coordinates of starting points for the streamlines in data coordinates
(the same coordinates as the *x* and *y* arrays).
zorder : float
The zorder of the streamlines and arrows.
Artists with lower zorder values are drawn first.
maxlength : float
Maximum length of streamline in axes coordinates.
integration_direction : {'forward', 'backward', 'both'}, default: 'both'
Integrate the streamline in forward, backward or both directions.
data : indexable object, optional
DATA_PARAMETER_PLACEHOLDER
broken_streamlines : boolean, default: True
If False, forces streamlines to continue until they
leave the plot domain.  If True, they may be terminated if they
come too close to another streamline.
Returns
-------
StreamplotSet
Container object with attributes
- ``lines``: `.LineCollection` of streamlines
- ``arrows``: `.PatchCollection` containing `.FancyArrowPatch`
objects representing the arrows half-way along streamlines.
This container will probably change in the future to allow changes
to the colormap, alpha, etc. for both lines and arrows, but these
changes should be backward compatible.
"""
grid = Grid(x, y)
mask = StreamMask(density)
dmap = DomainMap(grid, mask)
u = np.ma.masked_invalid(u)
v = np.ma.masked_invalid(v)
integrate = _get_integrator(u, v, dmap, minlength, maxlength,
integration_direction)
trajectories = []
if start_points is None:
for xm, ym in _gen_starting_points(mask.shape):
if mask[ym, xm] == 0:
xg, yg = dmap.mask2grid(xm, ym)
t = integrate(xg, yg, broken_streamlines)
if t is not None:
trajectories.append(t)
else:
sp2 = np.asanyarray(start_points, dtype=float).copy()
# Check if start_points are outside the data boundaries
for xs, ys in sp2:
if not (grid.x_origin <= xs <= grid.x_origin + grid.width and
grid.y_origin <= ys <= grid.y_origin + grid.height):
raise ValueError(f"Starting point ({xs}, {ys}) outside of "
"data boundaries")
# Convert start_points from data to array coords
# Shift the seed points from the bottom left of the data so that
# data2grid works properly.
sp2[:, 0] -= grid.x_origin
sp2[:, 1] -= grid.y_origin
for xs, ys in sp2:
xg, yg = dmap.data2grid(xs, ys)
# Floating point issues can cause xg, yg to be slightly out of
# bounds for xs, ys on the upper boundaries. Because we have
# already checked that the starting points are within the original
# grid, clip the xg, yg to the grid to work around this issue
xg = np.clip(xg, 0, grid.nx - 1)
yg = np.clip(yg, 0, grid.ny - 1)
t = integrate(xg, yg, broken_streamlines)
if t is not None:
trajectories.append(t)
streamlines = []
for t in trajectories:
tgx, tgy = t.T
# Rescale from grid-coordinates to data-coordinates.
tx, ty = dmap.grid2data(tgx, tgy)
tx += grid.x_origin
ty += grid.y_origin
points = np.transpose([tx, ty])
streamlines.append(points)
return streamlines
class StreamplotSet:
def __init__(self, lines, arrows):
self.lines = lines
self.arrows = arrows
# Coordinate definitions
# ========================
class DomainMap:
"""
Map representing different coordinate systems.
Coordinate definitions:
* axes-coordinates goes from 0 to 1 in the domain.
* data-coordinates are specified by the input x-y coordinates.
* grid-coordinates goes from 0 to N and 0 to M for an N x M grid,
where N and M match the shape of the input data.
* mask-coordinates goes from 0 to N and 0 to M for an N x M mask,
where N and M are user-specified to control the density of streamlines.
This class also has methods for adding trajectories to the StreamMask.
Before adding a trajectory, run `start_trajectory` to keep track of regions
crossed by a given trajectory. Later, if you decide the trajectory is bad
(e.g., if the trajectory is very short) just call `undo_trajectory`.
"""
def __init__(self, grid, mask):
self.grid = grid
self.mask = mask
# Constants for conversion between grid- and mask-coordinates
self.x_grid2mask = (mask.nx - 1) / (grid.nx - 1)
self.y_grid2mask = (mask.ny - 1) / (grid.ny - 1)
self.x_mask2grid = 1. / self.x_grid2mask
self.y_mask2grid = 1. / self.y_grid2mask
self.x_data2grid = 1. / grid.dx
self.y_data2grid = 1. / grid.dy
def grid2mask(self, xi, yi):
"""Return nearest space in mask-coords from given grid-coords."""
return round(xi * self.x_grid2mask), round(yi * self.y_grid2mask)
def mask2grid(self, xm, ym):
return xm * self.x_mask2grid, ym * self.y_mask2grid
def data2grid(self, xd, yd):
return xd * self.x_data2grid, yd * self.y_data2grid
def grid2data(self, xg, yg):
return xg / self.x_data2grid, yg / self.y_data2grid
def start_trajectory(self, xg, yg, broken_streamlines=True):
xm, ym = self.grid2mask(xg, yg)
self.mask._start_trajectory(xm, ym, broken_streamlines)
def reset_start_point(self, xg, yg):
xm, ym = self.grid2mask(xg, yg)
self.mask._current_xy = (xm, ym)
def update_trajectory(self, xg, yg, broken_streamlines=True):
if not self.grid.within_grid(xg, yg):
raise InvalidIndexError
xm, ym = self.grid2mask(xg, yg)
self.mask._update_trajectory(xm, ym, broken_streamlines)
def undo_trajectory(self):
self.mask._undo_trajectory()
class Grid:
"""Grid of data."""
def __init__(self, x, y):
if np.ndim(x) == 1:
pass
elif np.ndim(x) == 2:
x_row = x[0]
if not np.allclose(x_row, x):
raise ValueError("The rows of 'x' must be equal")
x = x_row
else:
raise ValueError("'x' can have at maximum 2 dimensions")
if np.ndim(y) == 1:
pass
elif np.ndim(y) == 2:
yt = np.transpose(y)  # Also works for nested lists.
y_col = yt[0]
if not np.allclose(y_col, yt):
raise ValueError("The columns of 'y' must be equal")
y = y_col
else:
raise ValueError("'y' can have at maximum 2 dimensions")
if not (np.diff(x) > 0).all():
raise ValueError("'x' must be strictly increasing")
if not (np.diff(y) > 0).all():
raise ValueError("'y' must be strictly increasing")
self.nx = len(x)
self.ny = len(y)
self.dx = x[1] - x[0]
self.dy = y[1] - y[0]
self.x_origin = x[0]
self.y_origin = y[0]
self.width = x[-1] - x[0]
self.height = y[-1] - y[0]
if not np.allclose(np.diff(x), self.width / (self.nx - 1)):
raise ValueError("'x' values must be equally spaced")
if not np.allclose(np.diff(y), self.height / (self.ny - 1)):
raise ValueError("'y' values must be equally spaced")
@property
def shape(self):
return self.ny, self.nx
def within_grid(self, xi, yi):
"""Return whether (*xi*, *yi*) is a valid index of the grid."""
# Note that xi/yi can be floats; so, for example, we can't simply check
# `xi < self.nx` since *xi* can be `self.nx - 1 < xi < self.nx`
return 0 <= xi <= self.nx - 1 and 0 <= yi <= self.ny - 1
class StreamMask:
"""
Mask to keep track of discrete regions crossed by streamlines.
The resolution of this grid determines the approximate spacing between
trajectories. Streamlines are only allowed to pass through zeroed cells:
When a streamline enters a cell, that cell is set to 1, and no new
streamlines are allowed to enter.
"""
def __init__(self, density):
try:
self.nx, self.ny = (30 * np.broadcast_to(density, 2)).astype(int)
except ValueError as err:
raise ValueError("'density' must be a scalar or be of length "
"2") from err
if self.nx < 0 or self.ny < 0:
raise ValueError("'density' must be positive")
self._mask = np.zeros((self.ny, self.nx))
self.shape = self._mask.shape
self._current_xy = None
def __getitem__(self, args):
return self._mask[args]
def _start_trajectory(self, xm, ym, broken_streamlines=True):
"""Start recording streamline trajectory"""
self._traj = []
self._update_trajectory(xm, ym, broken_streamlines)
def _undo_trajectory(self):
"""Remove current trajectory from mask"""
for t in self._traj:
self._mask[t] = 0
def _update_trajectory(self, xm, ym, broken_streamlines=True):
"""
Update current trajectory position in mask.
If the new position has already been filled, raise `InvalidIndexError`.
"""
if self._current_xy != (xm, ym):
if self[ym, xm] == 0:
self._traj.append((ym, xm))
self._mask[ym, xm] = 1
self._current_xy = (xm, ym)
else:
if broken_streamlines:
raise InvalidIndexError
else:
pass
class InvalidIndexError(Exception):
pass
class TerminateTrajectory(Exception):
pass
# Integrator definitions
# =======================
def _get_integrator(u, v, dmap, minlength, maxlength, integration_direction):
# rescale velocity onto grid-coordinates for integrations.
u, v = dmap.data2grid(u, v)
# speed (path length) will be in axes-coordinates
u_ax = u / (dmap.grid.nx - 1)
v_ax = v / (dmap.grid.ny - 1)
speed = np.ma.sqrt(u_ax ** 2 + v_ax ** 2)
#print("speed", speed)
def forward_time(xi, yi):
if not dmap.grid.within_grid(xi, yi):
raise OutOfBounds
#print("xi",xi)
#print("yi",yi)
ds_dt = interpgrid(speed, xi, yi)
if ds_dt == 0:
#print("ds_dt", ds_dt)
raise TerminateTrajectory()
dt_ds = 1. / ds_dt
ui = interpgrid(u, xi, yi)
vi = interpgrid(v, xi, yi)
return ui * dt_ds, vi * dt_ds
def backward_time(xi, yi):
dxi, dyi = forward_time(xi, yi)
return -dxi, -dyi
def integrate(x0, y0, broken_streamlines=True):
"""
Return x, y grid-coordinates of trajectory based on starting point.
Integrate both forward and backward in time from starting point in
grid coordinates.
Integration is terminated when a trajectory reaches a domain boundary
or when it crosses into an already occupied cell in the StreamMask. The
resulting trajectory is None if it is shorter than `minlength`.
"""
stotal, xy_traj = 0., []
try:
dmap.start_trajectory(x0, y0, broken_streamlines)
except InvalidIndexError:
return None
if integration_direction in ['both', 'backward']:
s, xyt = _integrate_rk12(x0, y0, dmap, backward_time, maxlength,
broken_streamlines)
stotal += s
xy_traj += xyt[::-1]
if integration_direction in ['both', 'forward']:
dmap.reset_start_point(x0, y0)
s, xyt = _integrate_rk12(x0, y0, dmap, forward_time, maxlength,
broken_streamlines)
stotal += s
xy_traj += xyt[1:]
if stotal > minlength:
return np.broadcast_arrays(xy_traj, np.empty((1, 2)))[0]
else:  # reject short trajectories
dmap.undo_trajectory()
return None
return integrate
class OutOfBounds(IndexError):
pass
def _integrate_rk12(x0, y0, dmap, f, maxlength, broken_streamlines=True):
"""
2nd-order Runge-Kutta algorithm with adaptive step size.
This method is also referred to as the improved Euler's method, or Heun's
method. This method is favored over higher-order methods because:
1. To get decent looking trajectories and to sample every mask cell
on the trajectory we need a small timestep, so a lower order
solver doesn't hurt us unless the data is *very* high resolution.
In fact, for cases where the user inputs
data smaller or of similar grid size to the mask grid, the higher
order corrections are negligible because of the very fast linear
interpolation used in `interpgrid`.
2. For high resolution input data (i.e. beyond the mask
resolution), we must reduce the timestep. Therefore, an adaptive
timestep is more suited to the problem as this would be very hard
to judge automatically otherwise.
This integrator is about 1.5 - 2x as fast as RK4 and RK45 solvers (using
similar Python implementations) in most setups.
"""
# This error is below that needed to match the RK4 integrator. It
# is set for visual reasons -- too low and corners start
# appearing ugly and jagged. Can be tuned.
maxerror = 0.003
# This limit is important (for all integrators) to avoid the
# trajectory skipping some mask cells. We could relax this
# condition if we use the code which is commented out below to
# increment the location gradually. However, due to the efficient
# nature of the interpolation, this doesn't boost speed by much
# for quite a bit of complexity.
#print("dmap.mask.nx", dmap.mask.nx)
#maxds = min(1. / dmap.mask.nx, 1. / dmap.mask.ny, 0.1)
maxds = 0.01
ds = maxds
print("ds", ds)
stotal = 0
xi = x0
yi = y0
xyf_traj = []
while True:
try:
if dmap.grid.within_grid(xi, yi):
xyf_traj.append((xi, yi))
else:
raise OutOfBounds
# Compute the two intermediate gradients.
# f should raise OutOfBounds if the locations given are
# outside the grid.
k1x, k1y = f(xi, yi)
k2x, k2y = f(xi + ds * k1x, yi + ds * k1y)
except OutOfBounds:
# Out of the domain during this step.
# Take an Euler step to the boundary to improve neatness
# unless the trajectory is currently empty.
if xyf_traj:
ds, xyf_traj = _euler_step(xyf_traj, dmap, f)
stotal += ds
break
except TerminateTrajectory:
break
dx1 = ds * k1x
dy1 = ds * k1y
dx2 = ds * 0.5 * (k1x + k2x)
dy2 = ds * 0.5 * (k1y + k2y)
ny, nx = dmap.grid.shape
# Error is normalized to the axes coordinates
error = np.hypot((dx2 - dx1) / (nx - 1), (dy2 - dy1) / (ny - 1))
print("error",error)
# Only save step if within error tolerance
if error < maxerror:
xi += dx2
yi += dy2
try:
dmap.update_trajectory(xi, yi, broken_streamlines)
except InvalidIndexError:
break
if stotal + ds > maxlength:
break
stotal += ds
# recalculate stepsize based on step error
if error == 0:
ds = maxds
else:
ds = min(maxds, 0.85 * ds * (maxerror / error) ** 0.5)
return stotal, xyf_traj
def _euler_step(xyf_traj, dmap, f):
"""Simple Euler integration step that extends streamline to boundary."""
ny, nx = dmap.grid.shape
xi, yi = xyf_traj[-1]
cx, cy = f(xi, yi)
if cx == 0:
dsx = np.inf
elif cx < 0:
dsx = xi / -cx
else:
dsx = (nx - 1 - xi) / cx
if cy == 0:
dsy = np.inf
elif cy < 0:
dsy = yi / -cy
else:
dsy = (ny - 1 - yi) / cy
ds = min(dsx, dsy)
xyf_traj.append((xi + cx * ds, yi + cy * ds))
return ds, xyf_traj
# Utility functions
# ========================
def interpgrid(a, xi, yi):
"""Fast 2D, linear interpolation on an integer grid"""
Ny, Nx = np.shape(a)
if isinstance(xi, np.ndarray):
x = xi.astype(int)
y = yi.astype(int)
# Check that xn, yn don't exceed max index
xn = np.clip(x + 1, 0, Nx - 1)
yn = np.clip(y + 1, 0, Ny - 1)
else:
x = int(xi)
y = int(yi)
# conditional is faster than clipping for integers
if x == (Nx - 1):
xn = x
else:
xn = x + 1
if y == (Ny - 1):
yn = y
else:
yn = y + 1
a00 = a[y, x]
a01 = a[y, xn]
a10 = a[yn, x]
a11 = a[yn, xn]
xt = xi - x
yt = yi - y
a0 = a00 * (1 - xt) + a01 * xt
a1 = a10 * (1 - xt) + a11 * xt
ai = a0 * (1 - yt) + a1 * yt
if not isinstance(xi, np.ndarray):
if np.ma.is_masked(ai):
raise TerminateTrajectory
return ai
def _gen_starting_points(shape):
"""
Yield starting points for streamlines.
Trying points on the boundary first gives higher quality streamlines.
This algorithm starts with a point on the mask corner and spirals inward.
This algorithm is inefficient, but fast compared to rest of streamplot.
"""
ny, nx = shape
xfirst = 0
yfirst = 1
xlast = nx - 1
ylast = ny - 1
x, y = 0, 0
direction = 'right'
for i in range(nx * ny):
yield x, y
if direction == 'right':
x += 1
if x >= xlast:
xlast -= 1
direction = 'up'
elif direction == 'up':
y += 1
if y >= ylast:
ylast -= 1
direction = 'left'
elif direction == 'left':
x -= 1
if x <= xfirst:
xfirst += 1
direction = 'down'
elif direction == 'down':
y -= 1
if y <= yfirst:
yfirst += 1
direction = 'right'

huangapple
  • 本文由 发表于 2023年2月10日 11:22:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定