Ejemplo n.º 1
0
def _unit(s):
    if isinstance(s, pq.quantity.Quantity):
        return s

    if is_float(s):
        return float(s) * pq.dimensionless

    #if isinstance(s, list):
    #    return [unit(obj) for obj in s]
    #if isinstance(s, dict):
    #    return dict([(key, unit(value)) for (key, value) in s.iteritems()])

    if ':' in s:
        (value_str, unit_str) = s.split(':')
        value = float(value_str)
        unt = morphforge.core.quantities.unit_string_parser.parse(unit_str)

        return value * unt

    if ' ' in s:
        tokens = s.split(' ')
        if len(tokens) == 2:
            (value_str, unit_str) = tokens
            value = float(value_str)
            unt = morphforge.core.quantities.unit_string_parser.parse(unit_str)
            return value * unt
        else:
            assert False

    return morphforge.core.quantities.unit_string_parser.parse(s)
Ejemplo n.º 2
0
def _unit(s):
    if isinstance(s, pq.quantity.Quantity):
        return s

    if is_float(s):
        return float(s) * pq.dimensionless

    #if isinstance(s, list):
    #    return [unit(obj) for obj in s]
    #if isinstance(s, dict):
    #    return dict([(key, unit(value)) for (key, value) in s.iteritems()])

    if ':' in s:
        (value_str, unit_str) = s.split(':')
        value = float(value_str)
        unt = morphforge.core.quantities.unit_string_parser.parse(unit_str)

        return value * unt

    if ' ' in s:
        tokens = s.split(' ')
        if len(tokens) == 2:
            (value_str, unit_str) = tokens
            value = float(value_str)
            unt = morphforge.core.quantities.unit_string_parser.parse(unit_str)
            return value * unt
        else:
            assert False

    return morphforge.core.quantities.unit_string_parser.parse(s)
Ejemplo n.º 3
0
def _convert_to_unit(o, default_unit):

    assert not isinstance(default_unit, units.Quantity)

    if isinstance(o, units.Quantity):
        return o.rescale(default_unit)
    elif is_float(o) or is_int(o):
        return o * morphforge.units.parse_unit_str(default_unit)#.rescale(default_unit)
    elif isinstance(o, (str, unicode)) and ':' in o:
        return qty(o).rescale(default_unit)
    else:
        raise ValueError()
Ejemplo n.º 4
0
def _convert_to_unit(o, default_unit):

    assert not isinstance(default_unit, pq.quantity.Quantity)

    if isinstance(o, pq.quantity.Quantity):
        return o.rescale(default_unit)
    elif is_float(o) or is_int(o):
        return o * morphforge.core.quantities.unit_string_parser.parse(default_unit).rescale(default_unit)
    elif isinstance(o, (str, unicode)) and ':' in o:
        return unit(o).rescale(default_unit)
    else:
        raise ValueError()
Ejemplo n.º 5
0
def _qty(s):
    import quantities as pq
    if isinstance(s, pq.quantity.Quantity):
        return s

    if is_float(s):
        return float(s) * pq.dimensionless


    if ':' in s:
        (value_str, unit_str) = s.split(':')
        value = float(value_str)
        unt = parse_unit(unit_str)
        return value * unt

    # Lets parse it as a neurounits string:
    import neurounits
    return neurounits.NeuroUnitParser.QuantityExpr(s).as_quantities_quantity()