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 all 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/
14 changes: 12 additions & 2 deletions attic_util/util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import random
import string
import resource
import logbook
import arrow
import numpy as np
import os

importedResource = False

try:
import resource
importedResource = True
except ImportError:
pass

def split_Xy(df, y_colname='label'):
X = df.drop([y_colname], axis=1)
y = df[y_colname]
Expand All @@ -26,7 +33,10 @@ 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
if(importedResource):
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1E6
else:
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))
36 changes: 17 additions & 19 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
arrow==0.8.0
biopython==1.68
boto==2.46.1
arrow==0.15.6
biopython==1.76
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
ConfigArgParse==1.2.3
gensim==3.8.3
Logbook==1.5.3
numpy==1.18.1
pep8==1.7.1
pluggy==0.13.1
py==1.8.1
pytest==5.4.2
python-dateutil==2.8.1
requests==2.23.0
six==1.14.0
smart-open==2.0.0
tox==3.15.0
tox-pyenv==1.1.0
virtualenv==20.0.20
2 changes: 1 addition & 1 deletion scripts/train_dna2vec.py
Original file line number Diff line number Diff line change
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:
Copy link
Owner

Choose a reason for hiding this comment

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

Why the w+ change?

with Tee(summary_fptr):
logbook.StreamHandler(sys.stdout, level=log_level).push_application()
redirect_logging()
Expand Down