Skip to content

Commit

Permalink
py3: first stage of xrange to range
Browse files Browse the repository at this point in the history
- range import from six.moves
- No conversion of range to list
  • Loading branch information
bkpoon authored and nksauter committed Jun 4, 2019
1 parent d72cd95 commit f78ead8
Show file tree
Hide file tree
Showing 96 changed files with 161 additions and 65 deletions.
1 change: 1 addition & 0 deletions boost/rational.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, division, print_function
import boost.python
from six.moves import range
ext = boost.python.import_ext("boost_rational_ext")
from boost_rational_ext import *

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from boost_adaptbx.graph import clustering_algorithm

import unittest
from six.moves import range


class TestBetweennessCentralityClustering(unittest.TestCase):
Expand Down
1 change: 1 addition & 0 deletions boost_adaptbx/graph/tests/tst_graph_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from boost_adaptbx.graph import metric

import unittest
from six.moves import range


class TestBrandesBetweennessCentrality(unittest.TestCase):
Expand Down
1 change: 1 addition & 0 deletions boost_adaptbx/hybrid_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from boost_python_hybrid_times_ext import run_c_plus_plus
import sys, os
from six.moves import range

if (not hasattr(sys, "gettickeraccumulation")):
print("***************************************************")
Expand Down
1 change: 1 addition & 0 deletions boost_adaptbx/tests/tst_python_streambuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import boost.python
from boost.python import streambuf, ostream
from six.moves import range
ext = boost.python.import_ext("boost_adaptbx_python_streambuf_test_ext")
import StringIO
import cStringIO
Expand Down
1 change: 1 addition & 0 deletions boost_adaptbx/tests/tst_rational.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from boost import rational
from libtbx.test_utils import Exception_expected, approx_equal, show_diff
from six.moves import range
try:
import cPickle as pickle
except ImportError:
Expand Down
1 change: 1 addition & 0 deletions boost_adaptbx/tests/tst_string_representation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import, division, print_function
from six.moves import range

def exercise():
import boost.python
Expand Down
3 changes: 2 additions & 1 deletion cma_es/cma_es_interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function
from scitbx.array_family import flex
from cma_es import cma_es
from six.moves import range

class cma_es_driver(object):
"""
Expand All @@ -22,7 +23,7 @@ def __init__(self, N, mean, sigma, evaluator, l=0):
pop_size = p.accessor().all()[0]
# update objective function
v = flex.double(pop_size)
for i in xrange(pop_size):
for i in range(pop_size):
vector = p[(i*N):(i*N + N)]
v[i] = self.evaluator( vector )
self.optimizer.update_distribution(v)
Expand Down
11 changes: 6 additions & 5 deletions cma_es/tst_cma_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
from libtbx.test_utils import approx_equal
from scitbx.array_family import flex
from cma_es import cma_es
from six.moves import range

# =============================================================================
center = flex.double([8.0,-13.0,0.5])

def obj_fun(x):
assert(len(x) == 3)
fun = 0.0
for i in xrange(len(x)):
for i in range(len(x)):
xi = x[i] - center[i]
fun += xi*xi
return fun

# from example1.c
def fitfun(x,N):
isum = 1e4*x[0]*x[0] + 1e-4*x[1]*x[1]
for i in xrange(2,N):
for i in range(2,N):
isum += x[i]*x[i]
return isum

Expand All @@ -34,7 +35,7 @@ def test_cma_es():

# update objective function
v = flex.double(pop_size)
for i in xrange(pop_size):
for i in range(pop_size):
v[i] = obj_fun(p[(i*N):(i*N + N)])
m.update_distribution(v)

Expand Down Expand Up @@ -88,7 +89,7 @@ def test_cma_es_lambda():

# update objective function
v = flex.double(pop_size)
for i in xrange(pop_size):
for i in range(pop_size):
v[i] = obj_fun(p[(i*N):(i*N + N)])
m.update_distribution(v)

Expand All @@ -107,7 +108,7 @@ def test_cma_es_file():

# update objective function
v = flex.double(pop_size)
for i in xrange(pop_size):
for i in range(pop_size):
v[i] = fitfun(p[(i*N):(i*N + N)],N)
m.update_distribution(v)

Expand Down
1 change: 1 addition & 0 deletions cootbx/ligand_checklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys

from libtbx.utils import to_str
from six.moves import range

t_wait = 250

Expand Down
1 change: 1 addition & 0 deletions cootbx/validation_lists.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from __future__ import absolute_import, division, print_function
import cPickle
from six.moves import range
try :
import gobject
except ImportError :
Expand Down
1 change: 1 addition & 0 deletions crys3d/command_line/multiplicity_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cctbx import crystal
import wx
import sys
from six.moves import range


master_phil = iotbx.phil.parse("""
Expand Down
1 change: 1 addition & 0 deletions crys3d/hklview/jsview_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os.path, time
import libtbx
import webbrowser, tempfile
from six.moves import range



