gregorias / pycodec2

Python's interface to codec 2
BSD 3-Clause "New" or "Revised" License
20 stars 6 forks source link

Can't import pycodec2.Codec2 #3

Closed MittalShruti closed 4 years ago

MittalShruti commented 4 years ago

I am getting the following error on running

self.c2 = pycodec2.Codec2(kbps)
AttributeError: module 'pycodec2' has no attribute 'Codec2'

I git cloned codec2 in pycodec2. and installed it

gregorias commented 4 years ago

Hi,

I recommend that you use your system's package manager to install codec2. See for example the README.md.

The problem stems from how compilation works. Pycodec2 uses Cython for installation. Cython requires codec2's files. Cython searches standard directories, e.g. /usr/include, but it doesn't search your working directory.

This means that if you don't want to install the library in your system, you can remedy the problem by setting CPATH to ., which will hint Cython to look into your local directory.

MittalShruti commented 4 years ago

Hi @gregorias

I am using pycodec2 in google colab. I set CPATH to current directory, which is /content but I am stilling getting the same error

import os 
os.environ['CPATH'] = '/content'
import struct
import sys

import numpy as np
import pycodec2

c2 = pycodec2.Codec2(1200)

Error:

      8 import pycodec2
      9 
---> 10 c2 = pycodec2.Codec2(1200)
     11 

AttributeError: module 'pycodec2' has no attribute 'Codec2'
MittalShruti commented 4 years ago

I did :

!git clone https://github.com/gregorias/pycodec2 
!cd pycodec2 && git clone https://github.com/drowe67/codec2 && cd codec2 && mkdir build_linux && cd build_linux && cmake ../ && make && sudo make install

edit setup.py file to include numpy.get_include()

!cd pycodec2 && python3 setup.py install

Output

running install
running build
running build_py
package init file 'pycodec2/__init__.py' not found (or not a regular file)
package init file 'pycodec2/__init__.py' not found (or not a regular file)
running build_ext
cythoning pycodec2/pycodec2.pyx to pycodec2/pycodec2.c
/usr/local/lib/python3.6/dist-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /content/pycodec2/pycodec2/pycodec2.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
building 'pycodec2' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/local/lib/python3.6/dist-packages/numpy/core/include -I/usr/include/python3.6m -c pycodec2/pycodec2.c -o build/temp.linux-x86_64-3.6/pycodec2/pycodec2.o
In file included from /usr/local/lib/python3.6/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1832:0,
                 from /usr/local/lib/python3.6/dist-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /usr/local/lib/python3.6/dist-packages/numpy/core/include/numpy/arrayobject.h:4,
                 from pycodec2/pycodec2.c:601:
/usr/local/lib/python3.6/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it with " \
  ^~~~~~~
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.6/pycodec2/pycodec2.o -lcodec2 -o build/lib.linux-x86_64-3.6/pycodec2.cpython-36m-x86_64-linux-gnu.so
running install_lib
copying build/lib.linux-x86_64-3.6/pycodec2.cpython-36m-x86_64-linux-gnu.so -> /usr/local/lib/python3.6/dist-packages
running install_egg_info
Removing /usr/local/lib/python3.6/dist-packages/pycodec2-1.0.2.egg-info
Writing /usr/local/lib/python3.6/dist-packages/pycodec2-1.0.2.egg-info

On

import struct
import sys

import numpy as np
import pycodec2

c2 = pycodec2.Codec2(1200)

I get

      5 import pycodec2
      6 
----> 7 c2 = pycodec2.Codec2(1200)
      8 

AttributeError: module 'pycodec2' has no attribute 'Codec2'
MittalShruti commented 4 years ago

Adding __init__.py in directory pycodec2/pycodec2 results in error compiling cython file

running install
running build
running build_py
creating build/lib.linux-x86_64-3.6/pycodec2
copying pycodec2/__init__.py -> build/lib.linux-x86_64-3.6/pycodec2
running build_ext
cythoning pycodec2/pycodec2.pyx to pycodec2/pycodec2.c
/usr/local/lib/python3.6/dist-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /content/pycodec2/pycodec2/pycodec2.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)

Error compiling Cython file:
------------------------------------------------------------
...
from codec2 cimport *
^
------------------------------------------------------------

pycodec2/pycodec2.pyx:1:0: 'codec2.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
cdef class Codec2:
  '''Wrapper for codec2 state and its functions.

  Initialization method expects an integer defining expected bitrate per
  second.'''
  cdef CODEC2 *_c_codec2_state
      ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:25:7: 'CODEC2' is not a type identifier

Error compiling Cython file:
------------------------------------------------------------
...

ctypedef cnp.int16_t SHORT_DTYPE_t
ctypedef cnp.int_t INT_DTYPE_t

