Project: s41467-025-61454-1 | Function: transform_distances (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Transforms original distances using exponential weighting with the kappa parameter to create the linearized proxy for the EDE optimization problem.

**KEY TERMS:** {
  "transformed": "e^(-κ d_{r,s}) exponential transformation",
  "kappa": "κ parameter",
  "distances_transformed_dict": "transformed distance matrix"
}

**CONTEXT CODE:**     
    #make sure we only have the populations of "good" origins
    origins_df = origins_df[origins_df["id_orig"].isin(origins_list)]
    
    #all currently open destinations (including bad ones)
    open_current_list = destinations_df.loc[destinations_df['dest_type']== 'supermarket', 'id_dest'].unique().tolist()
    
    #definining dictionaries    
    distances_dict = dict(zip(zip(distances_df['id_orig'], distances_df['id_dest']), distances_df['network']))
    origins_dict = dict(zip(origins_df['id_orig'],origins_df['U7B001']))
    
    total_pop=sum(origins_df['U7B001'])
    
    if num_to_open:
         open_total = num_to_open + len(open_current_list)
    else: open_total= 'n/a'
    
    return total_pop, open_current_list, origins_list, destinations_list, origins_df, destinations_df, distances_df, origins_dict, distances_dict, open_total
    


def compute_kappa(open_current_list,origins_df,distances_df,aversion_parameter):
    import os
    if os.getenv('TEST_GOLDEN', 'true') == 'false':
        raise NotImplementedError('This function is masked for testing purposes.')
    '''
    Use inequalipy to compute kappa value of given parameters

    Parameters:
    open_current_list (list): list of currently open facilities
    distances_df (pandas.Dataframe): contains information about the network distance traveled from all origins to all destinations
    aversion_parameter (float): user defined aversion to inequality parameter. Larger in magnitude signifies larger penalty of inequality.

    Returns:
    kappa (float): the appropriate value of kappa given the open facilities and populations of origins.

    '''

    logger.info('computing kappa')

    #get distances for all origins to all destinations that are already open
    distances_open_df = distances_df[distances_df["id_dest"].isin(open_current_list)]
    distances_open_df = distances_open_df.loc[distances_open_df.groupby('id_orig')['network'].idxmin()]
    distances_open_df = pd.merge(distances_open_df, origins_df, on='id_orig', how='left')
    # calculate kappa using inequalipy package
    kappa = ineq.kolmpollak.calc_kappa(distances_open_df['network'].values, epsilon = aversion_parameter, weights=distances_open_df['U7B001'].values)

    return kappa
    
    
def transform_distances(distances_df,kappa):
    import os
    if os.getenv('TEST_GOLDEN', 'true') == 'false':
        raise NotImplementedError('This function is masked for testing purposes.')
    '''
    Transform distances before optimization to lessen computational burden

    Parameters:
    distances_df (pandas.DataFrame): Contains distance information
    kappa (float): kappa value to be used in the optimization model

    Returns: distances_df with additional transformed distance column and distances_transformed_dict

    '''

    logger.info('transforming distances')

    #transforming distances
    distances_df['transformed'] = distances_df.network.apply(lambda d:np.exp(-kappa*d))
    #to get all the right pairs of origins and destinations in the dict
    distances_transformed_dict = dict(zip(zip(distances_df["id_orig"], distances_df["id_dest"]), distances_df["transformed"]))

    return distances_transformed_dict
    
    
def create_pyomo_model(kappa,origins_list,destinations_list,origins_dict,distances_dict,distances_transformed_dict,open_current_list,open_total,total_pop,measure,kp_threshold=None):
    '''
    Defines pyomo model, adds appropriate variables and constraints.
     
    Parameters: 
    kappa (float) : kappa value to be used in the optimization model
    origins_list (list) : list of origins (r)
    destinations_list (list) : list of destinations (s)
    origins_dict (dict) : dictionary of origins and populations for each origin
    distances_dict (dict) : contains information about the network distance traveled from all origins to all destinations
    distances_transformed_dict (dict) : transformed network distance traveled from all origins to all destinations
    open_current_list (list) : list of currently open facilities 
    open_total (int) : number of currently open facilities + number of desired facilities to add
    total_pop (int) : total population for all residential areas
    measure (str): Defines which measure to use in model, PCENT, PMED, KPL, or KPCON.
    kp_threshold (float,default=None) : Upper bound for Kolm-Pollak value to be used in KPCON model.

    
    Returns:
    model
    '''
    
    logger.info('building model in pyomo')
    
    
    #DEFINE DIFFERENT MODEL OBJECTIVE FUNCTIONS

    #KPL Objective: Minimize the population weighted kolm-pollak distance for residential areas (r) and services (s)
    def kpl_rule(model):
        import os
        if os.getenv('TEST_GOLDEN', 'true') == 'false':
            raise NotImplementedError('This function is masked for testing purposes.')
        return sum(origins_dict[r]*distances_transformed_dict[r,s]*model.y[r,s] for r, s in distances_transformed_dict)       
        
        
    #PMED Objective: Minimize the population weighted average distance for residential areas (r) and services (s)
    def pmed_rule(model):
        return sum(origins_dict*distances_dict*model.y[r,s] for r, s in distances_dict)
            

    #PCENT Objective: Minimize the maximum distance any resident area has to travel
    def pcent_rule(model):
        return model.u
            
    def minimax_rule(model,r,s):
        return model.y[r,s]*distances_dict <= model.u
        
    #KPCON Objective: Minimize total number of facilities to open
    def kpcon_obj_rule(model):
        return sum(model.x[s] for s in destinations_list)
          

    #DEFINE MODEL CONSTRAINTS

    #residential area r is only assigned to service s if service s is open.
    def open_rule(model,r,s):
        return model.y[r,s]-model.x[s]<=0


    #each origin is only assigned to a single destination
    def single_rule(model,r):
        return sum(model.y[r,s] for s in destinations_list if (r,s) in distances_transformed_dict) == 1


    #sum of open destinations  should equal the number we want to open
    def open_total_rule(model):
        return sum(model.x[s] for s in destinations_list)==open_total


    #forces open service locations that are already there
    def currently_open_rule(model,s):
        return model.x[s]==1
        
    #bounding the population weighted kolm-pollak distance for residential areas (r) and services (s) from above by the kp_threshold
    def kpcon_rule(model):


Complete the function implementation replacing "raise NotImplementedError".