本文整理汇总了Python中safe.impact_function.impact_function.ImpactFunction类的典型用法代码示例。如果您正苦于以下问题:Python ImpactFunction类的具体用法?Python ImpactFunction怎么用?Python ImpactFunction使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImpactFunction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_pre_processors_earthquake_contour
def test_pre_processors_earthquake_contour(self):
"""Test the pre_processors_earthquake_contour"""
hazard_layer = load_test_raster_layer(
'gisv4', 'hazard', 'earthquake.asc')
exposure_layer = load_test_vector_layer(
'gisv4', 'exposure', 'building-points.geojson')
impact_function = ImpactFunction()
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
impact_function.crs = QgsCoordinateReferenceSystem(4326)
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
self.assertTrue(
pre_processor_earthquake_contour['condition'](impact_function))
hazard_layer = load_test_raster_layer(
'hazard', 'classified_flood_20_20.asc')
exposure_layer = load_test_vector_layer(
'gisv4', 'exposure', 'places.geojson')
impact_function = ImpactFunction()
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
impact_function.crs = QgsCoordinateReferenceSystem(4326)
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
# not ok, since the hazard is flood, not earthquake
self.assertFalse(
pre_processor_earthquake_contour['condition'](impact_function))
示例2: test_pre_processors_nearby_places
def test_pre_processors_nearby_places(self):
"""Test the pre_processors_nearby_places"""
hazard_layer = load_test_raster_layer(
'gisv4', 'hazard', 'earthquake.asc')
exposure_layer = load_test_vector_layer(
'gisv4', 'exposure', 'building-points.geojson')
impact_function = ImpactFunction()
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
impact_function.crs = QgsCoordinateReferenceSystem(4326)
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
# The exposure is not place but buildings
self.assertFalse(
pre_processors_nearby_places['condition'](impact_function))
hazard_layer = load_test_raster_layer(
'gisv4', 'hazard', 'earthquake.asc')
exposure_layer = load_test_vector_layer(
'gisv4', 'exposure', 'places.geojson')
impact_function = ImpactFunction()
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
impact_function.crs = QgsCoordinateReferenceSystem(4326)
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
# EQ on places, it must be OK.
self.assertTrue(
pre_processors_nearby_places['condition'](impact_function))
示例3: test_profiling
def test_profiling(self):
"""Test running impact function on test data."""
hazard_layer = load_test_vector_layer(
'gisv4', 'hazard', 'classified_vector.geojson')
exposure_layer = load_test_vector_layer(
'gisv4', 'exposure', 'building-points.geojson')
aggregation_layer = load_test_vector_layer(
'gisv4', 'aggregation', 'small_grid.geojson')
# Set up impact function
impact_function = ImpactFunction()
impact_function.aggregation = aggregation_layer
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
status, message = impact_function.run()
self.assertEqual(ANALYSIS_FAILED_BAD_INPUT, status, message)
impact_function.prepare()
status, message = impact_function.run()
self.assertEqual(ANALYSIS_SUCCESS, status, message)
message = impact_function.performance_log_message().to_text()
expected_result = get_control_text(
'test-profiling-logs.txt')
for line in expected_result:
line = line.replace('\n', '')
if line == '' or line == '-':
continue
self.assertIn(line, message)
# Notes(IS): For some unknown reason I need to do this to make
# test_provenance pass
del hazard_layer
示例4: test_impact_function_behaviour
def test_impact_function_behaviour(self):
"""Test behaviour of impact function."""
hazard_layer = load_test_vector_layer(
'hazard', 'flood_multipart_polygons.shp')
exposure_layer = load_test_vector_layer('exposure', 'roads.shp')
impact_function = ImpactFunction()
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
impact_function.prepare()
self.assertEqual(impact_function.name, 'Flood Polygon On Road Line')
self.assertEqual(impact_function.title, 'be affected')
示例5: test_post_processor
def test_post_processor(self):
"""Test for running post processor."""
impact_layer = load_test_vector_layer(
'impact',
'indivisible_polygon_impact.geojson',
clone_to_memory=True)
impact_layer.keywords['hazard_keywords'] = {
'classification': 'flood_hazard_classes'
}
impact_layer.keywords['exposure_keywords'] = {
'exposure': 'structure',
}
def debug_layer(layer, add_to_datastore):
"""Monkey patching because we can't check inasafe_fields.
:param layer: The layer.
:type layer: QgsVectorLayer
:param add_to_datastore: Flag
:type add_to_datastore: bool
"""
# We do nothing here.
return layer, add_to_datastore
impact_function = ImpactFunction()
impact_function.debug_layer = debug_layer
impact_function.post_process(impact_layer)
used_post_processors = [
post_processor_size,
post_processor_male,
post_processor_female,
post_processor_youth,
post_processor_adult,
post_processor_elderly,
]
# Check if new field is added
impact_fields = impact_layer.dataProvider().fieldNameMap().keys()
for post_processor in used_post_processors:
# noinspection PyTypeChecker
for output_value in post_processor['output'].values():
field_name = output_value['value']['field_name']
self.assertIn(field_name, impact_fields)
print_attribute_table(impact_layer, 1)
示例6: test_provenance_without_aggregation
def test_provenance_without_aggregation(self):
"""Test provenance of impact function without aggregation."""
hazard_layer = load_test_vector_layer(
'gisv4', 'hazard', 'classified_vector.geojson')
exposure_layer = load_test_vector_layer(
'gisv4', 'exposure', 'building-points.geojson')
hazard = definition(hazard_layer.keywords['hazard'])
exposure = definition(exposure_layer.keywords['exposure'])
hazard_category = definition(hazard_layer.keywords['hazard_category'])
expected_provenance = {
'gdal_version': gdal.__version__,
'host_name': gethostname(),
'map_title': get_map_title(hazard, exposure, hazard_category),
'map_legend_title': exposure['layer_legend_title'],
'inasafe_version': get_version(),
'pyqt_version': PYQT_VERSION_STR,
'qgis_version': QGis.QGIS_VERSION,
'qt_version': QT_VERSION_STR,
'user': getpass.getuser(),
'os': readable_os_version(),
'aggregation_layer': None,
'aggregation_layer_id': None,
'exposure_layer': exposure_layer.source(),
'exposure_layer_id': exposure_layer.id(),
'hazard_layer': hazard_layer.source(),
'hazard_layer_id': hazard_layer.id(),
'analysis_question': get_analysis_question(hazard, exposure),
'aggregation_keywords': None,
'exposure_keywords': deepcopy(exposure_layer.keywords),
'hazard_keywords': deepcopy(hazard_layer.keywords),
}
# Set up impact function
impact_function = ImpactFunction()
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
status, message = impact_function.run()
self.assertEqual(ANALYSIS_SUCCESS, status, message)
self.maxDiff = None
expected_provenance.update({
'action_checklist': impact_function.action_checklist(),
'analysis_extent': impact_function.analysis_extent.exportToWkt(),
'impact_function_name': impact_function.name,
'impact_function_title': impact_function.title,
'notes': impact_function.notes(),
'requested_extent': impact_function.requested_extent,
'data_store_uri': impact_function.datastore.uri_path,
'start_datetime': impact_function.start_datetime,
'end_datetime': impact_function.end_datetime,
'duration': impact_function.duration
})
self.assertDictEqual(expected_provenance, impact_function.provenance)
示例7: test_old_fields_keywords
def test_old_fields_keywords(self):
"""The IF is not ready with we have some wrong inasafe_fields."""
hazard_layer = load_test_vector_layer(
'gisv4', 'hazard', 'classified_vector.geojson')
exposure_layer = load_test_vector_layer(
'gisv4', 'exposure', 'building-points.geojson',
clone=True)
aggregation_layer = load_test_vector_layer(
'gisv4', 'aggregation', 'small_grid.geojson')
impact_function = ImpactFunction()
impact_function.aggregation = aggregation_layer
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
status, message = impact_function.prepare()
# The layer should be fine.
self.assertEqual(PREPARE_SUCCESS, status, message)
# Now, we remove one field
exposure_layer.startEditing()
field = exposure_layer.keywords['inasafe_fields'].values()[0]
index = exposure_layer.fieldNameIndex(field)
exposure_layer.deleteAttribute(index)
exposure_layer.commitChanges()
# It shouldn't be fine as we removed one field which
# was in inasafe_fields
status, message = impact_function.prepare()
self.assertNotEqual(PREPARE_SUCCESS, status, message)
示例8: test_analysis_earthquake_summary
def test_analysis_earthquake_summary(self):
"""Test we can compute summary after an EQ on population."""
hazard = load_test_raster_layer('gisv4', 'hazard', 'earthquake.asc')
exposure = load_test_raster_layer(
'gisv4', 'exposure', 'raster', 'population.asc')
aggregation = load_test_vector_layer(
'gisv4', 'aggregation', 'small_grid.geojson')
impact_function = ImpactFunction()
impact_function.hazard = hazard
impact_function.exposure = exposure
impact_function.aggregation = aggregation
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
status, message = impact_function.run()
self.assertEqual(ANALYSIS_SUCCESS, status, message)
layer = impact_function.analysis_impacted
classification = hazard.keywords['classification']
classes = definition(classification)['classes']
for hazard_class in classes:
field_name = hazard_count_field['field_name'] % hazard_class['key']
message = '%s is not found in the EQ summary layer.' % field_name
self.assertNotEqual(-1, layer.fieldNameIndex(field_name), message)
check_inasafe_fields(impact_function.analysis_impacted)
check_inasafe_fields(impact_function.aggregation_summary)
示例9: test_keyword_monkey_patch
def test_keyword_monkey_patch(self):
"""Test behaviour of generating keywords."""
exposure_path = standard_data_path('exposure', 'building-points.shp')
# noinspection PyCallingNonCallable
exposure_layer = QgsVectorLayer(exposure_path, 'Building', 'ogr')
impact_function = ImpactFunction()
impact_function.exposure = exposure_layer
impact_function._check_layer(impact_function.exposure, 'exposure')
expected_inasafe_fields = {
exposure_type_field['key']: 'TYPE',
}
self.assertDictEqual(
exposure_layer.keywords['inasafe_fields'], expected_inasafe_fields)
fields = impact_function.exposure.dataProvider().fieldNameMap().keys()
self.assertIn(
exposure_layer.keywords['inasafe_fields']['exposure_type_field'],
fields
)
inasafe_fields = exposure_layer.keywords['inasafe_fields']
示例10: test_raster_post_minimum_needs_value_generation
def test_raster_post_minimum_needs_value_generation(self):
"""Test minimum needs postprocessors on raster exposure.
Minimum needs postprocessors is defined to only generate values
when exposure contains population data.
Especially important to test, since on raster exposure the population
field is generated on the fly.
The postprocessors need to expect generated population field exists.
"""
# # #
# Test with raster exposure data with population_exposure_count
# exists.
# # #
hazard_layer = load_test_raster_layer(
'hazard', 'tsunami_wgs84.tif')
exposure_layer = load_test_raster_layer(
'exposure', 'pop_binary_raster_20_20.asc')
impact_function = ImpactFunction()
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
impact_function.prepare()
return_code, message = impact_function.run()
self.assertEqual(return_code, ANALYSIS_SUCCESS, message)
# minimum needs fields should exists in the results
self._check_minimum_fields_exists(impact_function)
# TODO: should include demographic postprocessor value too
expected_value = {
u'total_affected': 9.208200000039128,
u'minimum_needs__rice': 25,
u'minimum_needs__toilets': 0,
u'minimum_needs__drinking_water': 161,
u'minimum_needs__clean_water': 616,
u'male': 4,
u'female': 4,
u'youth': 2,
u'adult': 6,
u'elderly': 0,
u'total': 162.7667000000474,
u'minimum_needs__family_kits': 1,
u'total_not_affected': 153.55850000000828,
}
self._check_minimum_fields_value(expected_value, impact_function)
示例11: impact_function_setup
def impact_function_setup(
command_line_arguments, hazard, exposure, aggregation=None):
"""Sets up an analysis object.
.. versionadded:: 3.2
:param command_line_arguments: User inputs.
:type command_line_arguments: CommandLineArguments
:param hazard: Hazard layer
:type hazard: QgsLayer
:param exposure: Exposure Layer
:type exposure: QgsLayer
:param aggregation: Aggregation Layer
:type aggregation: QgsLayer
:raises: Exception
"""
# IF
impact_function = ImpactFunction()
impact_function.hazard = hazard
impact_function.exposure = exposure
impact_function.aggregation = aggregation
impact_function.map_canvas = CANVAS
# QSetting context
settings = QSettings()
crs = settings.value('inasafe/user_extent_crs', '', type=str)
impact_function.requested_extent_crs = QgsCoordinateReferenceSystem(crs)
try:
impact_function.requested_extent = QgsRectangle(
float(command_line_arguments.extent[0]),
float(command_line_arguments.extent[1]),
float(command_line_arguments.extent[2]),
float(command_line_arguments.extent[3])
)
except AttributeError:
print "No extents"
pass
return impact_function
示例12: test_vector_post_minimum_needs_value_generation
def test_vector_post_minimum_needs_value_generation(self):
"""Test minimum needs postprocessors on vector exposure.
Test with vector exposure data with population_count_field exists.
Minimum needs postprocessors is defined to only generate values when
exposure contains population data.
"""
hazard_layer = load_test_vector_layer(
'gisv4', 'hazard', 'tsunami_vector.geojson')
exposure_layer = load_test_vector_layer(
'gisv4', 'exposure', 'population.geojson')
aggregation_layer = load_test_vector_layer(
'gisv4', 'aggregation', 'small_grid.geojson')
impact_function = ImpactFunction()
impact_function.aggregation = aggregation_layer
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
return_code, message = impact_function.run()
self.assertEqual(return_code, ANALYSIS_SUCCESS, message)
# minimum needs fields should exists in the results
self._check_minimum_fields_exists(impact_function)
expected_value = {
u'population': 69,
u'total': 9.0,
u'minimum_needs__rice': 491,
u'minimum_needs__clean_water': 11763,
u'minimum_needs__toilets': 8,
u'minimum_needs__drinking_water': 3072,
u'minimum_needs__family_kits': 35,
u'male': 34,
u'female': 34,
u'youth': 17,
u'adult': 45,
u'elderly': 6,
u'total_affected': 6.0,
}
self._check_minimum_fields_value(expected_value, impact_function)
示例13: test_ratios_with_raster_exposure
def test_ratios_with_raster_exposure(self):
"""Test if we can add defaults to a raster exposure.
See ticket #3851 how to manage ratios with a raster exposure.
"""
hazard_layer = load_test_vector_layer(
'gisv4', 'hazard', 'tsunami_vector.geojson')
exposure_layer = load_test_raster_layer(
'gisv4', 'exposure', 'raster', 'population.asc')
# Set up impact function
impact_function = ImpactFunction()
impact_function.debug_mode = True
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
impact_function.prepare()
status, message = impact_function.run()
self.assertEqual(ANALYSIS_SUCCESS, status, message)
for layer in impact_function.outputs:
if layer.keywords['layer_purpose'] == (
layer_purpose_analysis_impacted['key']):
analysis = layer
if layer.keywords['layer_purpose'] == (
layer_purpose_aggregate_hazard_impacted['key']):
impact = layer
# We check in the impact layer if we have :
# female default ratio with the default value
index = impact.fieldNameIndex(female_ratio_field['field_name'])
self.assertNotEqual(-1, index)
unique_values = impact.uniqueValues(index)
self.assertEqual(1, len(unique_values))
female_ratio = unique_values[0]
# female displaced count and youth displaced count
self.assertNotEqual(
-1, impact.fieldNameIndex(
female_displaced_count_field['field_name']))
self.assertNotEqual(
-1, impact.fieldNameIndex(
youth_displaced_count_field['field_name']))
# Check that we have more than 0 female displaced in the analysis layer
index = analysis.fieldNameIndex(
female_displaced_count_field['field_name'])
female_displaced = analysis.uniqueValues(index)[0]
self.assertGreater(female_displaced, 0)
# Let's check computation
index = analysis.fieldNameIndex(
displaced_field['field_name'])
displaced_population = analysis.uniqueValues(index)[0]
self.assertEqual(
int(displaced_population * female_ratio), female_displaced)
# Check that we have more than 0 youth displaced in the analysis layer
index = analysis.fieldNameIndex(
female_displaced_count_field['field_name'])
value = analysis.uniqueValues(index)[0]
self.assertGreater(value, 0)
# Let do another test with the special aggregation layer
hazard_layer = load_test_vector_layer(
'gisv4', 'hazard', 'tsunami_vector.geojson')
exposure_layer = load_test_raster_layer(
'gisv4', 'exposure', 'raster', 'population.asc')
aggregation_layer = load_test_vector_layer(
'gisv4', 'aggregation', 'small_grid_ratios.geojson')
# This aggregation layer has :
# * a field for female ratio : 1, 0.5 and 0
# * use global default for youth ratio
# * do not ust for adult ratio
# * use custom 0.75 for elderly ratio
# Set up impact function
impact_function = ImpactFunction()
impact_function.debug_mode = True
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
impact_function.aggregation = aggregation_layer
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
status, message = impact_function.run()
self.assertEqual(ANALYSIS_SUCCESS, status, message)
impact = impact_function.impact
# We should have a female_ratio with many values
index = impact.fieldNameIndex(female_ratio_field['field_name'])
self.assertNotEqual(-1, index)
values = impact.uniqueValues(index)
self.assertEqual(3, len(values))
# We should have a youth_ratio with global default
index = impact.fieldNameIndex(youth_ratio_field['field_name'])
self.assertNotEqual(-1, index)
values = impact.uniqueValues(index)
self.assertEqual(1, len(values))
#.........这里部分代码省略.........
示例14: prepare_impact_function
def prepare_impact_function(self):
"""Create analysis as a representation of current situation of IFCW."""
# Impact Functions
impact_function = ImpactFunction()
impact_function.callback = self.progress_callback
# Layers
impact_function.hazard = self.parent.hazard_layer
impact_function.exposure = self.parent.exposure_layer
aggregation = self.parent.aggregation_layer
if aggregation:
impact_function.aggregation = aggregation
impact_function.use_selected_features_only = (
setting('useSelectedFeaturesOnly', False, bool))
else:
mode = setting('analysis_extents_mode')
if self.extent.user_extent:
# This like a hack to transform a geometry to a rectangle.
# self.extent.user_extent is a QgsGeometry.
# impact_function.requested_extent needs a QgsRectangle.
wkt = self.extent.user_extent.exportToWkt()
impact_function.requested_extent = wkt_to_rectangle(wkt)
impact_function.requested_extent_crs = self.extent.crs
elif mode == HAZARD_EXPOSURE_VIEW:
impact_function.requested_extent = (
self.iface.mapCanvas().extent())
impact_function.requested_extent_crs = self.extent.crs
elif mode == EXPOSURE:
impact_function.use_exposure_view_only = True
# We don't have any checkbox in the wizard for the debug mode.
impact_function.debug_mode = False
return impact_function
示例15: run_scenario
def run_scenario(scenario, use_debug=False):
"""Run scenario.
:param scenario: Dictionary of hazard, exposure, and aggregation.
:type scenario: dict
:param use_debug: If we should use debug_mode when we run the scenario.
:type use_debug: bool
:returns: Tuple(status, Flow dictionary, outputs).
:rtype: list
"""
if os.path.exists(scenario['exposure']):
exposure_path = scenario['exposure']
elif os.path.exists(standard_data_path('exposure', scenario['exposure'])):
exposure_path = standard_data_path('exposure', scenario['exposure'])
elif os.path.exists(
standard_data_path(*(scenario['exposure'].split('/')))):
exposure_path = standard_data_path(*(scenario['exposure'].split('/')))
else:
raise IOError('No exposure file')
if os.path.exists(scenario['hazard']):
hazard_path = scenario['hazard']
elif os.path.exists(standard_data_path('hazard', scenario['hazard'])):
hazard_path = standard_data_path('hazard', scenario['hazard'])
elif os.path.exists(standard_data_path(*(scenario['hazard'].split('/')))):
hazard_path = standard_data_path(*(scenario['hazard'].split('/')))
else:
raise IOError('No hazard file')
if not scenario['aggregation']:
aggregation_path = None
else:
if os.path.exists(scenario['aggregation']):
aggregation_path = scenario['aggregation']
elif os.path.exists(standard_data_path(
'aggregation', scenario['aggregation'])):
aggregation_path = standard_data_path(
'aggregation', scenario['aggregation'])
elif os.path.exists(
standard_data_path(*(scenario['aggregation'].split('/')))):
aggregation_path = standard_data_path(
*(scenario['aggregation'].split('/')))
else:
raise IOError('No aggregation file')
impact_function = ImpactFunction()
impact_function.debug_mode = use_debug
layer = QgsVectorLayer(hazard_path, 'Hazard', 'ogr')
if not layer.isValid():
layer = QgsRasterLayer(hazard_path, 'Hazard')
impact_function.hazard = layer
layer = QgsVectorLayer(exposure_path, 'Exposure', 'ogr')
if not layer.isValid():
layer = QgsRasterLayer(exposure_path, 'Exposure')
impact_function.exposure = layer
if aggregation_path:
impact_function.aggregation = QgsVectorLayer(
aggregation_path, 'Aggregation', 'ogr')
status, message = impact_function.prepare()
if status != 0:
return status, message, None
status, message = impact_function.run()
if status != 0:
return status, message, None
for layer in impact_function.outputs:
if layer.type() == QgsMapLayer.VectorLayer:
check_inasafe_fields(layer)
return status, impact_function.state, impact_function.outputs