Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,34 +5,6 @@ import json
|
|
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 and 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 |
-
# deactivate adjustment factor !!!!!!!!!!!!!!!!!!!!
|
24 |
-
adjustment_factors[:] = 1
|
25 |
-
|
26 |
-
adjusted_population = df_population.repeat(df_distances.shape[1]).values.reshape(df_distances.shape) * adjustment_factors
|
27 |
-
attractiveness_term = df_attractiveness ** alpha
|
28 |
-
distance_term =np.exp(df_distances * -beta)
|
29 |
-
|
30 |
-
numerator = (attractiveness_term * distance_term).multiply(adjusted_population, axis=0)
|
31 |
-
denominator = numerator.sum(axis=1)
|
32 |
-
probabilities = numerator.div(denominator, axis=0).fillna(0)
|
33 |
-
|
34 |
-
return probabilities, adjustment_factors
|
35 |
-
|
36 |
def app_function(input_json):
|
37 |
print("Received input")
|
38 |
# Parse the input JSON string
|
@@ -45,40 +17,13 @@ def app_function(input_json):
|
|
45 |
# Assuming the input structure is correctly formatted to include the necessary parameters
|
46 |
inputs = inputs["input"]
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
print("df_distances shape:", df_distances.shape)
|
51 |
-
|
52 |
-
# Convert 'df_attractiveness' into a Series
|
53 |
-
df_attractiveness = pd.Series(inputs["df_attractiveness"])
|
54 |
-
print("df_attractiveness shape:", df_attractiveness.shape)
|
55 |
-
|
56 |
-
# Extract alpha and beta parameters
|
57 |
-
alpha = inputs["alpha"]
|
58 |
-
beta = inputs["beta"]
|
59 |
-
|
60 |
-
# Check if 'df_population' is provided and convert to Series, else default to None
|
61 |
-
df_population = pd.Series(inputs["df_population"]) if "df_population" in inputs else None
|
62 |
-
|
63 |
-
# Additional parameters for the updated Huff model
|
64 |
-
distance_threshold = inputs.get("distance_threshold", None)
|
65 |
-
decay_factor = inputs.get("decay_factor", 0.1) # Default decay factor if not provided
|
66 |
|
67 |
-
# Call the updated Huff model function
|
68 |
-
probabilities, adjustment_factors = huff_model_probability(
|
69 |
-
df_distances=df_distances,
|
70 |
-
df_attractiveness=df_attractiveness,
|
71 |
-
alpha=alpha,
|
72 |
-
beta=beta,
|
73 |
-
df_population=df_population,
|
74 |
-
distance_threshold=distance_threshold,
|
75 |
-
decay_factor=decay_factor
|
76 |
-
)
|
77 |
|
78 |
# Prepare the output
|
79 |
output = {
|
80 |
-
"
|
81 |
-
"adjustment_factors": adjustment_factors.to_dict(orient='split')
|
82 |
}
|
83 |
|
84 |
return json.dumps(output)
|
|
|
5 |
from io import StringIO
|
6 |
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def app_function(input_json):
|
9 |
print("Received input")
|
10 |
# Parse the input JSON string
|
|
|
17 |
# Assuming the input structure is correctly formatted to include the necessary parameters
|
18 |
inputs = inputs["input"]
|
19 |
|
20 |
+
print(inputs["somedata"])
|
21 |
+
calcRes = float(inputs["somedata"])*float(inputs["somedata"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Prepare the output
|
25 |
output = {
|
26 |
+
"calcRes": calcRes
|
|
|
27 |
}
|
28 |
|
29 |
return json.dumps(output)
|