def compareReconstructedWorms(cl, eigenworms, avTrue, thetaTrue, pc3, avP, tP, tPnew): pcsNew, meanAngle, lengths, refPoint = dh.calculateEigenwormsFromCL( cl, eigenworms) pc3New, pc2New, pc1New = pcsNew cl2 = dh.calculateCLfromEW(pcsNew, eigenworms, meanAngle, lengths, refPoint) cl = cl[:, :-1, :] cl = ndimage.gaussian_filter1d(cl, 5, 1) # transform eigenworms exactly the same way. Otherwise we get some artefacts from nans r = (pcsNew[2]**2 + pcsNew[1]**2) r = np.repeat(np.median(r), len(r)) #lengths = 5 #=============================================================================# # here we reconstruct from the true angular velocity to check the math. This is smoothed, so we need to compare with this version #=============================================================================# xt, yt, zt = dh.recrWorm(avTrue, pc3, thetaTrue, r=r, show=False) pcsR = np.vstack([zt, yt, xt]) clApprox = dh.calculateCLfromEW(pcsR, eigenworms, meanAngle, lengths, refPoint) #=============================================================================# # reconstruct worm from predicted angular velocity. #=============================================================================# xP, yP, zP = dh.recrWorm(avP, tP, thetaTrue, r=r) pcsP = np.vstack([zP, yP, xP]) clPred = dh.calculateCLfromEW(pcsP, eigenworms, meanAngle, lengths, refPoint) #=============================================================================# # reconstruct worm from non-linear corrected turns #=============================================================================# xP, yP, zP = dh.recrWorm(avP, tPnew, thetaTrue, r=r) pcsP = np.vstack([zP, yP, xP]) cl3 = dh.calculateCLfromEW(pcsP, eigenworms, meanAngle, lengths, refPoint) #=============================================================================# # reconstruct worm from non-linear corrected turns #=============================================================================# # center around midpoint originalCMS = np.tile(np.mean(cl2, axis=1)[:, np.newaxis, :], (1, 99, 1)) clApprox -= originalCMS cl -= originalCMS cl2 -= originalCMS clPred -= originalCMS cl3 -= originalCMS return cl, cl2, clPred, cl3
cl = moving['CL'] #=============================================================================# # for debugging recreate an existing, approximated shape from 3 eigenworms #=============================================================================# pcsNew, meanAngle, lengths, refPoint = dh.calculateEigenwormsFromCL( cl, eigenworms) pc3New, pc2New, pc1New = pcsNew cl2 = dh.calculateCLfromEW(pcsNew, eigenworms, meanAngle, lengths, refPoint) # transform eigenworms exactly the same way. Otherwise we get some artefacts from nans r = (pcsNew[2]**2 + pcsNew[1]**2) r = np.repeat(np.mean((pcsNew[2]**2 + pcsNew[1]**2)), len(pcsNew[0])) #=============================================================================# # here we reconstruct from the true angular velocity to check the math. This is smoothed, so we need to compare with this version #=============================================================================# xt, yt, zt = dh.recrWorm(avTrue, pc3, thetaTrue, r=r, show=False) pcsR = np.vstack([zt, yt, xt]) clApprox = dh.calculateCLfromEW(pcsR, eigenworms, meanAngle, lengths, refPoint) #=============================================================================# # load predicted worm #=============================================================================# avP = movingAnalysis['ElasticNet']['AngleVelocity']['output'][:len(pcs[0])] tP = movingAnalysis['ElasticNet']['Eigenworm3']['output'][:len(pcs[0])] print 'R2' print movingAnalysis['ElasticNet']['AngleVelocity']['score'], movingAnalysis[ 'ElasticNet']['AngleVelocity']['scorepredicted'] print movingAnalysis['ElasticNet']['Eigenworm3']['score'], movingAnalysis[ 'ElasticNet']['Eigenworm3']['scorepredicted'] ################################################