dtw debug:dtaidistance C library is not available 总结(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.

不过这个不影响,还是可以计算出结果的。
总结:去看源码来看错误原因。

你可能感兴趣的:(经验)