Tripartite Graph
Please remember that this class is a subclass of NXBipartiteGraph, so it inherits all its methods. You can check their documentation as well!
NXTripartiteGraph(source_frame=None, item_exo_properties=None, item_contents_dir=None, link_label=None)
Bases: NXBipartiteGraph
, TripartiteDiGraph
Class that implements a Tripartite graph through networkx library.
Info
A Tripartite Graph is a graph which supports User nodes, Item nodes and Property nodes, but the latter can only be linked to Item nodes. If you need maximum flexibility, consider using a Full Graph
It creates a graph from an initial Rating object.
Consider the following matrix representation of the Rating object
+------+-----------+-------+
| User | Item | Score |
+------+-----------+-------+
| u1 | Tenet | 4 |
| u2 | Inception | 5 |
| ... | ... | ... |
+------+-----------+-------+
The graph will be created with the following interactions:
4
u1 -----> Tenet
5
u2 -----> Inception
where u1
and u2
become User nodes and Tenet
and Inception
become Item nodes,
with the edge weighted depending on the score given
If the link_label
parameter is specified, then each link between users and items will be labeled with the label
specified (e.g. link_label='score'
):
(4, 'score')
u1 -------------> Tenet
(5, 'score')
u2 -------------> Inception
Then the framework tries to load 'Tenet' and 'Inception' from the item_contents_dir
if it is specified and if
succeeds, adds in the graph their loaded properties as specified in the item_exo_properties
parameter.
Load exogenous properties
In order to load properties in the graph, we must specify where items are serialized and which properties to add:
- If item_exo_properties is specified as a set, then the graph will try to load all properties from said exogenous representation
{'my_exo_id'}
- If item_exo_properties is specified as a dict, then the graph will try to load said properties from said exogenous representation
{'my_exo_id': ['my_prop1', 'my_prop2']]}
PARAMETER | DESCRIPTION |
---|---|
source_frame |
The initial Ratings object needed to create the graph
TYPE:
|
item_exo_properties |
Set or Dict which contains representations to load from items. Use a |
item_contents_dir |
The path containing items serialized with the Content Analyzer
TYPE:
|
link_label |
If specified, each link will be labeled with the given label. Default is None
TYPE:
|
Source code in clayrs/recsys/graphs/nx_implementation/nx_tripartite_graphs.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
property_nodes: Set[PropertyNode]
property
Returns a set of all Property nodes in the graph
add_link(start_node, final_node, weight=None, label=None, timestamp=None)
Creates a link connecting the start_node
to the final_node
. If two lists are passed, then the node in
position \(i\) in the start_node
list will be linked to the node in position \(i\) in the final_node
list.
If nodes to link do not exist, they will be added automatically to the graph. Please remember that since this is a Tripartite Graph, only User nodes, Item nodes and Property nodes can be added! And Property nodes can only be linked to Item nodes!
A link can be weighted with the weight
parameter and labeled with the label
parameter.
A timestamp can also be specified via timestamp
parameter.
All three are optional parameters, so they are not required
PARAMETER | DESCRIPTION |
---|---|
start_node |
Single Node object or a list of Node objects. They will be the 'head' of the link, since it's a directed graph |
final_node |
Single Node object or a list Node objects. They will be the 'tail' of the link, since it's a directed graph
TYPE:
|
weight |
weight of the link, default is None (no weight)
TYPE:
|
label |
label of the link, default is None (no label)
TYPE:
|
timestamp |
timestamp of the link, default is None (no timestamp)
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
Exception raised when Property nodes are tried to be linked with non-Item nodes |
Source code in clayrs/recsys/graphs/nx_implementation/nx_tripartite_graphs.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
|
add_node(node)
Adds one or multiple Node objects to the graph.
Since this is a Tripartite Graph, only User Node
, Item Node
and Property Node
can be added!
No duplicates are allowed, but different category nodes with same id are (e.g. ItemNode('1')
and
UserNode('1')
)
PARAMETER | DESCRIPTION |
---|---|
node |
Node(s) object(s) that needs to be added to the graph |
RAISES | DESCRIPTION |
---|---|
ValueError
|
Exception raised when one of the node to add to the graph is not a User, Item or Property node |
Source code in clayrs/recsys/graphs/nx_implementation/nx_tripartite_graphs.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
add_node_with_prop(node, item_exo_properties, item_contents_dir, item_filename=None)
Adds one or multiple Node objects and its/their properties to the graph.
Since this is a Tripartite Graph, only Item Node
are allowed to have properties!
In order to load properties in the graph, we must specify where items are serialized and which properties to add:
- If item_exo_properties is specified as a set, then the graph will try to load all properties from said exogenous representation
{'my_exo_id'}
- If item_exo_properties is specified as a dict, then the graph will try to load said properties from said exogenous representation
{'my_exo_id': ['my_prop1', 'my_prop2']]}
In case you want your node to have a different id from serialized contents, via the item_filename
parameter
you can specify what is the filename of the node that you are adding, e.g.
item_to_add = ItemNode('different_id')
# item_filename is 'item_serialized_1.xz'
graph.add_node_with_prop(item_to_add, ..., item_filename='item_serialized_1')
In case you are adding a list of nodes, you can specify the filename for each node in the list.
PARAMETER | DESCRIPTION |
---|---|
node |
Node(s) object(s) that needs to be added to the graph along with their properties |
item_exo_properties |
Set or Dict which contains representations to load from items. Use a |
item_contents_dir |
The path containing items serialized with the Content Analyzer
TYPE:
|
item_filename |
Filename(s) of the node(s) to add |
RAISES | DESCRIPTION |
---|---|
ValueError
|
Exception raised when one of the node to add to the graph with their properties is not an ItemNode |
Source code in clayrs/recsys/graphs/nx_implementation/nx_tripartite_graphs.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|