Skip to content

Commit

Permalink
autopep8 - commands were: libtbx.pip install autopep8; libtbx.refresh…
Browse files Browse the repository at this point in the history
…; libtbx.autopep8 --in-place -a -a ./test/test_end_to_end.py

./test/test_resource_access.py
./fast_dp/image_names.py
./fast_dp/fast_rdp.py
./fast_dp/merge.py
./fast_dp/autoindex.py
./fast_dp/pointgroup.py
./fast_dp/cell_spacegroup.py
./fast_dp/__init__.py
./fast_dp/logger.py
./fast_dp/integrate.py
./fast_dp/image_readers.py
./fast_dp/xds_reader.py
./fast_dp/diff.py
./fast_dp/scale.py
./fast_dp/output.py
./fast_dp/fast_dp.py
./fast_dp/run_job.py
./fast_dp/pointless_reader.py
./setup.py; run tests on code
  • Loading branch information
graeme-winter committed Nov 21, 2018
1 parent 2b3f3eb commit af0e99c
Show file tree
Hide file tree
Showing 19 changed files with 380 additions and 301 deletions.
29 changes: 17 additions & 12 deletions fast_dp/autoindex.py
Expand Up @@ -10,7 +10,8 @@

from fast_dp.logger import write

#TODO add pytests for this method
# TODO add pytests for this method


def add_spot_range(xds_inp):
start, end = map(int, xds_inp['DATA_RANGE'].split())
Expand All @@ -27,7 +28,7 @@ def add_spot_range(xds_inp):
spot_ranges.append('%d %d' % (start, start + wedge - 1))
spot_ranges.append('%d %d' % (half, half + wedge - 1))
spot_ranges.append('%d %d' % (int(90.0 / osc) + start,
int(90.0 / osc) + start + wedge - 1))
int(90.0 / osc) + start + wedge - 1))
else:
half = int((start + end - wedge) / 2)
spot_ranges.append('%d %d' % (start, start + wedge - 1))
Expand All @@ -38,8 +39,9 @@ def add_spot_range(xds_inp):

return xds_inp


def segment_text(xds_inp):
if not 'SEGMENT' in xds_inp:
if 'SEGMENT' not in xds_inp:
return ''

result = []
Expand All @@ -49,11 +51,12 @@ def segment_text(xds_inp):
for j in range(n):
for k in 'SEGMENT', 'SEGMENT_DISTANCE', \
'SEGMENT_ORGX', 'SEGMENT_ORGY', \
'DIRECTION_OF_SEGMENT_X-AXIS', 'DIRECTION_OF_SEGMENT_Y-AXIS':
'DIRECTION_OF_SEGMENT_X-AXIS', 'DIRECTION_OF_SEGMENT_Y-AXIS':
result.append('%s=%s' % (k, xds_inp[k][j]))

return '\n'.join(result)


