API reference¶
Every public function and the Theory class, in the groups the R package's
reference index uses
and in the same order, so a name is found in the same place on either site. The
R package prefixes each name with tf_, so check() here is tf_check()
there.
The four functions whose name matches their module (diagram, dossier,
implications, simulate) are documented by their canonical path, so that the
function rather than the module is shown.
Core IO¶
Read, validate and write theory objects. Theory is also the builder: adding a
construct, a proposition or a prediction, and logging the provenance of each,
are methods on the class here, where the R package exposes them as tf_add_*
functions.
theoryforge.Theory
¶
A theory as a versioned, machine-checkable object.
Wraps the parsed mapping (self.data); all accessors tolerate missing
optional collections by treating them as empty.
Source code in src/theoryforge/core.py
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 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
appraise_amendment(prior)
¶
Progressive vs degenerating verdict for this theory relative to a prior version.
Source code in src/theoryforge/core.py
306 307 308 | |
compile_sem()
¶
Compile constructs and propositions to lavaan model syntax.
Source code in src/theoryforge/core.py
326 327 328 | |
diagram(type='nomological_net', engine='graphviz')
¶
Return the diagram IR for type (one of nomological_net, provenance,
causal_dag, development_roadmap, pipeline, context, workflow, venn, rigour,
severity).
Source code in src/theoryforge/core.py
290 291 292 293 294 | |
dossier()
¶
A reviewer-facing audit bundle of rigour report, severity, provenance, and preregistration.
Source code in src/theoryforge/core.py
330 331 332 | |
embedding_redundancy(embedder, threshold=None)
¶
An opt-in embedding-based redundancy screen; results depend on the supplied embedder.
Source code in src/theoryforge/core.py
339 340 341 | |
implications()
¶
The conditional independencies the causal subgraph commits the theory to.
Source code in src/theoryforge/core.py
310 311 312 | |
landscape(corpus, min_link=2)
¶
Map this theory and its alternatives onto a literature corpus's themes.
Source code in src/theoryforge/core.py
318 319 320 | |
new_evidence_dois(candidate_dois)
¶
DOIs in candidate_dois not already cited by this theory's evidence or alternatives.
Source code in src/theoryforge/core.py
322 323 324 | |
osf_push(token=None, node=None, filename=None, dry_run=True, base_url=None)
¶
Deposit the dossier on OSF (dry-run by default). A live push needs a token and node.
Source code in src/theoryforge/core.py
347 348 349 350 351 | |
preregister(path=None)
¶
Render a preregistration document (and write it if a path is given).
Source code in src/theoryforge/core.py
314 315 316 | |
render_diagram(type='nomological_net')
¶
Render a digraph view via the optional graphviz library; see
:func:theoryforge.render.render_diagram.
Source code in src/theoryforge/core.py
296 297 298 299 300 | |
render_report(path, title=None, render=False, to='html')
¶
Write (and optionally render) a Quarto report of the audit dossier.
Source code in src/theoryforge/core.py
343 344 345 | |
severity()
¶
Per-prediction risk and computed severity from the operationalised rubric.
Source code in src/theoryforge/core.py
302 303 304 | |
simulate(steps=10, dt=0.1, k=1.0, damping=0.5, init=1.0)
¶
Integrate the construct network as a linear dynamical system.
Source code in src/theoryforge/core.py
334 335 336 337 | |
validate(*, full=False)
¶
Structural validation against the schema's required fields and enums.
Returns True on success, raises ValueError listing every problem found.
With full=True additionally checks referential integrity: that every
id is unique within its collection and that every cross-reference
(proposition endpoints, prediction derivations and diagnostics,
assumption/evidence/test-outcome targets) points to a declared id, and
that every prediction severity is a number within [0, 1]. The
full checks are deterministic.
Source code in src/theoryforge/core.py
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 | |
theoryforge.new_theory(id, title, maturity='building', theory_form='network')
¶
Start a new, empty theory object (BUILDING mode entry point).
Source code in src/theoryforge/core.py
372 373 374 375 376 377 378 379 380 381 382 | |
theoryforge.read(path)
¶
Read a theory object from a YAML or JSON file.
Source code in src/theoryforge/core.py
357 358 359 360 361 362 363 364 | |
theoryforge.write(theory, path)
¶
Write a theory object to YAML or JSON (chosen by file extension).
Source code in src/theoryforge/core.py
367 368 369 | |
Packaged examples¶
Example theories shipped inside the package, so that nothing has to be
downloaded to follow the documentation. The R twin reaches the same files
through tf_example_names() and tf_example_path().
theoryforge.example_names()
¶
The names of the example theory and corpus files, sorted.
Only .yaml entries count, matching the R twin's list.files(pattern =
"\\.yaml$"). Listing the directory wholesale would let anything else
that lands beside the examples, such as a __pycache__ directory left by
a local build, show up here and not in R.
Source code in src/theoryforge/examples.py
14 15 16 17 18 19 20 21 22 23 | |
theoryforge.example_path(name)
¶
Filesystem path of a packaged example, e.g. panic-network.theory.yaml.
Raises FileNotFoundError naming the available files when name is not
one of them.
Source code in src/theoryforge/examples.py
26 27 28 29 30 31 32 33 34 35 36 37 38 | |
Rigour¶
Score a theory against the versioned rigour checklist, and report what the score was made of.
theoryforge.check(T)
¶
Compute the full rigour report (dict) for a Theory or theory mapping.
Source code in src/theoryforge/rigor.py
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 | |
theoryforge.report(T, format='json')
¶
Render the rigour report. format in {'json', 'html'}.
Source code in src/theoryforge/rigor.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
theoryforge.severity(T)
¶
Per-prediction risk and computed severity, in file order.
References
Mayo, D. G. (2018). Statistical inference as severe testing. Cambridge University Press. https://doi.org/10.1017/9781107286184 Meehl, P. E. (1990). Why summaries of research on psychological theories are often uninterpretable. Psychological Reports, 66, 195-244. https://doi.org/10.2466/pr0.1990.66.1.195
Source code in src/theoryforge/scoring.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 | |
Redundancy¶
Screen a theory's constructs for lexical redundancy against each other and against the field, deterministically, with an opt-in embedding variant.
theoryforge.tokens(s)
¶
Tokenise a string into a set of content tokens.
Source code in src/theoryforge/redundancy.py
21 22 23 24 25 | |
theoryforge.jaccard(a, b)
¶
Source code in src/theoryforge/redundancy.py
28 29 30 31 32 33 | |
theoryforge.redundancy_check(T)
¶
Pairwise lexical similarity of construct definitions.
Returns one record per unordered construct pair, sorted by descending
similarity then (a, b) ascending.
References
Le, H., Schmidt, F. L., Harter, J. K., & Lauver, K. J. (2010). The problem of empirical redundancy of constructs. Organizational Behavior and Human Decision Processes, 112(2), 112-125. https://doi.org/10.1016/j.obhdp.2010.02.003 Lawson, K. M., & Robins, R. W. (2021). Sibling constructs. Personality and Social Psychology Review, 25(4), 344-366. https://doi.org/10.1177/10888683211047101
Source code in src/theoryforge/redundancy.py
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 | |
theoryforge.embedding_redundancy(T, embedder, threshold=None)
¶
Pairwise cosine similarity of embedded construct definitions.
embedder maps a definition string to a numeric vector; the vectors of every compared
pair must be of equal, nonzero length, or the pair is refused. Returns one record per
unordered construct pair, sorted by descending similarity then (a, b), with a
review/ok flag.
Source code in src/theoryforge/embedding.py
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 | |
Diagram¶
Byte-identical diagram intermediate representations, and native rendering of them.
theoryforge.diagram.diagram(T, type='nomological_net', engine='graphviz')
¶
Return the diagram IR string for the requested type.
engine is accepted but has no effect, because the IR is engine-independent
(DOT for the digraphs, dagitty syntax for the causal DAG, and SVG for the Venn,
the rigour grid and the severity chart).
Source code in src/theoryforge/diagram.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
theoryforge.lit_diagram(obj, type='keyword_cooccurrence')
¶
DOT for the literature layer. type in {keyword_cooccurrence, co_citation, theme_landscape}.
Source code in src/theoryforge/lit.py
187 188 189 190 191 192 193 194 195 196 | |
theoryforge.render_diagram(x, type='nomological_net')
¶
Render a digraph view without leaving Python.
Where :func:theoryforge.diagram.diagram returns the deterministic Graphviz
DOT string, render_diagram wraps it in a graphviz.Source, which
displays inline in Jupyter and renders to a file with its render method.
Requires the optional graphviz <https://pypi.org/project/graphviz/>_
library (pip install theoryforge[render]) and, to write image files, the
Graphviz system binaries.
The three chart views (venn, rigour and severity) are already
SVG, so they come back as an :class:SVGString, which also displays inline
in Jupyter. The causal_dag view emits dagitty syntax rather than DOT, so
it is not rendered here; paste it into a dagitty tool instead.
Parameters¶
x:
A :class:~theoryforge.core.Theory, a parsed theory mapping, or a
diagram IR string from diagram() or lit_diagram(), so literature
diagrams render the same way.
type:
The diagram type, as in diagram(). Ignored when x is already an
IR string.
Returns¶
graphviz.Source or SVGString
A graphviz.Source for the digraph views; an :class:SVGString for
the chart views.
Source code in src/theoryforge/render.py
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 | |
Develop¶
Appraise an amendment to a theory as progressive or degenerating, in Lakatos's sense.
theoryforge.appraise_amendment(new, prior)
¶
Appraise an amendment as progressive, degenerating, or neutral relative to a prior version.
References
Lakatos, I. (1970). Falsification and the methodology of scientific research programmes. In Criticism and the growth of knowledge (pp. 91-196). Cambridge University Press. https://doi.org/10.1017/cbo9781139171434.009 Meehl, P. E. (1990). Appraising and amending theories. Psychological Inquiry, 1(2), 108-141. https://doi.org/10.1207/s15327965pli0102_1
Source code in src/theoryforge/develop.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 62 | |
Testing and review¶
The conditional independencies a causal theory commits to, preregistration, SEM compilation and the reviewer-facing audit dossier.
theoryforge.implications.implications(T)
¶
Conditional independencies implied by a theory's causal subgraph.
Reads the propositions whose relation is causal (causes, increases,
decreases) as directed edges over the constructs they connect, checks
that the resulting graph is acyclic, and returns its basis set: for every
pair of non-adjacent constructs, the claim that the two are independent
given the parents of both (Pearl, 1988; Shipley, 2000). A basis set implies
every other conditional independence the graph entails, so it is the shortest
complete statement of what the theory forbids in data.
Returns {theory_id, acyclic, constructs, n_edges, implications,
n_implications}. constructs lists, in file order, the constructs that
a causal proposition connects; constructs the theory says nothing causal
about are left out, because silence about a construct is not a claim that it
is independent of anything. acyclic is always True in a returned record,
since a cyclic graph is refused, and is carried so that a serialised record
states the verdict rather than leaving a reader to infer that the check ran.
Each entry of implications is {a, b, given, statement}, where
statement renders the claim in the notation dagitty prints,
a _||_ b | z1, z2. Pairs come in construct file order, as do the members
of given, so the two engines return the same records in the same order.
A theory with no causal propositions comes back with an empty basis set and
no error: constructs and implications are empty and n_implications
is 0.
Raises:
| Type | Description |
|---|---|
ValueError
|
if two constructs share an id, if a causal proposition names a construct the theory has not declared, or if the causal graph has a cycle, in which case no basis set is defined and the message names a cycle that was found. |
References
Pearl, J. (1988). Probabilistic reasoning in intelligent systems: Networks of plausible inference. Morgan Kaufmann. Shipley, B. (2000). A new inferential test for path models based on directed acyclic graphs. Structural Equation Modeling, 7(2), 206-218. https://doi.org/10.1207/S15328007SEM0702_4
Example
A mediated chain, arousal raising perceived threat and perceived threat raising avoidance, commits the theory to one thing it does not state directly, that arousal and avoidance are independent once perceived threat is held fixed.
import theoryforge as tf
t = tf.new_theory("mediation", "A mediated chain")
t.add_construct("c_arousal", "Arousal", "Bodily activation.")
t.add_construct("c_threat", "Perceived threat", "Appraised danger.")
t.add_construct("c_avoidance", "Avoidance", "Withdrawal from the trigger.")
t.add_proposition("p1", "c_arousal", "c_threat", "increases")
t.add_proposition("p2", "c_threat", "c_avoidance", "increases")
[i["statement"] for i in t.implications()["implications"]]
# ['c_arousal _||_ c_avoidance | c_threat']
Source code in src/theoryforge/implications.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 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
theoryforge.preregister(T, path=None)
¶
Render a preregistration document, writing it to path when one is given.
Source code in src/theoryforge/prereg.py
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 | |
theoryforge.compile_sem(T)
¶
Source code in src/theoryforge/sem.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
theoryforge.dossier.dossier(T)
¶
Source code in src/theoryforge/dossier.py
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 | |
Simulation¶
Integrate the construct network as a deterministic dynamical system.
theoryforge.simulate.simulate(T, steps=10, dt=0.1, k=1.0, damping=0.5, init=1.0)
¶
Integrate the theory's construct network as a linear dynamical system.
Returns {states, dt, steps, k, damping, init, trajectory}, where trajectory[t] is the state vector at step t (t = 0..steps), each value rounded to 6 decimals. All five knobs are echoed back, because the trajectory cannot be reproduced without them.
Source code in src/theoryforge/simulate.py
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 | |
Reporting and deposit¶
Render a report from a theory, and deposit the result.
theoryforge.render_report(T, path, title=None, render=False, to='html')
¶
Write a Quarto report for the theory; render it with Quarto when render=True.
Returns the path of the written .qmd.
Source code in src/theoryforge/report_render.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
theoryforge.osf_push(T, token=None, node=None, filename=None, dry_run=True, base_url=_DEFAULT_BASE)
¶
Deposit the theory's dossier on OSF.
With dry_run=True (default) the planned request is returned and nothing is sent. A live
upload (dry_run=False) requires both token and node (the OSF project id).
Source code in src/theoryforge/osf.py
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 | |
Literature layer¶
Map the literature a theory sits in, and check what of it the theory does not yet cite.
theoryforge.read_corpus(path)
¶
Read a literature corpus from YAML or JSON.
Source code in src/theoryforge/lit.py
25 26 27 28 29 30 31 32 | |
theoryforge.litmap(corpus, min_link=DEFAULT_MIN_LINK)
¶
Keyword co-occurrence, thematic components, and co-citation, all deterministic.
Source code in src/theoryforge/lit.py
87 88 89 90 91 92 93 94 95 96 97 98 99 | |
theoryforge.landscape(theory, corpus, min_link=DEFAULT_MIN_LINK)
¶
Map a theory and its registered alternatives onto the literature's themes.
Source code in src/theoryforge/lit.py
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 | |
theoryforge.fetch_corpus(query, per_page=25, mailto=None)
¶
Build a corpus from the OpenAlex API (network call).
This adapter is assistive. It depends on a live external service whose results
change over time, so it sits outside the package's deterministic core.
per_page must be between 1 and 200, the range OpenAlex accepts.
Source code in src/theoryforge/lit.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
theoryforge.new_evidence_dois(theory, candidate_dois)
¶
DOIs in candidate_dois not already cited by the theory's evidence or alternatives.
Compares by normalised form (lowercased, with any doi.org/dx.doi.org URL prefix stripped), so a fresh literature search, for example via OpenAlex, Scopus, or any other source, can be checked against what the theory already engages with. Returns the qualifying DOIs in their original form, deduplicated and sorted by normalised form. Deterministic and takes no network dependency: the search itself is left to whichever literature tool the caller prefers.
Source code in src/theoryforge/lit.py
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 240 | |