_modes = {
    1200 : CODEC2_MODE_1200,
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:12:11: undeclared name not builtin: CODEC2_MODE_1200

Error compiling Cython file:
------------------------------------------------------------
...
ctypedef cnp.int16_t SHORT_DTYPE_t
ctypedef cnp.int_t INT_DTYPE_t

_modes = {
    1200 : CODEC2_MODE_1200,
    1300 : CODEC2_MODE_1300,
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:13:11: undeclared name not builtin: CODEC2_MODE_1300

Error compiling Cython file:
------------------------------------------------------------
...
ctypedef cnp.int_t INT_DTYPE_t

_modes = {
    1200 : CODEC2_MODE_1200,
    1300 : CODEC2_MODE_1300,
    1400 : CODEC2_MODE_1400,
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:14:11: undeclared name not builtin: CODEC2_MODE_1400

Error compiling Cython file:
------------------------------------------------------------
...

_modes = {
    1200 : CODEC2_MODE_1200,
    1300 : CODEC2_MODE_1300,
    1400 : CODEC2_MODE_1400,
    1600 : CODEC2_MODE_1600,
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:15:11: undeclared name not builtin: CODEC2_MODE_1600

Error compiling Cython file:
------------------------------------------------------------
...
_modes = {
    1200 : CODEC2_MODE_1200,
    1300 : CODEC2_MODE_1300,
    1400 : CODEC2_MODE_1400,
    1600 : CODEC2_MODE_1600,
    2400 : CODEC2_MODE_2400,
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:16:11: undeclared name not builtin: CODEC2_MODE_2400

Error compiling Cython file:
------------------------------------------------------------
...
    1200 : CODEC2_MODE_1200,
    1300 : CODEC2_MODE_1300,
    1400 : CODEC2_MODE_1400,
    1600 : CODEC2_MODE_1600,
    2400 : CODEC2_MODE_2400,
    3200 : CODEC2_MODE_3200,
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:17:11: undeclared name not builtin: CODEC2_MODE_3200

Error compiling Cython file:
------------------------------------------------------------
...
  Initialization method expects an integer defining expected bitrate per
  second.'''
  cdef CODEC2 *_c_codec2_state

  def __cinit__(self, mode):
    self._c_codec2_state = codec2_create(_modes[mode])
                          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:28:27: undeclared name not builtin: codec2_create

Error compiling Cython file:
------------------------------------------------------------
...
    if self._c_codec2_state is NULL:
      raise MemoryError()

  def __dealloc__(self):
    if self._c_codec2_state is not NULL:
      codec2_destroy(self._c_codec2_state)
     ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:34:6: undeclared name not builtin: codec2_destroy

Error compiling Cython file:
------------------------------------------------------------
...
    Encode the given ndarray of samples to bits represented as a byte array.'''
    assert len(speech_in) % self.samples_per_frame() == 0
    frames = len(speech_in) // self.samples_per_frame()
    bit_count = frames * self.bits_per_frame()
    bits = b'\x00' * int(math.ceil(bit_count / 8.0))
    codec2_encode(self._c_codec2_state, bits, <short *>speech_in.data)
   ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:44:4: undeclared name not builtin: codec2_encode

Error compiling Cython file:
------------------------------------------------------------
...
    Encode the given ndarray of samples to bits represented as a byte array.'''
    assert len(speech_in) % self.samples_per_frame() == 0
    frames = len(speech_in) // self.samples_per_frame()
    bit_count = frames * self.bits_per_frame()
    bits = b'\x00' * int(math.ceil(bit_count / 8.0))
    codec2_encode(self._c_codec2_state, bits, <short *>speech_in.data)
                                             ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:44:46: Cannot convert 'short *' to Python object

Error compiling Cython file:
------------------------------------------------------------
...
    assert (len(bits) * 8) >= self.bits_per_frame()
    cdef cnp.ndarray[SHORT_DTYPE_t, ndim=1] speech_out
    frames = int(math.floor((len(bits) * 8.0) / self.bits_per_frame()))
    sample_count = frames * self.samples_per_frame()
    speech_out = np.empty(sample_count, dtype=np.int16, order='C')
    codec2_decode(self._c_codec2_state, <short *>speech_out.data, bits)
   ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:54:4: undeclared name not builtin: codec2_decode

Error compiling Cython file:
------------------------------------------------------------
...
    assert (len(bits) * 8) >= self.bits_per_frame()
    cdef cnp.ndarray[SHORT_DTYPE_t, ndim=1] speech_out
    frames = int(math.floor((len(bits) * 8.0) / self.bits_per_frame()))
    sample_count = frames * self.samples_per_frame()
    speech_out = np.empty(sample_count, dtype=np.int16, order='C')
    codec2_decode(self._c_codec2_state, <short *>speech_out.data, bits)
                                       ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:54:40: Cannot convert 'short *' to Python object

Error compiling Cython file:
------------------------------------------------------------
...
    assert (len(bits) * 8) >= self.bits_per_frame()
    cdef cnp.ndarray[SHORT_DTYPE_t, ndim=1] speech_out
    frames = (len(bits) * 8) // self.bits_per_frame()
    sample_count = frames * self.samples_per_frame()
    speech_out = np.empty(sample_count, dtype=np.int16, order='C')
    codec2_decode_ber(self._c_codec2_state,
   ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:63:4: undeclared name not builtin: codec2_decode_ber

Error compiling Cython file:
------------------------------------------------------------
...
    cdef cnp.ndarray[SHORT_DTYPE_t, ndim=1] speech_out
    frames = (len(bits) * 8) // self.bits_per_frame()
    sample_count = frames * self.samples_per_frame()
    speech_out = np.empty(sample_count, dtype=np.int16, order='C')
    codec2_decode_ber(self._c_codec2_state,
        <short *>speech_out.data,
       ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:64:8: Cannot convert 'short *' to Python object

Error compiling Cython file:
------------------------------------------------------------
...
        bits,
        ber_est)
    return speech_out

  def samples_per_frame(self):
    return codec2_samples_per_frame(self._c_codec2_state)
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:70:11: undeclared name not builtin: codec2_samples_per_frame

Error compiling Cython file:
------------------------------------------------------------
...

  def samples_per_frame(self):
    return codec2_samples_per_frame(self._c_codec2_state)

  def bits_per_frame(self):
    return codec2_bits_per_frame(self._c_codec2_state)
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:73:11: undeclared name not builtin: codec2_bits_per_frame

Error compiling Cython file:
------------------------------------------------------------
...
  def set_lpc_post_filter(self,
      int enable,
      int bass_boost,
      float beta,
      float gamma):
    codec2_set_lpc_post_filter(self._c_codec2_state,
   ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:80:4: undeclared name not builtin: codec2_set_lpc_post_filter

Error compiling Cython file:
------------------------------------------------------------
...
        bass_boost,
        beta,
        gamma)

  def get_spare_bit_index(self):
    return codec2_get_spare_bit_index(self._c_codec2_state)
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:87:11: undeclared name not builtin: codec2_get_spare_bit_index

Error compiling Cython file:
------------------------------------------------------------
...

  def get_spare_bit_index(self):
    return codec2_get_spare_bit_index(self._c_codec2_state)

  def rebuild_spare_bit(self, cnp.ndarray[INT_DTYPE_t, ndim=1] unpacked_bits):
    return codec2_rebuild_spare_bit(self._c_codec2_state,
          ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:90:11: undeclared name not builtin: codec2_rebuild_spare_bit

Error compiling Cython file:
------------------------------------------------------------
...
  def get_spare_bit_index(self):
    return codec2_get_spare_bit_index(self._c_codec2_state)

  def rebuild_spare_bit(self, cnp.ndarray[INT_DTYPE_t, ndim=1] unpacked_bits):
    return codec2_rebuild_spare_bit(self._c_codec2_state,
        <int *>unpacked_bits.data)
       ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:91:8: Cannot convert 'int *' to Python object

Error compiling Cython file:
------------------------------------------------------------
...
  def rebuild_spare_bit(self, cnp.ndarray[INT_DTYPE_t, ndim=1] unpacked_bits):
    return codec2_rebuild_spare_bit(self._c_codec2_state,
        <int *>unpacked_bits.data)

  def set_natural_or_gray(self, int gray):
    codec2_set_natural_or_gray(self._c_codec2_state, gray)
   ^
------------------------------------------------------------

pycodec2/pycodec2.pyx:94:4: undeclared name not builtin: codec2_set_natural_or_gray
running install_lib
copying build/lib.linux-x86_64-3.6/pycodec2/__init__.py -> /usr/local/lib/python3.6/dist-packages/pycodec2
byte-compiling /usr/local/lib/python3.6/dist-packages/pycodec2/__init__.py to __init__.cpython-36.pyc
running install_egg_info
Removing /usr/local/lib/python3.6/dist-packages/pycodec2-1.0.2.egg-info
Writing /usr/local/lib/python3.6/dist-packages/pycodec2-1.0.2.egg-info
gregorias commented 4 years ago

Hi @MittalShruti,

I am using pycodec2 in google colab. I set CPATH to current directory, which is /content but I am stilling getting the same error

I've linked to the README in the post you mention, and the README explicitly says that pycodec2 requires codec2 to function.

From the multi-post problem description, I don't see that you've tried to install codec2. Could you try that? The root-cause of your problem is lack of codec2.

MittalShruti commented 4 years ago

Thanks for replying.

I had mentioned in the first post that I cloned the codec2 in pycodec2 folder .. I tried other locations to clone codec2 as well

I did :

!git clone https://github.com/gregorias/pycodec2 
!cd pycodec2 && git clone https://github.com/drowe67/codec2 && cd codec2 && mkdir build_linux && cd build_linux && cmake ../ && make && sudo make install
baicaitongee commented 4 years ago

I installed pycodec2 successfully on ubuntu 20.04!

sudo apt install libcodec2-dev
sudo apt install codec2
pip install pycodec2