Skip to content

input4mips_validation.cvs#

Sub-package Description
activity_id Activity ID CV handling
author Definition of an author of a dataset.
cvs Data model of the controlled vocabularies (CVs)
drs Data reference syntax data model
exceptions Exceptions related to CVs
institution_id Institution ID CV handling
license License CV handling
loading Loading of CVs from a given source
loading_raw Tools for loading the raw CVs
source_id Source ID CV handling

input4mips_validation.cvs #

Controlled vocabularies (CVs) handling

Input4MIPsCVs #

Data model of input4MIPs' CVs

Source code in src/input4mips_validation/cvs/cvs.py
@define
class Input4MIPsCVs:
    """
    Data model of input4MIPs' CVs
    """

    raw_loader: RawCVLoader
    """Object used to load the raw CVs"""

    DRS: DataReferenceSyntax
    """Data reference syntax used with these CVs"""
    # TODO: validation - check that all bits of the DRS
    #       are known in the metadata universe
    #        e.g. are required fields of files or something
    #        (may have to maintain list of 'known stuff' by hand).

    activity_id_entries: ActivityIDEntries
    """Activity ID entries"""

    # dataset_categories: tuple[str, ...]
    # """Recognised dataset categories"""
    # Would make sense for this to actually be entries,
    # and to specify the variables in each category here
    # No other validation applied

    institution_ids: tuple[str, ...]
    """Recognised institution IDs"""
    # TODO: check these against the global CVs when validating

    license_entries: LicenseEntries
    """License entries"""

    # mip_eras: tuple[str, ...]
    # """Recognised MIP eras"""
    # These should be linked back to the global CVs somehow
    # (probably as part of validation)

    # products: ProductEntries
    # """Recognised product types"""
    # These should be linked back to the global CVs somehow I assume (?)
    # (probably as part of validation)

    # publication_statuses: PublicationStatusEntries
    # """Recognised publication statuses"""
    # These should be linked back to the global CVs somehow I assume (?)
    # (probably as part of validation)

    # required_global_attribute: tuple[str, ...]
    # """Global attributes required in input4MIPs files"""
    # Would be nice if these were entries and hence we could get descriptions
    # of the meanins of the fields/link back to global CVs.
    # Might be easy with JSON-LD.

    source_id_entries: SourceIDEntries
    """Source ID entries"""

    # target_mip_entries: TargetMIPEntries
    # """Target MIP entries"""
    # These should be linked back to the global CVs somehow I assume (?)
    # (probably as part of validation)

    # tracking_id_regexp: str | regexp
    # """Regular expression which files' tracking IDs must match"""
    def validate_activity_id(self, value: str) -> None:
        """
        Validate that a value of activity ID is valid

        Parameters
        ----------
        value
            Value to validate

        Raises
        ------
        ValueNotAllowedByCVsError
            The provided value is not allowed by the CVs
        """
        if value not in self.activity_id_entries.activity_ids:
            raise ValueNotAllowedByCVsError(
                value=value,
                cv_component="activity_id",
                cv_allowed_values=self.activity_id_entries.activity_ids,
                cv_entries=self.activity_id_entries.entries,
            )

    def validate_contact(self, value: str, source_id: str) -> None:
        """
        Validate that a value of contact is valid

        Parameters
        ----------
        value
            Value to validate

        source_id
            Source ID value

            This is required because the source ID defines
            what the expected value of contact is.

        Raises
        ------
        ValueInconsistentWithCVsError
            The provided value is not the correct value according to the CVs
            and the value of `source_id`.
        """
        cv_source_id_entry = self.source_id_entries[source_id]
        expected_value = cv_source_id_entry.values.contact

        if value != expected_value:
            raise ValueInconsistentWithCVsError(
                value=value,
                expected_value=expected_value,
                cv_component="contact",
                cv_component_dependent_on="source_id",
                cv_entry_dependenty_component=cv_source_id_entry,
            )

    def validate_source_version(self, value: str, source_id: str) -> None:
        """
        Validate that a value of source version is valid

        Parameters
        ----------
        value
            Value to validate

        source_id
            Source ID value

            This is required because the source ID defines
            what the expected value of source_version is.

        Raises
        ------
        ValueInconsistentWithCVsError
            The provided value is not the correct value according to the CVs
            and the value of `source_id`.
        """
        cv_source_id_entry = self.source_id_entries[source_id]
        expected_value = cv_source_id_entry.values.source_version

        if value != expected_value:
            raise ValueInconsistentWithCVsError(
                value=value,
                expected_value=expected_value,
                cv_component="source_version",
                cv_component_dependent_on="source_id",
                cv_entry_dependenty_component=cv_source_id_entry,
            )

DRS: DataReferenceSyntax instance-attribute #

Data reference syntax used with these CVs

