serJD commited on
Commit
91689b5
1 Parent(s): 94ba0ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -45
app.py CHANGED
@@ -5,62 +5,31 @@ import json
5
  from io import StringIO
6
 
7
 
8
- def adjust_population_by_distance(df_distances, df_population, distance_threshold, decay_factor):
9
- """
10
- Adjusts the population of each origin based on the distance to any destination, applying a decay effect for distances beyond the threshold.
11
-
12
- Parameters:
13
- - df_distances (pd.DataFrame): DataFrame with distances from origins to destinations.
14
- - df_population (pd.Series): Series with population for each origin.
15
- - distance_threshold (float): Distance beyond which the decay effect is applied.
16
- - decay_factor (float): Factor controlling the rate of decay in willingness to travel beyond the threshold.
17
-
18
- Returns:
19
- - pd.Series: Adjusted population for each origin.
20
- """
21
- # Calculate the minimum distance from each origin to any destination
22
- min_distance = df_distances.min(axis=1)
23
-
24
- # Adjust the population based on the minimum distance and the decay factor
25
- def adjustment_factor(distance):
26
- if distance > distance_threshold:
27
- return np.exp(-(distance - distance_threshold) * decay_factor)
28
- else:
29
- return 1
30
-
31
- adjustment_factors = min_distance.apply(adjustment_factor)
32
- return df_population * adjustment_factors
33
-
34
  def huff_model_probability(df_distances, df_attractiveness, alpha, beta, df_population=None, distance_threshold=None, decay_factor=0.1):
35
  """
36
- Calculates the probability of choosing among destinations based on an enhanced Huff model that considers a willingness to travel threshold and applies a decay effect for distances beyond this threshold.
37
-
38
- Parameters:
39
- - df_distances (pd.DataFrame): DataFrame where rows are origins, columns are destinations, and values are distances.
40
- - df_attractiveness (pd.Series): Series with attractiveness weights for each destination.
41
- - alpha (float): Attractiveness parameter of the Huff model.
42
- - beta (float): Distance decay parameter of the Huff model.
43
- - df_population (pd.Series, optional): Series with population for each origin. Defaults to 1 if not provided.
44
- - distance_threshold (float, optional): Distance beyond which the decay effect on willingness to travel is applied.
45
- - decay_factor (float, optional): Factor controlling the rate of decay in willingness to travel beyond the threshold.
46
-
47
- Returns:
48
- - pd.DataFrame: DataFrame with probabilities of choosing each destination from each origin.
49
  """
50
  if df_population is None:
51
  df_population = pd.Series(np.ones(df_distances.shape[0]), index=df_distances.index)
52
 
 
53
  if distance_threshold is not None:
54
- df_population = adjust_population_by_distance(df_distances, df_population, distance_threshold, decay_factor)
55
-
 
 
 
 
 
 
56
  attractiveness_term = df_attractiveness ** alpha
57
  distance_term = df_distances ** -beta
58
 
59
- numerator = (attractiveness_term * distance_term).multiply(df_population, axis=0)
60
  denominator = numerator.sum(axis=1)
61
  probabilities = numerator.div(denominator, axis=0)
62
 
63
- return probabilities
64
 
65
  def app_function(input_json):
66
  print("Received input")
@@ -94,7 +63,7 @@ def app_function(input_json):
94
  decay_factor = inputs.get("decay_factor", 0.1) # Default decay factor if not provided
95
 
96
  # Call the updated Huff model function
97
- probabilities = huff_model_probability(
98
  df_distances=df_distances,
99
  df_attractiveness=df_attractiveness,
100
  alpha=alpha,
@@ -104,7 +73,13 @@ def app_function(input_json):
104
  decay_factor=decay_factor
105
  )
106
 
107
- return probabilities.to_json(orient='split')
 
 
 
 
 
 
108
 
109
  # Define the Gradio interface with a single JSON input
110
  iface = gr.Interface(
 
5
  from io import StringIO
6
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def huff_model_probability(df_distances, df_attractiveness, alpha, beta, df_population=None, distance_threshold=None, decay_factor=0.1):
9
  """
10
+ Calculates the probability of choosing among destinations and the adjustment factors for willingness to travel.
 
 
 
 
 
 
 
 
 
 
 
 
11
  """
12
  if df_population is None:
13
  df_population = pd.Series(np.ones(df_distances.shape[0]), index=df_distances.index)
14
 
15
+ adjustment_factors = pd.DataFrame(index=df_distances.index, columns=df_distances.columns)
16
  if distance_threshold is not None:
17
+ # Calculate adjustment factors for each origin-destination pair
18
+ for destination in df_distances.columns:
19
+ adjustment_factors[destination] = df_distances[destination].apply(
20
+ lambda x: np.exp(-(max(0, x - distance_threshold)) * decay_factor))
21
+ else:
22
+ adjustment_factors[:] = 1
23
+
24
+ adjusted_population = df_population.repeat(df_distances.shape[1]).values.reshape(df_distances.shape) * adjustment_factors
25
  attractiveness_term = df_attractiveness ** alpha
26
  distance_term = df_distances ** -beta
27
 
28
+ numerator = (attractiveness_term * distance_term).multiply(adjusted_population, axis=0)
29
  denominator = numerator.sum(axis=1)
30
  probabilities = numerator.div(denominator, axis=0)
31
 
32
+ return probabilities, adjustment_factors
33
 
34
  def app_function(input_json):
35
  print("Received input")
 
63
  decay_factor = inputs.get("decay_factor", 0.1) # Default decay factor if not provided
64
 
65
  # Call the updated Huff model function
66
+ probabilities, adjustment_factors = huff_model_probability(
67
  df_distances=df_distances,
68
  df_attractiveness=df_attractiveness,
69
  alpha=alpha,
 
73
  decay_factor=decay_factor
74
  )
75
 
76
+ # Prepare the output
77
+ output = {
78
+ "probabilities": probabilities.to_json(orient='split'),
79
+ "adjustment_factors": adjustment_factors.to_json(orient='split')
80
+ }
81
+
82
+ return output.to_json(orient='split')
83
 
84
  # Define the Gradio interface with a single JSON input
85
  iface = gr.Interface(