The HTFA global template

Simulate three subjects whose factor centers are jittered copies of a shared template (htfa.make_htfa_data()), fit htfa.HTFA directly, wrap the fitted model in htfa.HTFAResults and draw the estimated template (black) together with each subject’s own centers (colors) using htfa.HTFAResults.plot_nodes(). The printed numbers come from the code below.

HTFA template (black) and per-subject centers (colors)
fit time: 0.8 s over 3 subjects
converged: True, global iterations: 9
template center error per factor (grid units): [0.355 0.506 0.669 0.782]
subject 0: mean center error 0.017
subject 1: mean center error 0.022
subject 2: mean center error 0.023

import time

import matplotlib.pyplot as plt
import numpy as np

import htfa
from htfa.synthetic import match_factors

Xs, Rs, truth = htfa.make_htfa_data(
    n_subjects=3, grid_shape=(24, 24), n_samples=40, K=4, random_state=0
)

start = time.perf_counter()
model = htfa.HTFA(K=4, standardize=False, max_global_iter=15, random_state=0)
model.fit(Xs, Rs)
res = htfa.HTFAResults(model, htfa.load(Xs, coords=Rs))
elapsed = time.perf_counter() - start
print(f"fit time: {elapsed:.1f} s over {len(res.subjects)} subjects")
print(f"converged: {model.converged_}, global iterations: {model.n_iter_}")

perm = match_factors(truth["global_centers"], res.global_centers)
err = np.linalg.norm(res.global_centers[perm] - truth["global_centers"], axis=1)
print("template center error per factor (grid units):", np.round(err, 3))
for s in range(len(res.subjects)):
    p = match_factors(truth["centers"][s], res.centers[s])
    e = np.linalg.norm(res.centers[s][p] - truth["centers"][s], axis=1).mean()
    print(f"subject {s}: mean center error {e:.3f}")

fig = res.plot_nodes("global")
ax = fig.axes[0]
ax.set_title("HTFA template (black) and per-subject centers (colors)")
ax.set_xlabel("column coordinate")
ax.set_ylabel("row coordinate")
plt.show()

Total running time of the script: (0 minutes 0.874 seconds)

Gallery generated by Sphinx-Gallery