예제 #1
0
    def test_non_numeric_or_negative_values_raise_exception(self):
        """checks that an error is raised when a negative or non-numeric
        value is used for:
         - area
         - age (when using_age_class is false)
         - delay
         - spatial reference (negatives allowed here)
        """

        classifiers, classifier_values = self.get_mock_classifiers()
        disturbance_types = self.get_mock_disturbance_types()
        for data in [
                ("b", "a", "F", "x", 1, 0, 0),
                ("b", "a", "F", -1, 1, 0, 0),
                ("b", "a", "F", 0, "x", 0, 0),
                ("b", "a", "F", 0, -1, 0, 0),
                ("b", "a", "F", 0, 0, "x", 0),
                ("b", "a", "F", 0, 0, -1, 0),
                ("b", "a", "F", 0, 0, -1, 0, "dist1", "dist1", "x")]:

            inventory_table = pd.DataFrame([data])
            with self.assertRaises(ValueError):
                sit_inventory_parser.parse(
                    inventory_table, classifiers, classifier_values,
                    disturbance_types, None)
예제 #2
0
    def test_spatial_reference_mixed_with_using_age_class(self):
        """checks that an error is raised when a row has both non-negative
        spatial reference value and a True "using_age_class" value
        """
        classifiers, classifier_values = self.get_mock_classifiers()
        disturbance_types = self.get_mock_disturbance_types()
        age_classes = self.get_mock_age_classes()
        for data in [("b", "a", True, 1, 0, 0, 0, "dist1", "dist1", 1)]:

            inventory_table = pd.DataFrame([data])
            with self.assertRaises(ValueError):
                sit_inventory_parser.parse(
                    inventory_table, classifiers, classifier_values,
                    disturbance_types, age_classes)
예제 #3
0
    def test_missing_classifier_values_raise_exception(self):
        """checks that an error is raised when a classifier value is not
        defined in sit classifiers metadata.
        """
        classifiers, classifier_values = self.get_mock_classifiers()
        disturbance_types = self.get_mock_disturbance_types()
        for data in [("MISSING", "a", "F", 0, 0, 0, 0),
                     ("b", "MISSING", "F", 0, 0, 0, 0)]:

            inventory_table = pd.DataFrame([data])
            with self.assertRaises(ValueError):
                sit_inventory_parser.parse(
                    inventory_table, classifiers, classifier_values,
                    disturbance_types, None)
예제 #4
0
    def test_exception_on_missing_age_class_id(self):
        """Checks the age class expansion feature "using_age_class"
        """
        inventory_table = pd.DataFrame(
            data=[
                ("b", "a", "TRUE", "MISSING", 1, 0, 0),
                ("a", "a", False, 100, 1, 0, 0),
                ("a", "a", "-1", 4, 1, 0, 0)])

        classifiers, classifier_values = self.get_mock_classifiers()
        age_classes = self.get_mock_age_classes()
        with self.assertRaises(ValueError):
            sit_inventory_parser.parse(
                inventory_table, classifiers, classifier_values, None,
                age_classes)
예제 #5
0
    def test_expected_result_with_numeric_classifiers(self):
        """Checks that numeric classifiers that appear in inventory data
        are parsed as strings
        """
        inventory_table = pd.DataFrame(
            data=[
                ("1", 1, "F", "1", 1, 0, 0),
                (2.0, 1, False, 100, 1, 0, 0),
                (1, 1, "-1", 4, 1, 0, 0)])

        classifiers = pd.DataFrame(
            data=[
                (1, "classifier1"),
                (2, "classifier2")
            ],
            columns=["id", "name"]
        )
        classifier_values = pd.DataFrame(
            data=[
                (1, "1", "1"),
                (1, "2.0", "2"),
                (2, "1", "1"),
            ],
            columns=["classifier_id", "name", "description"]
        )

        age_classes = self.get_mock_age_classes()
        result = sit_inventory_parser.parse(
            inventory_table, classifiers, classifier_values, None, age_classes)
        self.assertTrue((result.classifier1 == ["1", "2.0", "1"]).all())
예제 #6
0
    def test_duplicate_spatial_reference_raises_error(self):
        """Checks that any duplicate spatial reference values in the inventory
        table triggers an error
        """
        classifiers, classifier_values = self.get_mock_classifiers()
        disturbance_types = self.get_mock_disturbance_types()
        age_classes = self.get_mock_age_classes()

        inventory_table = pd.DataFrame([
            ("a", "a", False, 100, 1, 0, 0, "dist2", "dist1", 10000),
            ("a", "a", "-1", 4, 1, 0, 0, "dist1", "dist1", 10000)])
        # note the same identifier 10000 appears 2 times

        with self.assertRaises(ValueError):
            sit_inventory_parser.parse(
                inventory_table, classifiers, classifier_values,
                disturbance_types, age_classes)