def autoindex(xds_inp, input_cell=None):
'''Perform the autoindexing, using metatdata, get a list of possible
lattices and record / return the triclinic cell constants (get these from
Expand All @@ -68,7 +71,7 @@ def autoindex(xds_inp, input_cell=None):
if 'SEGMENT' in k:
continue
v = xds_inp[k]
if type(v) == list:
if isinstance(v, list):
for _v in v:
fout.write('%s=%s\n' % (k, _v))
else:
Expand All @@ -78,7 +81,7 @@ def autoindex(xds_inp, input_cell=None):

if input_cell:
fout.write('SPACE_GROUP_NUMBER=1\n')
fout.write('UNIT_CELL_CONSTANTS=%f %f %f %f %f %f\n' % \
fout.write('UNIT_CELL_CONSTANTS=%f %f %f %f %f %f\n' %
tuple(input_cell))

fout.write('JOB=XYCORR INIT COLSPOT IDXREF\n')
Expand All @@ -98,8 +101,10 @@ def autoindex(xds_inp, input_cell=None):
for step in ['XYCORR', 'INIT', 'COLSPOT', 'IDXREF']:
lastrecord = open('%s.LP' % step).readlines()[-1]
if '!!! ERROR !!!' in lastrecord:
raise RuntimeError('error in %s: %s' % \
(step, lastrecord.replace('!!! ERROR !!!', '').strip()))
raise RuntimeError(
'error in %s: %s' %
(step, lastrecord.replace(
'!!! ERROR !!!', '').strip()))

results = read_xds_idxref_lp('IDXREF.LP')

Expand All @@ -108,20 +113,20 @@ def autoindex(xds_inp, input_cell=None):
# fixed

write('All autoindexing results:')
write('%3s %6s %6s %6s %6s %6s %6s' % \
write('%3s %6s %6s %6s %6s %6s %6s' %
('Lattice', 'a', 'b', 'c', 'alpha', 'beta', 'gamma'))

for r in reversed(sorted(results)):
if not type(r) == type(1):
if not isinstance(r, type(1)):
continue
cell = results[r][1]
write('%7s %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f' % \
write('%7s %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f' %
(spacegroup_to_lattice(r), cell[0], cell[1], cell[2],
cell[3], cell[4], cell[5]))

# should probably print this for debuging

try:
return results[1][1]
except:
except BaseException:
raise RuntimeError('getting P1 cell for autoindex')
76 changes: 47 additions & 29 deletions fast_dp/cell_spacegroup.py
Expand Up @@ -8,14 +8,15 @@
from cctbx.uctbx import unit_cell
from cctbx.crystal import symmetry


def ersatz_pointgroup(spacegroup_name):
'''Guess the pointgroup for the spacegroup by mapping from short to
long name, then taking 1st character from each block.'''

pg = None

for record in open(
os.path.join(os.environ['CLIBD'], 'symop.lib'), 'r').readlines():
os.path.join(os.environ['CLIBD'], 'symop.lib'), 'r').readlines():
if ' ' in record[:1]:
continue
if spacegroup_name == record.split()[3]:
Expand All @@ -40,6 +41,7 @@ def ersatz_pointgroup(spacegroup_name):

return result


def spacegroup_to_lattice(input_spacegroup):
''' This generates a lattics from a the imported file bu chopping off
the first letter of the cell type, changing to lowercase and then
Expand All @@ -50,47 +52,56 @@ def fix_hH(lattice):
return lattice
return 'hR'

mapping = {'TRICLINIC':'a',
'MONOCLINIC':'m',
'ORTHORHOMBIC':'o',
'TETRAGONAL':'t',
'TRIGONAL':'h',
'HEXAGONAL':'h',
'CUBIC':'c'}
mapping = {'TRICLINIC': 'a',
'MONOCLINIC': 'm',
'ORTHORHOMBIC': 'o',
'TETRAGONAL': 't',
'TRIGONAL': 'h',
'HEXAGONAL': 'h',
'CUBIC': 'c'}

if type(input_spacegroup) == type(u''):
if isinstance(input_spacegroup, type(u'')):
input_spacegroup = str(input_spacegroup)

if type(input_spacegroup) == type(''):
if isinstance(input_spacegroup, type('')):
for record in open(
os.path.join(os.environ['CLIBD'], 'symop.lib'), 'r').readlines():
os.path.join(
os.environ['CLIBD'],
'symop.lib'),
'r').readlines():
if ' ' in record[:1]:
continue
if input_spacegroup == record.split()[3]:
return fix_hH(mapping[record.split()[5]] + record.split()[3][0])
return fix_hH(
mapping[record.split()[5]] + record.split()[3][0])

elif type(input_spacegroup) == type(0):
elif isinstance(input_spacegroup, type(0)):
for record in open(
os.path.join(os.environ['CLIBD'], 'symop.lib'), 'r').readlines():
os.path.join(
os.environ['CLIBD'],
'symop.lib'),
'r').readlines():
if ' ' in record[:1]:
continue
if input_spacegroup == int(record.split()[0]):
return fix_hH(mapping[record.split()[5]] + record.split()[3][0])
return fix_hH(
mapping[record.split()[5]] + record.split()[3][0])

else:
raise RuntimeError('bad type for input: %s' % type(input_spacegroup))

return None


def check_spacegroup_name(spacegroup_name):
'''Will return normalised name if spacegroup name is recognised,
raise exception otherwise. For checking command-line options.'''

try:
j = int(spacegroup_name)
if j > 230 or j <= 0:
raise RuntimeError('spacegroup number nonsense: %s' \
% spacegroup_name)
raise RuntimeError('spacegroup number nonsense: %s'
% spacegroup_name)
return spacegroup_number_to_name(j)

except ValueError:
Expand All @@ -99,14 +110,15 @@ def check_spacegroup_name(spacegroup_name):
found_spacegroup = None

for record in open(
os.path.join(os.environ['CLIBD'], 'symop.lib'), 'r').readlines():
os.path.join(os.environ['CLIBD'], 'symop.lib'), 'r').readlines():
if ' ' in record[:1]:
continue
if spacegroup_name == record.split()[3]:
return spacegroup_name

raise RuntimeError('spacegroup name "%s" not recognised' % spacegroup_name)


