@@ -765,7 +765,33 @@ def get_inflow_to_outflow_ratio_by_item_and_facilitylevel(_df):
765765mwanza_1b = sf .loc [(sf .district_std == 'Mwanza' ) & (sf .fac_type_tlo == '1a' )].copy ().assign (fac_type_tlo = '1b' )
766766sf = pd .concat ([sf , mwanza_1b ], axis = 0 , ignore_index = True )
767767
768- # 4) Copy all the results to create a level 0 with an availability equal to half that in the respective 1a
768+ # 4) Update the availability Xpert (item_code = 187)
769+ # First add rows for Xpert at level 1b by cloning rows for level 2 -> only if not already present
770+ xpert_item = sf ['item_code' ].eq (187 )
771+ level_2 = sf ['fac_type_tlo' ].eq ('2' )
772+ level_1b = sf ['fac_type_tlo' ].eq ('1b' )
773+
774+ # Clone rows from level 2
775+ base = sf .loc [level_2 & xpert_item ].copy ()
776+ new_rows = base .copy ()
777+ new_rows ['fac_type_tlo' ] = '1b'
778+
779+ # Add rows to main availability dataframe and drop duplicates, if any
780+ sf = pd .concat ([sf , new_rows ], ignore_index = True )
781+ id_cols = [c for c in sf .columns if c != 'available_prop' ]
782+ dupe_mask = sf .duplicated (subset = id_cols , keep = False )
783+ dupes = sf .loc [dupe_mask ].sort_values (id_cols )
784+ sf = sf .drop_duplicates (subset = id_cols , keep = 'first' ).reset_index (drop = True )
785+
786+ # Compute the average availability Sep–Dec (months >= 9) for level 2, item 187
787+ sep_to_dec = sf ['month' ].ge (9 )
788+ new_xpert_availability = sf .loc [level_2 & xpert_item & sep_to_dec , 'available_prop' ].mean ()
789+ # Assign new availability to relevant facility levels
790+ levels_1b_2_or_3 = sf ['fac_type_tlo' ].isin (['1b' , '2' , '3' ])
791+ xpert_item = sf ['item_code' ].eq (187 )
792+ sf .loc [levels_1b_2_or_3 & xpert_item , 'available_prop' ] = new_xpert_availability
793+
794+ # 5) Copy all the results to create a level 0 with an availability equal to half that in the respective 1a
769795all_1a = sf .loc [sf .fac_type_tlo == '1a' ]
770796all_0 = sf .loc [sf .fac_type_tlo == '1a' ].copy ().assign (fac_type_tlo = '0' )
771797all_0 .available_prop *= 0.5
0 commit comments