From 5ddc0cdf683200f8dfd9a3de2b179a36e9638f34 Mon Sep 17 00:00:00 2001
From: "Campos Cordobes, Sergio" <sergio.campos@tecnalia.com>
Date: Mon, 27 Mar 2023 07:54:05 +0200
Subject: [PATCH] Update modal_choice.py

---
 src/modal_choice.py | 134 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 131 insertions(+), 3 deletions(-)

diff --git a/src/modal_choice.py b/src/modal_choice.py
index f0fc6e3..30d0c71 100644
--- a/src/modal_choice.py
+++ b/src/modal_choice.py
@@ -2,16 +2,53 @@
 # -*- coding: utf-8 -*-
 """
 Created on: 16/01/2023
-@author: Andoni Aranguren Ubierna
-@updates: Sergio Campos 02-03/2023
+Sergio Campos 02-03/2023
 """
 import math
 
+
+import pysmile
+import json
+from src import constants, pysmile_license
+
 import sys
 print(sys.path)
 from src import constants, utils
 
 
+net = pysmile.Network()
+net.read_file("URBANITE_ModalChoice.xdsl")
+
+def modal_choice_getconfigurationfile(cnx, request):
+    """
+    We ask for actions based on popularity of the actions
+    :return: json
+    """
+    try:
+        city = request.args.get("city", default=None, type=str)
+        if (city=="BIO"):
+            fichero = open('URBANITE_ModalChoice.xdsl')
+            ret_xml = fichero.read()
+
+        if (city=="AMS"):
+            fichero = open('URBANITE_ModalChoice.xdsl')
+            ret_xml = fichero.read()
+
+        if (city=="HEL"):
+            fichero = open('URBANITE_ModalChoice.xdsl')
+            ret_xml = fichero.read()
+
+        if (city=="MES"):
+            fichero = open('URBANITE_ModalChoice.xdsl')
+            ret_xml = fichero.read()
+
+        return ret_xml
+    except:
+        return constants.ERROR_EXCEPTION
+
+
+
+
 def modal_choice_estimation(cnx, request):
     """
     We ask for actions based on popularity of the actions
@@ -20,10 +57,73 @@ def modal_choice_estimation(cnx, request):
     try:
         kpi = request.args.get("kpi", default=None, type=int)
         ret_json = {"modal_choice": [kpi]}
+        net.set_evidence("Gender", "male")
+        ret_json = "{ "+"\"mode_choice"+"\": ["
+        net.update_beliefs()
+        beliefs = net.get_node_value("Mode_choice")
+        
+        for i in range(0, len(beliefs)):
+           print(net.get_outcome_id("Mode_choice", i) + "=" + str(beliefs[i]))
+           ret_json = ret_json + "{\"" + net.get_outcome_id("Mode_choice", i) + "\":\""+ str(beliefs[i])+"\"}"
+           if (i<len(beliefs)-1): 
+               ret_json = ret_json + ","
+        ret_json = ret_json + "]}"
+        
         return ret_json
     except:
         return constants.ERROR_EXCEPTION
 
+def print_cpt_matrix(node_handle):
+    print("AQUI")
+    cpt = net.get_node_definition(node_handle)
+    print("AQUI2")
+    parents = net.get_parents_ids(node_handle)
+    print("cpt:" + cpt)
+    print("parents:" + parents)
+    """
+    dim_count = 1 + len(parents)
+    dim_sizes = [0] * dim_count
+    for i in range(0, dim_count - 1):
+        dim_sizes[i] = net.get_outcome_count(parents[i])
+        dim_sizes[len(dim_sizes) - 1] = net.get_outcome_count(node_handle)
+        coords = [0] * dim_count
+    for elem_idx in range(0, len(cpt)):
+        index_to_coords(elem_idx, dim_sizes, coords)
+        outcome = net.get_outcome_id(node_handle, coords[dim_count - 1])
+        out_str = " P(" + outcome
+    if dim_count > 1:
+        out_str += " | "
+    for parent_idx in range(0, len(parents)):
+        if parent_idx > 0:
+            out_str += ","
+            parent_handle = parents[parent_idx]
+            out_str += net.get_node_id(parent_handle) + "=" + \
+            net.get_outcome_id(parent_handle, coords[parent_idx])
+            prob = cpt[elem_idx]
+            out_str += ")=" + str(prob)
+            print(out_str)
+    """
+
+
+
+
+def print_node_info(node_handle):
+    print("Node id/name: " + net.get_node_id(node_handle) + "/" +
+    net.get_node_name(node_handle))
+    """
+    print("Outcomes: " + net.get_outcome_ids(node_handle))
+    """
+    parent_ids = net.get_parent_ids(node_handle)
+    if len(parent_ids) > 0:
+        print(" Parents: " + " ".join(parent_ids))
+    print_cpt_matrix(node_handle)
+   
+def index_to_coords(index, dim_sizes, coords):
+    prod = 1
+    for i in range(len(dim_sizes) - 1, -1, -1):
+        coords[i] = int(index / prod) % dim_sizes[i]
+        prod *= dim_sizes[i]
+
 
 def modal_choice_getprobabilities(cnx, request):
     """
@@ -32,6 +132,23 @@ def modal_choice_getprobabilities(cnx, request):
     """
     try:
         node = request.args.get("node", default=None, type=str)
+
+        print("1") 
+        cpt = net.get_node_definition(node)
+        print("2")
+
+        parents = net.get_parents(node)
+        print(parents)
+        print(len(parents)) 
+       
+        if len(parents) > 0:
+           print(parents)
+        
+        """for h in net.get_all_nodes():
+            print_node_info(h)
+        """            
+        print("Tutorial3 complete.")
+
         ret_json = {"node": [node], "probabilities": [32,43,54,23]}
         return ret_json
     except:
@@ -79,9 +196,20 @@ def modal_choice_getvalues(cnx, request):
              ret_json = {"node": [node], "values": ["val1","val2","val3"]} 
         else:  
             ret_json = [{"node": "node1", "values": ["val1","val2","val3"]},{"node": "node2","values": ["val1", "val2", "val3"]},{"node": "node3","values": ["val1", "val2", "val3"]}]
- 
+
+
+        print(net.get_node_definition("Gender"))
+        print(net.get_outcome_id("Gender", 0)) 
+        print(net.get_outcome_id("Gender", 1))       
+        
+
+        ret_json=net.get_node_definition("Gender")
+
+
+
         return ret_json
     except:
         return constants.ERROR_EXCEPTION
 
 
+
-- 
GitLab