Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making package compatible with language_level=3 #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

shz9
Copy link

@shz9 shz9 commented Sep 21, 2023

Hey,

I tried installing phasedibd on the compute cluster and noticed that there are compilation errors depending on which cython version you use. For example, if I use cython==3.0.2, I get compilation errors along the line of:

Error compiling Cython file:
------------------------------------------------------------
...
from libc.stdint cimport uint8_t, uint16_t, uint32_t
from libc.stdio cimport *
from haplotype_alignment cimport HaplotypeAlignment
^
------------------------------------------------------------

phasedibd/compressed_haplotype_alignment.pxd:3:0: 'haplotype_alignment.pxd' not found

In other words, the compiler is not finding the relevant .pxd file. This issue does not arise when working with cython==0.29.23 as well as earlier versions of python3. I noticed that what is driving the differences in behavior is the default assignment of the language_level compiler flag. Earlier versions of cython and python3 use language_level="2" by default (if not specified). In newer versions, they use language_level="3". So, the first change I made was I explicitly set the language_level in the new setup.py file to avoid these issues:

ext_modules=cythonize([Extension('phasedibd.*', ['phasedibd/*.pyx'], include_dirs=[np.get_include()])], language_level="3")

However, this raises other issues. In python3, import paths are expected to be absolute (issue and its relevance to language_level is discussed here). So, I had to update all the .pxd and .pyx files to ensure that the import paths are absolute.

Finally, because of the switch to python3, there is an additional compiling error that arises when cythonizing templated_pbwt_analysis.pyx. The issue has to do with the integer division being translated to a double on lines 93 and 165:

num_cols = sizeof(self.current_matches_start)/sizeof(self.current_matches_start[0])
num_rows = sizeof(self.current_matches_start)/sizeof(self.current_matches_start[0])

To make this work, I changed the division to //. (Not sure if this would preserve the intended behavior; Please confirm).

Finally, I added a requirements.txt file because a friend noticed that the unit tests fail when using newer versions of pandas. Please feel free to modify/update this based on your latest tests.

Adding explicit flag for `language_level="3"`.
Making sure division of integers remains an integer.
Added package requirements.
@roohy
Copy link

roohy commented May 11, 2024

Hi,
May I ask which version of Python/Cython you ended up using. From the pull request, I thought your modifications enabled the use of Cython==3.0.2. However, the version in the requirements file is older. So my questions is, what are the specific versions of all software in case someone uses your pull request?
I am trying to make a Docker container for this software.

@shz9
Copy link
Author

shz9 commented May 13, 2024

Hey,

It's been a while, but I think the current version should work with both Cython 0.29 and Cython 3.0. As I mentioned in the discussion, the main fix was to make the code compatible with latest versions of python3. Maybe it's better to relax the requirements file to say

cython>=0.29

Or you could remove the version number altogether and grab the latest available from PIP:

cython

I haven't tested that thoroughly, so you may need to do some basic tests on your end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants