Exemplo n.º 1
0
    def _linear(self, default):
        """Create and assign a linear dimension."""
        missing_key = ["increment", "count"]

        for item in missing_key:
            if default[item] is None:
                raise KeyError(f"Missing a required `{item}` key from the "
                               "LinearDimension object.")

        validate(default["count"], "count", int)

        return LinearDimension(**default)
Exemplo n.º 2
0
 def _to(self, unit="", equivalencies=None):
     r"""Convert the unit to given value `unit`."""
     unit = validate(unit, "unit", str)
     if equivalencies is None:
         self._unit = ScalarQuantity(unit, self._unit).quantity.unit
     else:
         self._unit = ScalarQuantity(unit).quantity.unit
     self._equivalencies = equivalencies
Exemplo n.º 3
0
    def set_component_labels(self, component_labels):
        """
        Assign an array of strings, based on the number of components.

        If no label is provided, a default values,
        :math:`['', '', N_k]`, is assigned. If the number of component labels
        does not match the total number of components, a warning is raised and
        the inconsistency is resolved by appropriate truncating or adding the
        required number of strings.
        """
        n = self._quantity_type.p
        if component_labels is None:
            self._component_labels = ["" for i in range(n)]
            return

        validate(component_labels, "component_labels", list)

        component_length = len(component_labels)
        if component_length == n:
            self._component_labels = component_labels
            return

        warning_statement = (
            f"The number of component labels, {component_length}, is not equal"
            f" to the number of components, {n}. The inconsistency is resolved"
            f" by appropriate truncation or addition of the strings.")
        warnings.warn(warning_statement)

        if component_length > n:
            self._component_labels = component_labels[:n]
            return

        lables = ["" for i in range(n)]
        for i, item in enumerate(component_labels):
            lables[i] = item
        self._component_labels = lables
        return
Exemplo n.º 4
0
    def count(self, value):
        value = validate(value, "count", int)

        if self.type in functional_dimension:
            self.subtype._count = value
            self.subtype._get_coordinates()
            return

        if value > self.count:
            raise ValueError(
                (f"Cannot set count, {value}, more than the number of "
                 f"coordinates, {self.count}, for monotonic and labeled"
                 " dimensions."))

        if value < self.count:
            warnings.warn(
                f"The number of coordinates, {self.count}, are truncated "
                f"to {value}.")
            self.subtype._count = value
Exemplo n.º 5
0
def parse_dict(dictionary):
    """Parse a CSDM compliant python dictionary and return a CSDM object.

        Args:
            dictionary: A CSDM compliant python dictionary.
    """
    key_list_root = dictionary.keys()
    if "CSDM" in key_list_root:
        raise KeyError("'CSDM' is not a valid keyword. Did you mean 'csdm'?")

    if "csdm" not in key_list_root:
        raise KeyError("Missing a required `csdm` key.")

    # inside csdm object
    optional_keys = [
        "read_only",
        "timestamp",
        "geographic_coordinate",
        "application",
        "tags",
        "description",
    ]
    required_keys = ["version"]
    all_keys = optional_keys + required_keys

    key_list_csdm = list(dictionary["csdm"].keys())
    key_list_csdm_lower_case = [item.lower() for item in key_list_csdm]

    for i in range(len(key_list_csdm)):
        if key_list_csdm[i] not in all_keys and key_list_csdm_lower_case[
                i] in all_keys:
            raise KeyError((f"{key_list_csdm[i]} is not a valid keyword. "
                            f"Did you mean '{key_list_csdm_lower_case[i]}'?"))

    for item in required_keys:
        if item not in key_list_csdm:
            raise KeyError(f"Missing a required `{item}` key.")

    _version = dictionary["csdm"]["version"]
    validate(_version, "version", str)

    if "filename" in dictionary.keys():
        filename = dictionary["filename"]
    else:
        filename = None
    csdm = CSDM(filename=filename, version=_version)

    if "timestamp" in dictionary["csdm"].keys():
        _timestamp = dictionary["csdm"]["timestamp"]
        validate(_timestamp, "timestamp", str)
        csdm._timestamp = _timestamp

    if "dimensions" in key_list_csdm:
        for dim in dictionary["csdm"]["dimensions"]:
            csdm.add_dimension(dim)

    if "dependent_variables" in key_list_csdm:
        for dat in dictionary["csdm"]["dependent_variables"]:
            csdm.add_dependent_variable(dat)

    n_points = [item.count for item in csdm.dimensions]
    if n_points != []:
        csdm._reshape(n_points[::-1])

    for key in optional_keys:
        if key in key_list_csdm:
            setattr(csdm, "_" + key, dictionary["csdm"][key])

    return csdm
Exemplo n.º 6
0
 def description(self, value):
     self._description = validate(value, "description", str)
Exemplo n.º 7
0
 def label(self, label=""):
     self._label = validate(label, "label", str)
Exemplo n.º 8
0
 def application(self, value):
     self._application = validate(value, "application", dict)
Exemplo n.º 9
0
 def encoding(self, value):
     self._encoding = validate(value, "encoding", str, check_encoding)
Exemplo n.º 10
0
 def name(self, value):
     self._name = validate(value, "name", str)
Exemplo n.º 11
0
 def read_only(self, value):
     self._read_only = validate(value, "read_only", bool)
Exemplo n.º 12
0
 def tags(self, value):
     self._tags = validate(value, "tags", list)
Exemplo n.º 13
0
 def origin_offset(self, value):
     allowed_types = (Quantity, str, ScalarQuantity)
     value = validate(value, "origin_offset", allowed_types)
     self._origin_offset = ScalarQuantity(value, self._unit).quantity
Exemplo n.º 14
0
 def coordinates_offset(self, value):
     allowed_types = (Quantity, str, ScalarQuantity)
     value = validate(value, "coordinates_offset", allowed_types)
     value = ScalarQuantity(value, self._unit).quantity
     self._coordinates_offset = value