Skip to content

Commit

Permalink
Fix some issues noted by flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
benmwebb committed Dec 18, 2020
1 parent 0018b3a commit 4dc202f
Show file tree
Hide file tree
Showing 11 changed files with 500 additions and 358 deletions.
139 changes: 91 additions & 48 deletions pyext/mdt/__init__.py

Large diffs are not rendered by default.

81 changes: 74 additions & 7 deletions pyext/mdt/features.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions test/mdt_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
from modeller.test import ModellerTest
import modeller
import mdt
Expand All @@ -7,6 +6,7 @@
if sys.version_info[0] >= 3:
from functools import reduce


class MDTTest(ModellerTest):

def get_mdt_library(self, **vars):
Expand Down Expand Up @@ -48,7 +48,7 @@ def assertMDTDataEqual(self, mdt1, mdt2):
while self.roll_inds(inds, mdt1.shape, mdt1.offset):
npoints += 1
self.assertAlmostEqual(mdt1[inds], mdt2[inds], places=3)
self.assertEqual(npoints, reduce(lambda x,y: x*y, shape))
self.assertEqual(npoints, reduce(lambda x, y: x*y, shape))

def assertMDTsEqual(self, mdt1, mdt2, check_pdf=True):
"""Make sure that two MDTs are equal"""
Expand Down
11 changes: 9 additions & 2 deletions test/run-all-tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import print_function
import unittest, sys, os, re
import unittest
import sys
import os
import re
from optparse import OptionParser
import glob

Expand All @@ -24,6 +27,7 @@ def getDescription(self, test):
return doc_first_line
else:
return str(test)

def _makeResult(self):
return self._TestResult(self.stream, self.descriptions,
self.verbosity)
Expand Down Expand Up @@ -83,9 +87,11 @@ def regressionTest():
modnames = [os.path.splitext(f)[0] for f in files]

modobjs = [__import__(m) for m in modnames]
tests = [unittest.defaultTestLoader.loadTestsFromModule(o) for o in modobjs]
tests = [unittest.defaultTestLoader.loadTestsFromModule(o)
for o in modobjs]
return unittest.TestSuite(tests)


def parse_options():
parser = OptionParser()
parser.add_option("-v", dest="verbose", action='store_true',
Expand All @@ -95,6 +101,7 @@ def parse_options():
help="directory to write HTML coverage info into")
return parser.parse_args()


if __name__ == "__main__":
opts, args = parse_options()
sys.argv = [sys.argv[0]] + args
Expand Down
26 changes: 14 additions & 12 deletions test/test_bondlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import sys


class BondLibTests(MDTTest):

def get_test_mdt(self, mlib, features):
Expand All @@ -26,15 +27,15 @@ def test_write_bondlib(self):
mlib.bond_classes.read('data/bndgrp.lib')

bondtype = mdt.features.BondType(mlib)
bondlen = mdt.features.BondLength(mlib,
bins=mdt.uniform_bins(200, 1.0, 0.005))
bondlen = mdt.features.BondLength(
mlib, bins=mdt.uniform_bins(200, 1.0, 0.005))
m = self.get_test_mdt(mlib, [bondtype, bondlen])
with open('test.out', 'w') as fh:
mdt.write_bondlib(fh, m)

# Make sure that valid Python code was produced
with open('test.out') as fh:
code = compile(fh.read(), 'test.out', 'exec')
_ = compile(fh.read(), 'test.out', 'exec')
os.unlink('test.out')

def test_bad_bin_name_write_bondlib(self):
Expand All @@ -61,7 +62,7 @@ def test_write_anglelib(self):

# Make sure that valid Python code was produced
with open('test.out') as fh:
code = compile(fh.read(), 'test.out', 'exec')
_ = compile(fh.read(), 'test.out', 'exec')
os.unlink('test.out')

def test_write_improperlib(self):
Expand All @@ -77,23 +78,23 @@ def test_write_improperlib(self):

# Make sure that valid Python code was produced
with open('test.out') as fh:
code = compile(fh.read(), 'test.out', 'exec')
_ = compile(fh.read(), 'test.out', 'exec')
os.unlink('test.out')

def test_write_splinelib(self):
"""Test the write_splinelib function"""
mlib = self.get_mdt_library()

restype = mdt.features.ResidueType(mlib)
ang = mdt.features.Chi1Dihedral(mlib,
bins=mdt.uniform_bins(180, -180.0, 4.0))
ang = mdt.features.Chi1Dihedral(
mlib, bins=mdt.uniform_bins(180, -180.0, 4.0))
m = self.get_test_mdt(mlib, [restype, ang])
with open('test.out', 'w') as fh:
mdt.write_splinelib(fh, m, 'chi1')