Expand Down
3 changes: 2 additions & 1 deletion crys3d/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import, division, print_function
from six.moves import range

# TODO: clean up handling of changes in atom count

Expand Down Expand Up @@ -257,7 +258,7 @@ def color_mono (self) :
else :
from scitbx.array_family import flex
self.atom_colors = flex.vec3_double(
[ self.base_color for i in xrange(0, self.atoms.size()) ]
[ self.base_color for i in range(0, self.atoms.size()) ]
)
self._color_cache["mono"] = self.atom_colors

Expand Down
3 changes: 2 additions & 1 deletion crys3d/qttbx/xray_structure_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from scitbx import matrix as mat
import itertools
import six
from six.moves import range


def display(**kwds):
Expand Down Expand Up @@ -180,7 +181,7 @@ def draw_object_in_cartesian_coordinates(self):
self.principal_ellipses_tex.unbind()

self.bond_material.execute()
for i in xrange(0, len(self.bonds), 2):
for i in range(0, len(self.bonds), 2):
start, end = self.bonds[i], self.bonds[i+1]
self.cylindre_proto.draw(start, end, base_radius=0.05)

Expand Down
1 change: 1 addition & 0 deletions crys3d/reverse_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from libtbx import adopt_init_args
import re
import six
from six.moves import range

single_quote = re.compile(r"'")

Expand Down
1 change: 1 addition & 0 deletions crys3d/ribbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import scitbx.matrix
import time
import sys
from six.moves import range

class cartoon (object) :
def __init__ (self, pdb_hierarchy, sec_str, selection_cache=None) :
Expand Down
5 changes: 3 additions & 2 deletions crys3d/wx_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import wx

import libtbx.object_oriented_patterns as oop
from six.moves import range

class copy_init_args(object):
def __init__(self, args, exclude=()):
Expand Down Expand Up @@ -34,8 +35,8 @@ def FillWith3DGradient(self, rect, colour, direction, step=1):
slopes = [ x*step/d for x in step_adjustements ]
red_green_slopes = [ x*58 for x in slopes ]
blue_slopes = [ x*44 for x in slopes ]
ranges = [ xrange(slope_breaks[i], slope_breaks[i+1])
for i in xrange(len(slope_breaks)-1) ]
ranges = [ range(slope_breaks[i], slope_breaks[i+1])
for i in range(len(slope_breaks)-1) ]
for range, red_green_slope, blue_slope in zip(
ranges, red_green_slopes, blue_slopes):
for i in range:
Expand Down
7 changes: 4 additions & 3 deletions cudatbx/cufft/tst_ext.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import, division, print_function
from six.moves import range

