示例#1
0
    def read2dict(cls, path, info):
        """Reads the control parameters of the given path (and its subpaths
        where appropriate) and stores it in the given :class:`dict` `info`.

        Arguments:
            * path (:class:`str`): Any object returning a valid path
              with or without extension.
            * info (:class:`dict`): Target dictionary.

        Note that the :class:`dict` `info` can be used to feed information
        into the execution of control files.  Use this function only if you
        are completely sure on how the control parameter import of HydPy
        works.  Otherwise, you should most probably prefer to use
        :func:`loadfile` or :func:`loadfiles`.
        """
        path = str(path)
        if not path.endswith('.py'):
            path += '.py'
        try:
            if path not in cls._registry:
                with open(path) as file_:
                    cls._registry[path] = file_.read()
            exec(cls._registry[path], {}, info)
        except BaseException:
            prefix = 'While trying to load the control file `%s`' % path
            objecttools.augmentexcmessage(prefix)
        if 'model' not in info:
            raise IOError('Model parameters cannot be loaded from control '
                          'file `%s`.  Please refer to the HydPy '
                          'documentation on how to prepare control files '
                          'properly.' % path)
示例#2
0
 def _convertandtest(self, values, name):
     """Try to convert the given values to a :mod:`numpy`
     :class:`~numpy.ndarrray` and check if it is plausible.  If so, return
     the array, other raise a :class:`~exceptions.ValueError` or re-raise a
     :mod:`numpy` specific exception.
     """
     from hydpy.pub import timegrids
     try:
         array = numpy.array(values, dtype=int)
     except BaseException:
         objecttools.augmentexcmessage('While trying to assign a new `%s` '
                                       'index array to an Indexer object' %
                                       name)
     if array.ndim != 1:
         raise ValueError('The `%s` index array of an Indexer object must '
                          'be 1-dimensional.  However, the given value has '
                          'interpreted as a %d-dimensional object.' %
                          (name, array.ndim))
     if timegrids is not None:
         if len(array) != len(timegrids.init):
             raise ValueError(
                 'The %s` index array of an Indexer object '
                 'must have a number of entries fitting to '
                 'the initialization time period precisely.  '
                 'However, the given value has been '
                 'interpreted to be of length %d and the '
                 'length of the Timegrid object representing '
                 'the actual initialization time period is %d.' %
                 (name, len(array), len(timegrids.init)))
     return array
示例#3
0
    def load(self):
        """Load nodes and elements from all network files and return them in
        a :class:`~hydpy.selectiontools.Selections` instance.  Each single
        network file defines a seperate
        :class:`~hydpy.selectiontools.Selection` instance.  Additionally, all
        elements and nodes are bundled in a selection named `complete`.
        """
        selections = selectiontools.Selections()
        for (filename, path) in zip(self.filenames, self.filepaths):
            # Ensure both `Node` and `Element`start with a `fresh` memory.
            devicetools.Node.gathernewnodes()
            devicetools.Element.gathernewelements()
            info = {}
            try:
                with open(path) as file_:
                    code = compile(file_.read(), path, 'exec')
                    exec(code, {}, info)
            except Exception:
                prefix = 'While trying to load the network file `%s`' % path
                objecttools.augmentexcmessage(prefix)
            try:
                selections += selectiontools.Selection(
                    filename.split('.')[0], info['Node'].gathernewnodes(),
                    info['Element'].gathernewelements())

            except KeyError as exc:
                KeyError('The class `%s` cannot be loaded from the network '
                         'file `%s`.  Please refer to the HydPy documentation '
                         'on how to prepare network files properly.' %
                         (exc.args[0], filename))
        selections += selectiontools.Selection(
            'complete', info['Node'].registerednodes(),
            info['Element'].registeredelements())
        return selections
示例#4
0
 def __init__(self, *values):
     try:
         self._extractvalues(values)
     except BaseException:
         objecttools.augmentexcmessage(
             'While trying to initialize a `%s` object' %
             objecttools.classname(self))
示例#5
0
 def __exit__(self, exception, message, traceback_):
     if not self.texts:
         self.print_('no failures occurred')
     else:
         for text in self.texts:
             self.print_(text)
     sys.stdout = self.stdout
     sys.stderr = self.stderr
     if exception:
         objecttools.augmentexcmessage()
示例#6
0
 def loadinfo(self):
     """Load general information from the project's main file."""
     try:
         with open(self.path) as file_:
             code = compile(file_.read(), self.path, 'exec')
             exec(code, {}, self.info)
     except Exception:
         prefix = ('While trying to load the general project settings '
                   'from `%s`' % self.path)
         objecttools.augmentexcmessage(prefix)
示例#7
0
 def connect(self, model=None):
     if model is not None:
         self.model = model
         model.element = self
     try:
         self.model.connect()
     except BaseException:
         objecttools.augmentexcmessage(
             'While trying to build the connections of the model handled '
             'by element `%s`' % self.name)
