Example #1
0
def get_var_shape(name, scope):
    val = scope.get(name)
    if isinstance(val, ndarray):
        if val.shape == ():
            return (1, )
        return val.shape
    if isinstance(val, real_types):
        return (1, )

    if IVariableTree.providedBy(val):
        raise NotImplementedError("get_var_shape not supported for vartrees")

    sz = flattened_size(name, val, scope)
    if sz:
        return (sz, )

    return None
def get_var_shape(name, scope):
    val = scope.get(name)
    if isinstance(val, ndarray):
        if val.shape == ():
            return (1,)
        return val.shape
    if isinstance(val, real_types):
        return (1,)

    if IVariableTree.providedBy(val):
        raise NotImplementedError("get_var_shape not supported for vartrees")

    sz = flattened_size(name, val, scope)
    if sz:
        return (sz,)

    return None
    def set_flattened_value(self, path, value):
        val,rop = deep_getattr(self, path.split('[',1)[0])
        idx = get_index(path)
        if isinstance(val, int_types):
            pass  # fall through to exception
        if isinstance(val, complex_or_real_types):
            if idx is None:
                setattr(self, path, value[0])
                return
            # else, fall through to error
        elif isinstance(val, ndarray):
            if idx is None:
                setattr(self, path, value)
            else:
                val[idx] = value
            return
        elif IVariableTree.providedBy(val):
            raise NotImplementedError("no support for setting flattened values into vartrees")

        raise TypeError("%s: Failed to set flattened value to variable %s" % (self.name, path))
def _flattened_names(name, val, names=None):
    """ Return list of names for values in `val`.
    Note that this expands arrays into an entry for each index!.
    """
    if names is None:
        names = []
    if isinstance(val, float):
        names.append(name)
    elif isinstance(val, ndarray):
        for i in range(len(val)):
            value = val[i]
            _flattened_names('%s[%s]' % (name, i), value, names)
    elif IVariableTree.providedBy(val):
        for key in sorted(val.list_vars()):  # Force repeatable order.
            value = getattr(val, key)
            _flattened_names('.'.join((name, key)), value, names)
    else:
        raise TypeError('Variable %s is of type %s which is not convertable'
                        ' to a 1D float array.' % (name, type(val)))
    return names
    def set_flattened_value(self, path, value):
        val, rop = deep_getattr(self, path.split('[', 1)[0])
        idx = get_index(path)
        if isinstance(val, int_types):
            pass  # fall through to exception
        if isinstance(val, complex_or_real_types):
            if idx is None:
                setattr(self, path, value[0])
                return
            # else, fall through to error
        elif isinstance(val, ndarray):
            if idx is None:
                setattr(self, path, value)
            else:
                val[idx] = value
            return
        elif IVariableTree.providedBy(val):
            raise NotImplementedError(
                "no support for setting flattened values into vartrees")

        raise TypeError("%s: Failed to set flattened value to variable %s" %
                        (self.name, path))