diff --git a/wetb/gtsdf/__init__.py b/wetb/gtsdf/__init__.py index 8e583291a9a11dd40f5bbf0fc36235605ad62689..364d20a40cbaa101738ccc39ca6e896806f2ab1a 100644 --- a/wetb/gtsdf/__init__.py +++ b/wetb/gtsdf/__init__.py @@ -44,11 +44,14 @@ class Dataset(object): self.time, self.data, self.info = load(filename) def __call__(self, id): if isinstance(id, str): - return self((['Time'] + self.info['attribute_names']).index(id) + 1) + if id=="Time": + return self.time + else: + return self(self.info['attribute_names'].index(id) + 2) if id == 1: return self.time else: - return self.data[:, id - 1] + return self.data[:, id - 2] def __getattribute__(self, name): try: diff --git a/wetb/gtsdf/tests/test_gtsdf.py b/wetb/gtsdf/tests/test_gtsdf.py index 116b27f9e4030c2d9fe96737cb8480ccefe16f27..7a5ed608a2918223edfbdda3a010b17798ca8058 100644 --- a/wetb/gtsdf/tests/test_gtsdf.py +++ b/wetb/gtsdf/tests/test_gtsdf.py @@ -246,13 +246,14 @@ class Test_gsdf(unittest.TestCase): def test_gtsdf_dataset(self): ds = gtsdf.Dataset(tfp+'test.hdf5') + self.assertEqual(ds.data.shape, (2440,49)) self.assertEqual(ds('Time')[1], 0.05) self.assertEqual(ds.Time[1], 0.05) self.assertRaisesRegex(AttributeError, "'Dataset' object has no attribute 'Time1'", lambda : ds.Time1) - self.assertEqual(ds(2)[1], 11.986652374267578) + self.assertEqual(ds(2)[1], 12.04148006439209) n = ds.info['attribute_names'][2] self.assertEqual(n, "WSP gl. coo.,Vy") - self.assertEqual(ds(n)[1], 11.986652374267578) + self.assertEqual(ds(n)[1], 12.04148006439209) diff --git a/wetb/utils/cython_compile/__init__.py b/wetb/utils/cython_compile/__init__.py deleted file mode 100644 index 7db504d7fb31152836d8c88fcbc87882dff54f60..0000000000000000000000000000000000000000 --- a/wetb/utils/cython_compile/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -from __future__ import unicode_literals -from __future__ import print_function -from __future__ import division -from __future__ import absolute_import -from future import standard_library -standard_library.install_aliases() -d = None;d = dir() - -from .cython_compile import cython_compile, cython_import, cython_compile_autodeclare, is_compiled - -__all__ = [m for m in set(dir()) - set(d)] \ No newline at end of file diff --git a/wetb/utils/cython_compile/cython_compile.py b/wetb/utils/cython_compile/cython_compile.py deleted file mode 100644 index f66ab60aaefc7eeb3fe9b9f6886c2e19084235d5..0000000000000000000000000000000000000000 --- a/wetb/utils/cython_compile/cython_compile.py +++ /dev/null @@ -1,393 +0,0 @@ -''' -Created on 10/07/2013 - -@author: Mads M. Pedersen (mmpe@dtu.dk) - - -Wrapper functions and decorators for compiling functions using Cython - -''' -from __future__ import print_function -from __future__ import unicode_literals -from __future__ import division -from __future__ import absolute_import -from builtins import dict -from io import open -from builtins import zip -from builtins import str -from future import standard_library -standard_library.install_aliases() -import inspect -import os -import re -import shutil -import subprocess -import sys -import numpy as np -import warnings -import imp - - - -def wrap(f, autodeclare, *args, **kwargs): - """ - Wrapper function returned by the cython_compile and cython_compile_autodeclare decorators - - :param f: Function to compile - :type f: function - :param py2pyx_func: py2pyx or py2pyx_autodeclare - :type py2pyx_func: function - """ - - # Generate name: "c:\documents\project\mymodule.py" -> mymodule_myfunction - # When called from ipython notebooks, filename is an object e.g: "<ipython-input-12-e897f9fefc0c>" - # therefore <,>,- is removed to make it a legal python module name - name = os.path.relpath(inspect.getabsfile(f), str(os.getcwd())).replace(".py", "") - name = name.replace("<", "").replace(">", "").replace("-", "") - name = "%s_%s" % (name, f.__name__) - if name.startswith(".."): - rel_dir = os.path.dirname(name) + os.path.sep - sys.path.append(rel_dir) - name = name[len(rel_dir):] - pass - - module_name = name.replace(os.path.sep, ".") - - # import compiled module_name if exists, otherwise compile and import - try: - cmodule = __import__(module_name, fromlist="*") - if cmodule.__name__ != module_name or not hasattr(cmodule, f.__name__): - raise ImportError() - except ImportError: - # Generate pyrex code lines - if autodeclare: - pyx_lines = py2pyx_autodeclare(f, args, kwargs.copy()) - else: - # source lines except '@cython_compile' - source_lines = inspect.getsourcelines(f)[0][1:] - indent = source_lines[0].index("def") - pyx_lines = py2pyx([l[indent:] for l in source_lines]) - - # Write pyrex code lines to .pyx file - pyx_filename = name.replace("\\", "/") + ".pyx" - with open(pyx_filename, 'w', encoding='utf-8') as fid: - fid.writelines(pyx_lines) - - # compile, import compiled module_name and delete temporary files - cmodule = compile_and_cleanup(module_name, pyx_filename, module_name) - try: - cf = getattr(cmodule, f.__name__) - if kwargs == {}: - return cf(*args) - else: - return cf(*args, **kwargs) - except AttributeError: - warnings.warn( - "Compilation or import of %s failed. Python function used instead" % - f) - return f(*args, **kwargs) - - -def cython_compile(f): - """Decorator for compilation, import and execution of the function, f. - Variables can be declared using Pure or cdef syntax, see module description - Example: - @cython_compile - def my_func(p): - pass - """ - w = lambda *args, **kwargs: wrap(f, False, *args, **kwargs) - w.__name__ = f.__name__ - return w - - -def cython_compile_autodeclare(f): - """Decorator for autodeclaring, compilation, import and execution of the function, f. - Declared variables using cdef syntax overrides autodeclaration, see module description - Example: - @cython_compile_autocompile - def my_func(p): - pass - """ - w = lambda *args, **kwargs: wrap(f, True, *args, **kwargs) - w.__name__ = f.__name__ - return w - - -def cython_import(import_module_name, compiler=None): - """Compiles and imports a module. Use it similar to the normal import - Example (import my_func from my_module): - - from wetb.utils.cython_compile import cython_import - cython_import('my_module') - import my_module # import must be after cython_import statement - my_module.my_func() - - """ - module_name = import_module_name.split(".")[-1] - globals()[module_name] = __import__(import_module_name, fromlist=module_name) - - pyd_module = eval(module_name) - if not is_compiled(pyd_module): - - # Read py-module - file_name = import_module_name.replace(".", "/") + ".py" - for p in ["."] + sys.path: - if os.path.isfile(os.path.join(p, file_name)): - file_path = os.path.join(p, file_name).replace("\\", "/") - break - else: - raise IOError("Module %s cannot be found" % file_name) - - fid = open(file_path) - pylines = fid.readlines() - fid.close() - - # write pyrex file - pyx_filename = file_path.replace('.py', '.pyx') - fid = open(pyx_filename, 'w') - pyxlines = py2pyx(pylines) - fid.writelines(pyxlines) - fid.close() - - # compile, import compiled module and delete temporary files - module_relname = eval(module_name).__package__ + "." + module_name #"os.path.relpath(eval(module_name).__file__, str(os.getcwd())).replace(os.path.sep, ".")[:-3] - return compile_and_cleanup(import_module_name, pyx_filename, module_relname, compiler) - return eval(module_name) - - -def compile_and_cleanup(module, pyx_filename, module_relname, compiler=None): - """compile, import compiled module and delete temporary files""" - - # Generate setup.py script - fid = open('setup.py', 'w') - setup_str = """from distutils.core import setup -from distutils.extension import Extension -from Cython.Distutils import build_ext -import numpy - -ext_modules = [Extension("%s", ["%s"], include_dirs = [numpy.get_include()])] - -setup( -name = 'name', -cmdclass = {'build_ext': build_ext}, -ext_modules = ext_modules -)""" % (module_relname, pyx_filename) - fid.write(setup_str) - fid.close() - if hasattr(sys, 'frozen'): - raise Exception ("Trying to compile %s in frozen application" % str(module)) - # create compile command - import platform - if compiler is not None: - compiler_str = "--compiler=%s" % compiler - elif platform.architecture()[0] == "32bit" and os.name == 'nt' and "mingw" in os.environ['path'].lower(): - compiler_str = "--compiler=mingw32" - else: - compiler_str = "" - - - - bin_python = sys.executable - cmd = "%s setup.py build_ext --inplace %s" % (bin_python, compiler_str) - - # compile - print ("compiling %s: %s" % (module, cmd)) - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) - (out, err) = proc.communicate() - - # Reload and check that module is compiled - try: - try: - del sys.modules[module] - except KeyError: - pass - cmodule = __import__(module, fromlist=module.split(".")[-1]) - imp.reload(cmodule) - - except ImportError: - cmodule = None - - if cmodule is None or is_compiled(cmodule) == False: - line = '\n' + '=' * 79 + '\n' - sys.stderr.write("%s was not compiled correctly. It may result in slower execution" % module) - sys.stderr.write('%sstdout:\n%s' % (line, out.decode())) - sys.stderr.write('%sstderr:%s%s' % (line, line, err)) - - else: - print ("Compiling succeeded") - for f in ['setup.py', pyx_filename, pyx_filename.replace(".pyx", '.c')]: - if os.path.isfile(f): - os.remove(f) - - # Clean up. Remove temporary files and folders - if os.path.isdir("build"): - try: - shutil.rmtree("build") - except: - pass - - return cmodule - - -def py2pyx_autodeclare(f, args, kwargs): - """Generate pyrex code of function f and its input arguments - This function invokes py2pyx and extends with autodeclarations: - - arguments: input arguments are declared based their values in args and kwargs - - cdef: Variables declared by cdef, overrides autodeclaration, e.g: - "def func(a): #cpdef func(int a):" -> "cpdef func(int a):" (independent of type of a) - "#cdef int a" - "cdef int a" (independent of type of a) - - assignment: Variables assigned in function, e.g. - "a = xxx" - are declared based on the type of eval(xxx) - - in: variables returned by iterators, e.g: - "for a in range(5)", - "for a,b in [(1,.2)]:" - are declared base on the type of the first element in eval(iterator), e.g. "type(eval(range(5)[0]))" - """ - - arg_names, _, _, defaults = inspect.getargspec(f) - - # update kwargs with defaults - if defaults: - for k, v in zip(arg_names[::-1], defaults[::-1]): - if k not in kwargs: - kwargs[k] = v - kwargs.update(dict(zip(arg_names, args))) - - # get pyx code lines using py2pyx - lines = inspect.getsourcelines(f)[0] - if lines[0].strip() == "@cython_compile_autodeclare": - lines = lines[1:] - lines = py2pyx(lines) - - # prepare regular expressions - var_name = "(?:_*[a-zA-Z][a-zA-Z0-9_]*)" # optional "_" + one alpha + [0..n] x alphanum. "?:" = no group - reg_args = re.compile("[ \t]*def +(?:%s) *\(([^:]*)\) *:" % (var_name)) - reg_import = re.compile("[ \t]*c?import *(%s)(?: +as +(%s))?" % (var_name, var_name)) - reg_cdef = re.compile("[ \t]*cdef +(?:(?:signed|unsigned|int|long|float|double|np\.ndarray\[.*\]) *)*(%s)" % var_name) - reg_assign = re.compile('[ \t]*(%s) *=(.*)' % var_name) - reg_in = re.compile('[ \t]*for +(%s(?:, *%s)*) +in +(.*):' % (var_name, var_name)) - - - def declare_str(var_name, var_value): - """Generate declaration string '<type(var_value)> <var_name>' e.g: - declare_str('a',1) -> "long a" - """ - if isinstance(var_value, (int)): - return "long long %s" % var_name - if isinstance(var_value, float): - return "double %s" % var_name - elif isinstance(var_value, np.ndarray): - return "np.ndarray[np.%s_t,ndim=%d] %s" % (var_value.dtype, len(var_value.shape), var_name) - else: - raise NotImplementedError(type(var_value)) - - defs = {} # dict for known local variables - def_line = None # line nr of "def func():". Autodeclaration of local field inserted below this line - for i, line in enumerate(lines): - if def_line is None and 'def' in line: - # first line containing "def" = function declaration line - def_line = i - match = reg_args.match(line) - if match is not None: - args = match.group(1).strip() # line=" def func(xxx):#comment" -> args='xxx' - arg_strs = [] - if args != "": - for arg in args.split(','): - arg_name = arg.split('=')[0].strip() - arg_value = kwargs[arg_name] - try: - arg_strs.append(arg.strip().replace(arg_name, declare_str(arg_name, arg_value), 1)) - except NotImplementedError: - arg_strs.append(arg) - # replace function declaration line - lines[i] = '%scpdef %s(%s):\n' % (" " * line.index('def'), f.__name__, ", ".join(arg_strs)) - else: - #Import: import x as y - match = reg_import.match(line) - if match is not None: - # add imported moduled to kwargs -> enable evaluation of variables - import_module = match.group(1) - if match.group(2) is None: - kwargs[import_module] = __import__(import_module) - else: - # import xxx as y - kwargs[match.group(2)] = __import__(import_module) - continue - #Cdef: cdef int x - match = reg_cdef.match(line) - if match is not None: - # line contains a 'cdef' declaration. - # Add to defs to avoid redeclaration - var_name = match.group(1) - if var_name not in defs: - defs[var_name] = None - continue - #Assignment: x = y - match = reg_assign.match(line) - if match is not None: - # line contains an assignment, e.g. a = xxx. - # Try to evaluate xxx and declare a as type of eval(xxx) - try: - var_name = match.group(1) - if var_name not in defs: - var_value = eval(match.group(2), globals(), kwargs) - defs[var_name] = declare_str( - var_name.strip(), var_value) - kwargs[var_name] = var_value - except NotImplementedError: - pass - continue - #In: For x in y - match = reg_in.match(line) - if match is not None: - # line contains 'for xxx in yyy:'. - # Try to evaluate yyy and declare xxx as type of first element - # of eval(yyy) - var_names = [v.strip() for v in match.group(1).split(",")] - var_values = eval(match.group(2), globals(), kwargs)[0] - if not isinstance(var_values, (list, tuple)): - var_values = (var_values,) - for var_name, var_value in zip(var_names, var_values): - try: - if var_name not in defs: - defs[var_name] = declare_str(var_name, var_value) - kwargs[var_name] = var_value - except NotImplementedError: - pass - indent = lines[def_line + 1].replace(lines[def_line + 1].lstrip(), "") - - # Insert declaration of local fields ordered by name just below function - # declaration - for key in sorted(defs.keys(), reverse=True): - if defs[key] is not None: - lines.insert(def_line + 1, "%scdef %s\n" % (indent, defs[key])) - - return lines - - -def py2pyx(pylines): - """Generate pyrex code lines from python code lines - - Adds import of cython and numpy - - searches every line for "#c". If found text before "#c" is replaced with text after "#c", e.g: - "def func(a): #cpdef func(int a):" -> "cpdef func(int a): - "#cdef int b" -> "cdef int b" - """ - pyxlines = ['import cython\n', 'import numpy as np\n', 'cimport numpy as np\n'] - for l in pylines: - - if "#c" in l: - indent = l[:len(l) - len(l.lstrip())] - cdef = l[l.index("#c") + 1:] - l = indent + cdef - if "__future__" in l: - #pyxlines.insert(0, l) # from __future__ import ... must be first - pass - else: - pyxlines.append(l) - return pyxlines - - -def is_compiled(module): - return module.__file__.lower()[-4:] == ".pyd" or module.__file__.lower()[-3:] == ".so" diff --git a/wetb/utils/cython_compile/examples/__init__.py b/wetb/utils/cython_compile/examples/__init__.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/wetb/utils/cython_compile/examples/cycheck.py b/wetb/utils/cython_compile/examples/cycheck.py deleted file mode 100644 index 602cbf3521261a1565a51dc443890686665cb60c..0000000000000000000000000000000000000000 --- a/wetb/utils/cython_compile/examples/cycheck.py +++ /dev/null @@ -1,41 +0,0 @@ -''' -Created on 29/03/2013 - -@author: Mads -''' -from __future__ import unicode_literals -from __future__ import print_function -from __future__ import division -from __future__ import absolute_import -from builtins import range -from builtins import int -from future import standard_library -standard_library.install_aliases() -import cython -import math - - -def cycheck(p): - for i in range(10): - for y in range(2, int(math.sqrt(p)) + 1): - if p % y == 0: - return False - return True - -@cython.ccall -@cython.locals(y=cython.int, p=cython.ulonglong) -def cycheck_pure(p): - for i in range(10): - for y in range(2, int(math.sqrt(p)) + 1): - if p % y == 0: - return False - return True - - -def cycheck_cdef(p): #cpdef cycheck_cdef(unsigned long long p): - #cdef int y - for i in range(10): - for y in range(2, int(math.sqrt(p)) + 1): - if p % y == 0: - return False - return True diff --git a/wetb/utils/cython_compile/examples/examples.py b/wetb/utils/cython_compile/examples/examples.py deleted file mode 100644 index 14f794fe31c4866f8f8597e2ace752e2322c8c74..0000000000000000000000000000000000000000 --- a/wetb/utils/cython_compile/examples/examples.py +++ /dev/null @@ -1,62 +0,0 @@ -''' -Created on 11/07/2013 - -@author: Mads M. Pedersen (mmpe@dtu.dk) -''' -from __future__ import print_function -from __future__ import unicode_literals -from __future__ import division -from __future__ import absolute_import -from builtins import range -from builtins import int -from future import standard_library -standard_library.install_aliases() -import math - -from wetb.utils.cython_compile.cython_compile import cython_compile, \ - cython_compile_autodeclare, cython_import -from wetb.utils.cython_compile.examples import cycheck - - -def pycheck(p): - for i in range(10): - for y in range(2, int(math.sqrt(p)) + 1): - if p % y == 0: - return False - return True - - -@cython_compile -def cycheck_compile(p): - import math - for i in range(10): - for y in range(2, int(math.sqrt(p)) + 1): - if p % y == 0: - return False - return True - - -@cython_compile_autodeclare -def cycheck_compile_autodeclare(p): - import math - for i in range(10): - for y in range(2, int(math.sqrt(p)) + 1): - if p % y == 0: - return False - return True - -if __name__ == "__main__": - p = 17 - - print (pycheck(p)) - - cython_import('cycheck') - print (cycheck.cycheck(p)) - print (cycheck.cycheck_pure(p)) - print (cycheck.cycheck_cdef(p)) - - print (cycheck_compile(p)) - - print (cycheck_compile_autodeclare(p)) - -