Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
Recommendation Engine
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
URBANITE
Open
Recommendation Engine
Commits
5ddc0cdf
Commit
5ddc0cdf
authored
2 years ago
by
Campos Cordobes, Sergio
Browse files
Options
Downloads
Patches
Plain Diff
Update modal_choice.py
parent
0b4200f4
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/modal_choice.py
+131
-3
131 additions, 3 deletions
src/modal_choice.py
with
131 additions
and
3 deletions
src/modal_choice.py
+
131
−
3
View file @
5ddc0cdf
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment