繁体   English   中英

如何在 x,y 点的二维数组中找到第一个极值?

[英]How to find the first extremum in 2D array of x,y points?

我有一组点x_i, y_i表示不是双射映射(没有一一对应。见附图: 在此处输入图片说明

(不要注意第二行。它只是显示一个中心质量)。 我试图找到它的第一个峰值(如您所见,它发现不正确)。 代码如下。 在这里,我按 Ox 轴对点进行排序,然后使用find_peaks函数:

# sort points by X axis
aa = zip(x,y)
bb = sorted(aa, key=lambda x: (x[0], x[1]))
x,y = zip(*bb)
x = np.array(x)
y = np.array(y)
# find all peaks
peaks, props = find_peaks(y, prominence=0.01, height=0.4)
print('rmax candidates=', y[peaks])
rmax = y[peaks[0]] # first peak is not correct

我注意到这里的排序处理不正确 如果我只绘制y数组,那么我会看到图片: 在此处输入图片说明 . 我们看到的“齿轮非常锋利”。

因此,如何以最接近的方式对点进行排序(如果我可以设置一个起点)。 对于人类来说,在图形上画一条线是一件容易的事,但如何为计算机开发算法呢? 我知道Online Digitizer 中已经使用了类似的算法。 可以轻松提取图形中的点坐标。

也许你有更好的算法来找到第一个峰值? 如果您有任何问题,请问我。

数据可以在这里找到:

可以给出以下想法:

读取文件并将数据放入二维 NumPy 数组中。

arr = []        
with open(str(Path('example.csv'))) as csv_file:
    reader = csv.reader(csv_file, delimiter=',')
    for row in reader:
        arr.append([float(row[0]),float(row[1])])
arr = np.array(arr)

由于数据是无序的,因此当我们随后连接点时,我们会观察到一个丑陋的图。

fig0 = go.Figure(go.Scatter(x=arr[:,0],y=arr[:,1], mode='lines'))
fig0.show()

在此处输入图片说明

为了获得合适的图片,我们需要从某个起点对点进行排序,以便仅连接最近的点

arx, x0, xmin, xmax = sort_by_nearest(arr)
x = arx[:,0]
y = arx[:,1]

排序后的数据y看起来像: 在此处输入图片说明 并且非双射行为消失了,我们可以使用find_peaks函数轻松找到仅y数据的第一个峰值。 prominence是从一个峰到下一个峰的下降和上升的最小高度。 因此,小的嘈杂峰将被丢弃。 height负责一个参数,其中仅在y>height区域中搜索峰值

peaks, props = find_peaks(y, prominence=0.01, height=0.4)

这里我们有几个候选人

print('rmax candidates=', y[peaks])

当然,我们取第一个

rmax = y[peaks[0]]
x_rmax = x[peaks[0]] - x0

让我们绘制结果: 在此处输入图片说明

fig0 = go.Figure(go.Scatter(y=arx[:,1], mode='lines'))
fig0.show()

fig0 = go.Figure(go.Scatter(x=arx[:,0]-x0, y=arx[:,1], name='my_line', mode='lines'))
fig0.add_trace(go.Scatter(x=[x_rmax,x_rmax], y=[0,0.5], name='peak coord', mode='lines', line=dict(color='black', width=2, dash='dot')))

fig0.show()

让我们专注于按邻居对数据进行排序的算法。

import pandas as pd
import numpy as np
import plotly.graph_objects as go
import csv
from scipy.signal import find_peaks
import scipy.spatial.distance as ds
from pathlib import Path

搜索最近点的函数很棘手,我们需要以某种方式标记已经观察到的点以避免无限循环。 我们只是通过大数字修改点坐标来做到这一点 数组arr_full的副本

def closest_point(arr_full, xi, yi, inds):
    N = arr_full.shape[0]
    arr_full[inds] = 1e+30
    dist = (arr_full[:, 0] - xi)**2 + (arr_full[:, 1] - yi)**2
    i_min = dist.argmin()
    #returns an index of the nearest point and its coordinate
    return i_min, arr_full[i_min][0], arr_full[i_min][1]

def sort_by_nearest(arr):
    N = arr.shape[0]
    nearest_point = [None]*N
    #find initial point
    xmin = min(arr[:,0])
    xmax = max(arr[:,0])
    # we copy the original array
    arr_cut = np.copy(arr)
    # the area in which we are 100% sure in absence of the starting point we flag by big value
    arr_cut[arr[:,0] > 0.5*(xmin + xmax)] = 10e+30
    iymin = arr_cut[:,1].argmin()
    # the staring points are
    x0, y0 = arr[iymin, 0], arr[iymin, 1]
    # we initialize the sorted value nearest_point
    nearest_point[0] = [arr[iymin,0],arr[iymin,1]]
    print('Starting point:', x0, y0)
    # we put in it the indices of visited points
    observed = [iymin]
    i_min = iymin
    for i in range(1,N):
        xi, yi = arr[i_min]
        i_min, xip, yip = closest_point(arr, xi, yi, i_min)
        nearest_point[i] = [xip, yip]
        observed.append(i_min)
    nearest_point = np.array(nearest_point)
    return np.array(nearest_point), x0, xmin, xmax

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM