This notebooks shows how to use a client to connect to the server. Note that access to a running server is required. If you're using docker, this is as easy as running the following command on the command line:
docker run --rm -it -p 5000:5000 mellesies/thomas-server
You should be able to login using the user root
with password toor
.
See https://github.com/mellesies/thomas-server for details on how to run the server without docker.
%run '_preamble.ipynb'
The autoreload extension is already loaded. To reload it, use: %reload_ext autoreload available imports: import os import logging import pandas as pd import numpy as np connect to this kernel with: jupyter console --existing 553507c9-cef1-4499-97ab-af543b5267bf Could not create logging directory "../logs" Logging to: "../logs/notebook.log" Current date/time: 26-04-2021, 13:21 Current working directory: "/Users/melle/software-development/thomas-master/notebooks"
import requests
from requests.exceptions import ConnectionError
import json
from thomas.core import examples
from thomas.client import Client
from thomas.jupyter import BayesianNetworkWidget
servers = [
# 'http://thomas.zakbroek.com',
'http://localhost:5000',
'http://thomas-server:5000',
]
for hostname in servers:
try:
client = Client(hostname)
client.authenticate('root', 'toor')
except ConnectionError as e:
print(f'!!! Could not connect to {hostname}')
else:
print(f'Connected to {hostname}')
break
Connected to http://localhost:5000
client.list_networks()
id | name | |
---|---|---|
0 | lungcancer | Lungcancer |
1 | student | Student |
2 | sprinkler | Sprinkler |
Gs = client.load('student')
client._metadata[Gs]
{'resourceType': 'Network', '_id': '/network/student', '_links': {'_self': '/network/student', '_collection': '/network', 'owner': '/user/1'}, '_excluded': [], 'id': 'student', 'name': 'Student'}
view = BayesianNetworkWidget(Gs, height=300)
view
BayesianNetworkWidget(marginals_and_evidence={'marginals': {'I': {'i0': 0.7000000000000001, 'i1': 0.3}, 'S': {…
Gs.set_evidence_hard('I', 'i0')
# Gs.as_json()
Gs = examples.get_student_network_from_CPTs()
client.save(Gs)
<BayesianNetwork name='Student'> <Node RV='I' description='' states=['i0', 'i1'] /> <Node RV='S' description='' states=['s0', 's1'] /> <Node RV='D' description='' states=['d0', 'd1'] /> <Node RV='G' description='' states=['g1', 'g2', 'g3'] /> <Node RV='L' description='' states=['l0', 'l1'] /> </BayesianNetwork>
client._metadata[Gs]
{'resourceType': 'Network', '_id': '/network/89a7b8ce-a681-11eb-bdb8-0242ac110002', '_excluded': [], 'id': '89a7b8ce-a681-11eb-bdb8-0242ac110002', 'name': 'Student'}
Gs.as_dict()['name']
'Student'
client.list_networks()
id | name | |
---|---|---|
0 | lungcancer | Lungcancer |
1 | student | Student |
2 | sprinkler | Sprinkler |
3 | 89a7b8ce-a681-11eb-bdb8-0242ac110002 | Student |
client._metadata[Gs]
{'resourceType': 'Network', '_id': '/network/89a7b8ce-a681-11eb-bdb8-0242ac110002', '_excluded': [], 'id': '89a7b8ce-a681-11eb-bdb8-0242ac110002', 'name': 'Student'}
Gs2 = client.save_as(Gs, 'student2')
Gs2.name
'Student'
client.delete(Gs)
{'message': 'OK'}
client.delete('student2')
{'message': 'OK'}