PySemForge
PySemForge is the name of the software; on PyPI and in Python code it is the lower-case
pysemforge(pip install pysemforge,import pysemforge).
Structural Equation Modeling for Python — closing the R-Python gap for behavioral and econometric modeling.
A Python-native SEM/CFA package inspired by lavaan (syntax) and semTools (multigroup + measurement invariance), with a graphviz-free path diagram backend.
Why PySemForge?
Python has mature estimation backends (e.g. semopy), but still lacks a single package that combines:
| Capability | R (lavaan + semTools) | Python (semopy / pySEM) | PySemForge |
|---|---|---|---|
| lavaan-style syntax | ✅ | partial | ✅ |
| CFA / path / mediation | ✅ | partial | ✅ |
| Multigroup analysis (measurement models) | ✅ native | ❌ not native | ✅ native |
| Measurement invariance (configural → metric → scalar) | ✅ semTools |
❌ missing | ✅ one-liner |
| Partial invariance | ✅ semTools |
❌ | ✅ (tear-down) |
| Ordinal / WLSMV | ✅ | limited | — planned |
| Robust ML (MLR) + fit-index CIs | ✅ | partial | — planned |
| Path diagrams (no graphviz, publication-ready) | lavaanPlot |
⚠️ needs graphviz | ✅ matplotlib backend |
The two highlighted rows are the real gaps this package targets — so you stay in Python end-to-end.
Quick Start
import pandas as pd
from pysemforge import Model
# 1) lavaan-compatible syntax
syntax = """
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
"""
model = Model(syntax)
result = model.fit(data) # a pandas DataFrame of your indicators
print(result.summary())
# 2) Path diagram (no graphviz needed)
model.plot("sem_path.png", standardized=True)
# 3) Multigroup + measurement invariance (the headline feature)
inv = model.measurement_invariance(data, group_col="school")
print(inv.summary()) # configural -> metric -> scalar, with dCFI/dRMSEA
Install
pip install pysemforge
This pulls in NumPy, SciPy, pandas, Matplotlib and semopy (the estimation backend). No Graphviz installation is needed: path diagrams are drawn with Matplotlib.
To check the installation and reproduce the validation numbers from the paper:
import pysemforge
from pysemforge import Model
from pysemforge.datasets import load_holzinger_swineford, HOLZINGER_SWINFORD_MODEL
data = load_holzinger_swineford()
model = Model(HOLZINGER_SWINFORD_MODEL)
print(model.fit(data).summary()) # chi2 = 85.306, df = 24
print(model.measurement_invariance(data, "school").summary())
For the latest development version:
git clone https://github.com/demon528/pysemforge.git
cd pysemforge
pip install -e .
Features
- lavaan-style syntax —
=~(measurement),~(regression),~~(co)variances, comments; statements may be separated by newlines or semicolons - CFA and path models through lavaan-style syntax (estimation by semopy)
- Estimator — normal-theory ML; robust ML and ordinal/WLSMV are on the roadmap
- Native multigroup estimation for measurement models (joint ML over the stacked groups)
- Measurement invariance — fully automated
configural → metric → scalarladder with Δχ² / ΔCFI / ΔRMSEA decisions (RsemToolsparity) - Partial invariance — releases the constraints with the largest modification indices and reports the group difference in each released parameter
- Fit indices — χ², CFI, TLI, RMSEA (+90% CI), SRMR
- graphviz-free diagrams — publication-quality path plots via matplotlib (no system dependencies)
- pandas / NumPy / SciPy first-class interoperability
Current scope
Estimation is normal-theory ML. Robust ML (MLR), ordinal/WLSMV, latent growth
models and Bayesian estimation are not implemented yet and are listed as
planned work. Multigroup estimation covers measurement models (CFA): path
models with observed-variable regressions raise NotImplementedError instead
of returning a silently wrong answer. The syntax is lavaan-style but not
lavaan-complete — parameter labels (a*x1), intercept statements (x1 ~ 1)
and equality constraints written in the model string are not supported; in the
multigroup estimator, cross-group equality is imposed by the model level
(level= / release=), not in the syntax.
Measurement Invariance Example
inv = model.measurement_invariance(data, group_col="group")
# Access each level's fit
inv.levels["configural"]
inv.levels["metric"]
inv.levels["scalar"]
# Pairwise comparisons (Δχ², ΔCFI, decision)
inv.comparisons
The returned object mirrors semTools::measurementInvariance() output, so R users feel at home.
Documentation & Publishing
- Source: https://github.com/demon528/pysemforge
- API reference: docstrings, plus
docs/in the repository - Issues: https://github.com/demon528/pysemforge/issues
Publish to PyPI (main release target):
pip install build twine
python -m build
twine check dist/*
twine upload --repository testpypi dist/* # test first
twine upload dist/* # then production
Roadmap (post-MVP)
Latent growth curve models · Bayesian estimation · complex survey designs · full lavaanPlot-parity plotting.
License
MIT — see LICENSE.txt.
Metadata
Release files for pysemforge 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pysemforge-0.2.0.tar.gz | 58.6 kB | Without provenance | Details for pysemforge-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pysemforge-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for pysemforge-0.2.0-py3-none-any.whl |
Total release size: 104.8 kB
Release files / pysemforge-0.2.0.tar.gz
| Download URL | pysemforge-0.2.0.tar.gz |
|---|---|
| Size | 58.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08db9e54ea7dc03faa9e3e3d2caf5cfb717a3ce9c5196cd73c0d0ca2599a28ae
|
|
BLAKE2b-256 checksum How to use checksums |
c25cf05cadaa69f32289168f96efbae3e617ec2744b2c3dc104c39ad294ac386
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.8
|
Release files / pysemforge-0.2.0-py3-none-any.whl
| Download URL | pysemforge-0.2.0-py3-none-any.whl |
|---|---|
| Size | 46.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3c816b038522c10958e71ed0e5d7a7b1c5fbfc8303ba9d7d59b7ecdf07dbe53b
|
|
BLAKE2b-256 checksum How to use checksums |
a5ace848adaa8420a0782849a9642cb8e1dcd6ec70a9eb3a6af2d2bf629d877a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.8
|
Security for pysemforge 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.