def _units_are_valid(units_dict: dict) -> bool:
    """
    Checks that the units for the slit edges, radius, and slit height are valid.
    :param units_dict: The dictionary of units for the slit edges, radius, and slit height.
    :return: True if the units are all valid, False otherwise.
    """
    good_units = True

    for field in UNITS_REQUIRED:
        unit_input = units_dict[field]

        if not units_are_recognised_by_pint(unit_input, False):
            logging.info(
                f"{UNABLE} Units for {field} are not recognised. Found value:"
                f" {unit_input}")
            good_units = False
            continue
        if not units_are_expected_dimensionality(
                unit_input, EXPECTED_UNIT_TYPE[field], False):
            logging.info(
                f"{UNABLE} Units for {field} have wrong type. Found {unit_input} but"
                " expected something that can be converted to"
                f" {EXPECTED_UNIT_TYPE[field]}.")
            good_units = False
            continue
        if not units_have_magnitude_of_one(unit_input, False):
            logging.info(
                f"{UNABLE} Units for {field} should have a magnitude of one. Found"
                f" value: {unit_input}")
            good_units = False

    return good_units
Ejemplo n.º 2
0
    def validate(self, input: str, pos: int):

        if not (units_are_recognised_by_pint(input)
                and units_are_expected_type(input, METRES)
                and units_have_magnitude_of_one(input)):
            self.is_valid.emit(False)
            return QValidator.Intermediate

        self.is_valid.emit(True)
        return QValidator.Acceptable
Ejemplo n.º 3
0
    def validate(self, input: str, pos: int):

        if not (units_are_recognised(input) and
                (True if self.expected_dimensionality is None else
                 units_are_expected_dimensionality(
                     input, self.expected_dimensionality))
                and units_have_magnitude_of_one(input)):
            self.is_valid.emit(False)
            return QValidator.Intermediate

        self.is_valid.emit(True)
        return QValidator.Acceptable
Ejemplo n.º 4
0
    def _find_and_validate_units(self, vertices_dataset: Dict) -> Union[str, None]:
        """
        Attempts to retrieve and validate the units data.
        :param vertices_dataset: The vertices dataset.
        :return: Th units value if it was found and passed validation, otherwise None is returned.
        """
        try:
            attributes_list = vertices_dataset[CommonKeys.ATTRIBUTES]
        except KeyError:
            self.warnings.append(
                InvalidShape(
                    f"{self.error_message} Unable to find attributes list in vertices dataset."
                )
            )
            return None

        units = _find_attribute_from_list_or_dict(CommonAttrs.UNITS, attributes_list)
        if not units:
            self.warnings.append(
                InvalidShape(
                    f"{self.error_message} Unable to find units attribute in vertices dataset."
                )
            )
            return None

        if not units_are_recognised_by_pint(units, False):
            self.warnings.append(
                InvalidShape(
                    f"{self.error_message} Vertices units are not recognised by pint. Found {units}."
                )
            )
            return None
        if not units_are_expected_dimensionality(units, METRES, False):
            self.warnings.append(
                InvalidShape(
                    f"{self.error_message} Vertices units have wrong dimensionality. Expected something that can be "
                    f"converted to metred but found {units}. "
                )
            )
            return None
        if not units_have_magnitude_of_one(units, False):
            self.warnings.append(
                InvalidShape(
                    f"{self.error_message} Vertices units do not have magnitude of one. Found {units}."
                )
            )
            return None

        return units