@@ -387,6 +387,7 @@ def get_all_label_attributes(labels: pd.Series, activity_type: str, country_code
387387
388388def get_instances_from_dataframe (
389389 context : AssetExecutionContext ,
390+ config : BSSMetadataConfig ,
390391 df : pd .DataFrame ,
391392 livelihood_zone_baseline : LivelihoodZoneBaseline ,
392393 activity_type : str ,
@@ -727,8 +728,9 @@ def get_instances_from_dataframe(
727728 for i , livelihood_activity in enumerate (livelihood_activities_for_strategy ):
728729 livelihood_activity ["livelihood_strategy" ] = livelihood_zone_baseline_key + [
729730 livelihood_strategy ["strategy_type" ],
730- livelihood_strategy ["season" ] if livelihood_strategy ["season" ] else "" ,
731- livelihood_strategy ["product_id" ] if livelihood_strategy ["product_id" ] else "" ,
731+ livelihood_strategy ["season" ] or "" , # Natural key components must be "" rather than None
732+ livelihood_strategy ["product_id" ]
733+ or "" , # Natural key components must be "" rather than None
732734 livelihood_strategy ["additional_identifier" ],
733735 ]
734736
@@ -1149,13 +1151,6 @@ def get_instances_from_dataframe(
11491151 % (partition_key , worksheet_name , row , label )
11501152 ) from e
11511153
1152- raise_errors = True
1153- if errors and raise_errors :
1154- errors = "\n " .join (errors )
1155- raise RuntimeError (
1156- "Missing or inconsistent metadata in BSS %s worksheet '%s':\n %s" % (partition_key , worksheet_name , errors )
1157- )
1158-
11591154 result = {
11601155 "LivelihoodStrategy" : livelihood_strategies ,
11611156 "LivelihoodActivity" : livelihood_activities ,
@@ -1177,6 +1172,19 @@ def get_instances_from_dataframe(
11771172 if not unrecognized_labels .empty :
11781173 metadata ["unrecognized_labels" ] = MetadataValue .md (unrecognized_labels .to_markdown (index = False ))
11791174
1175+ if errors :
1176+ if config .strict :
1177+ raise RuntimeError (
1178+ "Missing or inconsistent metadata in BSS %s worksheet '%s':\n %s"
1179+ % (partition_key , worksheet_name , "\n " .join (errors ))
1180+ )
1181+ else :
1182+ context .log .error (
1183+ "Missing or inconsistent metadata in BSS %s worksheet '%s':\n %s"
1184+ % (partition_key , worksheet_name , "\n " .join (errors ))
1185+ )
1186+ metadata ["errors" ] = MetadataValue .md (f'```text\n { "\n " .join (errors )} \n ```' )
1187+
11801188 return Output (
11811189 result ,
11821190 metadata = metadata ,
@@ -1185,6 +1193,7 @@ def get_instances_from_dataframe(
11851193
11861194def get_annotated_instances_from_dataframe (
11871195 context : AssetExecutionContext ,
1196+ config : BSSMetadataConfig ,
11881197 livelihood_activity_dataframe : pd .DataFrame ,
11891198 livelihood_summary_dataframe : pd .DataFrame ,
11901199 activity_type : str ,
@@ -1203,6 +1212,7 @@ def get_annotated_instances_from_dataframe(
12031212 # Get the detail LivelihoodStrategy and LivelihoodActivity instances
12041213 output = get_instances_from_dataframe (
12051214 context ,
1215+ config ,
12061216 livelihood_activity_dataframe ,
12071217 livelihood_zone_baseline ,
12081218 activity_type ,
@@ -1214,6 +1224,7 @@ def get_annotated_instances_from_dataframe(
12141224 # Get the summary instances
12151225 reported_summary_output = get_instances_from_dataframe (
12161226 context ,
1227+ config ,
12171228 livelihood_summary_dataframe ,
12181229 livelihood_zone_baseline ,
12191230 ActivityLabel .LivelihoodActivityType .LIVELIHOOD_SUMMARY ,
@@ -1325,7 +1336,9 @@ def get_annotated_instances_from_dataframe(
13251336 summary_df .replace (pd .NA , None ).to_markdown (floatfmt = ",.0f" )
13261337 )
13271338
1328- # Move the preview and metadata item to the end of the dict
1339+ # Move the preview and errors metadata item to the end of the dict
1340+ if "errors" in output .metadata :
1341+ output .metadata ["errors" ] = output .metadata .pop ("errors" )
13291342 output .metadata ["preview" ] = output .metadata .pop ("preview" )
13301343
13311344 return output
@@ -1334,26 +1347,27 @@ def get_annotated_instances_from_dataframe(
13341347@asset (partitions_def = bss_instances_partitions_def , io_manager_key = "json_io_manager" )
13351348def livelihood_activity_instances (
13361349 context : AssetExecutionContext ,
1350+ config : BSSMetadataConfig ,
13371351 livelihood_activity_dataframe : pd .DataFrame ,
13381352 livelihood_summary_dataframe : pd .DataFrame ,
13391353) -> Output [dict ]:
13401354 """
13411355 LivelhoodStrategy and LivelihoodActivity instances extracted from the BSS.
13421356 """
1343- output = get_annotated_instances_from_dataframe (
1357+ return get_annotated_instances_from_dataframe (
13441358 context ,
1359+ config ,
13451360 livelihood_activity_dataframe ,
13461361 livelihood_summary_dataframe ,
1347- ActivityLabel .LivelihoodActivityType .LIVELIHOOD_SUMMARY ,
1362+ ActivityLabel .LivelihoodActivityType .LIVELIHOOD_ACTIVITY ,
13481363 len (HEADER_ROWS ),
13491364 )
13501365
1351- return output
1352-
13531366
13541367@asset (partitions_def = bss_instances_partitions_def , io_manager_key = "json_io_manager" )
13551368def livelihood_activity_valid_instances (
13561369 context : AssetExecutionContext ,
1370+ config : BSSMetadataConfig ,
13571371 livelihood_activity_instances : dict ,
13581372 wealth_characteristic_instances : dict ,
13591373) -> Output [dict ]:
@@ -1369,16 +1383,7 @@ def livelihood_activity_valid_instances(
13691383 ** {"WealthGroup" : wealth_characteristic_instances ["WealthGroup" ]},
13701384 ** livelihood_activity_instances ,
13711385 }
1372- valid_instances , metadata = validate_instances (context , livelihood_activity_instances , partition_key )
1373- metadata = {f"num_{ key .lower ()} " : len (value ) for key , value in valid_instances .items ()}
1374- metadata ["total_instances" ] = sum (len (value ) for value in valid_instances .values ())
1375- metadata ["preview" ] = MetadataValue .md (
1376- f"```json\n { json .dumps (valid_instances , indent = 4 , ensure_ascii = False )} \n ```"
1377- )
1378- return Output (
1379- valid_instances ,
1380- metadata = metadata ,
1381- )
1386+ return validate_instances (context , config , livelihood_activity_instances , partition_key )
13821387
13831388
13841389@asset (partitions_def = bss_instances_partitions_def , io_manager_key = "json_io_manager" )
@@ -1390,11 +1395,7 @@ def livelihood_activity_fixture(
13901395 """
13911396 Django fixture for the Livelihood Activities from a BSS.
13921397 """
1393- fixture , metadata = get_fixture_from_instances (livelihood_activity_valid_instances )
1394- return Output (
1395- fixture ,
1396- metadata = metadata ,
1397- )
1398+ return get_fixture_from_instances (livelihood_activity_valid_instances )
13981399
13991400
14001401@asset (partitions_def = bss_instances_partitions_def )
@@ -1405,8 +1406,4 @@ def imported_livelihood_activities(
14051406 """
14061407 Imported Django fixtures for a BSS, added to the Django database.
14071408 """
1408- metadata = import_fixture (livelihood_activity_fixture )
1409- return Output (
1410- None ,
1411- metadata = metadata ,
1412- )
1409+ return import_fixture (livelihood_activity_fixture )
0 commit comments