From bd93faeb9533122c39802c10de8188f4634fef61 Mon Sep 17 00:00:00 2001 From: Jameson Graef Rollins <jrollins@finestructure.net> Date: Fri, 15 Dec 2017 17:45:24 -0800 Subject: [PATCH] python 2/3 compatibility --- gwinc/gwinc_matlab.py | 4 ++-- gwinc/ifo/__init__.py | 22 +++++++++++----------- gwinc/plot.py | 2 +- gwinc/test/__main__.py | 11 +++++++---- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/gwinc/gwinc_matlab.py b/gwinc/gwinc_matlab.py index 3602ea40..cfc94288 100644 --- a/gwinc/gwinc_matlab.py +++ b/gwinc/gwinc_matlab.py @@ -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 diff --git a/gwinc/ifo/__init__.py b/gwinc/ifo/__init__.py index ea4b0552..29429f3d 100644 --- a/gwinc/ifo/__init__.py +++ b/gwinc/ifo/__init__.py @@ -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 diff --git a/gwinc/plot.py b/gwinc/plot.py index f6140408..7e0ba305 100644 --- a/gwinc/plot.py +++ b/gwinc/plot.py @@ -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): diff --git a/gwinc/test/__main__.py b/gwinc/test/__main__.py index 8efaa405..a0548ac7 100644 --- a/gwinc/test/__main__.py +++ b/gwinc/test/__main__.py @@ -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: -- GitLab