diff --git a/python/tamaas/dumpers/_helper.py b/python/tamaas/dumpers/_helper.py index 89c8b51..3ee708e 100644 --- a/python/tamaas/dumpers/_helper.py +++ b/python/tamaas/dumpers/_helper.py @@ -1,104 +1,103 @@ # @file # @section LICENSE # # Copyright (©) 2016-19 EPFL (École Polytechnique Fédérale de Lausanne), # Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . """ Helper functions for dumpers """ import numpy as np import os __all__ = ["PeriodicHelper", "step_dump", "directory_dump"] def makePeriodic(data): data = np.append(data, np.expand_dims(data[:, 0, ...], axis=1), axis=1) data = np.append(data, np.expand_dims(data[:, :, 0, ...], axis=2), axis=2) return data def makeTractionPeriodic(data): data = np.append(data, np.expand_dims(data[0, ...], axis=0), axis=0) data = np.append(data, np.expand_dims(data[:, 0, ...], axis=1), axis=1) return data class PeriodicHelper: """Helper class to make fields periodic""" def __getitem__(self, item): if item == 'traction': return makeTractionPeriodic else: return makePeriodic def step_dump(cls): """ Decorator for dumper with counter for steps """ orig_init = cls.__init__ orig_dump = cls.dump def __init__(obj, *args, **kwargs): orig_init(obj, *args, **kwargs) obj.count = 0 def postfix(obj): return "_{:04d}".format(obj.count) def dump(obj, *args, **kwargs): orig_dump(obj, *args, **kwargs) obj.count += 1 cls.__init__ = __init__ cls.dump = dump cls.postfix = property(postfix) return cls class directory_dump: """ Decorator for dumper in a directory """ def __init__(self, directory=""): self.directory = directory def __call__(self, cls): directory = self.directory - orig_init = cls.__init__ + orig_dump = cls.dump orig_filepath = cls.file_path.fget - def __init__(obj, *args, **kwargs): - orig_init(obj, *args, **kwargs) - + def dump(obj, *args, **kwargs): if not os.path.exists(directory): os.mkdir(directory) - if not os.path.isdir(directory): raise Exception('{} is not a directory'.format(directory)) + orig_dump(obj, *args, **kwargs) + def file_path(obj): return os.path.join(directory, orig_filepath(obj)) - cls.__init__ = __init__ + cls.dump = dump cls.file_path = property(file_path) return cls