Skip to contents

spharmlithic is an R package for the quantitative analysis of 3D stone artefacts. It supports two complementary lines of inquiry: artefact morphology (overall 3D shape from mesh surfaces) and flaking scar patterns (orientation and organisation of flaking removals). The package brings together two coordinate-alignment pipelines, descriptive statistics (e.g. SPI, Elongation/Isotropy), interactive 3D visualisation, and a Python back-end for spherical harmonic decomposition — all in a single, script-based workflow.

Version 0.2.0 adds scar pattern measures that can be compared between cores with different scar counts (see Scar pattern indices below).

Documentation: https://peiyuanxiao.github.io/spharmlithic/


📦 Installation

Option A — Install from R

# Step 1: Install the R package
# install.packages("remotes")
remotes::install_github("PeiyuanXiao/spharmlithic")

# Step 2: Install the Python back-end (only needed for spherical harmonics)
library(spharmlithic)
install_spharmlithic_python()              # scar pattern analysis
install_spharmlithic_python(mesh = TRUE)   # also enables morphological analysis

On subsequent R sessions, activate the environment with:

use_spharmlithic_python("r-spharmlithic")

Note: A working conda installation is required. If conda is not on your system PATH, add Sys.setenv(RETICULATE_CONDA = "path/to/conda") to your ~/.Rprofile.

A pre-built Docker image includes R, RStudio Server, and the full Python environment. On macOS the direction-vector pipeline works in a native installation, but spharm_from_meshes() does not: the OpenMP runtime bundled with open3d clashes with the one that comes with R, which aborts the R session with OMP: Error #15. Use Docker there.

docker pull peiyuanxiao/spharmlithic
docker run -d -p 8787:8787 \
  -v /path/to/your/data:/home/rstudio/data \
  peiyuanxiao/spharmlithic

Open http://localhost:8787 in your browser (user: rstudio, password: rstudio). Example data and a quick-start script are pre-loaded in ~/examples/.


🚀 Quick Start

library(spharmlithic)
library(readxl)

# ── Scar pattern indices (no Python needed) ───────────────
scar_path <- system.file("extdata", "example_scars.xlsx",
                         package = "spharmlithic")
scars <- read_excel(scar_path)

# Raw and rarefied SPI, ER and IR, with random intervals
spi_ei_batch(scars)

# Rarefied SP-SPHARM features (pairs of different scars)
spharm_rarefied(scars)

# CPI and PRA also need the outline of each core: see ?cpi_pra_batch.
# Group comparisons: see ?compare_indices and ?compare_groups.
library(spharmlithic)
library(readxl)
use_spharmlithic_python("r-spharmlithic")

# ── Scar pattern analysis ─────────────────────────────────
scar_path <- system.file("extdata", "example_scars.xlsx",
                          package = "spharmlithic")
raw_data  <- read_excel(scar_path)
aligned   <- align_scar_batch(raw_data)

compute_spi(aligned$d_x, aligned$d_y, aligned$d_z)
compute_ei(aligned$d_x, aligned$d_y, aligned$d_z)

sh_scar <- spharm_from_directions(aligned, lmax = 20)

# ── Morphological analysis ────────────────────────────────
stl_dir  <- system.file("extdata", "meshes", package = "spharmlithic")
sh_morph <- spharm_from_meshes(stl_dir, lmax = 20)

# ── Interactive viewer ────────────────────────────────────
export_spharm_html(morph = sh_morph, scar = sh_scar,
                   out_path = "spharm_viewer.html")

🔧 Function Reference

Alignment

Function Description
align_scar_batch() SVD three-step alignment of scar orientation vectors
align_morph_batch() Two-step alignment following Lin et al. (2024)

Descriptive Statistics

Function Description
compute_spi() Scar Pattern Index (length-weighted: Clarkson et al., 2006; non-weighted: Bretzke & Conard, 2012)
compute_spi_angle() Angular conversion of SPI (θ = arccos(SPI))
compute_ei() Elongation and Isotropy ratio from Lin et al. (2024)
get_scar_length() Individual scar lengths from coordinate data

Scar pattern indices

Function Description
check_scars() Check a scar table before analysis
spi_ei_batch() Raw and rarefied SPI, ER and IR for every core, with random intervals
random_interval() Random interval of SPI, ER or IR at a scar count
plot_random_interval() Plot raw index values against their random interval
read_outline_iges() Read core outlines from IGES files
fit_intersection_plane() Fit the plane of intersection of a core to its outline
cpi_pra_batch() CPI and PRA for every peripherally exploited core
cpi_pra_per_scar() CPI and PRA of every scar of one core
spharm_rarefied() Rarefied SP-SPHARM features of every core
compare_indices() Compare groups of cores on single indices
compare_groups() Compare the overall scar patterns of groups of cores