예제 #7
0
    def test_expected_result_with_using_non_zero_age_class(self):
        """Checks the age class expansion feature "using_age_class"
        """
        inventory_table = pd.DataFrame(
            data=[
                ("b", "a", "TRUE", "1", 1, 0, 0),
                ("a", "a", False, 100, 1, 0, 0),
                ("a", "a", "-1", 4, 1, 0, 0)])

        classifiers, classifier_values = self.get_mock_classifiers()
        age_classes = self.get_mock_age_classes()
        result = sit_inventory_parser.parse(
            inventory_table, classifiers, classifier_values, None, age_classes)
        self.assertTrue(result.shape[0] == len(inventory_table) + 1)
        self.assertTrue(result.area.sum() == 3)
        self.assertTrue(set(result.age) == {100, 4, 1, 2})
예제 #8
0
def parse(sit_classifiers,
          sit_disturbance_types,
          sit_age_classes,
          sit_inventory,
          sit_yield,
          sit_events=None,
          sit_transitions=None,
          sit_eligibilities=None):
    """Parses and validates CBM Standard import tool formatted data including
    the complicated interdependencies in the SIT format. Returns an object
    containing the validated result.

    The returned object has the following properties:

     - classifiers: a pandas.DataFrame of classifiers in the sit_classifiers
        input
     - classifier_values: a pandas.DataFrame of the classifier values in the
        sit_classifiers input
     - classifier_aggregates: a dictionary of the classifier aggregates
        in the sit_classifiers input
     - disturbance_types: a pandas.DataFrame based on the disturbance types in
        the sit_disturbance_types input
     - age_classes: a pandas.DataFrame of the age classes based on
        sit_age_classes
     - inventory: a pandas.DataFrame of the inventory based on sit_inventory
     - yield_table: a pandas.DataFrame of the merchantable volume yield curves
        in the sit_yield input
     - disturbance_events: a pandas.DataFrame of the disturbance events based
        on sit_events.  If the sit_events parameter is None this field is None.
     - transition_rules: a pandas.DataFrame of the transition rules based on
        sit_transitions.  If the sit_transitions parameter is None this field
        is None.
     - disturbance_eligibilities: a pandas.DataFrame of the disturbance event
        eligibilities based on sit_eligibilities.  If the sit_events parameter
        is None this field is None.

    Args:
        sit_classifiers (pandas.DataFrame): SIT formatted classifiers
        sit_disturbance_types (pandas.DataFrame): SIT formatted disturbance
            types
        sit_age_classes (pandas.DataFrame): SIT formatted age classes
        sit_inventory (pandas.DataFrame): SIT formatted inventory
        sit_yield (pandas.DataFrame): SIT formatted yield curves
        sit_events (pandas.DataFrame, optional): SIT formatted disturbance
            events
        sit_transitions (pandas.DataFrame, optional): SIT formatted transition
            rules. Defaults to None.
        sit_eligibilities (pandas.DataFrame, optional): SIT formatted
            disturbance eligibilities. Defaults to None.

    Returns:
        object: an object containing parsed and validated SIT dataset
    """
    s = SimpleNamespace()
    classifiers, classifier_values, classifier_aggregates = \
        sit_classifier_parser.parse(sit_classifiers)
    s.classifiers = classifiers
    s.classifier_values = classifier_values
    s.classifier_aggregates = classifier_aggregates
    s.disturbance_types = sit_disturbance_type_parser.parse(
        sit_disturbance_types)
    s.age_classes = sit_age_class_parser.parse(sit_age_classes)
    s.inventory = sit_inventory_parser.parse(sit_inventory, classifiers,
                                             classifier_values,
                                             s.disturbance_types,
                                             s.age_classes)
    s.yield_table = sit_yield_parser.parse(sit_yield, s.classifiers,
                                           s.classifier_values, s.age_classes)

    if sit_events is not None:
        separate_eligibilities = False
        if sit_eligibilities is not None:
            separate_eligibilities = True
        s.disturbance_events = sit_disturbance_event_parser.parse(
            sit_events, s.classifiers, s.classifier_values,
            s.classifier_aggregates, s.disturbance_types, s.age_classes,
            separate_eligibilities)
        if sit_eligibilities is not None:
            s.disturbance_eligibilities = \
                sit_disturbance_event_parser.parse_eligibilities(
                    s.disturbance_events, sit_eligibilities)
        else:
            s.disturbance_eligibilities = None
    else:
        s.disturbance_events = None
        s.disturbance_eligibilities = None
    if sit_transitions is not None:
        s.transition_rules = sit_transition_rule_parser.parse(
            sit_transitions, s.classifiers, s.classifier_values,
            s.classifier_aggregates, s.disturbance_types, s.age_classes)
    else:
        s.transition_rules = None
    return s