File size: 3,668 Bytes
fa7b8b5
 
 
 
 
 
 
94ba0ac
 
91689b5
94ba0ac
fa7b8b5
 
 
91689b5
56dfff9
91689b5
 
 
 
 
 
de71d06
 
91689b5
 
94ba0ac
651ee82
94ba0ac
91689b5
94ba0ac
cf9dc09
94ba0ac
91689b5
fa7b8b5
 
94ba0ac
fa7b8b5
b6f5dd4
 
94ba0ac
b6f5dd4
94ba0ac
 
 
e46c84a
 
94ba0ac
08aba45
94ba0ac
 
 
714bb14
94ba0ac
 
 
fa7b8b5
 
714bb14
94ba0ac
714bb14
94ba0ac
 
 
 
714bb14
94ba0ac
91689b5
94ba0ac
 
 
 
 
 
 
 
 
91689b5
 
c992ce0
 
91689b5
 
8451ac2
fa7b8b5
 
 
 
b1ead6c
 
fa7b8b5
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import pandas as pd
import numpy as np
import json
from io import StringIO


def huff_model_probability(df_distances, df_attractiveness, alpha, beta, df_population=None, distance_threshold=None, decay_factor=0.1):
    """
    Calculates the probability of choosing among destinations and the adjustment factors for willingness to travel.
    """
    if df_population is None:
        df_population = pd.Series(np.ones(df_distances.shape[0]), index=df_distances.index)

    adjustment_factors = pd.DataFrame(index=df_distances.index, columns=df_distances.columns)
    if distance_threshold is not None and distance_threshold is not "None":
        # Calculate adjustment factors for each origin-destination pair
        for destination in df_distances.columns:
            adjustment_factors[destination] = df_distances[destination].apply(
                lambda x: np.exp(-(max(0, x - distance_threshold)) * decay_factor))
    else:
        adjustment_factors[:] = 1
    # deactivate adjustment factor !!!!!!!!!!!!!!!!!!!!
    adjustment_factors[:] = 1

    adjusted_population = df_population.repeat(df_distances.shape[1]).values.reshape(df_distances.shape) * adjustment_factors
    attractiveness_term = df_attractiveness ** alpha
    distance_term =np.exp(df_distances * -beta)

    numerator = (attractiveness_term * distance_term).multiply(adjusted_population, axis=0)
    denominator = numerator.sum(axis=1)
    probabilities = numerator.div(denominator, axis=0).fillna(0)

    return probabilities, adjustment_factors

def app_function(input_json):
    print("Received input")
    # Parse the input JSON string
    try:
        inputs = json.loads(input_json)
    except json.JSONDecodeError:
        inputs = json.loads(input_json.replace("'", '"'))
    print("Parsed input keys:", inputs.keys())

    # Assuming the input structure is correctly formatted to include the necessary parameters
    inputs = inputs["input"]

    # Convert 'df_distances' from a list of lists into a DataFrame
    df_distances = pd.DataFrame(inputs["df_distances"])
    print("df_distances shape:", df_distances.shape)
    
    # Convert 'df_attractiveness' into a Series
    df_attractiveness = pd.Series(inputs["df_attractiveness"])
    print("df_attractiveness shape:", df_attractiveness.shape)
    
    # Extract alpha and beta parameters
    alpha = inputs["alpha"]
    beta = inputs["beta"]
    
    # Check if 'df_population' is provided and convert to Series, else default to None
    df_population = pd.Series(inputs["df_population"]) if "df_population" in inputs else None

    # Additional parameters for the updated Huff model
    distance_threshold = inputs.get("distance_threshold", None)
    decay_factor = inputs.get("decay_factor", 0.1)  # Default decay factor if not provided
    
    # Call the updated Huff model function
    probabilities, adjustment_factors = huff_model_probability(
        df_distances=df_distances,
        df_attractiveness=df_attractiveness,
        alpha=alpha,
        beta=beta,
        df_population=df_population,
        distance_threshold=distance_threshold,
        decay_factor=decay_factor
    )

    # Prepare the output
    output = {
        "probabilities": probabilities.to_dict(orient='split'),
        "adjustment_factors": adjustment_factors.to_dict(orient='split')
    }

    return json.dumps(output)

# Define the Gradio interface with a single JSON input
iface = gr.Interface(
    fn=app_function,
    inputs=gr.Textbox(label="Input JSON", lines=20, placeholder="Enter JSON with all parameters here..."),
    outputs=gr.JSON(label="Output JSON"),
    title="Dynamic Huff Model"
)

iface.launch()