2016-12-08 13 views
1

私は、座標間の距離を表す整数のフラットなリストを作成するために、大きな2D Pythonの浮動小数点リストをループするジェネレータを使用する関数を持っています。Cython - 2次元座標間の距離の配列を計算する

point_input = {"x": -8081441.0, "y": 5685214.0} 
output = [-8081441, 5685214] 

polyline_input = {"paths" : [[-8081441.0, 5685214.0], [-8081446.0, 5685216.0], [-8081442.0, 5685219.0], [-8081440.0, 5685211.0], [-8081441.0, 5685214.0]]} 
output = [[-8081441, 5685214, 5, -2, -4, -3, -2, 8, 1, -3]] 

polygon_input = {"rings" : [[-8081441.0, 5685214.0], [-8081446.0, 5685216.0], [-8081442.0, 5685219.0], [-8081440.0, 5685211.0], [-8081441.0, 5685214.0]]} 
output = [[-8081441, 5685214, 5, -2, -4, -3, -2, 8, 1, -3]] 

純粋なのpython:スピード性能を実現するため

def geometry_to_distance(geometry, geometry_type): 
    def calculate_distance(coords): 
     iterator = iter(coords) 
     previous_x, previous_y = iterator.next() 
     yield int(previous_x) 
     yield int(previous_y) 
     for current_x, current_y in iterator: 
      yield int(previous_x - current_x) 
      yield int(previous_y - current_y) 
      previous_x, previous_y = current_x, current_y 

    if geometry_type == "POINT": 
     distance_array = [int(geometry["x"]), int(geometry["y"])] 
    elif geometry_type == "POLYLINE": 
     distance_array = [list(calculate_distance(path)) for path in geometry["paths"]] 
    elif geometry_type == "POLYGON": 
     distance_array = [list(calculate_distance(ring)) for ring in geometry["rings"]] 
    else: 
     raise Exception("{} geometry type not supported".format(geometry_type)) 

    return distance_array 

、私は、同じ機能のcython実装を使用します。私はcalculate_distance関数で整数変数の型宣言を使用しています。

cython実装:ここ

def geometry_to_distance(geometry, geometry_type): 
    def calculate_distance(coords): 
     cdef int previous_x, previous_y, current_x, current_y 
     iterator = iter(coords) 
     previous_x, previous_y = iterator.next() 
     yield previous_x 
     yield previous_y 
     for current_x, current_y in iterator: 
      yield previous_x - current_x 
      yield previous_y - current_y 
      previous_x, previous_y = current_x, current_y 

    if geometry_type == "POINT": 
     distance_array = [geometry["x"], geometry["y"]] 
    elif geometry_type == "POLYLINE": 
     distance_array = [list(calculate_distance(path)) for path in geometry["paths"]] 
    elif geometry_type == "POLYGON": 
     distance_array = [list(calculate_distance(ring)) for ring in geometry["rings"]] 
    else: 
     raise Exception("{} geometry type not supported".format(geometry_type)) 

    return distance_array 

機能ベンチマークに使用できるスクリプト:

import time 
from functools import wraps 
import numpy as np 
import geometry_converter as gc 

def timethis(func): 
    '''Decorator that reports the execution time.''' 
    @wraps(func) 
    def wrapper(*args, **kwargs): 
     start = time.time() 
     result = func(*args, **kwargs) 
     end = time.time() 
     print(func.__name__, end-start) 
     return result 
    return wrapper 


def prepare_data(featCount, size): 
    ''' Create arrays of polygon geometry (see polygon_input above)''' 
    input = [] 
    for i in xrange(0, featCount): 
     polygon = {"rings" : []} 
     #random x,y coordinates inside a quadrant of the world bounding box in a spherical mercator (epsg:3857) projection 
     ys = np.random.uniform(-20037507.0,0,size).tolist() 
     xs = np.random.uniform(0,20037507.0,size).tolist() 
     polygon["rings"].append(zip(xs,ys)) 
     input.append(polygon) 
    return input 

@timethis 
def process_data(data): 
    output = [gc.esriJson_to_CV(x, "POLYGON") for x in data] 
    return output 

data = prepare_data(100, 100000) 
process_data(data) 

はcython実装のパフォーマンスを向上させる可能性があり改善がありますか?多分2Dのサイモン配列やカーレーを使って?発電機なしで書き換え

+0

「numpy.diff」を使ってX座標とY座標の1番目の違いを取得するだけではどうですか? – pbreach

+0

巨大な2DのPythonリストからnumpy.arrayを作成するのは遅すぎるようです。 –

+0

cythonやc配列にも同じ問題があります。リストは連続したメモリには格納されず、(均一な)numpy、cython、およびc配列が格納されます。したがって、これらのアプローチに関係なく、変換には時間がかかるでしょう。 'numpy.diff'はジェネレータとリストの使用を考慮してcythonの実装よりも高速ではありません。 – pbreach

答えて

1

ザ・Pythonは、私はちょうど計算を表現する別の方法のにと思ってい

In [362]: polyline_input = {"paths" : [[-8081441.0, 5685214.0], [-8081446.0, 568 
    ...: 5216.0], [-8081442.0, 5685219.0], [-8081440.0, 5685211.0], [-8081441.0 
    ...: , 5685214.0]]} 
In [363]: output=polyline_input['paths'][0][:] # copy 
In [364]: i0,j0 = output 
    ...: for i,j in polyline_input['paths'][1:]: 
    ...:  output.extend([i0-i, j0-j][:]) 
    ...:  i0,j0 = i,j 
    ...:  
In [365]: output 
Out[365]: [-8081441.0, 5685214.0, 5.0, -2.0, -4.0, -3.0, -2.0, 8.0, 1.0, -3.0] 

です。フラットリストの代わりにappendをペアのリストに使用できました。

アレイ当量

In [375]: arr=np.array(polyline_input['paths']) 
In [376]: arr[1:,:]=arr[:-1,:]-arr[1:,:] 
In [377]: arr.ravel().tolist() 
Out[377]: [-8081441.0, 5685214.0, 5.0, -2.0, -4.0, -3.0, -2.0, 8.0, 1.0, -3.0] 

配列にリストを変換するコストを無視し、それは、効率的なnumpyの動作のように見えます。それを改善するためには、配列をmemoryviewに変換して、値の対を使ってcスタイルを反復しなければならないと思います。

私はこの距離フォーマットに切り替える理由を忘れています。ファイルスペースを節約しようとしていますか?または、下流の計算をスピードアップしますか?

+0

あなたの答えをありがとう!はい、目的は、ジオメトリの軽量表現を取得して、ネットワーク上の共有の速度を向上させることです –

関連する問題