In the section calculating LIS, I believe the line
valid_pae = selected_pae[selected_pae <= 12] # Apply the threshold
should be
valid_pae = selected_pae[selected_pae < 12] # Apply the threshold
because only nonzero transformed PAE values are used to calculate LIS.