示例#8
0
 def connect(self):
     """Connect the link sequences of the actual model."""
     try:
         for group in ('inlets', 'receivers', 'outlets', 'senders'):
             self._connect_subgroup(group)
     except BaseException:
         objecttools.augmentexcmessage(
             'While trying to build the node connection of the `%s` '
             'sequences of the model handled by element `%s`' %
             (group[:-1], objecttools.devicename(self)))
示例#9
0
 def __call__(self, *args, **kwargs):
     try:
         args = [timetools.Period(args[0]).seconds]
     except BaseException:
         objecttools.augmentexcmessage(
             'While trying the set the value of parameter `maxdt` '
             'of the lake model handled by element `%s`' %
             objecttools.devicename(self),
             '(An example: set `max dt` to 3600 seconds by writing '
             '`maxdt("1h"))')
     parametertools.SingleParameter.__call__(self, *args, **kwargs)
示例#10
0
 def loadfile(self, filename, dirname=None):
     if not filename.endswith('.py'):
         filename += '.py'
     if dirname is None:
         dirname = os.path.join(pub.conditionmanager.loadpath)
     filepath = os.path.join(dirname, filename)
     try:
         with open(filepath) as file_:
             return file_.read()
     except BaseException:
         prefix = 'While trying to read the conditions file `%s`' % filepath
         objecttools.augmentexcmessage(prefix)
示例#11
0
 def __call__(self, *args, **kwargs):
     """The prefered way to pass values to :class:`DMin` instances
     within parameter control files.
     """
     try:
         lland_parameters.MultiParameterSoil.__call__(self, *args, **kwargs)
     except NotImplementedError:
         args = kwargs.get('r_dmax')
         if args is not None:
             self.values = 2.4192 * self.applytimefactor(numpy.array(args))
             self.trim()
         else:
             objecttools.augmentexcmessage()
示例#12
0
def calc_mean_time_deviation(timepoints, weights, mean_time=None):
    """Return the weighted deviation of the given timepoints from their mean
    time.

    With equal given weights, the is simply the standard deviation of the
    given time points:

    >>> from hydpy.auxs.statstools import calc_mean_time_deviation
    >>> calc_mean_time_deviation(timepoints=[3., 7.],
    ...                          weights=[2., 2.])
    2.0

    One can pass a precalculated or alternate mean time:

    >>> from hydpy.core.objecttools import round_
    >>> round_(calc_mean_time_deviation(timepoints=[3., 7.],
    ...                                 weights=[2., 2.],
    ...                                 mean_time=4.))
    2.236068

    >>> round_(calc_mean_time_deviation(timepoints=[3., 7.],
    ...                                 weights=[1., 3.]))
    1.732051

    Or, in the most extreme case:

    >>> calc_mean_time_deviation(timepoints=[3., 7.],
    ...                          weights=[0., 4.])
    0.0

    There will be some checks for input plausibility perfomed, e.g.:

    >>> calc_mean_time_deviation(timepoints=[3., 7.],
    ...                          weights=[-2., 2.])
    Traceback (most recent call last):
    ...
    ValueError: While trying to calculate the weighted time deviation from mean time, the following error occured: For the following objects, at least one value is negative: weights.
    """
    try:
        timepoints = numpy.array(timepoints)
        weights = numpy.array(weights)
        validtools.test_equal_shape(timepoints=timepoints, weights=weights)
        validtools.test_non_negative(weights=weights)
        if mean_time is None:
            mean_time = calc_mean_time(timepoints, weights)
        return (numpy.sqrt(
            numpy.dot(weights,
                      (timepoints - mean_time)**2) / numpy.sum(weights)))
    except BaseException:
        objecttools.augmentexcmessage('While trying to calculate the weighted '
                                      'time deviation from mean time')
示例#13
0
    def movedll(self):
        """Try to find the resulting dll file and to move it into the
        `cythons` package.

        Things to be aware of:
          * The file extension either `pyd` (Window) or `so` (Linux).
          * The folder containing the dll file is system dependend, but is
            always a subfolder of the `cythons` package.
          * Under Linux, the filename might contain system information, e.g.
            ...cpython-36m-x86_64-linux-gnu.so.
        """
        dirinfos = os.walk(self.buildpath)
        next(dirinfos)
        system_dependend_filename = None
        for dirinfo in dirinfos:
            for filename in dirinfo[2]:
                if (filename.startswith(self.cyname)
                        and filename.endswith(dllextension)):
                    system_dependend_filename = filename
                    break
            if system_dependend_filename:
                try:
                    shutil.move(
                        os.path.join(dirinfo[0], system_dependend_filename),
                        os.path.join(self.cydirpath,
                                     self.cyname + dllextension))
                    break
                except BaseException:
                    prefix = ('After trying to cythonize module %s, when '
                              'trying to move the final cython module %s '
                              'from directory %s to directory %s' %
                              (self.pyname, system_dependend_filename,
                               self.buildpath, self.cydirpath))
                    suffix = ('A likely error cause is that the cython module '
                              '%s does already exist in this directory and is '
                              'currently blocked by another Python process.  '
                              'Maybe it helps to close all Python processes '
                              'and restart the cyhonization afterwards.' %
                              self.cyname + dllextension)
                    objecttools.augmentexcmessage(prefix, suffix)
        else:
            raise IOError('After trying to cythonize module %s, the resulting '
                          'file %s could neither be found in directory %s nor '
                          'its subdirectories.  The distul report should tell '
                          'whether the file has been stored somewhere else,'
                          'is named somehow else, or could not be build at '
                          'all.' % self.buildpath)