# Make sure that valid Python code was produced
with open('test.out') as fh:
code = compile(fh.read(), 'test.out', 'exec')
_ = compile(fh.read(), 'test.out', 'exec')
os.unlink('test.out')

def test_write_2dsplinelib(self):
Expand All @@ -111,7 +112,7 @@ def test_write_2dsplinelib(self):

# Make sure that valid Python code was produced
with open('test.out') as fh:
code = compile(fh.read(), 'test.out', 'exec')
_ = compile(fh.read(), 'test.out', 'exec')
os.unlink('test.out')

def test_write_statpot(self):
Expand All @@ -123,9 +124,9 @@ def test_write_statpot(self):
d = mdt.features.AtomDistance(mlib, bins=mdt.uniform_bins(5, 0, 0.5))
a1 = mdt.features.AtomType(mlib)
a2 = mdt.features.AtomType(mlib, pos2=True)
m = mdt.Table(mlib, features=(d,a1,a2))
m = mdt.Table(mlib, features=(d, a1, a2))
# Remove undefined bin
m = m.reshape((d,a1,a2), m.offset, (-1, -1, -1))
m = m.reshape((d, a1, a2), m.offset, (-1, -1, -1))
with open('test.out', 'w') as fh:
mdt.write_statpot(fh, m)
# Check size of file
Expand All @@ -134,9 +135,10 @@ def test_write_statpot(self):
self.assertEqual(len(lines), 7)
self.assertEqual(len(lines[-1].split()), 21)
# Make sure Modeller can read the file
g = modeller.group_restraints(env, 'test/data/atmcls-tiny.lib',
_ = modeller.group_restraints(env, 'test/data/atmcls-tiny.lib',
'test.out')
os.unlink('test.out')


if __name__ == '__main__':
unittest.main()
69 changes: 37 additions & 32 deletions test/test_bondsep.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import modeller
import os


class BondSeparationFeatureTests(MDTTest):

def build_mdt_from_sequence(self, mlib, features, seq, **keys):
Expand All @@ -29,24 +30,25 @@ def get_all_libraries(self):
def test_chain_span(self):
"""Atom pairs spanning chains should not be connected"""
mlib = self.get_all_libraries()
bsep = mdt.features.AtomBondSeparation(mlib,
bins=mdt.uniform_bins(20, 0, 1.0))
bsep = mdt.features.AtomBondSeparation(
mlib, bins=mdt.uniform_bins(20, 0, 1.0))
m = self.build_mdt_from_sequence(mlib, bsep, 'C/C',
residue_span_range=(-999,-1,1,999))
residue_span_range=(-999, -1, 1, 999))
# All atom pairs should be undefined, since chains are not connected
self.assertEqual(m.sample_size, 49.0)
self.assertEqual(m[-1], 49.0)

def test_unknown_atom_type(self):
"""Atoms of unknown type should count as undefined"""
mlib = self.get_all_libraries()
bsep = mdt.features.AtomBondSeparation(mlib,
bins=mdt.uniform_bins(10, -1000, 1000.0))
bsep = mdt.features.AtomBondSeparation(
mlib, bins=mdt.uniform_bins(10, -1000, 1000.0))
m = self.build_mdt_from_sequence(mlib, bsep, 'C',
residue_span_range=(0,0,0,0))
# The OXT atom is not referenced in the bond library, so bond separation
# to any of the other 6 atoms should count as undefined (even though
# the raw "distance" of -1 would otherwise fall in the first bin)
residue_span_range=(0, 0, 0, 0))
# The OXT atom is not referenced in the bond library, so bond
# separation to any of the other 6 atoms should count as undefined
# (even though the raw "distance" of -1 would otherwise fall in the
# first bin)
self.assertEqual(m[-1], 6.0)

def test_unconnected_internal(self):
Expand All @@ -67,10 +69,10 @@ def test_unconnected_internal(self):

attyp1 = mdt.features.AtomType(mlib)
attyp2 = mdt.features.AtomType(mlib, pos2=True)
bsep = mdt.features.AtomBondSeparation(mlib,
bins=mdt.uniform_bins(20, 0, 1.0))
m = self.build_mdt_from_sequence(mlib, [attyp1, attyp2, bsep],
'A', residue_span_range=(0,0,0,0))
bsep = mdt.features.AtomBondSeparation(
mlib, bins=mdt.uniform_bins(20, 0, 1.0))
m = self.build_mdt_from_sequence(
mlib, [attyp1, attyp2, bsep], 'A', residue_span_range=(0, 0, 0, 0))
atom_types = {}
for n, b in enumerate(m.features[0].bins):
atom_types[b.symbol] = n
Expand Down Expand Up @@ -105,10 +107,11 @@ def test_unconnected_external(self):

