Core
The graph data model: nodes, relations, credences, and their JSON form.
Beta
dataclass
¶
A Beta(alpha, beta) distribution over the probability.
Attributes:
| Name | Type | Description |
|---|---|---|
alpha |
float
|
First shape parameter, |
beta |
float
|
Second shape parameter, |
Source code in src/credencegraph/core/credence.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
concentration
property
¶
The concentration alpha + beta.
mean
property
¶
The mean alpha / (alpha + beta).
When alpha + beta overflows to infinity, both parameters are halved first. Halving
a float this large is exact, so the ratio is unchanged and the mean is computed
correctly instead of collapsing to zero.
variance
property
¶
The variance mean * (1 - mean) / (alpha + beta + 1).
__post_init__()
¶
Validate and normalise both shape parameters to floats.
Source code in src/credencegraph/core/credence.py
77 78 79 80 81 82 83 84 85 86 87 88 | |
from_mean_concentration(mean, concentration)
classmethod
¶
Build a Beta from its mean and concentration alpha + beta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
float
|
The mean, strictly between 0 and 1. |
required |
concentration
|
float
|
The concentration, greater than 0. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The Beta with |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If an argument is out of range. |
Source code in src/credencegraph/core/credence.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
from_mean_sd(mean, sd)
classmethod
¶
Build a Beta from its mean and standard deviation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
float
|
The mean, strictly between 0 and 1. |
required |
sd
|
float
|
The standard deviation, greater than 0 and below |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The Beta with the requested mean and standard deviation. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If an argument is out of range or the pair is unattainable. |
Source code in src/credencegraph/core/credence.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
CredenceGraphError
¶
Bases: Exception
Base class for every error raised by credencegraph.
Source code in src/credencegraph/core/errors.py
6 7 | |
CycleError
¶
Bases: ValidationError
A relation would close a cycle among requires, supports and refutes relations.
Attributes:
| Name | Type | Description |
|---|---|---|
cycle |
The node ids around the cycle, starting and ending at the same node. |
|
relations |
The ids of the relations that form the cycle, in the order they are traversed. |
Source code in src/credencegraph/core/errors.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
__init__(message, cycle, relations)
¶
Store the cycle alongside the message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable description naming the cycle. |
required |
cycle
|
tuple[str, ...]
|
Node ids around the cycle, first equal to last. |
required |
relations
|
tuple[str, ...]
|
Relation ids along the cycle. |
required |
Source code in src/credencegraph/core/errors.py
22 23 24 25 26 27 28 29 30 31 32 | |
Discharge
dataclass
¶
How an obligation was met.
Each kind takes only its own fields: theorem for formal; script, and the optional
digest and status of its recorded run, for symbolic and numerical, which also
needs the expected value and the tolerance around it; anchor, with a quote, for
citation; nothing for judgement.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
str
|
One of |
theorem |
str | None
|
The name of the theorem that proves the step. |
script |
str | None
|
Path of the Python script, relative to the graph file's directory. |
digest |
str | None
|
|
status |
int | None
|
The exit status of that run; 0 means the check passed. |
value |
float | None
|
The value the numerical script's printed result must lie near. |
tolerance |
float | None
|
How far from |
anchor |
SourceAnchor | None
|
The cited source and the quote from it. |
Source code in src/credencegraph/core/obligation.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
__post_init__()
¶
Validate the fields against the kind.
Source code in src/credencegraph/core/obligation.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
Graph
¶
Nodes and relations keyed by stable string ids.
Every write is validated and leaves the graph unchanged when it is rejected. Relations must name
existing nodes, and the subgraph of requires, supports and refutes relations must
stay acyclic. Annotation relations and equivalent/exclusive relations do not take part in
the cycle check.
Source code in src/credencegraph/core/graph.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 | |
nodes
property
¶
Read-only view of the nodes by id, in insertion order.
relations
property
¶
Read-only view of the relations by id, in insertion order.
__contains__(node_id)
¶
Tell whether a node with this id exists.
Source code in src/credencegraph/core/graph.py
47 48 49 | |
__eq__(other)
¶
Compare graphs by their nodes and relations, ignoring insertion order.
Source code in src/credencegraph/core/graph.py
51 52 53 54 55 | |
__init__()
¶
Create an empty graph.
Source code in src/credencegraph/core/graph.py
22 23 24 25 26 27 | |
__iter__()
¶
Iterate over the nodes in insertion order.
Source code in src/credencegraph/core/graph.py
43 44 45 | |
__len__()
¶
Return the number of nodes.
Source code in src/credencegraph/core/graph.py
39 40 41 | |
__repr__()
¶
Summarise the graph.
Source code in src/credencegraph/core/graph.py
59 60 61 | |
add_node(node)
¶
Add a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
The node to add. |
required |
Returns:
| Type | Description |
|---|---|
Node
|
The node. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If |
Source code in src/credencegraph/core/graph.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
add_relation(relation)
¶
Add a relation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relation
|
Relation
|
The relation to add. |
required |
Returns:
| Type | Description |
|---|---|
Relation
|
The relation. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If |
CycleError
|
If a |
Source code in src/credencegraph/core/graph.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
Node
dataclass
¶
A node of a graph: usually a proposition, but any carried entity.
base and stated accept a bare float, read as a Point. sources accepts any iterable
of anchors. attributes is deep-copied and frozen on construction.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Stable identifier, unique within the graph. |
kind |
str
|
Free label the engine attaches no meaning to. |
statement |
str | None
|
The normalised proposition. |
sources |
tuple[SourceAnchor, ...]
|
Where the proposition came from. |
base |
Credence | None
|
For a node with no inferential parents, its prior credence. |
stated |
Credence | None
|
The credence the source itself asserts, used only by diagnostics. |
attributes |
Mapping[str, FrozenJSON]
|
Open JSON metadata, stored read-only: objects as read-only mappings, arrays as tuples. |
Source code in src/credencegraph/core/node.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
__post_init__()
¶
Validate every field and normalise the flexible ones.
Source code in src/credencegraph/core/node.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
Obligation
dataclass
¶
What must be true for an inferential step to hold, and how it was met, if it was.
Attributes:
| Name | Type | Description |
|---|---|---|
statement |
str
|
The condition, in words. |
discharge |
Discharge | None
|
How it was met, or |
Source code in src/credencegraph/core/obligation.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
__post_init__()
¶
Validate the fields.
Source code in src/credencegraph/core/obligation.py
157 158 159 160 161 162 | |
Point
dataclass
¶
A credence with no spread: the probability is exactly p.
Attributes:
| Name | Type | Description |
|---|---|---|
p |
float
|
The probability, |
Source code in src/credencegraph/core/credence.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
mean
property
¶
The mean, equal to p.
variance
property
¶
The variance, always zero.
__post_init__()
¶
Validate and normalise p to a float.
Source code in src/credencegraph/core/credence.py
46 47 48 49 50 51 52 | |
Relation
dataclass
¶
A directed relation between two nodes.
strength is required for requires, supports and refutes, and forbidden for
every other type, including equivalent, exclusive and all annotations. A bare float is read
as a Point. equivalent and exclusive relations may not join a node to itself. Only an
inferential relation may carry an obligation.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Stable identifier, unique within the graph. |
type |
str
|
An inferential type or any other string, which makes the relation an annotation. |
source |
str
|
Id of the source node. |
target |
str
|
Id of the target node. |
strength |
Credence | None
|
The relation's strength, per the rules above. |
attributes |
Mapping[str, FrozenJSON]
|
Open JSON metadata, stored read-only: objects as read-only mappings, arrays as tuples. |
obligation |
Obligation | None
|
What must be true for the step to hold, and how it was met. |
Source code in src/credencegraph/core/relation.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
is_inferential
property
¶
Whether the relation is inferential rather than an annotation.
__post_init__()
¶
Validate the fields and enforce the strength rules.
Source code in src/credencegraph/core/relation.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
SourceAnchor
dataclass
¶
Where a proposition came from.
Attributes:
| Name | Type | Description |
|---|---|---|
document |
str
|
Caller-chosen identifier of the source (a DOI, a path, a URL). |
locator |
str | None
|
A page, section or character span within the document. |
quote |
str | None
|
The anchored text. |
digest |
str | None
|
A hash of the anchored text, for detecting later changes to the source. |
Source code in src/credencegraph/core/anchor.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
__post_init__()
¶
Validate the fields.
Source code in src/credencegraph/core/anchor.py
44 45 46 47 48 49 | |
ValidationError
¶
Bases: CredenceGraphError, ValueError
A value, node, relation or document breaks a rule of the data model.
Source code in src/credencegraph/core/errors.py
10 11 | |
as_credence(value)
¶
Coerce value to a credence, reading a bare float as a Point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Credence | float
|
A |
required |
Returns:
| Type | Description |
|---|---|
Credence
|
The credence. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If |
Source code in src/credencegraph/core/credence.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
dump(graph, path, *, indent=2)
¶
Write a graph to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph. |
required |
path
|
str | Path
|
Destination file. |
required |
indent
|
int | None
|
JSON indentation, or |
2
|
Source code in src/credencegraph/core/serialization.py
281 282 283 284 285 286 287 288 289 | |
dumps(graph, *, indent=2)
¶
Serialise a graph to a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph. |
required |
indent
|
int | None
|
JSON indentation, or |
2
|
Returns:
| Type | Description |
|---|---|
str
|
The JSON text. |
Source code in src/credencegraph/core/serialization.py
242 243 244 245 246 247 248 249 250 251 252 | |
graph_from_dict(data)
¶
Build a graph from a dictionary in the credencegraph JSON format.
Nodes are added before relations, in document order, through the same validated writes as any other graph, so a cycle in the file is rejected and named.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The decoded JSON document. |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
The graph. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the document is malformed, has the wrong format or an unsupported version, or breaks a rule of the data model. The message locates the offending item. |
CycleError
|
If the relations form a cycle among |
Source code in src/credencegraph/core/serialization.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
graph_to_dict(graph)
¶
Convert a graph to a JSON-ready dictionary in the current format version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary with |
Source code in src/credencegraph/core/serialization.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
is_inferential(relation_type)
¶
Tell whether relation_type is inferential rather than an annotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relation_type
|
str
|
The relation type string. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/credencegraph/core/relation.py
30 31 32 33 34 35 36 37 38 39 | |
json_schema()
¶
Return the JSON Schema for the current format version.
The schema covers structure and types. Rules it cannot express, namely unique ids, relations
naming existing nodes, acyclicity, and the ban on an equivalent or exclusive relation
joining a node to itself, are enforced when a graph is loaded.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The schema as a dictionary. |
Source code in src/credencegraph/core/serialization.py
308 309 310 311 312 313 314 315 316 317 318 319 | |
load(path)
¶
Read a graph from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Source file. |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
The graph. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
As |
CycleError
|
As |
Source code in src/credencegraph/core/serialization.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
loads(text)
¶
Parse a graph from a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The JSON text. |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
The graph. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the text is not valid JSON, or as |
CycleError
|
As |
Source code in src/credencegraph/core/serialization.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
parse_credence(text)
¶
Parse a credence written as text: a bare probability is a Point, beta:A,B a Beta.
This is the syntax of the command line and of rubric files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text, such as |
required |
Returns:
| Type | Description |
|---|---|
Credence
|
The credence. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the text is neither form, or its numbers are out of range. The message starts with the text, quoted. |
Source code in src/credencegraph/core/credence.py
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 | |
JSON format¶
A graph serialises to a JSON document with "format": "credencegraph" and an
integer "version" (currently 1). The structure and types are described by a
published JSON Schema, available from credencegraph.core.json_schema() and
shipped in the package as credencegraph/core/schema/graph-1.schema.json.
The schema covers structure only. Unique ids, relations that name existing
nodes, the absence of cycles among requires, supports and refutes
relations, and the ban on an equivalent or exclusive relation joining a node
to itself are checked when a document is loaded, because a schema cannot express
them.
A credence is a bare number p (a point value with 0 <= p <= 1) or an object
{"alpha": a, "beta": b} with a, b > 0.