Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ SegTraQ documentation
modules/clustering_stability
modules/nuclear_correlation
modules/spillover_metrics
modules/threedimensional_metrics

.. toctree::
:maxdepth: 1
Expand Down
9 changes: 9 additions & 0 deletions docs/modules/theedimensional_metrics.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. highlight:: shell

===================================================
The three-dimensional metrics (:code:`sp`) accessor
===================================================

The three-dimensional metrics accessor provides metrics to quantify the distribution of transcripts along the $z$-axis
.. automodule:: segtraq.tm.threedimensional_metrics
:members:
4 changes: 2 additions & 2 deletions src/segtraq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
__author__ = """Daria Lazic, Matthias Meyer-Bender, Martin Emons"""
__email__ = "daria.lazic@embl.de, matthias.meyerbender@embl.de, martin.emons@uzh.ch"

from . import bl, cs, nc, sp
from . import bl, cs, nc, sp, tm

__all__ = ["bl", "cs", "nc", "sp"]
__all__ = ["bl", "cs", "nc", "sp", "tm"]
7 changes: 7 additions & 0 deletions src/segtraq/tm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .threedimensional_metrics import (
shape_metrics,
)

__all__ = [
"shape_metrics",
]
36 changes: 36 additions & 0 deletions src/segtraq/tm/threedimensional_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import numpy as np
import pandas as pd
import spatialdata as sd
from scipy.stats import kurtosis, skew

def shape_metrics(
sdata: sd.SpatialData,
transcript_key: str = "transcripts",
z_coordinate: str = "z"
) -> pd.DataFrame:

"""
Calculates shape statistics (mean, variance, skew, kurtosis) based on the first four moments of the distribution

Parameters
----------
sdata : sd.SpatialData
The SpatialData object containing spatial transcriptomics data.
transcript_key : str, optional
The key in the transcript table indicating transcript identifiers. Default is "transcripts".
z_coordinate : str, optional
The coordinate name of the z coordinate


Returns
-------
pd.DataFrame
A DataFrame with keys `["mean", "variance", "skew, "kurtosis"]`
"""


z = np.array(sdata.points[transcript_key][z_coordinate])

d = {"mean" : np.mean(z), "variance" : np.var(z), "skew" : skew(z), "kurtosis" : kurtosis(z)}
df = pd.DataFrame(data = d, index = [0])
return(df)
11 changes: 11 additions & 0 deletions tests/tm/test_shape_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pandas as pd

import segtraq as st


def test_shape_metrics_structure(sdata_new):
df = st.tm.shape_metrics(sdata_new)

assert isinstance(df, pd.DataFrame), f"shape_metrics should return a DataFrame, got {type(df)}"
expected_cols = {"mean", "variance", "skew", "kurtosis"}
assert set(df.columns) == expected_cols, f"Expected columns {expected_cols}, but got {set(df.columns)}"