本文主要是介绍python 笔记 :trajectory_distance包(如何可以正确使用)【debug篇】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
包的地址:maikol-solis/trajectory_distance (github.com)
1 模块介绍
用Cython实现的Python模块,用于计算二维轨迹之间的距离
trajectory_distance包提供了9种轨迹间的距离计算方法:
- SSPD(对称线段路径距离)
- OWD(单向距离)
- Hausdorff
- Frechet
- 离散Frechet
- DTW(动态时间规整)
- LCSS(最长公共子序列)
- ERP(带实数惩罚的编辑距离)
- EDR(实数序列上的编辑距离)
除了离散Frechet和离散Frechet,所有的距离计算都提供欧几里得或球形选项:
- 欧几里得是基于二维坐标之间的欧几里得距离。
- 球形是基于二维坐标之间的Haversine距离。
2 debug过程
2.1 python setup.py install > 1.txt 2>&1 之后的报错
部分内容如下:
Error compiling Cython file:
------------------------------------------------------------
...q=len(Q)cc=_compute_critical_values(P,Q,p,q)eps=cc[0]while(len(cc)!=1):m_i=len(cc)/2-1^
------------------------------------------------------------traj_dist/cydist/frechet.pyx:535:21: Cannot assign type 'double' to 'int'
Compiling traj_dist/cydist/frechet.pyx because it changed.
[1/1] Cythonizing traj_dist/cydist/frechet.pyx
Traceback (most recent call last):File "/home_nfs/liushuai/Dataspark/trajectory_distance/setup.py", line 20, in <module>setup(File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/__init__.py", line 107, in setupreturn distutils.core.setup(**attrs)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/core.py", line 185, in setupreturn run_commands(dist)^^^^^^^^^^^^^^^^^^File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/core.py", line 201, in run_commandsdist.run_commands()File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/dist.py", line 969, in run_commandsself.run_command(cmd)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/dist.py", line 1234, in run_commandsuper().run_command(command)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/dist.py", line 988, in run_commandcmd_obj.run()File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/command/install.py", line 80, in runself.do_egg_install()File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/command/install.py", line 129, in do_egg_installself.run_command('bdist_egg')File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/cmd.py", line 318, in run_commandself.distribution.run_command(command)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/dist.py", line 1234, in run_commandsuper().run_command(command)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/dist.py", line 988, in run_commandcmd_obj.run()File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/command/bdist_egg.py", line 164, in runcmd = self.call_command('install_lib', warn_dir=0)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/command/bdist_egg.py", line 150, in call_commandself.run_command(cmdname)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/cmd.py", line 318, in run_commandself.distribution.run_command(command)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/dist.py", line 1234, in run_commandsuper().run_command(command)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/dist.py", line 988, in run_commandcmd_obj.run()File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/command/install_lib.py", line 11, in runself.build()File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/command/install_lib.py", line 111, in buildself.run_command('build_ext')File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/cmd.py", line 318, in run_commandself.distribution.run_command(command)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/dist.py", line 1234, in run_commandsuper().run_command(command)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/dist.py", line 988, in run_commandcmd_obj.run()File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/command/build_ext.py", line 345, in runself.build_extensions()File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/command/build_ext.py", line 467, in build_extensionsself._build_extensions_serial()File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/setuptools/_distutils/command/build_ext.py", line 493, in _build_extensions_serialself.build_extension(ext)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/Cython/Distutils/build_ext.py", line 122, in build_extensionnew_ext = cythonize(^^^^^^^^^^File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/Cython/Build/Dependencies.py", line 1134, in cythonizecythonize_one(*args)File "/home_nfs/liushuai/anaconda3/lib/python3.11/site-packages/Cython/Build/Dependencies.py", line 1301, in cythonize_oneraise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: traj_dist/cydist/frechet.pyx
- 在
traj_dist/cydist/frechet.pyx
文件中,有一处类型分配错误。错误信息提示不能将类型 'double' 分配给 'int'。这通常意味着在 Cython 代码中有一个类型不匹配的问题。 - 在这个具体的例子中,
m_i=len(cc)/2-1
这行代码的问题是len(cc)/2
的结果是一个浮点数(double),而m_i
应该是一个整数(int)。 - 解决方法:通过使用整数除法
//
替代普通除法/
来修复这个问题
然后就成功地install了
2.2 ModuleNotFoundError: No module named 'pydist'
在python中敲入:
import traj_dist.distance as tdist
后,会出现如下错误:
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/home_nfs/liushuai/Dataspark/trajectory_distance/traj_dist/distance.py", line 1, in <module>from pydist.linecell import trajectory_set_grid
ModuleNotFoundError: No module named 'pydist'
- 表明 Python 无法在环境中找到名为
pydist
的模块 但pydist
确实存在于traj_dist
包的目录中- 这意味着问题可能不在于
pydist
模块的缺失,而是在于 Python 环境在寻找这个模块时出现了问题。
- 这意味着问题可能不在于
- 解决方法:
- 在
traj_dist/distance.py
文件中,导入语句使用相对导入
- 在
#from pydist.linecell import trajectory_set_grid
from .pydist.linecell import trajectory_set_grid
-
- 或者使用绝对导入
from traj_dist.pydist.linecell import trajectory_set_grid
【类似语句一并修改】
2.3 ModuleNotFoundError: No module named 'geohash'
pip install python-geohash
即可
2.4 ModuleNotFoundError: No module named 'basic_euclidean'
import traj_dist.distance as tdist
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/home_nfs/liushuai/Dataspark/trajectory_distance/traj_dist/distance.py", line 3, in <module>from .pydist.sspd import e_sspd, g_sspdFile "/home_nfs/liushuai/Dataspark/trajectory_distance/traj_dist/pydist/sspd.py", line 2, in <module>from basic_euclidean import point_to_trajectory
ModuleNotFoundError: No module named 'basic_euclidean'
和2.2类似,修改导入语句
#from basic_euclidean import point_to_trajectoryfrom .basic_euclidean import point_to_trajectory
或者
from traj_dist.pydist.basic_euclidean import point_to_trajectory
【类似语句一并修改】
2.5 ModuleNotFoundError: No module named 'linecell'
import traj_dist.distance as tdist
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/home_nfs/liushuai/Dataspark/trajectory_distance/traj_dist/distance.py", line 11, in <module>from .pydist.sowd import sowd_gridFile "/home_nfs/liushuai/Dataspark/trajectory_distance/traj_dist/pydist/sowd.py", line 2, in <module>import linecell as linec
ModuleNotFoundError: No module named 'linecell'
还是修改导入内容
from . import linecell as linec
#或者
from traj_dist.pydist import linecell as linec
2.6 ModuleNotFoundError: No module named 'traj_dist.cydist.sspd'
import traj_dist.distance as tdist
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/home_nfs/liushuai/Dataspark/trajectory_distance/traj_dist/distance.py", line 13, in <module>from .cydist.sspd import c_e_sspd, c_g_sspd
ModuleNotFoundError: No module named 'traj_dist.cydist.sspd'
- 错误
ModuleNotFoundError: No module named 'traj_dist.cydist.sspd'
表示 Python 无法在traj_dist.cydist
子模块中找到sspd
模块traj_dist/cydist
目录中包含sspd.pyx
和sspd.c
文件,说明sspd
模块是一个 Cython 模块,它需要被编译成一个 Python 可以导入的扩展- 在有setup.py 文件的地方执行如下命令:
- python setup.py build_ext --inplace
- 这个命令会编译
.pyx
文件并生成
相应的二进制扩展,如Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/home_nfs/liushuai/Dataspark/trajectory_distance/traj_dist/distance.py", line 13, in <module>from .cydist.sspd import c_e_sspd, c_g_sspdFile "traj_dist/cydist/sspd.pyx", line 8, in init traj_dist.cydist.sspdfrom basic_euclidean import c_point_to_trajectory ModuleNotFoundError: No module named 'basic_euclidean'
.so
文件(在 Linux 或 macOS)或.pyd
文件(在 Windows)
- 运行上述编译命令后,检查
traj_dist/cydist
目录,看看是否生成了sspd
的扩展文件(.so
或.pyd
)。如果生成了,那么遍解决了这个错误,如果没有生成,可能是编译过程中出现了错误。
2.7 ModuleNotFoundError: No module named 'basic_euclidean'
import traj_dist.distance as tdist
- 在
sspd.pyx
中尝试从basic_euclidean
导入时出现了问题 - 改成:
from .basic_euclidean import c_point_to_trajectory
但修改pyx文件和前面修改py文件不一样的是,在修改了 .pyx
文件之后,需要重新编译 Cython 扩展
python setup.py build_ext --inplace
然后就可以正常使用这个库了
这篇关于python 笔记 :trajectory_distance包(如何可以正确使用)【debug篇】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!