示例#14
0
 def __call__(self, *args, **kwargs):
     try:
         self.shape = (len(kwargs), self.subpars.xpoints.shape[0])
     except RuntimeError:
         raise RuntimeError('The shape of parameter `ypoints` depends on '
                            'the shape of parameter `xpoints`.  Make sure '
                            'parameter `xpoints` is defined first (and is '
                            'integrated into the hmodel as described in '
                            'the documentation).')
     branched = self.subpars.pars.model.sequences.outlets.branched
     try:
         branched.shape = self.shape[0]
     except RuntimeError:
         if branched.shape[0] != self.shape[0]:
             raise RuntimeError('The number of branches of the hbranch '
                                'model should not be changed during run '
                                'time.  If you really need to do this, '
                                'first initialize a new `branched` '
                                'sequence and connect it to the '
                                'respective outlet nodes properly.')
     if self.shape[0] == 0:
         raise ValueError('No branches are defined.  Do this via keyword '
                          'arguments of the same name as the related '
                          'outlet node instances.')
     self.subpars.pars.model.sequences.fluxes.outputs.shape = self.shape[0]
     for (idx, key) in enumerate(sorted(kwargs)):
         value = kwargs[key]
         if ((key not in devicetools.Node.registerednames())
                 and (pub.timegrids is not None)):
             raise ValueError('Node `%s` does not exist so far.  Hence it '
                              'is not possible to branch to it.' % key)
         try:
             self[idx] = value
         except ValueError:
             if self.shape[1] != len(value):
                 raise ValueError('Each branch requires the same number of '
                                  'supporting points as given for '
                                  'parameter `xpoints`, which is %d.  But '
                                  'for branch `%s` %d are given.' %
                                  (self.shape[1], key, len(value)))
             else:
                 message = 'The affected keyword argument is `%s`' % key
                 objecttools.augmentexcmessage(suffix=message)
         setattr(self, key, self[idx])
         self.subpars.pars.model.nodenames.append(key)
示例#15
0
def calc_mean_time(timepoints, weights):
    """Return the weighted mean of the given timepoints.

    With equal given weights, the result is simply the mean of the given
    time points:

    >>> from hydpy.auxs.statstools import calc_mean_time
    >>> calc_mean_time(timepoints=[3., 7.],
    ...                weights=[2., 2.])
    5.0

    With different weights, the resulting mean time is shifted to the larger
    ones:

    >>> calc_mean_time(timepoints=[3., 7.],
    ...                weights=[1., 3.])
    6.0

    Or, in the most extreme case:

    >>> calc_mean_time(timepoints=[3., 7.],
    ...                weights=[0., 4.])
    7.0

    There will be some checks for input plausibility perfomed, e.g.:

    >>> calc_mean_time(timepoints=[3., 7.],
    ...                weights=[-2., 2.])
    Traceback (most recent call last):
    ...
    ValueError: While trying to calculate the weighted mean time, the following error occured: For the following objects, at least one value is negative: weights.
    """
    try:
        timepoints = numpy.array(timepoints)
        weights = numpy.array(weights)
        validtools.test_equal_shape(timepoints=timepoints, weights=weights)
        validtools.test_non_negative(weights=weights)
        return numpy.dot(timepoints, weights) / numpy.sum(weights)
    except BaseException:
        objecttools.augmentexcmessage(
            'While trying to calculate the weighted mean time')
示例#16
0
 def initmodels(self):
     warn = pub.options.warnsimulationstep
     pub.options.warnsimulationstep = False
     try:
         for (name, element) in self.elements:
             try:
                 element.initmodel()
             except IOError as exc:
                 temp = 'While trying to load the control file'
                 if ((temp in str(exc))
                         and pub.options.warnmissingcontrolfile):
                     warnings.warn('No model could be initialized for '
                                   'element `%s`' % name)
                     self.model = None
                 else:
                     objecttools.augmentexcmessage(
                         'While trying to initialize the model of '
                         'element `%s`' % name)
             else:
                 element.model.parameters.update()
                 element.model.connect()
     finally:
         pub.options.warnsimulationstep = warn
示例#17
0
 def __exit__(self, exception, message, traceback_):
     print('\x1B[0m')
     if exception:
         objecttools.augmentexcmessage()
示例#18
0
 def __exit__(self, exception, message, traceback_):
     if pub.options.printincolor:
         print(end='\x1B[0m', file=self.file)
     if exception:
         objecttools.augmentexcmessage()