File size: 2,351 Bytes
1997c01
 
 
 
 
 
 
 
 
 
 
 
 
6866b1f
 
d8e3d53
cded988
 
d8e3d53
 
81225f7
d8e3d53
0b6419d
d8e3d53
0b6419d
d8e3d53
 
c024d74
 
e517d5e
 
 
0b6419d
d8e3d53
 
 
 
 
 
0b6419d
b65f10f
d8e3d53
c024d74
d8e3d53
c024d74
0b6419d
c024d74
b65f10f
 
 
 
1997c01
 
 
d8e3d53
c024d74
1997c01
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import numpy as np
import json
from io import StringIO

def test(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("'", '"'))

    # Accessing the 'a_list' string and converting it to a list of integers
    ids_index = inputs['input']['ids_list']

    # Extract the datatree part which is a list of dictionaries
    matrix = inputs['input']["matrix"]
    weights = inputs['input']["weights"]

    alpha = inputs['input']["alpha"]
    alpha = float(alpha)
    threshold = inputs['input']["threshold"]
    threshold = float(threshold)
    

    df_matrix = pd.DataFrame(matrix).T
    df_weights = pd.DataFrame(weights).T
    df_matrix = df_matrix.round(0).astype(int)
    df_weights = df_weights.round(0).astype(int)
    
    def computeAccessibility (DistanceMatrix,destinationWeights, alpha = 0.0038, threshold = 600):
    
      decay_factors = np.exp(-alpha * DistanceMatrix) * (DistanceMatrix <= threshold)
      subdomainsAccessibility = pd.DataFrame(index=DistanceMatrix.index, columns=destinationWeights.columns)
      for col in destinationWeights.columns:
        subdomainsAccessibility[col] = (decay_factors * destinationWeights[col].values).sum(axis=1)
    
      #subdomainsAccessibility.drop(columns='commercial', inplace=True)
          
      return subdomainsAccessibility
    

    subdomainsAccessibility = computeAccessibility(df_matrix,df_weights,alpha,threshold)

    subdomainsAccessibility_dict = subdomainsAccessibility.to_dict('index')
    
    for key, subdict in data['subdomainsAccessibility_dict'].items():
        ordered = OrderedDict(sorted(subdict.items(), key=lambda x: int(x[0])))
        subdomainsAccessibility_dict['subdomainsAccessibility_dict'][key] = ordered

    # Prepare the output
    output = {
        "list": ids_index,
        "subdomainsAccessibility_dict": subdomainsAccessibility_dict
    }

    return json.dumps(output)

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

iface.launch()