|
| 1 | +# Custom Thresholds Example |
| 2 | + |
| 3 | +Here is an example that utilizes custom thresholds. |
| 4 | + |
| 5 | +The following graph represents a network of People and a Text Message in their group chat. |
| 6 | +<img src="../media/group_chat_graph.png"/> |
| 7 | + |
| 8 | +In this case, we want to know when a text message has been viewed by all members of the group chat. |
| 9 | + |
| 10 | +## Graph |
| 11 | +First, lets create the group chat. |
| 12 | + |
| 13 | +```python |
| 14 | +import networkx as nx |
| 15 | + |
| 16 | +# Create an empty graph |
| 17 | +G = nx.Graph() |
| 18 | + |
| 19 | +# Add nodes |
| 20 | +nodes = ["TextMessage", "Zach", "Justin", "Michelle", "Amy"] |
| 21 | +G.add_nodes_from(nodes) |
| 22 | + |
| 23 | +# Add edges with attribute 'HaveAccess' |
| 24 | +edges = [ |
| 25 | + ("Zach", "TextMessage", {"HaveAccess": 1}), |
| 26 | + ("Justin", "TextMessage", {"HaveAccess": 1}), |
| 27 | + ("Michelle", "TextMessage", {"HaveAccess": 1}), |
| 28 | + ("Amy", "TextMessage", {"HaveAccess": 1}) |
| 29 | +] |
| 30 | +G.add_edges_from(edges) |
| 31 | + |
| 32 | +``` |
| 33 | + |
| 34 | +## Rules and Custom Thresholds |
| 35 | +Considering that we only want a text message to be considered viewed by all if it has been viewed by everyone that can view it, we define the rule as follows: |
| 36 | + |
| 37 | +```text |
| 38 | +ViewedByAll(x) <- HaveAccess(x,y), Viewed(y) |
| 39 | +``` |
| 40 | + |
| 41 | +The `head` of the rule is `ViewedByAll(x)` and the body is `HaveAccess(x,y), Viewed(y)`. The head and body are separated by an arrow which means the rule will start evaluating from |
| 42 | +timestep 0. |
| 43 | + |
| 44 | +We add the rule into pyreason with: |
| 45 | + |
| 46 | +```python |
| 47 | +import pyreason as pr |
| 48 | +from pyreason import Threshold |
| 49 | + |
| 50 | +user_defined_thresholds = [ |
| 51 | + Threshold("greater_equal", ("number", "total"), 1), |
| 52 | + Threshold("greater_equal", ("percent", "total"), 100), |
| 53 | +] |
| 54 | + |
| 55 | +pr.add_rule(pr.Rule('ViewedByAll(x) <- HaveAccess(x,y), Viewed(y)', 'viewed_by_all_rule', user_defined_thresholds)) |
| 56 | +``` |
| 57 | +Where `viewed_by_all_rule` is the name of the rule. This helps to understand which rule/s are fired during reasoning later on. |
| 58 | + |
| 59 | +The `user_defined_thresholds` are a list of custom thresholds of the format: (quantifier, quantifier_type, thresh) where: |
| 60 | +- quantifier can be greater_equal, greater, less_equal, less, equal |
| 61 | +- quantifier_type is a tuple where the first element can be either number or percent and the second element can be either total or available |
| 62 | +- thresh represents the numerical threshold value to compare against |
| 63 | + |
| 64 | +The custom thresholds are created corresponding to the two clauses (HaveAccess(x,y) and Viewed(y)) as below: |
| 65 | +- ('greater_equal', ('number', 'total'), 1) (there needs to be at least one person who has access to TextMessage for the first clause to be satisfied) |
| 66 | +- ('greater_equal', ('percent', 'total'), 100) (100% of people who have access to TextMessage need to view the message for second clause to be satisfied) |
| 67 | + |
| 68 | +## Facts |
| 69 | +The facts determine the initial conditions of elements in the graph. They can be specified from the graph attributes but in that |
| 70 | +case they will be immutable later on. Adding PyReason facts gives us more flexibility. |
| 71 | + |
| 72 | +In our case we want one person to view the TextMessage in a particular interval of timestep. |
| 73 | +For example, we create facts stating: |
| 74 | +- Zach and Justin view the TextMessage from at timestep 0 |
| 75 | +- Michelle views the TextMessage at timestep 1 |
| 76 | +- Amy views the TextMessage at timestep 2 |
| 77 | + |
| 78 | +We add the facts in PyReason as below: |
| 79 | +```python |
| 80 | +import pyreason as pr |
| 81 | + |
| 82 | +pr.add_fact(pr.Fact("seen-fact-zach", "Zach", "Viewed", [1, 1], 0, 0, static=True)) |
| 83 | +pr.add_fact(pr.Fact("seen-fact-justin", "Justin", "Viewed", [1, 1], 0, 0, static=True)) |
| 84 | +pr.add_fact(pr.Fact("seen-fact-michelle", "Michelle", "Viewed", [1, 1], 1, 1, static=True)) |
| 85 | +pr.add_fact(pr.Fact("seen-fact-amy", "Amy", "Viewed", [1, 1], 2, 2, static=True)) |
| 86 | +``` |
| 87 | + |
| 88 | +This allows us to specify the component that has an initial condition, the initial condition itself in the form of bounds |
| 89 | +as well as the start and end time of this condition. |
| 90 | + |
| 91 | +## Running PyReason |
| 92 | +Find the full code for this example [here](../tests/test_custom_thresholds.py) |
| 93 | + |
| 94 | +The main line that runs the reasoning in that file is: |
| 95 | +```python |
| 96 | +interpretation = pr.reason(timesteps=3) |
| 97 | +``` |
| 98 | +This specifies how many timesteps to run for. |
| 99 | + |
| 100 | +## Expected Output |
| 101 | +After running the python file, the expected output is: |
| 102 | + |
| 103 | +``` |
| 104 | +TIMESTEP - 0 |
| 105 | +Empty DataFrame |
| 106 | +Columns: [component, ViewedByAll] |
| 107 | +Index: [] |
| 108 | +
|
| 109 | +TIMESTEP - 1 |
| 110 | +Empty DataFrame |
| 111 | +Columns: [component, ViewedByAll] |
| 112 | +Index: [] |
| 113 | +
|
| 114 | +TIMESTEP - 2 |
| 115 | + component ViewedByAll |
| 116 | +0 TextMessage [1.0, 1.0] |
| 117 | +
|
| 118 | +TIMESTEP - 3 |
| 119 | + component ViewedByAll |
| 120 | +0 TextMessage [1.0, 1.0] |
| 121 | +
|
| 122 | +``` |
| 123 | + |
| 124 | +1. For timestep 0, we set `Zach -> Viewed: [1,1]` and `Justin -> Viewed: [1,1]` in the facts |
| 125 | +2. For timestep 1, Michelle views the TextMessage as stated in facts `Michelle -> Viewed: [1,1]` |
| 126 | +3. For timestep 2, since Amy has just viewed the TextMessage, therefore `Amy -> Viewed: [1,1]`. As per the rule, |
| 127 | +since all the people have viewed the TextMessage, the message is marked as ViewedByAll. |
| 128 | + |
| 129 | + |
| 130 | +We also output two CSV files detailing all the events that took place during reasoning (one for nodes, one for edges) |
0 commit comments