markovmodel / PyEMMA

🚂 Python API for Emma's Markov Model Algorithms 🚂
http://pyemma.org
GNU Lesser General Public License v3.0
311 stars 119 forks source link

using radius of gyrationn in the custom featurizer #1552

Closed instaM closed 2 years ago

instaM commented 2 years ago

I have been looking for a way to include the radius of gyratiion ini my TICA analyse I came up wth

rg_feature=pyemma.coordinates.data.CustomFeature(lambda x: mdtraj.compute_rg(x), dim=1) rg_feat=pyemma.coordinates.featurizer(topologyFile) rg_feat.add_custom_feature(rg_feature) rf_feat_data = pyemma.coordinates.load(trajectoryFiles, features=rg_feat)

the error I get is TypeError: Cannot cast array data from dtype('float64') to dtype('float32') according to the rule 'safe'

so I changed the first line to rg_feature=pyemma.coordinates.data.CustomFeature(lambda x : mdtraj.compute_rg(x).astype('float32'), dim=1) and end up with ValueError: Your custom feature ['CustomFeature[13][0] calling <function at 0x7f9df085b040> with args {args}'] did not return a 2d array. Shape was (20001,)

Why is the functiion expecting a 2D array with dim=1 ?

Thank you very much for helpng me. At that stage do not know if t iis a bug or a misuse of the custom_feturizer, all I could fnd where transformatins on the trajectories coordinates in the dverse tutorals.

clonker commented 2 years ago

Hey this is expected, for consistency reasons we require the nd arrays to be two-dimensional with the first axis being time and the second axis being feature dimensions. so what you can do is

rg_feature=pyemma.coordinates.data.CustomFeature(lambda x : mdtraj.compute_rg(x).astype('float32')[:, None], dim=1)

Let me know if that worked :slightly_smiling_face:

instaM commented 2 years ago

Thank you so much @clonker !! It works :-) It was actually "kind of" in the documentation, sorry for that. I really enjoy using pyemma, but the documentation is a bit cryptic at times and I have to rely a lot on on the tutorials which are in counterpart excellent. Keep up the great work

clonker commented 2 years ago

Cheers, glad it worked! Yeah the documentation could use some improvement. On the other hand we are transition toward deeptime, at least from the algorithm side; pyemma is going to remain for featurizing data and the analysis part being carried out with the other package. Depending on what you are doing it might be worth a look, too.

instaM commented 2 years ago

Good to know I'll definitely have a look :-)