attyp1 = mdt.features.AtomType(mlib)
attyp2 = mdt.features.AtomType(mlib, pos2=True)
bsep = mdt.features.AtomBondSeparation(mlib,
bins=mdt.uniform_bins(20, 0, 1.0))
m = self.build_mdt_from_sequence(mlib, [attyp1, attyp2, bsep],
'AA', residue_span_range=(-1,-1,1,1))
bsep = mdt.features.AtomBondSeparation(
mlib, bins=mdt.uniform_bins(20, 0, 1.0))
m = self.build_mdt_from_sequence(
mlib, [attyp1, attyp2, bsep], 'AA',
residue_span_range=(-1, -1, 1, 1))
atom_types = {}
for n, b in enumerate(m.features[0].bins):
atom_types[b.symbol] = n
Expand All @@ -133,10 +136,11 @@ def test_internal(self):
mlib = self.get_all_libraries()
attyp1 = mdt.features.AtomType(mlib)
attyp2 = mdt.features.AtomType(mlib, pos2=True)
bsep = mdt.features.AtomBondSeparation(mlib,
bins=mdt.uniform_bins(20, 0, 1.0))
m = self.build_mdt_from_sequence(mlib, [attyp1, attyp2, bsep],
'ARNDCQEHFWY', residue_span_range=(0,0,0,0))
bsep = mdt.features.AtomBondSeparation(
mlib, bins=mdt.uniform_bins(20, 0, 1.0))
m = self.build_mdt_from_sequence(
mlib, [attyp1, attyp2, bsep], 'ARNDCQEHFWY',
residue_span_range=(0, 0, 0, 0))
atom_types = {}
for n, b in enumerate(m.features[0].bins):
atom_types[b.symbol] = n
Expand Down Expand Up @@ -179,10 +183,11 @@ def test_external(self):
mlib = self.get_all_libraries()
attyp1 = mdt.features.AtomType(mlib)
attyp2 = mdt.features.AtomType(mlib, pos2=True)
bsep = mdt.features.AtomBondSeparation(mlib,
bins=mdt.uniform_bins(20, 0, 1.0))
m = self.build_mdt_from_sequence(mlib, [attyp1, attyp2, bsep],
'ARN', residue_span_range=(-999,-1,1,999))
bsep = mdt.features.AtomBondSeparation(
mlib, bins=mdt.uniform_bins(20, 0, 1.0))
m = self.build_mdt_from_sequence(
mlib, [attyp1, attyp2, bsep], 'ARN',
residue_span_range=(-999, -1, 1, 999))
atom_types = {}
for n, b in enumerate(m.features[0].bins):
atom_types[b.symbol] = n
Expand All @@ -204,11 +209,10 @@ def assertBondSep(at1, at2, numbond, sep):
def test_disulfide(self):
"""Test handling of disulfide bonds"""
mlib = self.get_all_libraries()
bsep = mdt.features.AtomBondSeparation(mlib,
bins=mdt.uniform_bins(20, 0, 1.0))
bsep_ss = mdt.features.AtomBondSeparation(mlib,
bins=mdt.uniform_bins(20, 0, 1.0),
disulfide=True)
bsep = mdt.features.AtomBondSeparation(
mlib, bins=mdt.uniform_bins(20, 0, 1.0))
bsep_ss = mdt.features.AtomBondSeparation(
mlib, bins=mdt.uniform_bins(20, 0, 1.0), disulfide=True)
env = self.get_environ()
mdl = modeller.model(env)
mdl.build_sequence('CC')
Expand All @@ -224,11 +228,12 @@ def test_disulfide(self):
a = modeller.alignment(env)
a.append_model(mdl, atom_files='test', align_codes='test')
m = mdt.Table(mlib, features=bsep)
m.add_alignment(a, residue_span_range=(-999,0,0,999))
m.add_alignment(a, residue_span_range=(-999, 0, 0, 999))
self.assertEqual(m[1], 11.0)
m2 = mdt.Table(mlib, features=bsep_ss)
m2.add_alignment(a, residue_span_range=(-999,0,0,999))
m2.add_alignment(a, residue_span_range=(-999, 0, 0, 999))
self.assertEqual(m2[1], num)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4dc202f

Please sign in to comment.