diff options
author | Thomas White <taw@physics.org> | 2017-05-12 15:51:33 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2017-05-12 15:51:33 +0200 |
commit | 690666e32643302970b1f54bbaa0efe83aac7e86 (patch) | |
tree | cd4e9b32b46160649a1fa4f5c1786b1cbf614356 | |
parent | 4227f0b190b08ecc50a49875e86dbb9b14714bdd (diff) |
Make all Python scripts compatible with Python 2 and 3
It would be nice to upgrade completely to Python 3, but this may
create problems at SLAC because psana only supports Python 2 (Python 3
is available, but means that users have to jump through hoops to run
a simple CrystFEL script). None of our scripts do anything complicated,
so they can all be compatible with both so far.
If anyone adds a script which requires a particular version, make sure
to specify the version in the first line, consistent with PEP 394.
-rw-r--r-- | scripts/Rsplit_surface.py | 16 | ||||
-rwxr-xr-x | scripts/add-beam-params | 14 | ||||
-rwxr-xr-x | scripts/ave-resolution | 15 | ||||
-rw-r--r-- | scripts/clean-stream.py | 4 | ||||
-rwxr-xr-x | scripts/crystal-frame-number | 13 | ||||
-rwxr-xr-x | scripts/detector-shift | 16 | ||||
-rwxr-xr-x | scripts/find-pairs | 37 | ||||
-rwxr-xr-x | scripts/gaincal-to-saturation-map | 9 | ||||
-rwxr-xr-x | scripts/move-entire-detector | 16 | ||||
-rwxr-xr-x | scripts/peak-intensity | 16 | ||||
-rwxr-xr-x | scripts/peakogram-stream | 8 | ||||
-rwxr-xr-x | scripts/split-by-mask | 12 | ||||
-rwxr-xr-x | scripts/truncate-stream | 14 |
13 files changed, 100 insertions, 90 deletions
diff --git a/scripts/Rsplit_surface.py b/scripts/Rsplit_surface.py index ca68a92e..63a72cd8 100644 --- a/scripts/Rsplit_surface.py +++ b/scripts/Rsplit_surface.py @@ -1,13 +1,5 @@ #!/usr/bin/python -# coding=utf-8 - -from __future__ import division -from array import array -from numpy import * - -import matplotlib - -# Rsplit_surface.py +# -*- coding: utf-8 -*- # # Plot Rsplit as a contour map # @@ -32,6 +24,12 @@ import matplotlib # along with CrystFEL. If not, see <http://www.gnu.org/licenses/>. +from __future__ import division +from array import array +from numpy import * + +import matplotlib + """there could be problems with dependencies, in this case, """ #matplotlib.use('PS') diff --git a/scripts/add-beam-params b/scripts/add-beam-params index 23c9caaf..e4e38f70 100755 --- a/scripts/add-beam-params +++ b/scripts/add-beam-params @@ -1,13 +1,13 @@ #!/usr/bin/env python - +# -*- coding: utf-8 -*- # # Add beam parameters to stream # -# Copyright (c) 2014 Deutsches Elektronen-Synchrotron DESY, -# a research centre of the Helmholtz Association. +# Copyright © 2014-2017 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. # # Author: -# 2014 Thomas White <taw@physics.org> +# 2014,2017 Thomas White <taw@physics.org> # import sys @@ -66,7 +66,7 @@ def count_crystals(f, g, start_after, stop_after): in_crystal = 0 need_end_chunk = 1 - print "Wrote %i crystals to %s" % (n_crystals_written, opt.ofn) + print("Wrote {} crystals to {}".format(n_crystals_written, opt.ofn)) def count_chunks(f, g, start_after, stop_after): n_chunks_seen = 0 @@ -95,7 +95,7 @@ def count_chunks(f, g, start_after, stop_after): if n_chunks_written == stop_after: break - print "Wrote %i chunks to %s" % (n_chunks_written, opt.ofn) + print("Wrote {} chunks to {}".format(n_chunks_written, opt.ofn)) op = optparse.OptionParser() op.add_option('', '--input', action='store', type='string', dest='ifn', @@ -111,7 +111,7 @@ op.add_option('', '--chunks', action='store_true', dest='chunks', opt,arg = op.parse_args(sys.argv) if not (opt.ifn and opt.ofn): - print "You need at least --input and --output" + print("You need at least --input and --output") exit(1) f = open(opt.ifn, 'r') diff --git a/scripts/ave-resolution b/scripts/ave-resolution index a69c456e..69c21195 100755 --- a/scripts/ave-resolution +++ b/scripts/ave-resolution @@ -1,14 +1,13 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - # # Find mean diffracting resolution # -# Copyright © 2014-2015 Deutsches Elektronen-Synchrotron DESY, +# Copyright © 2014-2017 Deutsches Elektronen-Synchrotron DESY, # a research centre of the Helmholtz Association. # # Author: -# 2014-2015 Thomas White <taw@physics.org> +# 2014-2017 Thomas White <taw@physics.org> # import sys @@ -24,16 +23,16 @@ while True: break if fline.find("diffraction_resolution_limit") != -1: res = float(fline.split('= ')[1].split(' ')[0].rstrip("\r\n")) - a.append(res) + a.append(res) continue f.close() b = numpy.array(a) -print " Mean: %.2f nm^-1 = %.2f A" % (numpy.mean(b),10.0/numpy.mean(b)) -print " Best: %.2f nm^-1 = %.2f A" % (numpy.max(b),10.0/numpy.max(b)) -print "Worst: %.2f nm^-1 = %.2f A" % (numpy.min(b),10.0/numpy.min(b)) -print "Std deviation: %.2f nm^-1" % (numpy.std(b)) +print(" Mean: {:.2} nm^-1 = {:.2} A".format(numpy.mean(b),10.0/numpy.mean(b))) +print(" Best: {:.2} nm^-1 = {:.2} A".format(numpy.max(b),10.0/numpy.max(b))) +print("Worst: {:.2} nm^-1 = {:.2} A".format(numpy.min(b),10.0/numpy.min(b))) +print("Std deviation: {:.2} nm^-1".format(numpy.std(b))) plt.hist(a,bins=30) plt.title('Resolution based on indexing results') diff --git a/scripts/clean-stream.py b/scripts/clean-stream.py index 1e906e54..57c0ed93 100644 --- a/scripts/clean-stream.py +++ b/scripts/clean-stream.py @@ -1,6 +1,6 @@ #!/usr/bin/python # coding=utf-8 - +# # clean-stream.py # # Remove non-indexed frames from a stream @@ -86,5 +86,5 @@ while (line != ''): line = infile_1.readline() -print '%d suited of %d patterns have been extracted and saved as %s' % (num_suited, n_patt, sys.argv[2]) +print('%d suited of %d patterns have been extracted and saved as %s' % (num_suited, n_patt, sys.argv[2])) Nfile.write('%d' % num_suited) diff --git a/scripts/crystal-frame-number b/scripts/crystal-frame-number index b3ed3f1f..883abb81 100755 --- a/scripts/crystal-frame-number +++ b/scripts/crystal-frame-number @@ -1,6 +1,13 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - +# +# Show sequence numbers of crystals and frames +# +# Copyright © 2015-2017 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. +# +# Author: +# 2015-2017 Thomas White <taw@physics.org> import sys @@ -16,9 +23,9 @@ while True: if fline.find("Image filename") != -1: frame_number += 1 fn = fline.split(': ')[1].split(' ')[0].rstrip("\r\n") - print 'Frame %i: %s' % (frame_number, fn) + print('Frame {}: {}'.format(frame_number, fn)) if fline.find("diffraction_resolution_limit") != -1: crystal_number += 1 - print ' Crystal %i: %s' % (crystal_number, fline.rstrip("\r\n")) + print(' Crystal {}: {}'.format(crystal_number, fline.rstrip("\r\n"))) f.close() diff --git a/scripts/detector-shift b/scripts/detector-shift index a348e3c9..24cbb504 100755 --- a/scripts/detector-shift +++ b/scripts/detector-shift @@ -1,13 +1,13 @@ #!/usr/bin/env python - +# -*- coding: utf-8 -*- # # Determine mean detector shift based on prediction refinement results # -# Copyright (c) 2015-2016 Deutsches Elektronen-Synchrotron DESY, -# a research centre of the Helmholtz Association. +# Copyright © 2015-2017 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. # # Author: -# 2015-2016 Thomas White <taw@physics.org> +# 2015-2017 Thomas White <taw@physics.org> # 2016 Marmoru Suzuki <mamoru.suzuki@protein.osaka-u.ac.jp> # @@ -54,13 +54,13 @@ f.close() mean_x = sum(x_shifts) / len(x_shifts) mean_y = sum(y_shifts) / len(y_shifts) -print 'Mean shifts: dx = %.2f mm, dy = %.2f mm' % (mean_x,mean_y) +print('Mean shifts: dx = {:.2} mm, dy = {:.2} mm'.format(mean_x,mean_y)) # Apply shifts to geometry if have_geom: out = os.path.splitext(geom)[0]+'-predrefine.geom' - print 'Applying corrections to %s, output filename %s' % (geom,out) + print('Applying corrections to {}, output filename {}'.format(geom,out)) g = open(geom, 'r') h = open(out, 'w') panel_resolutions = {} @@ -99,7 +99,7 @@ if have_geom: res = panel_resolutions[panel] else: res = default_res - print 'Using default resolution (%f px/m) for panel %s' % (res, panel) + print('Using default resolution ({} px/m) for panel {}'.format(res, panel)) h.write('%s/corner_x = %f\n' % (panel,panel_cnx+(mean_x*res*1e-3))) continue @@ -111,7 +111,7 @@ if have_geom: res = panel_resolutions[panel] else: res = default_res - print 'Using default resolution (%f px/m) for panel %s' % (res, panel) + print('Using default resolution ({} px/m) for panel {}'.format(res, panel)) h.write('%s/corner_y = %f\n' % (panel,panel_cny+(mean_y*res*1e-3))) continue diff --git a/scripts/find-pairs b/scripts/find-pairs index 4629767d..8c3b69d0 100755 --- a/scripts/find-pairs +++ b/scripts/find-pairs @@ -1,14 +1,13 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - # # Find pairs of observations from the same pattern # -# Copyright © 2014 Deutsches Elektronen-Synchrotron DESY, -# a research centre of the Helmholtz Association. +# Copyright © 2014-2017 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. # # Author: -# 2014 Thomas White <taw@physics.org> +# 2014-2017 Thomas White <taw@physics.org> # import re as regexp @@ -41,33 +40,33 @@ def find_405(f): f = open(fn1, 'r') flines = find_405(f) -print len(flines),"measurements in",fn1 +print("{} measurements in {}".format(len(flines),fn1)) g = open(fn2, 'r') glines = find_405(g) -print len(glines),"measurements in",fn2 +print("{} measurements in {}".format(len(glines),fn2)) -print "\nThe common measurements:\n" +print("\nThe common measurements:\n") for fn in flines.keys(): if fn in glines: - print fn - print flines[fn].rstrip("\r\n") - print glines[fn] - print + print(fn) + print(flines[fn].rstrip("\r\n")) + print(glines[fn]) + print() del flines[fn] del glines[fn] -print "\nThe measurements only in",fn1,":\n" +print("\nThe measurements only in {}:\n".format(fn1)) for fn in flines.keys(): - print fn - print flines[fn].rstrip("\r\n") - print + print(fn) + print(flines[fn].rstrip("\r\n")) + print() -print "\nThe measurements only in",fn2,":\n" +print("\nThe measurements only in {}:\n".format(fn2)) for fn in glines.keys(): - print fn - print glines[fn].rstrip("\r\n") - print + print(fn) + print(glines[fn].rstrip("\r\n")) + print("") diff --git a/scripts/gaincal-to-saturation-map b/scripts/gaincal-to-saturation-map index 8a803526..eac8883c 100755 --- a/scripts/gaincal-to-saturation-map +++ b/scripts/gaincal-to-saturation-map @@ -1,4 +1,13 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Convert gain map to saturation map +# +# Copyright © 2016-2017 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. +# +# Author: +# 2016-2017 Thomas White <taw@physics.org> import numpy as np import h5py diff --git a/scripts/move-entire-detector b/scripts/move-entire-detector index 79ce5a5c..3d06c408 100755 --- a/scripts/move-entire-detector +++ b/scripts/move-entire-detector @@ -1,13 +1,13 @@ #!/usr/bin/env python - +# -*- coding: utf-8 -*- # # Shift the detector by a given amount # -# Copyright (c) 2016 Deutsches Elektronen-Synchrotron DESY, -# a research centre of the Helmholtz Association. +# Copyright © 2016-2017 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. # # Author: -# 2016 Thomas White <taw@physics.org> +# 2016-2017 Thomas White <taw@physics.org> # import sys @@ -58,8 +58,8 @@ if args.beam: else: bm = 'detector' -print 'Input filename %s\nOutput filename %s' % (args.ifn,out) -print 'Shifting %s by %f,%f %s' % (bm, args.xshift, args.yshift, units) +print('Input filename {}\nOutput filename {}'.format(args.ifn,out)) +print('Shifting {} by {},{} {}'.format(bm, args.xshift, args.yshift, units)) if args.beam: args.xshift = -args.xshift @@ -104,7 +104,7 @@ while True: else: res = default_res if not args.px: - print 'Using default resolution (%f px/m) for panel %s' % (res, panel) + print('Using default resolution ({} px/m) for panel {}'.format(res, panel)) if args.px: h.write('%s/corner_x = %f\n' % (panel,panel_cnx+args.xshift)) else: @@ -120,7 +120,7 @@ while True: else: res = default_res if not args.px: - print 'Using default resolution (%f px/m) for panel %s' % (res, panel) + print('Using default resolution ({} px/m) for panel {}'.format(res, panel)) if args.px: h.write('%s/corner_y = %f\n' % (panel,panel_cny+args.yshift)) else: diff --git a/scripts/peak-intensity b/scripts/peak-intensity index 53610b73..65e066cf 100755 --- a/scripts/peak-intensity +++ b/scripts/peak-intensity @@ -1,13 +1,13 @@ #!/usr/bin/env python - +# -*- coding: utf-8 -*- # # Quantify peak intensities # -# Copyright (c) 2015 Deutsches Elektronen-Synchrotron DESY, -# a research centre of the Helmholtz Association. +# Copyright © 2015-2017 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. # # Author: -# 2015 Thomas White <taw@physics.org> +# 2015-2017 Thomas White <taw@physics.org> # import sys @@ -40,8 +40,8 @@ while True: f.close() -print '%i patterns, %i peaks' % (n_patt,n_peaks) -print 'Mean %.2f peaks per pattern' % (n_peaks/n_patt) -print 'Mean %.2f ADU per peak' % (total_intens/n_peaks) -print 'Mean %.2f ADU total per pattern' % (total_intens/n_patt) +print('{} patterns, %i peaks'.format(n_patt,n_peaks)) +print('Mean {:.2f} peaks per pattern'.format(n_peaks/float(n_patt))) +print('Mean {:.2f} ADU per peak'.format(total_intens/float(n_peaks))) +print('Mean {:.2f} ADU total per pattern'.format(total_intens/n_patt)) diff --git a/scripts/peakogram-stream b/scripts/peakogram-stream index 209e7e95..1f67b39c 100755 --- a/scripts/peakogram-stream +++ b/scripts/peakogram-stream @@ -1,16 +1,14 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - -# peakogram-stream # # Check a stream for saturation # -# Copyright © 2016 Deutsches Elektronen-Synchrotron DESY, +# Copyright © 2016-2017 Deutsches Elektronen-Synchrotron DESY, # a research centre of the Helmholtz Association. # Copyright © 2016 The Research Foundation for SUNY # # Authors: -# 2016 Thomas White <taw@physics.org> +# 2016-2017 Thomas White <taw@physics.org> # 2014-2016 Thomas Grant <tgrant@hwi.buffalo.edu> # # This file is part of CrystFEL. @@ -120,7 +118,7 @@ data = np.asarray(data,dtype=float) sys.stdout.write("\r%i peaks found" % n) sys.stdout.flush() -print "" +print("") x = data[:,0] y = data[:,1] diff --git a/scripts/split-by-mask b/scripts/split-by-mask index aede3f43..18b672e4 100755 --- a/scripts/split-by-mask +++ b/scripts/split-by-mask @@ -1,13 +1,13 @@ #!/usr/bin/env python - +# -*- coding: utf-8 -*- # # Split stream according to an external mask # -# Copyright (c) 2014-2016 Deutsches Elektronen-Synchrotron DESY, -# a research centre of the Helmholtz Association. +# Copyright © 2014-2017 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. # # Author: -# 2014-2016 Thomas White <taw@physics.org> +# 2014-2017 Thomas White <taw@physics.org> # import sys @@ -21,7 +21,7 @@ prog = re.compile("^\s+([\d-]+)\s+([\d-]+)\s+([\d-]+)\s+[\d\.\-]+\s+[\d\.\-]+\s+ def process_refln(fline, g, h, mask): match = prog.match(fline) if not match: - print 'Line not understood, WTF? %s' % fline + print('Line not understood, WTF? {}'.format(fline)) return fs = int(round(float(match.group(4)))) ss = int(round(float(match.group(5)))) @@ -48,7 +48,7 @@ op.add_option('', '--mask', action='store', type='string', dest='mask_fn', opt,arg = op.parse_args(sys.argv) if not opt.ifn: - print "You need at least --input" + print("You need at least --input") exit(1) f = open(opt.ifn, 'r') diff --git a/scripts/truncate-stream b/scripts/truncate-stream index 1725082a..60679d30 100755 --- a/scripts/truncate-stream +++ b/scripts/truncate-stream @@ -1,13 +1,13 @@ #!/usr/bin/env python - +# -*- coding: utf-8 -*- # # Chop chunks or crystals from one or both ends of a stream # -# Copyright (c) 2014 Deutsches Elektronen-Synchrotron DESY, -# a research centre of the Helmholtz Association. +# Copyright © 2014-2017 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. # # Author: -# 2014 Thomas White <taw@physics.org> +# 2014-2017 Thomas White <taw@physics.org> # import sys @@ -66,7 +66,7 @@ def count_crystals(f, g, start_after, stop_after): in_crystal = 0 need_end_chunk = 1 - print "Wrote %i crystals to %s" % (n_crystals_written, opt.ofn) + print("Wrote {} crystals to {}".format(n_crystals_written, opt.ofn)) def count_chunks(f, g, start_after, stop_after): n_chunks_seen = 0 @@ -95,7 +95,7 @@ def count_chunks(f, g, start_after, stop_after): if n_chunks_written == stop_after: break - print "Wrote %i chunks to %s" % (n_chunks_written, opt.ofn) + print("Wrote {} chunks to {}".format(n_chunks_written, opt.ofn)) op = optparse.OptionParser() op.add_option('', '--input', action='store', type='string', dest='ifn', @@ -111,7 +111,7 @@ op.add_option('', '--chunks', action='store_true', dest='chunks', opt,arg = op.parse_args(sys.argv) if not (opt.ifn and opt.ofn): - print "You need at least --input and --output" + print("You need at least --input and --output") exit(1) f = open(opt.ifn, 'r') |