Beispiel #1
0
def _str_to_quantity(v, values, prop_name):
    if isinstance(v, str):
        quantity = string_to_quantity(v)
        values["property_units"] = {prop_name: quantity.unit}
        return quantity.value
    if isinstance(v, float):
        return v
Beispiel #2
0
    def __init__(self, kernel_dimension, inverse_dimension):
        minimum = inverse_dimension["minimum"]
        maximum = inverse_dimension["maximum"]
        count = inverse_dimension.get("count", 32)
        scale = inverse_dimension.get("scale", "linear")
        label = inverse_dimension.get("label", None)

        unit = kernel_dimension.coordinates[0].unit
        x_min = string_to_quantity(minimum).to(unit).value
        x_max = string_to_quantity(maximum).to(unit).value
        check_log = scale == "log"

        if check_log:
            x_min, x_max = np.log10(x_min), np.log10(x_max)

        coords = (np.arange(count) / (count - 1)) * (x_max - x_min) + x_min

        if check_log:
            coords = 10**(coords)

        lbl_ = f"log({self.__class__.__name__} / {unit})" if check_log else None
        label = label if label is not None else lbl_
        inverse_dimension = cp.as_dimension(array=coords,
                                            unit=str(unit),
                                            label=label)

        meta = {
            "log":
            check_log,
            "label":
            f"log({self.__class__.__name__} / {unit})" if check_log else None,
        }
        inverse_dimension.application = {
            "com.github.deepanshs.mrinversion": meta
        }
        super().__init__(kernel_dimension, inverse_dimension, 1, 1)
Beispiel #3
0
def enforce_units(value: str,
                  required_type: str,
                  default_unit: str,
                  throw_error=True):
    """Enforces a required type and default unit on the value."""
    try:
        value = string_to_quantity(value)
        data_type = value.unit.physical_type

        if required_type != data_type:
            raise Exception(
                f"A {required_type} value is required but got a {data_type} instead"
            )

        return value.to(default_unit).value
    except Exception as e:
        if throw_error:
            raise e
        return None
Beispiel #4
0
    "type",
    "function",
    "config",
    "decompose_spectrum",
    "integration_density",
    "integration_volume",
    "number_of_sidebands",
    "name",
    "description",
    "transition_query",
    "ch1",
    "P",
    "channels",
]

CONST = string_to_quantity("1")


class Parseable(BaseModel):
    """Base class for all objects that can be parsed easily from JSON with units
    Don't directly use this. Rather inherit from it and implement a data model
    and property units and defaults
    """

    name: str = None
    description: str = None
    label: str = None
    property_unit_types: ClassVar[Dict] = {}
    property_default_units: ClassVar[Dict] = {}
    property_units: Dict = {}
Beispiel #5
0
# -*- coding: utf-8 -*-
import numpy as np
from csdmpy.units import string_to_quantity

__author__ = "Deepansh Srivastava"
__email__ = "*****@*****.**"

const = string_to_quantity("1")


def _get_broadcast_shape(array, dim, ndim):
    """Return the broadcast shape of a vector `array` at dimension `dim` for `ndim`
    total dimensions."""
    none = [np.newaxis for _ in range(ndim + 1)]
    if isinstance(dim, int):
        dim = [dim]
    for dim_ in dim:
        none[-dim_ - 1] = slice(None, None, None)
    return array[tuple(none)]


def _str_to_quantity(v, values, prop_name):
    if isinstance(v, str):
        quantity = string_to_quantity(v)
        values["property_units"] = {prop_name: quantity.unit}
        return quantity.value
    if isinstance(v, float):
        return v