Improve Python 3 compatibility of pizza tools and simplify read_snapshot code
This commit is contained in:
@ -146,6 +146,8 @@ d.extra(data) extract bond/tri/line list from data
|
|||||||
# History
|
# History
|
||||||
# 8/05, Steve Plimpton (SNL): original version
|
# 8/05, Steve Plimpton (SNL): original version
|
||||||
# 12/09, David Hart (SNL): allow use of NumPy or Numeric
|
# 12/09, David Hart (SNL): allow use of NumPy or Numeric
|
||||||
|
# 03/17, Richard Berger (Temple U): improve Python 3 compatibility,
|
||||||
|
# simplify read_snapshot by using reshape
|
||||||
|
|
||||||
# ToDo list
|
# ToDo list
|
||||||
# try to optimize this line in read_snap: words += f.readline().split()
|
# try to optimize this line in read_snap: words += f.readline().split()
|
||||||
@ -224,7 +226,7 @@ class dump:
|
|||||||
self.flist = []
|
self.flist = []
|
||||||
for word in words: self.flist += glob.glob(word)
|
for word in words: self.flist += glob.glob(word)
|
||||||
if len(self.flist) == 0 and len(list) == 1:
|
if len(self.flist) == 0 and len(list) == 1:
|
||||||
raise StandardError("no dump file specified")
|
raise Exception("no dump file specified")
|
||||||
|
|
||||||
if len(list) == 1:
|
if len(list) == 1:
|
||||||
self.increment = 0
|
self.increment = 0
|
||||||
@ -299,7 +301,7 @@ class dump:
|
|||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
|
|
||||||
if not self.increment: raise StandardError("cannot read incrementally")
|
if not self.increment: raise Exception("cannot read incrementally")
|
||||||
|
|
||||||
# read next snapshot in current file using eof as pointer
|
# read next snapshot in current file using eof as pointer
|
||||||
# if fail, try next file
|
# if fail, try next file
|
||||||
@ -344,13 +346,13 @@ class dump:
|
|||||||
try:
|
try:
|
||||||
snap = Snap()
|
snap = Snap()
|
||||||
item = f.readline()
|
item = f.readline()
|
||||||
snap.time = int(f.readline().split()[0]) # just grab 1st field
|
snap.time = int(f.readline().decode().split()[0]) # just grab 1st field
|
||||||
item = f.readline()
|
item = f.readline()
|
||||||
snap.natoms = int(f.readline())
|
snap.natoms = int(f.readline().decode())
|
||||||
|
|
||||||
snap.aselect = np.zeros(snap.natoms)
|
snap.aselect = np.zeros(snap.natoms)
|
||||||
|
|
||||||
item = f.readline()
|
item = f.readline().decode()
|
||||||
words = f.readline().split()
|
words = f.readline().split()
|
||||||
snap.xlo,snap.xhi = float(words[0]),float(words[1])
|
snap.xlo,snap.xhi = float(words[0]),float(words[1])
|
||||||
words = f.readline().split()
|
words = f.readline().split()
|
||||||
@ -358,7 +360,7 @@ class dump:
|
|||||||
words = f.readline().split()
|
words = f.readline().split()
|
||||||
snap.zlo,snap.zhi = float(words[0]),float(words[1])
|
snap.zlo,snap.zhi = float(words[0]),float(words[1])
|
||||||
|
|
||||||
item = f.readline()
|
item = f.readline().decode()
|
||||||
if len(self.names) == 0:
|
if len(self.names) == 0:
|
||||||
words = item.split()[2:]
|
words = item.split()[2:]
|
||||||
if len(words):
|
if len(words):
|
||||||
@ -372,24 +374,22 @@ class dump:
|
|||||||
else: self.names[words[i]] = i
|
else: self.names[words[i]] = i
|
||||||
|
|
||||||
if snap.natoms:
|
if snap.natoms:
|
||||||
words = f.readline().split()
|
words = f.readline().decode().split()
|
||||||
ncol = len(words)
|
ncol = len(words)
|
||||||
for i in range(1,snap.natoms):
|
for i in range(1,snap.natoms):
|
||||||
words += f.readline().split()
|
words += f.readline().decode().split()
|
||||||
floats = map(float,words)
|
floats = map(float,words)
|
||||||
if oldnumeric: atoms = np.zeros((snap.natoms,ncol),np.Float)
|
if oldnumeric:
|
||||||
else: atoms = np.zeros((snap.natoms,ncol),np.float)
|
atom_data = np.array(list(floats),np.Float)
|
||||||
start = 0
|
else:
|
||||||
stop = ncol
|
atom_data = np.array(list(floats),np.float)
|
||||||
for i in range(snap.natoms):
|
|
||||||
atoms[i] = floats[start:stop]
|
snap.atoms = atom_data.reshape((snap.natoms, ncol))
|
||||||
start = stop
|
else:
|
||||||
stop += ncol
|
snap.atoms = None
|
||||||
else: atoms = None
|
|
||||||
snap.atoms = atoms
|
|
||||||
return snap
|
return snap
|
||||||
except:
|
except:
|
||||||
return 0
|
return None
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# decide if snapshot i is scaled/unscaled from coords of first and last atom
|
# decide if snapshot i is scaled/unscaled from coords of first and last atom
|
||||||
@ -417,7 +417,7 @@ class dump:
|
|||||||
|
|
||||||
def map(self,*pairs):
|
def map(self,*pairs):
|
||||||
if len(pairs) % 2 != 0:
|
if len(pairs) % 2 != 0:
|
||||||
raise StandardError("dump map() requires pairs of mappings")
|
raise Exception("dump map() requires pairs of mappings")
|
||||||
for i in range(0,len(pairs),2):
|
for i in range(0,len(pairs),2):
|
||||||
j = i + 1
|
j = i + 1
|
||||||
self.names[pairs[j]] = pairs[i]-1
|
self.names[pairs[j]] = pairs[i]-1
|
||||||
@ -734,7 +734,7 @@ class dump:
|
|||||||
for snap in self.snaps:
|
for snap in self.snaps:
|
||||||
if not snap.tselect: continue
|
if not snap.tselect: continue
|
||||||
if snap.nselect != len(vec):
|
if snap.nselect != len(vec):
|
||||||
raise StandardError("vec length does not match # of selected atoms")
|
raise Exception("vec length does not match # of selected atoms")
|
||||||
atoms = snap.atoms
|
atoms = snap.atoms
|
||||||
m = 0
|
m = 0
|
||||||
for i in range(snap.natoms):
|
for i in range(snap.natoms):
|
||||||
@ -800,7 +800,7 @@ class dump:
|
|||||||
|
|
||||||
def atom(self,n,*list):
|
def atom(self,n,*list):
|
||||||
if len(list) == 0:
|
if len(list) == 0:
|
||||||
raise StandardError("no columns specified")
|
raise Exception("no columns specified")
|
||||||
columns = []
|
columns = []
|
||||||
values = []
|
values = []
|
||||||
for name in list:
|
for name in list:
|
||||||
@ -816,7 +816,7 @@ class dump:
|
|||||||
for i in range(snap.natoms):
|
for i in range(snap.natoms):
|
||||||
if atoms[i][id] == n: break
|
if atoms[i][id] == n: break
|
||||||
if atoms[i][id] != n:
|
if atoms[i][id] != n:
|
||||||
raise StandardError("could not find atom ID in snapshot")
|
raise Exception("could not find atom ID in snapshot")
|
||||||
for j in range(ncol):
|
for j in range(ncol):
|
||||||
values[j][m] = atoms[i][columns[j]]
|
values[j][m] = atoms[i][columns[j]]
|
||||||
m += 1
|
m += 1
|
||||||
@ -831,7 +831,7 @@ class dump:
|
|||||||
snap = self.snaps[self.findtime(n)]
|
snap = self.snaps[self.findtime(n)]
|
||||||
|
|
||||||
if len(list) == 0:
|
if len(list) == 0:
|
||||||
raise StandardError("no columns specified")
|
raise Exception("no columns specified")
|
||||||
columns = []
|
columns = []
|
||||||
values = []
|
values = []
|
||||||
for name in list:
|
for name in list:
|
||||||
@ -957,9 +957,9 @@ class dump:
|
|||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
|
||||||
def findtime(self,n):
|
def findtime(self,n):
|
||||||
for i in range(self.nsnaps):
|
for i, snap in enumerate(self.snaps):
|
||||||
if self.snaps[i].time == n: return i
|
if snap.time == n: return i
|
||||||
raise StandardError("no step %d exists" % n)
|
raise Exception("no step %d exists" % n)
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# return maximum box size across all selected snapshots
|
# return maximum box size across all selected snapshots
|
||||||
@ -1008,7 +1008,7 @@ class dump:
|
|||||||
nbonds = int(f.readline())
|
nbonds = int(f.readline())
|
||||||
item = f.readline()
|
item = f.readline()
|
||||||
if not re.search("BONDS",item):
|
if not re.search("BONDS",item):
|
||||||
raise StandardError("could not read bonds from dump file")
|
raise Exception("could not read bonds from dump file")
|
||||||
|
|
||||||
words = f.readline().split()
|
words = f.readline().split()
|
||||||
ncol = len(words)
|
ncol = len(words)
|
||||||
@ -1031,7 +1031,7 @@ class dump:
|
|||||||
self.bondflag = 1
|
self.bondflag = 1
|
||||||
self.bondlist = bondlist
|
self.bondlist = bondlist
|
||||||
except:
|
except:
|
||||||
raise StandardError("could not read from bond dump file")
|
raise Exception("could not read from bond dump file")
|
||||||
|
|
||||||
# request bonds from data object
|
# request bonds from data object
|
||||||
|
|
||||||
@ -1047,7 +1047,7 @@ class dump:
|
|||||||
self.bondflag = 1
|
self.bondflag = 1
|
||||||
self.bondlist = bondlist
|
self.bondlist = bondlist
|
||||||
except:
|
except:
|
||||||
raise StandardError("could not extract bonds from data object")
|
raise Exception("could not extract bonds from data object")
|
||||||
|
|
||||||
# request tris/lines from cdata object
|
# request tris/lines from cdata object
|
||||||
|
|
||||||
@ -1061,7 +1061,7 @@ class dump:
|
|||||||
self.lineflag = 1
|
self.lineflag = 1
|
||||||
self.linelist = lines
|
self.linelist = lines
|
||||||
except:
|
except:
|
||||||
raise StandardError("could not extract tris/lines from cdata object")
|
raise Exception("could not extract tris/lines from cdata object")
|
||||||
|
|
||||||
# request tris from mdump object
|
# request tris from mdump object
|
||||||
|
|
||||||
@ -1070,10 +1070,10 @@ class dump:
|
|||||||
self.triflag = 2
|
self.triflag = 2
|
||||||
self.triobj = arg
|
self.triobj = arg
|
||||||
except:
|
except:
|
||||||
raise StandardError("could not extract tris from mdump object")
|
raise Exception("could not extract tris from mdump object")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise StandardError("unrecognized argument to dump.extra()")
|
raise Exception("unrecognized argument to dump.extra()")
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@ -54,6 +54,7 @@ index,time,flag = p.iterator(1)
|
|||||||
|
|
||||||
# History
|
# History
|
||||||
# 8/05, Steve Plimpton (SNL): original version
|
# 8/05, Steve Plimpton (SNL): original version
|
||||||
|
# 3/17, Richard Berger (Temple U): improve Python 3 compatibility
|
||||||
|
|
||||||
# ToDo list
|
# ToDo list
|
||||||
# for generic PDB file (no template) from a LJ unit system,
|
# for generic PDB file (no template) from a LJ unit system,
|
||||||
@ -68,6 +69,12 @@ index,time,flag = p.iterator(1)
|
|||||||
# Imports and external programs
|
# Imports and external programs
|
||||||
|
|
||||||
import sys, types, glob, urllib
|
import sys, types, glob, urllib
|
||||||
|
PY3 = sys.version_info[0] == 3
|
||||||
|
|
||||||
|
if PY3:
|
||||||
|
string_types = str,
|
||||||
|
else:
|
||||||
|
string_types = basestring
|
||||||
|
|
||||||
# Class definition
|
# Class definition
|
||||||
|
|
||||||
@ -77,7 +84,7 @@ class pdbfile:
|
|||||||
|
|
||||||
def __init__(self,*args):
|
def __init__(self,*args):
|
||||||
if len(args) == 1:
|
if len(args) == 1:
|
||||||
if type(args[0]) is types.StringType:
|
if type(args[0]) is string_types:
|
||||||
filestr = args[0]
|
filestr = args[0]
|
||||||
self.data = None
|
self.data = None
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user