def check_split_cell(cell_string):
'''Will return tuple of floats a, b, c, alpha, beta, gamma from input
cell string which contains a,b,c,alpha,beta,gamma raising an exception
Expand All @@ -115,14 +127,15 @@ def check_split_cell(cell_string):
ideal_string = 'a,b,c,alpha,beta,gamma'

if not cell_string.count(',') == 5:
raise RuntimeError('%s should be of the form %s' % \
(cell_string, ideal_string))
raise RuntimeError('%s should be of the form %s' %
(cell_string, ideal_string))

a, b, c, alpha, beta, gamma = tuple(
map(float, cell_string.split(',')))

return a, b, c, alpha, beta, gamma


def constrain_cell(lattice_class, cell):
'''Constrain cell to fit lattice class x.'''

Expand All @@ -146,13 +159,14 @@ def constrain_cell(lattice_class, cell):

raise RuntimeError('lattice class not recognised: %s' % lattice_class)


def spacegroup_number_to_name(spg_num):
'''Convert a spacegroup number to a more readable name.'''

database = { }
database = {}

for record in open(
os.path.join(os.environ['CLIBD'], 'symop.lib'), 'r').readlines():
os.path.join(os.environ['CLIBD'], 'symop.lib'), 'r').readlines():
if ' ' in record[:1]:
continue
number = int(record.split()[0])
Expand All @@ -161,18 +175,20 @@ def spacegroup_number_to_name(spg_num):

return database[spg_num]


def lattice_to_spacegroup(lattice):
''' Converts a lattice to the spacegroup with the lowest symmetry
possible for that lattice'''
l2s = {
'aP':1, 'mP':3, 'mC':5, 'mI':5,
'oP':16, 'oC':21, 'oI':23, 'oF':22,
'tP':75, 'tI':79, 'hP':143, 'hR':146,
'hH':146, 'cP':195, 'cF':196, 'cI':197
}
'aP': 1, 'mP': 3, 'mC': 5, 'mI': 5,
'oP': 16, 'oC': 21, 'oI': 23, 'oF': 22,
'tP': 75, 'tI': 79, 'hP': 143, 'hR': 146,
'hH': 146, 'cP': 195, 'cF': 196, 'cI': 197
}

return l2s[lattice]


def lauegroup_to_lattice(lauegroup):
'''Convert a Laue group representation (from pointless, e.g. I m m m)
to something useful, like the implied crystal lattice (in this
Expand Down Expand Up @@ -224,18 +240,20 @@ def lauegroup_to_lattice(lauegroup):

return lauegroup_to_lattice[updated_laue]


def generate_primitive_cell(unit_cell_constants, space_group_name):
'''For a given set of unit cell constants and space group, determine the
corresponding primitive unit cell...'''

uc = unit_cell(unit_cell_constants)
sg = space_group(space_group_symbols(space_group_name).hall())
cs = symmetry(unit_cell = uc,
space_group = sg)
cs = symmetry(unit_cell=uc,
space_group=sg)
csp = cs.change_basis(cs.change_of_basis_op_to_primitive_setting())

return csp.unit_cell()


if __name__ == '__main__':

import sys
Expand Down
9 changes: 5 additions & 4 deletions fast_dp/diff.py
@@ -1,7 +1,8 @@
from __future__ import absolute_import, division, print_function


def XDS_INP_to_dict(inp_text):
result = { }
result = {}
for record in inp_text.split('\n'):
useful = record.split('!')[0].strip()
if not useful:
Expand All @@ -10,8 +11,8 @@ def XDS_INP_to_dict(inp_text):
# assume tokens of form key=value key=value
tokens = useful.replace('=', ' ').split()
for j in range(useful.count('=')):
key = tokens[2*j].strip()
value = tokens[2*j+1].strip()
key = tokens[2 * j].strip()
value = tokens[2 * j + 1].strip()
# handle multiples gracelessly
if key in result:
if not isinstance(result[key], list):
Expand Down Expand Up @@ -55,7 +56,7 @@ def diff(xds_inp_a, xds_inp_b):
import sys

if len(sys.argv) != 3:
raise RuntimeError, '%s XDS.INP ../other/XDS.INP' % sys.argv[1]
raise RuntimeError('%s XDS.INP ../other/XDS.INP' % sys.argv[1])

a = XDS_INP_to_dict(open(sys.argv[1]).read())
b = XDS_INP_to_dict(open(sys.argv[2]).read())
Expand Down

0 comments on commit af0e99c

Please sign in to comment.