spharm_from_directions() describes the scar pattern of each core as recorded, in the same spherical harmonic framework as core morphology (Xiao et al., 2027). Because each scar is also paired with itself in this power spectrum, its expected shape changes with scar count. To compare groups of cores whose scar counts differ, spharm_rarefied() computes rarefied SP-SPHARM from pairs of different scars only and adds scar positions (Xiao et al., submitted): features = "dir" gives the direction-only version (6 features), and the default the full version (22 features).

Spherical Harmonic Analysis

Function Description
spharm_from_directions() SH coefficients from scar orientation vectors
spharm_from_meshes() SH coefficients from 3D mesh surfaces
spharm_to_dataframe() Convert results to a wide-format data frame

Export & Visualisation

Function Description
export_spharm_html() Interactive Three.js viewer for SH reconstructions
export_alignment_html_svd() SVD alignment as self-contained HTML
export_alignment_html_lin2024() Lin et al (2024) alignment as self-contained HTML
plot_core() Plot one core in 3-D with its outline
export_outline_html() Export every core and its outline to an interactive HTML page

Python Environment

Function Description
install_spharmlithic_python() Create the conda environment
use_spharmlithic_python() Activate an existing environment

For detailed usage, see the two vignettes: vignette("Introduction", package = "spharmlithic") (from artefacts to spherical harmonic coefficients) and vignette("Statistics", package = "spharmlithic") (PCA, PERMANOVA, and tests of standardisation on the results). Examples for the scar pattern indices are in their help pages.


📖 References

Bretzke, K., & Conard, N. J. (2012). Evaluating morphological variability in lithic assemblages using 3D models of stone artifacts. Journal of Archaeological Science, 39(12), 3741–3749. https://doi.org/10.1016/j.jas.2012.06.039

Clarkson, C., Vinicius, L., & Lahr, M. M. (2006). Quantifying flake scar patterning on cores using 3D recording techniques. Journal of Archaeological Science, 33(1), 132–142. https://doi.org/10.1016/j.jas.2005.07.007

Hamidi, B., Wallace, K., Vasu, C., & Alekseyenko, A. V. (2019). Wd*-test: robust distance-based multivariate analysis of variance. Microbiome, 7, 51. https://doi.org/10.1186/s40168-019-0659-9

Lin, S. C., Clarkson, C., Julianto, I. M. A., Ferdianto, A., Jatmiko, & Sutikna, T. (2024). A new method for quantifying flake scar organisation on cores using orientation statistics. Journal of Archaeological Science, 167, 105998. https://doi.org/10.1016/j.jas.2024.105998

McPherron, S. P. (2018). Additional statistical and graphical methods for analyzing site formation processes using artifact orientations. PLoS ONE, 13(1), e0190195. https://doi.org/10.1371/journal.pone.0190195

Wieczorek, M. A., & Meschede, M. (2018). SHTools: Tools for working with spherical harmonics. Geochemistry, Geophysics, Geosystems, 19(8), 2574–2592. https://doi.org/10.1029/2018GC007529

Xiao, P. Y., Li, H., & Marwick, B. (2027). Characterizing core morphology and scar patterning within a unified spherical harmonic framework. Journal of Archaeological Method and Theory, 34, 16. https://doi.org/10.1007/s10816-026-09828-7

Ye, Z., Pei, S. W., Ma, D. D., Li, H., & Marwick, B. (2026). Spherical harmonic analysis of faceted spheroids identifies shaping strategies and standardisation at Qianshangying (North China). Journal of Archaeological Science, 190, 106551. https://doi.org/10.1016/j.jas.2026.106551


📝 Citation

If you use spharmlithic in your research, please cite it as:

Xiao, P. Y., Marwick, B. (2026). spharmlithic: Spherical Harmonic Analysis of Lithic Morphology and Flaking Scar Patterns. R package. https://github.com/PeiyuanXiao/spharmlithic

If you use the scar pattern indices, please also cite:

Xiao, P.-Y., Li, H., Marwick, B. (submitted). Counting what counts: Measuring centripetality and controlling for scar count in core scar pattern analysis. Journal of Archaeological Science.


⚖️ License

This project is licensed under the MIT License — see the LICENSE file for details.