activity_id_entries: ActivityIDEntries instance-attribute #

Activity ID entries

institution_ids: tuple[str, ...] instance-attribute #

Recognised institution IDs

license_entries: LicenseEntries instance-attribute #

License entries

raw_loader: RawCVLoader instance-attribute #

Object used to load the raw CVs

source_id_entries: SourceIDEntries instance-attribute #

Source ID entries

validate_activity_id(value) #

Validate that a value of activity ID is valid

Parameters:

Name Type Description Default
value str

Value to validate

required

Raises:

Type Description
ValueNotAllowedByCVsError

The provided value is not allowed by the CVs

Source code in src/input4mips_validation/cvs/cvs.py
def validate_activity_id(self, value: str) -> None:
    """
    Validate that a value of activity ID is valid

    Parameters
    ----------
    value
        Value to validate

    Raises
    ------
    ValueNotAllowedByCVsError
        The provided value is not allowed by the CVs
    """
    if value not in self.activity_id_entries.activity_ids:
        raise ValueNotAllowedByCVsError(
            value=value,
            cv_component="activity_id",
            cv_allowed_values=self.activity_id_entries.activity_ids,
            cv_entries=self.activity_id_entries.entries,
        )

validate_contact(value, source_id) #

Validate that a value of contact is valid

Parameters:

Name Type Description Default
value str

Value to validate

required
source_id str

Source ID value

This is required because the source ID defines what the expected value of contact is.

required

Raises:

Type Description
ValueInconsistentWithCVsError

The provided value is not the correct value according to the CVs and the value of source_id.

Source code in src/input4mips_validation/cvs/cvs.py
def validate_contact(self, value: str, source_id: str) -> None:
    """
    Validate that a value of contact is valid

    Parameters
    ----------
    value
        Value to validate

    source_id
        Source ID value

        This is required because the source ID defines
        what the expected value of contact is.

    Raises
    ------
    ValueInconsistentWithCVsError
        The provided value is not the correct value according to the CVs
        and the value of `source_id`.
    """
    cv_source_id_entry = self.source_id_entries[source_id]
    expected_value = cv_source_id_entry.values.contact

    if value != expected_value:
        raise ValueInconsistentWithCVsError(
            value=value,
            expected_value=expected_value,
            cv_component="contact",
            cv_component_dependent_on="source_id",
            cv_entry_dependenty_component=cv_source_id_entry,
        )

validate_source_version(value, source_id) #

Validate that a value of source version is valid

Parameters:

Name Type Description Default
value str

Value to validate

required
source_id str

Source ID value

This is required because the source ID defines what the expected value of source_version is.

required

Raises:

Type Description
ValueInconsistentWithCVsError

The provided value is not the correct value according to the CVs and the value of source_id.

Source code in src/input4mips_validation/cvs/cvs.py
def validate_source_version(self, value: str, source_id: str) -> None:
    """
    Validate that a value of source version is valid

    Parameters
    ----------
    value
        Value to validate

    source_id
        Source ID value

        This is required because the source ID defines
        what the expected value of source_version is.

    Raises
    ------
    ValueInconsistentWithCVsError
        The provided value is not the correct value according to the CVs
        and the value of `source_id`.
    """
    cv_source_id_entry = self.source_id_entries[source_id]
    expected_value = cv_source_id_entry.values.source_version

    if value != expected_value:
        raise ValueInconsistentWithCVsError(
            value=value,
            expected_value=expected_value,
            cv_component="source_version",
            cv_component_dependent_on="source_id",
            cv_entry_dependenty_component=cv_source_id_entry,
        )

load_cvs(cv_source=None, **kwargs) #

Load CVs

Parameters:

Name Type Description Default
cv_source None | str | Path

String identifying the source of the CVs.

For full details of possible options, see get_raw_cvs_loader.

None
kwargs Any

Passed through to get_raw_cvs_loader.

{}

Returns:

Type Description
Input4MIPsCVs

Loaded CVs

Source code in src/input4mips_validation/cvs/loading.py
def load_cvs(
    cv_source: None | str | Path = None,
    **kwargs: Any,
) -> Input4MIPsCVs:
    """
    Load CVs

    Parameters
    ----------
    cv_source
        String identifying the source of the CVs.

        For full details of possible options, see
        [`get_raw_cvs_loader`][input4mips_validation.cvs.loading_raw.get_raw_cvs_loader].

    kwargs
        Passed through to
        [`get_raw_cvs_loader`][input4mips_validation.cvs.loading_raw.get_raw_cvs_loader].

    Returns
    -------
    :
        Loaded CVs
    """
    raw_cvs_loader = get_raw_cvs_loader(cv_source=cv_source)
    logger.debug(f"{raw_cvs_loader=}")

    return load_cvs_known_loader(raw_cvs_loader=raw_cvs_loader)