def get_available_exposures(self, impact_function=None, ascending=True): """Return a list of valid available exposures for an impact function. If impact_function is None, return all available exposures .. versionadded:: 2.2 :param impact_function: Impact Function object. :type impact_function: FunctionProvider :param ascending: Sort ascending or not. :type ascending: bool :returns: A list of exposures full metadata. :rtype: list """ exposures = [] if impact_function is None: for impact_function in self.impact_functions: add_to_list( exposures, impact_function.Metadata.get_exposures()) else: # noinspection PyUnresolvedReferences exposures = impact_function.Metadata.get_exposures() # make it sorted if ascending: exposures = sorted(exposures, key=lambda k: k['id']) return exposures
def categories_for_layer(cls, layer_type, data_type): """Determine the valid categories for a layer. This method is used to determine if a given layer can be used as a hazard, exposure or aggregation layer. In the returned the values are categories (if any) applicable for that layer_type and data_type. :param layer_type: The type for this layer. Valid values would be, 'raster' or 'vector'. :type layer_type: str :param data_type: The data_type for this layer. Valid possibilities would be 'numeric' (for raster), point, line, polygon (for vectors). :type data_type: str :returns: A list as per the example above where each value represents a valid category. :rtype: list """ layer_constraints = { 'layer_type': layer_type, 'data_type': data_type } result = [] if layer_constraints in cls.allowed_layer_constraints('exposure'): result = add_to_list(result, 'exposure') if layer_constraints in cls.allowed_layer_constraints('hazard'): result = add_to_list(result, 'hazard') return result
def get_available_exposures(self, impact_function=None, ascending=True): """Return a list of valid available exposures for an impact function. If impact_function is None, return all available exposures .. versionadded:: 2.2 :param impact_function: Impact Function object. :type impact_function: FunctionProvider :param ascending: Sort ascending or not. :type ascending: bool :returns: A list of exposures full metadata. :rtype: list """ exposures = [] if impact_function is None: for impact_function in self.impact_functions: add_to_list(exposures, impact_function.Metadata.get_exposures()) else: # noinspection PyUnresolvedReferences exposures = impact_function.Metadata.get_exposures() # make it sorted if ascending: exposures = sorted(exposures, key=lambda k: k['id']) return exposures
def categories_for_layer(cls, layer_type, data_type): """Determine the valid categories for a layer. This method is used to determine if a given layer can be used as a hazard, exposure or aggregation layer. In the returned the values are categories (if any) applicable for that layer_type and data_type. :param layer_type: The type for this layer. Valid values would be, 'raster' or 'vector'. :type layer_type: str :param data_type: The data_type for this layer. Valid possibilities would be 'numeric' (for raster), point, line, polygon (for vectors). :type data_type: str :returns: A list as per the example above where each value represents a valid category. :rtype: list """ layer_constraints = {'layer_type': layer_type, 'data_type': data_type} result = [] if layer_constraints in cls.allowed_layer_constraints('exposure'): result = add_to_list(result, 'exposure') if layer_constraints in cls.allowed_layer_constraints('hazard'): result = add_to_list(result, 'hazard') return result
def allowed_data_types(cls, subcategory): """Get the list of allowed data types for a subcategory. Example usage:: foo = IF() meta = IF.metadata ubar = meta.allowed_data_types('structure') ubar > ['polygon'] In the above example it does not show ‘numeric’ as the request is specific to the structure subcategory for that IF (using the IF declaration at the top of this file as the basis for IF()) Passing a subcategory is required otherwise the context of the data_type(s) would be ambiguous (i.e. whether they can be used as exposure or hazards). :param subcategory: Required subcategory which will be used to subset the allowed data_types. :type subcategory: str :returns: A list of one or more strings is returned. :rtype: list """ result = [] metadata_dict = cls.get_metadata() categories = metadata_dict['categories'] if subcategory in [ x['id'] for x in cls.allowed_subcategories('exposure') ]: # implementation logic that returns the allowed data_types for # exposure layer with subcategory as passed in to this method layer_constraints = categories['exposure']['layer_constraints'] for layer_constraint in layer_constraints: result = add_to_list(result, layer_constraint['data_type']) elif subcategory in [ x['id'] for x in cls.allowed_subcategories('hazard') ]: # implementation logic that returns the allowed data_types for # hazard layer with subcategory as passed in to this method layer_constraints = categories['hazard']['layer_constraints'] for layer_constraint in layer_constraints: result = add_to_list(result, layer_constraint['data_type']) else: # raise Exception('Invalid subcategory.') # TODO (ismailsunni): create custom exception to catch since it # will called by all impact function pass return result
def allowed_units(cls, subcategory, data_type): """Get the list of allowed units for a subcategory and data_type. .. note:: One data_type could be used by more than one subcategory, so we need to explicitly pass the subcategory to this function. Example usage:: foo = IF() meta = IF.metadata ubar = meta.allowed_data_types('structure') ubar > ['polygon'] :param subcategory: Required subcategory which will be used to subset the allowed data_types. :type subcategory: str :param data_type: Required data_type which will be used to subset the allowed units. :type data_type: str :returns: A list of one or more strings is returned. :rtype: list """ # pass # must implement here result = [] if data_type not in cls.allowed_data_types(subcategory): return result metadata_dict = cls.get_metadata() categories = metadata_dict['categories'] if subcategory in [ x['id'] for x in cls.allowed_subcategories('exposure') ]: # implementation logic that returns the allowed data_types for # exposure layer with subcategory as passed in to this method result = add_to_list(result, categories['exposure']['units']) elif subcategory in [ x['id'] for x in cls.allowed_subcategories('hazard') ]: # implementation logic that returns the allowed data_types for # hazard layer with subcategory as passed in to this method result = add_to_list(result, categories['hazard']['units']) else: # raise Exception('Invalid subcategory.') # TODO (ismailsunni): create custom exception to catch since it # will called by all impact function pass return result
def allowed_data_types(cls, subcategory): """Get the list of allowed data types for a subcategory. Example usage:: foo = IF() meta = IF.metadata ubar = meta.allowed_data_types('structure') ubar > ['polygon'] In the above example it does not show ‘numeric’ as the request is specific to the structure subcategory for that IF (using the IF declaration at the top of this file as the basis for IF()) Passing a subcategory is required otherwise the context of the data_type(s) would be ambiguous (i.e. whether they can be used as exposure or hazards). :param subcategory: Required subcategory which will be used to subset the allowed data_types. :type subcategory: str :returns: A list of one or more strings is returned. :rtype: list """ result = [] metadata_dict = cls.get_metadata() categories = metadata_dict['categories'] if subcategory in [x['id'] for x in cls.allowed_subcategories( 'exposure')]: # implementation logic that returns the allowed data_types for # exposure layer with subcategory as passed in to this method layer_constraints = categories['exposure']['layer_constraints'] for layer_constraint in layer_constraints: result = add_to_list(result, layer_constraint['data_type']) elif subcategory in [x['id'] for x in cls.allowed_subcategories( 'hazard')]: # implementation logic that returns the allowed data_types for # hazard layer with subcategory as passed in to this method layer_constraints = categories['hazard']['layer_constraints'] for layer_constraint in layer_constraints: result = add_to_list(result, layer_constraint['data_type']) else: # raise Exception('Invalid subcategory.') # TODO (ismailsunni): create custom exception to catch since it # will called by all impact function pass return result
def allowed_units(cls, subcategory, data_type): """Get the list of allowed units for a subcategory and data_type. .. note:: One data_type could be used by more than one subcategory, so we need to explicitly pass the subcategory to this function. Example usage:: foo = IF() meta = IF.metadata ubar = meta.allowed_data_types('structure') ubar > ['polygon'] :param subcategory: Required subcategory which will be used to subset the allowed data_types. :type subcategory: str :param data_type: Required data_type which will be used to subset the allowed units. :type data_type: str :returns: A list of one or more strings is returned. :rtype: list """ # pass # must implement here result = [] if not data_type in cls.allowed_data_types(subcategory): return result metadata_dict = cls.get_metadata() categories = metadata_dict['categories'] if subcategory in [x['id'] for x in cls.allowed_subcategories( 'exposure')]: # implementation logic that returns the allowed data_types for # exposure layer with subcategory as passed in to this method result = add_to_list(result, categories['exposure']['units']) elif subcategory in [x['id'] for x in cls.allowed_subcategories( 'hazard')]: # implementation logic that returns the allowed data_types for # hazard layer with subcategory as passed in to this method result = add_to_list(result, categories['hazard']['units']) else: # raise Exception('Invalid subcategory.') # TODO (ismailsunni): create custom exception to catch since it # will called by all impact function pass return result
def allowed_units(self, subcategory, data_type): """Determine allowed units from all impact functions. It uses subcategory and data_type as a filter. .. note:: One data_type could be used by more than one subcategory, so we need to explicitly pass the subcategory to this function. :param subcategory: Required subcategory which will be used to subset the allowed data_types. :type subcategory: str :param data_type: Required data_type which will be used to subset the allowed units. :type data_type: str :returns: A list of one or more strings is returned. :rtype: list """ result = [] for impact_function in self.impact_functions: my_allowed_units = impact_function.Metadata \ .allowed_units(subcategory, data_type) result = add_to_list(result, my_allowed_units) return result
def allowed_units(self, subcategory, data_type): """Determine allowed units from all impact functions. It uses subcategory and data_type as a filter. .. note:: One data_type could be used by more than one subcategory, so we need to explicitly pass the subcategory to this function. :param subcategory: Required subcategory which will be used to subset the allowed data_types. :type subcategory: str :param data_type: Required data_type which will be used to subset the allowed units. :type data_type: str :returns: A list of one or more strings is returned. :rtype: list """ result = [] for impact_function in self.impact_functions: my_allowed_units = impact_function.Metadata \ .allowed_units(subcategory, data_type) result = add_to_list(result, my_allowed_units) return result
def test_add_to_list(self): """Test for add_to_list function """ list_original = ['a', 'b', ['a'], {'a': 'b'}] list_a = ['a', 'b', ['a'], {'a': 'b'}] # add same immutable element list_b = add_to_list(list_a, 'b') assert list_b == list_original # add list list_b = add_to_list(list_a, ['a']) assert list_b == list_original # add same mutable element list_b = add_to_list(list_a, {'a': 'b'}) assert list_b == list_original # add new mutable element list_b = add_to_list(list_a, 'c') assert len(list_b) == (len(list_original) + 1) assert list_b[-1] == 'c'
def test_add_to_list(self): """Test for add_to_list function """ list_original = ['a', 'b', ['a'], {'a': 'b'}] list_a = ['a', 'b', ['a'], {'a': 'b'}] # add same immutable element list_b = add_to_list(list_a, 'b') assert list_b == list_original # add list list_b = add_to_list(list_a, ['a']) assert list_b == list_original # add same mutable element list_b = add_to_list(list_a, {'a': 'b'}) assert list_b == list_original # add new mutable element list_b = add_to_list(list_a, 'c') assert len(list_b) == (len(list_original) + 1) assert list_b[-1] == 'c'
def allowed_layer_constraints(cls, category=None): """Determine allowed layer constraints. It is optionally filtered by category. Example usage:: foo = IF() meta = IF.metadata ubar = meta.allowed_layer_constraints('exposure') ubar > [ { 'layer_type': 'vector', 'data_type': 'polygon' }, { 'layer_type': 'raster', 'data_type': 'numeric' } ] :param category: Optional category which will be used to subset the allowed layer_constraints. If omitted, all supported layer_constraints will be returned (for both hazard and exposure). Default is None. :type category: str :returns: A list of one or more dictionary is returned. :rtype: list """ result = [] if category is None: result = add_to_list(result, cls.allowed_layer_constraints('hazard')) result = add_to_list( result, cls.allowed_layer_constraints('exposure')) return result else: metadata_dict = cls.get_metadata() categories = metadata_dict['categories'] return categories[category]['layer_constraints']
def allowed_layer_constraints(cls, category=None): """Determine allowed layer constraints. It is optionally filtered by category. Example usage:: foo = IF() meta = IF.metadata ubar = meta.allowed_layer_constraints('exposure') ubar > [ { 'layer_type': 'vector', 'data_type': 'polygon' }, { 'layer_type': 'raster', 'data_type': 'numeric' } ] :param category: Optional category which will be used to subset the allowed layer_constraints. If omitted, all supported layer_constraints will be returned (for both hazard and exposure). Default is None. :type category: str :returns: A list of one or more dictionary is returned. :rtype: list """ result = [] if category is None: result = add_to_list(result, cls.allowed_layer_constraints('hazard')) result = add_to_list(result, cls.allowed_layer_constraints('exposure')) return result else: metadata_dict = cls.get_metadata() categories = metadata_dict['categories'] return categories[category]['layer_constraints']
def categories_for_layer(self, layer_type, data_type): """Return a list of valid categories for a layer. This method is used to determine if a given layer can be used as a hazard, exposure or aggregation layer. Example usage:: foo = categories_for_layer('vector', 'polygon') print foo Would output this:: ['hazard', 'exposure', 'aggregation'] While passing a vector point layer:: foo = units_for_layer('vector', 'point') print foo Might return this:: ['hazard', 'exposure'] In the returned the values are categories (if any) applicable for that layer_type and data_type. :param layer_type: The type for this layer. Valid values would be, 'raster' or 'vector'. :type layer_type: str :param data_type: The data_type for this layer. Valid possibilities would be 'numeric' (for raster), point, line, polygon (for vectors). :type data_type: str :returns: A list as per the example above where each value represents a valid category. :rtype: list """ result = [] for impact_function in self.impact_functions: my_categories = impact_function.Metadata \ .categories_for_layer(layer_type, data_type) result = add_to_list(result, my_categories) categories_definitions = [] for my_category in result: if my_category == 'hazard': categories_definitions.append(hazard_definition) elif my_category == 'exposure': categories_definitions.append(exposure_definition) else: raise Exception('Unsupported categories') return categories_definitions
def categories_for_layer(self, layer_type, data_type): """Return a list of valid categories for a layer. This method is used to determine if a given layer can be used as a hazard, exposure or aggregation layer. Example usage:: foo = categories_for_layer('vector', 'polygon') print foo Would output this:: ['hazard', 'exposure', 'aggregation'] While passing a vector point layer:: foo = units_for_layer('vector', 'point') print foo Might return this:: ['hazard', 'exposure'] In the returned the values are categories (if any) applicable for that layer_type and data_type. :param layer_type: The type for this layer. Valid values would be, 'raster' or 'vector'. :type layer_type: str :param data_type: The data_type for this layer. Valid possibilities would be 'numeric' (for raster), point, line, polygon (for vectors). :type data_type: str :returns: A list as per the example above where each value represents a valid category. :rtype: list """ result = [] for impact_function in self.impact_functions: categories = impact_function.Metadata \ .categories_for_layer(layer_type, data_type) result = add_to_list(result, categories) categories_definitions = [] for category in result: if category == 'hazard': categories_definitions.append(hazard_definition) elif category == 'exposure': categories_definitions.append(exposure_definition) else: raise Exception('Unsupported categories') return categories_definitions
def units_for_layer(self, subcategory, layer_type, data_type): """Get the valid units for a layer. Example usage:: foo = units_for_layer('flood', 'vector', 'polygon') print foo Would output this:: {'Wet/Dry': ['wet','dry']} While passing a raster layer:: foo = units_for_layer('flood', 'raster', None) print foo Might return this:: { 'metres': None, 'feet': None, 'wet/dry': ['wet', 'dry'], } In the returned dictionary the keys are unit types and the values are the categories (if any) applicable for that unit type. :param subcategory: The subcategory for this layer. :type subcategory: str :param layer_type: The type for this layer. Valid values would be, 'raster' or 'vector'. :type layer_type: str :param data_type: The data_type for this layer. Valid possibilities would be 'numeric' (for raster), point, line, polygon (for vectors). :type data_type: str :returns: A dictionary as per the example above where each key represents a unit and each value that is not None represents a list of categories. :rtype: dict """ result = [] for impact_function in self.impact_functions: my_units = impact_function.Metadata \ .units_for_layer(subcategory, layer_type, data_type) result = add_to_list(result, my_units) return result
def units_for_layer(self, subcategory, layer_type, data_type): """Get the valid units for a layer. Example usage:: foo = units_for_layer('flood', 'vector', 'polygon') print foo Would output this:: {'Wet/Dry': ['wet','dry']} While passing a raster layer:: foo = units_for_layer('flood', 'raster', None) print foo Might return this:: { 'metres': None, 'feet': None, 'wet/dry': ['wet', 'dry'], } In the returned dictionary the keys are unit types and the values are the categories (if any) applicable for that unit type. :param subcategory: The subcategory for this layer. :type subcategory: str :param layer_type: The type for this layer. Valid values would be, 'raster' or 'vector'. :type layer_type: str :param data_type: The data_type for this layer. Valid possibilities would be 'numeric' (for raster), point, line, polygon (for vectors). :type data_type: str :returns: A dictionary as per the example above where each key represents a unit and each value that is not None represents a list of categories. :rtype: dict """ result = [] for impact_function in self.impact_functions: my_units = impact_function.Metadata \ .units_for_layer(subcategory, layer_type, data_type) result = add_to_list(result, my_units) return result
def allowed_subcategories(self, category=None): """Determine allowed subcategories, optionally filtered by category. :param category: Optional category which will be used to subset the allowed subcategories. If omitted, all supported subcategories will be returned (for both hazard and exposure). Default is None. :type category: str :returns: A list of strings is returned. :rtype: list """ result = [] for impact_function in self.impact_functions: my_allowed_subcategories = impact_function.Metadata\ .allowed_subcategories(category) result = add_to_list(result, my_allowed_subcategories) return result
def allowed_subcategories(self, category=None): """Determine allowed subcategories, optionally filtered by category. :param category: Optional category which will be used to subset the allowed subcategories. If omitted, all supported subcategories will be returned (for both hazard and exposure). Default is None. :type category: str :returns: A list of strings is returned. :rtype: list """ result = [] for impact_function in self.impact_functions: my_allowed_subcategories = impact_function.Metadata\ .allowed_subcategories(category) result = add_to_list(result, my_allowed_subcategories) return result
def subcategories_for_layer(self, category, layer_type, data_type): """Return a list of valid subcategories for a layer. This method is used to determine which subcategories a given layer can be for. Example usage:: foo = subcategories_for_layer('vector', 'polygon', 'exposure') print foo Would output this:: ['flood', 'landuse'] In the returned the values are categories (if any) applicable for that layer_type and data_type. :param layer_type: The type for this layer. Valid values would be, 'raster' or 'vector'. :type layer_type: str :param data_type: The data_type for this layer. Valid possibilities would be 'numeric' (for raster), point, line, polygon (for vectors). :type data_type: str :param category: The category for this layer. Valid possibilities would be 'hazard', 'exposure' and 'aggregation'. :type category: str :returns: A list as per the example above where each value represents a valid subcategory. :rtype: list """ result = [] for impact_function in self.impact_functions: subcategories = impact_function.Metadata \ .subcategories_for_layer(category, layer_type, data_type) result = add_to_list(result, subcategories) return result
def subcategories_for_layer(self, category, layer_type, data_type): """Return a list of valid subcategories for a layer. This method is used to determine which subcategories a given layer can be for. Example usage:: foo = subcategories_for_layer('vector', 'polygon', 'exposure') print foo Would output this:: ['flood', 'landuse'] In the returned the values are categories (if any) applicable for that layer_type and data_type. :param layer_type: The type for this layer. Valid values would be, 'raster' or 'vector'. :type layer_type: str :param data_type: The data_type for this layer. Valid possibilities would be 'numeric' (for raster), point, line, polygon (for vectors). :type data_type: str :param category: The category for this layer. Valid possibilities would be 'hazard', 'exposure' and 'aggregation'. :type category: str :returns: A list as per the example above where each value represents a valid subcategory. :rtype: list """ result = [] for impact_function in self.impact_functions: my_subcategories = impact_function.Metadata \ .subcategories_for_layer(category, layer_type, data_type) result = add_to_list(result, my_subcategories) return result
def allowed_subcategories(cls, category=None): """Get the list of allowed subcategories for a given category. :param category: Optional category which will be used to subset the allowed subcategories. If omitted, all supported subcategories will be returned (for both hazard and exposure). Default is None. :type category: str :returns: A list of strings is returned. :rtype: list """ result = [] if category is None: return cls.allowed_subcategories('exposure') + cls\ .allowed_subcategories('hazard') else: metadata_dict = cls.get_metadata() categories = metadata_dict['categories'] result = add_to_list(result, categories[category]['subcategory']) return result
def allowed_subcategories(cls, category=None): """Get the list of allowed subcategories for a given category. :param category: Optional category which will be used to subset the allowed subcategories. If omitted, all supported subcategories will be returned (for both hazard and exposure). Default is None. :type category: str :returns: A list of strings is returned. :rtype: list """ result = [] if category is None: return cls.allowed_subcategories('exposure') + cls\ .allowed_subcategories('hazard') else: metadata_dict = cls.get_metadata() categories = metadata_dict['categories'] result = add_to_list(result, categories[category]['subcategory']) return result
def allowed_data_types(self, subcategory): """Determine allowed data types for all impact functions. It uses subcategory as a filter. Passing a subcategory is required otherwise the context of the data_type(s) would be ambiguous (i.e. whether they can be used as exposure or hazards). :param subcategory: Required subcategory which will be used to subset the allowed data_types. :type subcategory: str :returns: A list of one or more strings is returned. :rtype: list """ result = [] for impact_function in self.impact_functions: my_allowed_data_types = impact_function.Metadata \ .allowed_data_types(subcategory) result = add_to_list(result, my_allowed_data_types) return result
def allowed_data_types(self, subcategory): """Determine allowed data types for all impact functions. It uses subcategory as a filter. Passing a subcategory is required otherwise the context of the data_type(s) would be ambiguous (i.e. whether they can be used as exposure or hazards). :param subcategory: Required subcategory which will be used to subset the allowed data_types. :type subcategory: str :returns: A list of one or more strings is returned. :rtype: list """ result = [] for impact_function in self.impact_functions: my_allowed_data_types = impact_function.Metadata \ .allowed_data_types(subcategory) result = add_to_list(result, my_allowed_data_types) return result