def exercise_real_to_complex_3d(benchmark=True):
sizes_1 = [((32,32,32), 16, 0.0000001),
Expand Down Expand Up @@ -97,7 +98,7 @@ def exercise_complex_to_complex_3d():
d0.reshape(flex.grid(n_complex))
#
t0 = time.time()
for i_trial in xrange(n_repeats):
for i_trial in range(n_repeats):
d = d0.deep_copy()
overhead = time.time()-t0
print(" overhead: %.2f seconds" % overhead)
Expand All @@ -108,15 +109,15 @@ def exercise_complex_to_complex_3d():
cufft.complex_to_complex_3d(n_complex).backward(d)
# benchmarking run
t0 = time.time()
for i_trial in xrange(n_repeats):
for i_trial in range(n_repeats):
d = d0.deep_copy()
cufft.complex_to_complex_3d(n_complex).forward(d)
cufft.complex_to_complex_3d(n_complex).backward(d)
print(" cufft: %6.2f seconds" % ((time.time()-t0-overhead)/n_repeats))
rw = d / np
#
t0 = time.time()
for i_trial in xrange(n_repeats):
for i_trial in range(n_repeats):
d = d0.deep_copy()
fftpack.complex_to_complex_3d(n_complex).forward(d)
fftpack.complex_to_complex_3d(n_complex).backward(d)
Expand Down
25 changes: 13 additions & 12 deletions cudatbx/math/special_functions/tst_spherical_bessel_jn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from libtbx.test_utils import approx_equal
from scitbx.array_family import flex
from six.moves import range

# =============================================================================
def spherical_bessel_jn_test(write_output = False):
Expand All @@ -13,7 +14,7 @@ def spherical_bessel_jn_test(write_output = False):
z_max = 200.0
order = 50
z = flex.double(z_size)
for i in xrange(z_size):
for i in range(z_size):
z[i] = z_max * (i+1)/z_size

dt = [0.0,0.0]
Expand All @@ -25,37 +26,37 @@ def spherical_bessel_jn_test(write_output = False):
dt[0] = t1 - t0
if write_output:
f = open('jn_gpu.dat','w')
for i in xrange(order+1):
for j in xrange(z_size):
for i in range(order+1):
for j in range(z_size):
f.write('%f %f\n'%(z[j],jn_gpu[i*z_size + j]))
f.write('&\n')
f.close()

# CPU
jn_cpu = [ None for i in xrange(order+1) ]
jn_cpu = [ None for i in range(order+1) ]
t0 = time.time()
for n in xrange(order+1):
for n in range(order+1):
jn_cpu[n] = spherical_bessel_array(n,z)
t1 = time.time()
dt[1] = t1 - t0
if write_output:
f = open('jn_cpu.dat','w')
for i in xrange(order+1):
for j in xrange(z_size):
for i in range(order+1):
for j in range(z_size):
f.write('%f %f\n'%(z[j],jn_cpu[i][j]))
f.write('&\n')
f.close()

# difference
d_jn = [ None for i in xrange(order+1) ]
for n in xrange(order+1):
d_jn = [ None for i in range(order+1) ]
for n in range(order+1):
d_jn[n] = jn_cpu[n] - jn_gpu[n*z_size:n*z_size + z_size]
for i in xrange(z_size):
for i in range(z_size):
assert( approx_equal(d_jn[n][i]*d_jn[n][i],0.0,eps=1.0e-6) )
if write_output:
f = open('d_jn.dat','w')
for i in xrange(order+1):
for j in xrange(z_size):
for i in range(order+1):
for j in range(z_size):
f.write('%f %f\n'%(z[j],d_jn[i][j]))
f.write('&\n')
f.close()
Expand Down
9 changes: 5 additions & 4 deletions cudatbx/scattering/tst_direct_summation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from scitbx.array_family import flex
from scitbx import matrix
from scitbx.math.des_3_240_21 import t_x,t_y,t_z
from six.moves import range

test_pdb = "\
CRYST1 127.692 225.403 306.106 90.00 90.00 90.00 P 21 21 21 4\n\
Expand Down Expand Up @@ -39,15 +40,15 @@ def test_direct_summation():
h = flex.vec3_double(len(indices))
fm = matrix.sqr(p.crystal_symmetry().unit_cell().fractionalization_matrix())
om = matrix.sqr(p.crystal_symmetry().unit_cell().orthogonalization_matrix())
for i in xrange(len(indices)):
for i in range(len(indices)):
h[i] = fm * indices[i]
sr = x.scattering_type_registry()
st = x.scattering_types()

sg = p.crystal_symmetry().space_group()
r = flex.double()
t = flex.vec3_double(len(sg))
for i in xrange(len(sg)):
for i in range(len(sg)):
r_i = om * matrix.sqr(sg[i].r().as_double())
for a in r_i:
r.append(a)
Expand All @@ -62,7 +63,7 @@ def test_direct_summation():
gpu_i = flex.norm(amplitudes)

mean = 0.0
for i in xrange(len(cpu_i)):
for i in range(len(cpu_i)):
e = math.fabs(cpu_i[i] - gpu_i[i])/cpu_i[i]
mean += e
mean = mean/(len(cpu_i))
Expand All @@ -77,7 +78,7 @@ def test_saxs():
blsf = flex.double(len(xyz),0.0)
sr = x.scattering_type_registry()
st = x.scattering_types()
q = flex.double(range(101))/200.0
q = flex.double(list(range(101)))/200.0

t_w = flex.double(len(t_x),1.0)

Expand Down
1 change: 1 addition & 0 deletions dox.sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#from __future__ import absolute_import, division, print_function
import sys, os, re
from six.moves import range

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down
Loading

0 comments on commit f78ead8

Please sign in to comment.