SOFM
Spatially orthogonal factor models
Installation
Please install scikit-sparse via conda before installing this package:
conda install -c conda-forge scikit-sparse
pip install sofm
Data
Please use the spatialLIBD_to_netcdf.R script to download the DLPFC data from R and save as a netcdf file to be imported into xarray.
Code example
from sofm import SOFM
# 1. Load netcdf into xarray
# Please see `spatialLIBD_to_netcdf.R` for directions on formatting the netcdf file.
da = xr.open_dataset("/data_directory/spatial_transcriptomic_data.nc", engine='netcdf4')['logcounts']
da = da - da.mean(dim='spot') #SOFM assumes the mean structure has been subtracted
da = da.rename({ #SOFM assumes `da` has `location` dimension indexed by `lat,lon` coordinates
'spot': 'location',
'array_col': 'lon',
'array_row': 'lat'
})
da = da.set_index(location=['lat', 'lon'])
# 2. Initialize SOFM model
sofm_model = SOFM(
data=da,
n_components=5,
n_cores=-1 #please set number of cores
)
# 3. Fit model
sofm_model.fit()
# 4. Visualize spatial loadings
fig = sofm_model.plot_loadings(robust=True)
fig.show()
# 5. Analyze latent factors
latent_factors = sofm_model.Ez_
If helpful, try out the code using a synthetic dataset,
import numpy as np
import xarray as xr
from sofm import SOFM
# 1. Generate Synthetic Data
# Create a 30x30 spatial grid
lats, lons = np.meshgrid(np.linspace(-5, 5, 30), np.linspace(-5, 5, 30))
lat_flat = lats.flatten()
lon_flat = lons.flatten()
n_locations = len(lat_flat)
n_features = 20
factor1 = np.sin(lat_flat) + np.cos(lon_flat)
factor2 = np.exp(-(lat_flat**2 + lon_flat**2) / 2)
factor3 = lat_flat + lon_flat
Z = np.column_stack([factor1, factor2, factor3])
np.random.seed(42)
W = np.random.randn(3, n_features) # Synthetic loadings
data_matrix = Z @ W + np.random.randn(n_locations, n_features) * 0.5
da = xr.DataArray(
data_matrix,
dims=['location', 'feature'],
coords={
'lat': ('location', lat_flat),
'lon': ('location', lon_flat),
'feature': np.arange(n_features)
}
)
da = da.set_index(location=['lat', 'lon'])
da = da - da.mean(dim='location')
# 2. Initialize SOFM model
sofm_model = SOFM(
data=da,
n_components=3,
n_cores=-1 #please set number of cores
)
# 3. Fit model
sofm_model.fit()
# 4. Visualize spatial loadings
fig = sofm_model.plot_loadings(robust=True)
fig.show()
# 5. Analyze latent factors
latent_factors = sofm_model.Ez_
Metadata
Release files for sofm 0.1.6
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 |
|---|---|---|---|---|
| sofm-0.1.6.tar.gz | 19.3 kB | Without provenance | Details for sofm-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sofm-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for sofm-0.1.6-py3-none-any.whl |
Total release size: 39.7 kB
Release files / sofm-0.1.6.tar.gz
| Download URL | sofm-0.1.6.tar.gz |
|---|---|
| Size | 19.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4d0892bb75ee68b3227444313b6eff54fea8d134df67a1c432bf3f4f8bc87452
|
|
BLAKE2b-256 checksum How to use checksums |
26df57578e504f754953c3d3818c4913ba8be7fb54e99daa5834476ed0340599
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Release files / sofm-0.1.6-py3-none-any.whl
| Download URL | sofm-0.1.6-py3-none-any.whl |
|---|---|
| Size | 20.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dd4f715b9ce822dfd0a58f7396c8a4a1b61c6a480937c053b1bca9e41b32792b
|
|
BLAKE2b-256 checksum How to use checksums |
bbef00da9f93972d23d8cccb7f6e4464ed4fc411c89e6e14adbd583f21b80046
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.5
|
Security for sofm 0.1.6
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.