Beispiel #1
0
def get_minimal_dtype_for_values(values,
                                 allowed_kinds,
                                 default,
                                 allow_bool_as_intlike=True):
    values_normalized = []
    for value in values:
        if ia.is_np_array(value):
            values_normalized.extend([np.min(values), np.max(values)])
        else:
            values_normalized.append(value)
    vmin = np.min(values_normalized)
    vmax = np.max(values_normalized)
    possible_kinds = []
    if ia.is_single_float(vmin) or ia.is_single_float(vmax):
        # at least one is a float
        possible_kinds.append("f")
    elif ia.is_single_bool(vmin) and ia.is_single_bool(vmax):
        # both are bools
        possible_kinds.extend(["b", "u", "i"])
    else:
        # at least one of them is an integer and none is float
        if vmin >= 0:
            possible_kinds.append("u")
        possible_kinds.append("i")
        # vmin and vmax are already guarantueed to not be float due to if-statement above
        if allow_bool_as_intlike and 0 <= vmin <= 1 and 0 <= vmax <= 1:
            possible_kinds.append("b")

    for allowed_kind in allowed_kinds:
        if allowed_kind in possible_kinds:
            dt = get_minimal_dtype_by_value_range(vmin,
                                                  vmax,
                                                  allowed_kind,
                                                  default=None)
            if dt is not None:
                return dt

    if ia.is_string(default) and default == "raise":
        raise Exception((
            "Did not find matching dtypes for vmin=%s (type %s) and vmax=%s (type %s). "
            + "Got %s input values of types %s.") %
                        (vmin, type(vmin), vmax, type(vmax), ", ".join(
                            [str(type(value)) for value in values])))
    return default
Beispiel #2
0
def get_minimal_dtype_for_values(values, allowed_kinds, default, allow_bool_as_intlike=True):
    values_normalized = []
    for value in values:
        if ia.is_np_array(value):
            values_normalized.extend([np.min(values), np.max(values)])
        else:
            values_normalized.append(value)
    vmin = np.min(values_normalized)
    vmax = np.max(values_normalized)
    possible_kinds = []
    if ia.is_single_float(vmin) or ia.is_single_float(vmax):
        # at least one is a float
        possible_kinds.append("f")
    elif ia.is_single_bool(vmin) and ia.is_single_bool(vmax):
        # both are bools
        possible_kinds.extend(["b", "u", "i"])
    else:
        # at least one of them is an integer and none is float
        if vmin >= 0:
            possible_kinds.append("u")
        possible_kinds.append("i")
        # vmin and vmax are already guarantueed to not be float due to if-statement above
        if allow_bool_as_intlike and 0 <= vmin <= 1 and 0 <= vmax <= 1:
            possible_kinds.append("b")

    for allowed_kind in allowed_kinds:
        if allowed_kind in possible_kinds:
            dt = get_minimal_dtype_by_value_range(vmin, vmax, allowed_kind, default=None)
            if dt is not None:
                return dt

    if ia.is_string(default) and default == "raise":
        raise Exception(("Did not find matching dtypes for vmin=%s (type %s) and vmax=%s (type %s). "
                         + "Got %s input values of types %s.") % (
            vmin, type(vmin), vmax, type(vmax), ", ".join([str(type(value)) for value in values])))
    return default
Beispiel #3
0
 def handle(val):
     if ia.is_single_integer(val):
         do_assert(val > 0)
         return (val, val)
     elif ia.is_single_float(val):
         do_assert(val > 0)
         return (val, val)
     elif isinstance(val, tuple):
         do_assert(len(val) == 2)
         do_assert(val[0] > 0 and val[1] > 0)
         return val
     else:
         raise Exception(
             "Expected size to be int, float or tuple, got %s." %
             type(size))