diff --git a/baseprocessor.py b/baseprocessor.py index 28202b7..34bf4f7 100644 --- a/baseprocessor.py +++ b/baseprocessor.py @@ -23,7 +23,7 @@ import subprocess from mypool import MyPool as Pool from functools import partial from abc import ABCMeta, abstractmethod -import bunch + from run_selector import * from units import * diff --git a/comparator.py b/comparator.py index 9a5d128..5436e80 100644 --- a/comparator.py +++ b/comparator.py @@ -89,22 +89,37 @@ class Comparator(Aggregator, HDF5Container): return missing_nums def _get_units(self, unit, data=None): - "Get real units from info files" + """ + Get real units from info files + unit is either: + 1. An instance of cst.Unit (pymses unit class) + 2. A string beginning by "unit_", referring to a code unit, + available in self.info + 3. A dict {unit1 : exp1, unit2: exp2, ...} with unitX as 2. + and expX a float, referring to the compound unit + unit1**exp1 * unit2**exp2 + 4. A dict {key: unit, ...} where key is a field name (eg. 'time', or 'mass') + and unit the corresponding unit (on one on the above format) + + Returns: + 1-3. : a cst.Unit instance + 4. : a dict {key: unit, ...} with same key as input and unit being cst.Unit instances + """ if isinstance(unit, cst.Unit): return unit - if isinstance(unit, str): + if isinstance(unit, str) and unit[:5] == "unit_": res = self.info[unit] if unit == "unit_length": res = res / self.info["boxlen"] return res - if unit.keys()[0] in self.info: + if list(unit)[0][:5] == "unit_": new_unit = cst.none for base_unit_str in unit: expo = unit[base_unit_str] base_unit = self._get_units(base_unit_str) new_unit = new_unit * base_unit ** expo return new_unit - if (not data is None) and isinstance(data, dict) and unit.keys()[0] in data: + if (not data is None) and isinstance(data, dict) and list(unit)[0] in data: for key in unit: unit[key] = self._get_units(unit[key]) return unit diff --git a/plotter.py b/plotter.py index 2c0e08e..cc084ab 100644 --- a/plotter.py +++ b/plotter.py @@ -222,6 +222,8 @@ class Plotter(Aggregator, BaseProcessor): if str(e) in [ "'LocatableAxes' object does not support indexing", "'AxesSubplot' object does not support indexing", + "'AxesSubplot' object is not subscriptable", + "'LocatableAxes' object is not subscriptable", ]: self._plot_rule( rule, @@ -786,7 +788,7 @@ class Plotter(Aggregator, BaseProcessor): time_str = self.pp_params.plot.time_fmt.format( time.express(time_unit), time_unit.latex ) - if len(label) > 0: + if label is not None and len(label) > 0: label = label + " | " + time_str else: label = time_str diff --git a/pp_params.py b/pp_params.py index 88f4bb1..ad9811c 100644 --- a/pp_params.py +++ b/pp_params.py @@ -2,7 +2,7 @@ import numpy as np import re -import bunch +import munch import yaml import os @@ -29,7 +29,7 @@ _loader.add_implicit_resolver( def load_params(filename): with open(filename) as f: para_disk = yaml.load(f, Loader=_loader) - return bunch.bunchify(para_disk) + return munch.munchify(para_disk) def default_params(): diff --git a/pspec_new.py b/pspec_new.py index f48eaef..006fe64 100644 --- a/pspec_new.py +++ b/pspec_new.py @@ -1,7 +1,9 @@ """Compute power spectra""" from __future__ import division +from __future__ import print_function +from builtins import range import argparse import sys import textwrap @@ -840,7 +842,7 @@ def main(arg): "cos_vB", "vz", ]: - for i in xrange(ns): + for i in range(ns): pspec, kbins, pspec2, fbins = pspectrum( pcubes2[v][:, :, i], cubes_k["k"][:, :, i], kbins, knorm, arg.fbins ) diff --git a/pspec_read.py b/pspec_read.py index 6268b9a..c8e3281 100644 --- a/pspec_read.py +++ b/pspec_read.py @@ -1,3 +1,4 @@ +from builtins import str import tables as T import numpy as np import matplotlib.pyplot as P diff --git a/run_selector.py b/run_selector.py index 8df4a03..f403d0c 100644 --- a/run_selector.py +++ b/run_selector.py @@ -86,15 +86,15 @@ class RunSelector: for run in runs: value[run] = self.get_nml_value(nml_key, run) if operator == "=": - runs = filter(lambda r: value[r] == operand, runs) + runs = list(filter(lambda r: value[r] == operand, runs)) if operator == "!=": - runs = filter(lambda r: not value[r] == operand, runs) + runs = list(filter(lambda r: not value[r] == operand, runs)) elif operator == ">": - runs = filter(lambda r: value[r] > operand, runs) + runs = list(filter(lambda r: value[r] > operand, runs)) elif operator == "<": - runs = filter(lambda r: value[r] < operand, runs) + runs = list(filter(lambda r: value[r] < operand, runs)) elif operator == "in": - runs = filter(lambda r: value[r] in operand, runs) + runs = list(filter(lambda r: value[r] in operand, runs)) return runs def get_runs(self, in_runs=None, name_run="*", namelist_cond={}, sort_run_by=None): @@ -106,13 +106,15 @@ class RunSelector: success = False return success - runs = map( - os.path.basename, - filter(os.path.isdir, glob.glob(self.path_in + "/" + name_run)), + runs = list( + map( + os.path.basename, + list(filter(os.path.isdir, glob.glob(self.path_in + "/" + name_run))), + ) ) if not in_runs is None: - runs = filter(lambda n: n in runs, in_runs) - runs = filter(try_load_nml, runs) + runs = list(filter(lambda n: n in runs, in_runs)) + runs = list(filter(try_load_nml, runs)) # Select runs that match namelist conditions runs = self.nml_select(runs, namelist_cond) @@ -160,12 +162,12 @@ class RunSelector: names = glob.glob( self.path_in + "/" + run + "/output_[0-9][0-9][0-9][0-9][0-9]" ) - nums = map(lambda n: int(n.split("/")[-1].split("_")[1]), names) + nums = list(map(lambda n: int(n.split("/")[-1].split("_")[1]), names)) if type(in_nums) == int: in_nums = [in_nums] if type(in_nums) == list: - nums = filter(lambda n: n in nums, in_nums) + nums = list(filter(lambda n: n in nums, in_nums)) nums = np.sort(nums) @@ -186,12 +188,12 @@ class RunSelector: else: nums = [] else: - nums = filter(try_load_info, nums) + nums = list(filter(try_load_info, nums)) if not time_min is None: - nums = filter(lambda n: self.info[run][n]["time"] >= time_min, nums) + nums = list(filter(lambda n: self.info[run][n]["time"] >= time_min, nums)) if not time_max is None: - nums = filter(lambda n: self.info[run][n]["time"] <= time_max, nums) + nums = list(filter(lambda n: self.info[run][n]["time"] <= time_max, nums)) if not time is None: times = np.asarray([[self.info[run][n]["time"], n] for n in nums])