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

Updating DNA2Vec to work with Python 3.7, and to work on Windows 10. #15

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
inputs/hg38/
results/
tmp/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down Expand Up @@ -90,3 +91,7 @@ ENV/

# Rope project settings
.ropeproject

test.py

data/
5 changes: 3 additions & 2 deletions attic_util/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random
import string
import resource
# import resource
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of commenting this out, you can use the try and catch ImportError pattern: https://stackoverflow.com/questions/3131217/error-handling-when-importing-modules

You can move this within memory_usage

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll work on it when I get the chance.

import logbook
import arrow
import numpy as np
Expand All @@ -26,7 +26,8 @@ def random_str(N):
return ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(N))

def memory_usage():
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1E6
# return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1E6
return 0

def estimate_bytes(filenames):
return sum([os.stat(f).st_size for f in filenames])
Expand Down
12 changes: 8 additions & 4 deletions dna2vec/multi_k_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
import tempfile
import numpy as np

from gensim.models import word2vec
# from gensim.models import word2vec
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just delete this.

from gensim import matutils

import gensim
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The train_dna2vec.py also imports word2vec. Would the upgrade of gensim break the training of that file? Have you try training?


class SingleKModel:
def __init__(self, model):
self.model = model
self.vocab_lst = sorted(model.vocab.keys())

class MultiKModel:
def __init__(self, filepath):
self.aggregate = word2vec.Word2Vec.load_word2vec_format(filepath, binary=False)
self.aggregate = gensim.models.KeyedVectors.load_word2vec_format(filepath, binary=False)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pnpnpn I'm not sure if this is actually broken in python 3.7, but Word2Vec has definitely been deprecated so this is probably still a good idea.

self.logger = logbook.Logger(self.__class__.__name__)

vocab_lens = [len(vocab) for vocab in self.aggregate.vocab.keys()]
Expand All @@ -25,6 +27,7 @@ def __init__(self, filepath):
self.data = {}
for k in range(self.k_low, self.k_high + 1):
self.data[k] = self.separate_out_model(k)
print(len(self.data))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for debugging? Please remove.


def model(self, k_len):
"""
Expand All @@ -50,10 +53,11 @@ def separate_out_model(self, k_len):
self.logger.warn('Missing {}-mers: {} / {}'.format(k_len, len(vocabs), 4 ** k_len))

header_str = '{} {}'.format(len(vocabs), self.vec_dim)
with tempfile.NamedTemporaryFile(mode='w') as fptr:
with tempfile.NamedTemporaryFile(mode='w', delete=False) as fptr:
print(header_str, file=fptr)
for vocab in vocabs:
vec_str = ' '.join("%f" % val for val in self.aggregate[vocab])
print('{} {}'.format(vocab, vec_str), file=fptr)
fptr.flush()
return SingleKModel(word2vec.Word2Vec.load_word2vec_format(fptr.name, binary=False))
open(fptr.name, "rb")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose for this?

return SingleKModel(gensim.models.KeyedVectors.load_word2vec_format(fptr.name, binary=False))
39 changes: 19 additions & 20 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
arrow==0.8.0
biopython==1.68
boto==2.46.1
bz2file==0.98
ConfigArgParse==0.11.0
gensim==0.13.2
Logbook==1.0.0
numpy==1.16
pep8==1.7.0
pluggy==0.4.0
py==1.4.33
pytest==3.0.7
python-dateutil==2.6.0
requests==2.20.0
scipy==0.19.0
six==1.10.0
smart-open==1.5.1
tox==2.7.0
tox-pyenv==1.0.3
virtualenv==15.1.0
arrow
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please pin the versions. This will install the latest versions, which may break the code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was one of the issues when moving up to Python 3.7. Several of the older versions of the libraries were incompatible with Python 3.7, so installing the latest versions would definitely reduce incompatibilities.

Copy link
Owner

@pnpnpn pnpnpn May 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that these older versions are incompatible with python 3.7. A couple of months from now, when there are upgrades to these libraries, the API for the new versions may not be backward-compatible to the current code.

Pinning the versions to new version numbers ensure that this is at least compatible to the version you tested on, which is python 3.7. You can see the versions of your current environment with pip freeze

biopython
biopython
bz2file
ConfigArgParse
gensim
Logbook
numpy
pep8
pluggy
py
pytest
python-dateutil
requests
six
smart-open
tox
tox-pyenv
virtualenv
4 changes: 2 additions & 2 deletions scripts/train_dna2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def train(self, kmer_seq_generator):

def write_vec(self):
out_filename = '{}.w2v'.format(self.out_fileroot)
self.model.wv.save_word2vec_format(out_filename, binary=False)
self.model.wv.save_word2vec_format("."+out_filename, binary=False)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you prepend with .? This would make it a hidden file in linux.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must've not noticed that change as I was working on Windows. I'll change it in the latest push.


def run_main(args, inputs, out_fileroot):
logbook.info(' '.join(sys.argv))
Expand Down Expand Up @@ -132,7 +132,7 @@ def main():
args.kmer_fragmenter))

out_txt_filename = '{}.txt'.format(out_fileroot)
with open(out_txt_filename, 'w') as summary_fptr:
with open("."+out_txt_filename, 'w+') as summary_fptr:
with Tee(summary_fptr):
logbook.StreamHandler(sys.stdout, level=log_level).push_application()
redirect_logging()
Expand Down