Skip to main content

Package to load and write morphologies with spines

Project description

morph-spines

A Python library for loading, writing, and accessing neuron morphologies with dendritic spine data from HDF5 files. It provides structured access to spine skeletons, meshes, and spatial transformations.

Quick example

Loading

from morph_spines import load_morphology_with_spines

m = load_morphology_with_spines("neuron.h5", spines_are_centered=True, load_meshes=True)

# Access spine meshes
mesh = m.spines.spine_mesh(0)
print(mesh.vertices.shape, mesh.faces.shape)

# Get only the head region of a spine
head_mesh = m.spines.spine_mesh(0, include_neck=False)

# Spine type classification
spine_type = m.spines.spine_type(0)

Writing

from morph_spines import write_spine_table, write_morphology, write_soma_mesh

# Write a spine table (pandas DataFrame with mandatory columns)
write_spine_table("output.h5", "neuron_01", spine_table_df)

# Write neuron morphology skeleton
write_morphology("output.h5", "neuron_01", points, structure)

# Write soma mesh
write_soma_mesh("output.h5", "neuron_01", vertices, triangles)

Validation

from morph_spines import validate_morph_with_spines_file

# Check file structure only (groups, datasets, metadata)
result = validate_morph_with_spines_file("neuron.h5")

# Also check data integrity (shapes, dtypes, value ranges, cross-references)
result = validate_morph_with_spines_file("neuron.h5", check_data_integrity=True)

print(result)          # Human-readable summary
assert result.is_valid # Use programmatically

### Merging

```python
from pathlib import Path
from morph_spines import merge_morphologies_with_spines

# Merge multiple files without renaming
merge_morphologies_with_spines(
    source_files=[Path("neuron_A.h5"), Path("neuron_B.h5")],
    output_path=Path("merged.h5"),
)

# Merge with renaming (neuron keys and/or spines library names)
src1 = Path("neuron_A.h5")
src2 = Path("neuron_B.h5")
merge_morphologies_with_spines(
    source_files=[src1, src2],
    output_path=Path("merged.h5"),
    rename_map={
        (src1, "morph_001"): "circuit_neuron_42",
        (src2, "morph_001"): "circuit_neuron_43",
    },
)

# Merge without meshes (smaller output)
merge_morphologies_with_spines(
    source_files=[Path("a.h5"), Path("b.h5")],
    output_path=Path("merged_no_meshes.h5"),
    include_meshes=False,
)

Installation

pip install morph-spines

For development:

git clone https://github.com/openbraininstitute/morph-spines.git
cd morph-spines
pip install -e ".[test]"

Features

  • Load and write neuron morphologies with spine data from/to HDF5 files
  • Merge multiple morph-with-spines files into one, with optional renaming of neuron keys and spines library names
  • Access the spine table with per-spine properties (position, orientation, section placement)
  • Access spine skeletons (via NeuroM/MorphIO) and meshes (via trimesh)
  • Write spine tables, morphologies, soma meshes, spine meshes, and spine skeletons
  • Validate spine tables against the format specification before writing
  • Validate entire morph-with-spines files (structure and optionally data integrity)
  • Head/neck triangle classification with filtering (include_head, include_neck)
  • Support for branched spines with multiple heads
  • Spine type classification (thin, mushroom, stubby, filopodium, branched, etc.)
  • Lazy or eager mesh loading
  • Coordinate transformations between local spine and global neuron frames

Upgrading from v0.x

Version 1.0 drops support for reading spine tables stored as pandas DataFrames (v0.1 format) inside HDF5 files. If you have files in the old format, convert them before loading:

python scripts/h5_dataframe_to_h5_datasets_group.py old_file.h5 new_file.h5

The conversion script requires the tables package:

pip install morph-spines[scripts]

File format

The morphology-with-spines format is documented in examples/data/README.md.

Development

Run tests:

pytest

Lint:

ruff check src/ tests/

Type check:

mypy src/

Examples

See the examples/ folder for Jupyter notebooks demonstrating visualization and usage.

License

Copyright (c) 2025-2026 Open Brain Institute.

Licensed under Apache-2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

morph_spines-1.1.0.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

morph_spines-1.1.0-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file morph_spines-1.1.0.tar.gz.

File metadata

  • Download URL: morph_spines-1.1.0.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for morph_spines-1.1.0.tar.gz
Algorithm Hash digest
SHA256 fba112f32392775891351f34f3428c28acae287c2137b5b96d8865aacd97ef31
MD5 09f3345f7dd7b88825284841ebd6f04b
BLAKE2b-256 3c46f546554344a5cc2f918dcb463ca34f4cc11356f7c649a5c891344f427b2b

See more details on using hashes here.

Provenance

The following attestation bundles were made for morph_spines-1.1.0.tar.gz:

Publisher: publish.yml on openbraininstitute/morph-spines

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file morph_spines-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: morph_spines-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for morph_spines-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2b651c5f5bfc901c518af714c189c11b49dbc211b7c7c76eb5ba68afaf47531
MD5 8ede94f2231d05cf447da5a48414c55f
BLAKE2b-256 1f11c2453af42fbd06d10d1843c8a62030109ceade0879768a0d1c68d3347134

See more details on using hashes here.

Provenance

The following attestation bundles were made for morph_spines-1.1.0-py3-none-any.whl:

Publisher: publish.yml on openbraininstitute/morph-spines

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page