程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

dtw debug:dtaidistance C library is not available 總結(python)

編輯:Python

最近在用dtw,發現:
dtw.distance 就不會報錯
dtw.distance_matrix_fast 就會報錯:

The compiled dtaidistance C library is not available.
See the documentation for alternative installation options.

上dtaidistance的官網看,官網地址:
https://dtaidistance.readthedocs.io/en/latest/usage/installation.html#from-source
叫我嘗試其他的安裝方法。
官網一共提供了三種這個依賴的安裝方法:

  • From PyPI
  • From Github
  • From source

我都嘗試了以下,效果都不是很好,還是有那個問題。不過第三種安裝方式其實是可以解決問題的,只是我不知道安裝目錄應該怎麼安排。
但是我的另一台電腦不報錯,我就對比了以下兩個工程Lib/site-packages目錄下的dtaidistance,發現,報錯的工程這個目錄下只有26個文件,不報錯的工程下這個目錄有34個文件。
所以我懷疑可能是這個包缺少文件導致的。

另外,我點進dtw.distance_matrix_fast 這個函數看到:

def distance_matrix_fast(s, max_dist=None, max_length_diff=None,
window=None, max_step=None, penalty=None, psi=None,
block=None, compact=False, parallel=True, use_mp=False,
only_triu=False):
"""Same as :meth:`distance_matrix` but with different defaults to choose the fast parallized C version (use_c = True and parallel = True). This method uses the C-compiled version of the DTW algorithm and uses parallelization. By default this is the OMP C parallelization. If the OMP functionality is not available the parallelization is changed to use Python's multiprocessing library. """
_check_library(raise_exception=True, include_omp=False)
if not use_mp and parallel:
try:
_check_library(raise_exception=True, include_omp=True)
except Exception:
use_mp = True
return distance_matrix(s, max_dist=max_dist, max_length_diff=max_length_diff,
window=window, max_step=max_step, penalty=penalty, psi=psi,
block=block, compact=compact, parallel=parallel,
use_c=True, use_mp=use_mp, show_progress=False, only_triu=only_triu)

這個函數有個check library,進而覺得是這裡有問題,再點進去可以看到:

def _check_library(include_omp=False, raise_exception=True):
if dtw_cc is None:
msg = "The compiled dtaidistance C library is not available.\n" + \
"See the documentation for alternative installation options."
logger.error(msg)
if raise_exception:
raise Exception(msg)
if include_omp and (dtw_cc_omp is None or not dtw_cc_omp.is_openmp_supported()):
msg = "The compiled dtaidistance C-OMP library "
if dtw_cc_omp and not dtw_cc_omp.is_openmp_supported():
msg += "indicates that OpenMP was not avaiable during compilation.\n"
else:
msg += "is not available.\n"
msg += "Use Python's multiprocessing library for parellelization (use_mp=True).\n" + \
"Call dtw.try_import_c() to get more verbose errors.\n" + \
"See the documentation for alternative installation options."
logger.error(msg)
if raise_exception:
raise Exception(msg)

第一個if後面提示的信息和我的報錯信息一模一樣。至此算是找到了問題:缺少dtw_cc。
於是我把dtaidistance裡的dtw_cc.c dtw_cc.pyx都復制到Lib/site-packages目錄下,運行發現還是不行。
我又嘗試把官網from source的安裝方法中的dtaidistance整個文件夾cut出來,貼在Lib/site-packages目錄下,替換掉原來的dtaidistance,再運行就成功了。
但是還有以下提示:

The compiled dtaidistance C-OMP library indicates that OpenMP was not avaiable during compilation.
Use Python's multiprocessing library for parellelization (use_mp=True).
Call dtw.try_import_c() to get more verbose errors.
See the documentation for alternative installation options.

不過這個不影響,還是可以計算出結果的。
總結:去看源碼來看錯誤原因。


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved