jdtuck / fdasrsf_python

elastic fda python code
http://research.tetonedge.net
BSD 3-Clause "New" or "Revised" License
52 stars 18 forks source link

Reconstruction of planar shapes from functional principal components. #48

Closed aa89113 closed 4 months ago

aa89113 commented 4 months ago

Hello, I am trying to use SRVF framework and fs.fdacurve with planar curves. I was able to compute the functional principal components, using the following code:

obj = fs.fdacurve(beta,N=M)
obj.karcher_mean(rotation=False)
obj.srvf_align(rotation=False)
obj.karcher_cov()
obj.shape_pca(no=5)

However, I am not sure how can i reconstruct the original planar functions using functional principal scores stored in obj.coef. The goal would be to reconstruct the original shapes and after compute a reconstruction error metric Can you please help?

jdtuck commented 4 months ago

You need project back on the basis stored in obj.U, that is the singular vectors. The coefficients are the scores.

aa89113 commented 4 months ago

@jdtuck many thanks for your reply. Performing

((obj.coef.T) @ (obj.U.T))  # n_curve_examples x n_points_per_curve

will project the scores obj.coef on the eigenfunctions. However, this seems to be in the q (?) domain. I am interested on reconstructing the original dataset from principal components. This would be comparable to obj.beta, but reconstructed from functional scores. My end goal would be to compare the reconstructed data w/ original dataset.

jdtuck commented 4 months ago

Correct then transform them back to curves using q_to_curve

aa89113 commented 4 months ago

@jdtuck many thanks again for your help.

I did the following, but the output does not resemble the original curve, at all ...

reconst_curves_q = ((obj.coef.T) @ (obj.U.T))  # n_curve_examples x n_points_per_curve
reconst_curve_q_first =reconst_curves_q[0].reshape(2, -1)  # 2 x n__points_per_curve
reconst_curve_first = cf.q_to_curve(reconst_curve_q_first)

My example is very similar to: https://fdasrsf-python.readthedocs.io/en/latest/curve_example.html

Can you please help me?

jdtuck commented 4 months ago

Two things the PCA is done on a tangent space to the Hilbert Sphere, so you need to project back down to the sphere, then convert back to curves (q_to_curve). Have you read the corresponding papers? See the function sample_shapes for an example on how to do this.

https://github.com/jdtuck/fdasrsf_python/blob/0c5b24851895d16164f94eb7aca49a77b1ee17e6/fdasrsf/curve_stats.py#L360