本文整理汇总了Python中safe.impact_functions.registry.Registry.filter方法的典型用法代码示例。如果您正苦于以下问题:Python Registry.filter方法的具体用法?Python Registry.filter怎么用?Python Registry.filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe.impact_functions.registry.Registry
的用法示例。
在下文中一共展示了Registry.filter方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_filter_by_metadata
# 需要导入模块: from safe.impact_functions.registry import Registry [as 别名]
# 或者: from safe.impact_functions.registry.Registry import filter [as 别名]
def test_filter_by_metadata(self):
"""TestRegistry: Test filtering IF by hazard and exposure metadata."""
hazard_metadata = {
'subcategory': hazard_flood,
'units': unit_wetdry,
'layer_constraints': layer_vector_polygon
}
exposure_metadata = {
'subcategory': exposure_structure,
'units': unit_building_type_type,
'layer_constraints': layer_vector_polygon
}
registry = Registry()
impact_functions = registry.filter(hazard_metadata, exposure_metadata)
expected = [FloodPolygonBuildingFunction]
message = 'Expecting %s. Got %s instead' % (expected, impact_functions)
self.assertEqual(expected, impact_functions, message)
示例2: test_filter_by_metadata
# 需要导入模块: from safe.impact_functions.registry import Registry [as 别名]
# 或者: from safe.impact_functions.registry.Registry import filter [as 别名]
def test_filter_by_metadata(self):
"""TestRegistry: Test filtering IF by hazard and exposure metadata."""
hazard_metadata = {
'layer_mode': layer_mode_continuous,
'layer_geometry': layer_geometry_raster,
'hazard_category': hazard_category_single_event,
'hazard': hazard_earthquake,
'continuous_hazard_unit': unit_mmi
}
exposure_metadata = {
'layer_mode': layer_mode_classified,
'layer_geometry': layer_geometry_point,
'exposure': exposure_structure,
}
registry = Registry()
impact_functions = registry.filter(hazard_metadata, exposure_metadata)
expected = [EarthquakeBuildingFunction]
message = 'Expecting \n%s.\n\nGot \n%s instead' % (
'\n'.join([x.__name__ for x in expected]),
'\n'.join([x.__name__ for x in impact_functions]))
self.assertEqual(expected, impact_functions, message)
示例3: ImpactFunctionManager
# 需要导入模块: from safe.impact_functions.registry import Registry [as 别名]
# 或者: from safe.impact_functions.registry.Registry import filter [as 别名]
class ImpactFunctionManager(object):
"""Class for managing metadata for all impact function.
.. versionadded:: 2.1
"""
def __init__(self):
"""Constructor."""
# Singleton Registry to track all the registered Impact Functions
self.registry = Registry()
@property
def impact_functions(self):
"""Return all registered impact functions."""
return self.registry.impact_functions
def get_instance(self, class_name):
"""Return an instance of an impact function given its class name.
.. example::
if_manager = ImpactFunctionManager()
if_class_name = 'FloodBuildingImpactFunction'
if = if_manager.get_instance(if_class_name)
:param class_name: The name of IF class.
:type class_name: str
:return: Impact function instance that matches the argument.
:rtype: safe.impact_functions.base.ImpactFunction
"""
return self.registry.get_instance(class_name)
def get_class(self, class_name):
"""Return the class of an impact function given its class name.
.. example::
if_manager = ImpactFunctionManager()
if_class_name = 'FloodBuildingImpactFunction'
if_class = if_manager.get_class(if_class_name)
:param class_name: the name of IF class
:type class_name: str
:return: impact function class that matches the argument.
:rtype: safe.impact_functions.base.ImpactFunction
"""
return self.registry.get_class(class_name)
def get(self, impact_function_id):
"""Return an instance of an impact function given its ID.
This is a preferred way to get an instance of IF. IF should have a
unique human readable ID in their metadata.
.. example::
if_manager = ImpactFunctionManager()
if_id = 'FloodBuildingImpactFunction'
if = if_manager.get(if_id)
:param impact_function_id: The ID of impact function in the metadata.
:type impact_function_id: str
:return An Impact function instance that has matched id.
:rtype: safe.impact_functions.base.ImpactFunction
"""
impact_functions = self.registry.filter_by_metadata("id", impact_function_id)
if len(impact_functions) == 0:
raise Exception("Impact function with ID: %s not found" % impact_function_id)
elif len(impact_functions) > 1:
raise Exception("There are some Impact Functions that have the same ID: %s" % impact_function_id)
return impact_functions[0].instance()
def filter(self, hazard_metadata=None, exposure_metadata=None):
"""Get available impact functions from hazard and exposure metadata.
Disabled impact function will not be loaded.
.. example::
if_manager = ImpactFunctionManager()
hazard_metadata = {
'subcategory': hazard_flood,
'units': unit_wetdry,
'layer_constraints': layer_vector_polygon
}
exposure_metadata = {
'subcategory': exposure_structure,
'units': unit_building_type_type,
'layer_constraints': layer_vector_polygon
}
ifs = if_manager.filter(hazard_metadata, exposure_metadata)
:param hazard_metadata: The metadata of the hazard.
:type hazard_metadata: dict
:param exposure_metadata: The metadata of the exposure.
:type exposure_metadata: dict
#.........这里部分代码省略.........
示例4: ImpactFunctionManager
# 需要导入模块: from safe.impact_functions.registry import Registry [as 别名]
# 或者: from safe.impact_functions.registry.Registry import filter [as 别名]
class ImpactFunctionManager(object):
"""Class for managing metadata for all impact function.
.. versionadded:: 2.1
"""
def __init__(self):
"""Constructor."""
# Singleton Registry to track all the registered Impact Functions
self.registry = Registry()
@property
def impact_functions(self):
"""Return all registered impact functions."""
return self.registry.impact_functions
def get_instance(self, class_name):
"""Return an instance of an impact function given its class name.
.. example::
if_manager = ImpactFunctionManager()
if_class_name = 'FloodBuildingImpactFunction'
if = if_manager.get_instance(if_class_name)
:param class_name: The name of IF class.
:type class_name: str
:return: Impact function instance that matches the argument.
:rtype: safe.impact_functions.base.ImpactFunction
"""
return self.registry.get_instance(class_name)
def get_class(self, class_name):
"""Return the class of an impact function given its class name.
.. example::
if_manager = ImpactFunctionManager()
if_class_name = 'FloodBuildingImpactFunction'
if_class = if_manager.get_class(if_class_name)
:param class_name: the name of IF class
:type class_name: str
:return: impact function class that matches the argument.
:rtype: safe.impact_functions.base.ImpactFunction
"""
return self.registry.get_class(class_name)
def get(self, impact_function_id):
"""Return an instance of an impact function given its ID.
This is a preferred way to get an instance of IF. IF should have a
unique human readable ID in their metadata.
.. example::
if_manager = ImpactFunctionManager()
if_id = 'FloodBuildingImpactFunction'
if = if_manager.get(if_id)
:param impact_function_id: The ID of impact function in the metadata.
:type impact_function_id: str
:return An Impact function instance that has matched id.
:rtype: safe.impact_functions.base.ImpactFunction
"""
impact_functions = self.registry.filter_by_metadata(
'id', impact_function_id)
if len(impact_functions) == 0:
raise Exception(
'Impact function with ID: %s not found' % impact_function_id)
elif len(impact_functions) > 1:
raise Exception(
'There are some Impact Functions that have the same ID: %s' %
impact_function_id)
return impact_functions[0].instance()
def filter(self, hazard_metadata=None, exposure_metadata=None):
"""Get available impact functions from hazard and exposure metadata.
Disabled impact function will not be loaded.
.. example::
if_manager = ImpactFunctionManager()
hazard_metadata = {
'subcategory': hazard_flood,
'units': unit_wetdry,
'layer_constraints': layer_vector_polygon
}
exposure_metadata = {
'subcategory': exposure_structure,
'units': unit_building_type_type,
'layer_constraints': layer_vector_polygon
}
ifs = if_manager.filter(hazard_metadata, exposure_metadata)
:param hazard_metadata: The metadata of the hazard.
#.........这里部分代码省略.........