Commit bd93faeb authored by Jameson Graef Rollins's avatar Jameson Graef Rollins
Browse files

python 2/3 compatibility

parent 3b6a5d0a
......@@ -93,7 +93,7 @@ NOISE_NAME_MAP = {
}
def _rename_noises(d):
nd = {}
for k,v in d.iteritems():
for k,v in d.items():
try:
nk = NOISE_NAME_MAP[k]
except KeyError:
......@@ -149,7 +149,7 @@ def gwinc_matlab(f, ifo, fig=False, title=None, gwincpath=None, eng=None, **kwar
score = data['score']
mnoises = Struct.from_matstruct(data['noises']).to_dict()
##### blow out 'MirrorThermal' sub-dict
for n,d in mnoises['MirrorThermal'].iteritems():
for n,d in mnoises['MirrorThermal'].items():
if n == 'Total':
continue
mnoises[n] = d
......
......@@ -17,15 +17,15 @@ from scipy.io.matlab.mio5_params import mat_struct
#
loader = yaml.SafeLoader
loader.add_implicit_resolver(
u'tag:yaml.org,2002:float',
re.compile(u'''^(?:
'tag:yaml.org,2002:float',
re.compile('''^(?:
[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)?
|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
|\\.[0-9_]+(?:[eE][-+][0-9]+)?
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*
|[-+]?\\.(?:inf|Inf|INF)
|\\.(?:nan|NaN|NAN))$''', re.X),
list(u'-+0123456789.'))
list('-+0123456789.'))
def dictlist2recarray(l):
......@@ -35,7 +35,7 @@ def dictlist2recarray(l):
else:
return type(v)
# get dtypes from first element dict
dtypes = [(k, dtype(v)) for k,v in l[0].iteritems()]
dtypes = [(k, dtype(v)) for k,v in l[0].items()]
values = [tuple(el.values()) for el in l]
out = np.array(values, dtype=dtypes)
return out.view(np.recarray)
......@@ -89,7 +89,7 @@ class Struct(object):
"""
d = {}
for k,v in self.__dict__.iteritems():
for k,v in self.__dict__.items():
if isinstance(v, Struct):
d[k] = v.to_dict(array=array)
else:
......@@ -127,7 +127,7 @@ class Struct(object):
# return self.to_yaml().strip('\n')
def __str__(self):
return '<GWINC Struct: {}>'.format(self.__dict__.keys())
return '<GWINC Struct: {}>'.format(list(self.__dict__.keys()))
def __iter__(self):
return iter(self.__dict__)
......@@ -136,7 +136,7 @@ class Struct(object):
"""Iterate over all leaves in the struct tree.
"""
for k,v in self.__dict__.iteritems():
for k,v in self.__dict__.items():
if type(v) is Struct:
for sk,sv in v.walk():
yield k+'.'+sk, sv
......@@ -154,12 +154,12 @@ class Struct(object):
"""
c = cls()
for k,v in d.iteritems():
for k,v in d.items():
if type(v) == dict:
c.__dict__[k] = Struct.from_dict(v)
else:
try:
c.__dict__[k] = map(Struct.from_dict, v)
c.__dict__[k] = list(map(Struct.from_dict, v))
except (AttributeError, TypeError):
c.__dict__[k] = v
return c
......@@ -174,7 +174,7 @@ class Struct(object):
s = s['ifo']
except:
pass
for k,v in s.__dict__.iteritems():
for k,v in s.__dict__.items():
if k in ['_fieldnames']:
# skip these fields
pass
......@@ -183,7 +183,7 @@ class Struct(object):
else:
# handle lists of Structs
try:
c.__dict__[k] = map(Struct.from_matstruct, v)
c.__dict__[k] = list(map(Struct.from_matstruct, v))
except:
c.__dict__[k] = v
return c
......
......@@ -22,7 +22,7 @@ def plot_noise(noises):
f = noises['Freq']
def plot_dict(noises):
for name, noise in noises.iteritems():
for name, noise in noises.items():
if name in ['Freq', 'Total']:
continue
if isinstance(noise, dict):
......
......@@ -33,15 +33,18 @@ def main():
matdata = os.path.join(os.path.dirname(__file__), 'matlab.pkl')
if os.path.exists(matdata):
logging.info("using existing {}...".format(matdata))
with open(matdata, 'r') as f:
mdata = pickle.load(f)
with open(matdata, 'rb') as f:
if sys.version_info.major > 2:
mdata = pickle.load(f, encoding='latin1')
else:
mdata = pickle.load(f)
else:
logging.info("calculating matlab noise...")
gwincpath = os.path.join(os.path.dirname(__file__), 'gwinc')
from ..gwinc_matlab import gwinc_matlab
score, noises, ifo = gwinc_matlab(freq, ifo, gwincpath=gwincpath)
mdata = dict(score=score, noises=noises, ifo=ifo)
with open(matdata, 'w') as f:
with open(matdata, 'wb') as f:
pickle.dump(mdata, f)
......@@ -50,7 +53,7 @@ def main():
mnoises = mdata['noises']
diffs = {}
for name, noise in noises.iteritems():
for name, noise in noises.items():
if name == 'Freq':
continue
try:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment