### Navigation

- index
- modules
- next
- previous
- Home »
- Contents »
- InaSAFE’s API documentation »
- Package::safe »
- Package::safe.impact_functions »

# Module: safe.impact_functions.core

Function to manage self-registering plugins

The design is based on http://effbot.org/zone/metaclass-plugins.htm

To register the plugin, the module must be imported by the Python process using it.

class `safe.impact_functions.core.``FunctionProvider`

  Mount point for plugins which refer to actions that can be performed.

  Plugins implementing this reference should provide the following method:

  run(layers)

  layers | A list of layers
  result | A list of layers

`safe.impact_functions.core.``aggregate`(data=None, boundaries=None, attribute_name=None, aggregation_function='count')

  Clip data to boundaries and aggregate their values for each.

  Input

    data: Point or Raster dataset boundaries: Polygon dataset attribute_name: Name of attribute to aggrate over.

    > This is only applicable for vector data

    aggregation_function: Function to apply (‘count’ or ‘sum’)

  Output
    Dictionary of {boundary_name: aggregated value}

`safe.impact_functions.core.``aggregate_point_data`(data=None, boundaries=None, attribute_name=None, aggregation_function='count')

  Clip data to boundaries and aggregate their values for each.

  Input
    data: Point dataset boundaries: Polygon dataset attribute_name: Name of attribute to aggrate over. aggregation_function: Function to apply (‘count’ or ‘sum’)
  Output
    List of aggregated values for each polygon.
  Note

    Aggregated values depend on aggregation function: ‘sum’: Sum of values for attribute_name ‘count’: Dictionary with counts of occurences of each value

    > of attribute_name

`safe.impact_functions.core.``compatible_layers`(func, layer_descriptors)

  Fetches all the layers that match the plugin requirements.

  Input
    func: ? (FIXME(Ole): Ted, can you fill in here? layer_descriptor: Layer names and meta data (keywords, type, etc)
  Output:
    Array of compatible layers, can be an empty list.

`safe.impact_functions.core.``extract_layers`(layers, keyword, value)

  Extract layers with specified keyword/value pair

`safe.impact_functions.core.``format_int`(x)

  Format integer with separator between thousands.

  Args:
    x: int - a number to be formatted in a locale friendly way.
  Returns:
    str - a locale friendly formatted string e.g. 1,000,0000.00
      representing the original x. If a ValueError exception occurs, x is simply returned.
  Raises:
    None
  From http://stackoverflow.com/questions/5513615/
    add-thousands-separators-to-a-number

  # FIXME (Ole) Currently not using locale coz broken

  Instead use this: http://docs.python.org/library/string.html#formatspec

`safe.impact_functions.core.``get_admissible_plugins`(keywords=None)

  Get plugins that match specified keywords

  > Input
  >   keywords: Either dictionary or list of dictionaries containing

  >     layer keywords of the form {‘category’: ‘hazard’, ‘subcategory’: ‘flood’, ...}

  >     If None or empty all plugins are returned

  # name: Optional impact function name (or part of function name) # used to further filter the result. # If None all names are considered to match

  > Output
  >   Dictionary of impact functions ({name: class})

`safe.impact_functions.core.``get_documentation`(func)

  Collect documentaion of a impact function and return it as a dictionary

  Args:
    - func : name of function
  Returns:
    - Dictionary contains:

        author : string (identified by :author) synopsis : string (first line) rating : integer (identified by :rating) param_req : list of param (identified by :param requires) detail : detail description (function properties) citation : list of citation in string (function properties) limitation : string (function properties)

`safe.impact_functions.core.``get_exposure_layer`(layers)

  Get exposure layer from list of layers

  If there are more than one, only the first is returned. Use get_hazard_layers if more are expected and needed

  If no layers fit the description None is returned

`safe.impact_functions.core.``get_exposure_layers`(layers)

  Get list of layers that have category==’exposure’

`safe.impact_functions.core.``get_function_title`(func)

  Get title for impact function

  Input
    func: Impact function class
  Output
    it’s title if available as an attribute in the class description, otherwise what is returned by the function pretty_function_name.

`safe.impact_functions.core.``get_hazard_layer`(layers)

  Get hazard layer from list of layers

  If there are more than one, only the first is returned. Use get_hazard_layers if more are expected and needed

  If no layers fit the description None is returned

`safe.impact_functions.core.``get_hazard_layers`(layers)

  Get list of layers that have category==’hazard’

`safe.impact_functions.core.``get_plugin`(name)

  Get plugin that matches given name

  This is just a wrapper around get_plugins to simplify the overly complicated way of extracting the function

`safe.impact_functions.core.``get_plugins`(name=None)

  Retrieve a list of plugins that match the name you pass

  Or all of them if no name is passed.

`safe.impact_functions.core.``get_plugins_as_table`(dict_filter=None)

  Retrieve a table listing all plugins and their requirements.

  Or just a single plugin if name is passed.

  Args:
    - dict_filter = dictionary that contains filters
        - id = list_id
        - title = list_title
        - category : list_category
        - subcategory : list_subcategory
        - layertype : list_layertype
        - datatype : list_datatype
        - unit: list_unit
        - disabled : list_disabled # not included
  Returns:
    - table contains plugins match with dict_filter

  Raises: None

`safe.impact_functions.core.``get_question`(hazard_title, exposure_title, func)

  Rephrase the question asked

  Input
    hazard_title: string exposure_title: string func: impact function class

`safe.impact_functions.core.``get_thresholds`(layer)

  Extract thresholds form layer keywords if present

`safe.impact_functions.core.``get_unique_values`()

  Get unique possible value for each column in impact functions doc table.

  Args: None

  Returns:
    - Dictionary contains list unique value for each column

`safe.impact_functions.core.``is_function_enabled`(func)

  Check whether a function is enabled or not :param func: :return: False is disabled param is True

`safe.impact_functions.core.``parse_single_requirement`(requirement)

  Parse single requirement from impact function’s doc to category, subcategory, layertype, datatype, unit, and disabled.

`safe.impact_functions.core.``pretty_function_name`(func)

  Return a human readable name for the function if the function has a func.plugin_name use this otherwise turn underscores to spaces and Caps to spaces

`safe.impact_functions.core.``requirement_check`(params, require_str, verbose=False)

  Checks a dictionary params against the requirements defined in require_str. Require_str must be a valid python expression and evaluate to True or False

`safe.impact_functions.core.``requirements_collect`(func)

  Collect the requirements from the plugin function doc

  The requirements need to be specified using
    :param requires <valid python expression>

  The layer keywords are put into the local name space each requires should be on a new line a ‘’ at the end of a line will be a continuation

  returns a (possibly empty) list of Python expressions

  Example of valid requirements expression :param requires category==’hazard’ and subcategory in [‘flood’, ‘tsunami’] and layertype==’raster’ and unit==’m’

`safe.impact_functions.core.``requirements_met`(requirements, params)

  Checks the plugin can run with a given layer.

  Based on the requirements specified in the doc string.

  Returns:
    True: if there are no requirements or they are all met. False: if it has requirements and none of them are met.

This module forms part of the InaSAFE tool.

### Navigation

- index
- modules
- next
- previous
- Home »
- Contents »
- InaSAFE’s API documentation »
- Package::safe »
- Package::safe.impact_functions »